1
2 /*******************************************************************************
3 MIT License
4 -----------
5
6 Copyright (c) 2002-2019 Advanced Micro Devices, Inc.
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this Software and associated documentaon files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 *******************************************************************************/
26
27 #include "libm.h"
28 #include "libm_util.h"
29
30
31 /* Given positive argument x, reduce it to the range [-pi/4,pi/4] using
32 extra precision, and return the result in r.
33 Return value "region" tells how many lots of pi/2 were subtracted
34 from x to put it in the range [-pi/4,pi/4], mod 4. */
__remainder_piby2f(unsigned long long ux,double * r,int * region)35 void __remainder_piby2f(unsigned long long ux, double *r, int *region)
36 {
37
38
39 /* This method simulates multi-precision floating-point
40 arithmetic and is accurate for all 1 <= x < infinity */
41 #define bitsper 36
42 unsigned long long res[10];
43 unsigned long long u, carry, mask, mant, nextbits;
44 int first, last, i, rexp, xexp, resexp, ltb, determ, bc;
45 double dx;
46 static const double
47 piby2 = 1.57079632679489655800e+00; /* 0x3ff921fb54442d18 */
48 static unsigned long long pibits[] =
49 {
50 0LL,
51 5215LL, 13000023176LL, 11362338026LL, 67174558139LL,
52 34819822259LL, 10612056195LL, 67816420731LL, 57840157550LL,
53 19558516809LL, 50025467026LL, 25186875954LL, 18152700886LL
54 };
55
56
57 xexp = (int)(((ux & EXPBITS_DP64) >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64);
58 ux = ((ux & MANTBITS_DP64) | IMPBIT_DP64) >> 29;
59
60
61 /* Now ux is the mantissa bit pattern of x as a long integer */
62 mask = 1;
63 mask = (mask << bitsper) - 1;
64
65 /* Set first and last to the positions of the first
66 and last chunks of 2/pi that we need */
67 first = xexp / bitsper;
68 resexp = xexp - first * bitsper;
69 /* 120 is the theoretical maximum number of bits (actually
70 115 for IEEE single precision) that we need to extract
71 from the middle of 2/pi to compute the reduced argument
72 accurately enough for our purposes */
73 last = first + 120 / bitsper;
74
75
76 /* Do a long multiplication of the bits of 2/pi by the
77 integer mantissa */
78 #if 0
79 for (i = last; i >= first; i--)
80 {
81 u = pibits[i] * ux + carry;
82 res[i - first] = u & mask;
83 carry = u >> bitsper;
84 }
85 res[last - first + 1] = 0;
86 #else
87 /* Unroll the loop. This is only correct because we know
88 that bitsper is fixed as 36. */
89 res[4] = 0;
90 u = pibits[last] * ux;
91 res[3] = u & mask;
92 carry = u >> bitsper;
93 u = pibits[last - 1] * ux + carry;
94 res[2] = u & mask;
95 carry = u >> bitsper;
96 u = pibits[last - 2] * ux + carry;
97 res[1] = u & mask;
98 carry = u >> bitsper;
99 u = pibits[first] * ux + carry;
100 res[0] = u & mask;
101 #endif
102
103
104 /* Reconstruct the result */
105 ltb = (int)((((res[0] << bitsper) | res[1])
106 >> (bitsper - 1 - resexp)) & 7);
107
108 /* determ says whether the fractional part is >= 0.5 */
109 determ = ltb & 1;
110
111 i = 1;
112 if (determ)
113 {
114 /* The mantissa is >= 0.5. We want to subtract it
115 from 1.0 by negating all the bits */
116 *region = ((ltb >> 1) + 1) & 3;
117 mant = 1;
118 mant = ~(res[1]) & ((mant << (bitsper - resexp)) - 1);
119 while (mant < 0x0000000000010000)
120 {
121 i++;
122 mant = (mant << bitsper) | (~(res[i]) & mask);
123 }
124 nextbits = (~(res[i+1]) & mask);
125 }
126 else
127 {
128 *region = (ltb >> 1);
129 mant = 1;
130 mant = res[1] & ((mant << (bitsper - resexp)) - 1);
131 while (mant < 0x0000000000010000)
132 {
133 i++;
134 mant = (mant << bitsper) | res[i];
135 }
136 nextbits = res[i+1];
137 }
138
139
140 /* Normalize the mantissa. The shift value 6 here, determined by
141 trial and error, seems to give optimal speed. */
142 bc = 0;
143 while (mant < 0x0000400000000000)
144 {
145 bc += 6;
146 mant <<= 6;
147 }
148 while (mant < 0x0010000000000000)
149 {
150 bc++;
151 mant <<= 1;
152 }
153 mant |= nextbits >> (bitsper - bc);
154
155 rexp = 52 + resexp - bc - i * bitsper;
156
157
158 /* Put the result exponent rexp onto the mantissa pattern */
159 u = ((unsigned long long)rexp + EXPBIAS_DP64) << EXPSHIFTBITS_DP64;
160 ux = (mant & MANTBITS_DP64) | u;
161 if (determ)
162 /* If we negated the mantissa we negate x too */
163 ux |= SIGNBIT_DP64;
164 PUT_BITS_DP64(ux, dx);
165
166
167 /* x is a double precision version of the fractional part of
168 x * 2 / pi. Multiply x by pi/2 in double precision
169 to get the reduced argument r. */
170 *r = dx * piby2;
171 return;
172
173 }
174