1 /* kf_rem_pio2.c -- float version of k_rem_pio2.c 2 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. 3 */ 4 5 /* 6 * ==================================================== 7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 8 * 9 * Developed at SunPro, a Sun Microsystems, Inc. business. 10 * Permission to use, copy, modify, and distribute this 11 * software is freely granted, provided that this notice 12 * is preserved. 13 * ==================================================== 14 */ 15 16 #include "fdlibm.h" 17 18 /* In the float version, the input parameter x contains 8 bit 19 integers, not 24 bit integers. 113 bit precision is not supported. */ 20 21 #ifdef __STDC__ 22 static const int init_jk[] = {4,7,9}; /* initial value for jk */ 23 #else 24 static int init_jk[] = {4,7,9}; 25 #endif 26 27 #ifdef __STDC__ 28 static const float PIo2[] = { 29 #else 30 static float PIo2[] = { 31 #endif 32 1.5703125000e+00, /* 0x3fc90000 */ 33 4.5776367188e-04, /* 0x39f00000 */ 34 2.5987625122e-05, /* 0x37da0000 */ 35 7.5437128544e-08, /* 0x33a20000 */ 36 6.0026650317e-11, /* 0x2e840000 */ 37 7.3896444519e-13, /* 0x2b500000 */ 38 5.3845816694e-15, /* 0x27c20000 */ 39 5.6378512969e-18, /* 0x22d00000 */ 40 8.3009228831e-20, /* 0x1fc40000 */ 41 3.2756352257e-22, /* 0x1bc60000 */ 42 6.3331015649e-25, /* 0x17440000 */ 43 }; 44 45 #ifdef __STDC__ 46 static const float 47 #else 48 static float 49 #endif 50 zero = 0.0, 51 one = 1.0, 52 two8 = 2.5600000000e+02, /* 0x43800000 */ 53 twon8 = 3.9062500000e-03; /* 0x3b800000 */ 54 55 #ifdef __STDC__ __kernel_rem_pio2f(float * x,float * y,int e0,int nx,int prec,const __int32_t * ipio2)56 int __kernel_rem_pio2f(float *x, float *y, int e0, int nx, int prec, const __int32_t *ipio2) 57 #else 58 int __kernel_rem_pio2f(x,y,e0,nx,prec,ipio2) 59 float x[], y[]; int e0,nx,prec; __int32_t ipio2[]; 60 #endif 61 { 62 __int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih; 63 float z,fw,f[20],fq[20],q[20]; 64 65 /* initialize jk*/ 66 jk = init_jk[prec]; 67 jp = jk; 68 69 /* determine jx,jv,q0, note that 3>q0 */ 70 jx = nx-1; 71 jv = (e0-3)/8; if(jv<0) jv=0; 72 q0 = e0-8*(jv+1); 73 74 /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */ 75 j = jv-jx; m = jx+jk; 76 for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (float) ipio2[j]; 77 78 /* compute q[0],q[1],...q[jk] */ 79 for (i=0;i<=jk;i++) { 80 for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; 81 q[i] = fw; 82 } 83 84 jz = jk; 85 recompute: 86 /* distill q[] into iq[] reversingly */ 87 for(i=0,j=jz,z=q[jz];j>0;i++,j--) { 88 fw = (float)((__int32_t)(twon8* z)); 89 iq[i] = (__int32_t)(z-two8*fw); 90 z = q[j-1]+fw; 91 } 92 93 /* compute n */ 94 z = scalbnf(z,(int)q0); /* actual value of z */ 95 z -= (float)8.0*floorf(z*(float)0.125); /* trim off integer >= 8 */ 96 n = (__int32_t) z; 97 z -= (float)n; 98 ih = 0; 99 if(q0>0) { /* need iq[jz-1] to determine n */ 100 i = (iq[jz-1]>>(8-q0)); n += i; 101 iq[jz-1] -= i<<(8-q0); 102 ih = iq[jz-1]>>(7-q0); 103 } 104 else if(q0==0) ih = iq[jz-1]>>8; 105 else if(z>=(float)0.5) ih=2; 106 107 if(ih>0) { /* q > 0.5 */ 108 n += 1; carry = 0; 109 for(i=0;i<jz ;i++) { /* compute 1-q */ 110 j = iq[i]; 111 if(carry==0) { 112 if(j!=0) { 113 carry = 1; iq[i] = 0x100- j; 114 } 115 } else iq[i] = 0xff - j; 116 } 117 if(q0>0) { /* rare case: chance is 1 in 12 */ 118 switch(q0) { 119 case 1: 120 iq[jz-1] &= 0x7f; break; 121 case 2: 122 iq[jz-1] &= 0x3f; break; 123 } 124 } 125 if(ih==2) { 126 z = one - z; 127 if(carry!=0) z -= scalbnf(one,(int)q0); 128 } 129 } 130 131 /* check if recomputation is needed */ 132 if(z==zero) { 133 j = 0; 134 for (i=jz-1;i>=jk;i--) j |= iq[i]; 135 if(j==0) { /* need recomputation */ 136 for(k=1;iq[jk-k]==0;k++); /* k = no. of terms needed */ 137 138 for(i=jz+1;i<=jz+k;i++) { /* add q[jz+1] to q[jz+k] */ 139 f[jx+i] = (float) ipio2[jv+i]; 140 for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; 141 q[i] = fw; 142 } 143 jz += k; 144 goto recompute; 145 } 146 } 147 148 /* chop off zero terms */ 149 if(z==(float)0.0) { 150 jz -= 1; q0 -= 8; 151 while(iq[jz]==0) { jz--; q0-=8;} 152 } else { /* break z into 8-bit if necessary */ 153 z = scalbnf(z,-(int)q0); 154 if(z>=two8) { 155 fw = (float)((__int32_t)(twon8*z)); 156 iq[jz] = (__int32_t)(z-two8*fw); 157 jz += 1; q0 += 8; 158 iq[jz] = (__int32_t) fw; 159 } else iq[jz] = (__int32_t) z ; 160 } 161 162 /* convert integer "bit" chunk to floating-point value */ 163 fw = scalbnf(one,(int)q0); 164 for(i=jz;i>=0;i--) { 165 q[i] = fw*(float)iq[i]; fw*=twon8; 166 } 167 168 /* compute PIo2[0,...,jp]*q[jz,...,0] */ 169 for(i=jz;i>=0;i--) { 170 for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k]; 171 fq[jz-i] = fw; 172 } 173 174 /* compress fq[] into y[] */ 175 switch(prec) { 176 case 0: 177 fw = 0.0; 178 for (i=jz;i>=0;i--) fw += fq[i]; 179 y[0] = (ih==0)? fw: -fw; 180 break; 181 case 1: 182 case 2: 183 fw = 0.0; 184 for (i=jz;i>=0;i--) fw += fq[i]; 185 y[0] = (ih==0)? fw: -fw; 186 fw = fq[0]-fw; 187 for (i=1;i<=jz;i++) fw += fq[i]; 188 y[1] = (ih==0)? fw: -fw; 189 break; 190 case 3: /* painful */ 191 for (i=jz;i>0;i--) { 192 fw = fq[i-1]+fq[i]; 193 fq[i] += fq[i-1]-fw; 194 fq[i-1] = fw; 195 } 196 for (i=jz;i>1;i--) { 197 fw = fq[i-1]+fq[i]; 198 fq[i] += fq[i-1]-fw; 199 fq[i-1] = fw; 200 } 201 for (fw=0.0,i=jz;i>=2;i--) fw += fq[i]; 202 if(ih==0) { 203 y[0] = fq[0]; y[1] = fq[1]; y[2] = fw; 204 } else { 205 y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw; 206 } 207 } 208 return n&7; 209 } 210