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.
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
13 * ====================================================
16 #include <sys/cdefs.h>
17 #if defined(LIBM_SCCS) && !defined(lint)
18 __RCSID("$NetBSD: k_rem_pio2f.c,v 1.6 1999/07/02 15:37:41 simonb Exp $");
22 #include "math_private.h"
24 /* In the float version, the input parameter x contains 8 bit
25 integers, not 24 bit integers. 113 bit precision is not supported. */
27 static const int init_jk
[] = {4,7,9}; /* initial value for jk */
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 */
46 two8
= 2.5600000000e+02, /* 0x43800000 */
47 twon8
= 3.9062500000e-03; /* 0x3b800000 */
50 __kernel_rem_pio2f(float *x
, float *y
, int e0
, int nx
, int prec
, const int32_t *ipio2
)
52 int32_t jz
,jx
,jv
,jp
,jk
,carry
,n
,iq
[20],i
,j
,k
,m
,q0
,ih
;
53 float z
,fw
,f
[20],fq
[20],q
[20];
59 /* determine jx,jv,q0, note that 3>q0 */
61 jv
= (e0
-3)/8; if(jv
<0) jv
=0;
64 /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */
66 for(i
=0;i
<=m
;i
++,j
++) f
[i
] = (j
<0)? zero
: (float) ipio2
[j
];
68 /* compute q[0],q[1],...q[jk] */
70 for(j
=0,fw
=0.0;j
<=jx
;j
++) fw
+= x
[j
]*f
[jx
+i
-j
]; q
[i
] = fw
;
75 /* distill q[] into iq[] reversingly */
76 for(i
=0,j
=jz
,z
=q
[jz
];j
>0;i
++,j
--) {
77 fw
= (float)((int32_t)(twon8
* z
));
78 iq
[i
] = (int32_t)(z
-two8
*fw
);
83 z
= scalbnf(z
,q0
); /* actual value of z */
84 z
-= (float)8.0*floorf(z
*(float)0.125); /* trim off integer >= 8 */
88 if(q0
>0) { /* need iq[jz-1] to determine n */
89 i
= (iq
[jz
-1]>>(8-q0
)); n
+= i
;
90 iq
[jz
-1] -= i
<<(8-q0
);
91 ih
= iq
[jz
-1]>>(7-q0
);
93 else if(q0
==0) ih
= iq
[jz
-1]>>8;
94 else if(z
>=(float)0.5) ih
=2;
96 if(ih
>0) { /* q > 0.5 */
98 for(i
=0;i
<jz
;i
++) { /* compute 1-q */
102 carry
= 1; iq
[i
] = 0x100- j
;
104 } else iq
[i
] = 0xff - j
;
106 if(q0
>0) { /* rare case: chance is 1 in 12 */
109 iq
[jz
-1] &= 0x7f; break;
111 iq
[jz
-1] &= 0x3f; break;
116 if(carry
!=0) z
-= scalbnf(one
,q0
);
120 /* check if recomputation is needed */
123 for (i
=jz
-1;i
>=jk
;i
--) j
|= iq
[i
];
124 if(j
==0) { /* need recomputation */
125 for(k
=1;iq
[jk
-k
]==0;k
++); /* k = no. of terms needed */
127 for(i
=jz
+1;i
<=jz
+k
;i
++) { /* add q[jz+1] to q[jz+k] */
128 f
[jx
+i
] = (float) ipio2
[jv
+i
];
129 for(j
=0,fw
=0.0;j
<=jx
;j
++) fw
+= x
[j
]*f
[jx
+i
-j
];
137 /* chop off zero terms */
140 while(iq
[jz
]==0) { jz
--; q0
-=8;}
141 } else { /* break z into 8-bit if necessary */
144 fw
= (float)((int32_t)(twon8
*z
));
145 iq
[jz
] = (int32_t)(z
-two8
*fw
);
147 iq
[jz
] = (int32_t) fw
;
148 } else iq
[jz
] = (int32_t) z
;
151 /* convert integer "bit" chunk to floating-point value */
152 fw
= scalbnf(one
,q0
);
154 q
[i
] = fw
*(float)iq
[i
]; fw
*=twon8
;
157 /* compute PIo2[0,...,jp]*q[jz,...,0] */
159 for(fw
=0.0,k
=0;k
<=jp
&&k
<=jz
-i
;k
++) fw
+= PIo2
[k
]*q
[i
+k
];
163 /* compress fq[] into y[] */
167 for (i
=jz
;i
>=0;i
--) fw
+= fq
[i
];
168 y
[0] = (ih
==0)? fw
: -fw
;
173 for (i
=jz
;i
>=0;i
--) fw
+= fq
[i
];
174 y
[0] = (ih
==0)? fw
: -fw
;
176 for (i
=1;i
<=jz
;i
++) fw
+= fq
[i
];
177 y
[1] = (ih
==0)? fw
: -fw
;
179 case 3: /* painful */
190 for (fw
=0.0,i
=jz
;i
>=2;i
--) fw
+= fq
[i
];
192 y
[0] = fq
[0]; y
[1] = fq
[1]; y
[2] = fw
;
194 y
[0] = -fq
[0]; y
[1] = -fq
[1]; y
[2] = -fw
;