2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001-2021 Free Software Foundation, Inc.
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.
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.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
19 /****************************************************************************/
21 /* MODULE_NAME:usncs.c */
25 /* FILES NEEDED: dla.h endian.h mpa.h mydefs.h usncs.h */
26 /* branred.c sincos.tbl */
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. */
33 /****************************************************************************/
42 #include <math_private.h>
43 #include <fenv_private.h>
44 #include <math-underflow.h>
45 #include <libm-alias-double.h>
48 /* Helper macros to compute sin of the input values. */
49 #define POLYNOMIAL2(xx) ((((s5 * (xx) + s4) * (xx) + s3) * (xx) + s2) * (xx))
51 #define POLYNOMIAL(xx) (POLYNOMIAL2 (xx) + s1)
53 /* The computed polynomial is a variation of the Taylor series expansion for
56 a - a^3/3! + a^5/5! - a^7/7! + a^9/9! + (1 - a^2) * da / 2
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) \
62 double t = ((POLYNOMIAL (xx) * (a) - 0.5 * (da)) * (xx) + (da)); \
63 double res = (a) + t; \
67 #define SINCOS_TABLE_LOOKUP(u, sn, ssn, cs, ccs) \
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]; \
84 } __sincostab attribute_hidden
;
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;
93 int __branred (double x
, double *a
, double *aa
);
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
99 static __always_inline
double
100 do_cos (double x
, double dx
)
107 u
.x
= big
+ fabs (x
);
108 x
= fabs (x
) - (u
.x
- big
) + dx
;
110 double xx
, s
, sn
, ssn
, c
, cs
, ccs
, cor
;
112 s
= x
+ x
* xx
* (sn3
+ xx
* sn5
);
113 c
= xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
114 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
115 cor
= (ccs
- s
* ssn
- cs
* c
) - sn
* s
;
119 /* Given a number partitioned into X and DX, this function computes the sine of
120 the number by combining the sin and cos of X (as computed by a variation of
121 the Taylor series) with the values looked up from the sin/cos table to get
123 static __always_inline
double
124 do_sin (double x
, double dx
)
127 /* Max ULP is 0.501 if |x| < 0.126, otherwise ULP is 0.518. */
128 if (fabs (x
) < 0.126)
129 return TAYLOR_SIN (x
* x
, x
, dx
);
135 u
.x
= big
+ fabs (x
);
136 x
= fabs (x
) - (u
.x
- big
);
138 double xx
, s
, sn
, ssn
, c
, cs
, ccs
, cor
;
140 s
= x
+ (dx
+ x
* xx
* (sn3
+ xx
* sn5
));
141 c
= x
* dx
+ xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
142 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
143 cor
= (ssn
+ s
* ccs
- sn
* c
) + cs
* s
;
144 return copysign (sn
+ cor
, xold
);
147 /* Reduce range of x to within PI/2 with abs (x) < 105414350. The high part
148 is written to *a, the low part to *da. Range reduction is accurate to 136
149 bits so that when x is large and *a very close to zero, all 53 bits of *a
151 static __always_inline int4
152 reduce_sincos (double x
, double *a
, double *da
)
156 double t
= (x
* hpinv
+ toint
);
157 double xn
= t
- toint
;
159 double y
= (x
- xn
* mp1
) - xn
* mp2
;
160 int4 n
= v
.i
[LOW_HALF
] & 3;
162 double b
, db
, t1
, t2
;
176 /* Compute sin or cos (A + DA) for the given quadrant N. */
177 static __always_inline
double
178 do_sincos (double a
, double da
, int4 n
)
183 /* Max ULP is 0.513. */
184 retval
= do_cos (a
, da
);
186 /* Max ULP is 0.501 if xx < 0.01588, otherwise ULP is 0.518. */
187 retval
= do_sin (a
, da
);
189 return (n
& 2) ? -retval
: retval
;
193 /*******************************************************************/
194 /* An ultimate sin routine. Given an IEEE double machine number x */
195 /* it computes the rounded value of sin(x). */
196 /*******************************************************************/
207 SET_RESTORE_ROUND_53BIT (FE_TONEAREST
);
211 k
= 0x7fffffff & m
; /* no sign */
212 if (k
< 0x3e500000) /* if x->0 =>sin(x)=x */
214 math_check_force_underflow (x
);
217 /*--------------------------- 2^-26<|x|< 0.855469---------------------- */
218 else if (k
< 0x3feb6000)
220 /* Max ULP is 0.548. */
221 retval
= do_sin (x
, 0);
222 } /* else if (k < 0x3feb6000) */
224 /*----------------------- 0.855469 <|x|<2.426265 ----------------------*/
225 else if (k
< 0x400368fd)
228 /* Max ULP is 0.51. */
229 retval
= copysign (do_cos (t
, hp1
), x
);
230 } /* else if (k < 0x400368fd) */
232 /*-------------------------- 2.426265<|x|< 105414350 ----------------------*/
233 else if (k
< 0x419921FB)
235 n
= reduce_sincos (x
, &a
, &da
);
236 retval
= do_sincos (a
, da
, n
);
237 } /* else if (k < 0x419921FB ) */
239 /* --------------------105414350 <|x| <2^1024------------------------------*/
240 else if (k
< 0x7ff00000)
242 n
= __branred (x
, &a
, &da
);
243 retval
= do_sincos (a
, da
, n
);
245 /*--------------------- |x| > 2^1024 ----------------------------------*/
248 if (k
== 0x7ff00000 && u
.i
[LOW_HALF
] == 0)
257 /*******************************************************************/
258 /* An ultimate cos routine. Given an IEEE double machine number x */
259 /* it computes the rounded value of cos(x). */
260 /*******************************************************************/
272 SET_RESTORE_ROUND_53BIT (FE_TONEAREST
);
278 /* |x|<2^-27 => cos(x)=1 */
282 else if (k
< 0x3feb6000)
283 { /* 2^-27 < |x| < 0.855469 */
284 /* Max ULP is 0.51. */
285 retval
= do_cos (x
, 0);
286 } /* else if (k < 0x3feb6000) */
288 else if (k
< 0x400368fd)
289 { /* 0.855469 <|x|<2.426265 */ ;
293 /* Max ULP is 0.501 if xx < 0.01588 or 0.518 otherwise.
294 Range reduction uses 106 bits here which is sufficient. */
295 retval
= do_sin (a
, da
);
296 } /* else if (k < 0x400368fd) */
298 else if (k
< 0x419921FB)
299 { /* 2.426265<|x|< 105414350 */
300 n
= reduce_sincos (x
, &a
, &da
);
301 retval
= do_sincos (a
, da
, n
+ 1);
302 } /* else if (k < 0x419921FB ) */
304 /* 105414350 <|x| <2^1024 */
305 else if (k
< 0x7ff00000)
307 n
= __branred (x
, &a
, &da
);
308 retval
= do_sincos (a
, da
, n
+ 1);
313 if (k
== 0x7ff00000 && u
.i
[LOW_HALF
] == 0)
315 retval
= x
/ x
; /* |x| > 2^1024 */
322 libm_alias_double (__cos
, cos
)
325 libm_alias_double (__sin
, sin
)