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