forked from bellard/mquickjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibm.c
More file actions
2260 lines (2075 loc) · 64.3 KB
/
libm.c
File metadata and controls
2260 lines (2075 loc) · 64.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Tiny Math Library
*
* Copyright (c) 2024 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* ====================================================
* Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved.
*
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/
#include <stdio.h>
#include <inttypes.h>
#include <math.h>
#define NDEBUG
#include <assert.h>
#include "cutils.h"
#include "libm.h"
/* define to enable softfloat support */
//#define USE_SOFTFLOAT
/* use less code for tan() but currently less precise */
#define USE_TAN_SHORTCUT
/*
TODO:
- smaller scalbn implementation ?
- add all ES6 math functions
*/
/*
tc32:
- base: size libm+libgcc: 21368
- size libm+libgcc: 11832
x86:
- size libm softfp: 18510
- size libm hardfp: 10051
TODO:
- unify i32 bit and i64 bit conversions
- unify comparisons operations
*/
typedef enum {
RM_RNE, /* Round to Nearest, ties to Even */
RM_RTZ, /* Round towards Zero */
RM_RDN, /* Round Down (must be even) */
RM_RUP, /* Round Up (must be odd) */
RM_RMM, /* Round to Nearest, ties to Max Magnitude */
RM_RMMUP, /* only for rint_sf64(): round to nearest, ties to +inf (must be odd) */
} RoundingModeEnum;
#define FFLAG_INVALID_OP (1 << 4)
#define FFLAG_DIVIDE_ZERO (1 << 3)
#define FFLAG_OVERFLOW (1 << 2)
#define FFLAG_UNDERFLOW (1 << 1)
#define FFLAG_INEXACT (1 << 0)
typedef enum {
FMINMAX_PROP, /* min(1, qNaN/sNaN) -> qNaN */
FMINMAX_IEEE754_2008, /* min(1, qNaN) -> 1, min(1, sNaN) -> qNaN */
FMINMAX_IEEE754_201X, /* min(1, qNaN/sNaN) -> 1 */
} SoftFPMinMaxTypeEnum;
typedef uint32_t sfloat32;
typedef uint64_t sfloat64;
#define F_STATIC static __maybe_unused
#define F_USE_FFLAGS 0
#define F_SIZE 32
#define F_NORMALIZE_ONLY
#include "softfp_template.h"
#define F_SIZE 64
#include "softfp_template.h"
#ifdef USE_SOFTFLOAT
/* wrappers */
double __adddf3(double a, double b)
{
return uint64_as_float64(add_sf64(float64_as_uint64(a),
float64_as_uint64(b), RM_RNE));
}
double __subdf3(double a, double b)
{
return uint64_as_float64(sub_sf64(float64_as_uint64(a),
float64_as_uint64(b), RM_RNE));
}
double __muldf3(double a, double b)
{
return uint64_as_float64(mul_sf64(float64_as_uint64(a),
float64_as_uint64(b), RM_RNE));
}
double __divdf3(double a, double b)
{
return uint64_as_float64(div_sf64(float64_as_uint64(a),
float64_as_uint64(b), RM_RNE));
}
/* comparisons */
int __eqdf2(double a, double b)
{
int ret = cmp_sf64(float64_as_uint64(a),
float64_as_uint64(b));
return ret;
}
/* NaN: return 0 */
int __nedf2(double a, double b)
{
int ret = cmp_sf64(float64_as_uint64(a),
float64_as_uint64(b));
if (unlikely(ret == 2))
return 0;
else
return ret;
}
int __ledf2(double a, double b)
{
int ret = cmp_sf64(float64_as_uint64(a),
float64_as_uint64(b));
return ret;
}
int __ltdf2(double a, double b)
{
int ret = cmp_sf64(float64_as_uint64(a),
float64_as_uint64(b));
return ret;
}
int __gedf2(double a, double b)
{
int ret = cmp_sf64(float64_as_uint64(a),
float64_as_uint64(b));
if (unlikely(ret == 2))
return -1;
else
return ret;
}
int __gtdf2(double a, double b)
{
int ret = cmp_sf64(float64_as_uint64(a),
float64_as_uint64(b));
if (unlikely(ret == 2))
return -1;
else
return ret;
}
int __unorddf2(double a, double b)
{
return isnan_sf64(float64_as_uint64(a)) ||
isnan_sf64(float64_as_uint64(b));
}
/* conversions */
double __floatsidf(int32_t a)
{
return uint64_as_float64(cvt_i32_sf64(a, RM_RNE));
}
double __floatdidf(int64_t a)
{
return uint64_as_float64(cvt_i64_sf64(a, RM_RNE));
}
double __floatunsidf(unsigned int a)
{
return uint64_as_float64(cvt_u32_sf64(a, RM_RNE));
}
int32_t __fixdfsi(double a)
{
return cvt_sf64_i32(float64_as_uint64(a), RM_RTZ);
}
double __extendsfdf2(float a)
{
return uint64_as_float64(cvt_sf32_sf64(float_as_uint(a)));
}
float __truncdfsf2(double a)
{
return uint_as_float(cvt_sf64_sf32(float64_as_uint64(a), RM_RNE));
}
double js_sqrt(double a)
{
return uint64_as_float64(sqrt_sf64(float64_as_uint64(a), RM_RNE));
}
#if defined(__tc32__)
/* XXX: check */
int __fpclassifyd(double a)
{
uint64_t u = float64_as_uint64(a);
uint32_t h = u >> 32;
uint32_t l = u;
h &= 0x7fffffff;
if (h >= 0x7ff00000) {
if (h == 0x7ff00000 && l == 0)
return FP_INFINITE;
else
return FP_NAN;
} else if (h < 0x00100000) {
if (h == 0 && l == 0)
return FP_ZERO;
else
return FP_SUBNORMAL;
} else {
return FP_NORMAL;
}
}
#endif
#endif /* USE_SOFTFLOAT */
int32_t js_lrint(double a)
{
return cvt_sf64_i32(float64_as_uint64(a), RM_RNE);
}
double js_fmod(double a, double b)
{
return uint64_as_float64(fmod_sf64(float64_as_uint64(a), float64_as_uint64(b)));
}
/* supported rounding modes: RM_UP, RM_DN, RM_RTZ, RM_RMMUP, RM_RMM */
static double rint_sf64(double a, RoundingModeEnum rm)
{
uint64_t u = float64_as_uint64(a);
uint64_t frac_mask, one, m, addend;
int e;
unsigned int s;
e = ((u >> 52) & 0x7ff) - 0x3ff;
s = u >> 63;
if (e < 0) {
m = u & (((uint64_t)1 << 52) - 1);
if (e == -0x3ff && m == 0) {
/* zero: nothing to do */
} else {
/* abs(a) < 1 */
s = u >> 63;
one = (uint64_t)0x3ff << 52;
u = 0;
switch(rm) {
case RM_RUP:
case RM_RDN:
if (s ^ (rm & 1))
u = one;
break;
default:
case RM_RMM:
case RM_RMMUP:
if (e == -1 && (m != 0 || (m == 0 && (!s || rm == RM_RMM))))
u = one;
break;
case RM_RTZ:
break;
}
u |= (uint64_t)s << 63;
}
} else if (e < 52) {
one = (uint64_t)1 << (52 - e);
frac_mask = one - 1;
addend = 0;
switch(rm) {
case RM_RMMUP:
addend = (one >> 1) - s;
break;
default:
case RM_RMM:
addend = (one >> 1);
break;
case RM_RTZ:
break;
case RM_RUP:
case RM_RDN:
if (s ^ (rm & 1))
addend = one - 1;
break;
}
u += addend;
u &= ~frac_mask; /* truncate to an integer */
}
/* otherwise: abs(a) >= 2^52, or NaN, +/-Infinity: no change */
return uint64_as_float64(u);
}
double js_floor(double x)
{
return rint_sf64(x, RM_RDN);
}
double js_ceil(double x)
{
return rint_sf64(x, RM_RUP);
}
double js_trunc(double x)
{
return rint_sf64(x, RM_RTZ);
}
double js_round_inf(double x)
{
return rint_sf64(x, RM_RMMUP);
}
double js_fabs(double x)
{
uint64_t a = float64_as_uint64(x);
return uint64_as_float64(a & 0x7fffffffffffffff);
}
/************************************************************/
/* libm */
#define EXTRACT_WORDS(ix0,ix1,d) \
do { \
uint64_t __u = float64_as_uint64(d); \
(ix0) = (uint32_t)(__u >> 32); \
(ix1) = (uint32_t)__u; \
} while (0)
static uint32_t get_high_word(double d)
{
return float64_as_uint64(d) >> 32;
}
static double set_high_word(double d, uint32_t h)
{
uint64_t u = float64_as_uint64(d);
u = (u & 0xffffffff) | ((uint64_t)h << 32);
return uint64_as_float64(u);
}
static uint32_t get_low_word(double d)
{
return float64_as_uint64(d);
}
/* set the low 32 bits to zero */
static double zero_low(double x)
{
uint64_t u = float64_as_uint64(x);
u &= 0xffffffff00000000;
return uint64_as_float64(u);
}
static double float64_from_u32(uint32_t h, uint32_t l)
{
return uint64_as_float64(((uint64_t)h << 32) | l);
}
static const double zero = 0.0;
static const double one = 1.0;
static const double half = 5.00000000000000000000e-01;
static const double tiny = 1.0e-300;
static const double huge = 1.0e300;
/* @(#)s_scalbn.c 1.3 95/01/18 */
/*
* ====================================================
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunSoft, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/
/*
* scalbn (double x, int n)
* scalbn(x,n) returns x* 2**n computed by exponent
* manipulation rather than by actually performing an
* exponentiation or a multiplication.
*/
static const double
two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
twom54 = 5.55111512312578270212e-17; /* 0x3C900000, 0x00000000 */
double js_scalbn(double x, int n)
{
int k,hx,lx;
EXTRACT_WORDS(hx, lx, x);
k = (hx&0x7ff00000)>>20; /* extract exponent */
if (k==0) { /* 0 or subnormal x */
if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */
x *= two54;
hx = get_high_word(x);
k = ((hx&0x7ff00000)>>20) - 54;
if (n< -50000) return tiny*x; /*underflow*/
}
if (k==0x7ff) return x+x; /* NaN or Inf */
k = k+n;
if (k > 0x7fe) return huge*copysign(huge,x); /* overflow */
if (k > 0) /* normal result */
{x = set_high_word(x, (hx&0x800fffff)|(k<<20)); return x;}
if (k <= -54) {
if (n > 50000) /* in case integer overflow in n+k */
return huge*copysign(huge,x); /*overflow*/
else
return tiny*copysign(tiny,x); /*underflow*/
}
k += 54; /* subnormal result */
x = set_high_word(x, (hx&0x800fffff)|(k<<20));
return x*twom54;
}
#ifndef USE_SOFTFLOAT
/* @(#)e_sqrt.c 1.3 95/01/18 */
/*
* ====================================================
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunSoft, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/
/* __ieee754_sqrt(x)
* Return correctly rounded sqrt.
* ------------------------------------------
* | Use the hardware sqrt if you have one |
* ------------------------------------------
* Method:
* Bit by bit method using integer arithmetic. (Slow, but portable)
* 1. Normalization
* Scale x to y in [1,4) with even powers of 2:
* find an integer k such that 1 <= (y=x*2^(2k)) < 4, then
* sqrt(x) = 2^k * sqrt(y)
* 2. Bit by bit computation
* Let q = sqrt(y) truncated to i bit after binary point (q = 1),
* i 0
* i+1 2
* s = 2*q , and y = 2 * ( y - q ). (1)
* i i i i
*
* To compute q from q , one checks whether
* i+1 i
*
* -(i+1) 2
* (q + 2 ) <= y. (2)
* i
* -(i+1)
* If (2) is false, then q = q ; otherwise q = q + 2 .
* i+1 i i+1 i
*
* With some algebric manipulation, it is not difficult to see
* that (2) is equivalent to
* -(i+1)
* s + 2 <= y (3)
* i i
*
* The advantage of (3) is that s and y can be computed by
* i i
* the following recurrence formula:
* if (3) is false
*
* s = s , y = y ; (4)
* i+1 i i+1 i
*
* otherwise,
* -i -(i+1)
* s = s + 2 , y = y - s - 2 (5)
* i+1 i i+1 i i
*
* One may easily use induction to prove (4) and (5).
* Note. Since the left hand side of (3) contain only i+2 bits,
* it does not necessary to do a full (53-bit) comparison
* in (3).
* 3. Final rounding
* After generating the 53 bits result, we compute one more bit.
* Together with the remainder, we can decide whether the
* result is exact, bigger than 1/2ulp, or less than 1/2ulp
* (it will never equal to 1/2ulp).
* The rounding mode can be detected by checking whether
* huge + tiny is equal to huge, and whether huge - tiny is
* equal to huge for some floating point number "huge" and "tiny".
*
* Special cases:
* sqrt(+-0) = +-0 ... exact
* sqrt(inf) = inf
* sqrt(-ve) = NaN ... with invalid signal
* sqrt(NaN) = NaN ... with invalid signal for signaling NaN
*
* Other methods : see the appended file at the end of the program below.
*---------------
*/
#if defined(__aarch64__) || defined(__x86_64__) || defined(__i386__)
/* hardware sqrt is available */
double js_sqrt(double x)
{
return sqrt(x);
}
#else
double js_sqrt(double x)
{
double z;
int sign = (int)0x80000000;
unsigned r,t1,s1,ix1,q1;
int ix0,s0,q,m,t,i;
EXTRACT_WORDS(ix0, ix1, x);
/* take care of Inf and NaN */
if((ix0&0x7ff00000)==0x7ff00000) {
return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
sqrt(-inf)=sNaN */
}
/* take care of zero */
if(ix0<=0) {
if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */
else if(ix0<0)
return (x-x)/(x-x); /* sqrt(-ve) = sNaN */
}
/* normalize x */
m = (ix0>>20);
if(m==0) { /* subnormal x */
while(ix0==0) {
m -= 21;
ix0 |= (ix1>>11); ix1 <<= 21;
}
for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1;
m -= i-1;
ix0 |= (ix1>>(32-i));
ix1 <<= i;
}
m -= 1023; /* unbias exponent */
ix0 = (ix0&0x000fffff)|0x00100000;
if(m&1){ /* odd m, double x to make it even */
ix0 += ix0 + ((ix1&sign)>>31);
ix1 += ix1;
}
m >>= 1; /* m = [m/2] */
/* generate sqrt(x) bit by bit */
ix0 += ix0 + ((ix1&sign)>>31);
ix1 += ix1;
q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
r = 0x00200000; /* r = moving bit from right to left */
while(r!=0) {
t = s0+r;
if(t<=ix0) {
s0 = t+r;
ix0 -= t;
q += r;
}
ix0 += ix0 + ((ix1&sign)>>31);
ix1 += ix1;
r>>=1;
}
r = sign;
while(r!=0) {
t1 = s1+r;
t = s0;
if((t<ix0)||((t==ix0)&&(t1<=ix1))) {
s1 = t1+r;
if(((t1&sign)==sign)&&(s1&sign)==0) s0 += 1;
ix0 -= t;
if (ix1 < t1) ix0 -= 1;
ix1 -= t1;
q1 += r;
}
ix0 += ix0 + ((ix1&sign)>>31);
ix1 += ix1;
r>>=1;
}
/* use floating add to find out rounding direction */
if((ix0|ix1)!=0) {
z = one-tiny; /* trigger inexact flag */
if (z>=one) {
z = one+tiny;
if (q1==(unsigned)0xffffffff) { q1=0; q += 1;}
else if (z>one) {
if (q1==(unsigned)0xfffffffe) q+=1;
q1+=2;
} else
q1 += (q1&1);
}
}
ix0 = (q>>1)+0x3fe00000;
ix1 = q1>>1;
if ((q&1)==1) ix1 |= sign;
ix0 += (m <<20);
return float64_from_u32(ix0, ix1);
}
#endif /* !hardware sqrt */
#endif /* USE_SOFTFLOAT */
/* to have smaller code */
/* n >= 1 */
/* return sum(x^i*coefs[i] with i = 0 ... n - 1 and n >= 1 using
Horner algorithm. */
static double eval_poly(double x, const double *coefs, int n)
{
double r;
int i;
r = coefs[n - 1];
for(i = n - 2; i >= 0; i--) {
r = r * x + coefs[i];
}
return r;
}
/* @(#)k_sin.c 1.3 95/01/18 */
/*
* ====================================================
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunSoft, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/
/* __kernel_sin( x, y, iy)
* kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
* Input x is assumed to be bounded by ~pi/4 in magnitude.
* Input y is the tail of x.
* Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
*
* Algorithm
* 1. Since sin(-x) = -sin(x), we need only to consider positive x.
* 2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0.
* 3. sin(x) is approximated by a polynomial of degree 13 on
* [0,pi/4]
* 3 13
* sin(x) ~ x + S1*x + ... + S6*x
* where
*
* |sin(x) 2 4 6 8 10 12 | -58
* |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
* | x |
*
* 4. sin(x+y) = sin(x) + sin'(x')*y
* ~ sin(x) + (1-x*x/2)*y
* For better accuracy, let
* 3 2 2 2 2
* r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
* then 3 2
* sin(x) = x + (S1*x + (x *(r-y/2)+y))
*/
static const double
S1 = -1.66666666666666324348e-01; /* 0xBFC55555, 0x55555549 */
static const double S_tab[] = {
/* S2 */ 8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */
/* S3 */ -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */
/* S4 */ 2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */
/* S5 */ -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */
/* S6 */ 1.58969099521155010221e-10, /* 0x3DE5D93A, 0x5ACFD57C */
};
/* iy=0 if y is zero */
static double __kernel_sin(double x, double y, int iy)
{
double z,r,v;
int ix;
ix = get_high_word(x)&0x7fffffff; /* high word of x */
if(ix<0x3e400000) /* |x| < 2**-27 */
{if((int)x==0) return x;} /* generate inexact */
z = x*x;
v = z*x;
r = eval_poly(z, S_tab, 5);
if(iy==0) return x+v*(S1+z*r);
else return x-((z*(half*y-v*r)-y)-v*S1);
}
/* @(#)k_cos.c 1.3 95/01/18 */
/*
* ====================================================
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunSoft, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/
/*
* __kernel_cos( x, y )
* kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
* Input x is assumed to be bounded by ~pi/4 in magnitude.
* Input y is the tail of x.
*
* Algorithm
* 1. Since cos(-x) = cos(x), we need only to consider positive x.
* 2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0.
* 3. cos(x) is approximated by a polynomial of degree 14 on
* [0,pi/4]
* 4 14
* cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
* where the remez error is
*
* | 2 4 6 8 10 12 14 | -58
* |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2
* | |
*
* 4 6 8 10 12 14
* 4. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then
* cos(x) = 1 - x*x/2 + r
* since cos(x+y) ~ cos(x) - sin(x)*y
* ~ cos(x) - x*y,
* a correction term is necessary in cos(x) and hence
* cos(x+y) = 1 - (x*x/2 - (r - x*y))
* For better accuracy when x > 0.3, let qx = |x|/4 with
* the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125.
* Then
* cos(x+y) = (1-qx) - ((x*x/2-qx) - (r-x*y)).
* Note that 1-qx and (x*x/2-qx) is EXACT here, and the
* magnitude of the latter is at least a quarter of x*x/2,
* thus, reducing the rounding error in the subtraction.
*/
static const double C_tab[] = {
/* C1 */ 4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */
/* C2 */ -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */
/* C3 */ 2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */
/* C4 */ -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */
/* C5 */ 2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */
/* C6 */ -1.13596475577881948265e-11, /* 0xBDA8FAE9, 0xBE8838D4 */
};
static double __kernel_cos(double x, double y)
{
double a,hz,z,r,qx;
int ix;
ix = get_high_word(x)&0x7fffffff; /* ix = |x|'s high word*/
if(ix<0x3e400000) { /* if x < 2**27 */
if(((int)x)==0) return one; /* generate inexact */
}
z = x*x;
r = z * eval_poly(z, C_tab, 6);
if(ix < 0x3FD33333) /* if |x| < 0.3 */
return one - (0.5*z - (z*r - x*y));
else {
if(ix > 0x3fe90000) { /* x > 0.78125 */
qx = 0.28125;
} else {
qx = float64_from_u32(ix-0x00200000, 0); /* x/4 */
}
hz = 0.5*z-qx;
a = one-qx;
return a - (hz - (z*r-x*y));
}
}
/* rem_pio2 */
#define T_LEN 19
/* T[i] = floor(2^(64*(T_LEN - i))/2pi) mod 2^64 */
static const uint64_t T[T_LEN] = {
0x1580cc11bf1edaea,
0x9afed7ec47e35742,
0xcf41ce7de294a4ba,
0x5d49eeb1faf97c5e,
0xd3d18fd9a797fa8b,
0xdb4d9fb3c9f2c26d,
0xfbcbc462d6829b47,
0xc7fe25fff7816603,
0x272117e2ef7e4a0e,
0x4e64758e60d4ce7d,
0x3a671c09ad17df90,
0xba208d7d4baed121,
0x3f877ac72c4a69cf,
0x01924bba82746487,
0x6dc91b8e909374b8,
0x7f9458eaf7aef158,
0x36d8a5664f10e410,
0x7f09d5f47d4d3770,
0x28be60db9391054a, /* high part */
};
/* PIO2[i] = floor(2^(64*(2 - i))*PI/4) mod 2^64 */
static const uint64_t PIO4[2] = {
0xc4c6628b80dc1cd1,
0xc90fdaa22168c234,
};
static uint64_t get_u64_at_bit(const uint64_t *tab, uint32_t tab_len,
uint32_t pos)
{
uint64_t v;
uint32_t p = pos / 64;
int shift = pos % 64;
v = tab[p] >> shift;
if (shift != 0 && (p + 1) < tab_len)
v |= tab[p + 1] << (64 - shift);
return v;
}
/* return n = round(x/(pi/2)) (only low 2 bits are valid) and
(y[0], y[1]) = x - (pi/2) * n.
'x' must be finite and such as abs(x) >= PI/4.
The initial algorithm comes from the CORE-MATH project.
*/
static int rem_pio2_large(double x, double *y)
{
uint64_t m;
int e, sgn, n, rnd, j, i, y_sgn;
uint64_t c[2], d[3];
uint64_t r0, r1;
uint32_t carry, carry1;
m = float64_as_uint64(x);
sgn = m >> 63;
e = (m >> 52) & 0x7ff;
/* 1022 <= e <= 2047 */
m = (m & (((uint64_t)1 << 52) - 1)) | ((uint64_t)1 << 52);
/* multiply m by T[j:j+192] */
j = T_LEN * 64 - (e - 1075) - 192;
/* 53 <= j <= 1077 */
// printf("m=0x%016" PRIx64 " e=%d j=%d\n", m, e, j);
for(i = 0; i < 3; i++) {
d[i] = get_u64_at_bit(T, T_LEN, j + i * 64);
}
r1 = mul_u64(&r0, m, d[0]);
c[0] = r1;
r1 = mul_u64(&r0, m, d[1]);
c[0] += r0;
carry = c[0] < r0;
c[1] = r1 + carry;
mul_u64(&r0, m, d[2]);
c[1] += r0;
// printf("c0=%016" PRIx64 " %016" PRIx64 "\n", c[1], c[0]);
/* n = round(c[1]/2^62) */
n = c[1] >> 62;
rnd = (c[1] >> 61) & 1;
n += rnd;
/* c = c * 4 - n */
c[1] = (c[1] << 2) | (c[0] >> 62);
c[0] = (c[0] << 2);
y_sgn = sgn;
if (rnd) {
/* 'y' sign change */
y_sgn ^= 1;
c[0] = ~c[0];
c[1] = ~c[1];
if (++c[0] == 0)
c[1]++;
}
// printf("c1=%016" PRIx64 " %016" PRIx64 " n=%d sgn=%d\n", c[1], c[0], n, sgn);
/* c = c * (PI/2) (high 128 bits of the product) */
r1 = mul_u64(&r0, c[0], PIO4[1]);
d[0] = r0;
d[1] = r1;
r1 = mul_u64(&r0, c[1], PIO4[0]);
d[0] += r0;
carry = d[0] < r0;
d[1] += r1;
carry1 = d[1] < r1;
d[1] += carry;
carry1 |= (d[1] < carry);
d[2] = carry1;
r1 = mul_u64(&r0, c[1], PIO4[1]);
d[1] += r0;
carry = d[1] < r0;
d[2] += r1 + carry;
/* convert d to two float64 */
// printf("d=%016" PRIx64 " %016" PRIx64 "\n", d[2], d[1]);
if (d[2] == 0) {
/* should never happen (see ARGUMENT REDUCTION FOR HUGE
ARGUMENTS: Good to the Last Bit, K. C. Ng and the members
of the FP group of SunPro */
y[0] = y[1] = 0;
} else {
uint64_t m0, m1;
int e1;
e = clz64(d[2]);
d[2] = (d[2] << e) | (d[1] >> (64 - e));
d[1] = (d[1] << e);
// printf("d=%016" PRIx64 " %016" PRIx64 " e=%d\n", d[2], d[1], e);
m0 = (d[2] >> 11) & (((uint64_t)1 << 52) - 1);
m1 = ((d[2] & 0x7ff) << 42) | (d[1] >> (64 - 42));
y[0] = uint64_as_float64(((uint64_t)y_sgn << 63) |
((uint64_t)(1023 - e) << 52) |
m0);
if (m1 == 0) {
y[1] = 0;
} else {
e1 = clz64(m1) - 11;
m1 = (m1 << e1) & (((uint64_t)1 << 52) - 1);
y[1] = uint64_as_float64(((uint64_t)y_sgn << 63) |
((uint64_t)(1023 - e - 53 - e1) << 52) |
m1);
}
}
if (sgn)
n = -n;
return n;
}
#ifdef USE_SOFTFLOAT
/* when using softfloat, the FP reduction should be not much faster
than the generic one */
int js_rem_pio2(double x, double *y)
{
int ix,hx;
hx = get_high_word(x); /* high word of x */
ix = hx&0x7fffffff;
if(ix<=0x3fe921fb) {
/* |x| ~<= pi/4 , no need for reduction */
y[0] = x;
y[1] = 0;
return 0;
}
/*
* all other (large) arguments
*/
if(ix>=0x7ff00000) { /* x is inf or NaN */
y[0]=y[1]=x-x;
return 0;
}
return rem_pio2_large(x, y);
}
#else
/*
* invpio2: 53 bits of 2/pi
* pio2_1: first 33 bit of pi/2
* pio2_1t: pi/2 - pio2_1
* pio2_2: second 33 bit of pi/2
* pio2_2t: pi/2 - (pio2_1+pio2_2)
* pio2_3: third 33 bit of pi/2
* pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3)
*/
static const double
invpio2 = 6.36619772367581382433e-01; /* 0x3FE45F30, 0x6DC9C883 */
static const double pio2_tab[3] = {
/* pio2_1 */ 1.57079632673412561417e+00, /* 0x3FF921FB, 0x54400000 */
/* pio2_2 */ 6.07710050630396597660e-11, /* 0x3DD0B461, 0x1A600000 */
/* pio2_3 */ 2.02226624871116645580e-21, /* 0x3BA3198A, 0x2E000000 */
};
static const double pio2_t_tab[3] = {
/* pio2_1t */ 6.07710050650619224932e-11, /* 0x3DD0B461, 0x1A626331 */
/* pio2_2t */ 2.02226624879595063154e-21, /* 0x3BA3198A, 0x2E037073 */
/* pio2_3t */ 8.47842766036889956997e-32, /* 0x397B839A, 0x252049C1 */
};
static uint8_t rem_pio2_emax[2] = { 16, 49 };
int js_rem_pio2(double x, double *y)
{
double w,t,r,fn;