1 /* From: @(#)e_rem_pio2.c 1.4 95/01/18 */
2 /*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
6 *
7 * Developed at SunSoft, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
10 * is preserved.
11 * ====================================================
12 *
13 * Optimized by Bruce D. Evans.
14 */
15
16
17 /* ld128 version of __ieee754_rem_pio2l(x,y)
18 *
19 * return the remainder of x rem pi/2 in y[0]+y[1]
20 * use __kernel_rem_pio2()
21 */
22
23 #include "../math_ld.h"
24
25 #define BIAS (LDBL_MAX_EXP - 1)
26
27 /*
28 * XXX need to verify that nonzero integer multiples of pi/2 within the
29 * range get no closer to a long double than 2**-140, or that
30 * ilogb(x) + ilogb(min_delta) < 45 - -140.
31 */
32 /*
33 * invpio2: 113 bits of 2/pi
34 * pio2_1: first 68 bits of pi/2
35 * pio2_1t: pi/2 - pio2_1
36 * pio2_2: second 68 bits of pi/2
37 * pio2_2t: pi/2 - (pio2_1+pio2_2)
38 * pio2_3: third 68 bits of pi/2
39 * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3)
40 */
41
42 static const double
43 zero = 0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */
44 two24 = 1.67772160000000000000e+07; /* 0x41700000, 0x00000000 */
45
46 static const long double
47 invpio2 = 6.3661977236758134307553505349005747e-01L, /* 0x145f306dc9c882a53f84eafa3ea6a.0p-113 */
48 pio2_1 = 1.5707963267948966192292994253909555e+00L, /* 0x1921fb54442d18469800000000000.0p-112 */
49 pio2_1t = 2.0222662487959507323996846200947577e-21L, /* 0x13198a2e03707344a4093822299f3.0p-181 */
50 pio2_2 = 2.0222662487959507323994779168837751e-21L, /* 0x13198a2e03707344a400000000000.0p-181 */
51 pio2_2t = 2.0670321098263988236496903051604844e-43L, /* 0x127044533e63a0105df531d89cd91.0p-254 */
52 pio2_3 = 2.0670321098263988236499468110329591e-43L, /* 0x127044533e63a0105e00000000000.0p-254 */
53 pio2_3t = -2.5650587247459238361625433492959285e-65L; /* -0x159c4ec64ddaeb5f78671cbfb2210.0p-327 */
54
55 //VBS
56 //static inline __always_inline int
57 //__ieee754_rem_pio2l(long double x, long double *y)
58
59 static inline int
__ieee754_rem_pio2l(long double x,long double * y)60 __ieee754_rem_pio2l(long double x, long double *y)
61 {
62 union IEEEl2bits u,u1;
63 long double z,w,t,r,fn;
64 double tx[5],ty[3];
65 int64_t n;
66 int e0,ex,i,j,nx;
67 int16_t expsign;
68
69 u.e = x;
70 expsign = u.xbits.expsign;
71 ex = expsign & 0x7fff;
72 if (ex < BIAS + 45 || (ex == BIAS + 45 && u.bits.manh < 0x921fb54442d1LL)) {
73 /* |x| ~< 2^45*(pi/2), medium size */
74 /* Use a specialized rint() to get fn. Assume round-to-nearest. */
75 fn = x*invpio2+0x1.8p112L;
76 fn = fn-0x1.8p112L;
77 #ifdef HAVE_EFFICIENT_I64RINT
78 n = i64rint(fn);
79 #else
80 n = fn;
81 #endif
82 r = x-fn*pio2_1;
83 w = fn*pio2_1t; /* 1st round good to 180 bit */
84 {
85 union IEEEl2bits u2;
86 int ex1;
87 j = ex;
88 y[0] = r-w;
89 u2.e = y[0];
90 ex1 = u2.xbits.expsign & 0x7fff;
91 i = j-ex1;
92 if(i>51) { /* 2nd iteration needed, good to 248 */
93 t = r;
94 w = fn*pio2_2;
95 r = t-w;
96 w = fn*pio2_2t-((t-r)-w);
97 y[0] = r-w;
98 u2.e = y[0];
99 ex1 = u2.xbits.expsign & 0x7fff;
100 i = j-ex1;
101 if(i>119) { /* 3rd iteration need, 316 bits acc */
102 t = r; /* will cover all possible cases */
103 w = fn*pio2_3;
104 r = t-w;
105 w = fn*pio2_3t-((t-r)-w);
106 y[0] = r-w;
107 }
108 }
109 }
110 y[1] = (r-y[0])-w;
111 return n;
112 }
113 /*
114 * all other (large) arguments
115 */
116 if(ex==0x7fff) { /* x is inf or NaN */
117 y[0]=y[1]=x-x; return 0;
118 }
119 /* set z = scalbn(|x|,ilogb(x)-23) */
120 u1.e = x;
121 e0 = ex - BIAS - 23; /* e0 = ilogb(|x|)-23; */
122 u1.xbits.expsign = ex - e0;
123 z = u1.e;
124 for(i=0;i<4;i++) {
125 tx[i] = (double)((int32_t)(z));
126 z = (z-(long double)tx[i])*(long double)two24;
127 }
128 tx[4] = z;
129 nx = 5;
130 while(tx[nx-1]==zero) nx--; /* skip zero term */
131 n = __kernel_rem_pio2(tx,ty,e0,nx,3);
132 t = (long double)ty[2] + (long double)ty[1];
133 r = t + (long double)ty[0];
134 w = (long double)ty[0] - (r - t);
135 if(expsign<0) {y[0] = -r; y[1] = -w; return -n;}
136 y[0] = r; y[1] = w; return n;
137 }
138