Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/LD/sincospil.c
+++ new/usr/src/lib/libm/common/LD/sincospil.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25 /*
26 26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 30 #pragma weak sincospil = __sincospil
31 31
32 32 /*
33 33 * void sincospil(long double x, long double *s, long double *c)
34 34 * *s = sinl(pi*x); *c = cosl(pi*x);
35 35 *
36 36 * Algorithm, 10/17/2002, K.C. Ng
37 37 * ------------------------------
38 38 * Let y = |4x|, z = floor(y), and n = (int)(z mod 8.0) (displayed in binary).
39 39 * 1. If y==z, then x is a multiple of pi/4. Return the following values:
40 40 * ---------------------------------------------------
41 41 * n x mod 2 sin(x*pi) cos(x*pi) tan(x*pi)
42 42 * ---------------------------------------------------
43 43 * 000 0.00 +0 ___ +1 ___ +0
44 44 * 001 0.25 +\/0.5 +\/0.5 +1
45 45 * 010 0.50 +1 ___ +0 ___ +inf
46 46 * 011 0.75 +\/0.5 -\/0.5 -1
47 47 * 100 1.00 -0 ___ -1 ___ +0
48 48 * 101 1.25 -\/0.5 -\/0.5 +1
49 49 * 110 1.50 -1 ___ -0 ___ +inf
50 50 * 111 1.75 -\/0.5 +\/0.5 -1
51 51 * ---------------------------------------------------
52 52 * 2. Otherwise,
53 53 * ---------------------------------------------------
54 54 * n t sin(x*pi) cos(x*pi) tan(x*pi)
55 55 * ---------------------------------------------------
56 56 * 000 (y-z)/4 sinpi(t) cospi(t) tanpi(t)
57 57 * 001 (z+1-y)/4 cospi(t) sinpi(t) 1/tanpi(t)
58 58 * 010 (y-z)/4 cospi(t) -sinpi(t) -1/tanpi(t)
59 59 * 011 (z+1-y)/4 sinpi(t) -cospi(t) -tanpi(t)
60 60 * 100 (y-z)/4 -sinpi(t) -cospi(t) tanpi(t)
61 61 * 101 (z+1-y)/4 -cospi(t) -sinpi(t) 1/tanpi(t)
62 62 * 110 (y-z)/4 -cospi(t) sinpi(t) -1/tanpi(t)
63 63 * 111 (z+1-y)/4 -sinpi(t) cospi(t) -tanpi(t)
64 64 * ---------------------------------------------------
65 65 *
66 66 * NOTE. This program compute sinpi/cospi(t<0.25) by __k_sin/cos(pi*t, 0.0).
67 67 * This will return a result with error slightly more than one ulp (but less
68 68 * than 2 ulp). If one wants accurate result, one may break up pi*t in
69 69 * high (tpi_h) and low (tpi_l) parts and call __k_sin/cos(tip_h, tip_lo)
70 70 * instead.
↓ open down ↓ |
70 lines elided |
↑ open up ↑ |
71 71 */
72 72
73 73 #include "libm.h"
74 74 #include "libm_synonyms.h"
75 75 #include "longdouble.h"
76 76
77 77 #include <sys/isa_defs.h>
78 78
79 79 #define I(q, m) ((int *) &(q))[m]
80 80 #define U(q, m) ((unsigned *) &(q))[m]
81 -#if defined(_LITTLE_ENDIAN)
81 +#if defined(__i386) || defined(__amd64)
82 82 #define LDBL_MOST_SIGNIF_I(ld) ((I(ld, 2) << 16) | (0xffff & (I(ld, 1) >> 15)))
83 83 #define LDBL_LEAST_SIGNIF_U(ld) U(ld, 0)
84 84 #define PREC 64
85 85 #define PRECM1 63
86 86 #define PRECM2 62
87 87 static const long double twoPRECM2 = 9.223372036854775808000000000000000e+18L;
88 88 #else
89 89 #define LDBL_MOST_SIGNIF_I(ld) I(ld, 0)
90 90 #define LDBL_LEAST_SIGNIF_U(ld) U(ld, sizeof(long double) / sizeof(int) - 1)
91 91 #define PREC 113
92 92 #define PRECM1 112
93 93 #define PRECM2 111
94 94 static const long double twoPRECM2 = 5.192296858534827628530496329220096e+33L;
95 95 #endif
96 96
97 97 static const long double
98 98 zero = 0.0L,
99 99 quater = 0.25L,
100 100 one = 1.0L,
101 101 pi = 3.141592653589793238462643383279502884197e+0000L,
102 102 sqrth = 0.707106781186547524400844362104849039284835937688474,
103 103 tiny = 1.0e-100;
104 104
105 105 void
106 106 sincospil(long double x, long double *s, long double *c) {
107 107 long double y, z, t;
108 108 int hx, n, k;
109 109 unsigned lx;
110 110
111 111 hx = LDBL_MOST_SIGNIF_I(x);
112 112 lx = LDBL_LEAST_SIGNIF_U(x);
113 113 k = ((hx & 0x7fff0000) >> 16) - 0x3fff;
114 114 if (k >= PRECM2) { /* |x| >= 2**(Prec-2) */
115 115 if (k >= 16384) {
116 116 *s = *c = x - x;
117 117 }
118 118 else {
119 119 if (k >= PREC) {
120 120 *s = zero;
121 121 *c = one;
122 122 }
123 123 else if (k == PRECM1) {
124 124 if ((lx & 1) == 0) {
125 125 *s = zero;
126 126 *c = one;
127 127 }
128 128 else {
129 129 *s = -zero;
130 130 *c = -one;
131 131 }
132 132 }
133 133 else { /* k = Prec - 2 */
134 134 if ((lx & 1) == 0) {
135 135 *s = zero;
136 136 *c = one;
137 137 }
138 138 else {
139 139 *s = one;
140 140 *c = zero;
141 141 }
142 142 if ((lx & 2) != 0) {
143 143 *s = -*s;
144 144 *c = -*c;
145 145 }
146 146 }
147 147 }
148 148 }
149 149 else if (k < -2) /* |x| < 0.25 */
150 150 *s = __k_sincosl(pi * fabsl(x), zero, c);
151 151 else {
152 152 /* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */
153 153 y = 4.0L * fabsl(x);
154 154 if (k < PRECM2) {
155 155 z = y + twoPRECM2;
156 156 n = LDBL_LEAST_SIGNIF_U(z) & 7; /* 3 LSb of z */
157 157 t = z - twoPRECM2;
158 158 k = 0;
159 159 if (t == y)
160 160 k = 1;
161 161 else if (t > y) {
162 162 n -= 1;
163 163 t = quater + (y - t) * quater;
164 164 }
165 165 else
166 166 t = (y - t) * quater;
167 167 }
168 168 else { /* k = Prec-3 */
169 169 n = LDBL_LEAST_SIGNIF_U(y) & 7; /* 3 LSb of z */
170 170 k = 1;
171 171 }
172 172 if (k) { /* x = N/4 */
173 173 if((n & 1) != 0)
174 174 *s = *c = sqrth + tiny;
175 175 else
176 176 if ((n & 2) == 0) {
177 177 *s = zero;
178 178 *c = one;
179 179 }
180 180 else {
181 181 *s = one;
182 182 *c = zero;
183 183 }
184 184 if ((n & 4) != 0)
185 185 *s = -*s;
186 186 if (((n + 1) & 4) != 0)
187 187 *c = -*c;
188 188 }
189 189 else {
190 190 if ((n & 1) != 0)
191 191 t = quater - t;
192 192 if (((n + (n & 1)) & 2) == 0)
193 193 *s = __k_sincosl(pi * t, zero, c);
194 194 else
195 195 *c = __k_sincosl(pi * t, zero, s);
196 196 if ((n & 4) != 0)
197 197 *s = -*s;
198 198 if (((n + 2) & 4) != 0)
199 199 *c = -*c;
200 200 }
201 201 }
202 202 if (hx < 0)
203 203 *s = -*s;
204 204 }
205 205 #undef U
206 206 #undef LDBL_LEAST_SIGNIF_U
207 207 #undef I
208 208 #undef LDBL_MOST_SIGNIF_I
↓ open down ↓ |
117 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX