xref: /freebsd/lib/msun/src/s_sincosf.c (revision 8657387683946d0c03e09fe77029edfe309eeb20)
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 <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
20 
21 #include <float.h>
22 
23 #include "math.h"
24 #define INLINE_REM_PIO2F
25 #include "math_private.h"
26 #include "e_rem_pio2f.c"
27 #include "k_sincosf.h"
28 
29 /* Small multiples of pi/2 rounded to double precision. */
30 static const double
31 p1pio2 = 1*M_PI_2,			/* 0x3FF921FB, 0x54442D18 */
32 p2pio2 = 2*M_PI_2,			/* 0x400921FB, 0x54442D18 */
33 p3pio2 = 3*M_PI_2,			/* 0x4012D97C, 0x7F3321D2 */
34 p4pio2 = 4*M_PI_2;			/* 0x401921FB, 0x54442D18 */
35 
36 void
37 sincosf(float x, float *sn, float *cs)
38 {
39 	float c, s;
40 	double y;
41 	int32_t n, hx, ix;
42 
43 	GET_FLOAT_WORD(hx, x);
44 	ix = hx & 0x7fffffff;
45 
46 	if (ix <= 0x3f490fda) {		/* |x| ~<= pi/4 */
47 		if (ix < 0x39800000) {	/* |x| < 2**-12 */
48 			if ((int)x == 0) {
49 				*sn = x;	/* x with inexact if x != 0 */
50 				*cs = 1;
51 				return;
52 			}
53 		}
54 		__kernel_sincosdf(x, sn, cs);
55 		return;
56 	}
57 
58 	if (ix <= 0x407b53d1) {		/* |x| ~<= 5*pi/4 */
59 		if (ix <= 0x4016cbe3) {	/* |x| ~<= 3pi/4 */
60 			if (hx > 0) {
61 				__kernel_sincosdf(x - p1pio2, cs, sn);
62 				*cs = -*cs;
63 			} else {
64 				__kernel_sincosdf(x + p1pio2, cs, sn);
65 				*sn = -*sn;
66 			}
67 		} else {
68 			if (hx > 0)
69 				__kernel_sincosdf(x - p2pio2, sn, cs);
70 			else
71 				__kernel_sincosdf(x + p2pio2, sn, cs);
72 			*sn = -*sn;
73 			*cs = -*cs;
74 		}
75 		return;
76 	}
77 
78 	if (ix <= 0x40e231d5) {		/* |x| ~<= 9*pi/4 */
79 		if (ix <= 0x40afeddf) {	/* |x| ~<= 7*pi/4 */
80 			if (hx > 0) {
81 				__kernel_sincosdf(x - p3pio2, cs, sn);
82 				*sn = -*sn;
83 			} else {
84 				__kernel_sincosdf(x + p3pio2, cs, sn);
85 				*cs = -*cs;
86 			}
87 		} else {
88 			if (hx > 0)
89 				__kernel_sincosdf(x - p4pio2, sn, cs);
90 			else
91 				__kernel_sincosdf(x + p4pio2, sn, cs);
92 		}
93 		return;
94 	}
95 
96 	/* If x = Inf or NaN, then sin(x) = NaN and cos(x) = NaN. */
97 	if (ix >= 0x7f800000) {
98 		*sn = x - x;
99 		*cs = x - x;
100 		return;
101 	}
102 
103 	/* Argument reduction. */
104 	n = __ieee754_rem_pio2f(x, &y);
105 	__kernel_sincosdf(y, &s, &c);
106 
107 	switch(n & 3) {
108 	case 0:
109 		*sn = s;
110 		*cs = c;
111 		break;
112 	case 1:
113 		*sn = c;
114 		*cs = -s;
115 		break;
116 	case 2:
117 		*sn = -s;
118 		*cs = -c;
119 		break;
120 	default:
121 		*sn = -c;
122 		*cs = s;
123 	}
124 }
125 
126 
127