OLD | NEW |
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_sinf.c */ | 1 /* origin: FreeBSD /usr/src/lib/msun/src/s_sinf.c */ |
2 /* | 2 /* |
3 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. | 3 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. |
4 * Optimized by Bruce D. Evans. | 4 * Optimized by Bruce D. Evans. |
5 */ | 5 */ |
6 /* | 6 /* |
7 * ==================================================== | 7 * ==================================================== |
8 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | 8 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
9 * | 9 * |
10 * Developed at SunPro, a Sun Microsystems, Inc. business. | 10 * Developed at SunPro, a Sun Microsystems, Inc. business. |
11 * Permission to use, copy, modify, and distribute this | 11 * Permission to use, copy, modify, and distribute this |
12 * software is freely granted, provided that this notice | 12 * software is freely granted, provided that this notice |
13 * is preserved. | 13 * is preserved. |
14 * ==================================================== | 14 * ==================================================== |
15 */ | 15 */ |
16 | 16 |
17 #define _GNU_SOURCE | 17 #define _GNU_SOURCE |
18 #include "libm.h" | 18 #include "libm.h" |
19 | 19 |
20 /* Small multiples of pi/2 rounded to double precision. */ | 20 /* Small multiples of pi/2 rounded to double precision. */ |
21 static const double | 21 static const double s1pio2 = 1 * M_PI_2, /* 0x3FF921FB, 0x54442D18 */ |
22 s1pio2 = 1*M_PI_2, /* 0x3FF921FB, 0x54442D18 */ | 22 s2pio2 = 2 * M_PI_2, /* 0x400921FB, 0x54442D18 */ |
23 s2pio2 = 2*M_PI_2, /* 0x400921FB, 0x54442D18 */ | 23 s3pio2 = 3 * M_PI_2, /* 0x4012D97C, 0x7F3321D2 */ |
24 s3pio2 = 3*M_PI_2, /* 0x4012D97C, 0x7F3321D2 */ | 24 s4pio2 = 4 * M_PI_2; /* 0x401921FB, 0x54442D18 */ |
25 s4pio2 = 4*M_PI_2; /* 0x401921FB, 0x54442D18 */ | |
26 | 25 |
27 void sincosf(float x, float *sin, float *cos) | 26 void sincosf(float x, float* sin, float* cos) { |
28 { | 27 double y; |
29 » double y; | 28 float_t s, c; |
30 » float_t s, c; | 29 uint32_t ix; |
31 » uint32_t ix; | 30 unsigned n, sign; |
32 » unsigned n, sign; | |
33 | 31 |
34 » GET_FLOAT_WORD(ix, x); | 32 GET_FLOAT_WORD(ix, x); |
35 » sign = ix >> 31; | 33 sign = ix >> 31; |
36 » ix &= 0x7fffffff; | 34 ix &= 0x7fffffff; |
37 | 35 |
38 » /* |x| ~<= pi/4 */ | 36 /* |x| ~<= pi/4 */ |
39 » if (ix <= 0x3f490fda) { | 37 if (ix <= 0x3f490fda) { |
40 » » /* |x| < 2**-12 */ | 38 /* |x| < 2**-12 */ |
41 » » if (ix < 0x39800000) { | 39 if (ix < 0x39800000) { |
42 » » » /* raise inexact if x!=0 and underflow if subnormal */ | 40 /* raise inexact if x!=0 and underflow if subnormal */ |
43 » » » FORCE_EVAL(ix < 0x00100000 ? x/0x1p120f : x+0x1p120f); | 41 FORCE_EVAL(ix < 0x00100000 ? x / 0x1p120f : x + 0x1p120f); |
44 » » » *sin = x; | 42 *sin = x; |
45 » » » *cos = 1.0f; | 43 *cos = 1.0f; |
46 » » » return; | 44 return; |
47 » » } | 45 } |
48 » » *sin = __sindf(x); | 46 *sin = __sindf(x); |
49 » » *cos = __cosdf(x); | 47 *cos = __cosdf(x); |
50 » » return; | 48 return; |
51 » } | 49 } |
52 | 50 |
53 » /* |x| ~<= 5*pi/4 */ | 51 /* |x| ~<= 5*pi/4 */ |
54 » if (ix <= 0x407b53d1) { | 52 if (ix <= 0x407b53d1) { |
55 » » if (ix <= 0x4016cbe3) { /* |x| ~<= 3pi/4 */ | 53 if (ix <= 0x4016cbe3) { /* |x| ~<= 3pi/4 */ |
56 » » » if (sign) { | 54 if (sign) { |
57 » » » » *sin = -__cosdf(x + s1pio2); | 55 *sin = -__cosdf(x + s1pio2); |
58 » » » » *cos = __sindf(x + s1pio2); | 56 *cos = __sindf(x + s1pio2); |
59 » » » } else { | 57 } else { |
60 » » » » *sin = __cosdf(s1pio2 - x); | 58 *sin = __cosdf(s1pio2 - x); |
61 » » » » *cos = __sindf(s1pio2 - x); | 59 *cos = __sindf(s1pio2 - x); |
62 » » » } | 60 } |
63 » » » return; | 61 return; |
64 » » } | 62 } |
65 » » /* -sin(x+c) is not correct if x+c could be 0: -0 vs +0 */ | 63 /* -sin(x+c) is not correct if x+c could be 0: -0 vs +0 */ |
66 » » *sin = -__sindf(sign ? x + s2pio2 : x - s2pio2); | 64 *sin = -__sindf(sign ? x + s2pio2 : x - s2pio2); |
67 » » *cos = -__cosdf(sign ? x + s2pio2 : x - s2pio2); | 65 *cos = -__cosdf(sign ? x + s2pio2 : x - s2pio2); |
68 » » return; | 66 return; |
69 » } | 67 } |
70 | 68 |
71 » /* |x| ~<= 9*pi/4 */ | 69 /* |x| ~<= 9*pi/4 */ |
72 » if (ix <= 0x40e231d5) { | 70 if (ix <= 0x40e231d5) { |
73 » » if (ix <= 0x40afeddf) { /* |x| ~<= 7*pi/4 */ | 71 if (ix <= 0x40afeddf) { /* |x| ~<= 7*pi/4 */ |
74 » » » if (sign) { | 72 if (sign) { |
75 » » » » *sin = __cosdf(x + s3pio2); | 73 *sin = __cosdf(x + s3pio2); |
76 » » » » *cos = -__sindf(x + s3pio2); | 74 *cos = -__sindf(x + s3pio2); |
77 » » » } else { | 75 } else { |
78 » » » » *sin = -__cosdf(x - s3pio2); | 76 *sin = -__cosdf(x - s3pio2); |
79 » » » » *cos = __sindf(x - s3pio2); | 77 *cos = __sindf(x - s3pio2); |
80 » » » } | 78 } |
81 » » » return; | 79 return; |
82 » » } | 80 } |
83 » » *sin = __sindf(sign ? x + s4pio2 : x - s4pio2); | 81 *sin = __sindf(sign ? x + s4pio2 : x - s4pio2); |
84 » » *cos = __cosdf(sign ? x + s4pio2 : x - s4pio2); | 82 *cos = __cosdf(sign ? x + s4pio2 : x - s4pio2); |
85 » » return; | 83 return; |
86 » } | 84 } |
87 | 85 |
88 » /* sin(Inf or NaN) is NaN */ | 86 /* sin(Inf or NaN) is NaN */ |
89 » if (ix >= 0x7f800000) { | 87 if (ix >= 0x7f800000) { |
90 » » *sin = *cos = x - x; | 88 *sin = *cos = x - x; |
91 » » return; | 89 return; |
92 » } | 90 } |
93 | 91 |
94 » /* general argument reduction needed */ | 92 /* general argument reduction needed */ |
95 » n = __rem_pio2f(x, &y); | 93 n = __rem_pio2f(x, &y); |
96 » s = __sindf(y); | 94 s = __sindf(y); |
97 » c = __cosdf(y); | 95 c = __cosdf(y); |
98 » switch (n&3) { | 96 switch (n & 3) { |
99 » case 0: | 97 case 0: |
100 » » *sin = s; | 98 *sin = s; |
101 » » *cos = c; | 99 *cos = c; |
102 » » break; | 100 break; |
103 » case 1: | 101 case 1: |
104 » » *sin = c; | 102 *sin = c; |
105 » » *cos = -s; | 103 *cos = -s; |
106 » » break; | 104 break; |
107 » case 2: | 105 case 2: |
108 » » *sin = -s; | 106 *sin = -s; |
109 » » *cos = -c; | 107 *cos = -c; |
110 » » break; | 108 break; |
111 » case 3: | 109 case 3: |
112 » default: | 110 default: |
113 » » *sin = -c; | 111 *sin = -c; |
114 » » *cos = s; | 112 *cos = s; |
115 » » break; | 113 break; |
116 » } | 114 } |
117 } | 115 } |
OLD | NEW |