2 * IBM Accurate Mathematical Library
3 * written by International Business Machines Corp.
4 * Copyright (C) 2001-2014 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 /****************************************************************************/
55 #include <math_private.h>
58 /* Helper macros to compute sin of the input values. */
59 #define POLYNOMIAL2(xx) ((((s5 * (xx) + s4) * (xx) + s3) * (xx) + s2) * (xx))
61 #define POLYNOMIAL(xx) (POLYNOMIAL2 (xx) + s1)
63 /* The computed polynomial is a variation of the Taylor series expansion for
66 a - a^3/3! + a^5/5! - a^7/7! + a^9/9! + (1 - a^2) * da / 2
68 The constants s1, s2, s3, etc. are pre-computed values of 1/3!, 1/5! and so
69 on. The result is returned to LHS and correction in COR. */
70 #define TAYLOR_SIN(xx, a, da, cor) \
72 double t = ((POLYNOMIAL (xx) * (a) - 0.5 * (da)) * (xx) + (da)); \
73 double res = (a) + t; \
74 (cor) = ((a) - res) + t; \
78 /* This is again a variation of the Taylor series expansion with the term
79 x^3/3! expanded into the following for better accuracy:
81 bb * x ^ 3 + 3 * aa * x * x1 * x2 + aa * x1 ^ 3 + aa * x2 ^ 3
83 The correction term is dx and bb + aa = -1/3!
85 #define TAYLOR_SLOW(x0, dx, cor) \
87 static const double th2_36 = 206158430208.0; /* 1.5*2**37 */ \
88 double xx = (x0) * (x0); \
89 double x1 = ((x0) + th2_36) - th2_36; \
90 double y = aa * x1 * x1 * x1; \
91 double r = (x0) + y; \
92 double x2 = ((x0) - x1) + (dx); \
93 double t = (((POLYNOMIAL2 (xx) + bb) * xx + 3.0 * aa * x1 * x2) \
94 * (x0) + aa * x2 * x2 * x2 + (dx)); \
95 t = (((x0) - r) + y) + t; \
97 (cor) = (r - res) + t; \
101 #define SINCOS_TABLE_LOOKUP(u, sn, ssn, cs, ccs) \
103 int4 k = u.i[LOW_HALF] << 2; \
104 sn = __sincostab.x[k]; \
105 ssn = __sincostab.x[k + 1]; \
106 cs = __sincostab.x[k + 2]; \
107 ccs = __sincostab.x[k + 3]; \
118 } __sincostab attribute_hidden
;
121 sn3
= -1.66666666666664880952546298448555E-01,
122 sn5
= 8.33333214285722277379541354343671E-03,
123 cs2
= 4.99999999999999999999950396842453E-01,
124 cs4
= -4.16666666666664434524222570944589E-02,
125 cs6
= 1.38888874007937613028114285595617E-03;
127 static const double t22
= 0x1.8p22
;
129 void __dubsin (double x
, double dx
, double w
[]);
130 void __docos (double x
, double dx
, double w
[]);
131 double __mpsin (double x
, double dx
, bool reduce_range
);
132 double __mpcos (double x
, double dx
, bool reduce_range
);
133 static double slow (double x
);
134 static double slow1 (double x
);
135 static double slow2 (double x
);
136 static double sloww (double x
, double dx
, double orig
);
137 static double sloww1 (double x
, double dx
, double orig
, int m
);
138 static double sloww2 (double x
, double dx
, double orig
, int n
);
139 static double bsloww (double x
, double dx
, double orig
, int n
);
140 static double bsloww1 (double x
, double dx
, double orig
, int n
);
141 static double bsloww2 (double x
, double dx
, double orig
, int n
);
142 int __branred (double x
, double *a
, double *aa
);
143 static double cslow2 (double x
);
144 static double csloww (double x
, double dx
, double orig
);
145 static double csloww1 (double x
, double dx
, double orig
, int m
);
146 static double csloww2 (double x
, double dx
, double orig
, int n
);
148 /* Given a number partitioned into U and X such that U is an index into the
149 sin/cos table, this macro computes the cosine of the number by combining
150 the sin and cos of X (as computed by a variation of the Taylor series) with
151 the values looked up from the sin/cos table to get the result in RES and a
152 correction value in COR. */
154 do_cos (mynumber u
, double x
, double *corp
)
156 double xx
, s
, sn
, ssn
, c
, cs
, ccs
, res
, cor
;
158 s
= x
+ x
* xx
* (sn3
+ xx
* sn5
);
159 c
= xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
160 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
161 cor
= (ccs
- s
* ssn
- cs
* c
) - sn
* s
;
163 cor
= (cs
- res
) + cor
;
168 /* A more precise variant of DO_COS where the number is partitioned into U, X
169 and DX. EPS is the adjustment to the correction COR. */
171 do_cos_slow (mynumber u
, double x
, double dx
, double eps
, double *corp
)
173 double xx
, y
, x1
, x2
, e1
, e2
, res
, cor
;
174 double s
, sn
, ssn
, c
, cs
, ccs
;
176 s
= x
* xx
* (sn3
+ xx
* sn5
);
177 c
= x
* dx
+ xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
178 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
179 x1
= (x
+ t22
) - t22
;
181 e1
= (sn
+ t22
) - t22
;
182 e2
= (sn
- e1
) + ssn
;
183 cor
= (ccs
- cs
* c
- e1
* x2
- e2
* x
) - sn
* s
;
185 cor
= cor
+ ((cs
- y
) - e1
* x1
);
187 cor
= (y
- res
) + cor
;
189 cor
= 1.0005 * cor
+ eps
;
191 cor
= 1.0005 * cor
- eps
;
196 /* Given a number partitioned into U and X and DX such that U is an index into
197 the sin/cos table, this macro computes the sine of the number by combining
198 the sin and cos of X (as computed by a variation of the Taylor series) with
199 the values looked up from the sin/cos table to get the result in RES and a
200 correction value in COR. */
202 do_sin (mynumber u
, double x
, double dx
, double *corp
)
204 double xx
, s
, sn
, ssn
, c
, cs
, ccs
, cor
, res
;
206 s
= x
+ (dx
+ x
* xx
* (sn3
+ xx
* sn5
));
207 c
= x
* dx
+ xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
208 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
209 cor
= (ssn
+ s
* ccs
- sn
* c
) + cs
* s
;
211 cor
= (sn
- res
) + cor
;
216 /* A more precise variant of res = do_sin where the number is partitioned into U, X
217 and DX. EPS is the adjustment to the correction COR. */
219 do_sin_slow (mynumber u
, double x
, double dx
, double eps
, double *corp
)
221 double xx
, y
, x1
, x2
, c1
, c2
, res
, cor
;
222 double s
, sn
, ssn
, c
, cs
, ccs
;
224 s
= x
* xx
* (sn3
+ xx
* sn5
);
225 c
= xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
226 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
227 x1
= (x
+ t22
) - t22
;
229 c1
= (cs
+ t22
) - t22
;
230 c2
= (cs
- c1
) + ccs
;
231 cor
= (ssn
+ s
* ccs
+ cs
* s
+ c2
* x
+ c1
* x2
- sn
* x
* dx
) - sn
* c
;
233 cor
= cor
+ ((sn
- y
) + c1
* x1
);
235 cor
= (y
- res
) + cor
;
237 cor
= 1.0005 * cor
+ eps
;
239 cor
= 1.0005 * cor
- eps
;
244 /* Reduce range of X and compute sin of a + da. K is the amount by which to
245 rotate the quadrants. This allows us to use the same routine to compute cos
246 by simply rotating the quadrants by 1. */
249 reduce_and_compute (double x
, unsigned int k
)
251 double retval
= 0, a
, da
;
252 unsigned int n
= __branred (x
, &a
, &da
);
258 retval
= bsloww (a
, da
, x
, n
);
260 retval
= bsloww1 (a
, da
, x
, n
);
264 retval
= bsloww (-a
, -da
, x
, n
);
266 retval
= bsloww1 (-a
, -da
, x
, n
);
271 retval
= bsloww2 (a
, da
, x
, n
);
277 /*******************************************************************/
278 /* An ultimate sin routine. Given an IEEE double machine number x */
279 /* it computes the correctly rounded (to nearest) value of sin(x) */
280 /*******************************************************************/
285 double xx
, res
, t
, cor
, y
, s
, c
, sn
, ssn
, cs
, ccs
, xn
, a
, da
, db
, eps
, xn1
,
291 SET_RESTORE_ROUND_53BIT (FE_TONEAREST
);
295 k
= 0x7fffffff & m
; /* no sign */
296 if (k
< 0x3e500000) /* if x->0 =>sin(x)=x */
298 /*---------------------------- 2^-26 < |x|< 0.25 ----------------------*/
299 else if (k
< 0x3fd00000)
303 t
= POLYNOMIAL (xx
) * (xx
* x
);
306 retval
= (res
== res
+ 1.07 * cor
) ? res
: slow (x
);
307 } /* else if (k < 0x3fd00000) */
308 /*---------------------------- 0.25<|x|< 0.855469---------------------- */
309 else if (k
< 0x3feb6000)
311 u
.x
= (m
> 0) ? big
+ x
: big
- x
;
312 y
= (m
> 0) ? x
- (u
.x
- big
) : x
+ (u
.x
- big
);
314 s
= y
+ y
* xx
* (sn3
+ xx
* sn5
);
315 c
= xx
* (cs2
+ xx
* (cs4
+ xx
* cs6
));
316 SINCOS_TABLE_LOOKUP (u
, sn
, ssn
, cs
, ccs
);
322 cor
= (ssn
+ s
* ccs
- sn
* c
) + cs
* s
;
324 cor
= (sn
- res
) + cor
;
325 retval
= (res
== res
+ 1.096 * cor
) ? res
: slow1 (x
);
326 } /* else if (k < 0x3feb6000) */
328 /*----------------------- 0.855469 <|x|<2.426265 ----------------------*/
329 else if (k
< 0x400368fd)
332 y
= (m
> 0) ? hp0
- x
: hp0
+ x
;
336 y
= (y
- (u
.x
- big
)) + hp1
;
341 y
= (-hp1
) - (y
+ (u
.x
- big
));
343 res
= do_cos (u
, y
, &cor
);
344 retval
= (res
== res
+ 1.020 * cor
) ? ((m
> 0) ? res
: -res
) : slow2 (x
);
345 } /* else if (k < 0x400368fd) */
347 /*-------------------------- 2.426265<|x|< 105414350 ----------------------*/
348 else if (k
< 0x419921FB)
350 t
= (x
* hpinv
+ toint
);
353 y
= (x
- xn
* mp1
) - xn
* mp2
;
354 n
= v
.i
[LOW_HALF
] & 3;
358 eps
= ABS (x
) * 1.2e-30;
361 { /* quarter of unit circle */
373 res
= TAYLOR_SIN (xx
, a
, da
, cor
);
374 cor
= (cor
> 0) ? 1.02 * cor
+ eps
: 1.02 * cor
- eps
;
375 retval
= (res
== res
+ cor
) ? res
: sloww (a
, da
, x
);
389 res
= do_sin (u
, y
, da
, &cor
);
390 cor
= (cor
> 0) ? 1.035 * cor
+ eps
: 1.035 * cor
- eps
;
391 retval
= ((res
== res
+ cor
) ? ((m
) ? res
: -res
)
392 : sloww1 (a
, da
, x
, m
));
404 y
= a
- (u
.x
- big
) + da
;
405 res
= do_cos (u
, y
, &cor
);
406 cor
= (cor
> 0) ? 1.025 * cor
+ eps
: 1.025 * cor
- eps
;
407 retval
= ((res
== res
+ cor
) ? ((n
& 2) ? -res
: res
)
408 : sloww2 (a
, da
, x
, n
));
411 } /* else if (k < 0x419921FB ) */
413 /*---------------------105414350 <|x|< 281474976710656 --------------------*/
414 else if (k
< 0x42F00000)
416 t
= (x
* hpinv
+ toint
);
419 xn1
= (xn
+ 8.0e22
) - 8.0e22
;
421 y
= ((((x
- xn1
* mp1
) - xn1
* mp2
) - xn2
* mp1
) - xn2
* mp2
);
422 n
= v
.i
[LOW_HALF
] & 3;
426 da
= (da
- xn2
* pp3
) - xn
* pp4
;
444 res
= TAYLOR_SIN (xx
, a
, da
, cor
);
445 cor
= (cor
> 0) ? 1.02 * cor
+ eps
: 1.02 * cor
- eps
;
446 retval
= (res
== res
+ cor
) ? res
: bsloww (a
, da
, x
, n
);
463 res
= do_sin (u
, y
, db
, &cor
);
464 cor
= (cor
> 0) ? 1.035 * cor
+ eps
: 1.035 * cor
- eps
;
465 retval
= ((res
== res
+ cor
) ? ((m
) ? res
: -res
)
466 : bsloww1 (a
, da
, x
, n
));
478 y
= a
- (u
.x
- big
) + da
;
479 res
= do_cos (u
, y
, &cor
);
480 cor
= (cor
> 0) ? 1.025 * cor
+ eps
: 1.025 * cor
- eps
;
481 retval
= ((res
== res
+ cor
) ? ((n
& 2) ? -res
: res
)
482 : bsloww2 (a
, da
, x
, n
));
485 } /* else if (k < 0x42F00000 ) */
487 /* -----------------281474976710656 <|x| <2^1024----------------------------*/
488 else if (k
< 0x7ff00000)
489 retval
= reduce_and_compute (x
, 0);
491 /*--------------------- |x| > 2^1024 ----------------------------------*/
494 if (k
== 0x7ff00000 && u
.i
[LOW_HALF
] == 0)
503 /*******************************************************************/
504 /* An ultimate cos routine. Given an IEEE double machine number x */
505 /* it computes the correctly rounded (to nearest) value of cos(x) */
506 /*******************************************************************/
512 double y
, xx
, res
, t
, cor
, xn
, a
, da
, db
, eps
, xn1
,
519 SET_RESTORE_ROUND_53BIT (FE_TONEAREST
);
525 /* |x|<2^-27 => cos(x)=1 */
529 else if (k
< 0x3feb6000)
530 { /* 2^-27 < |x| < 0.855469 */
534 res
= do_cos (u
, y
, &cor
);
535 retval
= (res
== res
+ 1.020 * cor
) ? res
: cslow2 (x
);
536 } /* else if (k < 0x3feb6000) */
538 else if (k
< 0x400368fd)
539 { /* 0.855469 <|x|<2.426265 */ ;
546 res
= TAYLOR_SIN (xx
, a
, da
, cor
);
547 cor
= (cor
> 0) ? 1.02 * cor
+ 1.0e-31 : 1.02 * cor
- 1.0e-31;
548 retval
= (res
== res
+ cor
) ? res
: csloww (a
, da
, x
);
564 res
= do_sin (u
, y
, da
, &cor
);
565 cor
= (cor
> 0) ? 1.035 * cor
+ 1.0e-31 : 1.035 * cor
- 1.0e-31;
566 retval
= ((res
== res
+ cor
) ? ((m
) ? res
: -res
)
567 : csloww1 (a
, da
, x
, m
));
570 } /* else if (k < 0x400368fd) */
573 else if (k
< 0x419921FB)
574 { /* 2.426265<|x|< 105414350 */
575 t
= (x
* hpinv
+ toint
);
578 y
= (x
- xn
* mp1
) - xn
* mp2
;
579 n
= v
.i
[LOW_HALF
] & 3;
583 eps
= ABS (x
) * 1.2e-30;
597 res
= TAYLOR_SIN (xx
, a
, da
, cor
);
598 cor
= (cor
> 0) ? 1.02 * cor
+ eps
: 1.02 * cor
- eps
;
599 retval
= (res
== res
+ cor
) ? res
: csloww (a
, da
, x
);
615 res
= do_sin (u
, y
, da
, &cor
);
616 cor
= (cor
> 0) ? 1.035 * cor
+ eps
: 1.035 * cor
- eps
;
617 retval
= ((res
== res
+ cor
) ? ((m
) ? res
: -res
)
618 : csloww1 (a
, da
, x
, m
));
630 y
= a
- (u
.x
- big
) + da
;
631 res
= do_cos (u
, y
, &cor
);
632 cor
= (cor
> 0) ? 1.025 * cor
+ eps
: 1.025 * cor
- eps
;
633 retval
= ((res
== res
+ cor
) ? ((n
) ? -res
: res
)
634 : csloww2 (a
, da
, x
, n
));
637 } /* else if (k < 0x419921FB ) */
639 else if (k
< 0x42F00000)
641 t
= (x
* hpinv
+ toint
);
644 xn1
= (xn
+ 8.0e22
) - 8.0e22
;
646 y
= ((((x
- xn1
* mp1
) - xn1
* mp2
) - xn2
* mp1
) - xn2
* mp2
);
647 n
= v
.i
[LOW_HALF
] & 3;
651 da
= (da
- xn2
* pp3
) - xn
* pp4
;
668 res
= TAYLOR_SIN (xx
, a
, da
, cor
);
669 cor
= (cor
> 0) ? 1.02 * cor
+ eps
: 1.02 * cor
- eps
;
670 retval
= (res
== res
+ cor
) ? res
: bsloww (a
, da
, x
, n
);
687 res
= do_sin (u
, y
, db
, &cor
);
688 cor
= (cor
> 0) ? 1.035 * cor
+ eps
: 1.035 * cor
- eps
;
689 retval
= ((res
== res
+ cor
) ? ((m
) ? res
: -res
)
690 : bsloww1 (a
, da
, x
, n
));
702 y
= a
- (u
.x
- big
) + da
;
703 res
= do_cos (u
, y
, &cor
);
704 cor
= (cor
> 0) ? 1.025 * cor
+ eps
: 1.025 * cor
- eps
;
705 retval
= ((res
== res
+ cor
) ? ((n
) ? -res
: res
)
706 : bsloww2 (a
, da
, x
, n
));
709 } /* else if (k < 0x42F00000 ) */
711 /* 281474976710656 <|x| <2^1024 */
712 else if (k
< 0x7ff00000)
713 retval
= reduce_and_compute (x
, 1);
717 if (k
== 0x7ff00000 && u
.i
[LOW_HALF
] == 0)
719 retval
= x
/ x
; /* |x| > 2^1024 */
725 /************************************************************************/
726 /* Routine compute sin(x) for 2^-26 < |x|< 0.25 by Taylor with more */
727 /* precision and if still doesn't accurate enough by mpsin or dubsin */
728 /************************************************************************/
734 double res
, cor
, w
[2];
735 res
= TAYLOR_SLOW (x
, 0, cor
);
736 if (res
== res
+ 1.0007 * cor
)
740 __dubsin (ABS (x
), 0, w
);
741 if (w
[0] == w
[0] + 1.000000001 * w
[1])
742 return (x
> 0) ? w
[0] : -w
[0];
744 return (x
> 0) ? __mpsin (x
, 0, false) : -__mpsin (-x
, 0, false);
748 /*******************************************************************************/
749 /* Routine compute sin(x) for 0.25<|x|< 0.855469 by __sincostab.tbl and Taylor */
750 /* and if result still doesn't accurate enough by mpsin or dubsin */
751 /*******************************************************************************/
758 double w
[2], y
, cor
, res
;
762 res
= do_sin_slow (u
, y
, 0, 0, &cor
);
763 if (res
== res
+ cor
)
764 return (x
> 0) ? res
: -res
;
767 __dubsin (ABS (x
), 0, w
);
768 if (w
[0] == w
[0] + 1.000000005 * w
[1])
769 return (x
> 0) ? w
[0] : -w
[0];
771 return (x
> 0) ? __mpsin (x
, 0, false) : -__mpsin (-x
, 0, false);
775 /**************************************************************************/
776 /* Routine compute sin(x) for 0.855469 <|x|<2.426265 by __sincostab.tbl */
777 /* and if result still doesn't accurate enough by mpsin or dubsin */
778 /**************************************************************************/
784 double w
[2], y
, y1
, y2
, cor
, res
, del
;
797 y
= -(y
+ (u
.x
- big
));
800 res
= do_cos_slow (u
, y
, del
, 0, &cor
);
801 if (res
== res
+ cor
)
802 return (x
> 0) ? res
: -res
;
809 if (w
[0] == w
[0] + 1.000000005 * w
[1])
810 return (x
> 0) ? w
[0] : -w
[0];
812 return (x
> 0) ? __mpsin (x
, 0, false) : -__mpsin (-x
, 0, false);
816 /***************************************************************************/
817 /* Routine compute sin(x+dx) (Double-Length number) where x is small enough*/
818 /* to use Taylor series around zero and (x+dx) */
819 /* in first or third quarter of unit circle.Routine receive also */
820 /* (right argument) the original value of x for computing error of */
821 /* result.And if result not accurate enough routine calls mpsin1 or dubsin */
822 /***************************************************************************/
826 sloww (double x
, double dx
, double orig
)
828 double y
, t
, res
, cor
, w
[2], a
, da
, xn
;
831 res
= TAYLOR_SLOW (x
, dx
, cor
);
833 cor
= 1.0005 * cor
+ ABS (orig
) * 3.1e-30;
835 cor
= 1.0005 * cor
- ABS (orig
) * 3.1e-30;
837 if (res
== res
+ cor
)
841 (x
> 0) ? __dubsin (x
, dx
, w
) : __dubsin (-x
, -dx
, w
);
843 cor
= 1.000000001 * w
[1] + ABS (orig
) * 1.1e-30;
845 cor
= 1.000000001 * w
[1] - ABS (orig
) * 1.1e-30;
847 if (w
[0] == w
[0] + cor
)
848 return (x
> 0) ? w
[0] : -w
[0];
851 t
= (orig
* hpinv
+ toint
);
854 y
= (orig
- xn
* mp1
) - xn
* mp2
;
855 n
= v
.i
[LOW_HALF
] & 3;
861 da
= ((t
- a
) - y
) + da
;
867 (a
> 0) ? __dubsin (a
, da
, w
) : __dubsin (-a
, -da
, w
);
869 cor
= 1.000000001 * w
[1] + ABS (orig
) * 1.1e-40;
871 cor
= 1.000000001 * w
[1] - ABS (orig
) * 1.1e-40;
873 if (w
[0] == w
[0] + cor
)
874 return (a
> 0) ? w
[0] : -w
[0];
876 return __mpsin (orig
, 0, true);
881 /***************************************************************************/
882 /* Routine compute sin(x+dx) (Double-Length number) where x in first or */
883 /* third quarter of unit circle.Routine receive also (right argument) the */
884 /* original value of x for computing error of result.And if result not */
885 /* accurate enough routine calls mpsin1 or dubsin */
886 /***************************************************************************/
890 sloww1 (double x
, double dx
, double orig
, int m
)
893 double w
[2], y
, cor
, res
;
897 res
= do_sin_slow (u
, y
, dx
, 3.1e-30 * ABS (orig
), &cor
);
899 if (res
== res
+ cor
)
900 return (m
> 0) ? res
: -res
;
906 cor
= 1.000000005 * w
[1] + 1.1e-30 * ABS (orig
);
908 cor
= 1.000000005 * w
[1] - 1.1e-30 * ABS (orig
);
910 if (w
[0] == w
[0] + cor
)
911 return (m
> 0) ? w
[0] : -w
[0];
913 return __mpsin (orig
, 0, true);
917 /***************************************************************************/
918 /* Routine compute sin(x+dx) (Double-Length number) where x in second or */
919 /* fourth quarter of unit circle.Routine receive also the original value */
920 /* and quarter(n= 1or 3)of x for computing error of result.And if result not*/
921 /* accurate enough routine calls mpsin1 or dubsin */
922 /***************************************************************************/
926 sloww2 (double x
, double dx
, double orig
, int n
)
929 double w
[2], y
, cor
, res
;
933 res
= do_cos_slow (u
, y
, dx
, 3.1e-30 * ABS (orig
), &cor
);
935 if (res
== res
+ cor
)
936 return (n
& 2) ? -res
: res
;
942 cor
= 1.000000005 * w
[1] + 1.1e-30 * ABS (orig
);
944 cor
= 1.000000005 * w
[1] - 1.1e-30 * ABS (orig
);
946 if (w
[0] == w
[0] + cor
)
947 return (n
& 2) ? -w
[0] : w
[0];
949 return __mpsin (orig
, 0, true);
953 /***************************************************************************/
954 /* Routine compute sin(x+dx) or cos(x+dx) (Double-Length number) where x */
955 /* is small enough to use Taylor series around zero and (x+dx) */
956 /* in first or third quarter of unit circle.Routine receive also */
957 /* (right argument) the original value of x for computing error of */
958 /* result.And if result not accurate enough routine calls other routines */
959 /***************************************************************************/
963 bsloww (double x
, double dx
, double orig
, int n
)
965 double res
, cor
, w
[2];
967 res
= TAYLOR_SLOW (x
, dx
, cor
);
968 cor
= (cor
> 0) ? 1.0005 * cor
+ 1.1e-24 : 1.0005 * cor
- 1.1e-24;
969 if (res
== res
+ cor
)
973 (x
> 0) ? __dubsin (x
, dx
, w
) : __dubsin (-x
, -dx
, w
);
975 cor
= 1.000000001 * w
[1] + 1.1e-24;
977 cor
= 1.000000001 * w
[1] - 1.1e-24;
978 if (w
[0] == w
[0] + cor
)
979 return (x
> 0) ? w
[0] : -w
[0];
981 return (n
& 1) ? __mpcos (orig
, 0, true) : __mpsin (orig
, 0, true);
985 /***************************************************************************/
986 /* Routine compute sin(x+dx) or cos(x+dx) (Double-Length number) where x */
987 /* in first or third quarter of unit circle.Routine receive also */
988 /* (right argument) the original value of x for computing error of result.*/
989 /* And if result not accurate enough routine calls other routines */
990 /***************************************************************************/
994 bsloww1 (double x
, double dx
, double orig
, int n
)
997 double w
[2], y
, cor
, res
;
1001 y
= y
- (u
.x
- big
);
1002 dx
= (x
> 0) ? dx
: -dx
;
1003 res
= do_sin_slow (u
, y
, dx
, 1.1e-24, &cor
);
1004 if (res
== res
+ cor
)
1005 return (x
> 0) ? res
: -res
;
1008 __dubsin (ABS (x
), dx
, w
);
1011 cor
= 1.000000005 * w
[1] + 1.1e-24;
1013 cor
= 1.000000005 * w
[1] - 1.1e-24;
1015 if (w
[0] == w
[0] + cor
)
1016 return (x
> 0) ? w
[0] : -w
[0];
1018 return (n
& 1) ? __mpcos (orig
, 0, true) : __mpsin (orig
, 0, true);
1022 /***************************************************************************/
1023 /* Routine compute sin(x+dx) or cos(x+dx) (Double-Length number) where x */
1024 /* in second or fourth quarter of unit circle.Routine receive also the */
1025 /* original value and quarter(n= 1or 3)of x for computing error of result. */
1026 /* And if result not accurate enough routine calls other routines */
1027 /***************************************************************************/
1031 bsloww2 (double x
, double dx
, double orig
, int n
)
1034 double w
[2], y
, cor
, res
;
1038 y
= y
- (u
.x
- big
);
1039 dx
= (x
> 0) ? dx
: -dx
;
1040 res
= do_cos_slow (u
, y
, dx
, 1.1e-24, &cor
);
1041 if (res
== res
+ cor
)
1042 return (n
& 2) ? -res
: res
;
1045 __docos (ABS (x
), dx
, w
);
1048 cor
= 1.000000005 * w
[1] + 1.1e-24;
1050 cor
= 1.000000005 * w
[1] - 1.1e-24;
1052 if (w
[0] == w
[0] + cor
)
1053 return (n
& 2) ? -w
[0] : w
[0];
1055 return (n
& 1) ? __mpsin (orig
, 0, true) : __mpcos (orig
, 0, true);
1059 /************************************************************************/
1060 /* Routine compute cos(x) for 2^-27 < |x|< 0.25 by Taylor with more */
1061 /* precision and if still doesn't accurate enough by mpcos or docos */
1062 /************************************************************************/
1069 double w
[2], y
, cor
, res
;
1073 y
= y
- (u
.x
- big
);
1074 res
= do_cos_slow (u
, y
, 0, 0, &cor
);
1075 if (res
== res
+ cor
)
1081 if (w
[0] == w
[0] + 1.000000005 * w
[1])
1084 return __mpcos (x
, 0, false);
1088 /***************************************************************************/
1089 /* Routine compute cos(x+dx) (Double-Length number) where x is small enough*/
1090 /* to use Taylor series around zero and (x+dx) .Routine receive also */
1091 /* (right argument) the original value of x for computing error of */
1092 /* result.And if result not accurate enough routine calls other routines */
1093 /***************************************************************************/
1098 csloww (double x
, double dx
, double orig
)
1100 double y
, t
, res
, cor
, w
[2], a
, da
, xn
;
1105 res
= TAYLOR_SLOW (x
, dx
, cor
);
1108 cor
= 1.0005 * cor
+ ABS (orig
) * 3.1e-30;
1110 cor
= 1.0005 * cor
- ABS (orig
) * 3.1e-30;
1112 if (res
== res
+ cor
)
1116 (x
> 0) ? __dubsin (x
, dx
, w
) : __dubsin (-x
, -dx
, w
);
1119 cor
= 1.000000001 * w
[1] + ABS (orig
) * 1.1e-30;
1121 cor
= 1.000000001 * w
[1] - ABS (orig
) * 1.1e-30;
1123 if (w
[0] == w
[0] + cor
)
1124 return (x
> 0) ? w
[0] : -w
[0];
1127 t
= (orig
* hpinv
+ toint
);
1130 y
= (orig
- xn
* mp1
) - xn
* mp2
;
1131 n
= v
.i
[LOW_HALF
] & 3;
1137 da
= ((t
- a
) - y
) + da
;
1143 (a
> 0) ? __dubsin (a
, da
, w
) : __dubsin (-a
, -da
, w
);
1146 cor
= 1.000000001 * w
[1] + ABS (orig
) * 1.1e-40;
1148 cor
= 1.000000001 * w
[1] - ABS (orig
) * 1.1e-40;
1150 if (w
[0] == w
[0] + cor
)
1151 return (a
> 0) ? w
[0] : -w
[0];
1153 return __mpcos (orig
, 0, true);
1158 /***************************************************************************/
1159 /* Routine compute sin(x+dx) (Double-Length number) where x in first or */
1160 /* third quarter of unit circle.Routine receive also (right argument) the */
1161 /* original value of x for computing error of result.And if result not */
1162 /* accurate enough routine calls other routines */
1163 /***************************************************************************/
1167 csloww1 (double x
, double dx
, double orig
, int m
)
1170 double w
[2], y
, cor
, res
;
1173 y
= x
- (u
.x
- big
);
1174 res
= do_sin_slow (u
, y
, dx
, 3.1e-30 * ABS (orig
), &cor
);
1176 if (res
== res
+ cor
)
1177 return (m
> 0) ? res
: -res
;
1180 __dubsin (x
, dx
, w
);
1182 cor
= 1.000000005 * w
[1] + 1.1e-30 * ABS (orig
);
1184 cor
= 1.000000005 * w
[1] - 1.1e-30 * ABS (orig
);
1185 if (w
[0] == w
[0] + cor
)
1186 return (m
> 0) ? w
[0] : -w
[0];
1188 return __mpcos (orig
, 0, true);
1193 /***************************************************************************/
1194 /* Routine compute sin(x+dx) (Double-Length number) where x in second or */
1195 /* fourth quarter of unit circle.Routine receive also the original value */
1196 /* and quarter(n= 1or 3)of x for computing error of result.And if result not*/
1197 /* accurate enough routine calls other routines */
1198 /***************************************************************************/
1202 csloww2 (double x
, double dx
, double orig
, int n
)
1205 double w
[2], y
, cor
, res
;
1208 y
= x
- (u
.x
- big
);
1209 res
= do_cos_slow (u
, y
, dx
, 3.1e-30 * ABS (orig
), &cor
);
1211 if (res
== res
+ cor
)
1212 return (n
) ? -res
: res
;
1217 cor
= 1.000000005 * w
[1] + 1.1e-30 * ABS (orig
);
1219 cor
= 1.000000005 * w
[1] - 1.1e-30 * ABS (orig
);
1220 if (w
[0] == w
[0] + cor
)
1221 return (n
) ? -w
[0] : w
[0];
1223 return __mpcos (orig
, 0, true);
1228 weak_alias (__cos
, cos
)
1229 # ifdef NO_LONG_DOUBLE
1230 strong_alias (__cos
, __cosl
)
1231 weak_alias (__cos
, cosl
)
1235 weak_alias (__sin
, sin
)
1236 # ifdef NO_LONG_DOUBLE
1237 strong_alias (__sin
, __sinl
)
1238 weak_alias (__sin
, sinl
)