1 | /* |
2 | * IBM Accurate Mathematical Library |
3 | * written by International Business Machines Corp. |
4 | * Copyright (C) 2001-2019 Free Software Foundation, Inc. |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU Lesser General Public License as published by |
8 | * the Free Software Foundation; either version 2.1 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public License |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | /****************************************************************************/ |
20 | /* */ |
21 | /* MODULE_NAME:usncs.c */ |
22 | /* */ |
23 | /* FUNCTIONS: usin */ |
24 | /* ucos */ |
25 | /* FILES NEEDED: dla.h endian.h mpa.h mydefs.h usncs.h */ |
26 | /* branred.c sincos.tbl */ |
27 | /* */ |
28 | /* An ultimate sin and cos routine. Given an IEEE double machine number x */ |
29 | /* it computes sin(x) or cos(x) with ~0.55 ULP. */ |
30 | /* Assumption: Machine arithmetic operations are performed in */ |
31 | /* round to nearest mode of IEEE 754 standard. */ |
32 | /* */ |
33 | /****************************************************************************/ |
34 | |
35 | |
36 | #include <errno.h> |
37 | #include <float.h> |
38 | #include "endian.h" |
39 | #include "mydefs.h" |
40 | #include "usncs.h" |
41 | #include "MathLib.h" |
42 | #include <math.h> |
43 | #include <math_private.h> |
44 | #include <fenv_private.h> |
45 | #include <math-underflow.h> |
46 | #include <libm-alias-double.h> |
47 | #include <fenv.h> |
48 | |
49 | /* Helper macros to compute sin of the input values. */ |
50 | #define POLYNOMIAL2(xx) ((((s5 * (xx) + s4) * (xx) + s3) * (xx) + s2) * (xx)) |
51 | |
52 | #define POLYNOMIAL(xx) (POLYNOMIAL2 (xx) + s1) |
53 | |
54 | /* The computed polynomial is a variation of the Taylor series expansion for |
55 | sin(a): |
56 | |
57 | a - a^3/3! + a^5/5! - a^7/7! + a^9/9! + (1 - a^2) * da / 2 |
58 | |
59 | The constants s1, s2, s3, etc. are pre-computed values of 1/3!, 1/5! and so |
60 | on. The result is returned to LHS. */ |
61 | #define TAYLOR_SIN(xx, a, da) \ |
62 | ({ \ |
63 | double t = ((POLYNOMIAL (xx) * (a) - 0.5 * (da)) * (xx) + (da)); \ |
64 | double res = (a) + t; \ |
65 | res; \ |
66 | }) |
67 | |
68 | #define SINCOS_TABLE_LOOKUP(u, sn, ssn, cs, ccs) \ |
69 | ({ \ |
70 | int4 k = u.i[LOW_HALF] << 2; \ |
71 | sn = __sincostab.x[k]; \ |
72 | ssn = __sincostab.x[k + 1]; \ |
73 | cs = __sincostab.x[k + 2]; \ |
74 | ccs = __sincostab.x[k + 3]; \ |
75 | }) |
76 | |
77 | #ifndef SECTION |
78 | # define SECTION |
79 | #endif |
80 | |
81 | extern const union |
82 | { |
83 | int4 i[880]; |
84 | double x[440]; |
85 | } __sincostab attribute_hidden; |
86 | |
87 | static const double |
88 | sn3 = -1.66666666666664880952546298448555E-01, |
89 | sn5 = 8.33333214285722277379541354343671E-03, |
90 | cs2 = 4.99999999999999999999950396842453E-01, |
91 | cs4 = -4.16666666666664434524222570944589E-02, |
92 | cs6 = 1.38888874007937613028114285595617E-03; |
93 | |
94 | int __branred (double x, double *a, double *aa); |
95 | |
96 | /* Given a number partitioned into X and DX, this function computes the cosine |
97 | of the number by combining the sin and cos of X (as computed by a variation |
98 | of the Taylor series) with the values looked up from the sin/cos table to |
99 | get the result. */ |
100 | static inline double |
101 | __always_inline |
102 | do_cos (double x, double dx) |
103 | { |
104 | mynumber u; |
105 | |
106 | if (x < 0) |
107 | dx = -dx; |
108 | |
109 | u.x = big + fabs (x); |
110 | x = fabs (x) - (u.x - big) + dx; |
111 | |
112 | double xx, s, sn, ssn, c, cs, ccs, cor; |
113 | xx = x * x; |
114 | s = x + x * xx * (sn3 + xx * sn5); |
115 | c = xx * (cs2 + xx * (cs4 + xx * cs6)); |
116 | SINCOS_TABLE_LOOKUP (u, sn, ssn, cs, ccs); |
117 | cor = (ccs - s * ssn - cs * c) - sn * s; |
118 | return cs + cor; |
119 | } |
120 | |
121 | /* Given a number partitioned into X and DX, this function computes the sine of |
122 | the number by combining the sin and cos of X (as computed by a variation of |
123 | the Taylor series) with the values looked up from the sin/cos table to get |
124 | the result. */ |
125 | static inline double |
126 | __always_inline |
127 | do_sin (double x, double dx) |
128 | { |
129 | double xold = x; |
130 | /* Max ULP is 0.501 if |x| < 0.126, otherwise ULP is 0.518. */ |
131 | if (fabs (x) < 0.126) |
132 | return TAYLOR_SIN (x * x, x, dx); |
133 | |
134 | mynumber u; |
135 | |
136 | if (x <= 0) |
137 | dx = -dx; |
138 | u.x = big + fabs (x); |
139 | x = fabs (x) - (u.x - big); |
140 | |
141 | double xx, s, sn, ssn, c, cs, ccs, cor; |
142 | xx = x * x; |
143 | s = x + (dx + x * xx * (sn3 + xx * sn5)); |
144 | c = x * dx + xx * (cs2 + xx * (cs4 + xx * cs6)); |
145 | SINCOS_TABLE_LOOKUP (u, sn, ssn, cs, ccs); |
146 | cor = (ssn + s * ccs - sn * c) + cs * s; |
147 | return copysign (sn + cor, xold); |
148 | } |
149 | |
150 | /* Reduce range of x to within PI/2 with abs (x) < 105414350. The high part |
151 | is written to *a, the low part to *da. Range reduction is accurate to 136 |
152 | bits so that when x is large and *a very close to zero, all 53 bits of *a |
153 | are correct. */ |
154 | static inline int4 |
155 | __always_inline |
156 | reduce_sincos (double x, double *a, double *da) |
157 | { |
158 | mynumber v; |
159 | |
160 | double t = (x * hpinv + toint); |
161 | double xn = t - toint; |
162 | v.x = t; |
163 | double y = (x - xn * mp1) - xn * mp2; |
164 | int4 n = v.i[LOW_HALF] & 3; |
165 | |
166 | double b, db, t1, t2; |
167 | t1 = xn * pp3; |
168 | t2 = y - t1; |
169 | db = (y - t2) - t1; |
170 | |
171 | t1 = xn * pp4; |
172 | b = t2 - t1; |
173 | db += (t2 - b) - t1; |
174 | |
175 | *a = b; |
176 | *da = db; |
177 | return n; |
178 | } |
179 | |
180 | /* Compute sin or cos (A + DA) for the given quadrant N. */ |
181 | static double |
182 | __always_inline |
183 | do_sincos (double a, double da, int4 n) |
184 | { |
185 | double retval; |
186 | |
187 | if (n & 1) |
188 | /* Max ULP is 0.513. */ |
189 | retval = do_cos (a, da); |
190 | else |
191 | /* Max ULP is 0.501 if xx < 0.01588, otherwise ULP is 0.518. */ |
192 | retval = do_sin (a, da); |
193 | |
194 | return (n & 2) ? -retval : retval; |
195 | } |
196 | |
197 | |
198 | /*******************************************************************/ |
199 | /* An ultimate sin routine. Given an IEEE double machine number x */ |
200 | /* it computes the correctly rounded (to nearest) value of sin(x) */ |
201 | /*******************************************************************/ |
202 | #ifndef IN_SINCOS |
203 | double |
204 | SECTION |
205 | __sin (double x) |
206 | { |
207 | double t, a, da; |
208 | mynumber u; |
209 | int4 k, m, n; |
210 | double retval = 0; |
211 | |
212 | SET_RESTORE_ROUND_53BIT (FE_TONEAREST); |
213 | |
214 | u.x = x; |
215 | m = u.i[HIGH_HALF]; |
216 | k = 0x7fffffff & m; /* no sign */ |
217 | if (k < 0x3e500000) /* if x->0 =>sin(x)=x */ |
218 | { |
219 | math_check_force_underflow (x); |
220 | retval = x; |
221 | } |
222 | /*--------------------------- 2^-26<|x|< 0.855469---------------------- */ |
223 | else if (k < 0x3feb6000) |
224 | { |
225 | /* Max ULP is 0.548. */ |
226 | retval = do_sin (x, 0); |
227 | } /* else if (k < 0x3feb6000) */ |
228 | |
229 | /*----------------------- 0.855469 <|x|<2.426265 ----------------------*/ |
230 | else if (k < 0x400368fd) |
231 | { |
232 | t = hp0 - fabs (x); |
233 | /* Max ULP is 0.51. */ |
234 | retval = copysign (do_cos (t, hp1), x); |
235 | } /* else if (k < 0x400368fd) */ |
236 | |
237 | /*-------------------------- 2.426265<|x|< 105414350 ----------------------*/ |
238 | else if (k < 0x419921FB) |
239 | { |
240 | n = reduce_sincos (x, &a, &da); |
241 | retval = do_sincos (a, da, n); |
242 | } /* else if (k < 0x419921FB ) */ |
243 | |
244 | /* --------------------105414350 <|x| <2^1024------------------------------*/ |
245 | else if (k < 0x7ff00000) |
246 | { |
247 | n = __branred (x, &a, &da); |
248 | retval = do_sincos (a, da, n); |
249 | } |
250 | /*--------------------- |x| > 2^1024 ----------------------------------*/ |
251 | else |
252 | { |
253 | if (k == 0x7ff00000 && u.i[LOW_HALF] == 0) |
254 | __set_errno (EDOM); |
255 | retval = x / x; |
256 | } |
257 | |
258 | return retval; |
259 | } |
260 | |
261 | |
262 | /*******************************************************************/ |
263 | /* An ultimate cos routine. Given an IEEE double machine number x */ |
264 | /* it computes the correctly rounded (to nearest) value of cos(x) */ |
265 | /*******************************************************************/ |
266 | |
267 | double |
268 | SECTION |
269 | __cos (double x) |
270 | { |
271 | double y, a, da; |
272 | mynumber u; |
273 | int4 k, m, n; |
274 | |
275 | double retval = 0; |
276 | |
277 | SET_RESTORE_ROUND_53BIT (FE_TONEAREST); |
278 | |
279 | u.x = x; |
280 | m = u.i[HIGH_HALF]; |
281 | k = 0x7fffffff & m; |
282 | |
283 | /* |x|<2^-27 => cos(x)=1 */ |
284 | if (k < 0x3e400000) |
285 | retval = 1.0; |
286 | |
287 | else if (k < 0x3feb6000) |
288 | { /* 2^-27 < |x| < 0.855469 */ |
289 | /* Max ULP is 0.51. */ |
290 | retval = do_cos (x, 0); |
291 | } /* else if (k < 0x3feb6000) */ |
292 | |
293 | else if (k < 0x400368fd) |
294 | { /* 0.855469 <|x|<2.426265 */ ; |
295 | y = hp0 - fabs (x); |
296 | a = y + hp1; |
297 | da = (y - a) + hp1; |
298 | /* Max ULP is 0.501 if xx < 0.01588 or 0.518 otherwise. |
299 | Range reduction uses 106 bits here which is sufficient. */ |
300 | retval = do_sin (a, da); |
301 | } /* else if (k < 0x400368fd) */ |
302 | |
303 | else if (k < 0x419921FB) |
304 | { /* 2.426265<|x|< 105414350 */ |
305 | n = reduce_sincos (x, &a, &da); |
306 | retval = do_sincos (a, da, n + 1); |
307 | } /* else if (k < 0x419921FB ) */ |
308 | |
309 | /* 105414350 <|x| <2^1024 */ |
310 | else if (k < 0x7ff00000) |
311 | { |
312 | n = __branred (x, &a, &da); |
313 | retval = do_sincos (a, da, n + 1); |
314 | } |
315 | |
316 | else |
317 | { |
318 | if (k == 0x7ff00000 && u.i[LOW_HALF] == 0) |
319 | __set_errno (EDOM); |
320 | retval = x / x; /* |x| > 2^1024 */ |
321 | } |
322 | |
323 | return retval; |
324 | } |
325 | |
326 | #ifndef __cos |
327 | libm_alias_double (__cos, cos) |
328 | #endif |
329 | #ifndef __sin |
330 | libm_alias_double (__sin, sin) |
331 | #endif |
332 | |
333 | #endif |
334 | |