xref: /csrg-svn/lib/libm/common/trig.h (revision 31931)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  *
4  * Use and reproduction of this software are granted  in  accordance  with
5  * the terms and conditions specified in  the  Berkeley  Software  License
6  * Agreement (in particular, this entails acknowledgement of the programs'
7  * source, and inclusion of this notice) with the additional understanding
8  * that  all  recipients  should regard themselves as participants  in  an
9  * ongoing  research  project and hence should  feel  obligated  to report
10  * their  experiences (good or bad) with these elementary function  codes,
11  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12  */
13 /* @(#)trig.h	1.1	1.1 (ucb.elefunt) 07/24/87 */
14 #if defined(vax)||defined(tahoe)
15 #ifdef vax
16 #define _0x(A,B)	0x/**/A/**/B
17 #else	/* vax */
18 #define _0x(A,B)	0x/**/B/**/A
19 #endif	/* vax */
20 /*thresh =  2.6117239648121182150E-1    , Hex  2^ -1   *  .85B8636B026EA0 */
21 /*PIo4   =  7.8539816339744830676E-1    , Hex  2^  0   *  .C90FDAA22168C2 */
22 /*PIo2   =  1.5707963267948966135E0     , Hex  2^  1   *  .C90FDAA22168C2 */
23 /*PI3o4  =  2.3561944901923449203E0     , Hex  2^  2   *  .96CBE3F9990E92 */
24 /*PI     =  3.1415926535897932270E0     , Hex  2^  2   *  .C90FDAA22168C2 */
25 /*PI2    =  6.2831853071795864540E0     ; Hex  2^  3   *  .C90FDAA22168C2 */
26 static long threshx[]	= { _0x(b863,3f85), _0x(6ea0,6b02)};
27 static long PIo4x[]	= { _0x(0fda,4049), _0x(68c2,a221)};
28 static long PIo2x[]	= { _0x(0fda,40c9), _0x(68c2,a221)};
29 static long PI3o4x[]	= { _0x(cbe3,4116), _0x(0e92,f999)};
30 static long PIx[]	= { _0x(0fda,4149), _0x(68c2,a221)};
31 static long PI2x[]	= { _0x(0fda,41c9), _0x(68c2,a221)};
32 #define thresh	(*(double*)threshx)
33 #define PIo4	(*(double*)PIo4x)
34 #define PIo2	(*(double*)PIo2x)
35 #define PI3o4	(*(double*)PI3o4x)
36 #define PI	(*(double*)PIx)
37 #define PI2	(*(double*)PI2x)
38 #else   /* defined(vax)||defined(tahoe) */
39 static double
40 thresh	=  2.6117239648121182150E-1    , /*Hex  2^ -2   *  1.0B70C6D604DD4 */
41 PIo4	=  7.8539816339744827900E-1    , /*Hex  2^ -1   *  1.921FB54442D18 */
42 PIo2	=  1.5707963267948965580E0     , /*Hex  2^  0   *  1.921FB54442D18 */
43 PI3o4	=  2.3561944901923448370E0     , /*Hex  2^  1   *  1.2D97C7F3321D2 */
44 PI	=  3.1415926535897931160E0     , /*Hex  2^  1   *  1.921FB54442D18 */
45 PI2	=  6.2831853071795862320E0     ; /*Hex  2^  2   *  1.921FB54442D18 */
46 #ifdef national
47 static long fmaxx[]	= { 0xffffffff, 0x7fefffff};
48 #define   fmax    (*(double*)fmaxx)
49 #endif	/* national */
50 #endif	/* defined(vax)||defined(tahoe) */
51 static double
52 	zero = 0,
53 	one = 1,
54 	negone = -1,
55 	half = 1.0/2.0,
56 	small = 1E-10,	/* 1+small**2 == 1; better values for small:
57 			 *		small	= 1.5E-9 for VAX D
58 			 *			= 1.2E-8 for IEEE Double
59 			 *			= 2.8E-10 for IEEE Extended
60 			 */
61 	big = 1E20;	/* big := 1/(small**2) */
62 
63 /* sin__S(x*x) ... re-implemented as a macro
64  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
65  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
66  * CODED IN C BY K.C. NG, 1/21/85;
67  * REVISED BY K.C. NG on 8/13/85.
68  *
69  *	    sin(x*k) - x
70  * RETURN  --------------- on [-PI/4,PI/4] , where k=pi/PI, PI is the rounded
71  *	            x
72  * value of pi in machine precision:
73  *
74  *	Decimal:
75  *		pi = 3.141592653589793 23846264338327 .....
76  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
77  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
78  *
79  *	Hexadecimal:
80  *		pi = 3.243F6A8885A308D313198A2E....
81  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
82  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
83  *
84  * Method:
85  *	1. Let z=x*x. Create a polynomial approximation to
86  *	    (sin(k*x)-x)/x  =  z*(S0 + S1*z^1 + ... + S5*z^5).
87  *	Then
88  *      sin__S(x*x) = z*(S0 + S1*z^1 + ... + S5*z^5)
89  *
90  *	The coefficient S's are obtained by a special Remez algorithm.
91  *
92  * Accuracy:
93  *	In the absence of rounding error, the approximation has absolute error
94  *	less than 2**(-61.11) for VAX D FORMAT, 2**(-57.45) for IEEE DOUBLE.
95  *
96  * Constants:
97  * The hexadecimal values are the intended ones for the following constants.
98  * The decimal values may be used, provided that the compiler will convert
99  * from decimal to binary accurately enough to produce the hexadecimal values
100  * shown.
101  *
102  */
103 
104 #if defined(vax)||defined(tahoe)
105 /*S0     = -1.6666666666666646660E-1    , Hex  2^ -2   * -.AAAAAAAAAAAA71 */
106 /*S1     =  8.3333333333297230413E-3    , Hex  2^ -6   *  .8888888888477F */
107 /*S2     = -1.9841269838362403710E-4    , Hex  2^-12   * -.D00D00CF8A1057 */
108 /*S3     =  2.7557318019967078930E-6    , Hex  2^-18   *  .B8EF1CA326BEDC */
109 /*S4     = -2.5051841873876551398E-8    , Hex  2^-25   * -.D73195374CE1D3 */
110 /*S5     =  1.6028995389845827653E-10   , Hex  2^-32   *  .B03D9C6D26CCCC */
111 /*S6     = -6.2723499671769283121E-13   ; Hex  2^-40   * -.B08D0B7561EA82 */
112 static long S0x[]	= { _0x(aaaa,bf2a), _0x(aa71,aaaa)};
113 static long S1x[]	= { _0x(8888,3d08), _0x(477f,8888)};
114 static long S2x[]	= { _0x(0d00,ba50), _0x(1057,cf8a)};
115 static long S3x[]	= { _0x(ef1c,3738), _0x(bedc,a326)};
116 static long S4x[]	= { _0x(3195,b3d7), _0x(e1d3,374c)};
117 static long S5x[]	= { _0x(3d9c,3030), _0x(cccc,6d26)};
118 static long S6x[]	= { _0x(8d0b,ac30), _0x(ea82,7561)};
119 #define S0	(*(double*)S0x)
120 #define S1	(*(double*)S1x)
121 #define S2	(*(double*)S2x)
122 #define S3	(*(double*)S3x)
123 #define S4	(*(double*)S4x)
124 #define S5	(*(double*)S5x)
125 #define S6	(*(double*)S6x)
126 #else	/* IEEE double  */
127 static double
128 S0     = -1.6666666666666463126E-1    , /*Hex  2^ -3   * -1.555555555550C */
129 S1     =  8.3333333332992771264E-3    , /*Hex  2^ -7   *  1.111111110C461 */
130 S2     = -1.9841269816180999116E-4    , /*Hex  2^-13   * -1.A01A019746345 */
131 S3     =  2.7557309793219876880E-6    , /*Hex  2^-19   *  1.71DE3209CDCD9 */
132 S4     = -2.5050225177523807003E-8    , /*Hex  2^-26   * -1.AE5C0E319A4EF */
133 S5     =  1.5868926979889205164E-10   ; /*Hex  2^-33   *  1.5CF61DF672B13 */
134 #endif
135 
136 #if defined(vax)||defined(tahoe)
137 #define sin__S(z)	(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*(S5+z*S6)))))))
138 #else 	/* defined(vax)||defined(tahoe) */
139 #define sin__S(z)	(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*S5))))))
140 #endif 	/* defined(vax)||defined(tahoe) */
141 
142 /* cos__C(x*x) ... re-implemented as a macro
143  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
144  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
145  * CODED IN C BY K.C. NG, 1/21/85;
146  * REVISED BY K.C. NG on 8/13/85.
147  *
148  *	   		    x*x
149  * RETURN   cos(k*x) - 1 + ----- on [-PI/4,PI/4],  where k = pi/PI,
150  *	  		     2
151  * PI is the rounded value of pi in machine precision :
152  *
153  *	Decimal:
154  *		pi = 3.141592653589793 23846264338327 .....
155  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
156  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
157  *
158  *	Hexadecimal:
159  *		pi = 3.243F6A8885A308D313198A2E....
160  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
161  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
162  *
163  *
164  * Method:
165  *	1. Let z=x*x. Create a polynomial approximation to
166  *	    cos(k*x)-1+z/2  =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
167  *	then
168  *      cos__C(z) =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
169  *
170  *	The coefficient C's are obtained by a special Remez algorithm.
171  *
172  * Accuracy:
173  *	In the absence of rounding error, the approximation has absolute error
174  *	less than 2**(-64) for VAX D FORMAT, 2**(-58.3) for IEEE DOUBLE.
175  *
176  *
177  * Constants:
178  * The hexadecimal values are the intended ones for the following constants.
179  * The decimal values may be used, provided that the compiler will convert
180  * from decimal to binary accurately enough to produce the hexadecimal values
181  * shown.
182  *
183  */
184 
185 #if defined(vax)||defined(tahoe)
186 /*C0     =  4.1666666666666504759E-2    , Hex  2^ -4   *  .AAAAAAAAAAA9F0 */
187 /*C1     = -1.3888888888865302059E-3    , Hex  2^ -9   * -.B60B60B60A0CCA */
188 /*C2     =  2.4801587285601038265E-5    , Hex  2^-15   *  .D00D00CDCD098F */
189 /*C3     = -2.7557313470902390219E-7    , Hex  2^-21   * -.93F27BB593E805 */
190 /*C4     =  2.0875623401082232009E-9    , Hex  2^-28   *  .8F74C8FA1E3FF0 */
191 /*C5     = -1.1355178117642986178E-11   ; Hex  2^-36   * -.C7C32D0A5C5A63 */
192 static long C0x[]	= { _0x(aaaa,3e2a), _0x(a9f0,aaaa)};
193 static long C1x[]	= { _0x(0b60,bbb6), _0x(0cca,b60a)};
194 static long C2x[]	= { _0x(0d00,38d0), _0x(098f,cdcd)};
195 static long C3x[]	= { _0x(f27b,b593), _0x(e805,b593)};
196 static long C4x[]	= { _0x(74c8,320f), _0x(3ff0,fa1e)};
197 static long C5x[]	= { _0x(c32d,ae47), _0x(5a63,0a5c)};
198 #define C0	(*(double*)C0x)
199 #define C1	(*(double*)C1x)
200 #define C2	(*(double*)C2x)
201 #define C3	(*(double*)C3x)
202 #define C4	(*(double*)C4x)
203 #define C5	(*(double*)C5x)
204 #else	/* defined(vax)||defined(tahoe) */
205 static double
206 C0     =  4.1666666666666504759E-2    , /*Hex  2^ -5   *  1.555555555553E */
207 C1     = -1.3888888888865301516E-3    , /*Hex  2^-10   * -1.6C16C16C14199 */
208 C2     =  2.4801587269650015769E-5    , /*Hex  2^-16   *  1.A01A01971CAEB */
209 C3     = -2.7557304623183959811E-7    , /*Hex  2^-22   * -1.27E4F1314AD1A */
210 C4     =  2.0873958177697780076E-9    , /*Hex  2^-29   *  1.1EE3B60DDDC8C */
211 C5     = -1.1250289076471311557E-11   ; /*Hex  2^-37   * -1.8BD5986B2A52E */
212 #endif	/* defined(vax)||defined(tahoe) */
213 
214 #define cos__C(z)	(z*z*(C0+z*(C1+z*(C2+z*(C3+z*(C4+z*C5))))))
215 
216 extern int finite();
217 extern double copysign(),drem();
218