@grain/stdlib 0.5.13 → 0.6.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 +193 -0
- package/LICENSE +1 -1
- package/README.md +25 -2
- package/array.gr +1512 -199
- package/array.md +2032 -94
- package/bigint.gr +239 -140
- package/bigint.md +450 -106
- package/buffer.gr +595 -102
- package/buffer.md +903 -145
- package/bytes.gr +401 -110
- package/bytes.md +551 -63
- package/char.gr +228 -49
- package/char.md +373 -7
- package/exception.gr +26 -12
- package/exception.md +29 -5
- package/float32.gr +130 -109
- package/float32.md +185 -57
- package/float64.gr +112 -99
- package/float64.md +185 -57
- package/hash.gr +47 -37
- package/hash.md +21 -3
- package/int16.gr +430 -0
- package/int16.md +618 -0
- package/int32.gr +200 -269
- package/int32.md +254 -289
- package/int64.gr +142 -225
- package/int64.md +254 -289
- package/int8.gr +511 -0
- package/int8.md +786 -0
- package/json.gr +2084 -0
- package/json.md +608 -0
- package/list.gr +120 -68
- package/list.md +125 -80
- package/map.gr +560 -57
- package/map.md +672 -56
- package/marshal.gr +239 -227
- package/marshal.md +36 -4
- package/number.gr +626 -676
- package/number.md +738 -153
- package/option.gr +33 -35
- package/option.md +58 -42
- package/package.json +2 -2
- package/path.gr +148 -187
- package/path.md +47 -96
- package/pervasives.gr +75 -416
- package/pervasives.md +85 -180
- package/priorityqueue.gr +433 -74
- package/priorityqueue.md +422 -54
- package/queue.gr +362 -80
- package/queue.md +433 -38
- package/random.gr +67 -75
- package/random.md +68 -40
- package/range.gr +135 -63
- package/range.md +198 -43
- package/rational.gr +284 -0
- package/rational.md +545 -0
- package/regex.gr +933 -1066
- package/regex.md +59 -60
- package/result.gr +23 -25
- package/result.md +54 -39
- package/runtime/atof/common.gr +78 -82
- package/runtime/atof/common.md +22 -10
- package/runtime/atof/decimal.gr +102 -127
- package/runtime/atof/decimal.md +28 -7
- package/runtime/atof/lemire.gr +56 -71
- package/runtime/atof/lemire.md +9 -1
- package/runtime/atof/parse.gr +83 -110
- package/runtime/atof/parse.md +12 -2
- package/runtime/atof/slow.gr +28 -35
- package/runtime/atof/slow.md +9 -1
- package/runtime/atof/table.gr +19 -18
- package/runtime/atof/table.md +10 -2
- package/runtime/atoi/parse.gr +153 -136
- package/runtime/atoi/parse.md +50 -1
- package/runtime/bigint.gr +410 -517
- package/runtime/bigint.md +71 -57
- package/runtime/compare.gr +176 -85
- package/runtime/compare.md +31 -1
- package/runtime/dataStructures.gr +144 -32
- package/runtime/dataStructures.md +267 -31
- package/runtime/debugPrint.gr +34 -15
- package/runtime/debugPrint.md +37 -5
- package/runtime/equal.gr +53 -52
- package/runtime/equal.md +30 -1
- package/runtime/exception.gr +38 -47
- package/runtime/exception.md +10 -8
- package/runtime/gc.gr +23 -152
- package/runtime/gc.md +13 -17
- package/runtime/malloc.gr +31 -31
- package/runtime/malloc.md +11 -3
- package/runtime/numberUtils.gr +191 -172
- package/runtime/numberUtils.md +17 -9
- package/runtime/numbers.gr +1695 -1021
- package/runtime/numbers.md +1098 -134
- package/runtime/string.gr +540 -242
- package/runtime/string.md +76 -6
- package/runtime/unsafe/constants.gr +30 -13
- package/runtime/unsafe/constants.md +80 -0
- package/runtime/unsafe/conv.gr +55 -28
- package/runtime/unsafe/conv.md +41 -9
- package/runtime/unsafe/memory.gr +10 -30
- package/runtime/unsafe/memory.md +15 -19
- package/runtime/unsafe/tags.gr +37 -21
- package/runtime/unsafe/tags.md +88 -8
- package/runtime/unsafe/wasmf32.gr +30 -36
- package/runtime/unsafe/wasmf32.md +64 -56
- package/runtime/unsafe/wasmf64.gr +30 -36
- package/runtime/unsafe/wasmf64.md +64 -56
- package/runtime/unsafe/wasmi32.gr +49 -66
- package/runtime/unsafe/wasmi32.md +102 -94
- package/runtime/unsafe/wasmi64.gr +52 -79
- package/runtime/unsafe/wasmi64.md +108 -100
- package/runtime/utils/printing.gr +13 -15
- package/runtime/utils/printing.md +11 -3
- package/runtime/wasi.gr +294 -295
- package/runtime/wasi.md +62 -42
- package/set.gr +574 -64
- package/set.md +634 -54
- package/stack.gr +181 -64
- package/stack.md +271 -42
- package/string.gr +453 -533
- package/string.md +241 -151
- package/uint16.gr +369 -0
- package/uint16.md +585 -0
- package/uint32.gr +470 -0
- package/uint32.md +737 -0
- package/uint64.gr +471 -0
- package/uint64.md +737 -0
- package/uint8.gr +369 -0
- package/uint8.md +585 -0
- package/uri.gr +1093 -0
- package/uri.md +477 -0
- package/{sys → wasi}/file.gr +914 -500
- package/{sys → wasi}/file.md +454 -50
- package/wasi/process.gr +292 -0
- package/{sys → wasi}/process.md +164 -6
- package/wasi/random.gr +77 -0
- package/wasi/random.md +80 -0
- package/{sys → wasi}/time.gr +15 -22
- package/{sys → wasi}/time.md +5 -5
- package/immutablearray.gr +0 -929
- package/immutablearray.md +0 -1038
- package/immutablemap.gr +0 -493
- package/immutablemap.md +0 -479
- package/immutablepriorityqueue.gr +0 -360
- package/immutablepriorityqueue.md +0 -291
- package/immutableset.gr +0 -498
- package/immutableset.md +0 -449
- package/runtime/debug.gr +0 -2
- package/runtime/debug.md +0 -6
- package/runtime/unsafe/errors.gr +0 -36
- package/runtime/unsafe/errors.md +0 -204
- package/sys/process.gr +0 -254
- package/sys/random.gr +0 -79
- package/sys/random.md +0 -66
package/bigint.gr
CHANGED
|
@@ -1,45 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Utilities for working with the BigInt type.
|
|
3
|
+
*
|
|
4
|
+
* @example from "bigint" include Bigint
|
|
5
|
+
*
|
|
6
|
+
* @example 9223372036854775809t
|
|
7
|
+
* @example 0t
|
|
8
|
+
* @example -9223372036854775809t
|
|
4
9
|
*
|
|
5
10
|
* @since v0.5.0
|
|
6
11
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
module BigInt
|
|
13
|
+
|
|
14
|
+
from "runtime/unsafe/wasmi32" include WasmI32
|
|
15
|
+
from "runtime/unsafe/memory" include Memory
|
|
16
|
+
from "runtime/bigint" include Bigint as BI
|
|
17
|
+
from "runtime/dataStructures" include DataStructures as DS
|
|
11
18
|
|
|
12
|
-
|
|
19
|
+
from "runtime/numbers" include Numbers
|
|
20
|
+
use Numbers.{
|
|
13
21
|
coerceNumberToBigInt as fromNumber,
|
|
14
22
|
coerceBigIntToNumber as toNumber,
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @section Conversions: Functions for converting between Numbers and the BigInt type.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Converts a Number to a BigInt.
|
|
23
|
-
*
|
|
24
|
-
* @param number: The value to convert
|
|
25
|
-
* @returns The Number represented as a BigInt
|
|
26
|
-
*
|
|
27
|
-
* @since v0.5.0
|
|
28
|
-
*/
|
|
29
|
-
export fromNumber
|
|
30
|
-
/**
|
|
31
|
-
* Converts a BigInt to a Number.
|
|
32
|
-
*
|
|
33
|
-
* @param num: The value to convert
|
|
34
|
-
* @returns The BigInt represented as a Number
|
|
35
|
-
*
|
|
36
|
-
* @since v0.5.0
|
|
37
|
-
*/
|
|
38
|
-
export toNumber
|
|
23
|
+
}
|
|
39
24
|
|
|
40
|
-
|
|
41
|
-
* @section Operations: Mathematical operations for BigInt values.
|
|
42
|
-
*/
|
|
25
|
+
provide { fromNumber, toNumber }
|
|
43
26
|
|
|
44
27
|
/**
|
|
45
28
|
* Increments the value by one.
|
|
@@ -47,10 +30,13 @@ export toNumber
|
|
|
47
30
|
* @param num: The value to increment
|
|
48
31
|
* @returns The incremented value
|
|
49
32
|
*
|
|
33
|
+
* @example BigInt.incr(1t) == 2t
|
|
34
|
+
* @example BigInt.incr(-2t) == -1t
|
|
35
|
+
*
|
|
50
36
|
* @since v0.5.0
|
|
51
37
|
*/
|
|
52
38
|
@unsafe
|
|
53
|
-
|
|
39
|
+
provide let incr = (num: BigInt) => {
|
|
54
40
|
WasmI32.toGrain(BI.incr(WasmI32.fromGrain(num))): BigInt
|
|
55
41
|
}
|
|
56
42
|
|
|
@@ -60,10 +46,13 @@ export let incr = (num: BigInt) => {
|
|
|
60
46
|
* @param num: The value to decrement
|
|
61
47
|
* @returns The decremented value
|
|
62
48
|
*
|
|
49
|
+
* @example BigInt.decr(2t) == 1t
|
|
50
|
+
* @example BigInt.decr(-2t) == -3t
|
|
51
|
+
*
|
|
63
52
|
* @since v0.5.0
|
|
64
53
|
*/
|
|
65
54
|
@unsafe
|
|
66
|
-
|
|
55
|
+
provide let decr = (num: BigInt) => {
|
|
67
56
|
WasmI32.toGrain(BI.decr(WasmI32.fromGrain(num))): BigInt
|
|
68
57
|
}
|
|
69
58
|
|
|
@@ -73,10 +62,13 @@ export let decr = (num: BigInt) => {
|
|
|
73
62
|
* @param num: The operand
|
|
74
63
|
* @returns The negated operand
|
|
75
64
|
*
|
|
65
|
+
* @example BigInt.neg(1t) == -1t
|
|
66
|
+
* @example BigInt.neg(-1t) == 1t
|
|
67
|
+
*
|
|
76
68
|
* @since v0.5.0
|
|
77
69
|
*/
|
|
78
70
|
@unsafe
|
|
79
|
-
|
|
71
|
+
provide let neg = (num: BigInt) => {
|
|
80
72
|
WasmI32.toGrain(BI.negate(WasmI32.fromGrain(num))): BigInt
|
|
81
73
|
}
|
|
82
74
|
|
|
@@ -86,10 +78,13 @@ export let neg = (num: BigInt) => {
|
|
|
86
78
|
* @param num: The operand
|
|
87
79
|
* @returns The operand's absolute value
|
|
88
80
|
*
|
|
81
|
+
* @example BigInt.abs(1t) == 1t
|
|
82
|
+
* @example BigInt.abs(-1t) == 1t
|
|
83
|
+
*
|
|
89
84
|
* @since v0.5.0
|
|
90
85
|
*/
|
|
91
86
|
@unsafe
|
|
92
|
-
|
|
87
|
+
provide let abs = (num: BigInt) => {
|
|
93
88
|
WasmI32.toGrain(BI.abs(WasmI32.fromGrain(num))): BigInt
|
|
94
89
|
}
|
|
95
90
|
|
|
@@ -100,13 +95,17 @@ export let abs = (num: BigInt) => {
|
|
|
100
95
|
* @param num2: The second operand
|
|
101
96
|
* @returns The sum of the two operands
|
|
102
97
|
*
|
|
103
|
-
* @
|
|
98
|
+
* @example
|
|
99
|
+
* use BigInt.{ (+) }
|
|
100
|
+
* assert 1t + 1t == 2t
|
|
101
|
+
*
|
|
102
|
+
* @since v0.6.0
|
|
103
|
+
* @history v0.5.0: Originally named `add`
|
|
104
104
|
*/
|
|
105
105
|
@unsafe
|
|
106
|
-
|
|
107
|
-
WasmI32.toGrain(
|
|
108
|
-
|
|
109
|
-
): BigInt
|
|
106
|
+
provide let (+) = (num1: BigInt, num2: BigInt) => {
|
|
107
|
+
WasmI32.toGrain(BI.add(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
|
|
108
|
+
BigInt
|
|
110
109
|
}
|
|
111
110
|
|
|
112
111
|
/**
|
|
@@ -116,13 +115,17 @@ export let add = (num1: BigInt, num2: BigInt) => {
|
|
|
116
115
|
* @param num2: The second operand
|
|
117
116
|
* @returns The difference of the two operands
|
|
118
117
|
*
|
|
119
|
-
* @
|
|
118
|
+
* @example
|
|
119
|
+
* use BigInt.{ (-) }
|
|
120
|
+
* assert 3t - 1t == 2t
|
|
121
|
+
*
|
|
122
|
+
* @since v0.6.0
|
|
123
|
+
* @history v0.5.0: Originally named `sub`
|
|
120
124
|
*/
|
|
121
125
|
@unsafe
|
|
122
|
-
|
|
123
|
-
WasmI32.toGrain(
|
|
124
|
-
|
|
125
|
-
): BigInt
|
|
126
|
+
provide let (-) = (num1: BigInt, num2: BigInt) => {
|
|
127
|
+
WasmI32.toGrain(BI.sub(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
|
|
128
|
+
BigInt
|
|
126
129
|
}
|
|
127
130
|
|
|
128
131
|
/**
|
|
@@ -132,13 +135,17 @@ export let sub = (num1: BigInt, num2: BigInt) => {
|
|
|
132
135
|
* @param num2: The second operand
|
|
133
136
|
* @returns The product of the two operands
|
|
134
137
|
*
|
|
135
|
-
* @
|
|
138
|
+
* @example
|
|
139
|
+
* use BigInt.{ (*) }
|
|
140
|
+
* assert 3t * 3t == 9t
|
|
141
|
+
*
|
|
142
|
+
* @since v0.6.0
|
|
143
|
+
* @history v0.5.0: Originally named `mul`
|
|
136
144
|
*/
|
|
137
145
|
@unsafe
|
|
138
|
-
|
|
139
|
-
WasmI32.toGrain(
|
|
140
|
-
|
|
141
|
-
): BigInt
|
|
146
|
+
provide let (*) = (num1: BigInt, num2: BigInt) => {
|
|
147
|
+
WasmI32.toGrain(BI.mul(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
|
|
148
|
+
BigInt
|
|
142
149
|
}
|
|
143
150
|
|
|
144
151
|
// For further reading on Truncated vs. Floored division: https://en.wikipedia.org/wiki/Modulo_operation
|
|
@@ -151,13 +158,17 @@ export let mul = (num1: BigInt, num2: BigInt) => {
|
|
|
151
158
|
* @param num2: The second operand
|
|
152
159
|
* @returns The quotient of its operands
|
|
153
160
|
*
|
|
154
|
-
* @
|
|
161
|
+
* @example
|
|
162
|
+
* use BigInt.{ (/) }
|
|
163
|
+
* assert 9t / 3t == 3t
|
|
164
|
+
*
|
|
165
|
+
* @since v0.6.0
|
|
166
|
+
* @history v0.5.0: Originally named `div`
|
|
155
167
|
*/
|
|
156
168
|
@unsafe
|
|
157
|
-
|
|
158
|
-
WasmI32.toGrain(
|
|
159
|
-
|
|
160
|
-
): BigInt
|
|
169
|
+
provide let (/) = (num1: BigInt, num2: BigInt) => {
|
|
170
|
+
WasmI32.toGrain(BI.quot(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
|
|
171
|
+
BigInt
|
|
161
172
|
}
|
|
162
173
|
|
|
163
174
|
/**
|
|
@@ -168,13 +179,14 @@ export let div = (num1: BigInt, num2: BigInt) => {
|
|
|
168
179
|
* @param num2: The second operand
|
|
169
180
|
* @returns The remainder of its operands
|
|
170
181
|
*
|
|
182
|
+
* @example BigInt.rem(3t, 2t) == 1t
|
|
183
|
+
*
|
|
171
184
|
* @since v0.5.0
|
|
172
185
|
*/
|
|
173
186
|
@unsafe
|
|
174
|
-
|
|
175
|
-
WasmI32.toGrain(
|
|
176
|
-
|
|
177
|
-
): BigInt
|
|
187
|
+
provide let rem = (num1: BigInt, num2: BigInt) => {
|
|
188
|
+
WasmI32.toGrain(BI.rem(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
|
|
189
|
+
BigInt
|
|
178
190
|
}
|
|
179
191
|
|
|
180
192
|
/**
|
|
@@ -184,10 +196,12 @@ export let rem = (num1: BigInt, num2: BigInt) => {
|
|
|
184
196
|
* @param num2: The second operand
|
|
185
197
|
* @returns The quotient and remainder of its operands
|
|
186
198
|
*
|
|
199
|
+
* @example BigInt.quotRem(7t, 2t) == (3t, 1t))
|
|
200
|
+
*
|
|
187
201
|
* @since v0.5.0
|
|
188
202
|
*/
|
|
189
203
|
@unsafe
|
|
190
|
-
|
|
204
|
+
provide let quotRem = (num1: BigInt, num2: BigInt) => {
|
|
191
205
|
let dest = Memory.malloc(8n)
|
|
192
206
|
BI.quotRem(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2), dest)
|
|
193
207
|
let q = WasmI32.toGrain(WasmI32.load(dest, 0n)): BigInt
|
|
@@ -203,19 +217,16 @@ export let quotRem = (num1: BigInt, num2: BigInt) => {
|
|
|
203
217
|
* @param num2: The second operand
|
|
204
218
|
* @returns The greatest common divisor of its operands
|
|
205
219
|
*
|
|
220
|
+
* @example BigInt.gcd(36t, 24t) == 12t
|
|
221
|
+
*
|
|
206
222
|
* @since v0.5.0
|
|
207
223
|
*/
|
|
208
224
|
@unsafe
|
|
209
|
-
|
|
210
|
-
WasmI32.toGrain(
|
|
211
|
-
|
|
212
|
-
): BigInt
|
|
225
|
+
provide let gcd = (num1: BigInt, num2: BigInt) => {
|
|
226
|
+
WasmI32.toGrain(BI.gcd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))):
|
|
227
|
+
BigInt
|
|
213
228
|
}
|
|
214
229
|
|
|
215
|
-
/**
|
|
216
|
-
* @section Bitwise operations: Functions for operating on bits of BigInt values.
|
|
217
|
-
*/
|
|
218
|
-
|
|
219
230
|
/**
|
|
220
231
|
* Shifts the bits of the value left by the given number of bits.
|
|
221
232
|
*
|
|
@@ -223,12 +234,17 @@ export let gcd = (num1: BigInt, num2: BigInt) => {
|
|
|
223
234
|
* @param places: The number of bits to shift by
|
|
224
235
|
* @returns The shifted value
|
|
225
236
|
*
|
|
226
|
-
* @
|
|
237
|
+
* @example
|
|
238
|
+
* use BigInt.{ (<<) }
|
|
239
|
+
* assert (10t << 2l) == 40t
|
|
240
|
+
*
|
|
241
|
+
* @since v0.6.0
|
|
242
|
+
* @history v0.5.0: Originally named `shl`
|
|
227
243
|
*/
|
|
228
244
|
@unsafe
|
|
229
|
-
|
|
245
|
+
provide let (<<) = (num: BigInt, places: Int32) => {
|
|
230
246
|
let num = WasmI32.fromGrain(num)
|
|
231
|
-
let places = WasmI32.load(WasmI32.fromGrain(places),
|
|
247
|
+
let places = WasmI32.load(WasmI32.fromGrain(places), 4n)
|
|
232
248
|
WasmI32.toGrain(BI.shl(num, places)): BigInt
|
|
233
249
|
}
|
|
234
250
|
|
|
@@ -239,31 +255,36 @@ export let shl = (num: BigInt, places: Int32) => {
|
|
|
239
255
|
* @param places: The amount to shift by
|
|
240
256
|
* @returns The shifted value
|
|
241
257
|
*
|
|
242
|
-
* @
|
|
258
|
+
* @example
|
|
259
|
+
* use BigInt.{ (>>) }
|
|
260
|
+
* assert (9999t >> 2l) == 2499t
|
|
261
|
+
*
|
|
262
|
+
* @since v0.6.0
|
|
263
|
+
* @history v0.5.0: Originally named `shr`
|
|
243
264
|
*/
|
|
244
265
|
@unsafe
|
|
245
|
-
|
|
266
|
+
provide let (>>) = (num: BigInt, places: Int32) => {
|
|
246
267
|
let num = WasmI32.fromGrain(num)
|
|
247
|
-
let places = WasmI32.load(WasmI32.fromGrain(places),
|
|
268
|
+
let places = WasmI32.load(WasmI32.fromGrain(places), 4n)
|
|
248
269
|
WasmI32.toGrain(BI.shrS(num, places)): BigInt
|
|
249
270
|
}
|
|
250
271
|
|
|
251
|
-
/**
|
|
252
|
-
* @section Comparisons: Functions for comparing BigInt values.
|
|
253
|
-
*/
|
|
254
|
-
|
|
255
272
|
/**
|
|
256
273
|
* Checks if the given value is equal to zero.
|
|
257
274
|
*
|
|
258
275
|
* @param num: The value to inspect
|
|
259
276
|
* @returns `true` if the first value is equal to zero or `false` otherwise
|
|
260
277
|
*
|
|
278
|
+
* @example assert BigInt.eqz(0t) == true
|
|
279
|
+
* @example assert BigInt.eqz(1t) == false
|
|
280
|
+
*
|
|
261
281
|
* @since v0.5.0
|
|
262
282
|
*/
|
|
263
283
|
@unsafe
|
|
264
|
-
|
|
265
|
-
let
|
|
266
|
-
|
|
284
|
+
provide let eqz = (num: BigInt) => {
|
|
285
|
+
let result = BI.eqz(WasmI32.fromGrain(num))
|
|
286
|
+
ignore(num)
|
|
287
|
+
result
|
|
267
288
|
}
|
|
268
289
|
|
|
269
290
|
/**
|
|
@@ -273,13 +294,22 @@ export let eqz = (num: BigInt) => {
|
|
|
273
294
|
* @param num2: The second value
|
|
274
295
|
* @returns `true` if the first value is equal to the second value or `false` otherwise
|
|
275
296
|
*
|
|
276
|
-
* @
|
|
297
|
+
* @example
|
|
298
|
+
* use BigInt.{ (==) }
|
|
299
|
+
* assert 1t == 1t
|
|
300
|
+
* @example
|
|
301
|
+
* use BigInt.{ (==) }
|
|
302
|
+
* assert -10t == -10t
|
|
303
|
+
*
|
|
304
|
+
* @since v0.6.0
|
|
305
|
+
* @history v0.5.0: Originally named `eq`
|
|
277
306
|
*/
|
|
278
307
|
@unsafe
|
|
279
|
-
|
|
280
|
-
let
|
|
281
|
-
|
|
282
|
-
|
|
308
|
+
provide let (==) = (num1: BigInt, num2: BigInt) => {
|
|
309
|
+
let result = BI.eq(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
310
|
+
ignore(num1)
|
|
311
|
+
ignore(num2)
|
|
312
|
+
result
|
|
283
313
|
}
|
|
284
314
|
|
|
285
315
|
/**
|
|
@@ -289,13 +319,22 @@ export let eq = (num1: BigInt, num2: BigInt) => {
|
|
|
289
319
|
* @param num2: The second value
|
|
290
320
|
* @returns `true` if the first value is not equal to the second value or `false` otherwise
|
|
291
321
|
*
|
|
292
|
-
* @
|
|
322
|
+
* @example
|
|
323
|
+
* use BigInt.{ (!=) }
|
|
324
|
+
* assert 1t != 2t
|
|
325
|
+
* @example
|
|
326
|
+
* use BigInt.{ (!=) }
|
|
327
|
+
* assert -10t != -20t
|
|
328
|
+
*
|
|
329
|
+
* @since v0.6.0
|
|
330
|
+
* @history v0.5.0: Originally named `ne`
|
|
293
331
|
*/
|
|
294
332
|
@unsafe
|
|
295
|
-
|
|
296
|
-
let
|
|
297
|
-
|
|
298
|
-
|
|
333
|
+
provide let (!=) = (num1: BigInt, num2: BigInt) => {
|
|
334
|
+
let result = BI.ne(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
335
|
+
ignore(num1)
|
|
336
|
+
ignore(num2)
|
|
337
|
+
result
|
|
299
338
|
}
|
|
300
339
|
|
|
301
340
|
/**
|
|
@@ -305,13 +344,22 @@ export let ne = (num1: BigInt, num2: BigInt) => {
|
|
|
305
344
|
* @param num2: The second value
|
|
306
345
|
* @returns `true` if the first value is less than the second value or `false` otherwise
|
|
307
346
|
*
|
|
308
|
-
* @
|
|
347
|
+
* @example
|
|
348
|
+
* use BigInt.{ (<) }
|
|
349
|
+
* assert 1t < 2t
|
|
350
|
+
* @example
|
|
351
|
+
* use BigInt.{ (<) }
|
|
352
|
+
* assert -10t < 0t
|
|
353
|
+
*
|
|
354
|
+
* @since v0.6.0
|
|
355
|
+
* @history v0.5.0: Originally named `lt`
|
|
309
356
|
*/
|
|
310
357
|
@unsafe
|
|
311
|
-
|
|
312
|
-
let
|
|
313
|
-
|
|
314
|
-
|
|
358
|
+
provide let (<) = (num1: BigInt, num2: BigInt) => {
|
|
359
|
+
let result = BI.lt(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
360
|
+
ignore(num1)
|
|
361
|
+
ignore(num2)
|
|
362
|
+
result
|
|
315
363
|
}
|
|
316
364
|
|
|
317
365
|
/**
|
|
@@ -321,13 +369,25 @@ export let lt = (num1: BigInt, num2: BigInt) => {
|
|
|
321
369
|
* @param num2: The second value
|
|
322
370
|
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
|
|
323
371
|
*
|
|
324
|
-
* @
|
|
372
|
+
* @example
|
|
373
|
+
* use BigInt.{ (<=) }
|
|
374
|
+
* assert 1t <= 1t
|
|
375
|
+
* @example
|
|
376
|
+
* use BigInt.{ (<=) }
|
|
377
|
+
* assert -10t <= 0t
|
|
378
|
+
* @example
|
|
379
|
+
* use BigInt.{ (<=) }
|
|
380
|
+
* assert 2t <= 3t
|
|
381
|
+
*
|
|
382
|
+
* @since v0.6.0
|
|
383
|
+
* @history v0.5.0: Originally named `lte`
|
|
325
384
|
*/
|
|
326
385
|
@unsafe
|
|
327
|
-
|
|
328
|
-
let
|
|
329
|
-
|
|
330
|
-
|
|
386
|
+
provide let (<=) = (num1: BigInt, num2: BigInt) => {
|
|
387
|
+
let result = BI.lte(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
388
|
+
ignore(num1)
|
|
389
|
+
ignore(num2)
|
|
390
|
+
result
|
|
331
391
|
}
|
|
332
392
|
|
|
333
393
|
/**
|
|
@@ -337,13 +397,22 @@ export let lte = (num1: BigInt, num2: BigInt) => {
|
|
|
337
397
|
* @param num2: The second value
|
|
338
398
|
* @returns `true` if the first value is greater than the second value or `false` otherwise
|
|
339
399
|
*
|
|
340
|
-
* @
|
|
400
|
+
* @example
|
|
401
|
+
* use BigInt.{ (>) }
|
|
402
|
+
* assert 2t > 1t
|
|
403
|
+
* @example
|
|
404
|
+
* use BigInt.{ (>) }
|
|
405
|
+
* assert 0t > -10t
|
|
406
|
+
*
|
|
407
|
+
* @since v0.6.0
|
|
408
|
+
* @history v0.5.0: Originally named `gt`
|
|
341
409
|
*/
|
|
342
410
|
@unsafe
|
|
343
|
-
|
|
344
|
-
let
|
|
345
|
-
|
|
346
|
-
|
|
411
|
+
provide let (>) = (num1: BigInt, num2: BigInt) => {
|
|
412
|
+
let result = BI.gt(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
413
|
+
ignore(num1)
|
|
414
|
+
ignore(num2)
|
|
415
|
+
result
|
|
347
416
|
}
|
|
348
417
|
|
|
349
418
|
/**
|
|
@@ -353,29 +422,39 @@ export let gt = (num1: BigInt, num2: BigInt) => {
|
|
|
353
422
|
* @param num2: The second value
|
|
354
423
|
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
|
|
355
424
|
*
|
|
356
|
-
* @
|
|
425
|
+
* @example
|
|
426
|
+
* use BigInt.{ (>=) }
|
|
427
|
+
* assert 1t >= 1t
|
|
428
|
+
* @example
|
|
429
|
+
* use BigInt.{ (>=) }
|
|
430
|
+
* assert 0t >= -10t
|
|
431
|
+
* @example
|
|
432
|
+
* use BigInt.{ (>=) }
|
|
433
|
+
* assert 3t >= 2t
|
|
434
|
+
*
|
|
435
|
+
* @since v0.6.0
|
|
436
|
+
* @history v0.5.0: Originally named `gte`
|
|
357
437
|
*/
|
|
358
438
|
@unsafe
|
|
359
|
-
|
|
360
|
-
let
|
|
361
|
-
|
|
362
|
-
|
|
439
|
+
provide let (>=) = (num1: BigInt, num2: BigInt) => {
|
|
440
|
+
let result = BI.gte(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
441
|
+
ignore(num1)
|
|
442
|
+
ignore(num2)
|
|
443
|
+
result
|
|
363
444
|
}
|
|
364
445
|
|
|
365
|
-
/**
|
|
366
|
-
* @section Bitwise logic: Boolean operations on the bits of BigInt values.
|
|
367
|
-
*/
|
|
368
|
-
|
|
369
446
|
/**
|
|
370
447
|
* Computes the bitwise NOT of the given value.
|
|
371
448
|
*
|
|
372
449
|
* @param num: The given value
|
|
373
450
|
* @returns Containing the inverted bits of the given value
|
|
374
451
|
*
|
|
452
|
+
* @example BigInt.lnot(91234t) == -91235t
|
|
453
|
+
*
|
|
375
454
|
* @since v0.5.0
|
|
376
455
|
*/
|
|
377
456
|
@unsafe
|
|
378
|
-
|
|
457
|
+
provide let lnot = (num: BigInt) => {
|
|
379
458
|
WasmI32.toGrain(BI.bitwiseNot(WasmI32.fromGrain(num))): BigInt
|
|
380
459
|
}
|
|
381
460
|
|
|
@@ -386,10 +465,15 @@ export let lnot = (num: BigInt) => {
|
|
|
386
465
|
* @param num2: The second operand
|
|
387
466
|
* @returns Containing a `1` in each bit position for which the corresponding bits of both operands are `1`
|
|
388
467
|
*
|
|
389
|
-
* @
|
|
468
|
+
* @example
|
|
469
|
+
* use BigInt.{ (&) }
|
|
470
|
+
* assert (4t & 3t) == 0t
|
|
471
|
+
*
|
|
472
|
+
* @since v0.6.0
|
|
473
|
+
* @history v0.5.0: Originally named `land`
|
|
390
474
|
*/
|
|
391
475
|
@unsafe
|
|
392
|
-
|
|
476
|
+
provide let (&) = (num1: BigInt, num2: BigInt) => {
|
|
393
477
|
WasmI32.toGrain(
|
|
394
478
|
BI.bitwiseAnd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
395
479
|
): BigInt
|
|
@@ -402,10 +486,15 @@ export let land = (num1: BigInt, num2: BigInt) => {
|
|
|
402
486
|
* @param num2: The second operand
|
|
403
487
|
* @returns Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`
|
|
404
488
|
*
|
|
405
|
-
* @
|
|
489
|
+
* @example
|
|
490
|
+
* use BigInt.{ (|) }
|
|
491
|
+
* assert (5t | 3t) == 7t
|
|
492
|
+
*
|
|
493
|
+
* @since v0.6.0
|
|
494
|
+
* @history v0.5.0: Originally named `lor`
|
|
406
495
|
*/
|
|
407
496
|
@unsafe
|
|
408
|
-
|
|
497
|
+
provide let (|) = (num1: BigInt, num2: BigInt) => {
|
|
409
498
|
WasmI32.toGrain(
|
|
410
499
|
BI.bitwiseOr(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
411
500
|
): BigInt
|
|
@@ -418,10 +507,15 @@ export let lor = (num1: BigInt, num2: BigInt) => {
|
|
|
418
507
|
* @param num2: The second operand
|
|
419
508
|
* @returns Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`
|
|
420
509
|
*
|
|
421
|
-
* @
|
|
510
|
+
* @example
|
|
511
|
+
* use BigInt.{ (^) }
|
|
512
|
+
* assert (5t ^ 3t) == 6t
|
|
513
|
+
*
|
|
514
|
+
* @since v0.6.0
|
|
515
|
+
* @history v0.5.0: Originally named `lxor`
|
|
422
516
|
*/
|
|
423
517
|
@unsafe
|
|
424
|
-
|
|
518
|
+
provide let (^) = (num1: BigInt, num2: BigInt) => {
|
|
425
519
|
WasmI32.toGrain(
|
|
426
520
|
BI.bitwiseXor(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
427
521
|
): BigInt
|
|
@@ -434,13 +528,14 @@ export let lxor = (num1: BigInt, num2: BigInt) => {
|
|
|
434
528
|
* @param num: The value to inspect
|
|
435
529
|
* @returns The amount of leading zeros
|
|
436
530
|
*
|
|
531
|
+
* @example BigInt.clz(5t) == 2147483647t
|
|
532
|
+
*
|
|
437
533
|
* @since v0.5.0
|
|
438
534
|
*/
|
|
439
535
|
@unsafe
|
|
440
|
-
|
|
441
|
-
WasmI32.toGrain(
|
|
442
|
-
|
|
443
|
-
): Int32
|
|
536
|
+
provide let clz = (num: BigInt) => {
|
|
537
|
+
WasmI32.toGrain(DS.newInt32(BI.countLeadingZeros(WasmI32.fromGrain(num)))):
|
|
538
|
+
Int32
|
|
444
539
|
}
|
|
445
540
|
|
|
446
541
|
/**
|
|
@@ -449,13 +544,14 @@ export let clz = (num: BigInt) => {
|
|
|
449
544
|
* @param num: The value to inspect
|
|
450
545
|
* @returns The amount of trailing zeros
|
|
451
546
|
*
|
|
547
|
+
* @example BigInt.ctz(14t) == 1t
|
|
548
|
+
*
|
|
452
549
|
* @since v0.5.0
|
|
453
550
|
*/
|
|
454
551
|
@unsafe
|
|
455
|
-
|
|
456
|
-
WasmI32.toGrain(
|
|
457
|
-
|
|
458
|
-
): Int64
|
|
552
|
+
provide let ctz = (num: BigInt) => {
|
|
553
|
+
WasmI32.toGrain(DS.newInt64(BI.countTrailingZeros(WasmI32.fromGrain(num)))):
|
|
554
|
+
Int64
|
|
459
555
|
}
|
|
460
556
|
|
|
461
557
|
/**
|
|
@@ -465,10 +561,12 @@ export let ctz = (num: BigInt) => {
|
|
|
465
561
|
* @param num: The value to inspect
|
|
466
562
|
* @returns The amount of 1-bits in its operand
|
|
467
563
|
*
|
|
564
|
+
* @example BigInt.popcnt(14t) == 1t
|
|
565
|
+
*
|
|
468
566
|
* @since v0.5.0
|
|
469
567
|
*/
|
|
470
568
|
@unsafe
|
|
471
|
-
|
|
569
|
+
provide let popcnt = (num: BigInt) => {
|
|
472
570
|
let flagDest = Memory.malloc(4n)
|
|
473
571
|
WasmI32.store(flagDest, 0n, 0n)
|
|
474
572
|
let res = BI.popcnt(WasmI32.fromGrain(num), flagDest)
|
|
@@ -479,19 +577,20 @@ export let popcnt = (num: BigInt) => {
|
|
|
479
577
|
}
|
|
480
578
|
}
|
|
481
579
|
|
|
482
|
-
/**
|
|
483
|
-
* @section Other: Other functions on BigInts.
|
|
484
|
-
*/
|
|
485
|
-
|
|
486
580
|
/**
|
|
487
581
|
* Converts the given operand to a string.
|
|
488
582
|
*
|
|
489
583
|
* @param num: The operand
|
|
490
584
|
* @returns The operand, as a string
|
|
491
585
|
*
|
|
586
|
+
* @example BigInt.toString(1t) == "1"
|
|
587
|
+
* @example BigInt.toString(-1t) == "-1"
|
|
588
|
+
*
|
|
492
589
|
* @since v0.5.0
|
|
493
590
|
*/
|
|
494
591
|
@unsafe
|
|
495
|
-
|
|
496
|
-
BI.bigIntToString10(WasmI32.fromGrain(num))
|
|
592
|
+
provide let toString = (num: BigInt) => {
|
|
593
|
+
let string = BI.bigIntToString10(WasmI32.fromGrain(num))
|
|
594
|
+
ignore(num)
|
|
595
|
+
string
|
|
497
596
|
}
|