math: sin cos cleanup
[musl] / src / math / sincos.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_sin.c */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12
13 #include "libm.h"
14
15 void sincos(double x, double *sin, double *cos)
16 {
17         double y[2], s, c;
18         uint32_t ix;
19         unsigned n;
20
21         GET_HIGH_WORD(ix, x);
22         ix &= 0x7fffffff;
23
24         /* |x| ~< pi/4 */
25         if (ix <= 0x3fe921fb) {
26                 /* if |x| < 2**-27 * sqrt(2) */
27                 if (ix < 0x3e46a09e) {
28                         /* raise inexact if x!=0 and underflow if subnormal */
29                         FORCE_EVAL(ix < 0x00100000 ? x/0x1p120f : x+0x1p120f);
30                         *sin = x;
31                         *cos = 1.0;
32                         return;
33                 }
34                 *sin = __sin(x, 0.0, 0);
35                 *cos = __cos(x, 0.0);
36                 return;
37         }
38
39         /* sincos(Inf or NaN) is NaN */
40         if (ix >= 0x7ff00000) {
41                 *sin = *cos = x - x;
42                 return;
43         }
44
45         /* argument reduction needed */
46         n = __rem_pio2(x, y);
47         s = __sin(y[0], y[1], 1);
48         c = __cos(y[0], y[1]);
49         switch (n&3) {
50         case 0:
51                 *sin = s;
52                 *cos = c;
53                 break;
54         case 1:
55                 *sin = c;
56                 *cos = -s;
57                 break;
58         case 2:
59                 *sin = -s;
60                 *cos = -c;
61                 break;
62         case 3:
63         default:
64                 *sin = -c;
65                 *cos = s;
66                 break;
67         }
68 }