4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
31 * double __k_sincos(double x, double y, double *c);
32 * kernel sincos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
33 * Input x is assumed to be bounded by ~pi/4 in magnitude.
34 * Input y is the tail of x.
35 * return sin(x) with *c = cos(x)
37 * Accurate Table look-up algorithm by K.C. Ng, May, 1995.
39 * 1. Reduce x to x>0 by sin(-x)=-sin(x),cos(-x)=cos(x).
40 * 2. For 0<= x < pi/4, let i = (64*x chopped)-10. Let d = x - a[i], where
41 * a[i] is a double that is close to (i+10.5)/64 and such that
42 * sin(a[i]) and cos(a[i]) is close to a double (with error less
43 * than 2**-8 ulp). Then
44 * cos(x) = cos(a[i]+d) = cos(a[i])cos(d) - sin(a[i])*sin(d)
45 * = TBL_cos_a[i]*(1+QQ1*d^2+QQ2*d^4) -
46 * TBL_sin_a[i]*(d+PP1*d^3+PP2*d^5)
47 * = TBL_cos_a[i] + (TBL_cos_a[i]*d^2*(QQ1+QQ2*d^2) -
48 * TBL_sin_a[i]*(d+PP1*d^3+PP2*d^5))
49 * sin(x) = sin(a[i]+d) = sin(a[i])cos(d) + cos(a[i])*sin(d)
50 * = TBL_sin_a[i]*(1+QQ1*d^2+QQ2*d^4) +
51 * TBL_cos_a[i]*(d+PP1*d^3+PP2*d^5)
52 * = TBL_sin_a[i] + (TBL_sin_a[i]*d^2*(QQ1+QQ2*d^2) +
53 * TBL_cos_a[i]*(d+PP1*d^3+PP2*d^5))
55 * For |y| less than 10.5/64 = 0.1640625, use
56 * sin(y) = y + y^3*(p1+y^2*(p2+y^2*(p3+y^2*p4)))
57 * cos(y) = 1 + y^2*(q1+y^2*(q2+y^2*(q3+y^2*q4)))
59 * For |y| less than 0.008, use
60 * sin(y) = y + y^3*(pp1+y^2*pp2)
61 * cos(y) = 1 + y^2*(qq1+y^2*qq2)
64 * TRIG(x) returns trig(x) nearly rounded (less than 1 ulp)
69 static const double sc
[] = {
73 * |sin(x) - (x+pp1*x^3+pp2*x^5)| <= 2^-58.79 for |x| < 0.008
75 /* PP1 = */ -0.166666666666316558867252052378889521480627858683055567,
76 /* PP2 = */ .008333315652997472323564894248466758248475374977974017927,
78 * |(sin(x) - (x+p1*x^3+...+p4*x^9)|
79 * |------------------------------ | <= 2^-57.63 for |x| < 0.1953125
82 /* P1 = */ -1.666666666666629669805215138920301589656e-0001,
83 /* P2 = */ 8.333333332390951295683993455280336376663e-0003,
84 /* P3 = */ -1.984126237997976692791551778230098403960e-0004,
85 /* P4 = */ 2.753403624854277237649987622848330351110e-0006,
87 * |cos(x) - (1+qq1*x^2+qq2*x^4)| <= 2^-55.99 for |x| <= 0.008 (0x3f80624d)
89 /* QQ1 = */ -0.4999999999975492381842911981948418542742729,
90 /* QQ2 = */ 0.041666542904352059294545209158357640398771740,
92 * |cos(x) - (1+q1*x^2+...+q4*x^8)| <= 2^-55.86 for |x| <= 0.1640625 (10.5/64)
95 /* Q2 = */ 4.166666666500350703680945520860748617445e-0002,
96 /* Q3 = */ -1.388888596436972210694266290577848696006e-0003,
97 /* Q4 = */ 2.478563078858589473679519517892953492192e-0005,
116 extern const double _TBL_sincos
[], _TBL_sincosx
[];
119 __k_sincos(double x
, double y
, double *c
) {
120 double z
, w
, s
, v
, p
, q
;
123 hx
= ((int *)&x
)[HIWORD
];
124 ix
= hx
& ~0x80000000;
126 if (ix
<= 0x3fc50000) { /* |x| < 10.5/64 = 0.164062500 */
127 if (ix
< 0x3e400000) { /* |x| < 2**-27 */
133 if (ix
< 0x3f800000) { /* |x| < 0.008 */
134 q
= z
* (QQ1
+ z
* QQ2
);
135 p
= (x
* z
) * (PP1
+ z
* PP2
) + y
;
137 q
= z
* ((Q1
+ z
* Q2
) + (z
* z
) * (Q3
+
139 p
= (x
* z
) * ((P1
+ z
* P2
) + (z
* z
) * (P3
+
145 } else { /* 0.164062500 < |x| < ~pi/4 */
147 i
= (((ix
>> 12) & 0xff) | 0x100) >> (0x401 - n
);
150 v
= -y
- (_TBL_sincosx
[j
] + x
);
152 v
= y
- (_TBL_sincosx
[j
] - x
);
156 z
= _TBL_sincos
[j
+1];
157 p
= s
* (PP1
+ s
* PP2
);
158 q
= s
* (QQ1
+ s
* QQ2
);
160 *c
= z
- (w
* p
- z
* q
);
162 return ((hx
>= 0)? w
+ s
: -(w
+ s
));