VM: munmap used by VM for itself is no longer used
[minix.git] / lib / libm / src / k_rem_pio2f.c
blobb5ad2d6ebff4255e2fe9fe02710464b31ee7f8fe
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 #include <sys/cdefs.h>
17 #if defined(LIBM_SCCS) && !defined(lint)
18 __RCSID("$NetBSD: k_rem_pio2f.c,v 1.8 2010/04/23 19:17:07 drochner Exp $");
19 #endif
21 #include "namespace.h"
22 #include "math.h"
23 #include "math_private.h"
25 /* In the float version, the input parameter x contains 8 bit
26 integers, not 24 bit integers. 113 bit precision is not supported. */
28 static const int init_jk[] = {4,7,9}; /* initial value for jk */
30 static const float PIo2[] = {
31 1.5703125000e+00, /* 0x3fc90000 */
32 4.5776367188e-04, /* 0x39f00000 */
33 2.5987625122e-05, /* 0x37da0000 */
34 7.5437128544e-08, /* 0x33a20000 */
35 6.0026650317e-11, /* 0x2e840000 */
36 7.3896444519e-13, /* 0x2b500000 */
37 5.3845816694e-15, /* 0x27c20000 */
38 5.6378512969e-18, /* 0x22d00000 */
39 8.3009228831e-20, /* 0x1fc40000 */
40 3.2756352257e-22, /* 0x1bc60000 */
41 6.3331015649e-25, /* 0x17440000 */
44 static const float
45 zero = 0.0,
46 one = 1.0,
47 two8 = 2.5600000000e+02, /* 0x43800000 */
48 twon8 = 3.9062500000e-03; /* 0x3b800000 */
50 int
51 __kernel_rem_pio2f(float *x, float *y, int e0, int nx, int prec, const int32_t *ipio2)
53 int32_t jz,jx,jv,jp,jk,carry,n,iq[20],i,j,k,m,q0,ih;
54 float z,fw,f[20],fq[20],q[20];
56 /* initialize jk*/
57 jk = init_jk[prec];
58 jp = jk;
60 /* determine jx,jv,q0, note that 3>q0 */
61 jx = nx-1;
62 jv = (e0-3)/8; if(jv<0) jv=0;
63 q0 = e0-8*(jv+1);
65 /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
66 j = jv-jx; m = jx+jk;
67 for(i=0;i<=m;i++,j++) f[i] = (j<0)? zero : (float) ipio2[j];
69 /* compute q[0],q[1],...q[jk] */
70 for (i=0;i<=jk;i++) {
71 for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j]; q[i] = fw;
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;
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);
94 else if(q0==0) ih = iq[jz-1]>>8;
95 else if(z>=(float)0.5) ih=2;
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;
105 } else iq[i] = 0xff - j;
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;
115 if(ih==2) {
116 z = one - z;
117 if(carry!=0) z -= scalbnf(one,q0);
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 for(k=1;iq[jk-k]==0;k++); /* k = no. of terms needed */
128 for(i=jz+1;i<=jz+k;i++) { /* add q[jz+1] to q[jz+k] */
129 f[jx+i] = (float) ipio2[jv+i];
130 for(j=0,fw=0.0;j<=jx;j++) fw += x[j]*f[jx+i-j];
131 q[i] = fw;
133 jz += k;
134 goto recompute;
138 /* chop off zero terms */
139 if(z==(float)0.0) {
140 jz -= 1; q0 -= 8;
141 while(iq[jz]==0) { jz--; q0-=8;}
142 } else { /* break z into 8-bit if necessary */
143 z = scalbnf(z,-q0);
144 if(z>=two8) {
145 fw = (float)((int32_t)(twon8*z));
146 iq[jz] = (int32_t)(z-two8*fw);
147 jz += 1; q0 += 8;
148 iq[jz] = (int32_t) fw;
149 } else iq[jz] = (int32_t) z ;
152 /* convert integer "bit" chunk to floating-point value */
153 fw = scalbnf(one,q0);
154 for(i=jz;i>=0;i--) {
155 q[i] = fw*(float)iq[i]; fw*=twon8;
158 /* compute PIo2[0,...,jp]*q[jz,...,0] */
159 for(i=jz;i>=0;i--) {
160 for(fw=0.0,k=0;k<=jp&&k<=jz-i;k++) fw += PIo2[k]*q[i+k];
161 fq[jz-i] = fw;
164 /* compress fq[] into y[] */
165 switch(prec) {
166 case 0:
167 fw = 0.0;
168 for (i=jz;i>=0;i--) fw += fq[i];
169 y[0] = (ih==0)? fw: -fw;
170 break;
171 case 1:
172 case 2:
173 fw = 0.0;
174 for (i=jz;i>=0;i--) fw += fq[i];
175 y[0] = (ih==0)? fw: -fw;
176 fw = fq[0]-fw;
177 for (i=1;i<=jz;i++) fw += fq[i];
178 y[1] = (ih==0)? fw: -fw;
179 break;
180 case 3: /* painful */
181 for (i=jz;i>0;i--) {
182 fw = fq[i-1]+fq[i];
183 fq[i] += fq[i-1]-fw;
184 fq[i-1] = fw;
186 for (i=jz;i>1;i--) {
187 fw = fq[i-1]+fq[i];
188 fq[i] += fq[i-1]-fw;
189 fq[i-1] = fw;
191 for (fw=0.0,i=jz;i>=2;i--) fw += fq[i];
192 if(ih==0) {
193 y[0] = fq[0]; y[1] = fq[1]; y[2] = fw;
194 } else {
195 y[0] = -fq[0]; y[1] = -fq[1]; y[2] = -fw;
198 return n&7;