@grain/stdlib 0.4.6 → 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 +73 -0
- package/array.gr +18 -18
- package/array.md +18 -18
- package/bigint.gr +497 -0
- package/bigint.md +811 -0
- package/buffer.gr +49 -213
- package/buffer.md +24 -17
- package/bytes.gr +100 -202
- package/bytes.md +19 -0
- package/char.gr +63 -133
- package/exception.md +6 -0
- package/float32.gr +39 -78
- package/float64.gr +43 -78
- package/hash.gr +37 -37
- package/int32.gr +152 -198
- package/int32.md +104 -0
- package/int64.gr +151 -197
- package/int64.md +104 -0
- package/list.gr +467 -70
- package/list.md +1141 -0
- package/map.gr +192 -7
- package/map.md +525 -0
- package/number.gr +30 -54
- package/number.md +3 -3
- package/option.md +1 -1
- package/package.json +3 -3
- package/pervasives.gr +499 -59
- package/pervasives.md +1116 -0
- package/queue.gr +4 -0
- package/queue.md +10 -0
- package/random.gr +196 -0
- package/random.md +179 -0
- 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 -278
- package/runtime/dataStructures.md +391 -0
- package/runtime/debug.md +6 -0
- package/runtime/equal.gr +5 -23
- package/runtime/equal.md +6 -0
- package/runtime/exception.md +30 -0
- package/runtime/gc.gr +20 -3
- package/runtime/gc.md +36 -0
- package/runtime/malloc.gr +13 -11
- package/runtime/malloc.md +55 -0
- package/runtime/numberUtils.gr +91 -41
- package/runtime/numberUtils.md +54 -0
- package/runtime/numbers.gr +1043 -391
- package/runtime/numbers.md +300 -0
- package/runtime/string.gr +136 -230
- package/runtime/string.md +24 -0
- package/runtime/stringUtils.gr +58 -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.md +71 -0
- package/runtime/unsafe/errors.md +204 -0
- package/runtime/unsafe/memory.md +54 -0
- package/runtime/unsafe/printWasm.md +24 -0
- package/runtime/unsafe/tags.gr +9 -8
- package/runtime/unsafe/tags.md +120 -0
- package/runtime/unsafe/wasmf32.md +168 -0
- package/runtime/unsafe/wasmf64.md +168 -0
- package/runtime/unsafe/wasmi32.md +282 -0
- 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 +1 -1
- package/runtime/wasi.md +839 -0
- package/set.gr +17 -8
- package/set.md +24 -21
- package/stack.gr +3 -3
- package/stack.md +4 -6
- package/string.gr +194 -329
- package/string.md +3 -3
- package/sys/file.gr +245 -429
- package/sys/process.gr +27 -45
- package/sys/random.gr +47 -16
- package/sys/random.md +38 -0
- package/sys/time.gr +11 -27
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
|
|
@@ -18,25 +18,34 @@ import 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
23
|
primitive (!): Bool -> Bool = "@not"
|
|
24
24
|
primitive (&&): (Bool, Bool) -> Bool = "@and"
|
|
25
25
|
primitive (||): (Bool, Bool) -> Bool = "@or"
|
|
26
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,12 +714,14 @@ let get_FRC_POWERS = () => {
|
|
|
695
714
|
_FRC_POWERS
|
|
696
715
|
}
|
|
697
716
|
|
|
717
|
+
@unsafe
|
|
698
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
|
|
724
|
+
@unsafe
|
|
704
725
|
export let decimalCount32 = value => {
|
|
705
726
|
if (WasmI32.ltU(value, 100000n)) {
|
|
706
727
|
if (WasmI32.ltU(value, 100n)) {
|
|
@@ -723,6 +744,7 @@ export let decimalCount32 = value => {
|
|
|
723
744
|
|
|
724
745
|
// Count number of decimals for u64 values
|
|
725
746
|
// In our case input value always greater than 2^32-1 so we can skip some parts
|
|
747
|
+
@unsafe
|
|
726
748
|
let decimalCount64High = value => {
|
|
727
749
|
if (WasmI64.ltU(value, 1000000000000000N)) {
|
|
728
750
|
if (WasmI64.ltU(value, 1000000000000N)) {
|
|
@@ -745,6 +767,7 @@ let decimalCount64High = value => {
|
|
|
745
767
|
}
|
|
746
768
|
}
|
|
747
769
|
|
|
770
|
+
@unsafe
|
|
748
771
|
let ulog_base = (num, base) => {
|
|
749
772
|
if (isPowerOf2(base)) {
|
|
750
773
|
WasmI32.divU(
|
|
@@ -770,6 +793,7 @@ let ulog_base = (num, base) => {
|
|
|
770
793
|
}
|
|
771
794
|
}
|
|
772
795
|
|
|
796
|
+
@unsafe
|
|
773
797
|
let utoa32_dec_lut = (buffer, num, offset) => {
|
|
774
798
|
let mut num = num
|
|
775
799
|
let mut offset = offset
|
|
@@ -809,6 +833,7 @@ let utoa32_dec_lut = (buffer, num, offset) => {
|
|
|
809
833
|
}
|
|
810
834
|
}
|
|
811
835
|
|
|
836
|
+
@unsafe
|
|
812
837
|
let utoa64_dec_lut = (buffer, num, offset) => {
|
|
813
838
|
let mut num = num
|
|
814
839
|
let mut offset = offset
|
|
@@ -841,6 +866,7 @@ let utoa64_dec_lut = (buffer, num, offset) => {
|
|
|
841
866
|
utoa32_dec_lut(buffer, WasmI32.wrapI64(num), offset)
|
|
842
867
|
}
|
|
843
868
|
|
|
869
|
+
@unsafe
|
|
844
870
|
let utoa_hex_lut = (buffer, num, offset) => {
|
|
845
871
|
let lut = get_HEX_DIGITS()
|
|
846
872
|
let mut num = num
|
|
@@ -863,22 +889,27 @@ let utoa_hex_lut = (buffer, num, offset) => {
|
|
|
863
889
|
}
|
|
864
890
|
}
|
|
865
891
|
|
|
892
|
+
@unsafe
|
|
866
893
|
let utoa32_dec_core = (buffer, num, offset) => {
|
|
867
894
|
utoa32_dec_lut(buffer, num, offset)
|
|
868
895
|
}
|
|
869
896
|
|
|
897
|
+
@unsafe
|
|
870
898
|
let utoa32_hex_core = (buffer, num, offset) => {
|
|
871
899
|
utoa_hex_lut(buffer, WasmI64.extendI32U(num), offset)
|
|
872
900
|
}
|
|
873
901
|
|
|
902
|
+
@unsafe
|
|
874
903
|
let utoa64_dec_core = (buffer, num, offset) => {
|
|
875
904
|
utoa64_dec_lut(buffer, num, offset)
|
|
876
905
|
}
|
|
877
906
|
|
|
907
|
+
@unsafe
|
|
878
908
|
let utoa64_hex_core = (buffer, num, offset) => {
|
|
879
909
|
utoa_hex_lut(buffer, num, offset)
|
|
880
910
|
}
|
|
881
911
|
|
|
912
|
+
@unsafe
|
|
882
913
|
let utoa64_any_core = (buffer, num, offset, radix) => {
|
|
883
914
|
let lut = get_ANY_DIGITS()
|
|
884
915
|
let base = WasmI64.extendI32U(radix)
|
|
@@ -915,13 +946,14 @@ let utoa64_any_core = (buffer, num, offset, radix) => {
|
|
|
915
946
|
}
|
|
916
947
|
}
|
|
917
948
|
|
|
949
|
+
@unsafe
|
|
918
950
|
export let utoa32Buffered = (buf, value, radix) => {
|
|
919
951
|
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
|
|
920
952
|
throw Exception.InvalidArgument(
|
|
921
953
|
"toString() radix argument must be between 2 and 36"
|
|
922
954
|
)
|
|
923
955
|
}
|
|
924
|
-
|
|
956
|
+
if (WasmI32.eqz(value)) {
|
|
925
957
|
WasmI32.store8(buf, _CHAR_CODE_0, 0n)
|
|
926
958
|
} else if (radix == 10n) {
|
|
927
959
|
let decimals = decimalCount32(value)
|
|
@@ -936,33 +968,34 @@ export let utoa32Buffered = (buf, value, radix) => {
|
|
|
936
968
|
}
|
|
937
969
|
}
|
|
938
970
|
|
|
971
|
+
@unsafe
|
|
939
972
|
export let utoa32 = (value, radix) => {
|
|
940
973
|
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
|
|
941
974
|
throw Exception.InvalidArgument(
|
|
942
975
|
"toString() radix argument must be between 2 and 36"
|
|
943
976
|
)
|
|
944
977
|
}
|
|
945
|
-
|
|
946
|
-
|
|
978
|
+
if (WasmI32.eqz(value)) {
|
|
979
|
+
"0"
|
|
947
980
|
} else if (radix == 10n) {
|
|
948
981
|
let decimals = decimalCount32(value)
|
|
949
982
|
let out = allocateString(decimals)
|
|
950
983
|
utoa32_dec_core(out, value, decimals)
|
|
951
|
-
out
|
|
984
|
+
WasmI32.toGrain(out): String
|
|
952
985
|
} else if (radix == 16n) {
|
|
953
986
|
let decimals = WasmI32.shrU(31n - WasmI32.clz(value), 2n) + 1n
|
|
954
987
|
let out = allocateString(decimals)
|
|
955
988
|
utoa32_hex_core(out, value, decimals)
|
|
956
|
-
out
|
|
989
|
+
WasmI32.toGrain(out): String
|
|
957
990
|
} else {
|
|
958
991
|
let decimals = ulog_base(WasmI64.extendI32U(value), radix)
|
|
959
992
|
let out = allocateString(decimals)
|
|
960
993
|
utoa64_any_core(out, WasmI64.extendI32U(value), decimals, radix)
|
|
961
|
-
out
|
|
994
|
+
WasmI32.toGrain(out): String
|
|
962
995
|
}
|
|
963
|
-
WasmI32.toGrain(str): String
|
|
964
996
|
}
|
|
965
997
|
|
|
998
|
+
@unsafe
|
|
966
999
|
export let itoa32 = (value, radix) => {
|
|
967
1000
|
let mut value = value
|
|
968
1001
|
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
|
|
@@ -970,67 +1003,71 @@ export let itoa32 = (value, radix) => {
|
|
|
970
1003
|
"toString() radix argument must be between 2 and 36"
|
|
971
1004
|
)
|
|
972
1005
|
}
|
|
973
|
-
let mut out = 0n
|
|
974
1006
|
let sign = WasmI32.shrU(value, 31n)
|
|
975
1007
|
|
|
976
1008
|
if (WasmI32.ne(sign, 0n)) value = 0n - value
|
|
977
1009
|
|
|
978
|
-
if (WasmI32.eqz(value)) {
|
|
979
|
-
|
|
1010
|
+
let out = if (WasmI32.eqz(value)) {
|
|
1011
|
+
"0"
|
|
980
1012
|
} else if (radix == 10n) {
|
|
981
1013
|
let decimals = decimalCount32(value) + sign
|
|
982
|
-
out = allocateString(decimals)
|
|
1014
|
+
let out = allocateString(decimals)
|
|
983
1015
|
utoa32_dec_core(out + 8n, value, decimals)
|
|
1016
|
+
WasmI32.toGrain(out): String
|
|
984
1017
|
} else if (radix == 16n) {
|
|
985
1018
|
let decimals = WasmI32.shrU(31n - WasmI32.clz(value), 2n) + 1n + sign
|
|
986
|
-
out = allocateString(decimals)
|
|
1019
|
+
let out = allocateString(decimals)
|
|
987
1020
|
utoa32_hex_core(out + 8n, value, decimals)
|
|
1021
|
+
WasmI32.toGrain(out): String
|
|
988
1022
|
} else {
|
|
989
1023
|
let val64 = WasmI64.extendI32U(value)
|
|
990
1024
|
let decimals = ulog_base(val64, radix) + sign
|
|
991
|
-
out = allocateString(decimals)
|
|
1025
|
+
let out = allocateString(decimals)
|
|
992
1026
|
utoa64_any_core(out + 8n, val64, decimals, radix)
|
|
1027
|
+
WasmI32.toGrain(out): String
|
|
993
1028
|
}
|
|
994
|
-
if (WasmI32.ne(sign, 0n))
|
|
995
|
-
|
|
1029
|
+
if (WasmI32.ne(sign, 0n))
|
|
1030
|
+
WasmI32.store8(WasmI32.fromGrain(out), _CHAR_CODE_MINUS, 8n)
|
|
1031
|
+
out
|
|
996
1032
|
}
|
|
997
1033
|
|
|
1034
|
+
@unsafe
|
|
998
1035
|
export let utoa64 = (value, radix) => {
|
|
999
1036
|
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
|
|
1000
1037
|
throw Exception.InvalidArgument(
|
|
1001
1038
|
"toString() radix argument must be between 2 and 36"
|
|
1002
1039
|
)
|
|
1003
1040
|
}
|
|
1004
|
-
|
|
1005
|
-
|
|
1041
|
+
if (WasmI64.eqz(value)) {
|
|
1042
|
+
"0"
|
|
1006
1043
|
} else if (radix == 10n) {
|
|
1007
1044
|
if (WasmI64.leU(value, _I32_MAX)) {
|
|
1008
1045
|
let val32 = WasmI32.wrapI64(value)
|
|
1009
1046
|
let decimals = decimalCount32(val32)
|
|
1010
1047
|
let out = allocateString(decimals)
|
|
1011
1048
|
utoa32_dec_core(out + 8n, val32, decimals)
|
|
1012
|
-
out
|
|
1049
|
+
WasmI32.toGrain(out): String
|
|
1013
1050
|
} else {
|
|
1014
1051
|
let decimals = decimalCount64High(value)
|
|
1015
1052
|
let out = allocateString(decimals)
|
|
1016
1053
|
utoa64_dec_core(out + 8n, value, decimals)
|
|
1017
|
-
out
|
|
1054
|
+
WasmI32.toGrain(out): String
|
|
1018
1055
|
}
|
|
1019
1056
|
} else if (radix == 16n) {
|
|
1020
1057
|
let decimals = WasmI32.shrU(63n - WasmI32.wrapI64(WasmI64.clz(value)), 2n) +
|
|
1021
1058
|
1n
|
|
1022
1059
|
let out = allocateString(decimals)
|
|
1023
1060
|
utoa64_hex_core(out + 8n, value, decimals)
|
|
1024
|
-
out
|
|
1061
|
+
WasmI32.toGrain(out): String
|
|
1025
1062
|
} else {
|
|
1026
1063
|
let decimals = ulog_base(value, radix)
|
|
1027
1064
|
let out = allocateString(decimals)
|
|
1028
1065
|
utoa64_any_core(out + 8n, value, decimals, radix)
|
|
1029
|
-
out
|
|
1066
|
+
WasmI32.toGrain(out): String
|
|
1030
1067
|
}
|
|
1031
|
-
WasmI32.toGrain(str): String
|
|
1032
1068
|
}
|
|
1033
1069
|
|
|
1070
|
+
@unsafe
|
|
1034
1071
|
export let itoa64 = (value, radix) => {
|
|
1035
1072
|
if (WasmI32.ltS(radix, 2n) || WasmI32.gtS(radix, 36n)) {
|
|
1036
1073
|
throw Exception.InvalidArgument(
|
|
@@ -1039,41 +1076,46 @@ export let itoa64 = (value, radix) => {
|
|
|
1039
1076
|
}
|
|
1040
1077
|
|
|
1041
1078
|
let mut value = value
|
|
1042
|
-
let mut out = 0n
|
|
1043
1079
|
|
|
1044
1080
|
let sign = WasmI32.wrapI64(WasmI64.shrU(value, 63N))
|
|
1045
1081
|
if (sign != 0n) value = WasmI64.sub(0N, value)
|
|
1046
1082
|
|
|
1047
|
-
if (WasmI64.eqz(value)) {
|
|
1048
|
-
|
|
1083
|
+
let out = if (WasmI64.eqz(value)) {
|
|
1084
|
+
"0"
|
|
1049
1085
|
} else if (radix == 10n) {
|
|
1050
1086
|
if (WasmI64.leU(value, _I32_MAX)) {
|
|
1051
1087
|
let val32 = WasmI32.wrapI64(value)
|
|
1052
1088
|
let decimals = decimalCount32(val32) + sign
|
|
1053
|
-
out = allocateString(decimals)
|
|
1089
|
+
let out = allocateString(decimals)
|
|
1054
1090
|
utoa32_dec_core(out + 8n, val32, decimals)
|
|
1091
|
+
WasmI32.toGrain(out): String
|
|
1055
1092
|
} else {
|
|
1056
1093
|
let decimals = decimalCount64High(value) + sign
|
|
1057
|
-
out = allocateString(decimals)
|
|
1094
|
+
let out = allocateString(decimals)
|
|
1058
1095
|
utoa64_dec_core(out + 8n, value, decimals)
|
|
1096
|
+
WasmI32.toGrain(out): String
|
|
1059
1097
|
}
|
|
1060
1098
|
} else if (radix == 16n) {
|
|
1061
1099
|
let decimals = WasmI32.shrU(63n - WasmI32.wrapI64(WasmI64.clz(value)), 2n) +
|
|
1062
1100
|
1n +
|
|
1063
1101
|
sign
|
|
1064
|
-
out = allocateString(decimals)
|
|
1102
|
+
let out = allocateString(decimals)
|
|
1065
1103
|
utoa64_hex_core(out + 8n, value, decimals)
|
|
1104
|
+
WasmI32.toGrain(out): String
|
|
1066
1105
|
} else {
|
|
1067
1106
|
let decimals = ulog_base(value, radix) + sign
|
|
1068
|
-
out = allocateString(decimals)
|
|
1107
|
+
let out = allocateString(decimals)
|
|
1069
1108
|
utoa64_any_core(out + 8n, value, decimals, radix)
|
|
1109
|
+
WasmI32.toGrain(out): String
|
|
1070
1110
|
}
|
|
1071
|
-
if (sign != 0n) WasmI32.store8(out, _CHAR_CODE_MINUS, 8n)
|
|
1072
|
-
|
|
1111
|
+
if (sign != 0n) WasmI32.store8(WasmI32.fromGrain(out), _CHAR_CODE_MINUS, 8n)
|
|
1112
|
+
out
|
|
1073
1113
|
}
|
|
1074
1114
|
|
|
1115
|
+
@unsafe
|
|
1075
1116
|
let mut _K = 0n
|
|
1076
1117
|
|
|
1118
|
+
@unsafe
|
|
1077
1119
|
let umul64f = (u, v) => {
|
|
1078
1120
|
let u0 = WasmI64.and(u, 0xFFFFFFFFN)
|
|
1079
1121
|
let v0 = WasmI64.and(v, 0xFFFFFFFFN)
|
|
@@ -1093,10 +1135,12 @@ let umul64f = (u, v) => {
|
|
|
1093
1135
|
WasmI64.add(WasmI64.add(WasmI64.mul(u1, v1), t), w)
|
|
1094
1136
|
}
|
|
1095
1137
|
|
|
1138
|
+
@unsafe
|
|
1096
1139
|
let umul64e = (e1, e2) => {
|
|
1097
1140
|
e1 + e2 + 64n // where 64 is significand size
|
|
1098
1141
|
}
|
|
1099
1142
|
|
|
1143
|
+
@unsafe
|
|
1100
1144
|
let grisuRound = (buffer, len, delta, rest, ten_kappa, wp_w) => {
|
|
1101
1145
|
let mut lastp = buffer + len - 1n
|
|
1102
1146
|
let mut digit = WasmI32.load8U(lastp, 0n)
|
|
@@ -1104,13 +1148,11 @@ let grisuRound = (buffer, len, delta, rest, ten_kappa, wp_w) => {
|
|
|
1104
1148
|
while (
|
|
1105
1149
|
WasmI64.ltU(rest, wp_w) &&
|
|
1106
1150
|
WasmI64.geU(WasmI64.sub(delta, rest), ten_kappa) &&
|
|
1107
|
-
(
|
|
1108
|
-
WasmI64.ltU(WasmI64.add(rest, ten_kappa), wp_w) ||
|
|
1151
|
+
(WasmI64.ltU(WasmI64.add(rest, ten_kappa), wp_w) ||
|
|
1109
1152
|
WasmI64.gtU(
|
|
1110
1153
|
WasmI64.sub(wp_w, rest),
|
|
1111
1154
|
WasmI64.sub(WasmI64.add(rest, ten_kappa), wp_w)
|
|
1112
|
-
)
|
|
1113
|
-
)
|
|
1155
|
+
))
|
|
1114
1156
|
) {
|
|
1115
1157
|
digit -= 1n
|
|
1116
1158
|
rest = WasmI64.add(rest, ten_kappa)
|
|
@@ -1118,7 +1160,8 @@ let grisuRound = (buffer, len, delta, rest, ten_kappa, wp_w) => {
|
|
|
1118
1160
|
WasmI32.store8(lastp, digit, 0n)
|
|
1119
1161
|
}
|
|
1120
1162
|
|
|
1121
|
-
|
|
1163
|
+
@unsafe
|
|
1164
|
+
let genDigits = (buffer, w_frc, mp_frc, mp_exp, delta, sign) => {
|
|
1122
1165
|
let mut delta = delta
|
|
1123
1166
|
let one_exp = 0n - mp_exp
|
|
1124
1167
|
let one_frc = WasmI64.shl(1N, WasmI64.extendI32U(one_exp))
|
|
@@ -1241,10 +1284,10 @@ let genDigits = (buffer, w_frc, w_exp, mp_frc, mp_exp, delta, sign) => {
|
|
|
1241
1284
|
break
|
|
1242
1285
|
}
|
|
1243
1286
|
}
|
|
1244
|
-
|
|
1245
1287
|
len
|
|
1246
1288
|
}
|
|
1247
1289
|
|
|
1290
|
+
@unsafe
|
|
1248
1291
|
let genExponent = (buffer, k) => {
|
|
1249
1292
|
let mut k = k
|
|
1250
1293
|
let sign = WasmI32.ltS(k, 0n)
|
|
@@ -1255,6 +1298,7 @@ let genExponent = (buffer, k) => {
|
|
|
1255
1298
|
decimals
|
|
1256
1299
|
}
|
|
1257
1300
|
|
|
1301
|
+
@unsafe
|
|
1258
1302
|
let grisu2 = (value, buffer, sign) => {
|
|
1259
1303
|
// frexp routine
|
|
1260
1304
|
let uv = WasmI64.reinterpretF64(value)
|
|
@@ -1310,7 +1354,6 @@ let grisu2 = (value, buffer, sign) => {
|
|
|
1310
1354
|
let exp_pow = _exp_pow
|
|
1311
1355
|
|
|
1312
1356
|
let w_frc = umul64f(frc, frc_pow)
|
|
1313
|
-
let w_exp = umul64e(exp, exp_pow)
|
|
1314
1357
|
|
|
1315
1358
|
let wp_frc = WasmI64.sub(umul64f(_frc_plus, frc_pow), 1N)
|
|
1316
1359
|
let wp_exp = umul64e(_exp, exp_pow)
|
|
@@ -1318,9 +1361,10 @@ let grisu2 = (value, buffer, sign) => {
|
|
|
1318
1361
|
let wm_frc = WasmI64.add(umul64f(_frc_minus, frc_pow), 1N)
|
|
1319
1362
|
let delta = WasmI64.sub(wp_frc, wm_frc)
|
|
1320
1363
|
|
|
1321
|
-
genDigits(buffer, w_frc,
|
|
1364
|
+
genDigits(buffer, w_frc, wp_frc, wp_exp, delta, sign)
|
|
1322
1365
|
}
|
|
1323
1366
|
|
|
1367
|
+
@unsafe
|
|
1324
1368
|
let prettify = (buffer, length, k) => {
|
|
1325
1369
|
let mut length = length
|
|
1326
1370
|
let kk = length + k
|
|
@@ -1364,6 +1408,7 @@ let prettify = (buffer, length, k) => {
|
|
|
1364
1408
|
}
|
|
1365
1409
|
}
|
|
1366
1410
|
|
|
1411
|
+
@unsafe
|
|
1367
1412
|
let dtoa_core = (buffer, value) => {
|
|
1368
1413
|
let mut value = value
|
|
1369
1414
|
let hasSign = WasmF64.lt(value, 0.W)
|
|
@@ -1377,8 +1422,10 @@ let dtoa_core = (buffer, value) => {
|
|
|
1377
1422
|
len + sign
|
|
1378
1423
|
}
|
|
1379
1424
|
|
|
1425
|
+
@unsafe
|
|
1380
1426
|
let mut _dtoa_buf = -1n
|
|
1381
1427
|
|
|
1428
|
+
@unsafe
|
|
1382
1429
|
let get_dtoa_buf = () => {
|
|
1383
1430
|
if (_dtoa_buf == -1n) {
|
|
1384
1431
|
_dtoa_buf = Memory.malloc(_MAX_DOUBLE_LENGTH)
|
|
@@ -1386,14 +1433,17 @@ let get_dtoa_buf = () => {
|
|
|
1386
1433
|
_dtoa_buf
|
|
1387
1434
|
}
|
|
1388
1435
|
|
|
1436
|
+
@unsafe
|
|
1389
1437
|
let isFinite = value => {
|
|
1390
1438
|
WasmF64.eq(WasmF64.sub(value, value), 0.W)
|
|
1391
1439
|
}
|
|
1392
1440
|
|
|
1441
|
+
@unsafe
|
|
1393
1442
|
let isNaN = value => {
|
|
1394
1443
|
WasmF64.ne(value, value)
|
|
1395
1444
|
}
|
|
1396
1445
|
|
|
1446
|
+
@unsafe
|
|
1397
1447
|
export let dtoa = value => {
|
|
1398
1448
|
let str = if (WasmF64.eq(value, 0.W)) {
|
|
1399
1449
|
let ret = allocateString(3n)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
### NumberUtils.**_MAX_DOUBLE_LENGTH**
|
|
2
|
+
|
|
3
|
+
```grain
|
|
4
|
+
_MAX_DOUBLE_LENGTH : WasmI32
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
### NumberUtils.**get_HEX_DIGITS**
|
|
8
|
+
|
|
9
|
+
```grain
|
|
10
|
+
get_HEX_DIGITS : () -> WasmI32
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### NumberUtils.**decimalCount32**
|
|
14
|
+
|
|
15
|
+
```grain
|
|
16
|
+
decimalCount32 : WasmI32 -> WasmI32
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### NumberUtils.**utoa32Buffered**
|
|
20
|
+
|
|
21
|
+
```grain
|
|
22
|
+
utoa32Buffered : (WasmI32, WasmI32, WasmI32) -> Void
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### NumberUtils.**utoa32**
|
|
26
|
+
|
|
27
|
+
```grain
|
|
28
|
+
utoa32 : (WasmI32, WasmI32) -> String
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### NumberUtils.**itoa32**
|
|
32
|
+
|
|
33
|
+
```grain
|
|
34
|
+
itoa32 : (WasmI32, WasmI32) -> String
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### NumberUtils.**utoa64**
|
|
38
|
+
|
|
39
|
+
```grain
|
|
40
|
+
utoa64 : (WasmI64, WasmI32) -> String
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### NumberUtils.**itoa64**
|
|
44
|
+
|
|
45
|
+
```grain
|
|
46
|
+
itoa64 : (WasmI64, WasmI32) -> String
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### NumberUtils.**dtoa**
|
|
50
|
+
|
|
51
|
+
```grain
|
|
52
|
+
dtoa : WasmF64 -> String
|
|
53
|
+
```
|
|
54
|
+
|