| 1 | /* Compute sine and cosine of argument. |
| 2 | Copyright (C) 1997-2020 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <https://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <math.h> |
| 22 | |
| 23 | #include <math_private.h> |
| 24 | #include <fenv_private.h> |
| 25 | #include <math-underflow.h> |
| 26 | #include <libm-alias-double.h> |
| 27 | |
| 28 | #define IN_SINCOS |
| 29 | #include "s_sin.c" |
| 30 | |
| 31 | void |
| 32 | __sincos (double x, double *sinx, double *cosx) |
| 33 | { |
| 34 | mynumber u; |
| 35 | int k; |
| 36 | |
| 37 | SET_RESTORE_ROUND_53BIT (FE_TONEAREST); |
| 38 | |
| 39 | u.x = x; |
| 40 | k = u.i[HIGH_HALF] & 0x7fffffff; |
| 41 | |
| 42 | if (k < 0x400368fd) |
| 43 | { |
| 44 | double a, da, y; |
| 45 | /* |x| < 2^-27 => cos (x) = 1, sin (x) = x. */ |
| 46 | if (k < 0x3e400000) |
| 47 | { |
| 48 | if (k < 0x3e500000) |
| 49 | math_check_force_underflow (x); |
| 50 | *sinx = x; |
| 51 | *cosx = 1.0; |
| 52 | return; |
| 53 | } |
| 54 | /* |x| < 0.855469. */ |
| 55 | else if (k < 0x3feb6000) |
| 56 | { |
| 57 | *sinx = do_sin (x, 0); |
| 58 | *cosx = do_cos (x, 0); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | /* |x| < 2.426265. */ |
| 63 | y = hp0 - fabs (x); |
| 64 | a = y + hp1; |
| 65 | da = (y - a) + hp1; |
| 66 | *sinx = copysign (do_cos (a, da), x); |
| 67 | *cosx = do_sin (a, da); |
| 68 | return; |
| 69 | } |
| 70 | /* |x| < 2^1024. */ |
| 71 | if (k < 0x7ff00000) |
| 72 | { |
| 73 | double a, da, xx; |
| 74 | unsigned int n; |
| 75 | |
| 76 | /* If |x| < 105414350 use simple range reduction. */ |
| 77 | n = k < 0x419921FB ? reduce_sincos (x, &a, &da) : __branred (x, &a, &da); |
| 78 | n = n & 3; |
| 79 | |
| 80 | if (n == 1 || n == 2) |
| 81 | { |
| 82 | a = -a; |
| 83 | da = -da; |
| 84 | } |
| 85 | |
| 86 | if (n & 1) |
| 87 | { |
| 88 | double *temp = cosx; |
| 89 | cosx = sinx; |
| 90 | sinx = temp; |
| 91 | } |
| 92 | |
| 93 | *sinx = do_sin (a, da); |
| 94 | xx = do_cos (a, da); |
| 95 | *cosx = (n & 2) ? -xx : xx; |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if (isinf (x)) |
| 100 | __set_errno (EDOM); |
| 101 | |
| 102 | *sinx = *cosx = x / x; |
| 103 | } |
| 104 | libm_alias_double (__sincos, sincos) |
| 105 | |