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]
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
31 * void sincospil(long double x, long double *s, long double *c)
32 * *s = sinl(pi*x); *c = cosl(pi*x);
34 * Algorithm, 10/17/2002, K.C. Ng
35 * ------------------------------
36 * Let y = |4x|, z = floor(y), and n = (int)(z mod 8.0) (displayed in binary).
37 * 1. If y == z, then x is a multiple of pi/4. Return the following values:
38 * ---------------------------------------------------
39 * n x mod 2 sin(x*pi) cos(x*pi) tan(x*pi)
40 * ---------------------------------------------------
41 * 000 0.00 +0 ___ +1 ___ +0
42 * 001 0.25 +\/0.5 +\/0.5 +1
43 * 010 0.50 +1 ___ +0 ___ +inf
44 * 011 0.75 +\/0.5 -\/0.5 -1
45 * 100 1.00 -0 ___ -1 ___ +0
46 * 101 1.25 -\/0.5 -\/0.5 +1
47 * 110 1.50 -1 ___ -0 ___ +inf
48 * 111 1.75 -\/0.5 +\/0.5 -1
49 * ---------------------------------------------------
51 * ---------------------------------------------------
52 * n t sin(x*pi) cos(x*pi) tan(x*pi)
53 * ---------------------------------------------------
54 * 000 (y-z)/4 sinpi(t) cospi(t) tanpi(t)
55 * 001 (z+1-y)/4 cospi(t) sinpi(t) 1/tanpi(t)
56 * 010 (y-z)/4 cospi(t) -sinpi(t) -1/tanpi(t)
57 * 011 (z+1-y)/4 sinpi(t) -cospi(t) -tanpi(t)
58 * 100 (y-z)/4 -sinpi(t) -cospi(t) tanpi(t)
59 * 101 (z+1-y)/4 -cospi(t) -sinpi(t) 1/tanpi(t)
60 * 110 (y-z)/4 -cospi(t) sinpi(t) -1/tanpi(t)
61 * 111 (z+1-y)/4 -sinpi(t) cospi(t) -tanpi(t)
62 * ---------------------------------------------------
64 * NOTE. This program compute sinpi/cospi(t<0.25) by __k_sin/cos(pi*t, 0.0).
65 * This will return a result with error slightly more than one ulp (but less
66 * than 2 ulp). If one wants accurate result, one may break up pi*t in
67 * high (tpi_h) and low (tpi_l) parts and call __k_sin/cos(tip_h, tip_lo)
72 #include "longdouble.h"
74 #include <sys/isa_defs.h>
76 #define I(q, m) ((int *) &(q))[m]
77 #define U(q, m) ((unsigned *) &(q))[m]
78 #if defined(__i386) || defined(__amd64)
79 #define LDBL_MOST_SIGNIF_I(ld) ((I(ld, 2) << 16) | (0xffff & (I(ld, 1) >> 15)))
80 #define LDBL_LEAST_SIGNIF_U(ld) U(ld, 0)
84 static const long double twoPRECM2
= 9.223372036854775808000000000000000e+18L;
86 #define LDBL_MOST_SIGNIF_I(ld) I(ld, 0)
87 #define LDBL_LEAST_SIGNIF_U(ld) U(ld, sizeof(long double) / sizeof(int) - 1)
91 static const long double twoPRECM2
= 5.192296858534827628530496329220096e+33L;
94 static const long double
98 pi
= 3.141592653589793238462643383279502884197e+0000L,
99 sqrth
= 0.707106781186547524400844362104849039284835937688474,
103 sincospil(long double x
, long double *s
, long double *c
) {
108 hx
= LDBL_MOST_SIGNIF_I(x
);
109 lx
= LDBL_LEAST_SIGNIF_U(x
);
110 k
= ((hx
& 0x7fff0000) >> 16) - 0x3fff;
111 if (k
>= PRECM2
) { /* |x| >= 2**(Prec-2) */
120 else if (k
== PRECM1
) {
130 else { /* k = Prec - 2 */
146 else if (k
< -2) /* |x| < 0.25 */
147 *s
= __k_sincosl(pi
* fabsl(x
), zero
, c
);
149 /* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */
153 n
= LDBL_LEAST_SIGNIF_U(z
) & 7; /* 3 LSb of z */
160 t
= quater
+ (y
- t
) * quater
;
163 t
= (y
- t
) * quater
;
165 else { /* k = Prec-3 */
166 n
= LDBL_LEAST_SIGNIF_U(y
) & 7; /* 3 LSb of z */
169 if (k
) { /* x = N/4 */
171 *s
= *c
= sqrth
+ tiny
;
183 if (((n
+ 1) & 4) != 0)
189 if (((n
+ (n
& 1)) & 2) == 0)
190 *s
= __k_sincosl(pi
* t
, zero
, c
);
192 *c
= __k_sincosl(pi
* t
, zero
, s
);
195 if (((n
+ 2) & 4) != 0)
203 #undef LDBL_LEAST_SIGNIF_U
205 #undef LDBL_MOST_SIGNIF_I