OLD | NEW |
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_cos.c */ | 1 /* origin: FreeBSD /usr/src/lib/msun/src/s_cos.c */ |
2 /* | 2 /* |
3 * ==================================================== | 3 * ==================================================== |
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | 4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
5 * | 5 * |
6 * Developed at SunPro, a Sun Microsystems, Inc. business. | 6 * Developed at SunPro, a Sun Microsystems, Inc. business. |
7 * Permission to use, copy, modify, and distribute this | 7 * Permission to use, copy, modify, and distribute this |
8 * software is freely granted, provided that this notice | 8 * software is freely granted, provided that this notice |
9 * is preserved. | 9 * is preserved. |
10 * ==================================================== | 10 * ==================================================== |
(...skipping 24 matching lines...) Expand all Loading... |
35 * Let trig be any of sin, cos, or tan. | 35 * Let trig be any of sin, cos, or tan. |
36 * trig(+-INF) is NaN, with signals; | 36 * trig(+-INF) is NaN, with signals; |
37 * trig(NaN) is that NaN; | 37 * trig(NaN) is that NaN; |
38 * | 38 * |
39 * Accuracy: | 39 * Accuracy: |
40 * TRIG(x) returns trig(x) nearly rounded | 40 * TRIG(x) returns trig(x) nearly rounded |
41 */ | 41 */ |
42 | 42 |
43 #include "libm.h" | 43 #include "libm.h" |
44 | 44 |
45 double cos(double x) | 45 double cos(double x) { |
46 { | 46 double y[2]; |
47 » double y[2]; | 47 uint32_t ix; |
48 » uint32_t ix; | 48 unsigned n; |
49 » unsigned n; | |
50 | 49 |
51 » GET_HIGH_WORD(ix, x); | 50 GET_HIGH_WORD(ix, x); |
52 » ix &= 0x7fffffff; | 51 ix &= 0x7fffffff; |
53 | 52 |
54 » /* |x| ~< pi/4 */ | 53 /* |x| ~< pi/4 */ |
55 » if (ix <= 0x3fe921fb) { | 54 if (ix <= 0x3fe921fb) { |
56 » » if (ix < 0x3e46a09e) { /* |x| < 2**-27 * sqrt(2) */ | 55 if (ix < 0x3e46a09e) { /* |x| < 2**-27 * sqrt(2) */ |
57 » » » /* raise inexact if x!=0 */ | 56 /* raise inexact if x!=0 */ |
58 » » » FORCE_EVAL(x + 0x1p120f); | 57 FORCE_EVAL(x + 0x1p120f); |
59 » » » return 1.0; | 58 return 1.0; |
60 » » } | 59 } |
61 » » return __cos(x, 0); | 60 return __cos(x, 0); |
62 » } | 61 } |
63 | 62 |
64 » /* cos(Inf or NaN) is NaN */ | 63 /* cos(Inf or NaN) is NaN */ |
65 » if (ix >= 0x7ff00000) | 64 if (ix >= 0x7ff00000) |
66 » » return x-x; | 65 return x - x; |
67 | 66 |
68 » /* argument reduction */ | 67 /* argument reduction */ |
69 » n = __rem_pio2(x, y); | 68 n = __rem_pio2(x, y); |
70 » switch (n&3) { | 69 switch (n & 3) { |
71 » case 0: return __cos(y[0], y[1]); | 70 case 0: |
72 » case 1: return -__sin(y[0], y[1], 1); | 71 return __cos(y[0], y[1]); |
73 » case 2: return -__cos(y[0], y[1]); | 72 case 1: |
74 » default: | 73 return -__sin(y[0], y[1], 1); |
75 » » return __sin(y[0], y[1], 1); | 74 case 2: |
76 » } | 75 return -__cos(y[0], y[1]); |
| 76 default: |
| 77 return __sin(y[0], y[1], 1); |
| 78 } |
77 } | 79 } |
OLD | NEW |