| 1 | /* k_rem_pio2f.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 | #if defined(LIBM_SCCS) && !defined(lint) |
| 17 | static char rcsid[] = "$NetBSD: k_rem_pio2f.c,v 1.4 1995/05/10 20:46:28 jtc Exp $" ; |
| 18 | #endif |
| 19 | |
| 20 | #include <math.h> |
| 21 | #include <math_private.h> |
| 22 | #include <libc-internal.h> |
| 23 | |
| 24 | /* In the float version, the input parameter x contains 8 bit |
| 25 | integers, not 24 bit integers. 113 bit precision is not supported. */ |
| 26 | |
| 27 | static const int init_jk[] = {4,7,9}; /* initial value for jk */ |
| 28 | |
| 29 | static const float PIo2[] = { |
| 30 | 1.5703125000e+00, /* 0x3fc90000 */ |
| 31 | 4.5776367188e-04, /* 0x39f00000 */ |
| 32 | 2.5987625122e-05, /* 0x37da0000 */ |
| 33 | 7.5437128544e-08, /* 0x33a20000 */ |
| 34 | 6.0026650317e-11, /* 0x2e840000 */ |
| 35 | 7.3896444519e-13, /* 0x2b500000 */ |
| 36 | 5.3845816694e-15, /* 0x27c20000 */ |
| 37 | 5.6378512969e-18, /* 0x22d00000 */ |
| 38 | 8.3009228831e-20, /* 0x1fc40000 */ |
| 39 | 3.2756352257e-22, /* 0x1bc60000 */ |
| 40 | 6.3331015649e-25, /* 0x17440000 */ |
| 41 | }; |
| 42 | |
| 43 | static const float |
| 44 | zero = 0.0, |
| 45 | one = 1.0, |
| 46 | two8 = 2.5600000000e+02, /* 0x43800000 */ |
| 47 | twon8 = 3.9062500000e-03; /* 0x3b800000 */ |
| 48 | |
| 49 | int __kernel_rem_pio2f(float *x, float *y, int e0, int nx, int prec, const int32_t *ipio2) |
| 50 | { |
| 51 | int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih; |
| 52 | float z,fw,f[20],fq[20],q[20]; |
| 53 | |
| 54 | /* initialize jk*/ |
| 55 | jk = init_jk[prec]; |
| 56 | jp = jk; |
| 57 | |
| 58 | /* determine jx,jv,q0, note that 3>q0 */ |
| 59 | jx = nx-1; |
| 60 | jv = (e0-3)/8; if(jv<0) jv=0; |
| 61 | q0 = e0-8*(jv+1); |
| 62 | |
| 63 | /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */ |
| 64 | j = jv-jx; m = jx+jk; |
| 65 | for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (float) ipio2[j]; |
| 66 | |
| 67 | /* compute q[0],q[1],...q[jk] */ |
| 68 | for (i=0;i<=jk;i++) { |
| 69 | for(j=0,fw=0.0;j<=jx;j++) |
| 70 | fw += x[j]*f[jx+i-j]; |
| 71 | q[i] = fw; |
| 72 | } |
| 73 | |
| 74 | jz = jk; |
| 75 | recompute: |
| 76 | /* distill q[] into iq[] reversingly */ |
| 77 | for(i=0,j=jz,z=q[jz];j>0;i++,j--) { |
| 78 | fw = (float)((int32_t)(twon8* z)); |
| 79 | iq[i] = (int32_t)(z-two8*fw); |
| 80 | z = q[j-1]+fw; |
| 81 | } |
| 82 | |
| 83 | /* compute n */ |
| 84 | z = __scalbnf(z,q0); /* actual value of z */ |
| 85 | z -= (float)8.0*__floorf(z*(float)0.125); /* trim off integer >= 8 */ |
| 86 | n = (int32_t) z; |
| 87 | z -= (float)n; |
| 88 | ih = 0; |
| 89 | if(q0>0) { /* need iq[jz-1] to determine n */ |
| 90 | i = (iq[jz-1]>>(8-q0)); n += i; |
| 91 | iq[jz-1] -= i<<(8-q0); |
| 92 | ih = iq[jz-1]>>(7-q0); |
| 93 | } |
| 94 | else if(q0==0) ih = iq[jz-1]>>7; |
| 95 | else if(z>=(float)0.5) ih=2; |
| 96 | |
| 97 | if(ih>0) { /* q > 0.5 */ |
| 98 | n += 1; carry = 0; |
| 99 | for(i=0;i<jz ;i++) { /* compute 1-q */ |
| 100 | j = iq[i]; |
| 101 | if(carry==0) { |
| 102 | if(j!=0) { |
| 103 | carry = 1; iq[i] = 0x100- j; |
| 104 | } |
| 105 | } else iq[i] = 0xff - j; |
| 106 | } |
| 107 | if(q0>0) { /* rare case: chance is 1 in 12 */ |
| 108 | switch(q0) { |
| 109 | case 1: |
| 110 | iq[jz-1] &= 0x7f; break; |
| 111 | case 2: |
| 112 | iq[jz-1] &= 0x3f; break; |
| 113 | } |
| 114 | } |
| 115 | if(ih==2) { |
| 116 | z = one - z; |
| 117 | if(carry!=0) z -= __scalbnf(one,q0); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* check if recomputation is needed */ |
| 122 | if(z==zero) { |
| 123 | j = 0; |
| 124 | for (i=jz-1;i>=jk;i--) j |= iq[i]; |
| 125 | if(j==0) { /* need recomputation */ |
| 126 | /* On s390x gcc 6.1 -O3 produces the warning "array subscript is |
| 127 | below array bounds [-Werror=array-bounds]". Only |
| 128 | __ieee754_rem_pio2f calls __kernel_rem_pio2f for normal |
| 129 | numbers and |x| ~> 2^7*(pi/2). Thus x can't be zero and |
| 130 | ipio2 is not zero, too. Thus not all iq[] values can't be |
| 131 | zero. */ |
| 132 | DIAG_PUSH_NEEDS_COMMENT; |
| 133 | DIAG_IGNORE_NEEDS_COMMENT (6.1, "-Warray-bounds" ); |
| 134 | for(k=1;iq[jk-k]==0;k++); /* k = no. of terms needed */ |
| 135 | DIAG_POP_NEEDS_COMMENT; |
| 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,-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,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 | float fv = 0.0; |
| 183 | for (i=jz;i>=0;i--) fv = math_narrow_eval (fv + fq[i]); |
| 184 | y[0] = (ih==0)? fv: -fv; |
| 185 | fv = math_narrow_eval (fq[0]-fv); |
| 186 | for (i=1;i<=jz;i++) fv = math_narrow_eval (fv + fq[i]); |
| 187 | y[1] = (ih==0)? fv: -fv; |
| 188 | break; |
| 189 | case 3: /* painful */ |
| 190 | for (i=jz;i>0;i--) { |
| 191 | float fv = math_narrow_eval (fq[i-1]+fq[i]); |
| 192 | fq[i] += fq[i-1]-fv; |
| 193 | fq[i-1] = fv; |
| 194 | } |
| 195 | for (i=jz;i>1;i--) { |
| 196 | float fv = math_narrow_eval (fq[i-1]+fq[i]); |
| 197 | fq[i] += fq[i-1]-fv; |
| 198 | fq[i-1] = fv; |
| 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 | |