xref: /titanic_44/usr/src/lib/libm/common/C/__sincos.c (revision 25c28e83beb90e7c80452a7c818c5e6f73a07dc8)
1*25c28e83SPiotr Jasiukajtis /*
2*25c28e83SPiotr Jasiukajtis  * CDDL HEADER START
3*25c28e83SPiotr Jasiukajtis  *
4*25c28e83SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
5*25c28e83SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
6*25c28e83SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
7*25c28e83SPiotr Jasiukajtis  *
8*25c28e83SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*25c28e83SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
10*25c28e83SPiotr Jasiukajtis  * See the License for the specific language governing permissions
11*25c28e83SPiotr Jasiukajtis  * and limitations under the License.
12*25c28e83SPiotr Jasiukajtis  *
13*25c28e83SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
14*25c28e83SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*25c28e83SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
16*25c28e83SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
17*25c28e83SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
18*25c28e83SPiotr Jasiukajtis  *
19*25c28e83SPiotr Jasiukajtis  * CDDL HEADER END
20*25c28e83SPiotr Jasiukajtis  */
21*25c28e83SPiotr Jasiukajtis /*
22*25c28e83SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
23*25c28e83SPiotr Jasiukajtis  */
24*25c28e83SPiotr Jasiukajtis /*
25*25c28e83SPiotr Jasiukajtis  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26*25c28e83SPiotr Jasiukajtis  * Use is subject to license terms.
27*25c28e83SPiotr Jasiukajtis  */
28*25c28e83SPiotr Jasiukajtis 
29*25c28e83SPiotr Jasiukajtis /* INDENT OFF */
30*25c28e83SPiotr Jasiukajtis /*
31*25c28e83SPiotr Jasiukajtis  * double __k_sincos(double x, double y, double *c);
32*25c28e83SPiotr Jasiukajtis  * kernel sincos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
33*25c28e83SPiotr Jasiukajtis  * Input x is assumed to be bounded by ~pi/4 in magnitude.
34*25c28e83SPiotr Jasiukajtis  * Input y is the tail of x.
35*25c28e83SPiotr Jasiukajtis  * return sin(x) with *c = cos(x)
36*25c28e83SPiotr Jasiukajtis  *
37*25c28e83SPiotr Jasiukajtis  * Accurate Table look-up algorithm by K.C. Ng, May, 1995.
38*25c28e83SPiotr Jasiukajtis  *
39*25c28e83SPiotr Jasiukajtis  * 1. Reduce x to x>0 by sin(-x)=-sin(x),cos(-x)=cos(x).
40*25c28e83SPiotr Jasiukajtis  * 2. For 0<= x < pi/4, let i = (64*x chopped)-10. Let d = x - a[i], where
41*25c28e83SPiotr Jasiukajtis  *    a[i] is a double that is close to (i+10.5)/64 and such that
42*25c28e83SPiotr Jasiukajtis  *    sin(a[i]) and cos(a[i]) is close to a double (with error less
43*25c28e83SPiotr Jasiukajtis  *    than 2**-8 ulp). Then
44*25c28e83SPiotr Jasiukajtis  *	cos(x) = cos(a[i]+d) = cos(a[i])cos(d) - sin(a[i])*sin(d)
45*25c28e83SPiotr Jasiukajtis  *	       = TBL_cos_a[i]*(1+QQ1*d^2+QQ2*d^4) -
46*25c28e83SPiotr Jasiukajtis  *			TBL_sin_a[i]*(d+PP1*d^3+PP2*d^5)
47*25c28e83SPiotr Jasiukajtis  *	       = TBL_cos_a[i] + (TBL_cos_a[i]*d^2*(QQ1+QQ2*d^2) -
48*25c28e83SPiotr Jasiukajtis  *			TBL_sin_a[i]*(d+PP1*d^3+PP2*d^5))
49*25c28e83SPiotr Jasiukajtis  *	sin(x) = sin(a[i]+d) = sin(a[i])cos(d) + cos(a[i])*sin(d)
50*25c28e83SPiotr Jasiukajtis  *	       = TBL_sin_a[i]*(1+QQ1*d^2+QQ2*d^4) +
51*25c28e83SPiotr Jasiukajtis  *			TBL_cos_a[i]*(d+PP1*d^3+PP2*d^5)
52*25c28e83SPiotr Jasiukajtis  *	       = TBL_sin_a[i] + (TBL_sin_a[i]*d^2*(QQ1+QQ2*d^2) +
53*25c28e83SPiotr Jasiukajtis  *			TBL_cos_a[i]*(d+PP1*d^3+PP2*d^5))
54*25c28e83SPiotr Jasiukajtis  *
55*25c28e83SPiotr Jasiukajtis  *    For |y| less than 10.5/64 = 0.1640625, use
56*25c28e83SPiotr Jasiukajtis  *	sin(y) = y + y^3*(p1+y^2*(p2+y^2*(p3+y^2*p4)))
57*25c28e83SPiotr Jasiukajtis  *	cos(y) = 1 + y^2*(q1+y^2*(q2+y^2*(q3+y^2*q4)))
58*25c28e83SPiotr Jasiukajtis  *
59*25c28e83SPiotr Jasiukajtis  *    For |y| less than 0.008, use
60*25c28e83SPiotr Jasiukajtis  *	sin(y) = y + y^3*(pp1+y^2*pp2)
61*25c28e83SPiotr Jasiukajtis  *	cos(y) = 1 + y^2*(qq1+y^2*qq2)
62*25c28e83SPiotr Jasiukajtis  *
63*25c28e83SPiotr Jasiukajtis  * Accuracy:
64*25c28e83SPiotr Jasiukajtis  *	TRIG(x) returns trig(x) nearly rounded (less than 1 ulp)
65*25c28e83SPiotr Jasiukajtis  */
66*25c28e83SPiotr Jasiukajtis 
67*25c28e83SPiotr Jasiukajtis #include "libm.h"
68*25c28e83SPiotr Jasiukajtis 
69*25c28e83SPiotr Jasiukajtis static const double sc[] = {
70*25c28e83SPiotr Jasiukajtis /* ONE	= */  1.0,
71*25c28e83SPiotr Jasiukajtis /* NONE	= */ -1.0,
72*25c28e83SPiotr Jasiukajtis /*
73*25c28e83SPiotr Jasiukajtis  * |sin(x) - (x+pp1*x^3+pp2*x^5)| <= 2^-58.79 for |x| < 0.008
74*25c28e83SPiotr Jasiukajtis  */
75*25c28e83SPiotr Jasiukajtis /* PP1	= */ -0.166666666666316558867252052378889521480627858683055567,
76*25c28e83SPiotr Jasiukajtis /* PP2	= */   .008333315652997472323564894248466758248475374977974017927,
77*25c28e83SPiotr Jasiukajtis /*
78*25c28e83SPiotr Jasiukajtis  * |(sin(x) - (x+p1*x^3+...+p4*x^9)|
79*25c28e83SPiotr Jasiukajtis  * |------------------------------ | <= 2^-57.63 for |x| < 0.1953125
80*25c28e83SPiotr Jasiukajtis  * |                 x             |
81*25c28e83SPiotr Jasiukajtis  */
82*25c28e83SPiotr Jasiukajtis /* P1  	= */ -1.666666666666629669805215138920301589656e-0001,
83*25c28e83SPiotr Jasiukajtis /* P2  	= */  8.333333332390951295683993455280336376663e-0003,
84*25c28e83SPiotr Jasiukajtis /* P3  	= */ -1.984126237997976692791551778230098403960e-0004,
85*25c28e83SPiotr Jasiukajtis /* P4  	= */  2.753403624854277237649987622848330351110e-0006,
86*25c28e83SPiotr Jasiukajtis /*
87*25c28e83SPiotr Jasiukajtis  * |cos(x) - (1+qq1*x^2+qq2*x^4)| <= 2^-55.99 for |x| <= 0.008 (0x3f80624d)
88*25c28e83SPiotr Jasiukajtis  */
89*25c28e83SPiotr Jasiukajtis /* QQ1	= */ -0.4999999999975492381842911981948418542742729,
90*25c28e83SPiotr Jasiukajtis /* QQ2	= */  0.041666542904352059294545209158357640398771740,
91*25c28e83SPiotr Jasiukajtis /*
92*25c28e83SPiotr Jasiukajtis  * |cos(x) - (1+q1*x^2+...+q4*x^8)| <= 2^-55.86 for |x| <= 0.1640625 (10.5/64)
93*25c28e83SPiotr Jasiukajtis  */
94*25c28e83SPiotr Jasiukajtis /* Q1  	= */ -0.5,
95*25c28e83SPiotr Jasiukajtis /* Q2  	= */  4.166666666500350703680945520860748617445e-0002,
96*25c28e83SPiotr Jasiukajtis /* Q3  	= */ -1.388888596436972210694266290577848696006e-0003,
97*25c28e83SPiotr Jasiukajtis /* Q4  	= */  2.478563078858589473679519517892953492192e-0005,
98*25c28e83SPiotr Jasiukajtis };
99*25c28e83SPiotr Jasiukajtis /* INDENT ON */
100*25c28e83SPiotr Jasiukajtis 
101*25c28e83SPiotr Jasiukajtis #define	ONE	sc[0]
102*25c28e83SPiotr Jasiukajtis #define	NONE	sc[1]
103*25c28e83SPiotr Jasiukajtis #define	PP1	sc[2]
104*25c28e83SPiotr Jasiukajtis #define	PP2	sc[3]
105*25c28e83SPiotr Jasiukajtis #define	P1	sc[4]
106*25c28e83SPiotr Jasiukajtis #define	P2	sc[5]
107*25c28e83SPiotr Jasiukajtis #define	P3	sc[6]
108*25c28e83SPiotr Jasiukajtis #define	P4	sc[7]
109*25c28e83SPiotr Jasiukajtis #define	QQ1	sc[8]
110*25c28e83SPiotr Jasiukajtis #define	QQ2	sc[9]
111*25c28e83SPiotr Jasiukajtis #define	Q1	sc[10]
112*25c28e83SPiotr Jasiukajtis #define	Q2	sc[11]
113*25c28e83SPiotr Jasiukajtis #define	Q3	sc[12]
114*25c28e83SPiotr Jasiukajtis #define	Q4	sc[13]
115*25c28e83SPiotr Jasiukajtis 
116*25c28e83SPiotr Jasiukajtis extern const double _TBL_sincos[], _TBL_sincosx[];
117*25c28e83SPiotr Jasiukajtis 
118*25c28e83SPiotr Jasiukajtis double
__k_sincos(double x,double y,double * c)119*25c28e83SPiotr Jasiukajtis __k_sincos(double x, double y, double *c) {
120*25c28e83SPiotr Jasiukajtis 	double	z, w, s, v, p, q;
121*25c28e83SPiotr Jasiukajtis 	int	i, j, n, hx, ix;
122*25c28e83SPiotr Jasiukajtis 
123*25c28e83SPiotr Jasiukajtis 	hx = ((int *)&x)[HIWORD];
124*25c28e83SPiotr Jasiukajtis 	ix = hx & ~0x80000000;
125*25c28e83SPiotr Jasiukajtis 
126*25c28e83SPiotr Jasiukajtis 	if (ix <= 0x3fc50000) {	/* |x| < 10.5/64 = 0.164062500 */
127*25c28e83SPiotr Jasiukajtis 		if (ix < 0x3e400000) {	/* |x| < 2**-27 */
128*25c28e83SPiotr Jasiukajtis 			if ((int)x == 0)
129*25c28e83SPiotr Jasiukajtis 				*c = ONE;
130*25c28e83SPiotr Jasiukajtis 			return (x + y);
131*25c28e83SPiotr Jasiukajtis 		} else {
132*25c28e83SPiotr Jasiukajtis 			z = x * x;
133*25c28e83SPiotr Jasiukajtis 			if (ix < 0x3f800000) {	/* |x| < 0.008 */
134*25c28e83SPiotr Jasiukajtis 				q = z * (QQ1 + z * QQ2);
135*25c28e83SPiotr Jasiukajtis 				p = (x * z) * (PP1 + z * PP2) + y;
136*25c28e83SPiotr Jasiukajtis 			} else {
137*25c28e83SPiotr Jasiukajtis 				q = z * ((Q1 + z * Q2) + (z * z) * (Q3 +
138*25c28e83SPiotr Jasiukajtis 				    z * Q4));
139*25c28e83SPiotr Jasiukajtis 				p = (x * z) * ((P1 + z * P2) + (z * z) * (P3 +
140*25c28e83SPiotr Jasiukajtis 				    z * P4)) + y;
141*25c28e83SPiotr Jasiukajtis 			}
142*25c28e83SPiotr Jasiukajtis 			*c = ONE + q;
143*25c28e83SPiotr Jasiukajtis 			return (x + p);
144*25c28e83SPiotr Jasiukajtis 		}
145*25c28e83SPiotr Jasiukajtis 	} else {		/* 0.164062500 < |x| < ~pi/4 */
146*25c28e83SPiotr Jasiukajtis 		n = ix >> 20;
147*25c28e83SPiotr Jasiukajtis 		i = (((ix >> 12) & 0xff) | 0x100) >> (0x401 - n);
148*25c28e83SPiotr Jasiukajtis 		j = i - 10;
149*25c28e83SPiotr Jasiukajtis 		if (hx < 0)
150*25c28e83SPiotr Jasiukajtis 			v = -y - (_TBL_sincosx[j] + x);
151*25c28e83SPiotr Jasiukajtis 		else
152*25c28e83SPiotr Jasiukajtis 			v = y - (_TBL_sincosx[j] - x);
153*25c28e83SPiotr Jasiukajtis 		s = v * v;
154*25c28e83SPiotr Jasiukajtis 		j <<= 1;
155*25c28e83SPiotr Jasiukajtis 		w = _TBL_sincos[j];
156*25c28e83SPiotr Jasiukajtis 		z = _TBL_sincos[j+1];
157*25c28e83SPiotr Jasiukajtis 		p = s * (PP1 + s * PP2);
158*25c28e83SPiotr Jasiukajtis 		q = s * (QQ1 + s * QQ2);
159*25c28e83SPiotr Jasiukajtis 		p = v + v * p;
160*25c28e83SPiotr Jasiukajtis 		*c = z - (w * p - z * q);
161*25c28e83SPiotr Jasiukajtis 		s = w * q + z * p;
162*25c28e83SPiotr Jasiukajtis 		return ((hx >= 0)? w + s : -(w + s));
163*25c28e83SPiotr Jasiukajtis 	}
164*25c28e83SPiotr Jasiukajtis }
165