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