1*5b2ba9d3SPiotr Jasiukajtis /*
2*5b2ba9d3SPiotr Jasiukajtis * CDDL HEADER START
3*5b2ba9d3SPiotr Jasiukajtis *
4*5b2ba9d3SPiotr Jasiukajtis * The contents of this file are subject to the terms of the
5*5b2ba9d3SPiotr Jasiukajtis * Common Development and Distribution License (the "License").
6*5b2ba9d3SPiotr Jasiukajtis * You may not use this file except in compliance with the License.
7*5b2ba9d3SPiotr Jasiukajtis *
8*5b2ba9d3SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5b2ba9d3SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
10*5b2ba9d3SPiotr Jasiukajtis * See the License for the specific language governing permissions
11*5b2ba9d3SPiotr Jasiukajtis * and limitations under the License.
12*5b2ba9d3SPiotr Jasiukajtis *
13*5b2ba9d3SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
14*5b2ba9d3SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5b2ba9d3SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
16*5b2ba9d3SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
17*5b2ba9d3SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
18*5b2ba9d3SPiotr Jasiukajtis *
19*5b2ba9d3SPiotr Jasiukajtis * CDDL HEADER END
20*5b2ba9d3SPiotr Jasiukajtis */
21*5b2ba9d3SPiotr Jasiukajtis
22*5b2ba9d3SPiotr Jasiukajtis /*
23*5b2ba9d3SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24*5b2ba9d3SPiotr Jasiukajtis */
25*5b2ba9d3SPiotr Jasiukajtis /*
26*5b2ba9d3SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27*5b2ba9d3SPiotr Jasiukajtis * Use is subject to license terms.
28*5b2ba9d3SPiotr Jasiukajtis */
29*5b2ba9d3SPiotr Jasiukajtis
30*5b2ba9d3SPiotr Jasiukajtis /* INDENT OFF */
31*5b2ba9d3SPiotr Jasiukajtis /*
32*5b2ba9d3SPiotr Jasiukajtis * void sincospi(double x, double *s, double *c)
33*5b2ba9d3SPiotr Jasiukajtis * *s = sin(pi*x); *c = cos(pi*x);
34*5b2ba9d3SPiotr Jasiukajtis *
35*5b2ba9d3SPiotr Jasiukajtis * Algorithm, 10/17/2002, K.C. Ng
36*5b2ba9d3SPiotr Jasiukajtis * ------------------------------
37*5b2ba9d3SPiotr Jasiukajtis * Let y = |4x|, z = floor(y), and n = (int)(z mod 8.0) (displayed in binary).
38*5b2ba9d3SPiotr Jasiukajtis * 1. If y == z, then x is a multiple of pi/4. Return the following values:
39*5b2ba9d3SPiotr Jasiukajtis * ---------------------------------------------------
40*5b2ba9d3SPiotr Jasiukajtis * n x mod 2 sin(x*pi) cos(x*pi) tan(x*pi)
41*5b2ba9d3SPiotr Jasiukajtis * ---------------------------------------------------
42*5b2ba9d3SPiotr Jasiukajtis * 000 0.00 +0 ___ +1 ___ +0
43*5b2ba9d3SPiotr Jasiukajtis * 001 0.25 +\/0.5 +\/0.5 +1
44*5b2ba9d3SPiotr Jasiukajtis * 010 0.50 +1 ___ +0 ___ +inf
45*5b2ba9d3SPiotr Jasiukajtis * 011 0.75 +\/0.5 -\/0.5 -1
46*5b2ba9d3SPiotr Jasiukajtis * 100 1.00 -0 ___ -1 ___ +0
47*5b2ba9d3SPiotr Jasiukajtis * 101 1.25 -\/0.5 -\/0.5 +1
48*5b2ba9d3SPiotr Jasiukajtis * 110 1.50 -1 ___ -0 ___ +inf
49*5b2ba9d3SPiotr Jasiukajtis * 111 1.75 -\/0.5 +\/0.5 -1
50*5b2ba9d3SPiotr Jasiukajtis * ---------------------------------------------------
51*5b2ba9d3SPiotr Jasiukajtis * 2. Otherwise,
52*5b2ba9d3SPiotr Jasiukajtis * ---------------------------------------------------
53*5b2ba9d3SPiotr Jasiukajtis * n t sin(x*pi) cos(x*pi) tan(x*pi)
54*5b2ba9d3SPiotr Jasiukajtis * ---------------------------------------------------
55*5b2ba9d3SPiotr Jasiukajtis * 000 (y-z)/4 sinpi(t) cospi(t) tanpi(t)
56*5b2ba9d3SPiotr Jasiukajtis * 001 (z+1-y)/4 cospi(t) sinpi(t) 1/tanpi(t)
57*5b2ba9d3SPiotr Jasiukajtis * 010 (y-z)/4 cospi(t) -sinpi(t) -1/tanpi(t)
58*5b2ba9d3SPiotr Jasiukajtis * 011 (z+1-y)/4 sinpi(t) -cospi(t) -tanpi(t)
59*5b2ba9d3SPiotr Jasiukajtis * 100 (y-z)/4 -sinpi(t) -cospi(t) tanpi(t)
60*5b2ba9d3SPiotr Jasiukajtis * 101 (z+1-y)/4 -cospi(t) -sinpi(t) 1/tanpi(t)
61*5b2ba9d3SPiotr Jasiukajtis * 110 (y-z)/4 -cospi(t) sinpi(t) -1/tanpi(t)
62*5b2ba9d3SPiotr Jasiukajtis * 111 (z+1-y)/4 -sinpi(t) cospi(t) -tanpi(t)
63*5b2ba9d3SPiotr Jasiukajtis * ---------------------------------------------------
64*5b2ba9d3SPiotr Jasiukajtis *
65*5b2ba9d3SPiotr Jasiukajtis * NOTE. This program compute sinpi/cospi(t<0.25) by __k_sin/cos(pi*t, 0.0).
66*5b2ba9d3SPiotr Jasiukajtis * This will return a result with error slightly more than one ulp (but less
67*5b2ba9d3SPiotr Jasiukajtis * than 2 ulp). If one wants accurate result, one may break up pi*t in
68*5b2ba9d3SPiotr Jasiukajtis * high (tpi_h) and low (tpi_l) parts and call __k_sin/cos(tip_h, tip_lo)
69*5b2ba9d3SPiotr Jasiukajtis * instead.
70*5b2ba9d3SPiotr Jasiukajtis */
71*5b2ba9d3SPiotr Jasiukajtis
72*5b2ba9d3SPiotr Jasiukajtis #include "libm.h"
73*5b2ba9d3SPiotr Jasiukajtis #include "libm_protos.h"
74*5b2ba9d3SPiotr Jasiukajtis #include "libm_macros.h"
75*5b2ba9d3SPiotr Jasiukajtis #include <math.h>
76*5b2ba9d3SPiotr Jasiukajtis #if defined(__SUNPRO_C)
77*5b2ba9d3SPiotr Jasiukajtis #include <sunmath.h>
78*5b2ba9d3SPiotr Jasiukajtis #endif
79*5b2ba9d3SPiotr Jasiukajtis
80*5b2ba9d3SPiotr Jasiukajtis static const double
81*5b2ba9d3SPiotr Jasiukajtis pi = 3.14159265358979323846, /* 400921FB,54442D18 */
82*5b2ba9d3SPiotr Jasiukajtis sqrth_h = 0.70710678118654757273731092936941422522068023681640625,
83*5b2ba9d3SPiotr Jasiukajtis sqrth_l = -4.8336466567264565185935844299127932213411660131004e-17;
84*5b2ba9d3SPiotr Jasiukajtis /* INDENT ON */
85*5b2ba9d3SPiotr Jasiukajtis
86*5b2ba9d3SPiotr Jasiukajtis void
sincospi(double x,double * s,double * c)87*5b2ba9d3SPiotr Jasiukajtis sincospi(double x, double *s, double *c) {
88*5b2ba9d3SPiotr Jasiukajtis double y, z, t;
89*5b2ba9d3SPiotr Jasiukajtis int n, ix, k;
90*5b2ba9d3SPiotr Jasiukajtis int hx = ((int *) &x)[HIWORD];
91*5b2ba9d3SPiotr Jasiukajtis unsigned h, lx = ((unsigned *) &x)[LOWORD];
92*5b2ba9d3SPiotr Jasiukajtis
93*5b2ba9d3SPiotr Jasiukajtis ix = hx & ~0x80000000;
94*5b2ba9d3SPiotr Jasiukajtis n = (ix >> 20) - 0x3ff;
95*5b2ba9d3SPiotr Jasiukajtis if (n >= 51) { /* |x| >= 2**51 */
96*5b2ba9d3SPiotr Jasiukajtis if (n >= 1024)
97*5b2ba9d3SPiotr Jasiukajtis #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN)
98*5b2ba9d3SPiotr Jasiukajtis *s = *c = ix >= 0x7ff80000 ? x : x - x;
99*5b2ba9d3SPiotr Jasiukajtis /* assumes sparc-like QNaN */
100*5b2ba9d3SPiotr Jasiukajtis #else
101*5b2ba9d3SPiotr Jasiukajtis *s = *c = x - x;
102*5b2ba9d3SPiotr Jasiukajtis #endif
103*5b2ba9d3SPiotr Jasiukajtis else {
104*5b2ba9d3SPiotr Jasiukajtis if (n >= 53) {
105*5b2ba9d3SPiotr Jasiukajtis *s = 0.0;
106*5b2ba9d3SPiotr Jasiukajtis *c = 1.0;
107*5b2ba9d3SPiotr Jasiukajtis }
108*5b2ba9d3SPiotr Jasiukajtis else if (n == 52) {
109*5b2ba9d3SPiotr Jasiukajtis if ((lx & 1) == 0) {
110*5b2ba9d3SPiotr Jasiukajtis *s = 0.0;
111*5b2ba9d3SPiotr Jasiukajtis *c = 1.0;
112*5b2ba9d3SPiotr Jasiukajtis }
113*5b2ba9d3SPiotr Jasiukajtis else {
114*5b2ba9d3SPiotr Jasiukajtis *s = -0.0;
115*5b2ba9d3SPiotr Jasiukajtis *c = -1.0;
116*5b2ba9d3SPiotr Jasiukajtis }
117*5b2ba9d3SPiotr Jasiukajtis }
118*5b2ba9d3SPiotr Jasiukajtis else { /* n == 51 */
119*5b2ba9d3SPiotr Jasiukajtis if ((lx & 1) == 0) {
120*5b2ba9d3SPiotr Jasiukajtis *s = 0.0;
121*5b2ba9d3SPiotr Jasiukajtis *c = 1.0;
122*5b2ba9d3SPiotr Jasiukajtis }
123*5b2ba9d3SPiotr Jasiukajtis else {
124*5b2ba9d3SPiotr Jasiukajtis *s = 1.0;
125*5b2ba9d3SPiotr Jasiukajtis *c = 0.0;
126*5b2ba9d3SPiotr Jasiukajtis }
127*5b2ba9d3SPiotr Jasiukajtis if ((lx & 2) != 0) {
128*5b2ba9d3SPiotr Jasiukajtis *s = -*s;
129*5b2ba9d3SPiotr Jasiukajtis *c = -*c;
130*5b2ba9d3SPiotr Jasiukajtis }
131*5b2ba9d3SPiotr Jasiukajtis }
132*5b2ba9d3SPiotr Jasiukajtis }
133*5b2ba9d3SPiotr Jasiukajtis }
134*5b2ba9d3SPiotr Jasiukajtis else if (n < -2) /* |x| < 0.25 */
135*5b2ba9d3SPiotr Jasiukajtis *s = __k_sincos(pi * fabs(x), 0.0, c);
136*5b2ba9d3SPiotr Jasiukajtis else {
137*5b2ba9d3SPiotr Jasiukajtis /* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */
138*5b2ba9d3SPiotr Jasiukajtis if (ix < 0x41C00000) { /* |x| < 2**29 */
139*5b2ba9d3SPiotr Jasiukajtis y = 4.0 * fabs(x);
140*5b2ba9d3SPiotr Jasiukajtis n = (int) y; /* exact */
141*5b2ba9d3SPiotr Jasiukajtis z = (double) n;
142*5b2ba9d3SPiotr Jasiukajtis k = z == y;
143*5b2ba9d3SPiotr Jasiukajtis t = (y - z) * 0.25;
144*5b2ba9d3SPiotr Jasiukajtis }
145*5b2ba9d3SPiotr Jasiukajtis else { /* 2**29 <= |x| < 2**51 */
146*5b2ba9d3SPiotr Jasiukajtis y = fabs(x);
147*5b2ba9d3SPiotr Jasiukajtis k = 50 - n;
148*5b2ba9d3SPiotr Jasiukajtis n = lx >> k;
149*5b2ba9d3SPiotr Jasiukajtis h = n << k;
150*5b2ba9d3SPiotr Jasiukajtis ((unsigned *) &z)[LOWORD] = h;
151*5b2ba9d3SPiotr Jasiukajtis ((int *) &z)[HIWORD] = ix;
152*5b2ba9d3SPiotr Jasiukajtis k = h == lx;
153*5b2ba9d3SPiotr Jasiukajtis t = y - z;
154*5b2ba9d3SPiotr Jasiukajtis }
155*5b2ba9d3SPiotr Jasiukajtis if (k) { /* x = N/4 */
156*5b2ba9d3SPiotr Jasiukajtis if ((n & 1) != 0)
157*5b2ba9d3SPiotr Jasiukajtis *s = *c = sqrth_h + sqrth_l;
158*5b2ba9d3SPiotr Jasiukajtis else
159*5b2ba9d3SPiotr Jasiukajtis if ((n & 2) == 0) {
160*5b2ba9d3SPiotr Jasiukajtis *s = 0.0;
161*5b2ba9d3SPiotr Jasiukajtis *c = 1.0;
162*5b2ba9d3SPiotr Jasiukajtis }
163*5b2ba9d3SPiotr Jasiukajtis else {
164*5b2ba9d3SPiotr Jasiukajtis *s = 1.0;
165*5b2ba9d3SPiotr Jasiukajtis *c = 0.0;
166*5b2ba9d3SPiotr Jasiukajtis }
167*5b2ba9d3SPiotr Jasiukajtis y = (n & 2) == 0 ? 0.0 : 1.0;
168*5b2ba9d3SPiotr Jasiukajtis if ((n & 4) != 0)
169*5b2ba9d3SPiotr Jasiukajtis *s = -*s;
170*5b2ba9d3SPiotr Jasiukajtis if (((n + 1) & 4) != 0)
171*5b2ba9d3SPiotr Jasiukajtis *c = -*c;
172*5b2ba9d3SPiotr Jasiukajtis }
173*5b2ba9d3SPiotr Jasiukajtis else {
174*5b2ba9d3SPiotr Jasiukajtis if ((n & 1) != 0)
175*5b2ba9d3SPiotr Jasiukajtis t = 0.25 - t;
176*5b2ba9d3SPiotr Jasiukajtis if (((n + (n & 1)) & 2) == 0)
177*5b2ba9d3SPiotr Jasiukajtis *s = __k_sincos(pi * t, 0.0, c);
178*5b2ba9d3SPiotr Jasiukajtis else
179*5b2ba9d3SPiotr Jasiukajtis *c = __k_sincos(pi * t, 0.0, s);
180*5b2ba9d3SPiotr Jasiukajtis if ((n & 4) != 0)
181*5b2ba9d3SPiotr Jasiukajtis *s = -*s;
182*5b2ba9d3SPiotr Jasiukajtis if (((n + 2) & 4) != 0)
183*5b2ba9d3SPiotr Jasiukajtis *c = -*c;
184*5b2ba9d3SPiotr Jasiukajtis }
185*5b2ba9d3SPiotr Jasiukajtis }
186*5b2ba9d3SPiotr Jasiukajtis if (hx < 0)
187*5b2ba9d3SPiotr Jasiukajtis *s = -*s;
188*5b2ba9d3SPiotr Jasiukajtis }
189