Home | History | Annotate | Download | only in libm
      1 /* kf_rem_pio2.c -- float version of k_rem_pio2.c
      2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian (at) 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__
     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]; q[i] = fw;
     81 	}
     82 
     83 	jz = jk;
     84 recompute:
     85     /* distill q[] into iq[] reversingly */
     86 	for(i=0,j=jz,z=q[jz];j>0;i++,j--) {
     87 	    fw    =  (float)((__int32_t)(twon8* z));
     88 	    iq[i] =  (__int32_t)(z-two8*fw);
     89 	    z     =  q[j-1]+fw;
     90 	}
     91 
     92     /* compute n */
     93 	z  = scalbnf(z,(int)q0);	/* actual value of z */
     94 	z -= (float)8.0*floorf(z*(float)0.125);	/* trim off integer >= 8 */
     95 	n  = (__int32_t) z;
     96 	z -= (float)n;
     97 	ih = 0;
     98 	if(q0>0) {	/* need iq[jz-1] to determine n */
     99 	    i  = (iq[jz-1]>>(8-q0)); n += i;
    100 	    iq[jz-1] -= i<<(8-q0);
    101 	    ih = iq[jz-1]>>(7-q0);
    102 	}
    103 	else if(q0==0) ih = iq[jz-1]>>8;
    104 	else if(z>=(float)0.5) ih=2;
    105 
    106 	if(ih>0) {	/* q > 0.5 */
    107 	    n += 1; carry = 0;
    108 	    for(i=0;i<jz ;i++) {	/* compute 1-q */
    109 		j = iq[i];
    110 		if(carry==0) {
    111 		    if(j!=0) {
    112 			carry = 1; iq[i] = 0x100- j;
    113 		    }
    114 		} else  iq[i] = 0xff - j;
    115 	    }
    116 	    if(q0>0) {		/* rare case: chance is 1 in 12 */
    117 	        switch(q0) {
    118 	        case 1:
    119 	    	   iq[jz-1] &= 0x7f; break;
    120 	    	case 2:
    121 	    	   iq[jz-1] &= 0x3f; break;
    122 	        }
    123 	    }
    124 	    if(ih==2) {
    125 		z = one - z;
    126 		if(carry!=0) z -= scalbnf(one,(int)q0);
    127 	    }
    128 	}
    129 
    130     /* check if recomputation is needed */
    131 	if(z==zero) {
    132 	    j = 0;
    133 	    for (i=jz-1;i>=jk;i--) j |= iq[i];
    134 	    if(j==0) { /* need recomputation */
    135 		for(k=1;iq[jk-k]==0;k++);   /* k = no. of terms needed */
    136 
    137 		for(i=jz+1;i<=jz+k;i++) {   /* add q[jz+1] to q[jz+k] */
    138 		    f[jx+i] = (float) ipio2[jv+i];
    139 		    for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j];
    140 		    q[i] = fw;
    141 		}
    142 		jz += k;
    143 		goto recompute;
    144 	    }
    145 	}
    146 
    147     /* chop off zero terms */
    148 	if(z==(float)0.0) {
    149 	    jz -= 1; q0 -= 8;
    150 	    while(iq[jz]==0) { jz--; q0-=8;}
    151 	} else { /* break z into 8-bit if necessary */
    152 	    z = scalbnf(z,-(int)q0);
    153 	    if(z>=two8) {
    154 		fw = (float)((__int32_t)(twon8*z));
    155 		iq[jz] = (__int32_t)(z-two8*fw);
    156 		jz += 1; q0 += 8;
    157 		iq[jz] = (__int32_t) fw;
    158 	    } else iq[jz] = (__int32_t) z ;
    159 	}
    160 
    161     /* convert integer "bit" chunk to floating-point value */
    162 	fw = scalbnf(one,(int)q0);
    163 	for(i=jz;i>=0;i--) {
    164 	    q[i] = fw*(float)iq[i]; fw*=twon8;
    165 	}
    166 
    167     /* compute PIo2[0,...,jp]*q[jz,...,0] */
    168 	for(i=jz;i>=0;i--) {
    169 	    for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k];
    170 	    fq[jz-i] = fw;
    171 	}
    172 
    173     /* compress fq[] into y[] */
    174 	switch(prec) {
    175 	    case 0:
    176 		fw = 0.0;
    177 		for (i=jz;i>=0;i--) fw += fq[i];
    178 		y[0] = (ih==0)? fw: -fw;
    179 		break;
    180 	    case 1:
    181 	    case 2:
    182 		fw = 0.0;
    183 		for (i=jz;i>=0;i--) fw += fq[i];
    184 		y[0] = (ih==0)? fw: -fw;
    185 		fw = fq[0]-fw;
    186 		for (i=1;i<=jz;i++) fw += fq[i];
    187 		y[1] = (ih==0)? fw: -fw;
    188 		break;
    189 	    case 3:	/* painful */
    190 		for (i=jz;i>0;i--) {
    191 		    fw      = fq[i-1]+fq[i];
    192 		    fq[i]  += fq[i-1]-fw;
    193 		    fq[i-1] = fw;
    194 		}
    195 		for (i=jz;i>1;i--) {
    196 		    fw      = fq[i-1]+fq[i];
    197 		    fq[i]  += fq[i-1]-fw;
    198 		    fq[i-1] = fw;
    199 		}
    200 		for (fw=0.0,i=jz;i>=2;i--) fw += fq[i];
    201 		if(ih==0) {
    202 		    y[0] =  fq[0]; y[1] =  fq[1]; y[2] =  fw;
    203 		} else {
    204 		    y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw;
    205 		}
    206 	}
    207 	return n&7;
    208 }
    209