1570af302Sopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/src/e_rem_pio2.c */
2570af302Sopenharmony_ci/*
3570af302Sopenharmony_ci * ====================================================
4570af302Sopenharmony_ci * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5570af302Sopenharmony_ci *
6570af302Sopenharmony_ci * Developed at SunSoft, a Sun Microsystems, Inc. business.
7570af302Sopenharmony_ci * Permission to use, copy, modify, and distribute this
8570af302Sopenharmony_ci * software is freely granted, provided that this notice
9570af302Sopenharmony_ci * is preserved.
10570af302Sopenharmony_ci * ====================================================
11570af302Sopenharmony_ci *
12570af302Sopenharmony_ci * Optimized by Bruce D. Evans.
13570af302Sopenharmony_ci */
14570af302Sopenharmony_ci/* __rem_pio2(x,y)
15570af302Sopenharmony_ci *
16570af302Sopenharmony_ci * return the remainder of x rem pi/2 in y[0]+y[1]
17570af302Sopenharmony_ci * use __rem_pio2_large() for large x
18570af302Sopenharmony_ci */
19570af302Sopenharmony_ci
20570af302Sopenharmony_ci#include "libm.h"
21570af302Sopenharmony_ci
22570af302Sopenharmony_ci#if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1
23570af302Sopenharmony_ci#define EPS DBL_EPSILON
24570af302Sopenharmony_ci#elif FLT_EVAL_METHOD==2
25570af302Sopenharmony_ci#define EPS LDBL_EPSILON
26570af302Sopenharmony_ci#endif
27570af302Sopenharmony_ci
28570af302Sopenharmony_ci/*
29570af302Sopenharmony_ci * invpio2:  53 bits of 2/pi
30570af302Sopenharmony_ci * pio2_1:   first  33 bit of pi/2
31570af302Sopenharmony_ci * pio2_1t:  pi/2 - pio2_1
32570af302Sopenharmony_ci * pio2_2:   second 33 bit of pi/2
33570af302Sopenharmony_ci * pio2_2t:  pi/2 - (pio2_1+pio2_2)
34570af302Sopenharmony_ci * pio2_3:   third  33 bit of pi/2
35570af302Sopenharmony_ci * pio2_3t:  pi/2 - (pio2_1+pio2_2+pio2_3)
36570af302Sopenharmony_ci */
37570af302Sopenharmony_cistatic const double
38570af302Sopenharmony_citoint   = 1.5/EPS,
39570af302Sopenharmony_cipio4    = 0x1.921fb54442d18p-1,
40570af302Sopenharmony_ciinvpio2 = 6.36619772367581382433e-01, /* 0x3FE45F30, 0x6DC9C883 */
41570af302Sopenharmony_cipio2_1  = 1.57079632673412561417e+00, /* 0x3FF921FB, 0x54400000 */
42570af302Sopenharmony_cipio2_1t = 6.07710050650619224932e-11, /* 0x3DD0B461, 0x1A626331 */
43570af302Sopenharmony_cipio2_2  = 6.07710050630396597660e-11, /* 0x3DD0B461, 0x1A600000 */
44570af302Sopenharmony_cipio2_2t = 2.02226624879595063154e-21, /* 0x3BA3198A, 0x2E037073 */
45570af302Sopenharmony_cipio2_3  = 2.02226624871116645580e-21, /* 0x3BA3198A, 0x2E000000 */
46570af302Sopenharmony_cipio2_3t = 8.47842766036889956997e-32; /* 0x397B839A, 0x252049C1 */
47570af302Sopenharmony_ci
48570af302Sopenharmony_ci/* caller must handle the case when reduction is not needed: |x| ~<= pi/4 */
49570af302Sopenharmony_ciint __rem_pio2(double x, double *y)
50570af302Sopenharmony_ci{
51570af302Sopenharmony_ci	union {double f; uint64_t i;} u = {x};
52570af302Sopenharmony_ci	double_t z,w,t,r,fn;
53570af302Sopenharmony_ci	double tx[3],ty[2];
54570af302Sopenharmony_ci	uint32_t ix;
55570af302Sopenharmony_ci	int sign, n, ex, ey, i;
56570af302Sopenharmony_ci
57570af302Sopenharmony_ci	sign = u.i>>63;
58570af302Sopenharmony_ci	ix = u.i>>32 & 0x7fffffff;
59570af302Sopenharmony_ci	if (ix <= 0x400f6a7a) {  /* |x| ~<= 5pi/4 */
60570af302Sopenharmony_ci		if ((ix & 0xfffff) == 0x921fb)  /* |x| ~= pi/2 or 2pi/2 */
61570af302Sopenharmony_ci			goto medium;  /* cancellation -- use medium case */
62570af302Sopenharmony_ci		if (ix <= 0x4002d97c) {  /* |x| ~<= 3pi/4 */
63570af302Sopenharmony_ci			if (!sign) {
64570af302Sopenharmony_ci				z = x - pio2_1;  /* one round good to 85 bits */
65570af302Sopenharmony_ci				y[0] = z - pio2_1t;
66570af302Sopenharmony_ci				y[1] = (z-y[0]) - pio2_1t;
67570af302Sopenharmony_ci				return 1;
68570af302Sopenharmony_ci			} else {
69570af302Sopenharmony_ci				z = x + pio2_1;
70570af302Sopenharmony_ci				y[0] = z + pio2_1t;
71570af302Sopenharmony_ci				y[1] = (z-y[0]) + pio2_1t;
72570af302Sopenharmony_ci				return -1;
73570af302Sopenharmony_ci			}
74570af302Sopenharmony_ci		} else {
75570af302Sopenharmony_ci			if (!sign) {
76570af302Sopenharmony_ci				z = x - 2*pio2_1;
77570af302Sopenharmony_ci				y[0] = z - 2*pio2_1t;
78570af302Sopenharmony_ci				y[1] = (z-y[0]) - 2*pio2_1t;
79570af302Sopenharmony_ci				return 2;
80570af302Sopenharmony_ci			} else {
81570af302Sopenharmony_ci				z = x + 2*pio2_1;
82570af302Sopenharmony_ci				y[0] = z + 2*pio2_1t;
83570af302Sopenharmony_ci				y[1] = (z-y[0]) + 2*pio2_1t;
84570af302Sopenharmony_ci				return -2;
85570af302Sopenharmony_ci			}
86570af302Sopenharmony_ci		}
87570af302Sopenharmony_ci	}
88570af302Sopenharmony_ci	if (ix <= 0x401c463b) {  /* |x| ~<= 9pi/4 */
89570af302Sopenharmony_ci		if (ix <= 0x4015fdbc) {  /* |x| ~<= 7pi/4 */
90570af302Sopenharmony_ci			if (ix == 0x4012d97c)  /* |x| ~= 3pi/2 */
91570af302Sopenharmony_ci				goto medium;
92570af302Sopenharmony_ci			if (!sign) {
93570af302Sopenharmony_ci				z = x - 3*pio2_1;
94570af302Sopenharmony_ci				y[0] = z - 3*pio2_1t;
95570af302Sopenharmony_ci				y[1] = (z-y[0]) - 3*pio2_1t;
96570af302Sopenharmony_ci				return 3;
97570af302Sopenharmony_ci			} else {
98570af302Sopenharmony_ci				z = x + 3*pio2_1;
99570af302Sopenharmony_ci				y[0] = z + 3*pio2_1t;
100570af302Sopenharmony_ci				y[1] = (z-y[0]) + 3*pio2_1t;
101570af302Sopenharmony_ci				return -3;
102570af302Sopenharmony_ci			}
103570af302Sopenharmony_ci		} else {
104570af302Sopenharmony_ci			if (ix == 0x401921fb)  /* |x| ~= 4pi/2 */
105570af302Sopenharmony_ci				goto medium;
106570af302Sopenharmony_ci			if (!sign) {
107570af302Sopenharmony_ci				z = x - 4*pio2_1;
108570af302Sopenharmony_ci				y[0] = z - 4*pio2_1t;
109570af302Sopenharmony_ci				y[1] = (z-y[0]) - 4*pio2_1t;
110570af302Sopenharmony_ci				return 4;
111570af302Sopenharmony_ci			} else {
112570af302Sopenharmony_ci				z = x + 4*pio2_1;
113570af302Sopenharmony_ci				y[0] = z + 4*pio2_1t;
114570af302Sopenharmony_ci				y[1] = (z-y[0]) + 4*pio2_1t;
115570af302Sopenharmony_ci				return -4;
116570af302Sopenharmony_ci			}
117570af302Sopenharmony_ci		}
118570af302Sopenharmony_ci	}
119570af302Sopenharmony_ci	if (ix < 0x413921fb) {  /* |x| ~< 2^20*(pi/2), medium size */
120570af302Sopenharmony_cimedium:
121570af302Sopenharmony_ci		/* rint(x/(pi/2)) */
122570af302Sopenharmony_ci		fn = (double_t)x*invpio2 + toint - toint;
123570af302Sopenharmony_ci		n = (int32_t)fn;
124570af302Sopenharmony_ci		r = x - fn*pio2_1;
125570af302Sopenharmony_ci		w = fn*pio2_1t;  /* 1st round, good to 85 bits */
126570af302Sopenharmony_ci		/* Matters with directed rounding. */
127570af302Sopenharmony_ci		if (predict_false(r - w < -pio4)) {
128570af302Sopenharmony_ci			n--;
129570af302Sopenharmony_ci			fn--;
130570af302Sopenharmony_ci			r = x - fn*pio2_1;
131570af302Sopenharmony_ci			w = fn*pio2_1t;
132570af302Sopenharmony_ci		} else if (predict_false(r - w > pio4)) {
133570af302Sopenharmony_ci			n++;
134570af302Sopenharmony_ci			fn++;
135570af302Sopenharmony_ci			r = x - fn*pio2_1;
136570af302Sopenharmony_ci			w = fn*pio2_1t;
137570af302Sopenharmony_ci		}
138570af302Sopenharmony_ci		y[0] = r - w;
139570af302Sopenharmony_ci		u.f = y[0];
140570af302Sopenharmony_ci		ey = u.i>>52 & 0x7ff;
141570af302Sopenharmony_ci		ex = ix>>20;
142570af302Sopenharmony_ci		if (ex - ey > 16) { /* 2nd round, good to 118 bits */
143570af302Sopenharmony_ci			t = r;
144570af302Sopenharmony_ci			w = fn*pio2_2;
145570af302Sopenharmony_ci			r = t - w;
146570af302Sopenharmony_ci			w = fn*pio2_2t - ((t-r)-w);
147570af302Sopenharmony_ci			y[0] = r - w;
148570af302Sopenharmony_ci			u.f = y[0];
149570af302Sopenharmony_ci			ey = u.i>>52 & 0x7ff;
150570af302Sopenharmony_ci			if (ex - ey > 49) {  /* 3rd round, good to 151 bits, covers all cases */
151570af302Sopenharmony_ci				t = r;
152570af302Sopenharmony_ci				w = fn*pio2_3;
153570af302Sopenharmony_ci				r = t - w;
154570af302Sopenharmony_ci				w = fn*pio2_3t - ((t-r)-w);
155570af302Sopenharmony_ci				y[0] = r - w;
156570af302Sopenharmony_ci			}
157570af302Sopenharmony_ci		}
158570af302Sopenharmony_ci		y[1] = (r - y[0]) - w;
159570af302Sopenharmony_ci		return n;
160570af302Sopenharmony_ci	}
161570af302Sopenharmony_ci	/*
162570af302Sopenharmony_ci	 * all other (large) arguments
163570af302Sopenharmony_ci	 */
164570af302Sopenharmony_ci	if (ix >= 0x7ff00000) {  /* x is inf or NaN */
165570af302Sopenharmony_ci		y[0] = y[1] = x - x;
166570af302Sopenharmony_ci		return 0;
167570af302Sopenharmony_ci	}
168570af302Sopenharmony_ci	/* set z = scalbn(|x|,-ilogb(x)+23) */
169570af302Sopenharmony_ci	u.f = x;
170570af302Sopenharmony_ci	u.i &= (uint64_t)-1>>12;
171570af302Sopenharmony_ci	u.i |= (uint64_t)(0x3ff + 23)<<52;
172570af302Sopenharmony_ci	z = u.f;
173570af302Sopenharmony_ci	for (i=0; i < 2; i++) {
174570af302Sopenharmony_ci		tx[i] = (double)(int32_t)z;
175570af302Sopenharmony_ci		z     = (z-tx[i])*0x1p24;
176570af302Sopenharmony_ci	}
177570af302Sopenharmony_ci	tx[i] = z;
178570af302Sopenharmony_ci	/* skip zero terms, first term is non-zero */
179570af302Sopenharmony_ci	while (tx[i] == 0.0)
180570af302Sopenharmony_ci		i--;
181570af302Sopenharmony_ci	n = __rem_pio2_large(tx,ty,(int)(ix>>20)-(0x3ff+23),i+1,1);
182570af302Sopenharmony_ci	if (sign) {
183570af302Sopenharmony_ci		y[0] = -ty[0];
184570af302Sopenharmony_ci		y[1] = -ty[1];
185570af302Sopenharmony_ci		return -n;
186570af302Sopenharmony_ci	}
187570af302Sopenharmony_ci	y[0] = ty[0];
188570af302Sopenharmony_ci	y[1] = ty[1];
189570af302Sopenharmony_ci	return n;
190570af302Sopenharmony_ci}
191