2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001-2015 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 <http://www.gnu.org/licenses/>.
19 /****************************************************************************/
21 /* MODULE_NAME:usncs.c */
38 /* FILES NEEDED: dla.h endian.h mpa.h mydefs.h usncs.h */
39 /* branred.c sincos32.c dosincos.c mpa.c */
42 /* An ultimate sin and routine. Given an IEEE double machine number x */
43 /* it computes the correctly rounded (to nearest) value of sin(x) or cos(x) */
44 /* Assumption: Machine arithmetic operations are performed in */
45 /* round to nearest mode of IEEE 754 standard. */
47 /****************************************************************************/
56 #include <math_private.h>
59 /* Helper macros to compute sin of the input values. */
60 #define POLYNOMIAL2(xx) ((((s5 * (xx) + s4) * (xx) + s3) * (xx) + s2) * (xx))
62 #define POLYNOMIAL(xx) (POLYNOMIAL2 (xx) + s1)
64 /* The computed polynomial is a variation of the Taylor series expansion for
67 a - a^3/3! + a^5/5! - a^7/7! + a^9/9! + (1 - a^2) * da / 2
69 The constants s1, s2, s3, etc. are pre-computed values of 1/3!, 1/5! and so
70 on. The result is returned to LHS and correction in COR. */
71 #define TAYLOR_SIN(xx, a, da, cor) \
73 double t = ((POLYNOMIAL (xx) * (a) - 0.5 * (da)) * (xx) + (da)); \
74 double res = (a) + t; \
75 (cor) = ((a) - res) + t; \
79 /* This is again a variation of the Taylor series expansion with the term
80 x^3/3! expanded into the following for better accuracy:
82 bb * x ^ 3 + 3 * aa * x * x1 * x2 + aa * x1 ^ 3 + aa * x2 ^ 3
84 The correction term is dx and bb + aa = -1/3!
86 #define TAYLOR_SLOW(x0, dx, cor) \
88 static const double th2_36 = 206158430208.0; /* 1.5*2**37 */ \
89 double xx = (x0) * (x0); \
90 double x1 = ((x0) + th2_36) - th2_36; \
91 double y = aa * x1 * x1 * x1; \
92 double r = (x0) + y; \
93 double x2 = ((x0) - x1) + (dx); \
94 double t = (((POLYNOMIAL2 (xx) + bb) * xx + 3.0 * aa * x1 * x2) \
95 * (x0) + aa * x2 * x2 * x2 + (dx)); \
96 t = (((x0) - r) + y) + t; \
98 (cor) = (r - res) + t; \
102 #define SINCOS_TABLE_LOOKUP(u, sn, ssn, cs, ccs) \
104 int4 k = u.i[LOW_HALF] << 2; \
105 sn = __sincostab.x[k]; \
106 ssn = __sincostab.x[k + 1]; \
107 cs = __sincostab.x[k + 2]; \
108 ccs = __sincostab.x[k + 3]; \
119 } __sincostab attribute_hidden
;
122 sn3
= -1.66666666666664880952546298448555E-01,
123 sn5
= 8.33333214285722277379541354343671E-03,
124 cs2
= 4.99999999999999999999950396842453E-01,
125 cs4
= -4.16666666666664434524222570944589E-02,
126 cs6
= 1.38888874007937613028114285595617E-03;
128 static const double t22
= 0x1.8p22
;
130 void __dubsin (double x
, double dx
, double w
[]);
131 void __docos (double x
, double dx
, double w
[]);
132 double __mpsin (double x
, double dx
, bool reduce_range
);
133 double __mpcos (double x
, double dx
, bool reduce_range
);
134 static double slow (double x
);
135 static double slow1 (double x
);
136 static double slow2 (double x
);
137 static double sloww (double x
, double dx
, double orig
);
138 static double sloww1 (double x
, double dx
, double orig
, int m
);
139 static double sloww2 (double x
, double dx
, double orig
, int n
);
140 static double bsloww (double x
, double dx
, double orig
, int n
);
141 static double bsloww1 (double x
, double dx
, double orig
, int n
);
142 static double bsloww2 (double x
, double dx
, double orig
, int n
);
143 int __branred (double x
, double *a
, double *aa
);
144 static double cslow2 (double x
);
145 static double csloww (double x
, double dx
, double orig
);
146 static double csloww1 (double x
, double dx
, double orig
, int m
);
147 static double csloww2 (double x
, double dx
, double orig
, int n
);
149 /* Given a number partitioned into U and X such that U is an index into the
150 sin/cos table, this macro computes the cosine of the number by combining
151 the sin and cos of X (as computed by a variation of the Taylor series) with
152 the values looked up from the sin/cos table to get the result in RES and a
153 correction value in COR. */
155 do_cos (mynumber u
, double x
, double *corp
)
157 double xx
, s
, sn
, ssn
, c
, cs
, ccs
, res
, cor
;
159 s
= x
+ x
* xx
* (sn3
+ xx
* sn5
);
160 c
= xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
161 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
162 cor
= (ccs
- s
* ssn
- cs
* c
) - sn
* s
;
164 cor
= (cs
- res
) + cor
;
169 /* A more precise variant of DO_COS where the number is partitioned into U, X
170 and DX. EPS is the adjustment to the correction COR. */
172 do_cos_slow (mynumber u
, double x
, double dx
, double eps
, double *corp
)
174 double xx
, y
, x1
, x2
, e1
, e2
, res
, cor
;
175 double s
, sn
, ssn
, c
, cs
, ccs
;
177 s
= x
* xx
* (sn3
+ xx
* sn5
);
178 c
= x
* dx
+ xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
179 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
180 x1
= (x
+ t22
) - t22
;
182 e1
= (sn
+ t22
) - t22
;
183 e2
= (sn
- e1
) + ssn
;
184 cor
= (ccs
- cs
* c
- e1
* x2
- e2
* x
) - sn
* s
;
186 cor
= cor
+ ((cs
- y
) - e1
* x1
);
188 cor
= (y
- res
) + cor
;
190 cor
= 1.0005 * cor
+ eps
;
192 cor
= 1.0005 * cor
- eps
;
197 /* Given a number partitioned into U and X and DX such that U is an index into
198 the sin/cos table, this macro computes the sine of the number by combining
199 the sin and cos of X (as computed by a variation of the Taylor series) with
200 the values looked up from the sin/cos table to get the result in RES and a
201 correction value in COR. */
203 do_sin (mynumber u
, double x
, double dx
, double *corp
)
205 double xx
, s
, sn
, ssn
, c
, cs
, ccs
, cor
, res
;
207 s
= x
+ (dx
+ x
* xx
* (sn3
+ xx
* sn5
));
208 c
= x
* dx
+ xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
209 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
210 cor
= (ssn
+ s
* ccs
- sn
* c
) + cs
* s
;
212 cor
= (sn
- res
) + cor
;
217 /* A more precise variant of res = do_sin where the number is partitioned into U, X
218 and DX. EPS is the adjustment to the correction COR. */
220 do_sin_slow (mynumber u
, double x
, double dx
, double eps
, double *corp
)
222 double xx
, y
, x1
, x2
, c1
, c2
, res
, cor
;
223 double s
, sn
, ssn
, c
, cs
, ccs
;
225 s
= x
* xx
* (sn3
+ xx
* sn5
);
226 c
= xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
227 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
228 x1
= (x
+ t22
) - t22
;
230 c1
= (cs
+ t22
) - t22
;
231 c2
= (cs
- c1
) + ccs
;
232 cor
= (ssn
+ s
* ccs
+ cs
* s
+ c2
* x
+ c1
* x2
- sn
* x
* dx
) - sn
* c
;
234 cor
= cor
+ ((sn
- y
) + c1
* x1
);
236 cor
= (y
- res
) + cor
;
238 cor
= 1.0005 * cor
+ eps
;
240 cor
= 1.0005 * cor
- eps
;
245 /* Reduce range of X and compute sin of a + da. K is the amount by which to
246 rotate the quadrants. This allows us to use the same routine to compute cos
247 by simply rotating the quadrants by 1. */
250 reduce_and_compute (double x
, unsigned int k
)
252 double retval
= 0, a
, da
;
253 unsigned int n
= __branred (x
, &a
, &da
);
259 retval
= bsloww (a
, da
, x
, n
);
261 retval
= bsloww1 (a
, da
, x
, n
);
265 retval
= bsloww (-a
, -da
, x
, n
);
267 retval
= bsloww1 (-a
, -da
, x
, n
);
272 retval
= bsloww2 (a
, da
, x
, n
);
278 /*******************************************************************/
279 /* An ultimate sin routine. Given an IEEE double machine number x */
280 /* it computes the correctly rounded (to nearest) value of sin(x) */
281 /*******************************************************************/
286 double xx
, res
, t
, cor
, y
, s
, c
, sn
, ssn
, cs
, ccs
, xn
, a
, da
, db
, eps
, xn1
,
292 SET_RESTORE_ROUND_53BIT (FE_TONEAREST
);
296 k
= 0x7fffffff & m
; /* no sign */
297 if (k
< 0x3e500000) /* if x->0 =>sin(x)=x */
299 /*---------------------------- 2^-26 < |x|< 0.25 ----------------------*/
300 else if (k
< 0x3fd00000)
304 t
= POLYNOMIAL (xx
) * (xx
* x
);
307 retval
= (res
== res
+ 1.07 * cor
) ? res
: slow (x
);
308 } /* else if (k < 0x3fd00000) */
309 /*---------------------------- 0.25<|x|< 0.855469---------------------- */
310 else if (k
< 0x3feb6000)
312 u
.x
= (m
> 0) ? big
+ x
: big
- x
;
313 y
= (m
> 0) ? x
- (u
.x
- big
) : x
+ (u
.x
- big
);
315 s
= y
+ y
* xx
* (sn3
+ xx
* sn5
);
316 c
= xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
317 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
323 cor
= (ssn
+ s
* ccs
- sn
* c
) + cs
* s
;
325 cor
= (sn
- res
) + cor
;
326 retval
= (res
== res
+ 1.096 * cor
) ? res
: slow1 (x
);
327 } /* else if (k < 0x3feb6000) */
329 /*----------------------- 0.855469 <|x|<2.426265 ----------------------*/
330 else if (k
< 0x400368fd)
333 y
= (m
> 0) ? hp0
- x
: hp0
+ x
;
337 y
= (y
- (u
.x
- big
)) + hp1
;
342 y
= (-hp1
) - (y
+ (u
.x
- big
));
344 res
= do_cos (u
, y
, &cor
);
345 retval
= (res
== res
+ 1.020 * cor
) ? ((m
> 0) ? res
: -res
) : slow2 (x
);
346 } /* else if (k < 0x400368fd) */
348 /*-------------------------- 2.426265<|x|< 105414350 ----------------------*/
349 else if (k
< 0x419921FB)
351 t
= (x
* hpinv
+ toint
);
354 y
= (x
- xn
* mp1
) - xn
* mp2
;
355 n
= v
.i
[LOW_HALF
] & 3;
359 eps
= fabs (x
) * 1.2e-30;
362 { /* quarter of unit circle */
374 res
= TAYLOR_SIN (xx
, a
, da
, cor
);
375 cor
= (cor
> 0) ? 1.02 * cor
+ eps
: 1.02 * cor
- eps
;
376 retval
= (res
== res
+ cor
) ? res
: sloww (a
, da
, x
);
390 res
= do_sin (u
, y
, da
, &cor
);
391 cor
= (cor
> 0) ? 1.035 * cor
+ eps
: 1.035 * cor
- eps
;
392 retval
= ((res
== res
+ cor
) ? ((m
) ? res
: -res
)
393 : sloww1 (a
, da
, x
, m
));
405 y
= a
- (u
.x
- big
) + da
;
406 res
= do_cos (u
, y
, &cor
);
407 cor
= (cor
> 0) ? 1.025 * cor
+ eps
: 1.025 * cor
- eps
;
408 retval
= ((res
== res
+ cor
) ? ((n
& 2) ? -res
: res
)
409 : sloww2 (a
, da
, x
, n
));
412 } /* else if (k < 0x419921FB ) */
414 /*---------------------105414350 <|x|< 281474976710656 --------------------*/
415 else if (k
< 0x42F00000)
417 t
= (x
* hpinv
+ toint
);
420 xn1
= (xn
+ 8.0e22
) - 8.0e22
;
422 y
= ((((x
- xn1
* mp1
) - xn1
* mp2
) - xn2
* mp1
) - xn2
* mp2
);
423 n
= v
.i
[LOW_HALF
] & 3;
427 da
= (da
- xn2
* pp3
) - xn
* pp4
;
445 res
= TAYLOR_SIN (xx
, a
, da
, cor
);
446 cor
= (cor
> 0) ? 1.02 * cor
+ eps
: 1.02 * cor
- eps
;
447 retval
= (res
== res
+ cor
) ? res
: bsloww (a
, da
, x
, n
);
466 res
= do_sin (u
, y
, db
, &cor
);
467 cor
= (cor
> 0) ? 1.035 * cor
+ eps
: 1.035 * cor
- eps
;
468 retval
= ((res
== res
+ cor
) ? ((m
) ? res
: -res
)
469 : bsloww1 (a
, da
, x
, n
));
481 y
= a
- (u
.x
- big
) + da
;
482 res
= do_cos (u
, y
, &cor
);
483 cor
= (cor
> 0) ? 1.025 * cor
+ eps
: 1.025 * cor
- eps
;
484 retval
= ((res
== res
+ cor
) ? ((n
& 2) ? -res
: res
)
485 : bsloww2 (a
, da
, x
, n
));
488 } /* else if (k < 0x42F00000 ) */
490 /* -----------------281474976710656 <|x| <2^1024----------------------------*/
491 else if (k
< 0x7ff00000)
492 retval
= reduce_and_compute (x
, 0);
494 /*--------------------- |x| > 2^1024 ----------------------------------*/
497 if (k
== 0x7ff00000 && u
.i
[LOW_HALF
] == 0)
506 /*******************************************************************/
507 /* An ultimate cos routine. Given an IEEE double machine number x */
508 /* it computes the correctly rounded (to nearest) value of cos(x) */
509 /*******************************************************************/
515 double y
, xx
, res
, t
, cor
, xn
, a
, da
, db
, eps
, xn1
,
522 SET_RESTORE_ROUND_53BIT (FE_TONEAREST
);
528 /* |x|<2^-27 => cos(x)=1 */
532 else if (k
< 0x3feb6000)
533 { /* 2^-27 < |x| < 0.855469 */
537 res
= do_cos (u
, y
, &cor
);
538 retval
= (res
== res
+ 1.020 * cor
) ? res
: cslow2 (x
);
539 } /* else if (k < 0x3feb6000) */
541 else if (k
< 0x400368fd)
542 { /* 0.855469 <|x|<2.426265 */ ;
549 res
= TAYLOR_SIN (xx
, a
, da
, cor
);
550 cor
= (cor
> 0) ? 1.02 * cor
+ 1.0e-31 : 1.02 * cor
- 1.0e-31;
551 retval
= (res
== res
+ cor
) ? res
: csloww (a
, da
, x
);
567 res
= do_sin (u
, y
, da
, &cor
);
568 cor
= (cor
> 0) ? 1.035 * cor
+ 1.0e-31 : 1.035 * cor
- 1.0e-31;
569 retval
= ((res
== res
+ cor
) ? ((m
) ? res
: -res
)
570 : csloww1 (a
, da
, x
, m
));
573 } /* else if (k < 0x400368fd) */
576 else if (k
< 0x419921FB)
577 { /* 2.426265<|x|< 105414350 */
578 t
= (x
* hpinv
+ toint
);
581 y
= (x
- xn
* mp1
) - xn
* mp2
;
582 n
= v
.i
[LOW_HALF
] & 3;
586 eps
= fabs (x
) * 1.2e-30;
600 res
= TAYLOR_SIN (xx
, a
, da
, cor
);
601 cor
= (cor
> 0) ? 1.02 * cor
+ eps
: 1.02 * cor
- eps
;
602 retval
= (res
== res
+ cor
) ? res
: csloww (a
, da
, x
);
618 res
= do_sin (u
, y
, da
, &cor
);
619 cor
= (cor
> 0) ? 1.035 * cor
+ eps
: 1.035 * cor
- eps
;
620 retval
= ((res
== res
+ cor
) ? ((m
) ? res
: -res
)
621 : csloww1 (a
, da
, x
, m
));
633 y
= a
- (u
.x
- big
) + da
;
634 res
= do_cos (u
, y
, &cor
);
635 cor
= (cor
> 0) ? 1.025 * cor
+ eps
: 1.025 * cor
- eps
;
636 retval
= ((res
== res
+ cor
) ? ((n
) ? -res
: res
)
637 : csloww2 (a
, da
, x
, n
));
640 } /* else if (k < 0x419921FB ) */
642 else if (k
< 0x42F00000)
644 t
= (x
* hpinv
+ toint
);
647 xn1
= (xn
+ 8.0e22
) - 8.0e22
;
649 y
= ((((x
- xn1
* mp1
) - xn1
* mp2
) - xn2
* mp1
) - xn2
* mp2
);
650 n
= v
.i
[LOW_HALF
] & 3;
654 da
= (da
- xn2
* pp3
) - xn
* pp4
;
671 res
= TAYLOR_SIN (xx
, a
, da
, cor
);
672 cor
= (cor
> 0) ? 1.02 * cor
+ eps
: 1.02 * cor
- eps
;
673 retval
= (res
== res
+ cor
) ? res
: bsloww (a
, da
, x
, n
);
692 res
= do_sin (u
, y
, db
, &cor
);
693 cor
= (cor
> 0) ? 1.035 * cor
+ eps
: 1.035 * cor
- eps
;
694 retval
= ((res
== res
+ cor
) ? ((m
) ? res
: -res
)
695 : bsloww1 (a
, da
, x
, n
));
707 y
= a
- (u
.x
- big
) + da
;
708 res
= do_cos (u
, y
, &cor
);
709 cor
= (cor
> 0) ? 1.025 * cor
+ eps
: 1.025 * cor
- eps
;
710 retval
= ((res
== res
+ cor
) ? ((n
) ? -res
: res
)
711 : bsloww2 (a
, da
, x
, n
));
714 } /* else if (k < 0x42F00000 ) */
716 /* 281474976710656 <|x| <2^1024 */
717 else if (k
< 0x7ff00000)
718 retval
= reduce_and_compute (x
, 1);
722 if (k
== 0x7ff00000 && u
.i
[LOW_HALF
] == 0)
724 retval
= x
/ x
; /* |x| > 2^1024 */
730 /************************************************************************/
731 /* Routine compute sin(x) for 2^-26 < |x|< 0.25 by Taylor with more */
732 /* precision and if still doesn't accurate enough by mpsin or dubsin */
733 /************************************************************************/
739 double res
, cor
, w
[2];
740 res
= TAYLOR_SLOW (x
, 0, cor
);
741 if (res
== res
+ 1.0007 * cor
)
745 __dubsin (fabs (x
), 0, w
);
746 if (w
[0] == w
[0] + 1.000000001 * w
[1])
747 return (x
> 0) ? w
[0] : -w
[0];
749 return (x
> 0) ? __mpsin (x
, 0, false) : -__mpsin (-x
, 0, false);
753 /*******************************************************************************/
754 /* Routine compute sin(x) for 0.25<|x|< 0.855469 by __sincostab.tbl and Taylor */
755 /* and if result still doesn't accurate enough by mpsin or dubsin */
756 /*******************************************************************************/
763 double w
[2], y
, cor
, res
;
767 res
= do_sin_slow (u
, y
, 0, 0, &cor
);
768 if (res
== res
+ cor
)
769 return (x
> 0) ? res
: -res
;
772 __dubsin (fabs (x
), 0, w
);
773 if (w
[0] == w
[0] + 1.000000005 * w
[1])
774 return (x
> 0) ? w
[0] : -w
[0];
776 return (x
> 0) ? __mpsin (x
, 0, false) : -__mpsin (-x
, 0, false);
780 /**************************************************************************/
781 /* Routine compute sin(x) for 0.855469 <|x|<2.426265 by __sincostab.tbl */
782 /* and if result still doesn't accurate enough by mpsin or dubsin */
783 /**************************************************************************/
789 double w
[2], y
, y1
, y2
, cor
, res
, del
;
802 y
= -(y
+ (u
.x
- big
));
805 res
= do_cos_slow (u
, y
, del
, 0, &cor
);
806 if (res
== res
+ cor
)
807 return (x
> 0) ? res
: -res
;
814 if (w
[0] == w
[0] + 1.000000005 * w
[1])
815 return (x
> 0) ? w
[0] : -w
[0];
817 return (x
> 0) ? __mpsin (x
, 0, false) : -__mpsin (-x
, 0, false);
821 /***************************************************************************/
822 /* Routine compute sin(x+dx) (Double-Length number) where x is small enough*/
823 /* to use Taylor series around zero and (x+dx) */
824 /* in first or third quarter of unit circle.Routine receive also */
825 /* (right argument) the original value of x for computing error of */
826 /* result.And if result not accurate enough routine calls mpsin1 or dubsin */
827 /***************************************************************************/
831 sloww (double x
, double dx
, double orig
)
833 double y
, t
, res
, cor
, w
[2], a
, da
, xn
;
836 res
= TAYLOR_SLOW (x
, dx
, cor
);
838 cor
= 1.0005 * cor
+ fabs (orig
) * 3.1e-30;
840 cor
= 1.0005 * cor
- fabs (orig
) * 3.1e-30;
842 if (res
== res
+ cor
)
846 (x
> 0) ? __dubsin (x
, dx
, w
) : __dubsin (-x
, -dx
, w
);
848 cor
= 1.000000001 * w
[1] + fabs (orig
) * 1.1e-30;
850 cor
= 1.000000001 * w
[1] - fabs (orig
) * 1.1e-30;
852 if (w
[0] == w
[0] + cor
)
853 return (x
> 0) ? w
[0] : -w
[0];
856 t
= (orig
* hpinv
+ toint
);
859 y
= (orig
- xn
* mp1
) - xn
* mp2
;
860 n
= v
.i
[LOW_HALF
] & 3;
866 da
= ((t
- a
) - y
) + da
;
872 (a
> 0) ? __dubsin (a
, da
, w
) : __dubsin (-a
, -da
, w
);
874 cor
= 1.000000001 * w
[1] + fabs (orig
) * 1.1e-40;
876 cor
= 1.000000001 * w
[1] - fabs (orig
) * 1.1e-40;
878 if (w
[0] == w
[0] + cor
)
879 return (a
> 0) ? w
[0] : -w
[0];
881 return __mpsin (orig
, 0, true);
886 /***************************************************************************/
887 /* Routine compute sin(x+dx) (Double-Length number) where x in first or */
888 /* third quarter of unit circle.Routine receive also (right argument) the */
889 /* original value of x for computing error of result.And if result not */
890 /* accurate enough routine calls mpsin1 or dubsin */
891 /***************************************************************************/
895 sloww1 (double x
, double dx
, double orig
, int m
)
898 double w
[2], y
, cor
, res
;
902 res
= do_sin_slow (u
, y
, dx
, 3.1e-30 * fabs (orig
), &cor
);
904 if (res
== res
+ cor
)
905 return (m
> 0) ? res
: -res
;
911 cor
= 1.000000005 * w
[1] + 1.1e-30 * fabs (orig
);
913 cor
= 1.000000005 * w
[1] - 1.1e-30 * fabs (orig
);
915 if (w
[0] == w
[0] + cor
)
916 return (m
> 0) ? w
[0] : -w
[0];
918 return __mpsin (orig
, 0, true);
922 /***************************************************************************/
923 /* Routine compute sin(x+dx) (Double-Length number) where x in second or */
924 /* fourth quarter of unit circle.Routine receive also the original value */
925 /* and quarter(n= 1or 3)of x for computing error of result.And if result not*/
926 /* accurate enough routine calls mpsin1 or dubsin */
927 /***************************************************************************/
931 sloww2 (double x
, double dx
, double orig
, int n
)
934 double w
[2], y
, cor
, res
;
938 res
= do_cos_slow (u
, y
, dx
, 3.1e-30 * fabs (orig
), &cor
);
940 if (res
== res
+ cor
)
941 return (n
& 2) ? -res
: res
;
947 cor
= 1.000000005 * w
[1] + 1.1e-30 * fabs (orig
);
949 cor
= 1.000000005 * w
[1] - 1.1e-30 * fabs (orig
);
951 if (w
[0] == w
[0] + cor
)
952 return (n
& 2) ? -w
[0] : w
[0];
954 return __mpsin (orig
, 0, true);
958 /***************************************************************************/
959 /* Routine compute sin(x+dx) or cos(x+dx) (Double-Length number) where x */
960 /* is small enough to use Taylor series around zero and (x+dx) */
961 /* in first or third quarter of unit circle.Routine receive also */
962 /* (right argument) the original value of x for computing error of */
963 /* result.And if result not accurate enough routine calls other routines */
964 /***************************************************************************/
968 bsloww (double x
, double dx
, double orig
, int n
)
970 double res
, cor
, w
[2];
972 res
= TAYLOR_SLOW (x
, dx
, cor
);
973 cor
= (cor
> 0) ? 1.0005 * cor
+ 1.1e-24 : 1.0005 * cor
- 1.1e-24;
974 if (res
== res
+ cor
)
978 (x
> 0) ? __dubsin (x
, dx
, w
) : __dubsin (-x
, -dx
, w
);
980 cor
= 1.000000001 * w
[1] + 1.1e-24;
982 cor
= 1.000000001 * w
[1] - 1.1e-24;
983 if (w
[0] == w
[0] + cor
)
984 return (x
> 0) ? w
[0] : -w
[0];
986 return (n
& 1) ? __mpcos (orig
, 0, true) : __mpsin (orig
, 0, true);
990 /***************************************************************************/
991 /* Routine compute sin(x+dx) or cos(x+dx) (Double-Length number) where x */
992 /* in first or third quarter of unit circle.Routine receive also */
993 /* (right argument) the original value of x for computing error of result.*/
994 /* And if result not accurate enough routine calls other routines */
995 /***************************************************************************/
999 bsloww1 (double x
, double dx
, double orig
, int n
)
1002 double w
[2], y
, cor
, res
;
1006 y
= y
- (u
.x
- big
);
1007 dx
= (x
> 0) ? dx
: -dx
;
1008 res
= do_sin_slow (u
, y
, dx
, 1.1e-24, &cor
);
1009 if (res
== res
+ cor
)
1010 return (x
> 0) ? res
: -res
;
1013 __dubsin (fabs (x
), dx
, w
);
1016 cor
= 1.000000005 * w
[1] + 1.1e-24;
1018 cor
= 1.000000005 * w
[1] - 1.1e-24;
1020 if (w
[0] == w
[0] + cor
)
1021 return (x
> 0) ? w
[0] : -w
[0];
1023 return (n
& 1) ? __mpcos (orig
, 0, true) : __mpsin (orig
, 0, true);
1027 /***************************************************************************/
1028 /* Routine compute sin(x+dx) or cos(x+dx) (Double-Length number) where x */
1029 /* in second or fourth quarter of unit circle.Routine receive also the */
1030 /* original value and quarter(n= 1or 3)of x for computing error of result. */
1031 /* And if result not accurate enough routine calls other routines */
1032 /***************************************************************************/
1036 bsloww2 (double x
, double dx
, double orig
, int n
)
1039 double w
[2], y
, cor
, res
;
1043 y
= y
- (u
.x
- big
);
1044 dx
= (x
> 0) ? dx
: -dx
;
1045 res
= do_cos_slow (u
, y
, dx
, 1.1e-24, &cor
);
1046 if (res
== res
+ cor
)
1047 return (n
& 2) ? -res
: res
;
1050 __docos (fabs (x
), dx
, w
);
1053 cor
= 1.000000005 * w
[1] + 1.1e-24;
1055 cor
= 1.000000005 * w
[1] - 1.1e-24;
1057 if (w
[0] == w
[0] + cor
)
1058 return (n
& 2) ? -w
[0] : w
[0];
1060 return (n
& 1) ? __mpsin (orig
, 0, true) : __mpcos (orig
, 0, true);
1064 /************************************************************************/
1065 /* Routine compute cos(x) for 2^-27 < |x|< 0.25 by Taylor with more */
1066 /* precision and if still doesn't accurate enough by mpcos or docos */
1067 /************************************************************************/
1074 double w
[2], y
, cor
, res
;
1078 y
= y
- (u
.x
- big
);
1079 res
= do_cos_slow (u
, y
, 0, 0, &cor
);
1080 if (res
== res
+ cor
)
1086 if (w
[0] == w
[0] + 1.000000005 * w
[1])
1089 return __mpcos (x
, 0, false);
1093 /***************************************************************************/
1094 /* Routine compute cos(x+dx) (Double-Length number) where x is small enough*/
1095 /* to use Taylor series around zero and (x+dx) .Routine receive also */
1096 /* (right argument) the original value of x for computing error of */
1097 /* result.And if result not accurate enough routine calls other routines */
1098 /***************************************************************************/
1103 csloww (double x
, double dx
, double orig
)
1105 double y
, t
, res
, cor
, w
[2], a
, da
, xn
;
1110 res
= TAYLOR_SLOW (x
, dx
, cor
);
1113 cor
= 1.0005 * cor
+ fabs (orig
) * 3.1e-30;
1115 cor
= 1.0005 * cor
- fabs (orig
) * 3.1e-30;
1117 if (res
== res
+ cor
)
1121 (x
> 0) ? __dubsin (x
, dx
, w
) : __dubsin (-x
, -dx
, w
);
1124 cor
= 1.000000001 * w
[1] + fabs (orig
) * 1.1e-30;
1126 cor
= 1.000000001 * w
[1] - fabs (orig
) * 1.1e-30;
1128 if (w
[0] == w
[0] + cor
)
1129 return (x
> 0) ? w
[0] : -w
[0];
1132 t
= (orig
* hpinv
+ toint
);
1135 y
= (orig
- xn
* mp1
) - xn
* mp2
;
1136 n
= v
.i
[LOW_HALF
] & 3;
1142 da
= ((t
- a
) - y
) + da
;
1148 (a
> 0) ? __dubsin (a
, da
, w
) : __dubsin (-a
, -da
, w
);
1151 cor
= 1.000000001 * w
[1] + fabs (orig
) * 1.1e-40;
1153 cor
= 1.000000001 * w
[1] - fabs (orig
) * 1.1e-40;
1155 if (w
[0] == w
[0] + cor
)
1156 return (a
> 0) ? w
[0] : -w
[0];
1158 return __mpcos (orig
, 0, true);
1163 /***************************************************************************/
1164 /* Routine compute sin(x+dx) (Double-Length number) where x in first or */
1165 /* third quarter of unit circle.Routine receive also (right argument) the */
1166 /* original value of x for computing error of result.And if result not */
1167 /* accurate enough routine calls other routines */
1168 /***************************************************************************/
1172 csloww1 (double x
, double dx
, double orig
, int m
)
1175 double w
[2], y
, cor
, res
;
1178 y
= x
- (u
.x
- big
);
1179 res
= do_sin_slow (u
, y
, dx
, 3.1e-30 * fabs (orig
), &cor
);
1181 if (res
== res
+ cor
)
1182 return (m
> 0) ? res
: -res
;
1185 __dubsin (x
, dx
, w
);
1187 cor
= 1.000000005 * w
[1] + 1.1e-30 * fabs (orig
);
1189 cor
= 1.000000005 * w
[1] - 1.1e-30 * fabs (orig
);
1190 if (w
[0] == w
[0] + cor
)
1191 return (m
> 0) ? w
[0] : -w
[0];
1193 return __mpcos (orig
, 0, true);
1198 /***************************************************************************/
1199 /* Routine compute sin(x+dx) (Double-Length number) where x in second or */
1200 /* fourth quarter of unit circle.Routine receive also the original value */
1201 /* and quarter(n= 1or 3)of x for computing error of result.And if result not*/
1202 /* accurate enough routine calls other routines */
1203 /***************************************************************************/
1207 csloww2 (double x
, double dx
, double orig
, int n
)
1210 double w
[2], y
, cor
, res
;
1213 y
= x
- (u
.x
- big
);
1214 res
= do_cos_slow (u
, y
, dx
, 3.1e-30 * fabs (orig
), &cor
);
1216 if (res
== res
+ cor
)
1217 return (n
) ? -res
: res
;
1222 cor
= 1.000000005 * w
[1] + 1.1e-30 * fabs (orig
);
1224 cor
= 1.000000005 * w
[1] - 1.1e-30 * fabs (orig
);
1225 if (w
[0] == w
[0] + cor
)
1226 return (n
) ? -w
[0] : w
[0];
1228 return __mpcos (orig
, 0, true);
1233 weak_alias (__cos
, cos
)
1234 # ifdef NO_LONG_DOUBLE
1235 strong_alias (__cos
, __cosl
)
1236 weak_alias (__cos
, cosl
)
1240 weak_alias (__sin
, sin
)
1241 # ifdef NO_LONG_DOUBLE
1242 strong_alias (__sin
, __sinl
)
1243 weak_alias (__sin
, sinl
)