* crypt/md5-crypt.c: Fix comment.
[glibc.git] / sysdeps / ieee754 / flt-32 / k_rem_pio2f.c
blob2783480fcbff4a4596fe68c9e1e04d061a4afc78
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 */
5 /*
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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 * ====================================================
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
20 #include "math.h"
21 #include "math_private.h"
23 /* In the float version, the input parameter x contains 8 bit
24 integers, not 24 bit integers. 113 bit precision is not supported. */
26 #ifdef __STDC__
27 static const int init_jk[] = {4,7,9}; /* initial value for jk */
28 #else
29 static int init_jk[] = {4,7,9};
30 #endif
32 #ifdef __STDC__
33 static const float PIo2[] = {
34 #else
35 static float PIo2[] = {
36 #endif
37 1.5703125000e+00, /* 0x3fc90000 */
38 4.5776367188e-04, /* 0x39f00000 */
39 2.5987625122e-05, /* 0x37da0000 */
40 7.5437128544e-08, /* 0x33a20000 */
41 6.0026650317e-11, /* 0x2e840000 */
42 7.3896444519e-13, /* 0x2b500000 */
43 5.3845816694e-15, /* 0x27c20000 */
44 5.6378512969e-18, /* 0x22d00000 */
45 8.3009228831e-20, /* 0x1fc40000 */
46 3.2756352257e-22, /* 0x1bc60000 */
47 6.3331015649e-25, /* 0x17440000 */
50 #ifdef __STDC__
51 static const float
52 #else
53 static float
54 #endif
55 zero = 0.0,
56 one = 1.0,
57 two8 = 2.5600000000e+02, /* 0x43800000 */
58 twon8 = 3.9062500000e-03; /* 0x3b800000 */
60 #ifdef __STDC__
61 int __kernel_rem_pio2f(float *x, float *y, int e0, int nx, int prec, const int32_t *ipio2)
62 #else
63 int __kernel_rem_pio2f(x,y,e0,nx,prec,ipio2)
64 float x[], y[]; int e0,nx,prec; int32_t ipio2[];
65 #endif
67 int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih;
68 float z,fw,f[20],fq[20],q[20];
70 /* initialize jk*/
71 jk = init_jk[prec];
72 jp = jk;
74 /* determine jx,jv,q0, note that 3>q0 */
75 jx = nx-1;
76 jv = (e0-3)/8; if(jv<0) jv=0;
77 q0 = e0-8*(jv+1);
79 /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
80 j = jv-jx; m = jx+jk;
81 for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (float) ipio2[j];
83 /* compute q[0],q[1],...q[jk] */
84 for (i=0;i<=jk;i++) {
85 for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; q[i] = fw;
88 jz = jk;
89 recompute:
90 /* distill q[] into iq[] reversingly */
91 for(i=0,j=jz,z=q[jz];j>0;i++,j--) {
92 fw = (float)((int32_t)(twon8* z));
93 iq[i] = (int32_t)(z-two8*fw);
94 z = q[j-1]+fw;
97 /* compute n */
98 z = __scalbnf(z,q0); /* actual value of z */
99 z -= (float)8.0*__floorf(z*(float)0.125); /* trim off integer >= 8 */
100 n = (int32_t) z;
101 z -= (float)n;
102 ih = 0;
103 if(q0>0) { /* need iq[jz-1] to determine n */
104 i = (iq[jz-1]>>(8-q0)); n += i;
105 iq[jz-1] -= i<<(8-q0);
106 ih = iq[jz-1]>>(7-q0);
108 else if(q0==0) ih = iq[jz-1]>>8;
109 else if(z>=(float)0.5) ih=2;
111 if(ih>0) { /* q > 0.5 */
112 n += 1; carry = 0;
113 for(i=0;i<jz ;i++) { /* compute 1-q */
114 j = iq[i];
115 if(carry==0) {
116 if(j!=0) {
117 carry = 1; iq[i] = 0x100- j;
119 } else iq[i] = 0xff - j;
121 if(q0>0) { /* rare case: chance is 1 in 12 */
122 switch(q0) {
123 case 1:
124 iq[jz-1] &= 0x7f; break;
125 case 2:
126 iq[jz-1] &= 0x3f; break;
129 if(ih==2) {
130 z = one - z;
131 if(carry!=0) z -= __scalbnf(one,q0);
135 /* check if recomputation is needed */
136 if(z==zero) {
137 j = 0;
138 for (i=jz-1;i>=jk;i--) j |= iq[i];
139 if(j==0) { /* need recomputation */
140 for(k=1;iq[jk-k]==0;k++); /* k = no. of terms needed */
142 for(i=jz+1;i<=jz+k;i++) { /* add q[jz+1] to q[jz+k] */
143 f[jx+i] = (float) ipio2[jv+i];
144 for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j];
145 q[i] = fw;
147 jz += k;
148 goto recompute;
152 /* chop off zero terms */
153 if(z==(float)0.0) {
154 jz -= 1; q0 -= 8;
155 while(iq[jz]==0) { jz--; q0-=8;}
156 } else { /* break z into 8-bit if necessary */
157 z = __scalbnf(z,-q0);
158 if(z>=two8) {
159 fw = (float)((int32_t)(twon8*z));
160 iq[jz] = (int32_t)(z-two8*fw);
161 jz += 1; q0 += 8;
162 iq[jz] = (int32_t) fw;
163 } else iq[jz] = (int32_t) z ;
166 /* convert integer "bit" chunk to floating-point value */
167 fw = __scalbnf(one,q0);
168 for(i=jz;i>=0;i--) {
169 q[i] = fw*(float)iq[i]; fw*=twon8;
172 /* compute PIo2[0,...,jp]*q[jz,...,0] */
173 for(i=jz;i>=0;i--) {
174 for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k];
175 fq[jz-i] = fw;
178 /* compress fq[] into y[] */
179 switch(prec) {
180 case 0:
181 fw = 0.0;
182 for (i=jz;i>=0;i--) fw += fq[i];
183 y[0] = (ih==0)? fw: -fw;
184 break;
185 case 1:
186 case 2:
187 fw = 0.0;
188 for (i=jz;i>=0;i--) fw += fq[i];
189 y[0] = (ih==0)? fw: -fw;
190 fw = fq[0]-fw;
191 for (i=1;i<=jz;i++) fw += fq[i];
192 y[1] = (ih==0)? fw: -fw;
193 break;
194 case 3: /* painful */
195 for (i=jz;i>0;i--) {
196 fw = fq[i-1]+fq[i];
197 fq[i] += fq[i-1]-fw;
198 fq[i-1] = fw;
200 for (i=jz;i>1;i--) {
201 fw = fq[i-1]+fq[i];
202 fq[i] += fq[i-1]-fw;
203 fq[i-1] = fw;
205 for (fw=0.0,i=jz;i>=2;i--) fw += fq[i];
206 if(ih==0) {
207 y[0] = fq[0]; y[1] = fq[1]; y[2] = fw;
208 } else {
209 y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw;
212 return n&7;