|
0
|
1 //=========================================================================== |
|
|
2 // |
|
|
3 // s_cos.c |
|
|
4 // |
|
|
5 // Part of the standard mathematical function library |
|
|
6 // |
|
|
7 //=========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
25 // by Cygnus are Copyright (C) 1998 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //=========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): jlarmour@cygnus.co.uk |
|
|
33 // Contributors: jlarmour@cygnus.co.uk |
|
|
34 // Date: 1998-02-13 |
|
|
35 // Purpose: |
|
|
36 // Description: |
|
|
37 // Usage: |
|
|
38 // |
|
|
39 //####DESCRIPTIONEND#### |
|
|
40 // |
|
|
41 //=========================================================================== |
|
|
42 |
|
|
43 // CONFIGURATION |
|
|
44 |
|
|
45 #include <pkgconf/libm.h> // Configuration header |
|
|
46 |
|
|
47 // Include the Math library? |
|
|
48 #ifdef CYGPKG_LIBM |
|
|
49 |
|
|
50 // Derived from code with the following copyright |
|
|
51 |
|
|
52 |
|
|
53 /* @(#)s_cos.c 1.3 95/01/18 */ |
|
|
54 /* |
|
|
55 * ==================================================== |
|
|
56 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
|
|
57 * |
|
|
58 * Developed at SunSoft, a Sun Microsystems, Inc. business. |
|
|
59 * Permission to use, copy, modify, and distribute this |
|
|
60 * software is freely granted, provided that this notice |
|
|
61 * is preserved. |
|
|
62 * ==================================================== |
|
|
63 */ |
|
|
64 |
|
|
65 /* cos(x) |
|
|
66 * Return cosine function of x. |
|
|
67 * |
|
|
68 * kernel function: |
|
|
69 * __kernel_sin ... sine function on [-pi/4,pi/4] |
|
|
70 * __kernel_cos ... cosine function on [-pi/4,pi/4] |
|
|
71 * __ieee754_rem_pio2 ... argument reduction routine |
|
|
72 * |
|
|
73 * Method. |
|
|
74 * Let S,C and T denote the sin, cos and tan respectively on |
|
|
75 * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 |
|
|
76 * in [-pi/4 , +pi/4], and let n = k mod 4. |
|
|
77 * We have |
|
|
78 * |
|
|
79 * n sin(x) cos(x) tan(x) |
|
|
80 * ---------------------------------------------------------- |
|
|
81 * 0 S C T |
|
|
82 * 1 C -S -1/T |
|
|
83 * 2 -S -C T |
|
|
84 * 3 -C S -1/T |
|
|
85 * ---------------------------------------------------------- |
|
|
86 * |
|
|
87 * Special cases: |
|
|
88 * Let trig be any of sin, cos, or tan. |
|
|
89 * trig(+-INF) is NaN, with signals; |
|
|
90 * trig(NaN) is that NaN; |
|
|
91 * |
|
|
92 * Accuracy: |
|
|
93 * TRIG(x) returns trig(x) nearly rounded |
|
|
94 */ |
|
|
95 |
|
|
96 #include "mathincl/fdlibm.h" |
|
|
97 |
|
|
98 double cos(double x) |
|
|
99 { |
|
|
100 double y[2],z=0.0; |
|
|
101 int n, ix; |
|
|
102 |
|
|
103 /* High word of x. */ |
|
|
104 ix = CYG_LIBM_HI(x); |
|
|
105 |
|
|
106 /* |x| ~< pi/4 */ |
|
|
107 ix &= 0x7fffffff; |
|
|
108 if(ix <= 0x3fe921fb) return __kernel_cos(x,z); |
|
|
109 |
|
|
110 /* cos(Inf or NaN) is NaN */ |
|
|
111 else if (ix>=0x7ff00000) return x-x; |
|
|
112 |
|
|
113 /* argument reduction needed */ |
|
|
114 else { |
|
|
115 n = __ieee754_rem_pio2(x,y); |
|
|
116 switch(n&3) { |
|
|
117 case 0: return __kernel_cos(y[0],y[1]); |
|
|
118 case 1: return -__kernel_sin(y[0],y[1],1); |
|
|
119 case 2: return -__kernel_cos(y[0],y[1]); |
|
|
120 default: |
|
|
121 return __kernel_sin(y[0],y[1],1); |
|
|
122 } |
|
|
123 } |
|
|
124 } |
|
|
125 |
|
|
126 #endif // ifdef CYGPKG_LIBM |
|
|
127 |
|
|
128 // EOF s_cos.c |