x11gfx.hidd: support 32 bit modes
[AROS.git] / compiler / stdc / math / k_rem_pio2f.c
blobbb775eb6bea39203e05a7e13b9d79a68c89a2a4a
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 #ifndef lint
17 static char rcsid[] = "$FreeBSD: src/lib/msun/src/k_rem_pio2f.c,v 1.8 2005/10/11 07:56:05 bde 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 static const int init_jk[] = {4,7,9}; /* initial value for jk */
28 static const float PIo2[] = {
29 1.5703125000e+00, /* 0x3fc90000 */
30 4.5776367188e-04, /* 0x39f00000 */
31 2.5987625122e-05, /* 0x37da0000 */
32 7.5437128544e-08, /* 0x33a20000 */
33 6.0026650317e-11, /* 0x2e840000 */
34 7.3896444519e-13, /* 0x2b500000 */
35 5.3845816694e-15, /* 0x27c20000 */
36 5.6378512969e-18, /* 0x22d00000 */
37 8.3009228831e-20, /* 0x1fc40000 */
38 3.2756352257e-22, /* 0x1bc60000 */
39 6.3331015649e-25, /* 0x17440000 */
42 static const float
43 zero = 0.0,
44 one = 1.0,
45 two8 = 2.5600000000e+02, /* 0x43800000 */
46 twon8 = 3.9062500000e-03; /* 0x3b800000 */
48 int __kernel_rem_pio2f(float *x, float *y, int e0, int nx, int prec, const int32_t *ipio2)
50 int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih;
51 float z,fw,f[20],fq[20],q[20];
53 /* initialize jk*/
54 jk = init_jk[prec];
55 jp = jk;
57 /* determine jx,jv,q0, note that 3>q0 */
58 jx = nx-1;
59 jv = (e0-3)/8; if(jv<0) jv=0;
60 q0 = e0-8*(jv+1);
62 /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
63 j = jv-jx; m = jx+jk;
64 for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (float) ipio2[j];
66 /* compute q[0],q[1],...q[jk] */
67 for (i=0;i<=jk;i++) {
68 for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j];
69 q[i] = fw;
72 jz = jk;
73 recompute:
74 /* distill q[] into iq[] reversingly */
75 for(i=0,j=jz,z=q[jz];j>0;i++,j--) {
76 fw = (float)((int32_t)(twon8* z));
77 iq[i] = (int32_t)(z-two8*fw);
78 z = q[j-1]+fw;
81 /* compute n */
82 z = scalbnf(z,q0); /* actual value of z */
83 z -= (float)8.0*floorf(z*(float)0.125); /* trim off integer >= 8 */
84 n = (int32_t) z;
85 z -= (float)n;
86 ih = 0;
87 if(q0>0) { /* need iq[jz-1] to determine n */
88 i = (iq[jz-1]>>(8-q0)); n += i;
89 iq[jz-1] -= i<<(8-q0);
90 ih = iq[jz-1]>>(7-q0);
92 else if(q0==0) ih = iq[jz-1]>>7;
93 else if(z>=(float)0.5) ih=2;
95 if(ih>0) { /* q > 0.5 */
96 n += 1; carry = 0;
97 for(i=0;i<jz ;i++) { /* compute 1-q */
98 j = iq[i];
99 if(carry==0) {
100 if(j!=0) {
101 carry = 1; iq[i] = 0x100- j;
103 } else iq[i] = 0xff - j;
105 if(q0>0) { /* rare case: chance is 1 in 12 */
106 switch(q0) {
107 case 1:
108 iq[jz-1] &= 0x7f; break;
109 case 2:
110 iq[jz-1] &= 0x3f; break;
113 if(ih==2) {
114 z = one - z;
115 if(carry!=0) z -= scalbnf(one,q0);
119 /* check if recomputation is needed */
120 if(z==zero) {
121 j = 0;
122 for (i=jz-1;i>=jk;i--) j |= iq[i];
123 if(j==0) { /* need recomputation */
124 for(k=1;iq[jk-k]==0;k++); /* k = no. of terms needed */
126 for(i=jz+1;i<=jz+k;i++) { /* add q[jz+1] to q[jz+k] */
127 f[jx+i] = (float) ipio2[jv+i];
128 for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j];
129 q[i] = fw;
131 jz += k;
132 goto recompute;
136 /* chop off zero terms */
137 if(z==(float)0.0) {
138 jz -= 1; q0 -= 8;
139 while(iq[jz]==0) { jz--; q0-=8;}
140 } else { /* break z into 8-bit if necessary */
141 z = scalbnf(z,-q0);
142 if(z>=two8) {
143 fw = (float)((int32_t)(twon8*z));
144 iq[jz] = (int32_t)(z-two8*fw);
145 jz += 1; q0 += 8;
146 iq[jz] = (int32_t) fw;
147 } else iq[jz] = (int32_t) z ;
150 /* convert integer "bit" chunk to floating-point value */
151 fw = scalbnf(one,q0);
152 for(i=jz;i>=0;i--) {
153 q[i] = fw*(float)iq[i]; fw*=twon8;
156 /* compute PIo2[0,...,jp]*q[jz,...,0] */
157 for(i=jz;i>=0;i--) {
158 for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k];
159 fq[jz-i] = fw;
162 /* compress fq[] into y[] */
163 switch(prec) {
164 case 0:
165 fw = 0.0;
166 for (i=jz;i>=0;i--) fw += fq[i];
167 y[0] = (ih==0)? fw: -fw;
168 break;
169 case 1:
170 case 2:
171 fw = 0.0;
172 for (i=jz;i>=0;i--) fw += fq[i];
173 fw = *(volatile float *)&fw; /* clip any extra precision */
174 y[0] = (ih==0)? fw: -fw;
175 fw = fq[0]-fw;
176 for (i=1;i<=jz;i++) fw += fq[i];
177 y[1] = (ih==0)? fw: -fw;
178 break;
179 case 3: /* painful */
180 for (i=jz;i>0;i--) {
181 fw = fq[i-1]+fq[i];
182 fq[i] += fq[i-1]-fw;
183 fq[i-1] = fw;
185 for (i=jz;i>1;i--) {
186 fw = fq[i-1]+fq[i];
187 fq[i] += fq[i-1]-fw;
188 fq[i-1] = fw;
190 for (fw=0.0,i=jz;i>=2;i--) fw += fq[i];
191 if(ih==0) {
192 y[0] = fq[0]; y[1] = fq[1]; y[2] = fw;
193 } else {
194 y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw;
197 return n&7;