fede5f23ffbeec93733636db6d6e51753cec95d8
[musl] / src / math / sincosf.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_sinf.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4  * Optimized by Bruce D. Evans.
5  */
6 /*
7  * ====================================================
8  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9  *
10  * Developed at SunPro, a Sun Microsystems, Inc. business.
11  * Permission to use, copy, modify, and distribute this
12  * software is freely granted, provided that this notice
13  * is preserved.
14  * ====================================================
15  */
16
17 #include "libm.h"
18
19 /* Small multiples of pi/2 rounded to double precision. */
20 static const double
21 s1pio2 = 1*M_PI_2, /* 0x3FF921FB, 0x54442D18 */
22 s2pio2 = 2*M_PI_2, /* 0x400921FB, 0x54442D18 */
23 s3pio2 = 3*M_PI_2, /* 0x4012D97C, 0x7F3321D2 */
24 s4pio2 = 4*M_PI_2; /* 0x401921FB, 0x54442D18 */
25
26 void sincosf(float x, float *sin, float *cos)
27 {
28         double y, s, c;
29         uint32_t n, hx, ix;
30
31         GET_FLOAT_WORD(hx, x);
32         ix = hx & 0x7fffffff;
33
34         /* |x| ~<= pi/4 */
35         if (ix <= 0x3f490fda) {
36                 /* |x| < 2**-12 */
37                 if (ix < 0x39800000) {
38                         /* raise inexact if x != 0 */
39                         if((int)x == 0) {
40                                 *sin = x;
41                                 *cos = 1.0f;
42                         }
43                         return;
44                 }
45                 *sin = __sindf(x);
46                 *cos = __cosdf(x);
47                 return;
48         }
49
50         /* |x| ~<= 5*pi/4 */
51         if (ix <= 0x407b53d1) {  
52                 if (ix <= 0x4016cbe3) {  /* |x| ~<= 3pi/4 */
53                         if (hx < 0x80000000) {
54                                 *sin = __cosdf(x - s1pio2);
55                                 *cos = __sindf(s1pio2 - x);
56                         } else {
57                                 *sin = -__cosdf(x + s1pio2);
58                                 *cos = __sindf(x + s1pio2);
59                         }
60                         return;
61                 }
62                 *sin = __sindf(hx < 0x80000000 ? s2pio2 - x : -s2pio2 - x);
63                 *cos = -__cosdf(hx < 0x80000000 ? x - s2pio2 : x + s2pio2);
64                 return;
65         }
66
67         /* |x| ~<= 9*pi/4 */
68         if (ix <= 0x40e231d5) {
69                 if (ix <= 0x40afeddf) {  /* |x| ~<= 7*pi/4 */
70                         if (hx < 0x80000000) {
71                                 *sin = -__cosdf(x - s3pio2);
72                                 *cos = __sindf(x - s3pio2);
73                         } else {
74                                 *sin = __cosdf(x + s3pio2);
75                                 *cos = __sindf(-s3pio2 - x);
76                         }
77                         return;
78                 }
79                 *sin = __sindf(hx < 0x80000000 ? x - s4pio2 : x + s4pio2);
80                 *cos = __cosdf(hx < 0x80000000 ? x - s4pio2 : x + s4pio2);
81                 return;
82         }
83
84         /* sin(Inf or NaN) is NaN */
85         if (ix >= 0x7f800000) {
86                 *sin = *cos = x - x;
87                 return;
88         }
89
90         /* general argument reduction needed */
91         n = __rem_pio2f(x, &y);
92         s = __sindf(y);
93         c = __cosdf(y);
94         switch (n&3) {
95         case 0:
96                 *sin = s;
97                 *cos = c;
98                 break;
99         case 1:
100                 *sin = c;
101                 *cos = -s;
102                 break;
103         case 2:
104                 *sin = -s;
105                 *cos = -c;
106                 break;
107         case 3:
108         default:
109                 *sin = -c;
110                 *cos = s;
111                 break;
112         }
113 }