1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
23 */
24 /*
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
27 */
28
29 /*
30 * int __rem_pio2m(x,y,e0,nx,prec,ipio2)
31 * double x[],y[]; int e0,nx,prec; const int ipio2[];
32 *
33 * __rem_pio2m return the last three digits of N with
34 * y = x - N*pi/2
35 * so that |y| < pi/4.
36 *
37 * The method is to compute the integer (mod 8) and fraction parts of
38 * (2/pi)*x without doing the full multiplication. In general we
39 * skip the part of the product that are known to be a huge integer (
40 * more accurately, = 0 mod 8 ). Thus the number of operations are
41 * independent of the exponent of the input.
42 *
43 * (2/PI) is represented by an array of 24-bit integers in ipio2[].
124 * into 24 bits chunks.
125 *
126 * f[] ipio2[] in floating point
127 *
128 * iq[] integer array by breaking up q[] in 24-bits chunk.
129 *
130 * fq[] final product of x*(2/pi) in fq[0],..,fq[jk]
131 *
132 * ih integer. If >0 it indicats q[] is >= 0.5, hence
133 * it also indicates the *sign* of the result.
134 *
135 */
136
137 #include "libm.h"
138
139 #if defined(__i386) && !defined(__amd64)
140 extern int __swapRP(int);
141 #endif
142
143 static const int init_jk[] = { 3, 4, 4, 6 }; /* initial value for jk */
144
145 static const double pio2[] = {
146 1.57079625129699707031e+00,
147 7.54978941586159635335e-08,
148 5.39030252995776476554e-15,
149 3.28200341580791294123e-22,
150 1.27065575308067607349e-29,
151 1.22933308981111328932e-36,
152 2.73370053816464559624e-44,
153 2.16741683877804819444e-51,
154 };
155
156 static const double
157 zero = 0.0,
158 one = 1.0,
159 half = 0.5,
160 eight = 8.0,
161 eighth = 0.125,
162 two24 = 16777216.0,
163 twon24 = 5.960464477539062500E-8;
164
165 int
166 __rem_pio2m(double *x, double *y, int e0, int nx, int prec, const int *ipio2)
167 {
168 int jz, jx, jv, jp, jk, carry, n, iq[20];
169 int i, j, k, m, q0, ih;
170 double z, fw, f[20], fq[20], q[20];
171 #if defined(__i386) && !defined(__amd64)
172 int rp;
173
174 rp = __swapRP(fp_extended);
175 #endif
176
177 /* initialize jk */
178 jp = jk = init_jk[prec];
179
180 /* determine jx,jv,q0, note that 3>q0 */
181 jx = nx - 1;
182 jv = (e0 - 3) / 24;
183 if (jv < 0)
184 jv = 0;
185 q0 = e0 - 24 * (jv + 1);
186
187 /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
188 j = jv - jx;
189 m = jx + jk;
190 for (i = 0; i <= m; i++, j++)
191 f[i] = (j < 0)? zero : (double)ipio2[j];
192
193 /* compute q[0],q[1],...q[jk] */
194 for (i = 0; i <= jk; i++) {
195 for (j = 0, fw = zero; j <= jx; j++)
196 fw += x[j] * f[jx+i-j];
197 q[i] = fw;
198 }
199
200 jz = jk;
201 recompute:
202 /* distill q[] into iq[] reversingly */
203 for (i = 0, j = jz, z = q[jz]; j > 0; i++, j--) {
204 fw = (double)((int)(twon24 * z));
205 iq[i] = (int)(z - two24 * fw);
206 z = q[j-1] + fw;
207 }
208
209 /* compute n */
210 z = scalbn(z, q0); /* actual value of z */
211 z -= eight * floor(z * eighth); /* trim off integer >= 8 */
212 n = (int)z;
213 z -= (double)n;
214 ih = 0;
215 if (q0 > 0) { /* need iq[jz-1] to determine n */
216 i = (iq[jz-1] >> (24 - q0));
217 n += i;
218 iq[jz-1] -= i << (24 - q0);
219 ih = iq[jz-1] >> (23 - q0);
220 } else if (q0 == 0) {
221 ih = iq[jz-1] >> 23;
222 } else if (z >= half) {
223 ih = 2;
224 }
225
226 if (ih > 0) { /* q > 0.5 */
227 n += 1;
228 carry = 0;
229 for (i = 0; i < jz; i++) { /* compute 1-q */
230 j = iq[i];
231 if (carry == 0) {
232 if (j != 0) {
233 carry = 1;
234 iq[i] = 0x1000000 - j;
235 }
236 } else {
237 iq[i] = 0xffffff - j;
238 }
239 }
240 if (q0 > 0) { /* rare case: chance is 1 in 12 */
241 switch (q0) {
242 case 1:
243 iq[jz-1] &= 0x7fffff;
244 break;
245 case 2:
246 iq[jz-1] &= 0x3fffff;
247 break;
248 }
249 }
250 if (ih == 2) {
251 z = one - z;
252 if (carry != 0)
253 z -= scalbn(one, q0);
254 }
255 }
256
257 /* check if recomputation is needed */
258 if (z == zero) {
259 j = 0;
260 for (i = jz - 1; i >= jk; i--)
261 j |= iq[i];
262 if (j == 0) { /* need recomputation */
263 /* set k to no. of terms needed */
264 for (k = 1; iq[jk-k] == 0; k++)
265 ;
266
267 /* add q[jz+1] to q[jz+k] */
268 for (i = jz + 1; i <= jz + k; i++) {
269 f[jx+i] = (double)ipio2[jv+i];
270 for (j = 0, fw = zero; j <= jx; j++)
271 fw += x[j] * f[jx+i-j];
272 q[i] = fw;
273 }
274 jz += k;
275 goto recompute;
276 }
277 }
278
279 /* cut out zero terms */
280 if (z == zero) {
281 jz -= 1;
282 q0 -= 24;
283 while (iq[jz] == 0) {
284 jz--;
285 q0 -= 24;
286 }
287 } else { /* break z into 24-bit if neccessary */
288 z = scalbn(z, -q0);
289 if (z >= two24) {
290 fw = (double)((int)(twon24 * z));
291 iq[jz] = (int)(z - two24 * fw);
292 jz += 1;
293 q0 += 24;
294 iq[jz] = (int)fw;
295 } else {
296 iq[jz] = (int)z;
297 }
298 }
299
300 /* convert integer "bit" chunk to floating-point value */
301 fw = scalbn(one, q0);
302 for (i = jz; i >= 0; i--) {
303 q[i] = fw * (double)iq[i];
304 fw *= twon24;
305 }
306
307 /* compute pio2[0,...,jp]*q[jz,...,0] */
308 for (i = jz; i >= 0; i--) {
309 for (fw = zero, k = 0; k <= jp && k <= jz - i; k++)
310 fw += pio2[k] * q[i+k];
311 fq[jz-i] = fw;
312 }
313
314 /* compress fq[] into y[] */
315 switch (prec) {
316 case 0:
317 fw = zero;
318 for (i = jz; i >= 0; i--)
319 fw += fq[i];
320 y[0] = (ih == 0)? fw : -fw;
321 break;
322
323 case 1:
324 case 2:
325 fw = zero;
326 for (i = jz; i >= 0; i--)
327 fw += fq[i];
328 y[0] = (ih == 0)? fw : -fw;
329 fw = fq[0] - fw;
330 for (i = 1; i <= jz; i++)
331 fw += fq[i];
332 y[1] = (ih == 0)? fw : -fw;
333 break;
334
335 default:
336 for (i = jz; i > 0; i--) {
337 fw = fq[i-1] + fq[i];
338 fq[i] += fq[i-1] - fw;
339 fq[i-1] = fw;
340 }
341 for (i = jz; i > 1; i--) {
342 fw = fq[i-1] + fq[i];
343 fq[i] += fq[i-1] - fw;
344 fq[i-1] = fw;
345 }
346 for (fw = zero, i = jz; i >= 2; i--)
347 fw += fq[i];
348 if (ih == 0) {
349 y[0] = fq[0];
350 y[1] = fq[1];
351 y[2] = fw;
352 } else {
353 y[0] = -fq[0];
354 y[1] = -fq[1];
355 y[2] = -fw;
356 }
357 }
358
359 #if defined(__i386) && !defined(__amd64)
360 (void) __swapRP(rp);
361 #endif
362 return (n & 7);
363 }
|
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 */
25
26 /*
27 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30
31 /*
32 * int __rem_pio2m(x,y,e0,nx,prec,ipio2)
33 * double x[],y[]; int e0,nx,prec; const int ipio2[];
34 *
35 * __rem_pio2m return the last three digits of N with
36 * y = x - N*pi/2
37 * so that |y| < pi/4.
38 *
39 * The method is to compute the integer (mod 8) and fraction parts of
40 * (2/pi)*x without doing the full multiplication. In general we
41 * skip the part of the product that are known to be a huge integer (
42 * more accurately, = 0 mod 8 ). Thus the number of operations are
43 * independent of the exponent of the input.
44 *
45 * (2/PI) is represented by an array of 24-bit integers in ipio2[].
126 * into 24 bits chunks.
127 *
128 * f[] ipio2[] in floating point
129 *
130 * iq[] integer array by breaking up q[] in 24-bits chunk.
131 *
132 * fq[] final product of x*(2/pi) in fq[0],..,fq[jk]
133 *
134 * ih integer. If >0 it indicats q[] is >= 0.5, hence
135 * it also indicates the *sign* of the result.
136 *
137 */
138
139 #include "libm.h"
140
141 #if defined(__i386) && !defined(__amd64)
142 extern int __swapRP(int);
143 #endif
144
145 static const int init_jk[] = { 3, 4, 4, 6 }; /* initial value for jk */
146 static const double pio2[] = {
147 1.57079625129699707031e+00, 7.54978941586159635335e-08,
148 5.39030252995776476554e-15, 3.28200341580791294123e-22,
149 1.27065575308067607349e-29, 1.22933308981111328932e-36,
150 2.73370053816464559624e-44, 2.16741683877804819444e-51,
151 };
152
153 static const double zero = 0.0,
154 one = 1.0,
155 half = 0.5,
156 eight = 8.0,
157 eighth = 0.125,
158 two24 = 16777216.0,
159 twon24 = 5.960464477539062500E-8;
160
161 int
162 __rem_pio2m(double *x, double *y, int e0, int nx, int prec, const int *ipio2)
163 {
164 int jz, jx, jv, jp, jk, carry, n, iq[20];
165 int i, j, k, m, q0, ih;
166 double z, fw, f[20], fq[20], q[20];
167
168 #if defined(__i386) && !defined(__amd64)
169 int rp;
170
171 rp = __swapRP(fp_extended);
172 #endif
173
174 /* initialize jk */
175 jp = jk = init_jk[prec];
176
177 /* determine jx,jv,q0, note that 3>q0 */
178 jx = nx - 1;
179 jv = (e0 - 3) / 24;
180
181 if (jv < 0)
182 jv = 0;
183
184 q0 = e0 - 24 * (jv + 1);
185
186 /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
187 j = jv - jx;
188 m = jx + jk;
189
190 for (i = 0; i <= m; i++, j++)
191 f[i] = (j < 0) ? zero : (double)ipio2[j];
192
193 /* compute q[0],q[1],...q[jk] */
194 for (i = 0; i <= jk; i++) {
195 for (j = 0, fw = zero; j <= jx; j++)
196 fw += x[j] * f[jx + i - j];
197
198 q[i] = fw;
199 }
200
201 jz = jk;
202
203 recompute:
204 /* distill q[] into iq[] reversingly */
205 for (i = 0, j = jz, z = q[jz]; j > 0; i++, j--) {
206 fw = (double)((int)(twon24 * z));
207 iq[i] = (int)(z - two24 * fw);
208 z = q[j - 1] + fw;
209 }
210
211 /* compute n */
212 z = scalbn(z, q0); /* actual value of z */
213 z -= eight * floor(z * eighth); /* trim off integer >= 8 */
214 n = (int)z;
215 z -= (double)n;
216 ih = 0;
217
218 if (q0 > 0) { /* need iq[jz-1] to determine n */
219 i = (iq[jz - 1] >> (24 - q0));
220 n += i;
221 iq[jz - 1] -= i << (24 - q0);
222 ih = iq[jz - 1] >> (23 - q0);
223 } else if (q0 == 0) {
224 ih = iq[jz - 1] >> 23;
225 } else if (z >= half) {
226 ih = 2;
227 }
228
229 if (ih > 0) { /* q > 0.5 */
230 n += 1;
231 carry = 0;
232
233 for (i = 0; i < jz; i++) { /* compute 1-q */
234 j = iq[i];
235
236 if (carry == 0) {
237 if (j != 0) {
238 carry = 1;
239 iq[i] = 0x1000000 - j;
240 }
241 } else {
242 iq[i] = 0xffffff - j;
243 }
244 }
245
246 if (q0 > 0) { /* rare case: chance is 1 in 12 */
247 switch (q0) {
248 case 1:
249 iq[jz - 1] &= 0x7fffff;
250 break;
251 case 2:
252 iq[jz - 1] &= 0x3fffff;
253 break;
254 }
255 }
256
257 if (ih == 2) {
258 z = one - z;
259
260 if (carry != 0)
261 z -= scalbn(one, q0);
262 }
263 }
264
265 /* check if recomputation is needed */
266 if (z == zero) {
267 j = 0;
268
269 for (i = jz - 1; i >= jk; i--)
270 j |= iq[i];
271
272 if (j == 0) { /* need recomputation */
273 /* set k to no. of terms needed */
274 for (k = 1; iq[jk - k] == 0; k++)
275 ;
276
277 /* add q[jz+1] to q[jz+k] */
278 for (i = jz + 1; i <= jz + k; i++) {
279 f[jx + i] = (double)ipio2[jv + i];
280
281 for (j = 0, fw = zero; j <= jx; j++)
282 fw += x[j] * f[jx + i - j];
283
284 q[i] = fw;
285 }
286
287 jz += k;
288 goto recompute;
289 }
290 }
291
292 /* cut out zero terms */
293 if (z == zero) {
294 jz -= 1;
295 q0 -= 24;
296
297 while (iq[jz] == 0) {
298 jz--;
299 q0 -= 24;
300 }
301 } else { /* break z into 24-bit if neccessary */
302 z = scalbn(z, -q0);
303
304 if (z >= two24) {
305 fw = (double)((int)(twon24 * z));
306 iq[jz] = (int)(z - two24 * fw);
307 jz += 1;
308 q0 += 24;
309 iq[jz] = (int)fw;
310 } else {
311 iq[jz] = (int)z;
312 }
313 }
314
315 /* convert integer "bit" chunk to floating-point value */
316 fw = scalbn(one, q0);
317
318 for (i = jz; i >= 0; i--) {
319 q[i] = fw * (double)iq[i];
320 fw *= twon24;
321 }
322
323 /* compute pio2[0,...,jp]*q[jz,...,0] */
324 for (i = jz; i >= 0; i--) {
325 for (fw = zero, k = 0; k <= jp && k <= jz - i; k++)
326 fw += pio2[k] * q[i + k];
327
328 fq[jz - i] = fw;
329 }
330
331 /* compress fq[] into y[] */
332 switch (prec) {
333 case 0:
334 fw = zero;
335
336 for (i = jz; i >= 0; i--)
337 fw += fq[i];
338
339 y[0] = (ih == 0) ? fw : -fw;
340 break;
341
342 case 1:
343 case 2:
344 fw = zero;
345
346 for (i = jz; i >= 0; i--)
347 fw += fq[i];
348
349 y[0] = (ih == 0) ? fw : -fw;
350 fw = fq[0] - fw;
351
352 for (i = 1; i <= jz; i++)
353 fw += fq[i];
354
355 y[1] = (ih == 0) ? fw : -fw;
356 break;
357
358 default:
359
360 for (i = jz; i > 0; i--) {
361 fw = fq[i - 1] + fq[i];
362 fq[i] += fq[i - 1] - fw;
363 fq[i - 1] = fw;
364 }
365
366 for (i = jz; i > 1; i--) {
367 fw = fq[i - 1] + fq[i];
368 fq[i] += fq[i - 1] - fw;
369 fq[i - 1] = fw;
370 }
371
372 for (fw = zero, i = jz; i >= 2; i--)
373 fw += fq[i];
374
375 if (ih == 0) {
376 y[0] = fq[0];
377 y[1] = fq[1];
378 y[2] = fw;
379 } else {
380 y[0] = -fq[0];
381 y[1] = -fq[1];
382 y[2] = -fw;
383 }
384 }
385
386 #if defined(__i386) && !defined(__amd64)
387 (void) __swapRP(rp);
388 #endif
389 return (n & 7);
390 }
|