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]
22 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
30 * int __rem_pio2m(x,y,e0,nx,prec,ipio2)
31 * double x[],y[]; int e0,nx,prec; const int ipio2[];
33 * __rem_pio2m return the last three digits of N with
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.
43 * (2/PI) is represented by an array of 24-bit integers in ipio2[].
44 * Here PI could as well be a machine value pi.
47 * x[] The input value (must be positive) is broken into nx
48 * pieces of 24-bit integers in double precision format.
49 * x[i] will be the i-th 24 bit of x. The scaled exponent
50 * of x[0] is given in input parameter e0 (i.e., x[0]*2^e0
51 * match x's up to 24 bits.
53 * Example of breaking a double z into x[0]+x[1]+x[2]:
61 * y[] ouput result in an array of double precision numbers.
62 * The dimension of y[] is:
67 * The actual value is the sum of them. Thus for 113-bit
68 * precsion, one may have to do something like:
70 * long double t,w,r_head, r_tail;
71 * t = (long double)y[2] + (long double)y[1];
72 * w = (long double)y[0];
74 * r_tail = w - (r_head - t);
76 * e0 The exponent of x[0]
80 * prec an interger indicating the precision:
83 * 2 64 bits (extended)
87 * integer array, contains the (24*i)-th to (24*i+23)-th
88 * bit of 2/pi or 2/PI after binary point. The corresponding
91 * ipio2[i] * 2^(-24(i+1)).
94 * double scalbn( ), floor( );
97 * Here is the description of some local variables:
99 * jk jk+1 is the initial number of terms of ipio2[] needed
100 * in the computation. The recommended value is 3,4,4,
101 * 6 for single, double, extended,and quad.
103 * jz local integer variable indicating the number of
104 * terms of ipio2[] used.
108 * jv index for pointing to the suitable ipio2[] for the
109 * computation. In general, we want
110 * ( 2^e0*x[0] * ipio2[jv-1]*2^(-24jv) )/8
111 * is an integer. Thus
112 * e0-3-24*jv >= 0 or (e0-3)/24 >= jv
113 * Hence jv = max(0,(e0-3)/24).
115 * jp jp+1 is the number of terms in pio2[] needed, jp = jk.
117 * q[] double array with integral value, representing the
118 * 24-bits chunk of the product of x and 2/pi.
120 * q0 the corresponding exponent of q[0]. Note that the
121 * exponent for q[i] would be q0-24*i.
123 * pio2[] double precision array, obtained by cutting pi/2
124 * into 24 bits chunks.
126 * f[] ipio2[] in floating point
128 * iq[] integer array by breaking up q[] in 24-bits chunk.
130 * fq[] final product of x*(2/pi) in fq[0],..,fq[jk]
132 * ih integer. If >0 it indicats q[] is >= 0.5, hence
133 * it also indicates the *sign* of the result.
139 #if defined(__i386) && !defined(__amd64)
140 extern int __swapRP(int);
143 static const int init_jk
[] = { 3, 4, 4, 6 }; /* initial value for jk */
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,
163 twon24
= 5.960464477539062500E-8;
166 __rem_pio2m(double *x
, double *y
, int e0
, int nx
, int prec
, const int *ipio2
)
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)
174 rp
= __swapRP(fp_extended
);
178 jp
= jk
= init_jk
[prec
];
180 /* determine jx,jv,q0, note that 3>q0 */
185 q0
= e0
- 24 * (jv
+ 1);
187 /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
190 for (i
= 0; i
<= m
; i
++, j
++)
191 f
[i
] = (j
< 0)? zero
: (double)ipio2
[j
];
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
];
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
);
210 z
= scalbn(z
, q0
); /* actual value of z */
211 z
-= eight
* floor(z
* eighth
); /* trim off integer >= 8 */
215 if (q0
> 0) { /* need iq[jz-1] to determine n */
216 i
= (iq
[jz
-1] >> (24 - q0
));
218 iq
[jz
-1] -= i
<< (24 - q0
);
219 ih
= iq
[jz
-1] >> (23 - q0
);
220 } else if (q0
== 0) {
222 } else if (z
>= half
) {
226 if (ih
> 0) { /* q > 0.5 */
229 for (i
= 0; i
< jz
; i
++) { /* compute 1-q */
234 iq
[i
] = 0x1000000 - j
;
237 iq
[i
] = 0xffffff - j
;
240 if (q0
> 0) { /* rare case: chance is 1 in 12 */
243 iq
[jz
-1] &= 0x7fffff;
246 iq
[jz
-1] &= 0x3fffff;
253 z
-= scalbn(one
, q0
);
257 /* check if recomputation is needed */
260 for (i
= jz
- 1; i
>= jk
; i
--)
262 if (j
== 0) { /* need recomputation */
263 /* set k to no. of terms needed */
264 for (k
= 1; iq
[jk
-k
] == 0; k
++)
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
];
279 /* cut out zero terms */
283 while (iq
[jz
] == 0) {
287 } else { /* break z into 24-bit if neccessary */
290 fw
= (double)((int)(twon24
* z
));
291 iq
[jz
] = (int)(z
- two24
* fw
);
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
];
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
];
314 /* compress fq[] into y[] */
318 for (i
= jz
; i
>= 0; i
--)
320 y
[0] = (ih
== 0)? fw
: -fw
;
326 for (i
= jz
; i
>= 0; i
--)
328 y
[0] = (ih
== 0)? fw
: -fw
;
330 for (i
= 1; i
<= jz
; i
++)
332 y
[1] = (ih
== 0)? fw
: -fw
;
336 for (i
= jz
; i
> 0; i
--) {
337 fw
= fq
[i
-1] + fq
[i
];
338 fq
[i
] += fq
[i
-1] - fw
;
341 for (i
= jz
; i
> 1; i
--) {
342 fw
= fq
[i
-1] + fq
[i
];
343 fq
[i
] += fq
[i
-1] - fw
;
346 for (fw
= zero
, i
= jz
; i
>= 2; i
--)
359 #if defined(__i386) && !defined(__amd64)