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
81extern const union
82{
83 int4 i[880];
84 double x[440];
85} __sincostab attribute_hidden;
86
87static 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
94int __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. */
100static __always_inline double
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 __always_inline double
125do_sin (double x, double dx)
126{
127 double xold = x;
128 /* Max ULP is 0.501 if |x| < 0.126, otherwise ULP is 0.518. */
129 if (fabs (x) < 0.126)
130 return TAYLOR_SIN (x * x, x, dx);
131
132 mynumber u;
133
134 if (x <= 0)
135 dx = -dx;
136 u.x = big + fabs (x);
137 x = fabs (x) - (u.x - big);
138
139 double xx, s, sn, ssn, c, cs, ccs, cor;
140 xx = x * x;
141 s = x + (dx + x * xx * (sn3 + xx * sn5));
142 c = x * dx + xx * (cs2 + xx * (cs4 + xx * cs6));
143 SINCOS_TABLE_LOOKUP (u, sn, ssn, cs, ccs);
144 cor = (ssn + s * ccs - sn * c) + cs * s;
145 return copysign (sn + cor, xold);
146}
147
148/* Reduce range of x to within PI/2 with abs (x) < 105414350. The high part
149 is written to *a, the low part to *da. Range reduction is accurate to 136
150 bits so that when x is large and *a very close to zero, all 53 bits of *a
151 are correct. */
152static __always_inline int4
153reduce_sincos (double x, double *a, double *da)
154{
155 mynumber v;
156
157 double t = (x * hpinv + toint);
158 double xn = t - toint;
159 v.x = t;
160 double y = (x - xn * mp1) - xn * mp2;
161 int4 n = v.i[LOW_HALF] & 3;
162
163 double b, db, t1, t2;
164 t1 = xn * pp3;
165 t2 = y - t1;
166 db = (y - t2) - t1;
167
168 t1 = xn * pp4;
169 b = t2 - t1;
170 db += (t2 - b) - t1;
171
172 *a = b;
173 *da = db;
174 return n;
175}
176
177/* Compute sin or cos (A + DA) for the given quadrant N. */
178static __always_inline double
179do_sincos (double a, double da, int4 n)
180{
181 double retval;
182
183 if (n & 1)
184 /* Max ULP is 0.513. */
185 retval = do_cos (a, da);
186 else
187 /* Max ULP is 0.501 if xx < 0.01588, otherwise ULP is 0.518. */
188 retval = do_sin (a, da);
189
190 return (n & 2) ? -retval : retval;
191}
192
193
194/*******************************************************************/
195/* An ultimate sin routine. Given an IEEE double machine number x */
196/* it computes the correctly rounded (to nearest) value of sin(x) */
197/*******************************************************************/
198#ifndef IN_SINCOS
199double
200SECTION
201__sin (double x)
202{
203 double t, a, da;
204 mynumber u;
205 int4 k, m, n;
206 double retval = 0;
207
208 SET_RESTORE_ROUND_53BIT (FE_TONEAREST);
209
210 u.x = x;
211 m = u.i[HIGH_HALF];
212 k = 0x7fffffff & m; /* no sign */
213 if (k < 0x3e500000) /* if x->0 =>sin(x)=x */
214 {
215 math_check_force_underflow (x);
216 retval = x;
217 }
218/*--------------------------- 2^-26<|x|< 0.855469---------------------- */
219 else if (k < 0x3feb6000)
220 {
221 /* Max ULP is 0.548. */
222 retval = do_sin (x, 0);
223 } /* else if (k < 0x3feb6000) */
224
225/*----------------------- 0.855469 <|x|<2.426265 ----------------------*/
226 else if (k < 0x400368fd)
227 {
228 t = hp0 - fabs (x);
229 /* Max ULP is 0.51. */
230 retval = copysign (do_cos (t, hp1), x);
231 } /* else if (k < 0x400368fd) */
232
233/*-------------------------- 2.426265<|x|< 105414350 ----------------------*/
234 else if (k < 0x419921FB)
235 {
236 n = reduce_sincos (x, &a, &da);
237 retval = do_sincos (a, da, n);
238 } /* else if (k < 0x419921FB ) */
239
240/* --------------------105414350 <|x| <2^1024------------------------------*/
241 else if (k < 0x7ff00000)
242 {
243 n = __branred (x, &a, &da);
244 retval = do_sincos (a, da, n);
245 }
246/*--------------------- |x| > 2^1024 ----------------------------------*/
247 else
248 {
249 if (k == 0x7ff00000 && u.i[LOW_HALF] == 0)
250 __set_errno (EDOM);
251 retval = x / x;
252 }
253
254 return retval;
255}
256
257
258/*******************************************************************/
259/* An ultimate cos routine. Given an IEEE double machine number x */
260/* it computes the correctly rounded (to nearest) value of cos(x) */
261/*******************************************************************/
262
263double
264SECTION
265__cos (double x)
266{
267 double y, a, da;
268 mynumber u;
269 int4 k, m, n;
270
271 double retval = 0;
272
273 SET_RESTORE_ROUND_53BIT (FE_TONEAREST);
274
275 u.x = x;
276 m = u.i[HIGH_HALF];
277 k = 0x7fffffff & m;
278
279 /* |x|<2^-27 => cos(x)=1 */
280 if (k < 0x3e400000)
281 retval = 1.0;
282
283 else if (k < 0x3feb6000)
284 { /* 2^-27 < |x| < 0.855469 */
285 /* Max ULP is 0.51. */
286 retval = do_cos (x, 0);
287 } /* else if (k < 0x3feb6000) */
288
289 else if (k < 0x400368fd)
290 { /* 0.855469 <|x|<2.426265 */ ;
291 y = hp0 - fabs (x);
292 a = y + hp1;
293 da = (y - a) + hp1;
294 /* Max ULP is 0.501 if xx < 0.01588 or 0.518 otherwise.
295 Range reduction uses 106 bits here which is sufficient. */
296 retval = do_sin (a, da);
297 } /* else if (k < 0x400368fd) */
298
299 else if (k < 0x419921FB)
300 { /* 2.426265<|x|< 105414350 */
301 n = reduce_sincos (x, &a, &da);
302 retval = do_sincos (a, da, n + 1);
303 } /* else if (k < 0x419921FB ) */
304
305 /* 105414350 <|x| <2^1024 */
306 else if (k < 0x7ff00000)
307 {
308 n = __branred (x, &a, &da);
309 retval = do_sincos (a, da, n + 1);
310 }
311
312 else
313 {
314 if (k == 0x7ff00000 && u.i[LOW_HALF] == 0)
315 __set_errno (EDOM);
316 retval = x / x; /* |x| > 2^1024 */
317 }
318
319 return retval;
320}
321
322#ifndef __cos
323libm_alias_double (__cos, cos)
324#endif
325#ifndef __sin
326libm_alias_double (__sin, sin)
327#endif
328
329#endif
330