xref: /titanic_44/usr/src/lib/libm/common/LD/sincospil.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  * void sincospil(long double x, long double *s, long double *c)
32*25c28e83SPiotr Jasiukajtis  * *s = sinl(pi*x); *c = cosl(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 #include <sys/isa_defs.h>
75*25c28e83SPiotr Jasiukajtis 
76*25c28e83SPiotr Jasiukajtis #define I(q, m)	((int *) &(q))[m]
77*25c28e83SPiotr Jasiukajtis #define U(q, m)	((unsigned *) &(q))[m]
78*25c28e83SPiotr Jasiukajtis #if defined(__i386) || defined(__amd64)
79*25c28e83SPiotr Jasiukajtis #define LDBL_MOST_SIGNIF_I(ld)	((I(ld, 2) << 16) | (0xffff & (I(ld, 1) >> 15)))
80*25c28e83SPiotr Jasiukajtis #define LDBL_LEAST_SIGNIF_U(ld)	U(ld, 0)
81*25c28e83SPiotr Jasiukajtis #define PREC	64
82*25c28e83SPiotr Jasiukajtis #define PRECM1	63
83*25c28e83SPiotr Jasiukajtis #define PRECM2	62
84*25c28e83SPiotr Jasiukajtis static const long double twoPRECM2 = 9.223372036854775808000000000000000e+18L;
85*25c28e83SPiotr Jasiukajtis #else
86*25c28e83SPiotr Jasiukajtis #define LDBL_MOST_SIGNIF_I(ld)	I(ld, 0)
87*25c28e83SPiotr Jasiukajtis #define LDBL_LEAST_SIGNIF_U(ld)	U(ld, sizeof(long double) / sizeof(int) - 1)
88*25c28e83SPiotr Jasiukajtis #define PREC	113
89*25c28e83SPiotr Jasiukajtis #define PRECM1	112
90*25c28e83SPiotr Jasiukajtis #define PRECM2	111
91*25c28e83SPiotr Jasiukajtis static const long double twoPRECM2 = 5.192296858534827628530496329220096e+33L;
92*25c28e83SPiotr Jasiukajtis #endif
93*25c28e83SPiotr Jasiukajtis 
94*25c28e83SPiotr Jasiukajtis static const long double
95*25c28e83SPiotr Jasiukajtis zero	= 0.0L,
96*25c28e83SPiotr Jasiukajtis quater	= 0.25L,
97*25c28e83SPiotr Jasiukajtis one	= 1.0L,
98*25c28e83SPiotr Jasiukajtis pi	= 3.141592653589793238462643383279502884197e+0000L,
99*25c28e83SPiotr Jasiukajtis sqrth	= 0.707106781186547524400844362104849039284835937688474,
100*25c28e83SPiotr Jasiukajtis tiny	= 1.0e-100;
101*25c28e83SPiotr Jasiukajtis 
102*25c28e83SPiotr Jasiukajtis void
sincospil(long double x,long double * s,long double * c)103*25c28e83SPiotr Jasiukajtis sincospil(long double x, long double *s, long double *c) {
104*25c28e83SPiotr Jasiukajtis 	long double y, z, t;
105*25c28e83SPiotr Jasiukajtis 	int hx, n, k;
106*25c28e83SPiotr Jasiukajtis 	unsigned lx;
107*25c28e83SPiotr Jasiukajtis 
108*25c28e83SPiotr Jasiukajtis 	hx = LDBL_MOST_SIGNIF_I(x);
109*25c28e83SPiotr Jasiukajtis 	lx = LDBL_LEAST_SIGNIF_U(x);
110*25c28e83SPiotr Jasiukajtis 	k = ((hx & 0x7fff0000) >> 16) - 0x3fff;
111*25c28e83SPiotr Jasiukajtis 	if (k >= PRECM2) {		/* |x| >= 2**(Prec-2) */
112*25c28e83SPiotr Jasiukajtis 		if (k >= 16384) {
113*25c28e83SPiotr Jasiukajtis 			*s = *c = x - x;
114*25c28e83SPiotr Jasiukajtis 		}
115*25c28e83SPiotr Jasiukajtis 		else {
116*25c28e83SPiotr Jasiukajtis 			if (k >= PREC) {
117*25c28e83SPiotr Jasiukajtis 				*s = zero;
118*25c28e83SPiotr Jasiukajtis 				*c = one;
119*25c28e83SPiotr Jasiukajtis 			}
120*25c28e83SPiotr Jasiukajtis 			else if (k == PRECM1) {
121*25c28e83SPiotr Jasiukajtis 				if ((lx & 1) == 0) {
122*25c28e83SPiotr Jasiukajtis 					*s = zero;
123*25c28e83SPiotr Jasiukajtis 					*c = one;
124*25c28e83SPiotr Jasiukajtis 				}
125*25c28e83SPiotr Jasiukajtis 				else {
126*25c28e83SPiotr Jasiukajtis 					*s = -zero;
127*25c28e83SPiotr Jasiukajtis 					*c = -one;
128*25c28e83SPiotr Jasiukajtis 				}
129*25c28e83SPiotr Jasiukajtis 			}
130*25c28e83SPiotr Jasiukajtis 			else {	/* k = Prec - 2 */
131*25c28e83SPiotr Jasiukajtis 				if ((lx & 1) == 0) {
132*25c28e83SPiotr Jasiukajtis 					*s = zero;
133*25c28e83SPiotr Jasiukajtis 					*c = one;
134*25c28e83SPiotr Jasiukajtis 				}
135*25c28e83SPiotr Jasiukajtis 				else {
136*25c28e83SPiotr Jasiukajtis 					*s = one;
137*25c28e83SPiotr Jasiukajtis 					*c = zero;
138*25c28e83SPiotr Jasiukajtis 				}
139*25c28e83SPiotr Jasiukajtis 				if ((lx & 2) != 0) {
140*25c28e83SPiotr Jasiukajtis 					*s = -*s;
141*25c28e83SPiotr Jasiukajtis 					*c = -*c;
142*25c28e83SPiotr Jasiukajtis 				}
143*25c28e83SPiotr Jasiukajtis 			}
144*25c28e83SPiotr Jasiukajtis 		}
145*25c28e83SPiotr Jasiukajtis 	}
146*25c28e83SPiotr Jasiukajtis 	else if (k < -2) 	/* |x| < 0.25 */
147*25c28e83SPiotr Jasiukajtis 		*s = __k_sincosl(pi * fabsl(x), zero, c);
148*25c28e83SPiotr Jasiukajtis 	else {
149*25c28e83SPiotr Jasiukajtis 		/* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */
150*25c28e83SPiotr Jasiukajtis 		y = 4.0L * fabsl(x);
151*25c28e83SPiotr Jasiukajtis 		if (k < PRECM2) {
152*25c28e83SPiotr Jasiukajtis 			z = y + twoPRECM2;
153*25c28e83SPiotr Jasiukajtis 			n = LDBL_LEAST_SIGNIF_U(z) & 7;	/* 3 LSb of z */
154*25c28e83SPiotr Jasiukajtis 			t = z - twoPRECM2;
155*25c28e83SPiotr Jasiukajtis 			k = 0;
156*25c28e83SPiotr Jasiukajtis 			if (t == y)
157*25c28e83SPiotr Jasiukajtis 				k = 1;
158*25c28e83SPiotr Jasiukajtis 			else if (t > y) {
159*25c28e83SPiotr Jasiukajtis 				n -= 1;
160*25c28e83SPiotr Jasiukajtis 				t = quater + (y - t) * quater;
161*25c28e83SPiotr Jasiukajtis 			}
162*25c28e83SPiotr Jasiukajtis 			else
163*25c28e83SPiotr Jasiukajtis 				t = (y - t) * quater;
164*25c28e83SPiotr Jasiukajtis 		}
165*25c28e83SPiotr Jasiukajtis 		else { 	/* k = Prec-3 */
166*25c28e83SPiotr Jasiukajtis 			n = LDBL_LEAST_SIGNIF_U(y) & 7;	/* 3 LSb of z */
167*25c28e83SPiotr Jasiukajtis 			k = 1;
168*25c28e83SPiotr Jasiukajtis 		}
169*25c28e83SPiotr Jasiukajtis 		if (k) {	/* x = N/4 */
170*25c28e83SPiotr Jasiukajtis 			if ((n & 1) != 0)
171*25c28e83SPiotr Jasiukajtis 				*s = *c = sqrth + tiny;
172*25c28e83SPiotr Jasiukajtis 			else
173*25c28e83SPiotr Jasiukajtis 				if ((n & 2) == 0) {
174*25c28e83SPiotr Jasiukajtis 					*s = zero;
175*25c28e83SPiotr Jasiukajtis 					*c = one;
176*25c28e83SPiotr Jasiukajtis 				}
177*25c28e83SPiotr Jasiukajtis 				else {
178*25c28e83SPiotr Jasiukajtis 					*s = one;
179*25c28e83SPiotr Jasiukajtis 					*c = zero;
180*25c28e83SPiotr Jasiukajtis 				}
181*25c28e83SPiotr Jasiukajtis 			if ((n & 4) != 0)
182*25c28e83SPiotr Jasiukajtis 				*s = -*s;
183*25c28e83SPiotr Jasiukajtis 			if (((n + 1) & 4) != 0)
184*25c28e83SPiotr Jasiukajtis 				*c = -*c;
185*25c28e83SPiotr Jasiukajtis 		}
186*25c28e83SPiotr Jasiukajtis 		else {
187*25c28e83SPiotr Jasiukajtis 			if ((n & 1) != 0)
188*25c28e83SPiotr Jasiukajtis 				t = quater - t;
189*25c28e83SPiotr Jasiukajtis 			if (((n + (n & 1)) & 2) == 0)
190*25c28e83SPiotr Jasiukajtis 				*s = __k_sincosl(pi * t, zero, c);
191*25c28e83SPiotr Jasiukajtis 			else
192*25c28e83SPiotr Jasiukajtis 				*c = __k_sincosl(pi * t, zero, s);
193*25c28e83SPiotr Jasiukajtis 			if ((n & 4) != 0)
194*25c28e83SPiotr Jasiukajtis 				*s = -*s;
195*25c28e83SPiotr Jasiukajtis 			if (((n + 2) & 4) != 0)
196*25c28e83SPiotr Jasiukajtis 				*c = -*c;
197*25c28e83SPiotr Jasiukajtis 		}
198*25c28e83SPiotr Jasiukajtis 	}
199*25c28e83SPiotr Jasiukajtis 	if (hx < 0)
200*25c28e83SPiotr Jasiukajtis 		*s = -*s;
201*25c28e83SPiotr Jasiukajtis }
202*25c28e83SPiotr Jasiukajtis #undef U
203*25c28e83SPiotr Jasiukajtis #undef LDBL_LEAST_SIGNIF_U
204*25c28e83SPiotr Jasiukajtis #undef I
205*25c28e83SPiotr Jasiukajtis #undef LDBL_MOST_SIGNIF_I
206