e_rem_pio2f.c revision 151855
1/* e_rem_pio2f.c -- float version of e_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#ifndef lint
17static char rcsid[] = "$FreeBSD: head/lib/msun/src/e_rem_pio2f.c 151855 2005-10-29 08:15:29Z bde $";
18#endif
19
20/* __ieee754_rem_pio2f(x,y)
21 *
22 * return the remainder of x rem pi/2 in y[0]+y[1]
23 * use __kernel_rem_pio2f()
24 */
25
26#include "math.h"
27#include "math_private.h"
28
29/* Clip any extra precision in the float variable v. */
30#define	cliptofloat(v)	(*(volatile float *)&(v))
31
32/*
33 * Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi
34 */
35static const int32_t two_over_pi[] = {
360xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62,
370x95993C, 0x439041, 0xFE5163, 0xABDEBB, 0xC561B7, 0x246E3A,
380x424DD2, 0xE00649, 0x2EEA09, 0xD1921C, 0xFE1DEB, 0x1CB129,
390xA73EE8, 0x8235F5, 0x2EBB44, 0x84E99C, 0x7026B4, 0x5F7E41,
400x3991D6, 0x398353, 0x39F49C, 0x845F8B, 0xBDF928, 0x3B1FF8,
410x97FFDE, 0x05980F, 0xEF2F11, 0x8B5A0A, 0x6D1F6D, 0x367ECF,
420x27CB09, 0xB74F46, 0x3F669E, 0x5FEA2D, 0x7527BA, 0xC7EBE5,
430xF17B3D, 0x0739F7, 0x8A5292, 0xEA6BFB, 0x5FB11F, 0x8D5D08,
440x560330, 0x46FC7B, 0x6BABF0, 0xCFBC20, 0x9AF436, 0x1DA9E3,
450x91615E, 0xE61B08, 0x659985, 0x5F14A0, 0x68408D, 0xFFD880,
460x4D7327, 0x310606, 0x1556CA, 0x73A8C9, 0x60E27B, 0xC08C6B,
47};
48
49/*
50 * This array is like the one in e_rem_pio2.c, but the numbers are
51 * single precision and the last few bits (8 here) are ignored by
52 * masking them off in the float word instead of by omitting the low
53 * word.
54 *
55 * Masking off 8 bits is not enough, but we defer further masking to
56 * runtime so that the mask is easy to change.  We now mask off 21
57 * bits, which is the smallest number that makes the "quick check no
58 * cancellation" detect all cancellations for cases that it is used.
59 * It doesn't detect all non-cancellations, especiallly for small
60 * multiples of pi/2, but then the non-quick code selects the best
61 * approximation of pi/2 to use.  The result is that arg reduction is
62 * always done with between 8 or 9 and 17 bits of extra precision in
63 * the medium-multiple case.  With only 8 bits masked of we had
64 * negative extra precision in some cases starting near +-13*pi/2.
65 */
66static const int32_t npio2_hw[] = {
670x3fc90f00, 0x40490f00, 0x4096cb00, 0x40c90f00, 0x40fb5300, 0x4116cb00,
680x412fed00, 0x41490f00, 0x41623100, 0x417b5300, 0x418a3a00, 0x4196cb00,
690x41a35c00, 0x41afed00, 0x41bc7e00, 0x41c90f00, 0x41d5a000, 0x41e23100,
700x41eec200, 0x41fb5300, 0x4203f200, 0x420a3a00, 0x42108300, 0x4216cb00,
710x421d1400, 0x42235c00, 0x4229a500, 0x422fed00, 0x42363600, 0x423c7e00,
720x4242c700, 0x42490f00
73};
74
75/*
76 * invpio2:  24 bits of 2/pi
77 * pio2_1:   first  17 bit of pi/2
78 * pio2_1t:  pi/2 - pio2_1
79 * pio2_2:   second 17 bit of pi/2
80 * pio2_2t:  pi/2 - (pio2_1+pio2_2)
81 * pio2_3:   third  17 bit of pi/2
82 * pio2_3t:  pi/2 - (pio2_1+pio2_2+pio2_3)
83 */
84
85static const float
86zero =  0.0000000000e+00, /* 0x00000000 */
87half =  5.0000000000e-01, /* 0x3f000000 */
88invpio2 =  6.3661980629e-01, /* 0x3f22f984 */
89pio2_1  =  1.5707855225e+00, /* 0x3fc90f80 */
90pio2_1t =  1.0804334124e-05, /* 0x37354443 */
91pio2_2  =  1.0804273188e-05, /* 0x37354400 */
92pio2_2t =  6.0770999344e-11, /* 0x2e85a308 */
93pio2_3  =  6.0770943833e-11, /* 0x2e85a300 */
94pio2_3t =  6.1232342629e-17; /* 0x248d3132 */
95
96static const double
97two24 =  1.67772160000000000000e+07; /* 0x41700000, 0x00000000 */
98
99	int32_t __ieee754_rem_pio2f(float x, float *y)
100{
101	double dz;
102	float z,w,t,r,fn;
103	double tx[3];
104	int32_t e0,i,j,nx,n,ix,hx;
105
106	GET_FLOAT_WORD(hx,x);
107	ix = hx&0x7fffffff;
108	if(ix<=0x3f490fd8)   /* |x| ~<= pi/4 , no need for reduction */
109	    {y[0] = x; y[1] = 0; return 0;}
110	if(ix<0x4016cbe4) {  /* |x| < 3pi/4, special case with n=+-1 */
111	    if(hx>0) {
112		z = x - pio2_1;
113		if((ix&0xfffe0000)!=0x3fc80000) { /* 17+24 bit pi OK */
114		    y[0] = z - pio2_1t;
115		    y[1] = (z-cliptofloat(y[0]))-pio2_1t;
116		} else {		/* near pi/2, use 17+17+24 bit pi */
117		    z -= pio2_2;
118		    y[0] = z - pio2_2t;
119		    y[1] = (z-cliptofloat(y[0]))-pio2_2t;
120		}
121		return 1;
122	    } else {	/* negative x */
123		z = x + pio2_1;
124		if((ix&0xfffe0000)!=0x3fc80000) { /* 17+24 bit pi OK */
125		    y[0] = z + pio2_1t;
126		    y[1] = (z-cliptofloat(y[0]))+pio2_1t;
127		} else {		/* near pi/2, use 17+17+24 bit pi */
128		    z += pio2_2;
129		    y[0] = z + pio2_2t;
130		    y[1] = (z-cliptofloat(y[0]))+pio2_2t;
131		}
132		return -1;
133	    }
134	}
135	if(ix<=0x43490f80) { /* |x| ~<= 2^7*(pi/2), medium size */
136	    t  = fabsf(x);
137	    n  = (int32_t) (t*invpio2+half);
138	    fn = (float)n;
139	    r  = t-fn*pio2_1;
140	    w  = fn*pio2_1t;	/* 1st round good to 40 bit */
141	    if(n<32&&(ix&0xffe00000)!=(npio2_hw[n-1]&0xffe00000)) {
142		y[0] = r-w;	/* quick check no cancellation */
143	    } else {
144	        u_int32_t high;
145	        j  = ix>>23;
146	        y[0] = r-w;
147		GET_FLOAT_WORD(high,y[0]);
148	        i = j-((high>>23)&0xff);
149	        if(i>8) {  /* 2nd iteration needed, good to 57 */
150		    t  = r;
151		    w  = fn*pio2_2;
152		    r  = t-w;
153		    w  = fn*pio2_2t-((t-r)-w);
154		    y[0] = r-w;
155		    GET_FLOAT_WORD(high,y[0]);
156		    i = j-((high>>23)&0xff);
157		    if(i>25)  {	/* 3rd iteration need, 74 bits acc */
158		    	t  = r;	/* will cover all possible cases */
159		    	w  = fn*pio2_3;
160		    	r  = t-w;
161		    	w  = fn*pio2_3t-((t-r)-w);
162		    	y[0] = r-w;
163		    }
164		}
165	    }
166	    y[1] = (r-cliptofloat(y[0]))-w;
167	    if(hx<0) 	{y[0] = -y[0]; y[1] = -y[1]; return -n;}
168	    else	 return n;
169	}
170    /*
171     * all other (large) arguments
172     */
173	if(ix>=0x7f800000) {		/* x is inf or NaN */
174	    y[0]=y[1]=x-x; return 0;
175	}
176#define	z	dz
177    /* set z = scalbn(|x|,ilogb(x)-23) */
178	z = x;
179	GET_HIGH_WORD(hx,z);
180	ix = hx&0x7fffffff;
181	e0 	= (ix>>20)-1046;	/* e0 = ilogb(z)-23; */
182	SET_HIGH_WORD(z, ix - ((int32_t)(e0<<20)));
183	for(i=0;i<2;i++) {
184		tx[i] = (double)((int32_t)(z));
185		z     = (z-tx[i])*two24;
186	}
187	tx[2] = z;
188	nx = 3;
189	while(tx[nx-1]==zero) nx--;	/* skip zero term */
190	n  =  __kernel_rem_pio2(tx,&z,e0,nx,1,two_over_pi);
191	y[0] = z;
192	y[1] = z - y[0];
193	if(hx<0) {y[0] = -y[0]; y[1] = -y[1]; return -n;}
194	return n;
195}
196