1*f504f610SAugustin Cavalier /* origin: FreeBSD /usr/src/lib/msun/ld80/e_rem_pio2.c */
2*f504f610SAugustin Cavalier /*
3*f504f610SAugustin Cavalier * ====================================================
4*f504f610SAugustin Cavalier * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5*f504f610SAugustin Cavalier * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
6*f504f610SAugustin Cavalier *
7*f504f610SAugustin Cavalier * Developed at SunSoft, a Sun Microsystems, Inc. business.
8*f504f610SAugustin Cavalier * Permission to use, copy, modify, and distribute this
9*f504f610SAugustin Cavalier * software is freely granted, provided that this notice
10*f504f610SAugustin Cavalier * is preserved.
11*f504f610SAugustin Cavalier * ====================================================
12*f504f610SAugustin Cavalier *
13*f504f610SAugustin Cavalier * Optimized by Bruce D. Evans.
14*f504f610SAugustin Cavalier */
15*f504f610SAugustin Cavalier #include "libm.h"
16*f504f610SAugustin Cavalier #if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
17*f504f610SAugustin Cavalier /* ld80 and ld128 version of __rem_pio2(x,y)
18*f504f610SAugustin Cavalier *
19*f504f610SAugustin Cavalier * return the remainder of x rem pi/2 in y[0]+y[1]
20*f504f610SAugustin Cavalier * use __rem_pio2_large() for large x
21*f504f610SAugustin Cavalier */
22*f504f610SAugustin Cavalier
23*f504f610SAugustin Cavalier static const long double toint = 1.5/LDBL_EPSILON;
24*f504f610SAugustin Cavalier
25*f504f610SAugustin Cavalier #if LDBL_MANT_DIG == 64
26*f504f610SAugustin Cavalier /* u ~< 0x1p25*pi/2 */
27*f504f610SAugustin Cavalier #define SMALL(u) (((u.i.se & 0x7fffU)<<16 | u.i.m>>48) < ((0x3fff + 25)<<16 | 0x921f>>1 | 0x8000))
28*f504f610SAugustin Cavalier #define QUOBITS(x) ((uint32_t)(int32_t)x & 0x7fffffff)
29*f504f610SAugustin Cavalier #define ROUND1 22
30*f504f610SAugustin Cavalier #define ROUND2 61
31*f504f610SAugustin Cavalier #define NX 3
32*f504f610SAugustin Cavalier #define NY 2
33*f504f610SAugustin Cavalier /*
34*f504f610SAugustin Cavalier * invpio2: 64 bits of 2/pi
35*f504f610SAugustin Cavalier * pio2_1: first 39 bits of pi/2
36*f504f610SAugustin Cavalier * pio2_1t: pi/2 - pio2_1
37*f504f610SAugustin Cavalier * pio2_2: second 39 bits of pi/2
38*f504f610SAugustin Cavalier * pio2_2t: pi/2 - (pio2_1+pio2_2)
39*f504f610SAugustin Cavalier * pio2_3: third 39 bits of pi/2
40*f504f610SAugustin Cavalier * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3)
41*f504f610SAugustin Cavalier */
42*f504f610SAugustin Cavalier static const double
43*f504f610SAugustin Cavalier pio2_1 = 1.57079632679597125389e+00, /* 0x3FF921FB, 0x54444000 */
44*f504f610SAugustin Cavalier pio2_2 = -1.07463465549783099519e-12, /* -0x12e7b967674000.0p-92 */
45*f504f610SAugustin Cavalier pio2_3 = 6.36831716351370313614e-25; /* 0x18a2e037074000.0p-133 */
46*f504f610SAugustin Cavalier static const long double
47*f504f610SAugustin Cavalier invpio2 = 6.36619772367581343076e-01L, /* 0xa2f9836e4e44152a.0p-64 */
48*f504f610SAugustin Cavalier pio2_1t = -1.07463465549719416346e-12L, /* -0x973dcb3b399d747f.0p-103 */
49*f504f610SAugustin Cavalier pio2_2t = 6.36831716351095013979e-25L, /* 0xc51701b839a25205.0p-144 */
50*f504f610SAugustin Cavalier pio2_3t = -2.75299651904407171810e-37L; /* -0xbb5bf6c7ddd660ce.0p-185 */
51*f504f610SAugustin Cavalier #elif LDBL_MANT_DIG == 113
52*f504f610SAugustin Cavalier /* u ~< 0x1p45*pi/2 */
53*f504f610SAugustin Cavalier #define SMALL(u) (((u.i.se & 0x7fffU)<<16 | u.i.top) < ((0x3fff + 45)<<16 | 0x921f))
54*f504f610SAugustin Cavalier #define QUOBITS(x) ((uint32_t)(int64_t)x & 0x7fffffff)
55*f504f610SAugustin Cavalier #define ROUND1 51
56*f504f610SAugustin Cavalier #define ROUND2 119
57*f504f610SAugustin Cavalier #define NX 5
58*f504f610SAugustin Cavalier #define NY 3
59*f504f610SAugustin Cavalier static const long double
60*f504f610SAugustin Cavalier invpio2 = 6.3661977236758134307553505349005747e-01L, /* 0x145f306dc9c882a53f84eafa3ea6a.0p-113 */
61*f504f610SAugustin Cavalier pio2_1 = 1.5707963267948966192292994253909555e+00L, /* 0x1921fb54442d18469800000000000.0p-112 */
62*f504f610SAugustin Cavalier pio2_1t = 2.0222662487959507323996846200947577e-21L, /* 0x13198a2e03707344a4093822299f3.0p-181 */
63*f504f610SAugustin Cavalier pio2_2 = 2.0222662487959507323994779168837751e-21L, /* 0x13198a2e03707344a400000000000.0p-181 */
64*f504f610SAugustin Cavalier pio2_2t = 2.0670321098263988236496903051604844e-43L, /* 0x127044533e63a0105df531d89cd91.0p-254 */
65*f504f610SAugustin Cavalier pio2_3 = 2.0670321098263988236499468110329591e-43L, /* 0x127044533e63a0105e00000000000.0p-254 */
66*f504f610SAugustin Cavalier pio2_3t = -2.5650587247459238361625433492959285e-65L; /* -0x159c4ec64ddaeb5f78671cbfb2210.0p-327 */
67*f504f610SAugustin Cavalier #endif
68*f504f610SAugustin Cavalier
__rem_pio2l(long double x,long double * y)69*f504f610SAugustin Cavalier int __rem_pio2l(long double x, long double *y)
70*f504f610SAugustin Cavalier {
71*f504f610SAugustin Cavalier union ldshape u,uz;
72*f504f610SAugustin Cavalier long double z,w,t,r,fn;
73*f504f610SAugustin Cavalier double tx[NX],ty[NY];
74*f504f610SAugustin Cavalier int ex,ey,n,i;
75*f504f610SAugustin Cavalier
76*f504f610SAugustin Cavalier u.f = x;
77*f504f610SAugustin Cavalier ex = u.i.se & 0x7fff;
78*f504f610SAugustin Cavalier if (SMALL(u)) {
79*f504f610SAugustin Cavalier /* rint(x/(pi/2)), Assume round-to-nearest. */
80*f504f610SAugustin Cavalier fn = x*invpio2 + toint - toint;
81*f504f610SAugustin Cavalier n = QUOBITS(fn);
82*f504f610SAugustin Cavalier r = x-fn*pio2_1;
83*f504f610SAugustin Cavalier w = fn*pio2_1t; /* 1st round good to 102/180 bits (ld80/ld128) */
84*f504f610SAugustin Cavalier y[0] = r-w;
85*f504f610SAugustin Cavalier u.f = y[0];
86*f504f610SAugustin Cavalier ey = u.i.se & 0x7fff;
87*f504f610SAugustin Cavalier if (ex - ey > ROUND1) { /* 2nd iteration needed, good to 141/248 (ld80/ld128) */
88*f504f610SAugustin Cavalier t = r;
89*f504f610SAugustin Cavalier w = fn*pio2_2;
90*f504f610SAugustin Cavalier r = t-w;
91*f504f610SAugustin Cavalier w = fn*pio2_2t-((t-r)-w);
92*f504f610SAugustin Cavalier y[0] = r-w;
93*f504f610SAugustin Cavalier u.f = y[0];
94*f504f610SAugustin Cavalier ey = u.i.se & 0x7fff;
95*f504f610SAugustin Cavalier if (ex - ey > ROUND2) { /* 3rd iteration, good to 180/316 bits */
96*f504f610SAugustin Cavalier t = r; /* will cover all possible cases (not verified for ld128) */
97*f504f610SAugustin Cavalier w = fn*pio2_3;
98*f504f610SAugustin Cavalier r = t-w;
99*f504f610SAugustin Cavalier w = fn*pio2_3t-((t-r)-w);
100*f504f610SAugustin Cavalier y[0] = r-w;
101*f504f610SAugustin Cavalier }
102*f504f610SAugustin Cavalier }
103*f504f610SAugustin Cavalier y[1] = (r - y[0]) - w;
104*f504f610SAugustin Cavalier return n;
105*f504f610SAugustin Cavalier }
106*f504f610SAugustin Cavalier /*
107*f504f610SAugustin Cavalier * all other (large) arguments
108*f504f610SAugustin Cavalier */
109*f504f610SAugustin Cavalier if (ex == 0x7fff) { /* x is inf or NaN */
110*f504f610SAugustin Cavalier y[0] = y[1] = x - x;
111*f504f610SAugustin Cavalier return 0;
112*f504f610SAugustin Cavalier }
113*f504f610SAugustin Cavalier /* set z = scalbn(|x|,-ilogb(x)+23) */
114*f504f610SAugustin Cavalier uz.f = x;
115*f504f610SAugustin Cavalier uz.i.se = 0x3fff + 23;
116*f504f610SAugustin Cavalier z = uz.f;
117*f504f610SAugustin Cavalier for (i=0; i < NX - 1; i++) {
118*f504f610SAugustin Cavalier tx[i] = (double)(int32_t)z;
119*f504f610SAugustin Cavalier z = (z-tx[i])*0x1p24;
120*f504f610SAugustin Cavalier }
121*f504f610SAugustin Cavalier tx[i] = z;
122*f504f610SAugustin Cavalier while (tx[i] == 0)
123*f504f610SAugustin Cavalier i--;
124*f504f610SAugustin Cavalier n = __rem_pio2_large(tx, ty, ex-0x3fff-23, i+1, NY);
125*f504f610SAugustin Cavalier w = ty[1];
126*f504f610SAugustin Cavalier if (NY == 3)
127*f504f610SAugustin Cavalier w += ty[2];
128*f504f610SAugustin Cavalier r = ty[0] + w;
129*f504f610SAugustin Cavalier /* TODO: for ld128 this does not follow the recommendation of the
130*f504f610SAugustin Cavalier comments of __rem_pio2_large which seem wrong if |ty[0]| > |ty[1]+ty[2]| */
131*f504f610SAugustin Cavalier w -= r - ty[0];
132*f504f610SAugustin Cavalier if (u.i.se >> 15) {
133*f504f610SAugustin Cavalier y[0] = -r;
134*f504f610SAugustin Cavalier y[1] = -w;
135*f504f610SAugustin Cavalier return -n;
136*f504f610SAugustin Cavalier }
137*f504f610SAugustin Cavalier y[0] = r;
138*f504f610SAugustin Cavalier y[1] = w;
139*f504f610SAugustin Cavalier return n;
140*f504f610SAugustin Cavalier }
141*f504f610SAugustin Cavalier #endif
142