1*f504f610SAugustin Cavalier /* origin: FreeBSD /usr/src/lib/msun/src/s_sinf.c */
2*f504f610SAugustin Cavalier /*
3*f504f610SAugustin Cavalier * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4*f504f610SAugustin Cavalier * Optimized by Bruce D. Evans.
5*f504f610SAugustin Cavalier */
6*f504f610SAugustin Cavalier /*
7*f504f610SAugustin Cavalier * ====================================================
8*f504f610SAugustin Cavalier * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9*f504f610SAugustin Cavalier *
10*f504f610SAugustin Cavalier * Developed at SunPro, a Sun Microsystems, Inc. business.
11*f504f610SAugustin Cavalier * Permission to use, copy, modify, and distribute this
12*f504f610SAugustin Cavalier * software is freely granted, provided that this notice
13*f504f610SAugustin Cavalier * is preserved.
14*f504f610SAugustin Cavalier * ====================================================
15*f504f610SAugustin Cavalier */
16*f504f610SAugustin Cavalier
17*f504f610SAugustin Cavalier #include "libm.h"
18*f504f610SAugustin Cavalier
19*f504f610SAugustin Cavalier /* Small multiples of pi/2 rounded to double precision. */
20*f504f610SAugustin Cavalier static const double
21*f504f610SAugustin Cavalier s1pio2 = 1*M_PI_2, /* 0x3FF921FB, 0x54442D18 */
22*f504f610SAugustin Cavalier s2pio2 = 2*M_PI_2, /* 0x400921FB, 0x54442D18 */
23*f504f610SAugustin Cavalier s3pio2 = 3*M_PI_2, /* 0x4012D97C, 0x7F3321D2 */
24*f504f610SAugustin Cavalier s4pio2 = 4*M_PI_2; /* 0x401921FB, 0x54442D18 */
25*f504f610SAugustin Cavalier
sinf(float x)26*f504f610SAugustin Cavalier float sinf(float x)
27*f504f610SAugustin Cavalier {
28*f504f610SAugustin Cavalier double y;
29*f504f610SAugustin Cavalier uint32_t ix;
30*f504f610SAugustin Cavalier int n, sign;
31*f504f610SAugustin Cavalier
32*f504f610SAugustin Cavalier GET_FLOAT_WORD(ix, x);
33*f504f610SAugustin Cavalier sign = ix >> 31;
34*f504f610SAugustin Cavalier ix &= 0x7fffffff;
35*f504f610SAugustin Cavalier
36*f504f610SAugustin Cavalier if (ix <= 0x3f490fda) { /* |x| ~<= pi/4 */
37*f504f610SAugustin Cavalier if (ix < 0x39800000) { /* |x| < 2**-12 */
38*f504f610SAugustin Cavalier /* raise inexact if x!=0 and underflow if subnormal */
39*f504f610SAugustin Cavalier FORCE_EVAL(ix < 0x00800000 ? x/0x1p120f : x+0x1p120f);
40*f504f610SAugustin Cavalier return x;
41*f504f610SAugustin Cavalier }
42*f504f610SAugustin Cavalier return __sindf(x);
43*f504f610SAugustin Cavalier }
44*f504f610SAugustin Cavalier if (ix <= 0x407b53d1) { /* |x| ~<= 5*pi/4 */
45*f504f610SAugustin Cavalier if (ix <= 0x4016cbe3) { /* |x| ~<= 3pi/4 */
46*f504f610SAugustin Cavalier if (sign)
47*f504f610SAugustin Cavalier return -__cosdf(x + s1pio2);
48*f504f610SAugustin Cavalier else
49*f504f610SAugustin Cavalier return __cosdf(x - s1pio2);
50*f504f610SAugustin Cavalier }
51*f504f610SAugustin Cavalier return __sindf(sign ? -(x + s2pio2) : -(x - s2pio2));
52*f504f610SAugustin Cavalier }
53*f504f610SAugustin Cavalier if (ix <= 0x40e231d5) { /* |x| ~<= 9*pi/4 */
54*f504f610SAugustin Cavalier if (ix <= 0x40afeddf) { /* |x| ~<= 7*pi/4 */
55*f504f610SAugustin Cavalier if (sign)
56*f504f610SAugustin Cavalier return __cosdf(x + s3pio2);
57*f504f610SAugustin Cavalier else
58*f504f610SAugustin Cavalier return -__cosdf(x - s3pio2);
59*f504f610SAugustin Cavalier }
60*f504f610SAugustin Cavalier return __sindf(sign ? x + s4pio2 : x - s4pio2);
61*f504f610SAugustin Cavalier }
62*f504f610SAugustin Cavalier
63*f504f610SAugustin Cavalier /* sin(Inf or NaN) is NaN */
64*f504f610SAugustin Cavalier if (ix >= 0x7f800000)
65*f504f610SAugustin Cavalier return x - x;
66*f504f610SAugustin Cavalier
67*f504f610SAugustin Cavalier /* general argument reduction needed */
68*f504f610SAugustin Cavalier n = __rem_pio2f(x, &y);
69*f504f610SAugustin Cavalier switch (n&3) {
70*f504f610SAugustin Cavalier case 0: return __sindf(y);
71*f504f610SAugustin Cavalier case 1: return __cosdf(y);
72*f504f610SAugustin Cavalier case 2: return __sindf(-y);
73*f504f610SAugustin Cavalier default:
74*f504f610SAugustin Cavalier return -__cosdf(y);
75*f504f610SAugustin Cavalier }
76*f504f610SAugustin Cavalier }
77