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 /*
23*25c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24*25c28e83SPiotr Jasiukajtis */
25*25c28e83SPiotr Jasiukajtis /*
26*25c28e83SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27*25c28e83SPiotr Jasiukajtis * Use is subject to license terms.
28*25c28e83SPiotr Jasiukajtis */
29*25c28e83SPiotr Jasiukajtis
30*25c28e83SPiotr Jasiukajtis /* long double sinpil(long double x),
31*25c28e83SPiotr Jasiukajtis * return long double precision sinl(pi*x).
32*25c28e83SPiotr Jasiukajtis *
33*25c28e83SPiotr Jasiukajtis * Algorithm, 10/17/2002, K.C. Ng
34*25c28e83SPiotr Jasiukajtis * ------------------------------
35*25c28e83SPiotr Jasiukajtis * Let y = |4x|, z = floor(y), and n = (int)(z mod 8.0) (displayed in binary).
36*25c28e83SPiotr Jasiukajtis * 1. If y == z, then x is a multiple of pi/4. Return the following values:
37*25c28e83SPiotr Jasiukajtis * ---------------------------------------------------
38*25c28e83SPiotr Jasiukajtis * n x mod 2 sin(x*pi) cos(x*pi) tan(x*pi)
39*25c28e83SPiotr Jasiukajtis * ---------------------------------------------------
40*25c28e83SPiotr Jasiukajtis * 000 0.00 +0 ___ +1 ___ +0
41*25c28e83SPiotr Jasiukajtis * 001 0.25 +\/0.5 +\/0.5 +1
42*25c28e83SPiotr Jasiukajtis * 010 0.50 +1 ___ +0 ___ +inf
43*25c28e83SPiotr Jasiukajtis * 011 0.75 +\/0.5 -\/0.5 -1
44*25c28e83SPiotr Jasiukajtis * 100 1.00 -0 ___ -1 ___ +0
45*25c28e83SPiotr Jasiukajtis * 101 1.25 -\/0.5 -\/0.5 +1
46*25c28e83SPiotr Jasiukajtis * 110 1.50 -1 ___ -0 ___ +inf
47*25c28e83SPiotr Jasiukajtis * 111 1.75 -\/0.5 +\/0.5 -1
48*25c28e83SPiotr Jasiukajtis * ---------------------------------------------------
49*25c28e83SPiotr Jasiukajtis * 2. Otherwise,
50*25c28e83SPiotr Jasiukajtis * ---------------------------------------------------
51*25c28e83SPiotr Jasiukajtis * n t sin(x*pi) cos(x*pi) tan(x*pi)
52*25c28e83SPiotr Jasiukajtis * ---------------------------------------------------
53*25c28e83SPiotr Jasiukajtis * 000 (y-z)/4 sinpi(t) cospi(t) tanpi(t)
54*25c28e83SPiotr Jasiukajtis * 001 (z+1-y)/4 cospi(t) sinpi(t) 1/tanpi(t)
55*25c28e83SPiotr Jasiukajtis * 010 (y-z)/4 cospi(t) -sinpi(t) -1/tanpi(t)
56*25c28e83SPiotr Jasiukajtis * 011 (z+1-y)/4 sinpi(t) -cospi(t) -tanpi(t)
57*25c28e83SPiotr Jasiukajtis * 100 (y-z)/4 -sinpi(t) -cospi(t) tanpi(t)
58*25c28e83SPiotr Jasiukajtis * 101 (z+1-y)/4 -cospi(t) -sinpi(t) 1/tanpi(t)
59*25c28e83SPiotr Jasiukajtis * 110 (y-z)/4 -cospi(t) sinpi(t) -1/tanpi(t)
60*25c28e83SPiotr Jasiukajtis * 111 (z+1-y)/4 -sinpi(t) cospi(t) -tanpi(t)
61*25c28e83SPiotr Jasiukajtis * ---------------------------------------------------
62*25c28e83SPiotr Jasiukajtis *
63*25c28e83SPiotr Jasiukajtis * NOTE. This program compute sinpi/cospi(t<0.25) by __k_sin/cos(pi*t, 0.0).
64*25c28e83SPiotr Jasiukajtis * This will return a result with error slightly more than one ulp (but less
65*25c28e83SPiotr Jasiukajtis * than 2 ulp). If one wants accurate result, one may break up pi*t in
66*25c28e83SPiotr Jasiukajtis * high (tpi_h) and low (tpi_l) parts and call __k_sin/cos(tip_h, tip_lo)
67*25c28e83SPiotr Jasiukajtis * instead.
68*25c28e83SPiotr Jasiukajtis */
69*25c28e83SPiotr Jasiukajtis
70*25c28e83SPiotr Jasiukajtis #include "libm.h"
71*25c28e83SPiotr Jasiukajtis #include "longdouble.h"
72*25c28e83SPiotr Jasiukajtis
73*25c28e83SPiotr Jasiukajtis #include <sys/isa_defs.h>
74*25c28e83SPiotr Jasiukajtis
75*25c28e83SPiotr Jasiukajtis #define I(q, m) ((int *) &(q))[m]
76*25c28e83SPiotr Jasiukajtis #define U(q, m) ((unsigned *) &(q))[m]
77*25c28e83SPiotr Jasiukajtis #if defined(__i386) || defined(__amd64)
78*25c28e83SPiotr Jasiukajtis #define LDBL_MOST_SIGNIF_I(ld) ((I(ld, 2) << 16) | (0xffff & (I(ld, 1) >> 15)))
79*25c28e83SPiotr Jasiukajtis #define LDBL_LEAST_SIGNIF_U(ld) U(ld, 0)
80*25c28e83SPiotr Jasiukajtis #define PREC 64
81*25c28e83SPiotr Jasiukajtis #define PRECM1 63
82*25c28e83SPiotr Jasiukajtis #define PRECM2 62
83*25c28e83SPiotr Jasiukajtis static const long double twoPRECM2 = 9.223372036854775808000000000000000e+18L;
84*25c28e83SPiotr Jasiukajtis #else
85*25c28e83SPiotr Jasiukajtis #define LDBL_MOST_SIGNIF_I(ld) I(ld, 0)
86*25c28e83SPiotr Jasiukajtis #define LDBL_LEAST_SIGNIF_U(ld) U(ld, sizeof(long double) / sizeof(int) - 1)
87*25c28e83SPiotr Jasiukajtis #define PREC 113
88*25c28e83SPiotr Jasiukajtis #define PRECM1 112
89*25c28e83SPiotr Jasiukajtis #define PRECM2 111
90*25c28e83SPiotr Jasiukajtis static const long double twoPRECM2 = 5.192296858534827628530496329220096e+33L;
91*25c28e83SPiotr Jasiukajtis #endif
92*25c28e83SPiotr Jasiukajtis
93*25c28e83SPiotr Jasiukajtis static const long double
94*25c28e83SPiotr Jasiukajtis zero = 0.0L,
95*25c28e83SPiotr Jasiukajtis quater = 0.25L,
96*25c28e83SPiotr Jasiukajtis one = 1.0L,
97*25c28e83SPiotr Jasiukajtis pi = 3.141592653589793238462643383279502884197e+0000L,
98*25c28e83SPiotr Jasiukajtis sqrth = 0.707106781186547524400844362104849039284835937688474,
99*25c28e83SPiotr Jasiukajtis tiny = 1.0e-100;
100*25c28e83SPiotr Jasiukajtis
101*25c28e83SPiotr Jasiukajtis long double
sinpil(long double x)102*25c28e83SPiotr Jasiukajtis sinpil(long double x) {
103*25c28e83SPiotr Jasiukajtis long double y, z, t;
104*25c28e83SPiotr Jasiukajtis int hx, n, k;
105*25c28e83SPiotr Jasiukajtis unsigned lx;
106*25c28e83SPiotr Jasiukajtis
107*25c28e83SPiotr Jasiukajtis hx = LDBL_MOST_SIGNIF_I(x);
108*25c28e83SPiotr Jasiukajtis lx = LDBL_LEAST_SIGNIF_U(x);
109*25c28e83SPiotr Jasiukajtis k = ((hx & 0x7fff0000) >> 16) - 0x3fff;
110*25c28e83SPiotr Jasiukajtis if (k >= PRECM2) { /* |x| >= 2**(Prec-2) */
111*25c28e83SPiotr Jasiukajtis if (k >= 16384)
112*25c28e83SPiotr Jasiukajtis y = x - x;
113*25c28e83SPiotr Jasiukajtis else {
114*25c28e83SPiotr Jasiukajtis if (k >= PREC)
115*25c28e83SPiotr Jasiukajtis y = zero;
116*25c28e83SPiotr Jasiukajtis else if (k == PRECM1)
117*25c28e83SPiotr Jasiukajtis y = (lx & 1) == 0 ? zero: -zero;
118*25c28e83SPiotr Jasiukajtis else { /* k = Prec - 2 */
119*25c28e83SPiotr Jasiukajtis y = (lx & 1) == 0 ? zero : one;
120*25c28e83SPiotr Jasiukajtis if ((lx & 2) != 0)
121*25c28e83SPiotr Jasiukajtis y = -y;
122*25c28e83SPiotr Jasiukajtis }
123*25c28e83SPiotr Jasiukajtis }
124*25c28e83SPiotr Jasiukajtis }
125*25c28e83SPiotr Jasiukajtis else if (k < -2) /* |x| < 0.25 */
126*25c28e83SPiotr Jasiukajtis y = __k_sinl(pi * fabsl(x), zero);
127*25c28e83SPiotr Jasiukajtis else {
128*25c28e83SPiotr Jasiukajtis /* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */
129*25c28e83SPiotr Jasiukajtis y = 4.0L * fabsl(x);
130*25c28e83SPiotr Jasiukajtis if (k < PRECM2) {
131*25c28e83SPiotr Jasiukajtis z = y + twoPRECM2;
132*25c28e83SPiotr Jasiukajtis n = LDBL_LEAST_SIGNIF_U(z) & 7; /* 3 LSb of z */
133*25c28e83SPiotr Jasiukajtis t = z - twoPRECM2;
134*25c28e83SPiotr Jasiukajtis k = 0;
135*25c28e83SPiotr Jasiukajtis if (t == y)
136*25c28e83SPiotr Jasiukajtis k = 1;
137*25c28e83SPiotr Jasiukajtis else if (t > y) {
138*25c28e83SPiotr Jasiukajtis n -= 1;
139*25c28e83SPiotr Jasiukajtis t = quater + (y - t) * quater;
140*25c28e83SPiotr Jasiukajtis }
141*25c28e83SPiotr Jasiukajtis else
142*25c28e83SPiotr Jasiukajtis t = (y - t) * quater;
143*25c28e83SPiotr Jasiukajtis }
144*25c28e83SPiotr Jasiukajtis else { /* k = Prec-3 */
145*25c28e83SPiotr Jasiukajtis n = LDBL_LEAST_SIGNIF_U(y) & 7; /* 3 LSb of z */
146*25c28e83SPiotr Jasiukajtis k = 1;
147*25c28e83SPiotr Jasiukajtis }
148*25c28e83SPiotr Jasiukajtis if (k) { /* x = N/4 */
149*25c28e83SPiotr Jasiukajtis if ((n & 1) != 0)
150*25c28e83SPiotr Jasiukajtis y = sqrth + tiny;
151*25c28e83SPiotr Jasiukajtis else
152*25c28e83SPiotr Jasiukajtis y = (n & 2) == 0 ? zero : one;
153*25c28e83SPiotr Jasiukajtis if ((n & 4) != 0)
154*25c28e83SPiotr Jasiukajtis y = -y;
155*25c28e83SPiotr Jasiukajtis }
156*25c28e83SPiotr Jasiukajtis else {
157*25c28e83SPiotr Jasiukajtis if ((n & 1) != 0)
158*25c28e83SPiotr Jasiukajtis t = quater - t;
159*25c28e83SPiotr Jasiukajtis if (((n + (n & 1)) & 2) == 0)
160*25c28e83SPiotr Jasiukajtis y = __k_sinl(pi * t, zero);
161*25c28e83SPiotr Jasiukajtis else
162*25c28e83SPiotr Jasiukajtis y = __k_cosl(pi * t, zero);
163*25c28e83SPiotr Jasiukajtis if ((n & 4) != 0)
164*25c28e83SPiotr Jasiukajtis y = -y;
165*25c28e83SPiotr Jasiukajtis }
166*25c28e83SPiotr Jasiukajtis }
167*25c28e83SPiotr Jasiukajtis return hx >= 0 ? y : -y;
168*25c28e83SPiotr Jasiukajtis }
169*25c28e83SPiotr Jasiukajtis #undef U
170*25c28e83SPiotr Jasiukajtis #undef LDBL_LEAST_SIGNIF_U
171*25c28e83SPiotr Jasiukajtis #undef I
172*25c28e83SPiotr Jasiukajtis #undef LDBL_MOST_SIGNIF_I
173