rework langinfo code for ABI compat and for use by time code
[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;
29         float_t s, c;
30         uint32_t ix;
31         unsigned n, sign;
32
33         GET_FLOAT_WORD(ix, x);
34         sign = ix >> 31;
35         ix &= 0x7fffffff;
36
37         /* |x| ~<= pi/4 */
38         if (ix <= 0x3f490fda) {
39                 /* |x| < 2**-12 */
40                 if (ix < 0x39800000) {
41                         /* raise inexact if x!=0 and underflow if subnormal */
42                         FORCE_EVAL(ix < 0x00100000 ? x/0x1p120f : x+0x1p120f);
43                         *sin = x;
44                         *cos = 1.0f;
45                         return;
46                 }
47                 *sin = __sindf(x);
48                 *cos = __cosdf(x);
49                 return;
50         }
51
52         /* |x| ~<= 5*pi/4 */
53         if (ix <= 0x407b53d1) {
54                 if (ix <= 0x4016cbe3) {  /* |x| ~<= 3pi/4 */
55                         if (sign) {
56                                 *sin = -__cosdf(x + s1pio2);
57                                 *cos = __sindf(x + s1pio2);
58                         } else {
59                                 *sin = __cosdf(s1pio2 - x);
60                                 *cos = __sindf(s1pio2 - x);
61                         }
62                         return;
63                 }
64                 /* -sin(x+c) is not correct if x+c could be 0: -0 vs +0 */
65                 *sin = -__sindf(sign ? x + s2pio2 : x - s2pio2);
66                 *cos = -__cosdf(sign ? x + s2pio2 : x - s2pio2);
67                 return;
68         }
69
70         /* |x| ~<= 9*pi/4 */
71         if (ix <= 0x40e231d5) {
72                 if (ix <= 0x40afeddf) {  /* |x| ~<= 7*pi/4 */
73                         if (sign) {
74                                 *sin = __cosdf(x + s3pio2);
75                                 *cos = -__sindf(x + s3pio2);
76                         } else {
77                                 *sin = -__cosdf(x - s3pio2);
78                                 *cos = __sindf(x - s3pio2);
79                         }
80                         return;
81                 }
82                 *sin = __sindf(sign ? x + s4pio2 : x - s4pio2);
83                 *cos = __cosdf(sign ? x + s4pio2 : x - s4pio2);
84                 return;
85         }
86
87         /* sin(Inf or NaN) is NaN */
88         if (ix >= 0x7f800000) {
89                 *sin = *cos = x - x;
90                 return;
91         }
92
93         /* general argument reduction needed */
94         n = __rem_pio2f(x, &y);
95         s = __sindf(y);
96         c = __cosdf(y);
97         switch (n&3) {
98         case 0:
99                 *sin = s;
100                 *cos = c;
101                 break;
102         case 1:
103                 *sin = c;
104                 *cos = -s;
105                 break;
106         case 2:
107                 *sin = -s;
108                 *cos = -c;
109                 break;
110         case 3:
111         default:
112                 *sin = -c;
113                 *cos = s;
114                 break;
115         }
116 }