xref: /minix3/lib/libm/noieee_src/trig.h (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /*	$NetBSD: trig.h,v 1.6 2003/08/07 16:44:53 agc Exp $	*/
2*2fe8fb19SBen Gras /*
3*2fe8fb19SBen Gras  * Copyright (c) 1987, 1993
4*2fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
5*2fe8fb19SBen Gras  *
6*2fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
7*2fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
8*2fe8fb19SBen Gras  * are met:
9*2fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
10*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
11*2fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
12*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
13*2fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
14*2fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
15*2fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
16*2fe8fb19SBen Gras  *    without specific prior written permission.
17*2fe8fb19SBen Gras  *
18*2fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*2fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*2fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*2fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*2fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*2fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*2fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*2fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*2fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*2fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*2fe8fb19SBen Gras  * SUCH DAMAGE.
29*2fe8fb19SBen Gras  *
30*2fe8fb19SBen Gras  *	@(#)trig.h	8.1 (Berkeley) 6/4/93
31*2fe8fb19SBen Gras  */
32*2fe8fb19SBen Gras 
33*2fe8fb19SBen Gras vc(thresh, 2.6117239648121182150E-1 ,b863,3f85,6ea0,6b02, -1, .85B8636B026EA0)
34*2fe8fb19SBen Gras vc(PIo4,   7.8539816339744830676E-1 ,0fda,4049,68c2,a221,  0, .C90FDAA22168C2)
35*2fe8fb19SBen Gras vc(PIo2,   1.5707963267948966135E0  ,0fda,40c9,68c2,a221,  1, .C90FDAA22168C2)
36*2fe8fb19SBen Gras vc(PI3o4,  2.3561944901923449203E0  ,cbe3,4116,0e92,f999,  2, .96CBE3F9990E92)
37*2fe8fb19SBen Gras vc(PI,     3.1415926535897932270E0  ,0fda,4149,68c2,a221,  2, .C90FDAA22168C2)
38*2fe8fb19SBen Gras vc(PI2,    6.2831853071795864540E0  ,0fda,41c9,68c2,a221,  3, .C90FDAA22168C2)
39*2fe8fb19SBen Gras 
40*2fe8fb19SBen Gras ic(thresh, 2.6117239648121182150E-1 , -2, 1.0B70C6D604DD4)
41*2fe8fb19SBen Gras ic(PIo4,   7.8539816339744827900E-1 , -1, 1.921FB54442D18)
42*2fe8fb19SBen Gras ic(PIo2,   1.5707963267948965580E0  ,  0, 1.921FB54442D18)
43*2fe8fb19SBen Gras ic(PI3o4,  2.3561944901923448370E0  ,  1, 1.2D97C7F3321D2)
44*2fe8fb19SBen Gras ic(PI,     3.1415926535897931160E0  ,  1, 1.921FB54442D18)
45*2fe8fb19SBen Gras ic(PI2,    6.2831853071795862320E0  ,  2, 1.921FB54442D18)
46*2fe8fb19SBen Gras 
47*2fe8fb19SBen Gras #ifdef vccast
48*2fe8fb19SBen Gras #define	thresh	vccast(thresh)
49*2fe8fb19SBen Gras #define	PIo4	vccast(PIo4)
50*2fe8fb19SBen Gras #define	PIo2	vccast(PIo2)
51*2fe8fb19SBen Gras #define	PI3o4	vccast(PI3o4)
52*2fe8fb19SBen Gras #define	PI	vccast(PI)
53*2fe8fb19SBen Gras #define	PI2	vccast(PI2)
54*2fe8fb19SBen Gras #endif
55*2fe8fb19SBen Gras 
56*2fe8fb19SBen Gras #ifdef national
57*2fe8fb19SBen Gras static long fmaxx[]	= { 0xffffffff, 0x7fefffff};
58*2fe8fb19SBen Gras #define   fmax    (*(double*)fmaxx)
59*2fe8fb19SBen Gras #endif	/* national */
60*2fe8fb19SBen Gras 
61*2fe8fb19SBen Gras #ifdef _LIBM_DECLARE
62*2fe8fb19SBen Gras const double
63*2fe8fb19SBen Gras 	__zero = 0,
64*2fe8fb19SBen Gras 	__one = 1,
65*2fe8fb19SBen Gras 	__negone = -1,
66*2fe8fb19SBen Gras 	__half = 1.0/2.0,
67*2fe8fb19SBen Gras #ifdef __vax__
68*2fe8fb19SBen Gras 	__small = 1E-9, /* 1+small**2 == 1; better values for small:
69*2fe8fb19SBen Gras 			  *		small	= 1.5E-9 for VAX D
70*2fe8fb19SBen Gras 			  *			= 1.2E-8 for IEEE Double
71*2fe8fb19SBen Gras 			  *			= 2.8E-10 for IEEE Extended
72*2fe8fb19SBen Gras 			  */
73*2fe8fb19SBen Gras 	__big = 1E18;	/* big := 1/(small**2) */
74*2fe8fb19SBen Gras #else
75*2fe8fb19SBen Gras 	__small = 1E-10, /* 1+small**2 == 1; better values for small:
76*2fe8fb19SBen Gras 			  *		small	= 1.5E-9 for VAX D
77*2fe8fb19SBen Gras 			  *			= 1.2E-8 for IEEE Double
78*2fe8fb19SBen Gras 			  *			= 2.8E-10 for IEEE Extended
79*2fe8fb19SBen Gras 			  */
80*2fe8fb19SBen Gras 	__big = 1E20;	/* big := 1/(small**2) */
81*2fe8fb19SBen Gras #endif
82*2fe8fb19SBen Gras #else
83*2fe8fb19SBen Gras extern const double __zero, __one, __negone, __half, __small, __big;
84*2fe8fb19SBen Gras #endif
85*2fe8fb19SBen Gras 
86*2fe8fb19SBen Gras /* sin__S(x*x) ... re-implemented as a macro
87*2fe8fb19SBen Gras  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
88*2fe8fb19SBen Gras  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
89*2fe8fb19SBen Gras  * CODED IN C BY K.C. NG, 1/21/85;
90*2fe8fb19SBen Gras  * REVISED BY K.C. NG on 8/13/85.
91*2fe8fb19SBen Gras  *
92*2fe8fb19SBen Gras  *	    sin(x*k) - x
93*2fe8fb19SBen Gras  * RETURN  --------------- on [-PI/4,PI/4] , where k=pi/PI, PI is the rounded
94*2fe8fb19SBen Gras  *	            x
95*2fe8fb19SBen Gras  * value of pi in machine precision:
96*2fe8fb19SBen Gras  *
97*2fe8fb19SBen Gras  *	Decimal:
98*2fe8fb19SBen Gras  *		pi = 3.141592653589793 23846264338327 .....
99*2fe8fb19SBen Gras  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
100*2fe8fb19SBen Gras  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
101*2fe8fb19SBen Gras  *
102*2fe8fb19SBen Gras  *	Hexadecimal:
103*2fe8fb19SBen Gras  *		pi = 3.243F6A8885A308D313198A2E....
104*2fe8fb19SBen Gras  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
105*2fe8fb19SBen Gras  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
106*2fe8fb19SBen Gras  *
107*2fe8fb19SBen Gras  * Method:
108*2fe8fb19SBen Gras  *	1. Let z=x*x. Create a polynomial approximation to
109*2fe8fb19SBen Gras  *	    (sin(k*x)-x)/x  =  z*(S0 + S1*z^1 + ... + S5*z^5).
110*2fe8fb19SBen Gras  *	Then
111*2fe8fb19SBen Gras  *      sin__S(x*x) = z*(S0 + S1*z^1 + ... + S5*z^5)
112*2fe8fb19SBen Gras  *
113*2fe8fb19SBen Gras  *	The coefficient S's are obtained by a special Remez algorithm.
114*2fe8fb19SBen Gras  *
115*2fe8fb19SBen Gras  * Accuracy:
116*2fe8fb19SBen Gras  *	In the absence of rounding error, the approximation has absolute error
117*2fe8fb19SBen Gras  *	less than 2**(-61.11) for VAX D FORMAT, 2**(-57.45) for IEEE DOUBLE.
118*2fe8fb19SBen Gras  *
119*2fe8fb19SBen Gras  * Constants:
120*2fe8fb19SBen Gras  * The hexadecimal values are the intended ones for the following constants.
121*2fe8fb19SBen Gras  * The decimal values may be used, provided that the compiler will convert
122*2fe8fb19SBen Gras  * from decimal to binary accurately enough to produce the hexadecimal values
123*2fe8fb19SBen Gras  * shown.
124*2fe8fb19SBen Gras  *
125*2fe8fb19SBen Gras  */
126*2fe8fb19SBen Gras 
127*2fe8fb19SBen Gras vc(S0, -1.6666666666666646660E-1  ,aaaa,bf2a,aa71,aaaa,  -2, -.AAAAAAAAAAAA71)
128*2fe8fb19SBen Gras vc(S1,  8.3333333333297230413E-3  ,8888,3d08,477f,8888,  -6,  .8888888888477F)
129*2fe8fb19SBen Gras vc(S2, -1.9841269838362403710E-4  ,0d00,ba50,1057,cf8a, -12, -.D00D00CF8A1057)
130*2fe8fb19SBen Gras vc(S3,  2.7557318019967078930E-6  ,ef1c,3738,bedc,a326, -18,  .B8EF1CA326BEDC)
131*2fe8fb19SBen Gras vc(S4, -2.5051841873876551398E-8  ,3195,b3d7,e1d3,374c, -25, -.D73195374CE1D3)
132*2fe8fb19SBen Gras vc(S5,  1.6028995389845827653E-10 ,3d9c,3030,cccc,6d26, -32,  .B03D9C6D26CCCC)
133*2fe8fb19SBen Gras vc(S6, -6.2723499671769283121E-13 ,8d0b,ac30,ea82,7561, -40, -.B08D0B7561EA82)
134*2fe8fb19SBen Gras 
135*2fe8fb19SBen Gras ic(S0, -1.6666666666666463126E-1  ,  -3, -1.555555555550C)
136*2fe8fb19SBen Gras ic(S1,  8.3333333332992771264E-3  ,  -7,  1.111111110C461)
137*2fe8fb19SBen Gras ic(S2, -1.9841269816180999116E-4  , -13, -1.A01A019746345)
138*2fe8fb19SBen Gras ic(S3,  2.7557309793219876880E-6  , -19,  1.71DE3209CDCD9)
139*2fe8fb19SBen Gras ic(S4, -2.5050225177523807003E-8  , -26, -1.AE5C0E319A4EF)
140*2fe8fb19SBen Gras ic(S5,  1.5868926979889205164E-10 , -33,  1.5CF61DF672B13)
141*2fe8fb19SBen Gras 
142*2fe8fb19SBen Gras #ifdef vccast
143*2fe8fb19SBen Gras #define	S0	vccast(S0)
144*2fe8fb19SBen Gras #define	S1	vccast(S1)
145*2fe8fb19SBen Gras #define	S2	vccast(S2)
146*2fe8fb19SBen Gras #define	S3	vccast(S3)
147*2fe8fb19SBen Gras #define	S4	vccast(S4)
148*2fe8fb19SBen Gras #define	S5	vccast(S5)
149*2fe8fb19SBen Gras #define	S6	vccast(S6)
150*2fe8fb19SBen Gras #endif
151*2fe8fb19SBen Gras 
152*2fe8fb19SBen Gras #if defined(__vax__)||defined(tahoe)
153*2fe8fb19SBen Gras #  define	sin__S(z)	(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*(S5+z*S6)))))))
154*2fe8fb19SBen Gras #else 	/* defined(__vax__)||defined(tahoe) */
155*2fe8fb19SBen Gras #  define	sin__S(z)	(z*(S0+z*(S1+z*(S2+z*(S3+z*(S4+z*S5))))))
156*2fe8fb19SBen Gras #endif 	/* defined(__vax__)||defined(tahoe) */
157*2fe8fb19SBen Gras 
158*2fe8fb19SBen Gras /* cos__C(x*x) ... re-implemented as a macro
159*2fe8fb19SBen Gras  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
160*2fe8fb19SBen Gras  * STATIC KERNEL FUNCTION OF SIN(X), COS(X), AND TAN(X)
161*2fe8fb19SBen Gras  * CODED IN C BY K.C. NG, 1/21/85;
162*2fe8fb19SBen Gras  * REVISED BY K.C. NG on 8/13/85.
163*2fe8fb19SBen Gras  *
164*2fe8fb19SBen Gras  *	   		    x*x
165*2fe8fb19SBen Gras  * RETURN   cos(k*x) - 1 + ----- on [-PI/4,PI/4],  where k = pi/PI,
166*2fe8fb19SBen Gras  *	  		     2
167*2fe8fb19SBen Gras  * PI is the rounded value of pi in machine precision :
168*2fe8fb19SBen Gras  *
169*2fe8fb19SBen Gras  *	Decimal:
170*2fe8fb19SBen Gras  *		pi = 3.141592653589793 23846264338327 .....
171*2fe8fb19SBen Gras  *    53 bits   PI = 3.141592653589793 115997963 ..... ,
172*2fe8fb19SBen Gras  *    56 bits   PI = 3.141592653589793 227020265 ..... ,
173*2fe8fb19SBen Gras  *
174*2fe8fb19SBen Gras  *	Hexadecimal:
175*2fe8fb19SBen Gras  *		pi = 3.243F6A8885A308D313198A2E....
176*2fe8fb19SBen Gras  *    53 bits   PI = 3.243F6A8885A30  =  2 * 1.921FB54442D18
177*2fe8fb19SBen Gras  *    56 bits   PI = 3.243F6A8885A308 =  4 * .C90FDAA22168C2
178*2fe8fb19SBen Gras  *
179*2fe8fb19SBen Gras  *
180*2fe8fb19SBen Gras  * Method:
181*2fe8fb19SBen Gras  *	1. Let z=x*x. Create a polynomial approximation to
182*2fe8fb19SBen Gras  *	    cos(k*x)-1+z/2  =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
183*2fe8fb19SBen Gras  *	then
184*2fe8fb19SBen Gras  *      cos__C(z) =  z*z*(C0 + C1*z^1 + ... + C5*z^5)
185*2fe8fb19SBen Gras  *
186*2fe8fb19SBen Gras  *	The coefficient C's are obtained by a special Remez algorithm.
187*2fe8fb19SBen Gras  *
188*2fe8fb19SBen Gras  * Accuracy:
189*2fe8fb19SBen Gras  *	In the absence of rounding error, the approximation has absolute error
190*2fe8fb19SBen Gras  *	less than 2**(-64) for VAX D FORMAT, 2**(-58.3) for IEEE DOUBLE.
191*2fe8fb19SBen Gras  *
192*2fe8fb19SBen Gras  *
193*2fe8fb19SBen Gras  * Constants:
194*2fe8fb19SBen Gras  * The hexadecimal values are the intended ones for the following constants.
195*2fe8fb19SBen Gras  * The decimal values may be used, provided that the compiler will convert
196*2fe8fb19SBen Gras  * from decimal to binary accurately enough to produce the hexadecimal values
197*2fe8fb19SBen Gras  * shown.
198*2fe8fb19SBen Gras  */
199*2fe8fb19SBen Gras 
200*2fe8fb19SBen Gras vc(C0,  4.1666666666666504759E-2  ,aaaa,3e2a,a9f0,aaaa,  -4,  .AAAAAAAAAAA9F0)
201*2fe8fb19SBen Gras vc(C1, -1.3888888888865302059E-3  ,0b60,bbb6,0cca,b60a,  -9, -.B60B60B60A0CCA)
202*2fe8fb19SBen Gras vc(C2,  2.4801587285601038265E-5  ,0d00,38d0,098f,cdcd, -15,  .D00D00CDCD098F)
203*2fe8fb19SBen Gras vc(C3, -2.7557313470902390219E-7  ,f27b,b593,e805,b593, -21, -.93F27BB593E805)
204*2fe8fb19SBen Gras vc(C4,  2.0875623401082232009E-9  ,74c8,320f,3ff0,fa1e, -28,  .8F74C8FA1E3FF0)
205*2fe8fb19SBen Gras vc(C5, -1.1355178117642986178E-11 ,c32d,ae47,5a63,0a5c, -36, -.C7C32D0A5C5A63)
206*2fe8fb19SBen Gras 
207*2fe8fb19SBen Gras ic(C0,  4.1666666666666504759E-2  ,  -5,  1.555555555553E)
208*2fe8fb19SBen Gras ic(C1, -1.3888888888865301516E-3  , -10, -1.6C16C16C14199)
209*2fe8fb19SBen Gras ic(C2,  2.4801587269650015769E-5  , -16,  1.A01A01971CAEB)
210*2fe8fb19SBen Gras ic(C3, -2.7557304623183959811E-7  , -22, -1.27E4F1314AD1A)
211*2fe8fb19SBen Gras ic(C4,  2.0873958177697780076E-9  , -29,  1.1EE3B60DDDC8C)
212*2fe8fb19SBen Gras ic(C5, -1.1250289076471311557E-11 , -37, -1.8BD5986B2A52E)
213*2fe8fb19SBen Gras 
214*2fe8fb19SBen Gras #ifdef vccast
215*2fe8fb19SBen Gras #define	C0	vccast(C0)
216*2fe8fb19SBen Gras #define	C1	vccast(C1)
217*2fe8fb19SBen Gras #define	C2	vccast(C2)
218*2fe8fb19SBen Gras #define	C3	vccast(C3)
219*2fe8fb19SBen Gras #define	C4	vccast(C4)
220*2fe8fb19SBen Gras #define	C5	vccast(C5)
221*2fe8fb19SBen Gras #endif
222*2fe8fb19SBen Gras 
223*2fe8fb19SBen Gras #define cos__C(z)	(z*z*(C0+z*(C1+z*(C2+z*(C3+z*(C4+z*C5))))))
224