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