xref: /freebsd/lib/msun/src/e_rem_pio2.c (revision a91a2465)
1 
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  *
12  * Optimized by Bruce D. Evans.
13  */
14 
15 /* __ieee754_rem_pio2(x,y)
16  *
17  * return the remainder of x rem pi/2 in y[0]+y[1]
18  * use __kernel_rem_pio2()
19  */
20 
21 #include <float.h>
22 
23 #include "math.h"
24 #include "math_private.h"
25 
26 /*
27  * invpio2:  53 bits of 2/pi
28  * pio2_1:   first  33 bit of pi/2
29  * pio2_1t:  pi/2 - pio2_1
30  * pio2_2:   second 33 bit of pi/2
31  * pio2_2t:  pi/2 - (pio2_1+pio2_2)
32  * pio2_3:   third  33 bit of pi/2
33  * pio2_3t:  pi/2 - (pio2_1+pio2_2+pio2_3)
34  */
35 
36 static const double
37 zero =  0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */
38 two24 =  1.67772160000000000000e+07, /* 0x41700000, 0x00000000 */
39 invpio2 =  6.36619772367581382433e-01, /* 0x3FE45F30, 0x6DC9C883 */
40 pio2_1  =  1.57079632673412561417e+00, /* 0x3FF921FB, 0x54400000 */
41 pio2_1t =  6.07710050650619224932e-11, /* 0x3DD0B461, 0x1A626331 */
42 pio2_2  =  6.07710050630396597660e-11, /* 0x3DD0B461, 0x1A600000 */
43 pio2_2t =  2.02226624879595063154e-21, /* 0x3BA3198A, 0x2E037073 */
44 pio2_3  =  2.02226624871116645580e-21, /* 0x3BA3198A, 0x2E000000 */
45 pio2_3t =  8.47842766036889956997e-32; /* 0x397B839A, 0x252049C1 */
46 
47 #ifdef INLINE_REM_PIO2
48 static __inline __always_inline
49 #endif
50 int
51 __ieee754_rem_pio2(double x, double *y)
52 {
53 	double z,w,t,r,fn;
54 	double tx[3],ty[2];
55 	int32_t e0,i,j,nx,n,ix,hx;
56 	u_int32_t low;
57 
58 	GET_HIGH_WORD(hx,x);		/* high word of x */
59 	ix = hx&0x7fffffff;
60 #if 0 /* Must be handled in caller. */
61 	if(ix<=0x3fe921fb)   /* |x| ~<= pi/4 , no need for reduction */
62 	    {y[0] = x; y[1] = 0; return 0;}
63 #endif
64 	if (ix <= 0x400f6a7a) {		/* |x| ~<= 5pi/4 */
65 	    if ((ix & 0xfffff) == 0x921fb)  /* |x| ~= pi/2 or 2pi/2 */
66 		goto medium;		/* cancellation -- use medium case */
67 	    if (ix <= 0x4002d97c) {	/* |x| ~<= 3pi/4 */
68 		if (hx > 0) {
69 		    z = x - pio2_1;	/* one round good to 85 bits */
70 		    y[0] = z - pio2_1t;
71 		    y[1] = (z-y[0])-pio2_1t;
72 		    return 1;
73 		} else {
74 		    z = x + pio2_1;
75 		    y[0] = z + pio2_1t;
76 		    y[1] = (z-y[0])+pio2_1t;
77 		    return -1;
78 		}
79 	    } else {
80 		if (hx > 0) {
81 		    z = x - 2*pio2_1;
82 		    y[0] = z - 2*pio2_1t;
83 		    y[1] = (z-y[0])-2*pio2_1t;
84 		    return 2;
85 		} else {
86 		    z = x + 2*pio2_1;
87 		    y[0] = z + 2*pio2_1t;
88 		    y[1] = (z-y[0])+2*pio2_1t;
89 		    return -2;
90 		}
91 	    }
92 	}
93 	if (ix <= 0x401c463b) {		/* |x| ~<= 9pi/4 */
94 	    if (ix <= 0x4015fdbc) {	/* |x| ~<= 7pi/4 */
95 		if (ix == 0x4012d97c)	/* |x| ~= 3pi/2 */
96 		    goto medium;
97 		if (hx > 0) {
98 		    z = x - 3*pio2_1;
99 		    y[0] = z - 3*pio2_1t;
100 		    y[1] = (z-y[0])-3*pio2_1t;
101 		    return 3;
102 		} else {
103 		    z = x + 3*pio2_1;
104 		    y[0] = z + 3*pio2_1t;
105 		    y[1] = (z-y[0])+3*pio2_1t;
106 		    return -3;
107 		}
108 	    } else {
109 		if (ix == 0x401921fb)	/* |x| ~= 4pi/2 */
110 		    goto medium;
111 		if (hx > 0) {
112 		    z = x - 4*pio2_1;
113 		    y[0] = z - 4*pio2_1t;
114 		    y[1] = (z-y[0])-4*pio2_1t;
115 		    return 4;
116 		} else {
117 		    z = x + 4*pio2_1;
118 		    y[0] = z + 4*pio2_1t;
119 		    y[1] = (z-y[0])+4*pio2_1t;
120 		    return -4;
121 		}
122 	    }
123 	}
124 	if(ix<0x413921fb) {	/* |x| ~< 2^20*(pi/2), medium size */
125 medium:
126 	    fn = rnint((double_t)x*invpio2);
127 	    n  = irint(fn);
128 	    r  = x-fn*pio2_1;
129 	    w  = fn*pio2_1t;	/* 1st round good to 85 bit */
130 	    {
131 	        u_int32_t high;
132 	        j  = ix>>20;
133 	        y[0] = r-w;
134 		GET_HIGH_WORD(high,y[0]);
135 	        i = j-((high>>20)&0x7ff);
136 	        if(i>16) {  /* 2nd iteration needed, good to 118 */
137 		    t  = r;
138 		    w  = fn*pio2_2;
139 		    r  = t-w;
140 		    w  = fn*pio2_2t-((t-r)-w);
141 		    y[0] = r-w;
142 		    GET_HIGH_WORD(high,y[0]);
143 		    i = j-((high>>20)&0x7ff);
144 		    if(i>49)  {	/* 3rd iteration need, 151 bits acc */
145 		    	t  = r;	/* will cover all possible cases */
146 		    	w  = fn*pio2_3;
147 		    	r  = t-w;
148 		    	w  = fn*pio2_3t-((t-r)-w);
149 		    	y[0] = r-w;
150 		    }
151 		}
152 	    }
153 	    y[1] = (r-y[0])-w;
154 	    return n;
155 	}
156     /*
157      * all other (large) arguments
158      */
159 	if(ix>=0x7ff00000) {		/* x is inf or NaN */
160 	    y[0]=y[1]=x-x; return 0;
161 	}
162     /* set z = scalbn(|x|,ilogb(x)-23) */
163 	GET_LOW_WORD(low,x);
164 	e0 	= (ix>>20)-1046;	/* e0 = ilogb(z)-23; */
165 	INSERT_WORDS(z, ix - ((int32_t)(e0<<20)), low);
166 	for(i=0;i<2;i++) {
167 		tx[i] = (double)((int32_t)(z));
168 		z     = (z-tx[i])*two24;
169 	}
170 	tx[2] = z;
171 	nx = 3;
172 	while(tx[nx-1]==zero) nx--;	/* skip zero term */
173 	n  =  __kernel_rem_pio2(tx,ty,e0,nx,1);
174 	if(hx<0) {y[0] = -ty[0]; y[1] = -ty[1]; return -n;}
175 	y[0] = ty[0]; y[1] = ty[1]; return n;
176 }
177