POK
|
00001 /* 00002 * POK header 00003 * 00004 * The following file is a part of the POK project. Any modification should 00005 * made according to the POK licence. You CANNOT use this file or a part of 00006 * this file is this part of a file for your own project 00007 * 00008 * For more information on the POK licence, please see our LICENCE FILE 00009 * 00010 * Please follow the coding guidelines described in doc/CODING_GUIDELINES 00011 * 00012 * Copyright (c) 2007-2009 POK team 00013 * 00014 * Created by julien on Sat Jan 31 20:12:07 2009 00015 */ 00016 00017 /* @(#)k_rem_pio2.c 5.1 93/09/24 */ 00018 /* 00019 * ==================================================== 00020 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 00021 * 00022 * Developed at SunPro, a Sun Microsystems, Inc. business. 00023 * Permission to use, copy, modify, and distribute this 00024 * software is freely granted, provided that this notice 00025 * is preserved. 00026 * ==================================================== 00027 */ 00028 00029 #ifdef POK_NEEDS_LIBMATH 00030 00031 /* 00032 * __kernel_rem_pio2(x,y,e0,nx,prec,ipio2) 00033 * double x[],y[]; int e0,nx,prec; int ipio2[]; 00034 * 00035 * __kernel_rem_pio2 return the last three digits of N with 00036 * y = x - N*pi/2 00037 * so that |y| < pi/2. 00038 * 00039 * The method is to compute the integer (mod 8) and fraction parts of 00040 * (2/pi)*x without doing the full multiplication. In general we 00041 * skip the part of the product that are known to be a huge integer ( 00042 * more accurately, = 0 mod 8 ). Thus the number of operations are 00043 * independent of the exponent of the input. 00044 * 00045 * (2/pi) is represented by an array of 24-bit integers in ipio2[]. 00046 * 00047 * Input parameters: 00048 * x[] The input value (must be positive) is broken into nx 00049 * pieces of 24-bit integers in double precision format. 00050 * x[i] will be the i-th 24 bit of x. The scaled exponent 00051 * of x[0] is given in input parameter e0 (i.e., x[0]*2^e0 00052 * match x's up to 24 bits. 00053 * 00054 * Example of breaking a double positive z into x[0]+x[1]+x[2]: 00055 * e0 = ilogb(z)-23 00056 * z = scalbn(z,-e0) 00057 * for i = 0,1,2 00058 * x[i] = floor(z) 00059 * z = (z-x[i])*2**24 00060 * 00061 * 00062 * y[] output result in an array of double precision numbers. 00063 * The dimension of y[] is: 00064 * 24-bit precision 1 00065 * 53-bit precision 2 00066 * 64-bit precision 2 00067 * 113-bit precision 3 00068 * The actual value is the sum of them. Thus for 113-bit 00069 * precison, one may have to do something like: 00070 * 00071 * long double t,w,r_head, r_tail; 00072 * t = (long double)y[2] + (long double)y[1]; 00073 * w = (long double)y[0]; 00074 * r_head = t+w; 00075 * r_tail = w - (r_head - t); 00076 * 00077 * e0 The exponent of x[0] 00078 * 00079 * nx dimension of x[] 00080 * 00081 * prec an integer indicating the precision: 00082 * 0 24 bits (single) 00083 * 1 53 bits (double) 00084 * 2 64 bits (extended) 00085 * 3 113 bits (quad) 00086 * 00087 * ipio2[] 00088 * integer array, contains the (24*i)-th to (24*i+23)-th 00089 * bit of 2/pi after binary point. The corresponding 00090 * floating value is 00091 * 00092 * ipio2[i] * 2^(-24(i+1)). 00093 * 00094 * External function: 00095 * double scalbn(), floor(); 00096 * 00097 * 00098 * Here is the description of some local variables: 00099 * 00100 * jk jk+1 is the initial number of terms of ipio2[] needed 00101 * in the computation. The recommended value is 2,3,4, 00102 * 6 for single, double, extended,and quad. 00103 * 00104 * jz local integer variable indicating the number of 00105 * terms of ipio2[] used. 00106 * 00107 * jx nx - 1 00108 * 00109 * jv index for pointing to the suitable ipio2[] for the 00110 * computation. In general, we want 00111 * ( 2^e0*x[0] * ipio2[jv-1]*2^(-24jv) )/8 00112 * is an integer. Thus 00113 * e0-3-24*jv >= 0 or (e0-3)/24 >= jv 00114 * Hence jv = max(0,(e0-3)/24). 00115 * 00116 * jp jp+1 is the number of terms in PIo2[] needed, jp = jk. 00117 * 00118 * q[] double array with integral value, representing the 00119 * 24-bits chunk of the product of x and 2/pi. 00120 * 00121 * q0 the corresponding exponent of q[0]. Note that the 00122 * exponent for q[i] would be q0-24*i. 00123 * 00124 * PIo2[] double precision array, obtained by cutting pi/2 00125 * into 24 bits chunks. 00126 * 00127 * f[] ipio2[] in floating point 00128 * 00129 * iq[] integer array by breaking up q[] in 24-bits chunk. 00130 * 00131 * fq[] final product of x*(2/pi) in fq[0],..,fq[jk] 00132 * 00133 * ih integer. If >0 it indicates q[] is >= 0.5, hence 00134 * it also indicates the *sign* of the result. 00135 * 00136 */ 00137 00138 00139 /* 00140 * Constants: 00141 * The hexadecimal values are the intended ones for the following 00142 * constants. The decimal values may be used, provided that the 00143 * compiler will convert from decimal to binary accurately enough 00144 * to produce the hexadecimal values shown. 00145 */ 00146 00147 #include <libm.h> 00148 #include "math_private.h" 00149 00150 static const int init_jk[] = {2,3,4,6}; /* initial value for jk */ 00151 00152 static const double PIo2[] = { 00153 1.57079625129699707031e+00, /* 0x3FF921FB, 0x40000000 */ 00154 7.54978941586159635335e-08, /* 0x3E74442D, 0x00000000 */ 00155 5.39030252995776476554e-15, /* 0x3CF84698, 0x80000000 */ 00156 3.28200341580791294123e-22, /* 0x3B78CC51, 0x60000000 */ 00157 1.27065575308067607349e-29, /* 0x39F01B83, 0x80000000 */ 00158 1.22933308981111328932e-36, /* 0x387A2520, 0x40000000 */ 00159 2.73370053816464559624e-44, /* 0x36E38222, 0x80000000 */ 00160 2.16741683877804819444e-51, /* 0x3569F31D, 0x00000000 */ 00161 }; 00162 00163 static const double 00164 zero = 0.0, 00165 one = 1.0, 00166 two24 = 1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */ 00167 twon24 = 5.96046447753906250000e-08; /* 0x3E700000, 0x00000000 */ 00168 00169 int 00170 __kernel_rem_pio2(double *x, double *y, int e0, int nx, int prec, const int *ipio2) 00171 { 00172 int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih; 00173 double z,fw,f[20],fq[20],q[20]; 00174 00175 /* initialize jk*/ 00176 jk = init_jk[prec]; 00177 jp = jk; 00178 00179 /* determine jx,jv,q0, note that 3>q0 */ 00180 jx = nx-1; 00181 jv = (e0-3)/24; if(jv<0) jv=0; 00182 q0 = e0-24*(jv+1); 00183 00184 /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */ 00185 j = jv-jx; m = jx+jk; 00186 for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (double) ipio2[j]; 00187 00188 /* compute q[0],q[1],...q[jk] */ 00189 for (i=0;i<=jk;i++) { 00190 for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; q[i] = fw; 00191 } 00192 00193 jz = jk; 00194 recompute: 00195 /* distill q[] into iq[] reversingly */ 00196 for(i=0,j=jz,z=q[jz];j>0;i++,j--) { 00197 fw = (double)((int32_t)(twon24* z)); 00198 iq[i] = (int32_t)(z-two24*fw); 00199 z = q[j-1]+fw; 00200 } 00201 00202 /* compute n */ 00203 z = scalbn(z,q0); /* actual value of z */ 00204 z -= 8.0*floor(z*0.125); /* trim off integer >= 8 */ 00205 n = (int32_t) z; 00206 z -= (double)n; 00207 ih = 0; 00208 if(q0>0) { /* need iq[jz-1] to determine n */ 00209 i = (iq[jz-1]>>(24-q0)); n += i; 00210 iq[jz-1] -= i<<(24-q0); 00211 ih = iq[jz-1]>>(23-q0); 00212 } 00213 else if(q0==0) ih = iq[jz-1]>>23; 00214 else if(z>=0.5) ih=2; 00215 00216 if(ih>0) { /* q > 0.5 */ 00217 n += 1; carry = 0; 00218 for(i=0;i<jz ;i++) { /* compute 1-q */ 00219 j = iq[i]; 00220 if(carry==0) { 00221 if(j!=0) { 00222 carry = 1; iq[i] = 0x1000000- j; 00223 } 00224 } else iq[i] = 0xffffff - j; 00225 } 00226 if(q0>0) { /* rare case: chance is 1 in 12 */ 00227 switch(q0) { 00228 case 1: 00229 iq[jz-1] &= 0x7fffff; break; 00230 case 2: 00231 iq[jz-1] &= 0x3fffff; break; 00232 } 00233 } 00234 if(ih==2) { 00235 z = one - z; 00236 if(carry!=0) z -= scalbn(one,q0); 00237 } 00238 } 00239 00240 /* check if recomputation is needed */ 00241 if(z==zero) { 00242 j = 0; 00243 for (i=jz-1;i>=jk;i--) j |= iq[i]; 00244 if(j==0) { /* need recomputation */ 00245 for(k=1;iq[jk-k]==0;k++); /* k = no. of terms needed */ 00246 00247 for(i=jz+1;i<=jz+k;i++) { /* add q[jz+1] to q[jz+k] */ 00248 f[jx+i] = (double) ipio2[jv+i]; 00249 for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; 00250 q[i] = fw; 00251 } 00252 jz += k; 00253 goto recompute; 00254 } 00255 } 00256 00257 /* chop off zero terms */ 00258 if(z==0.0) { 00259 jz -= 1; q0 -= 24; 00260 while(iq[jz]==0) { jz--; q0-=24;} 00261 } else { /* break z into 24-bit if necessary */ 00262 z = scalbn(z,-q0); 00263 if(z>=two24) { 00264 fw = (double)((int32_t)(twon24*z)); 00265 iq[jz] = (int32_t)(z-two24*fw); 00266 jz += 1; q0 += 24; 00267 iq[jz] = (int32_t) fw; 00268 } else iq[jz] = (int32_t) z ; 00269 } 00270 00271 /* convert integer "bit" chunk to floating-point value */ 00272 fw = scalbn(one,q0); 00273 for(i=jz;i>=0;i--) { 00274 q[i] = fw*(double)iq[i]; fw*=twon24; 00275 } 00276 00277 /* compute PIo2[0,...,jp]*q[jz,...,0] */ 00278 for(i=jz;i>=0;i--) { 00279 for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k]; 00280 fq[jz-i] = fw; 00281 } 00282 00283 /* compress fq[] into y[] */ 00284 switch(prec) { 00285 case 0: 00286 fw = 0.0; 00287 for (i=jz;i>=0;i--) fw += fq[i]; 00288 y[0] = (ih==0)? fw: -fw; 00289 break; 00290 case 1: 00291 case 2: 00292 fw = 0.0; 00293 for (i=jz;i>=0;i--) fw += fq[i]; 00294 y[0] = (ih==0)? fw: -fw; 00295 fw = fq[0]-fw; 00296 for (i=1;i<=jz;i++) fw += fq[i]; 00297 y[1] = (ih==0)? fw: -fw; 00298 break; 00299 case 3: /* painful */ 00300 for (i=jz;i>0;i--) { 00301 fw = fq[i-1]+fq[i]; 00302 fq[i] += fq[i-1]-fw; 00303 fq[i-1] = fw; 00304 } 00305 for (i=jz;i>1;i--) { 00306 fw = fq[i-1]+fq[i]; 00307 fq[i] += fq[i-1]-fw; 00308 fq[i-1] = fw; 00309 } 00310 for (fw=0.0,i=jz;i>=2;i--) fw += fq[i]; 00311 if(ih==0) { 00312 y[0] = fq[0]; y[1] = fq[1]; y[2] = fw; 00313 } else { 00314 y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw; 00315 } 00316 } 00317 return n&7; 00318 } 00319 00320 #endif