@grain/stdlib 0.4.6 → 0.5.2
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 +93 -0
- package/array.gr +18 -18
- package/array.md +18 -18
- package/bigint.gr +497 -0
- package/bigint.md +811 -0
- package/buffer.gr +59 -223
- package/buffer.md +24 -17
- package/bytes.gr +100 -202
- package/bytes.md +19 -0
- package/char.gr +63 -133
- package/exception.gr +28 -2
- package/exception.md +43 -0
- package/float32.gr +76 -95
- package/float32.md +69 -30
- package/float64.gr +81 -95
- package/float64.md +69 -30
- 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 +111 -54
- package/number.md +100 -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 +1049 -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/bigint.gr
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module BigInt: Utilities for working with the BigInt type.
|
|
3
|
+
* @example import BigInt from "bigint"
|
|
4
|
+
*
|
|
5
|
+
* @since v0.5.0
|
|
6
|
+
*/
|
|
7
|
+
import WasmI32 from "runtime/unsafe/wasmi32"
|
|
8
|
+
import Memory from "runtime/unsafe/memory"
|
|
9
|
+
import BI from "runtime/bigint"
|
|
10
|
+
import DS from "runtime/dataStructures"
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
coerceNumberToBigInt as fromNumber,
|
|
14
|
+
coerceBigIntToNumber as toNumber,
|
|
15
|
+
} from "runtime/numbers"
|
|
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
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @section Operations: Mathematical operations for BigInt values.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Increments the value by one.
|
|
46
|
+
*
|
|
47
|
+
* @param num: The value to increment
|
|
48
|
+
* @returns The incremented value
|
|
49
|
+
*
|
|
50
|
+
* @since v0.5.0
|
|
51
|
+
*/
|
|
52
|
+
@unsafe
|
|
53
|
+
export let incr = (num: BigInt) => {
|
|
54
|
+
WasmI32.toGrain(BI.incr(WasmI32.fromGrain(num))): BigInt
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Decrements the value by one.
|
|
59
|
+
*
|
|
60
|
+
* @param num: The value to decrement
|
|
61
|
+
* @returns The decremented value
|
|
62
|
+
*
|
|
63
|
+
* @since v0.5.0
|
|
64
|
+
*/
|
|
65
|
+
@unsafe
|
|
66
|
+
export let decr = (num: BigInt) => {
|
|
67
|
+
WasmI32.toGrain(BI.decr(WasmI32.fromGrain(num))): BigInt
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Negates the given operand.
|
|
72
|
+
*
|
|
73
|
+
* @param num: The operand
|
|
74
|
+
* @returns The negated operand
|
|
75
|
+
*
|
|
76
|
+
* @since v0.5.0
|
|
77
|
+
*/
|
|
78
|
+
@unsafe
|
|
79
|
+
export let neg = (num: BigInt) => {
|
|
80
|
+
WasmI32.toGrain(BI.negate(WasmI32.fromGrain(num))): BigInt
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Returns the absolute value of the given operand.
|
|
85
|
+
*
|
|
86
|
+
* @param num: The operand
|
|
87
|
+
* @returns The operand's absolute value
|
|
88
|
+
*
|
|
89
|
+
* @since v0.5.0
|
|
90
|
+
*/
|
|
91
|
+
@unsafe
|
|
92
|
+
export let abs = (num: BigInt) => {
|
|
93
|
+
WasmI32.toGrain(BI.abs(WasmI32.fromGrain(num))): BigInt
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Computes the sum of its operands.
|
|
98
|
+
*
|
|
99
|
+
* @param num1: The first operand
|
|
100
|
+
* @param num2: The second operand
|
|
101
|
+
* @returns The sum of the two operands
|
|
102
|
+
*
|
|
103
|
+
* @since v0.5.0
|
|
104
|
+
*/
|
|
105
|
+
@unsafe
|
|
106
|
+
export let add = (num1: BigInt, num2: BigInt) => {
|
|
107
|
+
WasmI32.toGrain(
|
|
108
|
+
BI.add(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
109
|
+
): BigInt
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Computes the difference of its operands.
|
|
114
|
+
*
|
|
115
|
+
* @param num1: The first operand
|
|
116
|
+
* @param num2: The second operand
|
|
117
|
+
* @returns The difference of the two operands
|
|
118
|
+
*
|
|
119
|
+
* @since v0.5.0
|
|
120
|
+
*/
|
|
121
|
+
@unsafe
|
|
122
|
+
export let sub = (num1: BigInt, num2: BigInt) => {
|
|
123
|
+
WasmI32.toGrain(
|
|
124
|
+
BI.sub(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
125
|
+
): BigInt
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Computes the product of its operands.
|
|
130
|
+
*
|
|
131
|
+
* @param num1: The first operand
|
|
132
|
+
* @param num2: The second operand
|
|
133
|
+
* @returns The product of the two operands
|
|
134
|
+
*
|
|
135
|
+
* @since v0.5.0
|
|
136
|
+
*/
|
|
137
|
+
@unsafe
|
|
138
|
+
export let mul = (num1: BigInt, num2: BigInt) => {
|
|
139
|
+
WasmI32.toGrain(
|
|
140
|
+
BI.mul(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
141
|
+
): BigInt
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// For further reading on Truncated vs. Floored division: https://en.wikipedia.org/wiki/Modulo_operation
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Computes the quotient of its operands using signed (truncated) division
|
|
148
|
+
* (in which the quotient is always rounded towards zero).
|
|
149
|
+
*
|
|
150
|
+
* @param num1: The first operand
|
|
151
|
+
* @param num2: The second operand
|
|
152
|
+
* @returns The quotient of its operands
|
|
153
|
+
*
|
|
154
|
+
* @since v0.5.0
|
|
155
|
+
*/
|
|
156
|
+
@unsafe
|
|
157
|
+
export let div = (num1: BigInt, num2: BigInt) => {
|
|
158
|
+
WasmI32.toGrain(
|
|
159
|
+
BI.quot(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
160
|
+
): BigInt
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Computes the remainder of the division of its operands using signed (truncated) division
|
|
165
|
+
* (in which the quotient is always rounded towards zero).
|
|
166
|
+
*
|
|
167
|
+
* @param num1: The first operand
|
|
168
|
+
* @param num2: The second operand
|
|
169
|
+
* @returns The remainder of its operands
|
|
170
|
+
*
|
|
171
|
+
* @since v0.5.0
|
|
172
|
+
*/
|
|
173
|
+
@unsafe
|
|
174
|
+
export let rem = (num1: BigInt, num2: BigInt) => {
|
|
175
|
+
WasmI32.toGrain(
|
|
176
|
+
BI.rem(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
177
|
+
): BigInt
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Computes the quotient and remainder of its operands using signed (truncated) division.
|
|
182
|
+
*
|
|
183
|
+
* @param num1: The first operand
|
|
184
|
+
* @param num2: The second operand
|
|
185
|
+
* @returns The quotient and remainder of its operands
|
|
186
|
+
*
|
|
187
|
+
* @since v0.5.0
|
|
188
|
+
*/
|
|
189
|
+
@unsafe
|
|
190
|
+
export let quotRem = (num1: BigInt, num2: BigInt) => {
|
|
191
|
+
let dest = Memory.malloc(8n)
|
|
192
|
+
BI.quotRem(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2), dest)
|
|
193
|
+
let q = WasmI32.toGrain(WasmI32.load(dest, 0n)): BigInt
|
|
194
|
+
let r = WasmI32.toGrain(WasmI32.load(dest, 4n)): BigInt
|
|
195
|
+
Memory.free(dest)
|
|
196
|
+
(q, r)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Computes the greatest common divisior of the two operands.
|
|
201
|
+
*
|
|
202
|
+
* @param num1: The first operand
|
|
203
|
+
* @param num2: The second operand
|
|
204
|
+
* @returns The greatest common divisor of its operands
|
|
205
|
+
*
|
|
206
|
+
* @since v0.5.0
|
|
207
|
+
*/
|
|
208
|
+
@unsafe
|
|
209
|
+
export let gcd = (num1: BigInt, num2: BigInt) => {
|
|
210
|
+
WasmI32.toGrain(
|
|
211
|
+
BI.gcd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
212
|
+
): BigInt
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @section Bitwise operations: Functions for operating on bits of BigInt values.
|
|
217
|
+
*/
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Shifts the bits of the value left by the given number of bits.
|
|
221
|
+
*
|
|
222
|
+
* @param num: The value to shift
|
|
223
|
+
* @param places: The number of bits to shift by
|
|
224
|
+
* @returns The shifted value
|
|
225
|
+
*
|
|
226
|
+
* @since v0.5.0
|
|
227
|
+
*/
|
|
228
|
+
@unsafe
|
|
229
|
+
export let shl = (num: BigInt, places: Int32) => {
|
|
230
|
+
let num = WasmI32.fromGrain(num)
|
|
231
|
+
let places = WasmI32.load(WasmI32.fromGrain(places), 8n)
|
|
232
|
+
WasmI32.toGrain(BI.shl(num, places)): BigInt
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Shifts the bits of the value right by the given number of bits, preserving the sign bit.
|
|
237
|
+
*
|
|
238
|
+
* @param num: The value to shift
|
|
239
|
+
* @param places: The amount to shift by
|
|
240
|
+
* @returns The shifted value
|
|
241
|
+
*
|
|
242
|
+
* @since v0.5.0
|
|
243
|
+
*/
|
|
244
|
+
@unsafe
|
|
245
|
+
export let shr = (num: BigInt, places: Int32) => {
|
|
246
|
+
let num = WasmI32.fromGrain(num)
|
|
247
|
+
let places = WasmI32.load(WasmI32.fromGrain(places), 8n)
|
|
248
|
+
WasmI32.toGrain(BI.shrS(num, places)): BigInt
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* @section Comparisons: Functions for comparing BigInt values.
|
|
253
|
+
*/
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Checks if the given value is equal to zero.
|
|
257
|
+
*
|
|
258
|
+
* @param num: The value to inspect
|
|
259
|
+
* @returns `true` if the first value is equal to zero or `false` otherwise
|
|
260
|
+
*
|
|
261
|
+
* @since v0.5.0
|
|
262
|
+
*/
|
|
263
|
+
@unsafe
|
|
264
|
+
export let eqz = (num: BigInt) => {
|
|
265
|
+
let num = WasmI32.fromGrain(num)
|
|
266
|
+
BI.eqz(num)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Checks if the first value is equal to the second value.
|
|
271
|
+
*
|
|
272
|
+
* @param num1: The first value
|
|
273
|
+
* @param num2: The second value
|
|
274
|
+
* @returns `true` if the first value is equal to the second value or `false` otherwise
|
|
275
|
+
*
|
|
276
|
+
* @since v0.5.0
|
|
277
|
+
*/
|
|
278
|
+
@unsafe
|
|
279
|
+
export let eq = (num1: BigInt, num2: BigInt) => {
|
|
280
|
+
let num1 = WasmI32.fromGrain(num1)
|
|
281
|
+
let num2 = WasmI32.fromGrain(num2)
|
|
282
|
+
BI.eq(num1, num2)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Checks if the first value is not equal to the second value.
|
|
287
|
+
*
|
|
288
|
+
* @param num1: The first value
|
|
289
|
+
* @param num2: The second value
|
|
290
|
+
* @returns `true` if the first value is not equal to the second value or `false` otherwise
|
|
291
|
+
*
|
|
292
|
+
* @since v0.5.0
|
|
293
|
+
*/
|
|
294
|
+
@unsafe
|
|
295
|
+
export let ne = (num1: BigInt, num2: BigInt) => {
|
|
296
|
+
let num1 = WasmI32.fromGrain(num1)
|
|
297
|
+
let num2 = WasmI32.fromGrain(num2)
|
|
298
|
+
BI.ne(num1, num2)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Checks if the first value is less than the second value.
|
|
303
|
+
*
|
|
304
|
+
* @param num1: The first value
|
|
305
|
+
* @param num2: The second value
|
|
306
|
+
* @returns `true` if the first value is less than the second value or `false` otherwise
|
|
307
|
+
*
|
|
308
|
+
* @since v0.5.0
|
|
309
|
+
*/
|
|
310
|
+
@unsafe
|
|
311
|
+
export let lt = (num1: BigInt, num2: BigInt) => {
|
|
312
|
+
let num1 = WasmI32.fromGrain(num1)
|
|
313
|
+
let num2 = WasmI32.fromGrain(num2)
|
|
314
|
+
BI.lt(num1, num2)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Checks if the first value is less than or equal to the second value.
|
|
319
|
+
*
|
|
320
|
+
* @param num1: The first value
|
|
321
|
+
* @param num2: The second value
|
|
322
|
+
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
|
|
323
|
+
*
|
|
324
|
+
* @since v0.5.0
|
|
325
|
+
*/
|
|
326
|
+
@unsafe
|
|
327
|
+
export let lte = (num1: BigInt, num2: BigInt) => {
|
|
328
|
+
let num1 = WasmI32.fromGrain(num1)
|
|
329
|
+
let num2 = WasmI32.fromGrain(num2)
|
|
330
|
+
BI.lte(num1, num2)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Checks if the first value is greater than the second value.
|
|
335
|
+
*
|
|
336
|
+
* @param num1: The first value
|
|
337
|
+
* @param num2: The second value
|
|
338
|
+
* @returns `true` if the first value is greater than the second value or `false` otherwise
|
|
339
|
+
*
|
|
340
|
+
* @since v0.5.0
|
|
341
|
+
*/
|
|
342
|
+
@unsafe
|
|
343
|
+
export let gt = (num1: BigInt, num2: BigInt) => {
|
|
344
|
+
let num1 = WasmI32.fromGrain(num1)
|
|
345
|
+
let num2 = WasmI32.fromGrain(num2)
|
|
346
|
+
BI.gt(num1, num2)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Checks if the first value is greater than or equal to the second value.
|
|
351
|
+
*
|
|
352
|
+
* @param num1: The first value
|
|
353
|
+
* @param num2: The second value
|
|
354
|
+
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
|
|
355
|
+
*
|
|
356
|
+
* @since v0.5.0
|
|
357
|
+
*/
|
|
358
|
+
@unsafe
|
|
359
|
+
export let gte = (num1: BigInt, num2: BigInt) => {
|
|
360
|
+
let num1 = WasmI32.fromGrain(num1)
|
|
361
|
+
let num2 = WasmI32.fromGrain(num2)
|
|
362
|
+
BI.gte(num1, num2)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* @section Bitwise logic: Boolean operations on the bits of BigInt values.
|
|
367
|
+
*/
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Computes the bitwise NOT of the given value.
|
|
371
|
+
*
|
|
372
|
+
* @param num: The given value
|
|
373
|
+
* @returns Containing the inverted bits of the given value
|
|
374
|
+
*
|
|
375
|
+
* @since v0.5.0
|
|
376
|
+
*/
|
|
377
|
+
@unsafe
|
|
378
|
+
export let lnot = (num: BigInt) => {
|
|
379
|
+
WasmI32.toGrain(BI.bitwiseNot(WasmI32.fromGrain(num))): BigInt
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Computes the bitwise AND (`&`) on the given operands.
|
|
384
|
+
*
|
|
385
|
+
* @param num1: The first operand
|
|
386
|
+
* @param num2: The second operand
|
|
387
|
+
* @returns Containing a `1` in each bit position for which the corresponding bits of both operands are `1`
|
|
388
|
+
*
|
|
389
|
+
* @since v0.5.0
|
|
390
|
+
*/
|
|
391
|
+
@unsafe
|
|
392
|
+
export let land = (num1: BigInt, num2: BigInt) => {
|
|
393
|
+
WasmI32.toGrain(
|
|
394
|
+
BI.bitwiseAnd(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
395
|
+
): BigInt
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Computes the bitwise OR (`|`) on the given operands.
|
|
400
|
+
*
|
|
401
|
+
* @param num1: The first operand
|
|
402
|
+
* @param num2: The second operand
|
|
403
|
+
* @returns Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`
|
|
404
|
+
*
|
|
405
|
+
* @since v0.5.0
|
|
406
|
+
*/
|
|
407
|
+
@unsafe
|
|
408
|
+
export let lor = (num1: BigInt, num2: BigInt) => {
|
|
409
|
+
WasmI32.toGrain(
|
|
410
|
+
BI.bitwiseOr(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
411
|
+
): BigInt
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Computes the bitwise XOR (`^`) on the given operands.
|
|
416
|
+
*
|
|
417
|
+
* @param num1: The first operand
|
|
418
|
+
* @param num2: The second operand
|
|
419
|
+
* @returns Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`
|
|
420
|
+
*
|
|
421
|
+
* @since v0.5.0
|
|
422
|
+
*/
|
|
423
|
+
@unsafe
|
|
424
|
+
export let lxor = (num1: BigInt, num2: BigInt) => {
|
|
425
|
+
WasmI32.toGrain(
|
|
426
|
+
BI.bitwiseXor(WasmI32.fromGrain(num1), WasmI32.fromGrain(num2))
|
|
427
|
+
): BigInt
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Counts the number of leading zero bits in the value.
|
|
432
|
+
* Will return the maximum integer for negative numbers.
|
|
433
|
+
*
|
|
434
|
+
* @param num: The value to inspect
|
|
435
|
+
* @returns The amount of leading zeros
|
|
436
|
+
*
|
|
437
|
+
* @since v0.5.0
|
|
438
|
+
*/
|
|
439
|
+
@unsafe
|
|
440
|
+
export let clz = (num: BigInt) => {
|
|
441
|
+
WasmI32.toGrain(
|
|
442
|
+
DS.newInt32(BI.countLeadingZeros(WasmI32.fromGrain(num)))
|
|
443
|
+
): Int32
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Counts the number of trailing zero bits in the value.
|
|
448
|
+
*
|
|
449
|
+
* @param num: The value to inspect
|
|
450
|
+
* @returns The amount of trailing zeros
|
|
451
|
+
*
|
|
452
|
+
* @since v0.5.0
|
|
453
|
+
*/
|
|
454
|
+
@unsafe
|
|
455
|
+
export let ctz = (num: BigInt) => {
|
|
456
|
+
WasmI32.toGrain(
|
|
457
|
+
DS.newInt64(BI.countTrailingZeros(WasmI32.fromGrain(num)))
|
|
458
|
+
): Int64
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Counts the number of bits set to `1` in the value, also known as a population count.
|
|
463
|
+
* Will return the `None` if given a negative integer
|
|
464
|
+
*
|
|
465
|
+
* @param num: The value to inspect
|
|
466
|
+
* @returns The amount of 1-bits in its operand
|
|
467
|
+
*
|
|
468
|
+
* @since v0.5.0
|
|
469
|
+
*/
|
|
470
|
+
@unsafe
|
|
471
|
+
export let popcnt = (num: BigInt) => {
|
|
472
|
+
let flagDest = Memory.malloc(4n)
|
|
473
|
+
WasmI32.store(flagDest, 0n, 0n)
|
|
474
|
+
let res = BI.popcnt(WasmI32.fromGrain(num), flagDest)
|
|
475
|
+
if (WasmI32.eqz(WasmI32.load(flagDest, 0n))) {
|
|
476
|
+
Some(WasmI32.toGrain(DS.newInt64(res)): Int64)
|
|
477
|
+
} else {
|
|
478
|
+
None
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* @section Other: Other functions on BigInts.
|
|
484
|
+
*/
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Converts the given operand to a string.
|
|
488
|
+
*
|
|
489
|
+
* @param num: The operand
|
|
490
|
+
* @returns The operand, as a string
|
|
491
|
+
*
|
|
492
|
+
* @since v0.5.0
|
|
493
|
+
*/
|
|
494
|
+
@unsafe
|
|
495
|
+
export let toString = (num: BigInt) => {
|
|
496
|
+
BI.bigIntToString10(WasmI32.fromGrain(num))
|
|
497
|
+
}
|