@grain/stdlib 0.4.4 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -0
- package/LICENSE +1 -1
- package/array.gr +92 -73
- package/array.md +18 -18
- package/bigint.gr +497 -0
- package/bigint.md +811 -0
- package/buffer.gr +56 -217
- package/buffer.md +24 -17
- package/bytes.gr +103 -205
- package/bytes.md +19 -0
- package/char.gr +152 -166
- package/char.md +200 -0
- package/exception.md +6 -0
- package/float32.gr +159 -82
- package/float32.md +315 -0
- package/float64.gr +163 -82
- package/float64.md +315 -0
- package/hash.gr +53 -49
- package/int32.gr +479 -230
- package/int32.md +937 -0
- package/int64.gr +479 -230
- package/int64.md +937 -0
- package/list.gr +530 -116
- package/list.md +1141 -0
- package/map.gr +302 -121
- package/map.md +525 -0
- package/number.gr +51 -57
- package/number.md +37 -3
- package/option.gr +25 -25
- package/option.md +1 -1
- package/package.json +3 -3
- package/pervasives.gr +504 -52
- package/pervasives.md +1116 -0
- package/queue.gr +8 -1
- package/queue.md +10 -0
- package/random.gr +196 -0
- package/random.md +179 -0
- package/range.gr +26 -26
- package/regex.gr +1833 -842
- package/regex.md +11 -11
- package/result.md +1 -1
- package/runtime/bigint.gr +2045 -0
- package/runtime/bigint.md +326 -0
- package/runtime/dataStructures.gr +99 -279
- package/runtime/dataStructures.md +391 -0
- package/runtime/debug.gr +0 -1
- package/runtime/debug.md +6 -0
- package/runtime/equal.gr +40 -37
- package/runtime/equal.md +6 -0
- package/runtime/exception.gr +28 -15
- package/runtime/exception.md +30 -0
- package/runtime/gc.gr +50 -20
- package/runtime/gc.md +36 -0
- package/runtime/malloc.gr +32 -22
- package/runtime/malloc.md +55 -0
- package/runtime/numberUtils.gr +297 -142
- package/runtime/numberUtils.md +54 -0
- package/runtime/numbers.gr +1204 -453
- package/runtime/numbers.md +300 -0
- package/runtime/string.gr +193 -228
- package/runtime/string.md +24 -0
- package/runtime/stringUtils.gr +62 -38
- package/runtime/stringUtils.md +6 -0
- package/runtime/unsafe/constants.gr +17 -0
- package/runtime/unsafe/constants.md +72 -0
- package/runtime/unsafe/conv.gr +10 -10
- package/runtime/unsafe/conv.md +71 -0
- package/runtime/unsafe/errors.md +204 -0
- package/runtime/unsafe/memory.gr +14 -3
- package/runtime/unsafe/memory.md +54 -0
- package/runtime/unsafe/printWasm.gr +4 -4
- package/runtime/unsafe/printWasm.md +24 -0
- package/runtime/unsafe/tags.gr +11 -10
- package/runtime/unsafe/tags.md +120 -0
- package/runtime/unsafe/wasmf32.gr +9 -2
- package/runtime/unsafe/wasmf32.md +168 -0
- package/runtime/unsafe/wasmf64.gr +9 -2
- package/runtime/unsafe/wasmf64.md +168 -0
- package/runtime/unsafe/wasmi32.gr +65 -47
- package/runtime/unsafe/wasmi32.md +282 -0
- package/runtime/unsafe/wasmi64.gr +78 -50
- package/runtime/unsafe/wasmi64.md +300 -0
- package/runtime/utils/printing.gr +62 -0
- package/runtime/utils/printing.md +18 -0
- package/runtime/wasi.gr +200 -46
- package/runtime/wasi.md +839 -0
- package/set.gr +125 -121
- package/set.md +24 -21
- package/stack.gr +29 -29
- package/stack.md +4 -6
- package/string.gr +434 -415
- package/string.md +3 -3
- package/sys/file.gr +477 -482
- package/sys/process.gr +33 -47
- package/sys/random.gr +48 -20
- package/sys/random.md +38 -0
- package/sys/time.gr +12 -28
package/runtime/numberUtils.gr
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* grainc-flags --
|
|
1
|
+
/* grainc-flags --no-pervasives */
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
* This file was inspired by AssemblyScript's std/assembly/util/number.ts
|
|
@@ -12,31 +12,40 @@ import WasmI32, {
|
|
|
12
12
|
sub as (-),
|
|
13
13
|
shl as (<<),
|
|
14
14
|
and as (&),
|
|
15
|
-
or as (|)
|
|
15
|
+
or as (|),
|
|
16
16
|
// no signed imports, as care should be taken to use signed or unsigned operators
|
|
17
17
|
} from "runtime/unsafe/wasmi32"
|
|
18
18
|
import WasmI64 from "runtime/unsafe/wasmi64"
|
|
19
19
|
import WasmF64 from "runtime/unsafe/wasmf64"
|
|
20
20
|
import Exception from "runtime/exception"
|
|
21
|
-
import { allocateString
|
|
21
|
+
import { allocateString } from "runtime/dataStructures"
|
|
22
22
|
|
|
23
|
-
primitive (!)
|
|
24
|
-
primitive (&&)
|
|
25
|
-
primitive (||)
|
|
26
|
-
primitive throw
|
|
23
|
+
primitive (!): Bool -> Bool = "@not"
|
|
24
|
+
primitive (&&): (Bool, Bool) -> Bool = "@and"
|
|
25
|
+
primitive (||): (Bool, Bool) -> Bool = "@or"
|
|
26
|
+
primitive throw: Exception -> a = "@throw"
|
|
27
27
|
|
|
28
|
+
@unsafe
|
|
28
29
|
export let _MAX_DOUBLE_LENGTH = 28n
|
|
29
30
|
|
|
31
|
+
@unsafe
|
|
30
32
|
let _CHAR_CODE_0 = 0x30n
|
|
33
|
+
@unsafe
|
|
31
34
|
let _CHAR_CODE_e = 0x65n
|
|
35
|
+
@unsafe
|
|
32
36
|
let _CHAR_CODE_PLUS = 0x2Bn
|
|
37
|
+
@unsafe
|
|
33
38
|
let _CHAR_CODE_MINUS = 0x2Dn
|
|
39
|
+
@unsafe
|
|
34
40
|
let _CHAR_CODE_DOT = 0x2En
|
|
35
41
|
|
|
42
|
+
@unsafe
|
|
36
43
|
let _I32_MAX = 0xffffffffN
|
|
37
44
|
|
|
45
|
+
@unsafe
|
|
38
46
|
let mut _POWERS10 = -1n
|
|
39
47
|
|
|
48
|
+
@unsafe
|
|
40
49
|
let get_POWERS10 = () => {
|
|
41
50
|
if (_POWERS10 == -1n) {
|
|
42
51
|
_POWERS10 = Memory.malloc(40n)
|
|
@@ -67,8 +76,10 @@ let get_POWERS10 = () => {
|
|
|
67
76
|
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
|
|
68
77
|
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99"
|
|
69
78
|
*/
|
|
79
|
+
@unsafe
|
|
70
80
|
let mut _DIGITS = -1n
|
|
71
81
|
|
|
82
|
+
@unsafe
|
|
72
83
|
let get_DIGITS = () => {
|
|
73
84
|
if (_DIGITS == -1n) {
|
|
74
85
|
_DIGITS = Memory.malloc(200n)
|
|
@@ -177,8 +188,10 @@ let get_DIGITS = () => {
|
|
|
177
188
|
}
|
|
178
189
|
|
|
179
190
|
// Lookup table for pairwise char codes in range [0x00-0xFF]
|
|
191
|
+
@unsafe
|
|
180
192
|
let mut _HEX_DIGITS = -1n
|
|
181
193
|
|
|
194
|
+
@unsafe
|
|
182
195
|
export let get_HEX_DIGITS = () => {
|
|
183
196
|
if (_HEX_DIGITS == -1n) {
|
|
184
197
|
_HEX_DIGITS = Memory.malloc(512n)
|
|
@@ -457,8 +470,10 @@ export let get_HEX_DIGITS = () => {
|
|
|
457
470
|
_HEX_DIGITS
|
|
458
471
|
}
|
|
459
472
|
|
|
473
|
+
@unsafe
|
|
460
474
|
let mut _ANY_DIGITS = -1n
|
|
461
475
|
|
|
476
|
+
@unsafe
|
|
462
477
|
let get_ANY_DIGITS = () => {
|
|
463
478
|
if (_ANY_DIGITS == -1n) {
|
|
464
479
|
_ANY_DIGITS = Memory.malloc(36n)
|
|
@@ -502,8 +517,10 @@ let get_ANY_DIGITS = () => {
|
|
|
502
517
|
_ANY_DIGITS
|
|
503
518
|
}
|
|
504
519
|
|
|
520
|
+
@unsafe
|
|
505
521
|
let mut _EXP_POWERS = -1n
|
|
506
522
|
|
|
523
|
+
@unsafe
|
|
507
524
|
let get_EXP_POWERS = () => {
|
|
508
525
|
if (_EXP_POWERS == -1n) {
|
|
509
526
|
_EXP_POWERS = Memory.malloc(174n)
|
|
@@ -599,8 +616,10 @@ let get_EXP_POWERS = () => {
|
|
|
599
616
|
}
|
|
600
617
|
|
|
601
618
|
// 1e-348, 1e-340, ..., 1e340
|
|
619
|
+
@unsafe
|
|
602
620
|
let mut _FRC_POWERS = -1n
|
|
603
621
|
|
|
622
|
+
@unsafe
|
|
604
623
|
let get_FRC_POWERS = () => {
|
|
605
624
|
if (_FRC_POWERS == -1n) {
|
|
606
625
|
_FRC_POWERS = Memory.malloc(696n)
|
|
@@ -695,49 +714,67 @@ let get_FRC_POWERS = () => {
|
|
|
695
714
|
_FRC_POWERS
|
|
696
715
|
}
|
|
697
716
|
|
|
698
|
-
|
|
717
|
+
@unsafe
|
|
718
|
+
let isPowerOf2 = value => {
|
|
699
719
|
WasmI32.popcnt(value) == 1n
|
|
700
720
|
}
|
|
701
721
|
|
|
702
722
|
// Count number of decimals for u32 values
|
|
703
723
|
// In our case input value always non-zero so we can simplify some parts
|
|
704
|
-
|
|
724
|
+
@unsafe
|
|
725
|
+
export let decimalCount32 = value => {
|
|
705
726
|
if (WasmI32.ltU(value, 100000n)) {
|
|
706
727
|
if (WasmI32.ltU(value, 100n)) {
|
|
707
728
|
1n + (if (WasmI32.geU(value, 10n)) 1n else 0n)
|
|
708
729
|
} else {
|
|
709
|
-
3n +
|
|
730
|
+
3n +
|
|
731
|
+
(if (WasmI32.geU(value, 10000n)) 1n else 0n) +
|
|
732
|
+
(if (WasmI32.geU(value, 1000n)) 1n else 0n)
|
|
710
733
|
}
|
|
711
734
|
} else {
|
|
712
735
|
if (WasmI32.ltU(value, 10000000n)) {
|
|
713
736
|
6n + (if (WasmI32.geU(value, 1000000n)) 1n else 0n)
|
|
714
737
|
} else {
|
|
715
|
-
8n +
|
|
738
|
+
8n +
|
|
739
|
+
(if (WasmI32.geU(value, 1000000000n)) 1n else 0n) +
|
|
740
|
+
(if (WasmI32.geU(value, 100000000n)) 1n else 0n)
|
|
716
741
|
}
|
|
717
742
|
}
|
|
718
743
|
}
|
|
719
744
|
|
|
720
745
|
// Count number of decimals for u64 values
|
|
721
746
|
// In our case input value always greater than 2^32-1 so we can skip some parts
|
|
722
|
-
|
|
747
|
+
@unsafe
|
|
748
|
+
let decimalCount64High = value => {
|
|
723
749
|
if (WasmI64.ltU(value, 1000000000000000N)) {
|
|
724
750
|
if (WasmI64.ltU(value, 1000000000000N)) {
|
|
725
|
-
10n +
|
|
751
|
+
10n +
|
|
752
|
+
(if (WasmI64.geU(value, 100000000000N)) 1n else 0n) +
|
|
753
|
+
(if (WasmI64.geU(value, 10000000000N)) 1n else 0n)
|
|
726
754
|
} else {
|
|
727
|
-
13n +
|
|
755
|
+
13n +
|
|
756
|
+
(if (WasmI64.geU(value, 100000000000000N)) 1n else 0n) +
|
|
757
|
+
(if (WasmI64.geU(value, 10000000000000N)) 1n else 0n)
|
|
728
758
|
}
|
|
729
759
|
} else {
|
|
730
760
|
if (WasmI64.ltU(value, 100000000000000000N)) {
|
|
731
761
|
16n + (if (WasmI64.geU(value, 10000000000000000N)) 1n else 0n)
|
|
732
762
|
} else {
|
|
733
|
-
18n +
|
|
763
|
+
18n +
|
|
764
|
+
(if (WasmI64.geU(value, 0x8AC7230489E80000N)) 1n else 0n) +
|
|
765
|
+
(if (WasmI64.geU(value, 1000000000000000000N)) 1n else 0n)
|
|
734
766
|
}
|
|
735
767
|
}
|
|
736
768
|
}
|
|
737
769
|
|
|
770
|
+
@unsafe
|
|
738
771
|
let ulog_base = (num, base) => {
|
|
739
772
|
if (isPowerOf2(base)) {
|
|
740
|
-
WasmI32.divU(
|
|
773
|
+
WasmI32.divU(
|
|
774
|
+
63n - WasmI32.wrapI64(WasmI64.clz(num)),
|
|
775
|
+
31n - WasmI32.clz(base)
|
|
776
|
+
) +
|
|
777
|
+
1n
|
|
741
778
|
} else {
|
|
742
779
|
let b64 = WasmI64.extendI32U(base)
|
|
743
780
|
let mut b = b64
|
|
@@ -750,12 +787,13 @@ let ulog_base = (num, base) => {
|
|
|
750
787
|
}
|
|
751
788
|
while (WasmI64.geU(num, 1N)) {
|
|
752
789
|
num = WasmI64.divU(num, b64)
|
|
753
|
-
e
|
|
790
|
+
e += 1n
|
|
754
791
|
}
|
|
755
792
|
e - 1n
|
|
756
793
|
}
|
|
757
794
|
}
|
|
758
795
|
|
|
796
|
+
@unsafe
|
|
759
797
|
let utoa32_dec_lut = (buffer, num, offset) => {
|
|
760
798
|
let mut num = num
|
|
761
799
|
let mut offset = offset
|
|
@@ -772,11 +810,11 @@ let utoa32_dec_lut = (buffer, num, offset) => {
|
|
|
772
810
|
let digits2 = WasmI32.load16U(get_DIGITS() + (d2 << 1n), 0n)
|
|
773
811
|
|
|
774
812
|
offset -= 4n
|
|
775
|
-
WasmI32.store(buffer + offset, digits1 |
|
|
813
|
+
WasmI32.store(buffer + offset, digits1 | digits2 << 16n, 0n)
|
|
776
814
|
}
|
|
777
815
|
|
|
778
816
|
if (WasmI32.geU(num, 100n)) {
|
|
779
|
-
let t
|
|
817
|
+
let t = WasmI32.divU(num, 100n)
|
|
780
818
|
let d1 = WasmI32.remU(num, 100n)
|
|
781
819
|
num = t
|
|
782
820
|
offset -= 2n
|
|
@@ -795,6 +833,7 @@ let utoa32_dec_lut = (buffer, num, offset) => {
|
|
|
795
833
|
}
|
|
796
834
|
}
|
|
797
835
|
|
|
836
|
+
@unsafe
|
|
798
837
|
let utoa64_dec_lut = (buffer, num, offset) => {
|
|
799
838
|
let mut num = num
|
|
800
839
|
let mut offset = offset
|
|
@@ -815,18 +854,19 @@ let utoa64_dec_lut = (buffer, num, offset) => {
|
|
|
815
854
|
let mut digits2 = WasmI32.load16U(get_DIGITS() + (c2 << 1n), 0n)
|
|
816
855
|
|
|
817
856
|
offset -= 4n
|
|
818
|
-
WasmI32.store(buffer + offset, digits1 |
|
|
857
|
+
WasmI32.store(buffer + offset, digits1 | digits2 << 16n, 0n)
|
|
819
858
|
|
|
820
859
|
digits1 = WasmI32.load16U(get_DIGITS() + (b1 << 1n), 0n)
|
|
821
860
|
digits2 = WasmI32.load16U(get_DIGITS() + (b2 << 1n), 0n)
|
|
822
861
|
|
|
823
862
|
offset -= 4n
|
|
824
|
-
WasmI32.store(buffer + offset, digits1 |
|
|
863
|
+
WasmI32.store(buffer + offset, digits1 | digits2 << 16n, 0n)
|
|
825
864
|
}
|
|
826
865
|
|
|
827
866
|
utoa32_dec_lut(buffer, WasmI32.wrapI64(num), offset)
|
|
828
867
|
}
|
|
829
868
|
|
|
869
|
+
@unsafe
|
|
830
870
|
let utoa_hex_lut = (buffer, num, offset) => {
|
|
831
871
|
let lut = get_HEX_DIGITS()
|
|
832
872
|
let mut num = num
|
|
@@ -841,37 +881,50 @@ let utoa_hex_lut = (buffer, num, offset) => {
|
|
|
841
881
|
num = WasmI64.shrU(num, 8N)
|
|
842
882
|
}
|
|
843
883
|
if (WasmI32.ne(offset & 1n, 0n)) {
|
|
844
|
-
WasmI32.store8(
|
|
884
|
+
WasmI32.store8(
|
|
885
|
+
buffer,
|
|
886
|
+
WasmI32.load8U(lut + (WasmI32.wrapI64(num) << 5n), 0n),
|
|
887
|
+
0n
|
|
888
|
+
)
|
|
845
889
|
}
|
|
846
890
|
}
|
|
847
891
|
|
|
892
|
+
@unsafe
|
|
848
893
|
let utoa32_dec_core = (buffer, num, offset) => {
|
|
849
894
|
utoa32_dec_lut(buffer, num, offset)
|
|
850
895
|
}
|
|
851
896
|
|
|
897
|
+
@unsafe
|
|
852
898
|
let utoa32_hex_core = (buffer, num, offset) => {
|
|
853
899
|
utoa_hex_lut(buffer, WasmI64.extendI32U(num), offset)
|
|
854
900
|
}
|
|
855
901
|
|
|
902
|
+
@unsafe
|
|
856
903
|
let utoa64_dec_core = (buffer, num, offset) => {
|
|
857
904
|
utoa64_dec_lut(buffer, num, offset)
|
|
858
905
|
}
|
|
859
906
|
|
|
907
|
+
@unsafe
|
|
860
908
|
let utoa64_hex_core = (buffer, num, offset) => {
|
|
861
909
|
utoa_hex_lut(buffer, num, offset)
|
|
862
910
|
}
|
|
863
911
|
|
|
912
|
+
@unsafe
|
|
864
913
|
let utoa64_any_core = (buffer, num, offset, radix) => {
|
|
865
914
|
let lut = get_ANY_DIGITS()
|
|
866
915
|
let base = WasmI64.extendI32U(radix)
|
|
867
916
|
let mut num = num
|
|
868
917
|
let mut offset = offset
|
|
869
|
-
if ((radix &
|
|
918
|
+
if ((radix & radix - 1n) == 0n) { // for radix which pow of two
|
|
870
919
|
let shift = WasmI64.extendI32U(WasmI32.ctz(radix) & 7n)
|
|
871
920
|
let mask = WasmI64.sub(base, 1N)
|
|
872
921
|
for (;;) {
|
|
873
922
|
offset -= 1n
|
|
874
|
-
WasmI32.store8(
|
|
923
|
+
WasmI32.store8(
|
|
924
|
+
buffer + offset,
|
|
925
|
+
WasmI32.load8U(lut + WasmI32.wrapI64(WasmI64.and(num, mask)), 0n),
|
|
926
|
+
0n
|
|
927
|
+
)
|
|
875
928
|
num = WasmI64.shrU(num, shift)
|
|
876
929
|
if (WasmI64.eqz(num)) break
|
|
877
930
|
}
|
|
@@ -879,18 +932,28 @@ let utoa64_any_core = (buffer, num, offset, radix) => {
|
|
|
879
932
|
for (;;) {
|
|
880
933
|
offset -= 1n
|
|
881
934
|
let q = WasmI64.divU(num, base)
|
|
882
|
-
WasmI32.store8(
|
|
935
|
+
WasmI32.store8(
|
|
936
|
+
buffer + offset,
|
|
937
|
+
WasmI32.load8U(
|
|
938
|
+
lut + WasmI32.wrapI64(WasmI64.sub(num, WasmI64.mul(q, base))),
|
|
939
|
+
0n
|
|
940
|
+
),
|
|
941
|
+
0n
|
|
942
|
+
)
|
|
883
943
|
num = q
|
|
884
944
|
if (WasmI64.eqz(num)) break
|
|
885
945
|
}
|
|
886
946
|
}
|
|
887
947
|
}
|
|
888
948
|
|
|
949
|
+
@unsafe
|
|
889
950
|
export let utoa32Buffered = (buf, value, radix) => {
|
|
890
951
|
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
|
|
891
|
-
throw Exception.InvalidArgument(
|
|
952
|
+
throw Exception.InvalidArgument(
|
|
953
|
+
"toString() radix argument must be between 2 and 36"
|
|
954
|
+
)
|
|
892
955
|
}
|
|
893
|
-
|
|
956
|
+
if (WasmI32.eqz(value)) {
|
|
894
957
|
WasmI32.store8(buf, _CHAR_CODE_0, 0n)
|
|
895
958
|
} else if (radix == 10n) {
|
|
896
959
|
let decimals = decimalCount32(value)
|
|
@@ -905,133 +968,154 @@ export let utoa32Buffered = (buf, value, radix) => {
|
|
|
905
968
|
}
|
|
906
969
|
}
|
|
907
970
|
|
|
971
|
+
@unsafe
|
|
908
972
|
export let utoa32 = (value, radix) => {
|
|
909
973
|
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
|
|
910
|
-
throw Exception.InvalidArgument(
|
|
974
|
+
throw Exception.InvalidArgument(
|
|
975
|
+
"toString() radix argument must be between 2 and 36"
|
|
976
|
+
)
|
|
911
977
|
}
|
|
912
|
-
|
|
913
|
-
|
|
978
|
+
if (WasmI32.eqz(value)) {
|
|
979
|
+
"0"
|
|
914
980
|
} else if (radix == 10n) {
|
|
915
981
|
let decimals = decimalCount32(value)
|
|
916
982
|
let out = allocateString(decimals)
|
|
917
983
|
utoa32_dec_core(out, value, decimals)
|
|
918
|
-
out
|
|
984
|
+
WasmI32.toGrain(out): String
|
|
919
985
|
} else if (radix == 16n) {
|
|
920
986
|
let decimals = WasmI32.shrU(31n - WasmI32.clz(value), 2n) + 1n
|
|
921
987
|
let out = allocateString(decimals)
|
|
922
988
|
utoa32_hex_core(out, value, decimals)
|
|
923
|
-
out
|
|
989
|
+
WasmI32.toGrain(out): String
|
|
924
990
|
} else {
|
|
925
991
|
let decimals = ulog_base(WasmI64.extendI32U(value), radix)
|
|
926
992
|
let out = allocateString(decimals)
|
|
927
993
|
utoa64_any_core(out, WasmI64.extendI32U(value), decimals, radix)
|
|
928
|
-
out
|
|
994
|
+
WasmI32.toGrain(out): String
|
|
929
995
|
}
|
|
930
|
-
WasmI32.toGrain(str): String
|
|
931
996
|
}
|
|
932
997
|
|
|
998
|
+
@unsafe
|
|
933
999
|
export let itoa32 = (value, radix) => {
|
|
934
1000
|
let mut value = value
|
|
935
1001
|
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
|
|
936
|
-
throw Exception.InvalidArgument(
|
|
1002
|
+
throw Exception.InvalidArgument(
|
|
1003
|
+
"toString() radix argument must be between 2 and 36"
|
|
1004
|
+
)
|
|
937
1005
|
}
|
|
938
|
-
let mut out = 0n
|
|
939
1006
|
let sign = WasmI32.shrU(value, 31n)
|
|
940
1007
|
|
|
941
1008
|
if (WasmI32.ne(sign, 0n)) value = 0n - value
|
|
942
1009
|
|
|
943
|
-
if (WasmI32.eqz(value)) {
|
|
944
|
-
|
|
1010
|
+
let out = if (WasmI32.eqz(value)) {
|
|
1011
|
+
"0"
|
|
945
1012
|
} else if (radix == 10n) {
|
|
946
1013
|
let decimals = decimalCount32(value) + sign
|
|
947
|
-
out = allocateString(decimals)
|
|
1014
|
+
let out = allocateString(decimals)
|
|
948
1015
|
utoa32_dec_core(out + 8n, value, decimals)
|
|
1016
|
+
WasmI32.toGrain(out): String
|
|
949
1017
|
} else if (radix == 16n) {
|
|
950
1018
|
let decimals = WasmI32.shrU(31n - WasmI32.clz(value), 2n) + 1n + sign
|
|
951
|
-
out = allocateString(decimals)
|
|
1019
|
+
let out = allocateString(decimals)
|
|
952
1020
|
utoa32_hex_core(out + 8n, value, decimals)
|
|
1021
|
+
WasmI32.toGrain(out): String
|
|
953
1022
|
} else {
|
|
954
1023
|
let val64 = WasmI64.extendI32U(value)
|
|
955
1024
|
let decimals = ulog_base(val64, radix) + sign
|
|
956
|
-
out = allocateString(decimals)
|
|
1025
|
+
let out = allocateString(decimals)
|
|
957
1026
|
utoa64_any_core(out + 8n, val64, decimals, radix)
|
|
1027
|
+
WasmI32.toGrain(out): String
|
|
958
1028
|
}
|
|
959
|
-
if (WasmI32.ne(sign, 0n))
|
|
960
|
-
|
|
1029
|
+
if (WasmI32.ne(sign, 0n))
|
|
1030
|
+
WasmI32.store8(WasmI32.fromGrain(out), _CHAR_CODE_MINUS, 8n)
|
|
1031
|
+
out
|
|
961
1032
|
}
|
|
962
1033
|
|
|
1034
|
+
@unsafe
|
|
963
1035
|
export let utoa64 = (value, radix) => {
|
|
964
1036
|
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
|
|
965
|
-
throw Exception.InvalidArgument(
|
|
1037
|
+
throw Exception.InvalidArgument(
|
|
1038
|
+
"toString() radix argument must be between 2 and 36"
|
|
1039
|
+
)
|
|
966
1040
|
}
|
|
967
|
-
|
|
968
|
-
|
|
1041
|
+
if (WasmI64.eqz(value)) {
|
|
1042
|
+
"0"
|
|
969
1043
|
} else if (radix == 10n) {
|
|
970
1044
|
if (WasmI64.leU(value, _I32_MAX)) {
|
|
971
|
-
let val32
|
|
1045
|
+
let val32 = WasmI32.wrapI64(value)
|
|
972
1046
|
let decimals = decimalCount32(val32)
|
|
973
1047
|
let out = allocateString(decimals)
|
|
974
1048
|
utoa32_dec_core(out + 8n, val32, decimals)
|
|
975
|
-
out
|
|
1049
|
+
WasmI32.toGrain(out): String
|
|
976
1050
|
} else {
|
|
977
1051
|
let decimals = decimalCount64High(value)
|
|
978
1052
|
let out = allocateString(decimals)
|
|
979
1053
|
utoa64_dec_core(out + 8n, value, decimals)
|
|
980
|
-
out
|
|
1054
|
+
WasmI32.toGrain(out): String
|
|
981
1055
|
}
|
|
982
1056
|
} else if (radix == 16n) {
|
|
983
|
-
let decimals = WasmI32.shrU(63n - WasmI32.wrapI64(WasmI64.clz(value)), 2n) +
|
|
1057
|
+
let decimals = WasmI32.shrU(63n - WasmI32.wrapI64(WasmI64.clz(value)), 2n) +
|
|
1058
|
+
1n
|
|
984
1059
|
let out = allocateString(decimals)
|
|
985
1060
|
utoa64_hex_core(out + 8n, value, decimals)
|
|
986
|
-
out
|
|
1061
|
+
WasmI32.toGrain(out): String
|
|
987
1062
|
} else {
|
|
988
1063
|
let decimals = ulog_base(value, radix)
|
|
989
1064
|
let out = allocateString(decimals)
|
|
990
1065
|
utoa64_any_core(out + 8n, value, decimals, radix)
|
|
991
|
-
out
|
|
1066
|
+
WasmI32.toGrain(out): String
|
|
992
1067
|
}
|
|
993
|
-
WasmI32.toGrain(str): String
|
|
994
1068
|
}
|
|
995
1069
|
|
|
1070
|
+
@unsafe
|
|
996
1071
|
export let itoa64 = (value, radix) => {
|
|
997
1072
|
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
|
|
998
|
-
throw Exception.InvalidArgument(
|
|
1073
|
+
throw Exception.InvalidArgument(
|
|
1074
|
+
"toString() radix argument must be between 2 and 36"
|
|
1075
|
+
)
|
|
999
1076
|
}
|
|
1000
1077
|
|
|
1001
1078
|
let mut value = value
|
|
1002
|
-
let mut out = 0n
|
|
1003
1079
|
|
|
1004
1080
|
let sign = WasmI32.wrapI64(WasmI64.shrU(value, 63N))
|
|
1005
1081
|
if (sign != 0n) value = WasmI64.sub(0N, value)
|
|
1006
1082
|
|
|
1007
|
-
if (WasmI64.eqz(value)) {
|
|
1008
|
-
|
|
1083
|
+
let out = if (WasmI64.eqz(value)) {
|
|
1084
|
+
"0"
|
|
1009
1085
|
} else if (radix == 10n) {
|
|
1010
1086
|
if (WasmI64.leU(value, _I32_MAX)) {
|
|
1011
|
-
let val32
|
|
1087
|
+
let val32 = WasmI32.wrapI64(value)
|
|
1012
1088
|
let decimals = decimalCount32(val32) + sign
|
|
1013
|
-
out = allocateString(decimals)
|
|
1089
|
+
let out = allocateString(decimals)
|
|
1014
1090
|
utoa32_dec_core(out + 8n, val32, decimals)
|
|
1091
|
+
WasmI32.toGrain(out): String
|
|
1015
1092
|
} else {
|
|
1016
1093
|
let decimals = decimalCount64High(value) + sign
|
|
1017
|
-
out = allocateString(decimals)
|
|
1094
|
+
let out = allocateString(decimals)
|
|
1018
1095
|
utoa64_dec_core(out + 8n, value, decimals)
|
|
1096
|
+
WasmI32.toGrain(out): String
|
|
1019
1097
|
}
|
|
1020
1098
|
} else if (radix == 16n) {
|
|
1021
|
-
let decimals = WasmI32.shrU(63n - WasmI32.wrapI64(WasmI64.clz(value)), 2n) +
|
|
1022
|
-
|
|
1099
|
+
let decimals = WasmI32.shrU(63n - WasmI32.wrapI64(WasmI64.clz(value)), 2n) +
|
|
1100
|
+
1n +
|
|
1101
|
+
sign
|
|
1102
|
+
let out = allocateString(decimals)
|
|
1023
1103
|
utoa64_hex_core(out + 8n, value, decimals)
|
|
1104
|
+
WasmI32.toGrain(out): String
|
|
1024
1105
|
} else {
|
|
1025
1106
|
let decimals = ulog_base(value, radix) + sign
|
|
1026
|
-
out = allocateString(decimals)
|
|
1107
|
+
let out = allocateString(decimals)
|
|
1027
1108
|
utoa64_any_core(out + 8n, value, decimals, radix)
|
|
1109
|
+
WasmI32.toGrain(out): String
|
|
1028
1110
|
}
|
|
1029
|
-
if (sign != 0n) WasmI32.store8(out, _CHAR_CODE_MINUS, 8n)
|
|
1030
|
-
|
|
1111
|
+
if (sign != 0n) WasmI32.store8(WasmI32.fromGrain(out), _CHAR_CODE_MINUS, 8n)
|
|
1112
|
+
out
|
|
1031
1113
|
}
|
|
1032
1114
|
|
|
1115
|
+
@unsafe
|
|
1033
1116
|
let mut _K = 0n
|
|
1034
1117
|
|
|
1118
|
+
@unsafe
|
|
1035
1119
|
let umul64f = (u, v) => {
|
|
1036
1120
|
let u0 = WasmI64.and(u, 0xFFFFFFFFN)
|
|
1037
1121
|
let v0 = WasmI64.and(v, 0xFFFFFFFFN)
|
|
@@ -1051,20 +1135,24 @@ let umul64f = (u, v) => {
|
|
|
1051
1135
|
WasmI64.add(WasmI64.add(WasmI64.mul(u1, v1), t), w)
|
|
1052
1136
|
}
|
|
1053
1137
|
|
|
1138
|
+
@unsafe
|
|
1054
1139
|
let umul64e = (e1, e2) => {
|
|
1055
1140
|
e1 + e2 + 64n // where 64 is significand size
|
|
1056
1141
|
}
|
|
1057
1142
|
|
|
1143
|
+
@unsafe
|
|
1058
1144
|
let grisuRound = (buffer, len, delta, rest, ten_kappa, wp_w) => {
|
|
1059
1145
|
let mut lastp = buffer + len - 1n
|
|
1060
1146
|
let mut digit = WasmI32.load8U(lastp, 0n)
|
|
1061
1147
|
let mut rest = rest
|
|
1062
1148
|
while (
|
|
1063
1149
|
WasmI64.ltU(rest, wp_w) &&
|
|
1064
|
-
WasmI64.geU(WasmI64.sub(delta, rest), ten_kappa) &&
|
|
1065
|
-
|
|
1066
|
-
WasmI64.gtU(
|
|
1067
|
-
|
|
1150
|
+
WasmI64.geU(WasmI64.sub(delta, rest), ten_kappa) &&
|
|
1151
|
+
(WasmI64.ltU(WasmI64.add(rest, ten_kappa), wp_w) ||
|
|
1152
|
+
WasmI64.gtU(
|
|
1153
|
+
WasmI64.sub(wp_w, rest),
|
|
1154
|
+
WasmI64.sub(WasmI64.add(rest, ten_kappa), wp_w)
|
|
1155
|
+
))
|
|
1068
1156
|
) {
|
|
1069
1157
|
digit -= 1n
|
|
1070
1158
|
rest = WasmI64.add(rest, ten_kappa)
|
|
@@ -1072,15 +1160,18 @@ let grisuRound = (buffer, len, delta, rest, ten_kappa, wp_w) => {
|
|
|
1072
1160
|
WasmI32.store8(lastp, digit, 0n)
|
|
1073
1161
|
}
|
|
1074
1162
|
|
|
1075
|
-
|
|
1163
|
+
@unsafe
|
|
1164
|
+
let genDigits = (buffer, w_frc, mp_frc, mp_exp, delta, sign) => {
|
|
1076
1165
|
let mut delta = delta
|
|
1077
1166
|
let one_exp = 0n - mp_exp
|
|
1078
1167
|
let one_frc = WasmI64.shl(1N, WasmI64.extendI32U(one_exp))
|
|
1079
|
-
let mask
|
|
1168
|
+
let mask = WasmI64.sub(one_frc, 1N)
|
|
1080
1169
|
|
|
1081
1170
|
let mut wp_w_frc = WasmI64.sub(mp_frc, w_frc)
|
|
1082
1171
|
|
|
1083
|
-
let mut p1 = WasmI32.wrapI64(
|
|
1172
|
+
let mut p1 = WasmI32.wrapI64(
|
|
1173
|
+
WasmI64.shrU(mp_frc, WasmI64.extendI32U(one_exp))
|
|
1174
|
+
)
|
|
1084
1175
|
let mut p2 = WasmI64.and(mp_frc, mask)
|
|
1085
1176
|
|
|
1086
1177
|
let mut kappa = decimalCount32(p1)
|
|
@@ -1091,17 +1182,49 @@ let genDigits = (buffer, w_frc, w_exp, mp_frc, mp_exp, delta, sign) => {
|
|
|
1091
1182
|
while (WasmI32.gtS(kappa, 0n)) {
|
|
1092
1183
|
let mut d = 0n
|
|
1093
1184
|
match (kappa) {
|
|
1094
|
-
10n => {
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1185
|
+
10n => {
|
|
1186
|
+
d = WasmI32.divU(p1, 1000000000n)
|
|
1187
|
+
p1 = WasmI32.remU(p1, 1000000000n)
|
|
1188
|
+
},
|
|
1189
|
+
9n => {
|
|
1190
|
+
d = WasmI32.divU(p1, 100000000n)
|
|
1191
|
+
p1 = WasmI32.remU(p1, 100000000n)
|
|
1192
|
+
},
|
|
1193
|
+
8n => {
|
|
1194
|
+
d = WasmI32.divU(p1, 10000000n)
|
|
1195
|
+
p1 = WasmI32.remU(p1, 10000000n)
|
|
1196
|
+
},
|
|
1197
|
+
7n => {
|
|
1198
|
+
d = WasmI32.divU(p1, 1000000n)
|
|
1199
|
+
p1 = WasmI32.remU(p1, 1000000n)
|
|
1200
|
+
},
|
|
1201
|
+
6n => {
|
|
1202
|
+
d = WasmI32.divU(p1, 100000n)
|
|
1203
|
+
p1 = WasmI32.remU(p1, 100000n)
|
|
1204
|
+
},
|
|
1205
|
+
5n => {
|
|
1206
|
+
d = WasmI32.divU(p1, 10000n)
|
|
1207
|
+
p1 = WasmI32.remU(p1, 10000n)
|
|
1208
|
+
},
|
|
1209
|
+
4n => {
|
|
1210
|
+
d = WasmI32.divU(p1, 1000n)
|
|
1211
|
+
p1 = WasmI32.remU(p1, 1000n)
|
|
1212
|
+
},
|
|
1213
|
+
3n => {
|
|
1214
|
+
d = WasmI32.divU(p1, 100n)
|
|
1215
|
+
p1 = WasmI32.remU(p1, 100n)
|
|
1216
|
+
},
|
|
1217
|
+
2n => {
|
|
1218
|
+
d = WasmI32.divU(p1, 10n)
|
|
1219
|
+
p1 = WasmI32.remU(p1, 10n)
|
|
1220
|
+
},
|
|
1221
|
+
1n => {
|
|
1222
|
+
d = p1
|
|
1223
|
+
p1 = 0n
|
|
1224
|
+
},
|
|
1225
|
+
_ => {
|
|
1226
|
+
d = 0n
|
|
1227
|
+
},
|
|
1105
1228
|
}
|
|
1106
1229
|
|
|
1107
1230
|
if (WasmI32.ne(d | len, 0n)) {
|
|
@@ -1110,54 +1233,83 @@ let genDigits = (buffer, w_frc, w_exp, mp_frc, mp_exp, delta, sign) => {
|
|
|
1110
1233
|
}
|
|
1111
1234
|
|
|
1112
1235
|
kappa -= 1n
|
|
1113
|
-
let tmp = WasmI64.add(
|
|
1236
|
+
let tmp = WasmI64.add(
|
|
1237
|
+
WasmI64.shl(WasmI64.extendI32U(p1), WasmI64.extendI32U(one_exp)),
|
|
1238
|
+
p2
|
|
1239
|
+
)
|
|
1114
1240
|
if (WasmI64.leU(tmp, delta)) {
|
|
1115
1241
|
_K += kappa
|
|
1116
|
-
grisuRound(
|
|
1242
|
+
grisuRound(
|
|
1243
|
+
buffer,
|
|
1244
|
+
len,
|
|
1245
|
+
delta,
|
|
1246
|
+
tmp,
|
|
1247
|
+
WasmI64.shl(
|
|
1248
|
+
WasmI64.extendI32U(WasmI32.load(get_POWERS10() + (kappa << 2n), 0n)),
|
|
1249
|
+
WasmI64.extendI32U(one_exp)
|
|
1250
|
+
),
|
|
1251
|
+
wp_w_frc
|
|
1252
|
+
)
|
|
1117
1253
|
done = true
|
|
1118
1254
|
break
|
|
1119
1255
|
}
|
|
1120
1256
|
}
|
|
1121
1257
|
|
|
1122
|
-
if (!done)
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1258
|
+
if (!done)
|
|
1259
|
+
while (true) {
|
|
1260
|
+
p2 = WasmI64.mul(p2, 10N)
|
|
1261
|
+
delta = WasmI64.mul(delta, 10N)
|
|
1262
|
+
|
|
1263
|
+
let d = WasmI64.shrU(p2, WasmI64.extendI32U(one_exp))
|
|
1264
|
+
if (WasmI64.ne(WasmI64.or(d, WasmI64.extendI32U(len)), 0N)) {
|
|
1265
|
+
WasmI32.store8(
|
|
1266
|
+
buffer + len,
|
|
1267
|
+
_CHAR_CODE_0 + (WasmI32.wrapI64(d) & 0xffn),
|
|
1268
|
+
0n
|
|
1269
|
+
)
|
|
1270
|
+
len += 1n
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
p2 = WasmI64.and(p2, mask)
|
|
1274
|
+
kappa -= 1n
|
|
1275
|
+
if (WasmI64.ltU(p2, delta)) {
|
|
1276
|
+
_K += kappa
|
|
1277
|
+
wp_w_frc = WasmI64.mul(
|
|
1278
|
+
wp_w_frc,
|
|
1279
|
+
WasmI64.extendI32U(
|
|
1280
|
+
WasmI32.load(get_POWERS10() + (0n - kappa << 2n), 0n)
|
|
1281
|
+
)
|
|
1282
|
+
)
|
|
1283
|
+
grisuRound(buffer, len, delta, p2, one_frc, wp_w_frc)
|
|
1284
|
+
break
|
|
1285
|
+
}
|
|
1130
1286
|
}
|
|
1131
|
-
|
|
1132
|
-
p2 = WasmI64.and(p2, mask)
|
|
1133
|
-
kappa -= 1n
|
|
1134
|
-
if (WasmI64.ltU(p2, delta)) {
|
|
1135
|
-
_K += kappa
|
|
1136
|
-
wp_w_frc = WasmI64.mul(wp_w_frc, WasmI64.extendI32U(WasmI32.load(get_POWERS10() + ((0n - kappa) << 2n), 0n)))
|
|
1137
|
-
grisuRound(buffer, len, delta, p2, one_frc, wp_w_frc)
|
|
1138
|
-
break
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
|
|
1142
1287
|
len
|
|
1143
1288
|
}
|
|
1144
1289
|
|
|
1290
|
+
@unsafe
|
|
1145
1291
|
let genExponent = (buffer, k) => {
|
|
1146
1292
|
let mut k = k
|
|
1147
1293
|
let sign = WasmI32.ltS(k, 0n)
|
|
1148
1294
|
if (sign) k = 0n - k
|
|
1149
1295
|
let decimals = decimalCount32(k) + 1n
|
|
1150
1296
|
utoa32_dec_core(buffer, k, decimals)
|
|
1151
|
-
WasmI32.store8(buffer,
|
|
1297
|
+
WasmI32.store8(buffer, if (sign) _CHAR_CODE_MINUS else _CHAR_CODE_PLUS, 0n)
|
|
1152
1298
|
decimals
|
|
1153
1299
|
}
|
|
1154
1300
|
|
|
1301
|
+
@unsafe
|
|
1155
1302
|
let grisu2 = (value, buffer, sign) => {
|
|
1156
1303
|
// frexp routine
|
|
1157
|
-
let uv
|
|
1158
|
-
let mut exp = WasmI32.wrapI64(
|
|
1304
|
+
let uv = WasmI64.reinterpretF64(value)
|
|
1305
|
+
let mut exp = WasmI32.wrapI64(
|
|
1306
|
+
WasmI64.shrU(WasmI64.and(uv, 0x7FF0000000000000N), 52N)
|
|
1307
|
+
)
|
|
1159
1308
|
let sid = WasmI64.and(uv, 0x000FFFFFFFFFFFFFN)
|
|
1160
|
-
let mut frc = WasmI64.add(
|
|
1309
|
+
let mut frc = WasmI64.add(
|
|
1310
|
+
WasmI64.shl(if (WasmI32.eqz(exp)) 0N else 1N, 52N),
|
|
1311
|
+
sid
|
|
1312
|
+
)
|
|
1161
1313
|
exp = (if (WasmI32.eqz(exp)) 1n else exp) - (0x3FFn + 52n)
|
|
1162
1314
|
|
|
1163
1315
|
// normalize boundaries
|
|
@@ -1165,77 +1317,78 @@ let grisu2 = (value, buffer, sign) => {
|
|
|
1165
1317
|
let mut exp_norm = exp - 1n
|
|
1166
1318
|
let off_norm = WasmI64.clz(frc_norm)
|
|
1167
1319
|
frc_norm = WasmI64.shl(frc_norm, off_norm)
|
|
1168
|
-
exp_norm
|
|
1320
|
+
exp_norm -= WasmI32.wrapI64(off_norm)
|
|
1169
1321
|
|
|
1170
1322
|
let m_norm = 1n + (if (WasmI64.eq(frc, 0x0010000000000000N)) 1n else 0n)
|
|
1171
1323
|
|
|
1172
|
-
let _frc_plus
|
|
1173
|
-
let _frc_minus = WasmI64.shl(
|
|
1324
|
+
let _frc_plus = frc_norm
|
|
1325
|
+
let _frc_minus = WasmI64.shl(
|
|
1326
|
+
WasmI64.sub(WasmI64.shl(frc, WasmI64.extendI32U(m_norm)), 1N),
|
|
1327
|
+
WasmI64.extendI32U(exp - m_norm - exp_norm)
|
|
1328
|
+
)
|
|
1174
1329
|
let _exp = exp_norm
|
|
1175
1330
|
|
|
1176
1331
|
// get cached power
|
|
1177
|
-
let c = WasmF64.reinterpretI64(
|
|
1178
|
-
|
|
1332
|
+
let c = WasmF64.reinterpretI64(
|
|
1333
|
+
0x3FD34413509F79FEN
|
|
1334
|
+
) // 1 / lg(10) = 0.30102999566398114
|
|
1335
|
+
let dk = WasmF64.add(
|
|
1336
|
+
WasmF64.mul(WasmF64.convertI32S(-61n - _exp), c),
|
|
1337
|
+
347.0W
|
|
1338
|
+
) // dk must be positive, so can do ceiling in positive
|
|
1179
1339
|
let mut k = WasmI32.truncF64S(dk)
|
|
1180
|
-
k += if (WasmF64.ne(WasmF64.convertI32S(k), dk)) 1n
|
|
1340
|
+
k += if (WasmF64.ne(WasmF64.convertI32S(k), dk)) 1n
|
|
1341
|
+
else 0n // conversion with ceil
|
|
1181
1342
|
|
|
1182
1343
|
let index = WasmI32.shrS(k, 3n) + 1n
|
|
1183
|
-
_K = 348n - (index << 3n)
|
|
1344
|
+
_K = 348n - (index << 3n) // decimal exponent doesn't need lookup table
|
|
1184
1345
|
let _frc_pow = WasmI64.load(get_FRC_POWERS() + (index << 3n), 0n)
|
|
1185
1346
|
let _exp_pow = WasmI32.load16S(get_EXP_POWERS() + (index << 1n), 0n)
|
|
1186
1347
|
|
|
1187
1348
|
// normalize
|
|
1188
1349
|
let off = WasmI32.wrapI64(WasmI64.clz(frc))
|
|
1189
1350
|
frc = WasmI64.shl(frc, WasmI64.extendI32U(off))
|
|
1190
|
-
exp
|
|
1351
|
+
exp -= off
|
|
1191
1352
|
|
|
1192
1353
|
let frc_pow = _frc_pow
|
|
1193
1354
|
let exp_pow = _exp_pow
|
|
1194
1355
|
|
|
1195
1356
|
let w_frc = umul64f(frc, frc_pow)
|
|
1196
|
-
let w_exp = umul64e(exp, exp_pow)
|
|
1197
1357
|
|
|
1198
1358
|
let wp_frc = WasmI64.sub(umul64f(_frc_plus, frc_pow), 1N)
|
|
1199
1359
|
let wp_exp = umul64e(_exp, exp_pow)
|
|
1200
1360
|
|
|
1201
1361
|
let wm_frc = WasmI64.add(umul64f(_frc_minus, frc_pow), 1N)
|
|
1202
|
-
let delta
|
|
1362
|
+
let delta = WasmI64.sub(wp_frc, wm_frc)
|
|
1203
1363
|
|
|
1204
|
-
genDigits(buffer, w_frc,
|
|
1364
|
+
genDigits(buffer, w_frc, wp_frc, wp_exp, delta, sign)
|
|
1205
1365
|
}
|
|
1206
1366
|
|
|
1367
|
+
@unsafe
|
|
1207
1368
|
let prettify = (buffer, length, k) => {
|
|
1208
1369
|
let mut length = length
|
|
1209
1370
|
let kk = length + k
|
|
1210
1371
|
if (WasmI32.eqz(k)) {
|
|
1211
|
-
WasmI32.store16(buffer + length, _CHAR_CODE_DOT |
|
|
1372
|
+
WasmI32.store16(buffer + length, _CHAR_CODE_DOT | _CHAR_CODE_0 << 8n, 0n)
|
|
1212
1373
|
length + 2n
|
|
1213
1374
|
} else if (WasmI32.leS(length, kk) && WasmI32.leS(kk, 21n)) {
|
|
1214
1375
|
// 1234e7 -> 12340000000
|
|
1215
1376
|
for (let mut i = length; WasmI32.ltS(i, kk); i += 1n) {
|
|
1216
1377
|
WasmI32.store8(buffer + i, _CHAR_CODE_0, 0n)
|
|
1217
1378
|
}
|
|
1218
|
-
WasmI32.store16(buffer + kk, _CHAR_CODE_DOT |
|
|
1379
|
+
WasmI32.store16(buffer + kk, _CHAR_CODE_DOT | _CHAR_CODE_0 << 8n, 0n)
|
|
1219
1380
|
kk + 2n
|
|
1220
1381
|
} else if (WasmI32.gtS(kk, 0n) && WasmI32.leS(kk, 21n)) {
|
|
1221
1382
|
// 1234e-2 -> 12.34
|
|
1222
1383
|
let ptr = buffer + kk
|
|
1223
|
-
Memory.copy(
|
|
1224
|
-
ptr + 1n,
|
|
1225
|
-
ptr,
|
|
1226
|
-
0n - k
|
|
1227
|
-
)
|
|
1384
|
+
Memory.copy(ptr + 1n, ptr, 0n - k)
|
|
1228
1385
|
WasmI32.store8(buffer + kk, _CHAR_CODE_DOT, 0n)
|
|
1229
1386
|
length + 1n
|
|
1230
1387
|
} else if (WasmI32.ltS(-6n, kk) && WasmI32.leS(kk, 0n)) {
|
|
1231
1388
|
// 1234e-6 -> 0.001234
|
|
1232
1389
|
let offset = 2n - kk
|
|
1233
|
-
Memory.copy(
|
|
1234
|
-
|
|
1235
|
-
buffer,
|
|
1236
|
-
length
|
|
1237
|
-
)
|
|
1238
|
-
WasmI32.store16(buffer, _CHAR_CODE_0 | (_CHAR_CODE_DOT << 8n), 0n)
|
|
1390
|
+
Memory.copy(buffer + offset, buffer, length)
|
|
1391
|
+
WasmI32.store16(buffer, _CHAR_CODE_0 | _CHAR_CODE_DOT << 8n, 0n)
|
|
1239
1392
|
for (let mut i = 2n; WasmI32.ltS(i, offset); i += 1n) {
|
|
1240
1393
|
WasmI32.store8(buffer + i, _CHAR_CODE_0, 0n)
|
|
1241
1394
|
}
|
|
@@ -1247,18 +1400,15 @@ let prettify = (buffer, length, k) => {
|
|
|
1247
1400
|
length + 2n
|
|
1248
1401
|
} else {
|
|
1249
1402
|
let len = length
|
|
1250
|
-
Memory.copy(
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
len - 1n
|
|
1254
|
-
)
|
|
1255
|
-
WasmI32.store8(buffer, _CHAR_CODE_DOT, 1n)
|
|
1256
|
-
WasmI32.store8(buffer + len, _CHAR_CODE_e, 1n)
|
|
1403
|
+
Memory.copy(buffer + 2n, buffer + 1n, len - 1n)
|
|
1404
|
+
WasmI32.store8(buffer, _CHAR_CODE_DOT, 1n)
|
|
1405
|
+
WasmI32.store8(buffer + len, _CHAR_CODE_e, 1n)
|
|
1257
1406
|
length += genExponent(buffer + len + 2n, kk - 1n)
|
|
1258
1407
|
length + 2n
|
|
1259
1408
|
}
|
|
1260
1409
|
}
|
|
1261
1410
|
|
|
1411
|
+
@unsafe
|
|
1262
1412
|
let dtoa_core = (buffer, value) => {
|
|
1263
1413
|
let mut value = value
|
|
1264
1414
|
let hasSign = WasmF64.lt(value, 0.W)
|
|
@@ -1272,8 +1422,10 @@ let dtoa_core = (buffer, value) => {
|
|
|
1272
1422
|
len + sign
|
|
1273
1423
|
}
|
|
1274
1424
|
|
|
1425
|
+
@unsafe
|
|
1275
1426
|
let mut _dtoa_buf = -1n
|
|
1276
1427
|
|
|
1428
|
+
@unsafe
|
|
1277
1429
|
let get_dtoa_buf = () => {
|
|
1278
1430
|
if (_dtoa_buf == -1n) {
|
|
1279
1431
|
_dtoa_buf = Memory.malloc(_MAX_DOUBLE_LENGTH)
|
|
@@ -1281,15 +1433,18 @@ let get_dtoa_buf = () => {
|
|
|
1281
1433
|
_dtoa_buf
|
|
1282
1434
|
}
|
|
1283
1435
|
|
|
1284
|
-
|
|
1436
|
+
@unsafe
|
|
1437
|
+
let isFinite = value => {
|
|
1285
1438
|
WasmF64.eq(WasmF64.sub(value, value), 0.W)
|
|
1286
1439
|
}
|
|
1287
1440
|
|
|
1288
|
-
|
|
1441
|
+
@unsafe
|
|
1442
|
+
let isNaN = value => {
|
|
1289
1443
|
WasmF64.ne(value, value)
|
|
1290
1444
|
}
|
|
1291
1445
|
|
|
1292
|
-
|
|
1446
|
+
@unsafe
|
|
1447
|
+
export let dtoa = value => {
|
|
1293
1448
|
let str = if (WasmF64.eq(value, 0.W)) {
|
|
1294
1449
|
let ret = allocateString(3n)
|
|
1295
1450
|
WasmI32.store8(ret, _CHAR_CODE_0, 8n)
|