xref: /freebsd/lib/msun/src/s_sincosf.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*-
2  * ====================================================
3  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4  *
5  * Developed at SunPro, a Sun Microsystems, Inc. business.
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11 
12 /* s_sincosf.c -- float version of s_sincos.c.
13  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
14  * Optimized by Bruce D. Evans.
15  * Merged s_sinf.c and s_cosf.c by Steven G. Kargl.
16  */
17 
18 #include <float.h>
19 
20 #include "math.h"
21 #define INLINE_REM_PIO2F
22 #include "math_private.h"
23 #include "e_rem_pio2f.c"
24 #include "k_sincosf.h"
25 
26 /* Small multiples of pi/2 rounded to double precision. */
27 static const double
28 p1pio2 = 1*M_PI_2,			/* 0x3FF921FB, 0x54442D18 */
29 p2pio2 = 2*M_PI_2,			/* 0x400921FB, 0x54442D18 */
30 p3pio2 = 3*M_PI_2,			/* 0x4012D97C, 0x7F3321D2 */
31 p4pio2 = 4*M_PI_2;			/* 0x401921FB, 0x54442D18 */
32 
33 void
34 sincosf(float x, float *sn, float *cs)
35 {
36 	float c, s;
37 	double y;
38 	int32_t n, hx, ix;
39 
40 	GET_FLOAT_WORD(hx, x);
41 	ix = hx & 0x7fffffff;
42 
43 	if (ix <= 0x3f490fda) {		/* |x| ~<= pi/4 */
44 		if (ix < 0x39800000) {	/* |x| < 2**-12 */
45 			if ((int)x == 0) {
46 				*sn = x;	/* x with inexact if x != 0 */
47 				*cs = 1;
48 				return;
49 			}
50 		}
51 		__kernel_sincosdf(x, sn, cs);
52 		return;
53 	}
54 
55 	if (ix <= 0x407b53d1) {		/* |x| ~<= 5*pi/4 */
56 		if (ix <= 0x4016cbe3) {	/* |x| ~<= 3pi/4 */
57 			if (hx > 0) {
58 				__kernel_sincosdf(x - p1pio2, cs, sn);
59 				*cs = -*cs;
60 			} else {
61 				__kernel_sincosdf(x + p1pio2, cs, sn);
62 				*sn = -*sn;
63 			}
64 		} else {
65 			if (hx > 0)
66 				__kernel_sincosdf(x - p2pio2, sn, cs);
67 			else
68 				__kernel_sincosdf(x + p2pio2, sn, cs);
69 			*sn = -*sn;
70 			*cs = -*cs;
71 		}
72 		return;
73 	}
74 
75 	if (ix <= 0x40e231d5) {		/* |x| ~<= 9*pi/4 */
76 		if (ix <= 0x40afeddf) {	/* |x| ~<= 7*pi/4 */
77 			if (hx > 0) {
78 				__kernel_sincosdf(x - p3pio2, cs, sn);
79 				*sn = -*sn;
80 			} else {
81 				__kernel_sincosdf(x + p3pio2, cs, sn);
82 				*cs = -*cs;
83 			}
84 		} else {
85 			if (hx > 0)
86 				__kernel_sincosdf(x - p4pio2, sn, cs);
87 			else
88 				__kernel_sincosdf(x + p4pio2, sn, cs);
89 		}
90 		return;
91 	}
92 
93 	/* If x = Inf or NaN, then sin(x) = NaN and cos(x) = NaN. */
94 	if (ix >= 0x7f800000) {
95 		*sn = x - x;
96 		*cs = x - x;
97 		return;
98 	}
99 
100 	/* Argument reduction. */
101 	n = __ieee754_rem_pio2f(x, &y);
102 	__kernel_sincosdf(y, &s, &c);
103 
104 	switch(n & 3) {
105 	case 0:
106 		*sn = s;
107 		*cs = c;
108 		break;
109 	case 1:
110 		*sn = c;
111 		*cs = -s;
112 		break;
113 	case 2:
114 		*sn = -s;
115 		*cs = -c;
116 		break;
117 	default:
118 		*sn = -c;
119 		*cs = s;
120 	}
121 }
122 
123 
124