@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.
Files changed (97) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +1 -1
  3. package/array.gr +92 -73
  4. package/array.md +18 -18
  5. package/bigint.gr +497 -0
  6. package/bigint.md +811 -0
  7. package/buffer.gr +56 -217
  8. package/buffer.md +24 -17
  9. package/bytes.gr +103 -205
  10. package/bytes.md +19 -0
  11. package/char.gr +152 -166
  12. package/char.md +200 -0
  13. package/exception.md +6 -0
  14. package/float32.gr +159 -82
  15. package/float32.md +315 -0
  16. package/float64.gr +163 -82
  17. package/float64.md +315 -0
  18. package/hash.gr +53 -49
  19. package/int32.gr +479 -230
  20. package/int32.md +937 -0
  21. package/int64.gr +479 -230
  22. package/int64.md +937 -0
  23. package/list.gr +530 -116
  24. package/list.md +1141 -0
  25. package/map.gr +302 -121
  26. package/map.md +525 -0
  27. package/number.gr +51 -57
  28. package/number.md +37 -3
  29. package/option.gr +25 -25
  30. package/option.md +1 -1
  31. package/package.json +3 -3
  32. package/pervasives.gr +504 -52
  33. package/pervasives.md +1116 -0
  34. package/queue.gr +8 -1
  35. package/queue.md +10 -0
  36. package/random.gr +196 -0
  37. package/random.md +179 -0
  38. package/range.gr +26 -26
  39. package/regex.gr +1833 -842
  40. package/regex.md +11 -11
  41. package/result.md +1 -1
  42. package/runtime/bigint.gr +2045 -0
  43. package/runtime/bigint.md +326 -0
  44. package/runtime/dataStructures.gr +99 -279
  45. package/runtime/dataStructures.md +391 -0
  46. package/runtime/debug.gr +0 -1
  47. package/runtime/debug.md +6 -0
  48. package/runtime/equal.gr +40 -37
  49. package/runtime/equal.md +6 -0
  50. package/runtime/exception.gr +28 -15
  51. package/runtime/exception.md +30 -0
  52. package/runtime/gc.gr +50 -20
  53. package/runtime/gc.md +36 -0
  54. package/runtime/malloc.gr +32 -22
  55. package/runtime/malloc.md +55 -0
  56. package/runtime/numberUtils.gr +297 -142
  57. package/runtime/numberUtils.md +54 -0
  58. package/runtime/numbers.gr +1204 -453
  59. package/runtime/numbers.md +300 -0
  60. package/runtime/string.gr +193 -228
  61. package/runtime/string.md +24 -0
  62. package/runtime/stringUtils.gr +62 -38
  63. package/runtime/stringUtils.md +6 -0
  64. package/runtime/unsafe/constants.gr +17 -0
  65. package/runtime/unsafe/constants.md +72 -0
  66. package/runtime/unsafe/conv.gr +10 -10
  67. package/runtime/unsafe/conv.md +71 -0
  68. package/runtime/unsafe/errors.md +204 -0
  69. package/runtime/unsafe/memory.gr +14 -3
  70. package/runtime/unsafe/memory.md +54 -0
  71. package/runtime/unsafe/printWasm.gr +4 -4
  72. package/runtime/unsafe/printWasm.md +24 -0
  73. package/runtime/unsafe/tags.gr +11 -10
  74. package/runtime/unsafe/tags.md +120 -0
  75. package/runtime/unsafe/wasmf32.gr +9 -2
  76. package/runtime/unsafe/wasmf32.md +168 -0
  77. package/runtime/unsafe/wasmf64.gr +9 -2
  78. package/runtime/unsafe/wasmf64.md +168 -0
  79. package/runtime/unsafe/wasmi32.gr +65 -47
  80. package/runtime/unsafe/wasmi32.md +282 -0
  81. package/runtime/unsafe/wasmi64.gr +78 -50
  82. package/runtime/unsafe/wasmi64.md +300 -0
  83. package/runtime/utils/printing.gr +62 -0
  84. package/runtime/utils/printing.md +18 -0
  85. package/runtime/wasi.gr +200 -46
  86. package/runtime/wasi.md +839 -0
  87. package/set.gr +125 -121
  88. package/set.md +24 -21
  89. package/stack.gr +29 -29
  90. package/stack.md +4 -6
  91. package/string.gr +434 -415
  92. package/string.md +3 -3
  93. package/sys/file.gr +477 -482
  94. package/sys/process.gr +33 -47
  95. package/sys/random.gr +48 -20
  96. package/sys/random.md +38 -0
  97. package/sys/time.gr +12 -28
package/array.md CHANGED
@@ -36,7 +36,7 @@ No other changes yet.
36
36
  length : Array<a> -> Number
37
37
  ```
38
38
 
39
- Returns the length of the input array.
39
+ Provides the length of the input array.
40
40
 
41
41
  Parameters:
42
42
 
@@ -114,7 +114,7 @@ Returns:
114
114
  Examples:
115
115
 
116
116
  ```grain
117
- Array.init(5, n => n + 3) // [> 8, 9, 10, 11, 12]
117
+ Array.init(5, n => n + 3) // [> 3, 4, 5, 6, 7]
118
118
  ```
119
119
 
120
120
  ### Array.**get**
@@ -195,8 +195,8 @@ No other changes yet.
195
195
  append : (Array<a>, Array<a>) -> Array<a>
196
196
  ```
197
197
 
198
- Creates a new array with the items the first array, followed by
199
- the items of the second array. This does not modify the arguments.
198
+ Creates a new array with the elements of the first array followed by
199
+ the elements of the second array. This does not modify the arguments.
200
200
 
201
201
  Parameters:
202
202
 
@@ -266,7 +266,7 @@ Returns:
266
266
  ### Array.**cycle**
267
267
 
268
268
  <details disabled>
269
- <summary tabindex="-1">Added in <code>next</code></summary>
269
+ <summary tabindex="-1">Added in <code>0.4.4</code></summary>
270
270
  No other changes yet.
271
271
  </details>
272
272
 
@@ -329,7 +329,7 @@ Parameters:
329
329
  forEachi : (((a, Number) -> Void), Array<a>) -> Void
330
330
  ```
331
331
 
332
- Iterates an array, calling an iterator function with each element.
332
+ Iterates an array, calling an iterator function on each element.
333
333
  Also passes the index as the second argument to the function.
334
334
 
335
335
  Parameters:
@@ -527,7 +527,7 @@ Returns:
527
527
 
528
528
  |type|description|
529
529
  |----|-----------|
530
- |`Bool`|`true` if all elements satify the condition, otherwise `false`|
530
+ |`Bool`|`true` if all elements satify the condition or `false` otherwise|
531
531
 
532
532
  ### Array.**some**
533
533
 
@@ -541,7 +541,7 @@ some : ((a -> Bool), Array<a>) -> Bool
541
541
  ```
542
542
 
543
543
  Checks that the given condition is satisfied **at least
544
- once** by an item in the input array.
544
+ once** by an element in the input array.
545
545
 
546
546
  Parameters:
547
547
 
@@ -554,7 +554,7 @@ Returns:
554
554
 
555
555
  |type|description|
556
556
  |----|-----------|
557
- |`Bool`|`true` if one or more elements satify the condition, otherwise `false`|
557
+ |`Bool`|`true` if one or more elements satify the condition or `false` otherwise|
558
558
 
559
559
  ### Array.**fill**
560
560
 
@@ -693,13 +693,13 @@ Parameters:
693
693
  |param|type|description|
694
694
  |-----|----|-----------|
695
695
  |`search`|`a`|The value to compare|
696
- |`array`|`Array<a>`|The array to iterate|
696
+ |`array`|`Array<a>`|The array to inspect|
697
697
 
698
698
  Returns:
699
699
 
700
700
  |type|description|
701
701
  |----|-----------|
702
- |`Bool`|`true` if the value exists in the array, otherwise `false`|
702
+ |`Bool`|`true` if the value exists in the array or `false` otherwise|
703
703
 
704
704
  ### Array.**find**
705
705
 
@@ -725,7 +725,7 @@ Returns:
725
725
 
726
726
  |type|description|
727
727
  |----|-----------|
728
- |`Option<a>`|`Some(element)` containing the first value found and `None` otherwise|
728
+ |`Option<a>`|`Some(element)` containing the first value found or `None` otherwise|
729
729
 
730
730
  ### Array.**findIndex**
731
731
 
@@ -751,7 +751,7 @@ Returns:
751
751
 
752
752
  |type|description|
753
753
  |----|-----------|
754
- |`Option<Number>`|`Some(index)` containing the index of the first element found and `None` otherwise|
754
+ |`Option<Number>`|`Some(index)` containing the index of the first element found or `None` otherwise|
755
755
 
756
756
  ### Array.**product**
757
757
 
@@ -1030,7 +1030,7 @@ Returns:
1030
1030
  ### Array.**sort**
1031
1031
 
1032
1032
  <details disabled>
1033
- <summary tabindex="-1">Added in <code>next</code></summary>
1033
+ <summary tabindex="-1">Added in <code>0.4.5</code></summary>
1034
1034
  No other changes yet.
1035
1035
  </details>
1036
1036
 
@@ -1052,7 +1052,7 @@ Parameters:
1052
1052
  ### Array.**rotate**
1053
1053
 
1054
1054
  <details disabled>
1055
- <summary tabindex="-1">Added in <code>next</code></summary>
1055
+ <summary tabindex="-1">Added in <code>0.4.5</code></summary>
1056
1056
  No other changes yet.
1057
1057
  </details>
1058
1058
 
@@ -1060,10 +1060,10 @@ No other changes yet.
1060
1060
  rotate : (Number, Array<a>) -> Void
1061
1061
  ```
1062
1062
 
1063
- Rotates an array by n elements to the right, in place.
1063
+ Rotates array elements by the specified amount to the right, in place.
1064
1064
 
1065
- If n is negative, the array will be rotated by n elements
1066
- to the left. See examples.
1065
+ If value is negative, array elements will be rotated by the
1066
+ specified amount to the left. See examples.
1067
1067
 
1068
1068
  Parameters:
1069
1069
 
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
+ }