Elliott Hughes | e332f65 | 2018-05-08 15:07:43 -0700 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2007, 2010-2013 Steven G. Kargl |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice unmodified, this list of conditions, and the following |
| 10 | * disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | * |
| 26 | * s_sinl.c and s_cosl.c merged by Steven G. Kargl. |
| 27 | */ |
| 28 | |
| 29 | #include <sys/cdefs.h> |
Elliott Hughes | bac0ebb | 2021-01-26 14:17:20 -0800 | [diff] [blame] | 30 | __FBSDID("$FreeBSD$"); |
Elliott Hughes | e332f65 | 2018-05-08 15:07:43 -0700 | [diff] [blame] | 31 | |
| 32 | #include <float.h> |
| 33 | #ifdef __i386__ |
| 34 | #include <ieeefp.h> |
| 35 | #endif |
| 36 | |
| 37 | #include "math.h" |
| 38 | #include "math_private.h" |
| 39 | #include "k_sincosl.h" |
| 40 | |
| 41 | #if LDBL_MANT_DIG == 64 |
| 42 | #include "../ld80/e_rem_pio2l.h" |
| 43 | #elif LDBL_MANT_DIG == 113 |
| 44 | #include "../ld128/e_rem_pio2l.h" |
| 45 | #else |
| 46 | #error "Unsupported long double format" |
| 47 | #endif |
| 48 | |
| 49 | void |
| 50 | sincosl(long double x, long double *sn, long double *cs) |
| 51 | { |
| 52 | union IEEEl2bits z; |
Elliott Hughes | 99ef447 | 2022-01-12 17:51:20 -0800 | [diff] [blame^] | 53 | int e0; |
Elliott Hughes | e332f65 | 2018-05-08 15:07:43 -0700 | [diff] [blame] | 54 | long double y[2]; |
| 55 | |
| 56 | z.e = x; |
Elliott Hughes | e332f65 | 2018-05-08 15:07:43 -0700 | [diff] [blame] | 57 | z.bits.sign = 0; |
| 58 | |
| 59 | ENTERV(); |
| 60 | |
| 61 | /* Optimize the case where x is already within range. */ |
| 62 | if (z.e < M_PI_4) { |
| 63 | /* |
| 64 | * If x = +-0 or x is a subnormal number, then sin(x) = x and |
| 65 | * cos(x) = 1. |
| 66 | */ |
| 67 | if (z.bits.exp == 0) { |
| 68 | *sn = x; |
| 69 | *cs = 1; |
| 70 | } else |
| 71 | __kernel_sincosl(x, 0, 0, sn, cs); |
| 72 | RETURNV(); |
| 73 | } |
| 74 | |
| 75 | /* If x = NaN or Inf, then sin(x) and cos(x) are NaN. */ |
| 76 | if (z.bits.exp == 32767) { |
| 77 | *sn = x - x; |
| 78 | *cs = x - x; |
| 79 | RETURNV(); |
| 80 | } |
| 81 | |
| 82 | /* Range reduction. */ |
| 83 | e0 = __ieee754_rem_pio2l(x, y); |
| 84 | |
| 85 | switch (e0 & 3) { |
| 86 | case 0: |
| 87 | __kernel_sincosl(y[0], y[1], 1, sn, cs); |
| 88 | break; |
| 89 | case 1: |
| 90 | __kernel_sincosl(y[0], y[1], 1, cs, sn); |
| 91 | *cs = -*cs; |
| 92 | break; |
| 93 | case 2: |
| 94 | __kernel_sincosl(y[0], y[1], 1, sn, cs); |
| 95 | *sn = -*sn; |
| 96 | *cs = -*cs; |
| 97 | break; |
| 98 | default: |
| 99 | __kernel_sincosl(y[0], y[1], 1, cs, sn); |
| 100 | *sn = -*sn; |
| 101 | } |
| 102 | |
| 103 | RETURNV(); |
| 104 | } |