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