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