-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathmath.ts
3209 lines (2979 loc) · 101 KB
/
math.ts
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
import * as JSMath from "./bindings/Math";
export { JSMath };
import {
pow_lut, exp_lut, exp2_lut, log_lut, log2_lut,
powf_lut, expf_lut, exp2f_lut, logf_lut, log2f_lut
} from "./util/math";
import {
abs as builtin_abs,
ceil as builtin_ceil,
clz as builtin_clz,
copysign as builtin_copysign,
floor as builtin_floor,
max as builtin_max,
min as builtin_min,
sqrt as builtin_sqrt,
trunc as builtin_trunc
} from "./builtins";
// SUN COPYRIGHT NOTICE
//
// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
// Developed at SunPro, a Sun Microsystems, Inc. business.
// Permission to use, copy, modify, and distribute this software
// is freely granted, provided that this notice is preserved.
//
// Applies to all functions marked with a comment referring here.
/** @internal */
// @ts-ignore: decorator
@lazy
var rempio2_y0: f64,
rempio2_y1: f64,
res128_hi: u64;
/** @internal */
// @ts-ignore: decorator
@lazy @inline
const PIO2_TABLE: StaticArray<u64> = [
0x00000000A2F9836E, 0x4E441529FC2757D1, 0xF534DDC0DB629599, 0x3C439041FE5163AB,
0xDEBBC561B7246E3A, 0x424DD2E006492EEA, 0x09D1921CFE1DEB1C, 0xB129A73EE88235F5,
0x2EBB4484E99C7026, 0xB45F7E413991D639, 0x835339F49C845F8B, 0xBDF9283B1FF897FF,
0xDE05980FEF2F118B, 0x5A0A6D1F6D367ECF, 0x27CB09B74F463F66, 0x9E5FEA2D7527BAC7,
0xEBE5F17B3D0739F7, 0x8A5292EA6BFB5FB1, 0x1F8D5D0856033046, 0xFC7B6BABF0CFBC20,
0x9AF4361DA9E39161, 0x5EE61B086599855F, 0x14A068408DFFD880, 0x4D73273106061557
];
/** @internal */
function R(z: f64): f64 { // Rational approximation of (asin(x)-x)/x^3
const // see: musl/src/math/asin.c and SUN COPYRIGHT NOTICE above
pS0 = reinterpret<f64>(0x3FC5555555555555), // 1.66666666666666657415e-01
pS1 = reinterpret<f64>(0xBFD4D61203EB6F7D), // -3.25565818622400915405e-01
pS2 = reinterpret<f64>(0x3FC9C1550E884455), // 2.01212532134862925881e-01
pS3 = reinterpret<f64>(0xBFA48228B5688F3B), // -4.00555345006794114027e-02
pS4 = reinterpret<f64>(0x3F49EFE07501B288), // 7.91534994289814532176e-04
pS5 = reinterpret<f64>(0x3F023DE10DFDF709), // 3.47933107596021167570e-05
qS1 = reinterpret<f64>(0xC0033A271C8A2D4B), // -2.40339491173441421878e+00
qS2 = reinterpret<f64>(0x40002AE59C598AC8), // 2.02094576023350569471e+00
qS3 = reinterpret<f64>(0xBFE6066C1B8D0159), // -6.88283971605453293030e-01
qS4 = reinterpret<f64>(0x3FB3B8C5B12E9282); // 7.70381505559019352791e-02
var p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5)))));
var q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4)));
return p / q;
}
/** @internal */
// @ts-ignore: decorator
@inline
function expo2(x: f64): f64 { // exp(x)/2 for x >= log(DBL_MAX)
const // see: musl/src/math/__expo2.c
k = <u32>2043,
kln2 = reinterpret<f64>(0x40962066151ADD8B); // 0x1.62066151add8bp+10
var scale = reinterpret<f64>(<u64>((<u32>0x3FF + k / 2) << 20) << 32);
return NativeMath.exp(x - kln2) * scale * scale;
}
/** @internal */
/* Helper function to eventually get bits of π/2 * |x|
*
* y = π/4 * (frac << clz(frac) >> 11)
* return clz(frac)
*
* Right shift 11 bits to make upper half fit in `double`
*/
// @ts-ignore: decorator
@inline
function pio2_right(q0: u64, q1: u64): u64 { // see: jdh8/metallic/blob/master/src/math/double/rem_pio2.c
// Bits of π/4
const p0: u64 = 0xC4C6628B80DC1CD1;
const p1: u64 = 0xC90FDAA22168C234;
const Ox1p_64 = reinterpret<f64>(0x3BF0000000000000); // 0x1p-64
const Ox1p_75 = reinterpret<f64>(0x3B40000000000000); // 0x1p-75
var shift = clz(q1);
q1 = q1 << shift | q0 >> (64 - shift);
q0 <<= shift;
var lo = umuldi(p1, q1);
var hi = res128_hi;
var ahi = hi >> 11;
var alo = lo >> 11 | hi << 53;
var blo = <u64>(Ox1p_75 * <f64>p0 * <f64>q1 + Ox1p_75 * <f64>p1 * <f64>q0);
rempio2_y0 = <f64>(ahi + u64(lo < blo));
rempio2_y1 = Ox1p_64 * <f64>(alo + blo);
return shift;
}
/** @internal */
// @ts-ignore: decorator
@inline
function umuldi(u: u64, v: u64): u64 {
var u1: u64 , v1: u64, w0: u64, w1: u64, t: u64;
u1 = u & 0xFFFFFFFF;
v1 = v & 0xFFFFFFFF;
u >>= 32;
v >>= 32;
t = u1 * v1;
w0 = t & 0xFFFFFFFF;
t = u * v1 + (t >> 32);
w1 = t >> 32;
t = u1 * v + (t & 0xFFFFFFFF);
res128_hi = u * v + w1 + (t >> 32);
return (t << 32) + w0;
}
/** @internal */
function pio2_large_quot(x: f64, u: i64): i32 { // see: jdh8/metallic/blob/master/src/math/double/rem_pio2.c
const bits = changetype<usize>(PIO2_TABLE);
var magnitude = u & 0x7FFFFFFFFFFFFFFF;
var offset = (magnitude >> 52) - 1045;
var shift = offset & 63;
var tblPtr = bits + (<i32>(offset >> 6) << 3);
var s0: u64, s1: u64, s2: u64;
var b0 = load<u64>(tblPtr, 0 << 3);
var b1 = load<u64>(tblPtr, 1 << 3);
var b2 = load<u64>(tblPtr, 2 << 3);
// Get 192 bits of 0x1p-31 / π with `offset` bits skipped
if (shift) {
let rshift = 64 - shift;
let b3 = load<u64>(tblPtr, 3 << 3);
s0 = b1 >> rshift | b0 << shift;
s1 = b2 >> rshift | b1 << shift;
s2 = b3 >> rshift | b2 << shift;
} else {
s0 = b0;
s1 = b1;
s2 = b2;
}
var significand = (u & 0x000FFFFFFFFFFFFF) | 0x0010000000000000;
// First 128 bits of fractional part of x/(2π)
var blo = umuldi(s1, significand);
var bhi = res128_hi;
var ahi = s0 * significand;
var clo = (s2 >> 32) * (significand >> 32);
var plo = blo + clo;
var phi = ahi + bhi + u64(plo < clo);
// r: u128 = p << 2
var rlo = plo << 2;
var rhi = phi << 2 | plo >> 62;
// s: i128 = r >> 127
var slo = <i64>rhi >> 63;
var shi = slo >> 1;
var q = (<i64>phi >> 62) - slo;
var shifter = 0x3CB0000000000000 - (pio2_right(rlo ^ slo, rhi ^ shi) << 52);
var signbit = (u ^ rhi) & 0x8000000000000000;
var coeff = reinterpret<f64>(shifter | signbit);
rempio2_y0 *= coeff;
rempio2_y1 *= coeff;
return <i32>q;
}
/** @internal */
// @ts-ignore: decorator
@inline
function rempio2(x: f64, u: u64, sign: i32): i32 {
const pio2_1 = reinterpret<f64>(0x3FF921FB54400000); // 1.57079632673412561417e+00
const pio2_1t = reinterpret<f64>(0x3DD0B4611A626331); // 6.07710050650619224932e-11
const pio2_2 = reinterpret<f64>(0x3DD0B4611A600000); // 6.07710050630396597660e-11
const pio2_2t = reinterpret<f64>(0x3BA3198A2E037073); // 2.02226624879595063154e-21
const pio2_3 = reinterpret<f64>(0x3BA3198A2E000000); // 2.02226624871116645580e-21
const pio2_3t = reinterpret<f64>(0x397B839A252049C1); // 8.47842766036889956997e-32
const invpio2 = reinterpret<f64>(0x3FE45F306DC9C883); // 0.63661977236758134308
var ix = <u32>(u >> 32) & 0x7FFFFFFF;
if (ASC_SHRINK_LEVEL < 1) {
if (ix < 0x4002D97C) { // |x| < 3pi/4, special case with n=+-1
let q = 1, z: f64, y0: f64, y1: f64;
if (!sign) {
z = x - pio2_1;
if (ix != 0x3FF921FB) { // 33+53 bit pi is good enough
y0 = z - pio2_1t;
y1 = (z - y0) - pio2_1t;
} else { // near pi/2, use 33+33+53 bit pi
z -= pio2_2;
y0 = z - pio2_2t;
y1 = (z - y0) - pio2_2t;
}
} else { // negative x
z = x + pio2_1;
if (ix != 0x3FF921FB) { // 33+53 bit pi is good enough
y0 = z + pio2_1t;
y1 = (z - y0) + pio2_1t;
} else { // near pi/2, use 33+33+53 bit pi
z += pio2_2;
y0 = z + pio2_2t;
y1 = (z - y0) + pio2_2t;
}
q = -1;
}
rempio2_y0 = y0;
rempio2_y1 = y1;
return q;
}
}
if (ix < 0x413921FB) { // |x| ~< 2^20*pi/2 (1647099)
// Use precise Cody Waite scheme
let q = nearest(x * invpio2);
let r = x - q * pio2_1;
let w = q * pio2_1t; // 1st round good to 85 bit
let j = ix >> 20;
let y0 = r - w;
let hi = <u32>(reinterpret<u64>(y0) >> 32);
let i = j - ((hi >> 20) & 0x7FF);
if (i > 16) { // 2nd iteration needed, good to 118
let t = r;
w = q * pio2_2;
r = t - w;
w = q * pio2_2t - ((t - r) - w);
y0 = r - w;
hi = <u32>(reinterpret<u64>(y0) >> 32);
i = j - ((hi >> 20) & 0x7FF);
if (i > 49) { // 3rd iteration need, 151 bits acc
let t = r;
w = q * pio2_3;
r = t - w;
w = q * pio2_3t - ((t - r) - w);
y0 = r - w;
}
}
let y1 = (r - y0) - w;
rempio2_y0 = y0;
rempio2_y1 = y1;
return <i32>q;
}
var q = pio2_large_quot(x, u);
return select(-q, q, sign);
}
/** @internal */
// @ts-ignore: decorator
@inline
function sin_kern(x: f64, y: f64, iy: i32): f64 { // see: musl/tree/src/math/__sin.c
const S1 = reinterpret<f64>(0xBFC5555555555549); // -1.66666666666666324348e-01
const S2 = reinterpret<f64>(0x3F8111111110F8A6); // 8.33333333332248946124e-03
const S3 = reinterpret<f64>(0xBF2A01A019C161D5); // -1.98412698298579493134e-04
const S4 = reinterpret<f64>(0x3EC71DE357B1FE7D); // 2.75573137070700676789e-06
const S5 = reinterpret<f64>(0xBE5AE5E68A2B9CEB); // -2.50507602534068634195e-08
const S6 = reinterpret<f64>(0x3DE5D93A5ACFD57C); // 1.58969099521155010221e-10
var z = x * x;
var w = z * z;
var r = S2 + z * (S3 + z * S4) + z * w * (S5 + z * S6);
var v = z * x;
if (!iy) {
return x + v * (S1 + z * r);
} else {
return x - ((z * (0.5 * y - v * r) - y) - v * S1);
}
}
/** @internal */
// @ts-ignore: decorator
@inline
function cos_kern(x: f64, y: f64): f64 { // see: musl/tree/src/math/__cos.c
const C1 = reinterpret<f64>(0x3FA555555555554C); // 4.16666666666666019037e-02
const C2 = reinterpret<f64>(0xBF56C16C16C15177); // -1.38888888888741095749e-03
const C3 = reinterpret<f64>(0x3EFA01A019CB1590); // 2.48015872894767294178e-05
const C4 = reinterpret<f64>(0xBE927E4F809C52AD); // -2.75573143513906633035e-07
const C5 = reinterpret<f64>(0x3E21EE9EBDB4B1C4); // 2.08757232129817482790e-09
const C6 = reinterpret<f64>(0xBDA8FAE9BE8838D4); // -1.13596475577881948265e-11
var z = x * x;
var w = z * z;
var r = z * (C1 + z * (C2 + z * C3)) + w * w * (C4 + z * (C5 + z * C6));
var hz = 0.5 * z;
w = 1.0 - hz;
return w + (((1.0 - w) - hz) + (z * r - x * y));
}
/** @internal */
function tan_kern(x: f64, y: f64, iy: i32): f64 { // see: src/lib/msun/src/k_tan.c
const T0 = reinterpret<f64>(0x3FD5555555555563); // 3.33333333333334091986e-01
const T1 = reinterpret<f64>(0x3FC111111110FE7A); // 1.33333333333201242699e-01
const T2 = reinterpret<f64>(0x3FABA1BA1BB341FE); // 5.39682539762260521377e-02
const T3 = reinterpret<f64>(0x3F9664F48406D637); // 2.18694882948595424599e-02
const T4 = reinterpret<f64>(0x3F8226E3E96E8493); // 8.86323982359930005737e-03
const T5 = reinterpret<f64>(0x3F6D6D22C9560328); // 3.59207910759131235356e-03
const T6 = reinterpret<f64>(0x3F57DBC8FEE08315); // 1.45620945432529025516e-03
const T7 = reinterpret<f64>(0x3F4344D8F2F26501); // 5.88041240820264096874e-04
const T8 = reinterpret<f64>(0x3F3026F71A8D1068); // 2.46463134818469906812e-04
const T9 = reinterpret<f64>(0x3F147E88A03792A6); // 7.81794442939557092300e-05
const T10 = reinterpret<f64>(0x3F12B80F32F0A7E9); // 7.14072491382608190305e-05
const T11 = reinterpret<f64>(0xBEF375CBDB605373); // -1.85586374855275456654e-05
const T12 = reinterpret<f64>(0x3EFB2A7074BF7AD4); // 2.59073051863633712884e-05
const one = reinterpret<f64>(0x3FF0000000000000); // 1.00000000000000000000e+00
const pio4 = reinterpret<f64>(0x3FE921FB54442D18); // 7.85398163397448278999e-01
const pio4lo = reinterpret<f64>(0x3C81A62633145C07); // 3.06161699786838301793e-17
var z: f64, r: f64, v: f64, w: f64, s: f64;
var hx = <i32>(reinterpret<u64>(x) >> 32); // high word of x
var ix = hx & 0x7FFFFFFF; // high word of |x|
var big = ix >= 0x3FE59428;
if (big) { // |x| >= 0.6744
if (hx < 0) { x = -x, y = -y; }
z = pio4 - x;
w = pio4lo - y;
x = z + w;
y = 0.0;
}
z = x * x;
w = z * z;
r = T1 + w * (T3 + w * (T5 + w * (T7 + w * (T9 + w * T11))));
v = z * (T2 + w * (T4 + w * (T6 + w * (T8 + w * (T10 + w * T12)))));
s = z * x;
r = y + z * (s * (r + v) + y);
r += T0 * s;
w = x + r;
if (big) {
v = iy;
return (1 - ((hx >> 30) & 2)) * (v - 2.0 * (x - (w * w / (w + v) - r)));
}
if (iy == 1) return w;
var a: f64, t: f64;
z = w;
z = reinterpret<f64>(reinterpret<u64>(z) & 0xFFFFFFFF00000000);
v = r - (z - x); // z + v = r + x
t = a = -one / w; // a = -1.0 / w
t = reinterpret<f64>(reinterpret<u64>(t) & 0xFFFFFFFF00000000);
s = one + t * z;
return t + a * (s + t * v);
}
/** @internal */
function dtoi32(x: f64): i32 {
if (ASC_SHRINK_LEVEL > 0) {
const inv32 = 1.0 / 4294967296;
return <i32><i64>(x - 4294967296 * floor(x * inv32));
} else {
let result = 0;
let u = reinterpret<u64>(x);
let e = (u >> 52) & 0x7FF;
if (e <= 1023 + 30) {
result = <i32>x;
} else if (e <= 1023 + 30 + 53) {
let v = (u & ((<u64>1 << 52) - 1)) | (<u64>1 << 52);
v = v << e - 1023 - 52 + 32;
result = <i32>(v >> 32);
result = select<i32>(-result, result, u >> 63);
}
return result;
}
}
// @ts-ignore: decorator
@lazy
var random_seeded = false;
// @ts-ignore: decorator
@lazy
var random_state0_64: u64;
// @ts-ignore: decorator
@lazy
var random_state1_64: u64;
// @ts-ignore: decorator
@lazy
var random_state0_32: u32;
// @ts-ignore: decorator
@lazy
var random_state1_32: u32;
function murmurHash3(h: u64): u64 { // Force all bits of a hash block to avalanche
h ^= h >> 33; // see: https://github.com/aappleby/smhasher
h *= 0xFF51AFD7ED558CCD;
h ^= h >> 33;
h *= 0xC4CEB9FE1A85EC53;
h ^= h >> 33;
return h;
}
function splitMix32(h: u32): u32 {
h += 0x6D2B79F5;
h = (h ^ (h >> 15)) * (h | 1);
h ^= h + (h ^ (h >> 7)) * (h | 61);
return h ^ (h >> 14);
}
export namespace NativeMath {
// @ts-ignore: decorator
@lazy
export const E = reinterpret<f64>(0x4005BF0A8B145769); // 2.7182818284590452354
// @ts-ignore: decorator
@lazy
export const LN2 = reinterpret<f64>(0x3FE62E42FEFA39EF); // 0.69314718055994530942
// @ts-ignore: decorator
@lazy
export const LN10 = reinterpret<f64>(0x40026BB1BBB55516); // 2.30258509299404568402
// @ts-ignore: decorator
@lazy
export const LOG2E = reinterpret<f64>(0x3FF71547652B82FE); // 1.4426950408889634074
// @ts-ignore: decorator
@lazy
export const LOG10E = reinterpret<f64>(0x3FDBCB7B1526E50E); // 0.43429448190325182765
// @ts-ignore: decorator
@lazy
export const PI = reinterpret<f64>(0x400921FB54442D18); // 3.14159265358979323846
// @ts-ignore: decorator
@lazy
export const SQRT1_2 = reinterpret<f64>(0x3FE6A09E667F3BCD); // 0.70710678118654752440
// @ts-ignore: decorator
@lazy
export const SQRT2 = reinterpret<f64>(0x3FF6A09E667F3BCD); // 1.41421356237309504880
// @ts-ignore: decorator
@lazy
export var sincos_sin: f64 = 0;
// @ts-ignore: decorator
@lazy
export var sincos_cos: f64 = 0;
// @ts-ignore: decorator
@inline export function abs(x: f64): f64 {
return builtin_abs<f64>(x);
}
export function acos(x: f64): f64 { // see: musl/src/math/acos.c and SUN COPYRIGHT NOTICE above
const
pio2_hi = reinterpret<f64>(0x3FF921FB54442D18), // 1.57079632679489655800e+00
pio2_lo = reinterpret<f64>(0x3C91A62633145C07), // 6.12323399573676603587e-17
Ox1p_120f = reinterpret<f32>(0x03800000);
var hx = <u32>(reinterpret<u64>(x) >> 32);
var ix = hx & 0x7FFFFFFF;
if (ix >= 0x3FF00000) {
let lx = <u32>reinterpret<u64>(x);
if ((ix - 0x3FF00000 | lx) == 0) {
if (hx >> 31) return 2 * pio2_hi + Ox1p_120f;
return 0;
}
return 0 / (x - x);
}
if (ix < 0x3FE00000) {
if (ix <= 0x3C600000) return pio2_hi + Ox1p_120f;
return pio2_hi - (x - (pio2_lo - x * R(x * x)));
}
var s: f64, w: f64, z: f64;
if (hx >> 31) {
// z = (1.0 + x) * 0.5;
z = 0.5 + x * 0.5;
s = builtin_sqrt<f64>(z);
w = R(z) * s - pio2_lo;
return 2 * (pio2_hi - (s + w));
}
// z = (1.0 - x) * 0.5;
z = 0.5 - x * 0.5;
s = builtin_sqrt<f64>(z);
var df = reinterpret<f64>(reinterpret<u64>(s) & 0xFFFFFFFF00000000);
var c = (z - df * df) / (s + df);
w = R(z) * s + c;
return 2 * (df + w);
}
export function acosh(x: f64): f64 { // see: musl/src/math/acosh.c
const s = reinterpret<f64>(0x3FE62E42FEFA39EF);
var e = reinterpret<u64>(x) >> 52 & 0x7FF;
if (e < 0x3FF + 1) return log1p(x - 1 + builtin_sqrt<f64>((x - 1) * (x - 1) + 2 * (x - 1)));
if (e < 0x3FF + 26) return log(2 * x - 1 / (x + builtin_sqrt<f64>(x * x - 1)));
return log(x) + s;
}
export function asin(x: f64): f64 { // see: musl/src/math/asin.c and SUN COPYRIGHT NOTICE above
const
pio2_hi = reinterpret<f64>(0x3FF921FB54442D18), // 1.57079632679489655800e+00
pio2_lo = reinterpret<f64>(0x3C91A62633145C07), // 6.12323399573676603587e-17
Ox1p_120f = reinterpret<f32>(0x03800000);
var hx = <u32>(reinterpret<u64>(x) >> 32);
var ix = hx & 0x7FFFFFFF;
if (ix >= 0x3FF00000) {
let lx = <u32>reinterpret<u64>(x);
if ((ix - 0x3FF00000 | lx) == 0) return x * pio2_hi + Ox1p_120f;
return 0 / (x - x);
}
if (ix < 0x3FE00000) {
if (ix < 0x3E500000 && ix >= 0x00100000) return x;
return x + x * R(x * x);
}
// var z = (1.0 - builtin_abs<f64>(x)) * 0.5;
var z = 0.5 - builtin_abs<f64>(x) * 0.5;
var s = builtin_sqrt<f64>(z);
var r = R(z);
if (ix >= 0x3FEF3333) x = pio2_hi - (2 * (s + s * r) - pio2_lo);
else {
let f = reinterpret<f64>(reinterpret<u64>(s) & 0xFFFFFFFF00000000);
let c = (z - f * f) / (s + f);
x = 0.5 * pio2_hi - (2 * s * r - (pio2_lo - 2 * c) - (0.5 * pio2_hi - 2 * f));
}
if (hx >> 31) return -x;
return x;
}
export function asinh(x: f64): f64 { // see: musl/src/math/asinh.c
const c = reinterpret<f64>(0x3FE62E42FEFA39EF); // 0.693147180559945309417232121458176568
var u = reinterpret<u64>(x);
var e = u >> 52 & 0x7FF;
var y = reinterpret<f64>(u & 0x7FFFFFFFFFFFFFFF);
if (e >= 0x3FF + 26) y = log(y) + c;
else if (e >= 0x3FF + 1) y = log(2 * y + 1 / (builtin_sqrt<f64>(y * y + 1) + y));
else if (e >= 0x3FF - 26) y = log1p(y + y * y / (builtin_sqrt<f64>(y * y + 1) + 1));
return builtin_copysign(y, x);
}
export function atan(x: f64): f64 { // see musl/src/math/atan.c and SUN COPYRIGHT NOTICE above
const
atanhi0 = reinterpret<f64>(0x3FDDAC670561BB4F), // 4.63647609000806093515e-01
atanhi1 = reinterpret<f64>(0x3FE921FB54442D18), // 7.85398163397448278999e-01
atanhi2 = reinterpret<f64>(0x3FEF730BD281F69B), // 9.82793723247329054082e-01
atanhi3 = reinterpret<f64>(0x3FF921FB54442D18), // 1.57079632679489655800e+00
atanlo0 = reinterpret<f64>(0x3C7A2B7F222F65E2), // 2.26987774529616870924e-17
atanlo1 = reinterpret<f64>(0x3C81A62633145C07), // 3.06161699786838301793e-17
atanlo2 = reinterpret<f64>(0x3C7007887AF0CBBD), // 1.39033110312309984516e-17
atanlo3 = reinterpret<f64>(0x3C91A62633145C07), // 6.12323399573676603587e-17
aT0 = reinterpret<f64>(0x3FD555555555550D), // 3.33333333333329318027e-01
aT1 = reinterpret<f64>(0xBFC999999998EBC4), // -1.99999999998764832476e-01
aT2 = reinterpret<f64>(0x3FC24924920083FF), // 1.42857142725034663711e-01
aT3 = reinterpret<f64>(0xBFBC71C6FE231671), // -1.11111104054623557880e-01,
aT4 = reinterpret<f64>(0x3FB745CDC54C206E), // 9.09088713343650656196e-02
aT5 = reinterpret<f64>(0xBFB3B0F2AF749A6D), // -7.69187620504482999495e-02
aT6 = reinterpret<f64>(0x3FB10D66A0D03D51), // 6.66107313738753120669e-02
aT7 = reinterpret<f64>(0xBFADDE2D52DEFD9A), // -5.83357013379057348645e-02
aT8 = reinterpret<f64>(0x3FA97B4B24760DEB), // 4.97687799461593236017e-02
aT9 = reinterpret<f64>(0xBFA2B4442C6A6C2F), // -3.65315727442169155270e-02
aT10 = reinterpret<f64>(0x3F90AD3AE322DA11), // 1.62858201153657823623e-02
Ox1p_120f = reinterpret<f32>(0x03800000);
var ix = <u32>(reinterpret<u64>(x) >> 32);
var sx = x;
ix &= 0x7FFFFFFF;
var z: f64;
if (ix >= 0x44100000) {
if (isNaN(x)) return x;
z = atanhi3 + Ox1p_120f;
return builtin_copysign<f64>(z, sx);
}
var id: i32;
if (ix < 0x3FDC0000) {
if (ix < 0x3E400000) return x;
id = -1;
} else {
x = builtin_abs<f64>(x);
if (ix < 0x3FF30000) {
if (ix < 0x3FE60000) {
id = 0;
x = (2.0 * x - 1.0) / (2.0 + x);
} else {
id = 1;
x = (x - 1.0) / (x + 1.0);
}
} else {
if (ix < 0x40038000) {
id = 2;
x = (x - 1.5) / (1.0 + 1.5 * x);
} else {
id = 3;
x = -1.0 / x;
}
}
}
z = x * x;
var w = z * z;
var s1 = z * (aT0 + w * (aT2 + w * (aT4 + w * (aT6 + w * (aT8 + w * aT10)))));
var s2 = w * (aT1 + w * (aT3 + w * (aT5 + w * (aT7 + w * aT9))));
var s3 = x * (s1 + s2);
if (id < 0) return x - s3;
switch (id) {
case 0: { z = atanhi0 - ((s3 - atanlo0) - x); break; }
case 1: { z = atanhi1 - ((s3 - atanlo1) - x); break; }
case 2: { z = atanhi2 - ((s3 - atanlo2) - x); break; }
case 3: { z = atanhi3 - ((s3 - atanlo3) - x); break; }
default: unreachable();
}
return builtin_copysign<f64>(z, sx);
}
export function atanh(x: f64): f64 { // see: musl/src/math/atanh.c
var u = reinterpret<u64>(x);
var e = u >> 52 & 0x7FF;
var y = builtin_abs(x);
if (e < 0x3FF - 1) {
if (e >= 0x3FF - 32) y = 0.5 * log1p(2 * y + 2 * y * y / (1 - y));
} else {
y = 0.5 * log1p(2 * (y / (1 - y)));
}
return builtin_copysign<f64>(y, x);
}
export function atan2(y: f64, x: f64): f64 { // see: musl/src/math/atan2.c and SUN COPYRIGHT NOTICE above
const pi_lo = reinterpret<f64>(0x3CA1A62633145C07); // 1.2246467991473531772E-16
if (isNaN(x) || isNaN(y)) return x + y;
var u = reinterpret<u64>(x);
var ix = <u32>(u >> 32);
var lx = <u32>u;
u = reinterpret<u64>(y);
var iy = <u32>(u >> 32);
var ly = <u32>u;
if ((ix - 0x3FF00000 | lx) == 0) return atan(y);
var m = ((iy >> 31) & 1) | ((ix >> 30) & 2);
ix = ix & 0x7FFFFFFF;
iy = iy & 0x7FFFFFFF;
if ((iy | ly) == 0) {
switch (m) {
case 0:
case 1: return y;
case 2: return PI;
case 3: return -PI;
}
}
if ((ix | lx) == 0) return m & 1 ? -PI / 2 : PI / 2;
if (ix == 0x7FF00000) {
if (iy == 0x7FF00000) {
let t = m & 2 ? 3 * PI / 4 : PI / 4;
return m & 1 ? -t : t;
} else {
let t = m & 2 ? PI : 0;
return m & 1 ? -t : t;
}
}
var z: f64;
if (ix + (64 << 20) < iy || iy == 0x7FF00000) return m & 1 ? -PI / 2 : PI / 2;
if ((m & 2) && iy + (64 << 20) < ix) z = 0;
else z = atan(builtin_abs<f64>(y / x));
switch (m) {
case 0: return z;
case 1: return -z;
case 2: return PI - (z - pi_lo);
case 3: return (z - pi_lo) - PI;
}
unreachable();
return 0;
}
export function cbrt(x: f64): f64 { // see: musl/src/math/cbrt.c and SUN COPYRIGHT NOTICE above
const
B1 = <u32>715094163,
B2 = <u32>696219795,
P0 = reinterpret<f64>(0x3FFE03E60F61E692), // 1.87595182427177009643
P1 = reinterpret<f64>(0xBFFE28E092F02420), // -1.88497979543377169875
P2 = reinterpret<f64>(0x3FF9F1604A49D6C2), // 1.621429720105354466140
P3 = reinterpret<f64>(0xBFE844CBBEE751D9), // -0.758397934778766047437
P4 = reinterpret<f64>(0x3FC2B000D4E4EDD7), // 0.145996192886612446982
Ox1p54 = reinterpret<f64>(0x4350000000000000);
var u = reinterpret<u64>(x);
var hx = <u32>(u >> 32) & 0x7FFFFFFF;
if (hx >= 0x7FF00000) return x + x;
if (hx < 0x00100000) {
u = reinterpret<u64>(x * Ox1p54);
hx = <u32>(u >> 32) & 0x7FFFFFFF;
if (hx == 0) return x;
hx = hx / 3 + B2;
} else {
hx = hx / 3 + B1;
}
u &= 1 << 63;
u |= <u64>hx << 32;
var t = reinterpret<f64>(u);
var r = (t * t) * (t / x);
t = t * ((P0 + r * (P1 + r * P2)) + ((r * r) * r) * (P3 + r * P4));
t = reinterpret<f64>((reinterpret<u64>(t) + 0x80000000) & 0xFFFFFFFFC0000000);
var s = t * t;
r = x / s;
r = (r - t) / (2 * t + r);
t = t + t * r;
return t;
}
// @ts-ignore: decorator
@inline
export function ceil(x: f64): f64 {
return builtin_ceil<f64>(x);
}
export function clz32(x: f64): f64 {
if (!isFinite(x)) return 32;
/*
* Wasm (MVP) and JS have different approaches for double->int conversions.
*
* For emulate JS conversion behavior and avoid trapping from wasm we should modulate by MAX_INT
* our float-point arguments before actual convertion to integers.
*/
return builtin_clz(dtoi32(x));
}
export function cos(x: f64): f64 { // see: musl/src/math/cos.c
var u = reinterpret<u64>(x);
var ix = <u32>(u >> 32);
var sign = ix >> 31;
ix &= 0x7FFFFFFF;
// |x| ~< pi/4
if (ix <= 0x3FE921FB) {
if (ix < 0x3E46A09E) { // |x| < 2**-27 * sqrt(2)
return 1.0;
}
return cos_kern(x, 0);
}
// sin(Inf or NaN) is NaN
if (ix >= 0x7FF00000) return x - x;
// argument reduction needed
var n = rempio2(x, u, sign);
var y0 = rempio2_y0;
var y1 = rempio2_y1;
x = n & 1 ? sin_kern(y0, y1, 1) : cos_kern(y0, y1);
return (n + 1) & 2 ? -x : x;
}
export function cosh(x: f64): f64 { // see: musl/src/math/cosh.c
var u = reinterpret<u64>(x);
u &= 0x7FFFFFFFFFFFFFFF;
x = reinterpret<f64>(u);
var w = <u32>(u >> 32);
var t: f64;
if (w < 0x3FE62E42) {
if (w < 0x3FF00000 - (26 << 20)) return 1;
t = expm1(x);
// return 1 + t * t / (2 * (1 + t));
return 1 + t * t / (2 + 2 * t);
}
if (w < 0x40862E42) {
t = exp(x);
return 0.5 * (t + 1 / t);
}
t = expo2(x);
return t;
}
export function exp(x: f64): f64 { // see: musl/src/math/exp.c and SUN COPYRIGHT NOTICE above
if (ASC_SHRINK_LEVEL < 1) {
return exp_lut(x);
} else {
const
ln2hi = reinterpret<f64>(0x3FE62E42FEE00000), // 6.93147180369123816490e-01
ln2lo = reinterpret<f64>(0x3DEA39EF35793C76), // 1.90821492927058770002e-10
invln2 = reinterpret<f64>(0x3FF71547652B82FE), // 1.44269504088896338700e+00
P1 = reinterpret<f64>(0x3FC555555555553E), // 1.66666666666666019037e-01
P2 = reinterpret<f64>(0xBF66C16C16BEBD93), // -2.77777777770155933842e-03
P3 = reinterpret<f64>(0x3F11566AAF25DE2C), // 6.61375632143793436117e-05
P4 = reinterpret<f64>(0xBEBBBD41C5D26BF1), // -1.65339022054652515390e-06
P5 = reinterpret<f64>(0x3E66376972BEA4D0), // 4.13813679705723846039e-08
overflow = reinterpret<f64>(0x40862E42FEFA39EF), // 709.782712893383973096
underflow = reinterpret<f64>(0xC0874910D52D3051), // -745.13321910194110842
Ox1p1023 = reinterpret<f64>(0x7FE0000000000000);
let hx = <u32>(reinterpret<u64>(x) >> 32);
let sign_ = <i32>(hx >> 31);
hx &= 0x7FFFFFFF;
if (hx >= 0x4086232B) {
if (isNaN(x)) return x;
if (x > overflow) return x * Ox1p1023;
if (x < underflow) return 0;
}
let hi: f64, lo: f64 = 0;
let k = 0;
if (hx > 0x3FD62E42) {
if (hx >= 0x3FF0A2B2) {
k = <i32>(invln2 * x + builtin_copysign<f64>(0.5, x));
} else {
k = 1 - (sign_ << 1);
}
hi = x - k * ln2hi;
lo = k * ln2lo;
x = hi - lo;
} else if (hx > 0x3E300000) {
hi = x;
} else return 1.0 + x;
let xs = x * x;
// var c = x - xp2 * (P1 + xp2 * (P2 + xp2 * (P3 + xp2 * (P4 + xp2 * P5))));
let xq = xs * xs;
let c = x - (xs * P1 + xq * ((P2 + xs * P3) + xq * (P4 + xs * P5)));
let y = 1.0 + (x * c / (2 - c) - lo + hi);
return k == 0 ? y : scalbn(y, k);
}
}
export function exp2(x: f64): f64 {
return exp2_lut(x);
}
export function expm1(x: f64): f64 { // see: musl/src/math/expm1.c and SUN COPYRIGHT NOTICE above
const
o_threshold = reinterpret<f64>(0x40862E42FEFA39EF), // 7.09782712893383973096e+02
ln2_hi = reinterpret<f64>(0x3FE62E42FEE00000), // 6.93147180369123816490e-01
ln2_lo = reinterpret<f64>(0x3DEA39EF35793C76), // 1.90821492927058770002e-10
invln2 = reinterpret<f64>(0x3FF71547652B82FE), // 1.44269504088896338700e+00
Q1 = reinterpret<f64>(0xBFA11111111110F4), // -3.33333333333331316428e-02
Q2 = reinterpret<f64>(0x3F5A01A019FE5585), // 1.58730158725481460165e-03
Q3 = reinterpret<f64>(0xBF14CE199EAADBB7), // -7.93650757867487942473e-05
Q4 = reinterpret<f64>(0x3ED0CFCA86E65239), // 4.00821782732936239552e-06
Q5 = reinterpret<f64>(0xBE8AFDB76E09C32D), // -2.01099218183624371326e-07
Ox1p1023 = reinterpret<f64>(0x7FE0000000000000);
var u = reinterpret<u64>(x);
var hx = <u32>(u >> 32 & 0x7FFFFFFF);
var k = 0, sign_ = <i32>(u >> 63);
if (hx >= 0x4043687A) {
if (isNaN(x)) return x;
if (sign_) return -1;
if (x > o_threshold) return x * Ox1p1023;
}
var c = 0.0, t: f64;
if (hx > 0x3FD62E42) {
k = select<i32>(
1 - (sign_ << 1),
<i32>(invln2 * x + builtin_copysign<f64>(0.5, x)),
hx < 0x3FF0A2B2
);
t = <f64>k;
let hi = x - t * ln2_hi;
let lo = t * ln2_lo;
x = hi - lo;
c = (hi - x) - lo;
} else if (hx < 0x3C900000) return x;
var hfx = 0.5 * x;
var hxs = x * hfx;
// var r1 = 1.0 + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
var hxq = hxs * hxs;
var r1 = (1.0 + hxs * Q1) + hxq * ((Q2 + hxs * Q3) + hxq * (Q4 + hxs * Q5));
t = 3.0 - r1 * hfx;
var e = hxs * ((r1 - t) / (6.0 - x * t));
if (k == 0) return x - (x * e - hxs);
e = x * (e - c) - c;
e -= hxs;
if (k == -1) return 0.5 * (x - e) - 0.5;
if (k == 1) {
if (x < -0.25) return -2.0 * (e - (x + 0.5));
return 1.0 + 2.0 * (x - e);
}
u = (0x3FF + k) << 52;
var twopk = reinterpret<f64>(u);
var y: f64;
if (k < 0 || k > 56) {
y = x - e + 1.0;
if (k == 1024) y = y * 2.0 * Ox1p1023;
else y = y * twopk;
return y - 1.0;
}
u = (0x3FF - k) << 52;
y = reinterpret<f64>(u);
if (k < 20) y = (1 - y) - e;
else y = 1 - (e + y);
return (x + y) * twopk;
}
// @ts-ignore: decorator
@inline
export function floor(x: f64): f64 {
return builtin_floor<f64>(x);
}
// @ts-ignore: decorator
@inline
export function fround(x: f64): f64 {
return <f32>x;
}
export function hypot(x: f64, y: f64): f64 { // see: musl/src/math/hypot.c
const
SPLIT = reinterpret<f64>(0x41A0000000000000) + 1, // 0x1p27 + 1
Ox1p700 = reinterpret<f64>(0x6BB0000000000000),
Ox1p_700 = reinterpret<f64>(0x1430000000000000);
var ux = reinterpret<u64>(x);
var uy = reinterpret<u64>(y);
ux &= 0x7FFFFFFFFFFFFFFF;
uy &= 0x7FFFFFFFFFFFFFFF;
if (ux < uy) {
let ut = ux;
ux = uy;
uy = ut;
}
var ex = <i32>(ux >> 52);
var ey = <i32>(uy >> 52);
y = reinterpret<f64>(uy);
if (ey == 0x7FF) return y;
x = reinterpret<f64>(ux);
if (ex == 0x7FF || uy == 0) return x;
if (ex - ey > 64) return x + y;
var z = 1.0;
if (ex > 0x3FF + 510) {
z = Ox1p700;
x *= Ox1p_700;
y *= Ox1p_700;
} else if (ey < 0x3FF - 450) {
z = Ox1p_700;
x *= Ox1p700;
y *= Ox1p700;
}
var c = x * SPLIT;
var h = x - c + c;
var l = x - h;
var hx = x * x;
var lx = h * h - hx + (2 * h + l) * l;
c = y * SPLIT;
h = y - c + c;
l = y - h;
var hy = y * y;
var ly = h * h - hy + (2 * h + l) * l;
return z * builtin_sqrt(ly + lx + hy + hx);
}
export function imul(x: f64, y: f64): f64 {
/*
* Wasm (MVP) and JS have different approaches for double->int conversions.
*
* For emulate JS conversion behavior and avoid trapping from wasm we should modulate by MAX_INT
* our float-point arguments before actual convertion to integers.
*/
if (!isFinite(x + y)) return 0;
return dtoi32(x) * dtoi32(y);
}
export function log(x: f64): f64 { // see: musl/src/math/log.c and SUN COPYRIGHT NOTICE above
if (ASC_SHRINK_LEVEL < 1) {
return log_lut(x);
} else {
const
ln2_hi = reinterpret<f64>(0x3FE62E42FEE00000), // 6.93147180369123816490e-01
ln2_lo = reinterpret<f64>(0x3DEA39EF35793C76), // 1.90821492927058770002e-10
Lg1 = reinterpret<f64>(0x3FE5555555555593), // 6.666666666666735130e-01
Lg2 = reinterpret<f64>(0x3FD999999997FA04), // 3.999999999940941908e-01
Lg3 = reinterpret<f64>(0x3FD2492494229359), // 2.857142874366239149e-01
Lg4 = reinterpret<f64>(0x3FCC71C51D8E78AF), // 2.222219843214978396e-01
Lg5 = reinterpret<f64>(0x3FC7466496CB03DE), // 1.818357216161805012e-01
Lg6 = reinterpret<f64>(0x3FC39A09D078C69F), // 1.531383769920937332e-01
Lg7 = reinterpret<f64>(0x3FC2F112DF3E5244), // 1.479819860511658591e-01
Ox1p54 = reinterpret<f64>(0x4350000000000000);
let u = reinterpret<u64>(x);
let hx = <u32>(u >> 32);
let k = 0;
if (hx < 0x00100000 || <bool>(hx >> 31)) {
if (u << 1 == 0) return -1 / (x * x);
if (hx >> 31) return (x - x) / 0.0;
k -= 54;
x *= Ox1p54;
u = reinterpret<u64>(x);
hx = <u32>(u >> 32);
} else if (hx >= 0x7FF00000) return x;
else if (hx == 0x3FF00000 && u << 32 == 0) return 0;
hx += 0x3FF00000 - 0x3FE6A09E;
k += (<i32>hx >> 20) - 0x3FF;
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
u = <u64>hx << 32 | (u & 0xFFFFFFFF);
x = reinterpret<f64>(u);
let f = x - 1.0;
let hfsq = 0.5 * f * f;
let s = f / (2.0 + f);
let z = s * s;