@grain/stdlib 0.4.2 → 0.4.6

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 (61) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/LICENSE +1 -1
  3. package/array.gr +200 -89
  4. package/array.md +81 -5
  5. package/buffer.gr +93 -36
  6. package/bytes.gr +10 -10
  7. package/char.gr +112 -56
  8. package/char.md +200 -0
  9. package/float32.gr +120 -4
  10. package/float32.md +315 -0
  11. package/float64.gr +120 -4
  12. package/float64.md +315 -0
  13. package/hash.gr +42 -15
  14. package/hash.md +44 -0
  15. package/int32.gr +370 -75
  16. package/int32.md +833 -0
  17. package/int64.gr +370 -75
  18. package/int64.md +833 -0
  19. package/list.gr +121 -50
  20. package/map.gr +106 -110
  21. package/number.gr +37 -1
  22. package/number.md +66 -0
  23. package/option.gr +260 -53
  24. package/option.md +579 -0
  25. package/package.json +1 -1
  26. package/pervasives.gr +32 -20
  27. package/queue.gr +102 -30
  28. package/queue.md +191 -0
  29. package/range.gr +26 -26
  30. package/range.md +1 -1
  31. package/regex.md +9 -9
  32. package/result.gr +216 -70
  33. package/result.md +446 -0
  34. package/runtime/dataStructures.gr +28 -29
  35. package/runtime/debug.gr +0 -1
  36. package/runtime/equal.gr +37 -16
  37. package/runtime/exception.gr +28 -15
  38. package/runtime/gc.gr +33 -20
  39. package/runtime/malloc.gr +19 -11
  40. package/runtime/numberUtils.gr +208 -103
  41. package/runtime/numbers.gr +217 -118
  42. package/runtime/string.gr +98 -39
  43. package/runtime/stringUtils.gr +176 -0
  44. package/runtime/unsafe/conv.gr +10 -10
  45. package/runtime/unsafe/memory.gr +14 -3
  46. package/runtime/unsafe/printWasm.gr +4 -4
  47. package/runtime/unsafe/tags.gr +2 -2
  48. package/runtime/unsafe/wasmf32.gr +9 -2
  49. package/runtime/unsafe/wasmf64.gr +9 -2
  50. package/runtime/unsafe/wasmi32.gr +65 -47
  51. package/runtime/unsafe/wasmi64.gr +78 -50
  52. package/runtime/wasi.gr +199 -45
  53. package/set.gr +281 -119
  54. package/set.md +502 -0
  55. package/stack.gr +26 -26
  56. package/string.gr +657 -341
  57. package/string.md +815 -0
  58. package/sys/file.gr +356 -177
  59. package/sys/process.gr +10 -6
  60. package/sys/random.gr +3 -6
  61. package/sys/time.gr +3 -3
package/int32.gr CHANGED
@@ -1,39 +1,93 @@
1
+ /**
2
+ * @module Int32: Utilities for working with the Int32 type.
3
+ * @example import Int32 from "int32"
4
+ *
5
+ * @since v0.2.0
6
+ */
1
7
  import WasmI32 from "runtime/unsafe/wasmi32"
2
8
  import Memory from "runtime/unsafe/memory"
3
9
  import Exception from "runtime/exception"
4
10
 
5
- import {
6
- newInt32
7
- } from "runtime/dataStructures"
11
+ import { newInt32 } from "runtime/dataStructures"
8
12
 
9
13
  import {
10
14
  coerceNumberToInt32 as fromNumber,
11
- coerceInt32ToNumber as toNumber
15
+ coerceInt32ToNumber as toNumber,
12
16
  } from "runtime/numbers"
13
17
 
18
+ /**
19
+ * @section Conversions: Functions for converting between Numbers and the Int32 type.
20
+ */
21
+
22
+ /**
23
+ * Converts a Number to an Int32.
24
+ *
25
+ * @param number: The value to convert
26
+ * @returns The Number represented as an Int32
27
+ *
28
+ * @since v0.2.0
29
+ */
14
30
  export fromNumber
31
+
32
+ /**
33
+ * Converts an Int32 to a Number.
34
+ *
35
+ * @param value: The value to convert
36
+ * @returns The Int32 represented as a Number
37
+ *
38
+ * @since v0.2.0
39
+ */
15
40
  export toNumber
16
41
 
42
+ /**
43
+ * @section Operations: Mathematical operations for Int32 values.
44
+ */
45
+
46
+ /**
47
+ * Increments the value by one.
48
+ *
49
+ * @param value: The value to increment
50
+ * @returns The incremented value
51
+ *
52
+ * @since v0.2.0
53
+ */
17
54
  @disableGC
18
- export let rec incr = (n: Int32) => {
19
- let n = WasmI32.fromGrain(n)
20
- let ptr = newInt32(WasmI32.add(WasmI32.load(n, 8n), 1n))
55
+ export let rec incr = (value: Int32) => {
56
+ let value = WasmI32.fromGrain(value)
57
+ let ptr = newInt32(WasmI32.add(WasmI32.load(value, 8n), 1n))
21
58
  let ret = WasmI32.toGrain(ptr): Int32
22
- Memory.decRef(n)
59
+ Memory.decRef(value)
23
60
  Memory.decRef(WasmI32.fromGrain(incr))
24
61
  ret
25
62
  }
26
63
 
64
+ /**
65
+ * Decrements the value by one.
66
+ *
67
+ * @param value: The value to decrement
68
+ * @returns The decremented value
69
+ *
70
+ * @since v0.2.0
71
+ */
27
72
  @disableGC
28
- export let rec decr = (n: Int32) => {
29
- let n = WasmI32.fromGrain(n)
30
- let ptr = newInt32(WasmI32.sub(WasmI32.load(n, 8n), 1n))
73
+ export let rec decr = (value: Int32) => {
74
+ let value = WasmI32.fromGrain(value)
75
+ let ptr = newInt32(WasmI32.sub(WasmI32.load(value, 8n), 1n))
31
76
  let ret = WasmI32.toGrain(ptr): Int32
32
- Memory.decRef(n)
77
+ Memory.decRef(value)
33
78
  Memory.decRef(WasmI32.fromGrain(decr))
34
79
  ret
35
80
  }
36
81
 
82
+ /**
83
+ * Computes the sum of its operands.
84
+ *
85
+ * @param x: The first operand
86
+ * @param y: The second operand
87
+ * @returns The sum of the two operands
88
+ *
89
+ * @since v0.2.0
90
+ */
37
91
  @disableGC
38
92
  export let rec add = (x: Int32, y: Int32) => {
39
93
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -46,6 +100,15 @@ export let rec add = (x: Int32, y: Int32) => {
46
100
  ret
47
101
  }
48
102
 
103
+ /**
104
+ * Computes the difference of its operands.
105
+ *
106
+ * @param x: The first operand
107
+ * @param y: The second operand
108
+ * @returns The difference of the two operands
109
+ *
110
+ * @since v0.2.0
111
+ */
49
112
  @disableGC
50
113
  export let rec sub = (x: Int32, y: Int32) => {
51
114
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -58,6 +121,15 @@ export let rec sub = (x: Int32, y: Int32) => {
58
121
  ret
59
122
  }
60
123
 
124
+ /**
125
+ * Computes the product of its operands.
126
+ *
127
+ * @param x: The first operand
128
+ * @param y: The second operand
129
+ * @returns The product of the two operands
130
+ *
131
+ * @since v0.2.0
132
+ */
61
133
  @disableGC
62
134
  export let rec mul = (x: Int32, y: Int32) => {
63
135
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -70,6 +142,15 @@ export let rec mul = (x: Int32, y: Int32) => {
70
142
  ret
71
143
  }
72
144
 
145
+ /**
146
+ * Computes the quotient of its operands using signed division.
147
+ *
148
+ * @param x: The first operand
149
+ * @param y: The second operand
150
+ * @returns The quotient of its operands
151
+ *
152
+ * @since v0.2.0
153
+ */
73
154
  @disableGC
74
155
  export let rec div = (x: Int32, y: Int32) => {
75
156
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -82,6 +163,15 @@ export let rec div = (x: Int32, y: Int32) => {
82
163
  ret
83
164
  }
84
165
 
166
+ /**
167
+ * Computes the quotient of its operands using unsigned division.
168
+ *
169
+ * @param x: The first operand
170
+ * @param y: The second operand
171
+ * @returns The quotient of its operands
172
+ *
173
+ * @since v0.2.0
174
+ */
85
175
  @disableGC
86
176
  export let rec divU = (x: Int32, y: Int32) => {
87
177
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -94,6 +184,15 @@ export let rec divU = (x: Int32, y: Int32) => {
94
184
  ret
95
185
  }
96
186
 
187
+ /**
188
+ * Computes the remainder of the division of its operands using signed division.
189
+ *
190
+ * @param x: The first operand
191
+ * @param y: The second operand
192
+ * @returns The remainder of its operands
193
+ *
194
+ * @since v0.2.0
195
+ */
97
196
  @disableGC
98
197
  export let rec rem = (x: Int32, y: Int32) => {
99
198
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -106,6 +205,15 @@ export let rec rem = (x: Int32, y: Int32) => {
106
205
  ret
107
206
  }
108
207
 
208
+ /**
209
+ * Computes the remainder of the division of its operands using unsigned division.
210
+ *
211
+ * @param x: The first operand
212
+ * @param y: The second operand
213
+ * @returns The remainder of its operands
214
+ *
215
+ * @since v0.2.0
216
+ */
109
217
  @disableGC
110
218
  export let rec remU = (x: Int32, y: Int32) => {
111
219
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -119,11 +227,21 @@ export let rec remU = (x: Int32, y: Int32) => {
119
227
  }
120
228
 
121
229
  @disableGC
122
- let abs = (n) => {
230
+ let abs = n => {
123
231
  let mask = WasmI32.shrS(n, 31n)
124
232
  WasmI32.sub(WasmI32.xor(n, mask), mask)
125
233
  }
126
234
 
235
+ /**
236
+ * Computes the remainder of the division of the first operand by the second.
237
+ * The result will have the sign of the second operand.
238
+ *
239
+ * @param x: The first operand
240
+ * @param y: The second operand
241
+ * @returns The modulus of its operands
242
+ *
243
+ * @since v0.2.0
244
+ */
127
245
  @disableGC
128
246
  export let rec mod = (x: Int32, y: Int32) => {
129
247
  let xval = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -138,7 +256,11 @@ export let rec mod = (x: Int32, y: Int32) => {
138
256
  let yabs = abs(yval)
139
257
  let mval = WasmI32.remS(xabs, yabs)
140
258
  let mres = WasmI32.sub(yabs, mval)
141
- newInt32(if (WasmI32.ne(mval, 0n)) (if (WasmI32.ltS(yval, 0n)) WasmI32.sub(0n, mres) else mres) else 0n)
259
+ newInt32(
260
+ if (WasmI32.ne(mval, 0n)) (
261
+ if (WasmI32.ltS(yval, 0n)) WasmI32.sub(0n, mres) else mres
262
+ ) else 0n
263
+ )
142
264
  } else {
143
265
  newInt32(WasmI32.remS(xval, yval))
144
266
  }
@@ -149,62 +271,128 @@ export let rec mod = (x: Int32, y: Int32) => {
149
271
  ret
150
272
  }
151
273
 
274
+ /**
275
+ * @section Bitwise operations: Functions for operating on bits of Int32 values.
276
+ */
277
+
278
+ /**
279
+ * Rotates the bits of the value left by the given number of bits.
280
+ *
281
+ * @param value: The value to rotate
282
+ * @param amount: The number of bits to rotate by
283
+ * @returns The rotated value
284
+ *
285
+ * @since v0.4.0
286
+ */
152
287
  @disableGC
153
- export let rec clz = (n: Int32) => {
154
- let nv = WasmI32.load(WasmI32.fromGrain(n), 8n)
155
- let ptr = newInt32(WasmI32.clz(nv))
288
+ export let rec rotl = (value: Int32, amount: Int32) => {
289
+ let xv = WasmI32.load(WasmI32.fromGrain(value), 8n)
290
+ let yv = WasmI32.load(WasmI32.fromGrain(amount), 8n)
291
+ let ptr = newInt32(WasmI32.rotl(xv, yv))
156
292
  let ret = WasmI32.toGrain(ptr): Int32
157
- Memory.decRef(WasmI32.fromGrain(n))
158
- Memory.decRef(WasmI32.fromGrain(clz))
293
+ Memory.decRef(WasmI32.fromGrain(value))
294
+ Memory.decRef(WasmI32.fromGrain(amount))
295
+ Memory.decRef(WasmI32.fromGrain(rotl))
159
296
  ret
160
297
  }
161
298
 
299
+ /**
300
+ * Rotates the bits of the value right by the given number of bits.
301
+ *
302
+ * @param value: The value to rotate
303
+ * @param amount: The number of bits to rotate by
304
+ * @returns The rotated value
305
+ *
306
+ * @since v0.4.0
307
+ */
162
308
  @disableGC
163
- export let rec ctz = (n: Int32) => {
164
- let nv = WasmI32.load(WasmI32.fromGrain(n), 8n)
165
- let ptr = newInt32(WasmI32.ctz(nv))
309
+ export let rec rotr = (value: Int32, amount: Int32) => {
310
+ let xv = WasmI32.load(WasmI32.fromGrain(value), 8n)
311
+ let yv = WasmI32.load(WasmI32.fromGrain(amount), 8n)
312
+ let ptr = newInt32(WasmI32.rotr(xv, yv))
166
313
  let ret = WasmI32.toGrain(ptr): Int32
167
- Memory.decRef(WasmI32.fromGrain(n))
168
- Memory.decRef(WasmI32.fromGrain(ctz))
314
+ Memory.decRef(WasmI32.fromGrain(value))
315
+ Memory.decRef(WasmI32.fromGrain(amount))
316
+ Memory.decRef(WasmI32.fromGrain(rotr))
169
317
  ret
170
318
  }
171
319
 
320
+ /**
321
+ * Shifts the bits of the value left by the given number of bits.
322
+ *
323
+ * @param value: The value to shift
324
+ * @param amount: The number of bits to shift by
325
+ * @returns The shifted value
326
+ *
327
+ * @since v0.2.0
328
+ */
172
329
  @disableGC
173
- export let rec popcnt = (n: Int32) => {
174
- let nv = WasmI32.load(WasmI32.fromGrain(n), 8n)
175
- let ptr = newInt32(WasmI32.popcnt(nv))
330
+ export let rec shl = (value: Int32, amount: Int32) => {
331
+ let xv = WasmI32.load(WasmI32.fromGrain(value), 8n)
332
+ let yv = WasmI32.load(WasmI32.fromGrain(amount), 8n)
333
+ let ptr = newInt32(WasmI32.shl(xv, yv))
176
334
  let ret = WasmI32.toGrain(ptr): Int32
177
- Memory.decRef(WasmI32.fromGrain(n))
178
- Memory.decRef(WasmI32.fromGrain(popcnt))
335
+ Memory.decRef(WasmI32.fromGrain(value))
336
+ Memory.decRef(WasmI32.fromGrain(amount))
337
+ Memory.decRef(WasmI32.fromGrain(shl))
179
338
  ret
180
339
  }
181
340
 
341
+ /**
342
+ * Shifts the bits of the value right by the given number of bits, preserving the sign bit.
343
+ *
344
+ * @param value: The value to shift
345
+ * @param amount: The amount to shift by
346
+ * @returns The shifted value
347
+ *
348
+ * @since v0.2.0
349
+ */
182
350
  @disableGC
183
- export let rec rotl = (x: Int32, y: Int32) => {
184
- let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
185
- let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
186
- let ptr = newInt32(WasmI32.rotl(xv, yv))
351
+ export let rec shr = (value: Int32, amount: Int32) => {
352
+ let xv = WasmI32.load(WasmI32.fromGrain(value), 8n)
353
+ let yv = WasmI32.load(WasmI32.fromGrain(amount), 8n)
354
+ let ptr = newInt32(WasmI32.shrS(xv, yv))
187
355
  let ret = WasmI32.toGrain(ptr): Int32
188
- Memory.decRef(WasmI32.fromGrain(x))
189
- Memory.decRef(WasmI32.fromGrain(y))
190
- Memory.decRef(WasmI32.fromGrain(rotl))
356
+ Memory.decRef(WasmI32.fromGrain(value))
357
+ Memory.decRef(WasmI32.fromGrain(amount))
358
+ Memory.decRef(WasmI32.fromGrain(shr))
191
359
  ret
192
360
  }
193
361
 
362
+ /**
363
+ * Shifts the bits of the value right by the given number of bits.
364
+ *
365
+ * @param value: The value to shift
366
+ * @param amount: The amount to shift by
367
+ * @returns The shifted value
368
+ *
369
+ * @since v0.2.0
370
+ */
194
371
  @disableGC
195
- export let rec rotr = (x: Int32, y: Int32) => {
196
- let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
197
- let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
198
- let ptr = newInt32(WasmI32.rotr(xv, yv))
372
+ export let rec shrU = (value: Int32, amount: Int32) => {
373
+ let xv = WasmI32.load(WasmI32.fromGrain(value), 8n)
374
+ let yv = WasmI32.load(WasmI32.fromGrain(amount), 8n)
375
+ let ptr = newInt32(WasmI32.shrU(xv, yv))
199
376
  let ret = WasmI32.toGrain(ptr): Int32
200
- Memory.decRef(WasmI32.fromGrain(x))
201
- Memory.decRef(WasmI32.fromGrain(y))
202
- Memory.decRef(WasmI32.fromGrain(rotr))
377
+ Memory.decRef(WasmI32.fromGrain(value))
378
+ Memory.decRef(WasmI32.fromGrain(amount))
379
+ Memory.decRef(WasmI32.fromGrain(shrU))
203
380
  ret
204
381
  }
205
382
 
206
- // Int32 comparisons
207
-
383
+ /**
384
+ * @section Comparisons: Functions for comparing Int32 values.
385
+ */
386
+
387
+ /**
388
+ * Checks if the first value is equal to the second value.
389
+ *
390
+ * @param x: The first value
391
+ * @param y: The second value
392
+ * @returns `true` if the first value is equal to the second value or `false` otherwise
393
+ *
394
+ * @since v0.4.0
395
+ */
208
396
  @disableGC
209
397
  export let rec eq = (x: Int32, y: Int32) => {
210
398
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -216,6 +404,15 @@ export let rec eq = (x: Int32, y: Int32) => {
216
404
  ret
217
405
  }
218
406
 
407
+ /**
408
+ * Checks if the first value is not equal to the second value.
409
+ *
410
+ * @param x: The first value
411
+ * @param y: The second value
412
+ * @returns `true` if the first value is not equal to the second value or `false` otherwise
413
+ *
414
+ * @since v0.4.0
415
+ */
219
416
  @disableGC
220
417
  export let rec ne = (x: Int32, y: Int32) => {
221
418
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -227,15 +424,32 @@ export let rec ne = (x: Int32, y: Int32) => {
227
424
  ret
228
425
  }
229
426
 
427
+ /**
428
+ * Checks if the given value is equal to zero.
429
+ *
430
+ * @param value: The value to inspect
431
+ * @returns `true` if the first value is equal to zero or `false` otherwise
432
+ *
433
+ * @since v0.4.0
434
+ */
230
435
  @disableGC
231
- export let rec eqz = (n: Int32) => {
232
- let xv = WasmI32.load(WasmI32.fromGrain(n), 8n)
436
+ export let rec eqz = (value: Int32) => {
437
+ let xv = WasmI32.load(WasmI32.fromGrain(value), 8n)
233
438
  let ret = WasmI32.eqz(xv)
234
- Memory.decRef(WasmI32.fromGrain(n))
439
+ Memory.decRef(WasmI32.fromGrain(value))
235
440
  Memory.decRef(WasmI32.fromGrain(eqz))
236
441
  ret
237
442
  }
238
443
 
444
+ /**
445
+ * Checks if the first value is less than the second value.
446
+ *
447
+ * @param x: The first value
448
+ * @param y: The second value
449
+ * @returns `true` if the first value is less than the second value or `false` otherwise
450
+ *
451
+ * @since v0.2.0
452
+ */
239
453
  @disableGC
240
454
  export let rec lt = (x: Int32, y: Int32) => {
241
455
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -247,6 +461,15 @@ export let rec lt = (x: Int32, y: Int32) => {
247
461
  ret
248
462
  }
249
463
 
464
+ /**
465
+ * Checks if the first value is greater than the second value.
466
+ *
467
+ * @param x: The first value
468
+ * @param y: The second value
469
+ * @returns `true` if the first value is greater than the second value or `false` otherwise
470
+ *
471
+ * @since v0.2.0
472
+ */
250
473
  @disableGC
251
474
  export let rec gt = (x: Int32, y: Int32) => {
252
475
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -258,6 +481,15 @@ export let rec gt = (x: Int32, y: Int32) => {
258
481
  ret
259
482
  }
260
483
 
484
+ /**
485
+ * Checks if the first value is less than or equal to the second value.
486
+ *
487
+ * @param x: The first value
488
+ * @param y: The second value
489
+ * @returns `true` if the first value is less than or equal to the second value or `false` otherwise
490
+ *
491
+ * @since v0.2.0
492
+ */
261
493
  @disableGC
262
494
  export let rec lte = (x: Int32, y: Int32) => {
263
495
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -269,6 +501,15 @@ export let rec lte = (x: Int32, y: Int32) => {
269
501
  ret
270
502
  }
271
503
 
504
+ /**
505
+ * Checks if the first value is greater than or equal to the second value.
506
+ *
507
+ * @param x: The first value
508
+ * @param y: The second value
509
+ * @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
510
+ *
511
+ * @since v0.2.0
512
+ */
272
513
  @disableGC
273
514
  export let rec gte = (x: Int32, y: Int32) => {
274
515
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -280,18 +521,37 @@ export let rec gte = (x: Int32, y: Int32) => {
280
521
  ret
281
522
  }
282
523
 
283
- // Int32 bit/logical operations
524
+ /**
525
+ * @section Bitwise logic: Boolean operations on the bits of Int32 values.
526
+ */
527
+
528
+ /**
529
+ * Computes the bitwise NOT of the given value.
530
+ *
531
+ * @param value: The given value
532
+ * @returns Containing the inverted bits of the given value
533
+ *
534
+ * @since v0.2.0
535
+ */
284
536
  @disableGC
285
- export let rec lnot = (x: Int32) => {
286
- let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
537
+ export let rec lnot = (value: Int32) => {
538
+ let xv = WasmI32.load(WasmI32.fromGrain(value), 8n)
287
539
  let ptr = newInt32(WasmI32.xor(xv, 0xffffffffn))
288
540
  let ret = WasmI32.toGrain(ptr): Int32
289
- Memory.decRef(WasmI32.fromGrain(x))
541
+ Memory.decRef(WasmI32.fromGrain(value))
290
542
  Memory.decRef(WasmI32.fromGrain(lnot))
291
543
  ret
292
544
  }
293
545
 
294
-
546
+ /**
547
+ * Computes the bitwise AND (`&`) on the given operands.
548
+ *
549
+ * @param x: The first operand
550
+ * @param y: The second operand
551
+ * @returns Containing a `1` in each bit position for which the corresponding bits of both operands are `1`
552
+ *
553
+ * @since v0.2.0
554
+ */
295
555
  @disableGC
296
556
  export let rec land = (x: Int32, y: Int32) => {
297
557
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -304,6 +564,15 @@ export let rec land = (x: Int32, y: Int32) => {
304
564
  ret
305
565
  }
306
566
 
567
+ /**
568
+ * Computes the bitwise OR (`|`) on the given operands.
569
+ *
570
+ * @param x: The first operand
571
+ * @param y: The second operand
572
+ * @returns Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`
573
+ *
574
+ * @since v0.2.0
575
+ */
307
576
  @disableGC
308
577
  export let rec lor = (x: Int32, y: Int32) => {
309
578
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -316,6 +585,15 @@ export let rec lor = (x: Int32, y: Int32) => {
316
585
  ret
317
586
  }
318
587
 
588
+ /**
589
+ * Computes the bitwise XOR (`^`) on the given operands.
590
+ *
591
+ * @param x: The first operand
592
+ * @param y: The second operand
593
+ * @returns Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`
594
+ *
595
+ * @since v0.2.0
596
+ */
319
597
  @disableGC
320
598
  export let rec lxor = (x: Int32, y: Int32) => {
321
599
  let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
@@ -328,39 +606,56 @@ export let rec lxor = (x: Int32, y: Int32) => {
328
606
  ret
329
607
  }
330
608
 
331
-
609
+ /**
610
+ * Counts the number of leading zero bits in the value.
611
+ *
612
+ * @param value: The value to inspect
613
+ * @returns The amount of leading zeros
614
+ *
615
+ * @since v0.4.0
616
+ */
332
617
  @disableGC
333
- export let rec shl = (x: Int32, y: Int32) => {
334
- let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
335
- let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
336
- let ptr = newInt32(WasmI32.shl(xv, yv))
618
+ export let rec clz = (value: Int32) => {
619
+ let nv = WasmI32.load(WasmI32.fromGrain(value), 8n)
620
+ let ptr = newInt32(WasmI32.clz(nv))
337
621
  let ret = WasmI32.toGrain(ptr): Int32
338
- Memory.decRef(WasmI32.fromGrain(x))
339
- Memory.decRef(WasmI32.fromGrain(y))
340
- Memory.decRef(WasmI32.fromGrain(shl))
622
+ Memory.decRef(WasmI32.fromGrain(value))
623
+ Memory.decRef(WasmI32.fromGrain(clz))
341
624
  ret
342
625
  }
343
626
 
627
+ /**
628
+ * Counts the number of trailing zero bits in the value.
629
+ *
630
+ * @param value: The value to inspect
631
+ * @returns The amount of trailing zeros
632
+ *
633
+ * @since v0.4.0
634
+ */
344
635
  @disableGC
345
- export let rec shr = (x: Int32, y: Int32) => {
346
- let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
347
- let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
348
- let ptr = newInt32(WasmI32.shrS(xv, yv))
636
+ export let rec ctz = (value: Int32) => {
637
+ let nv = WasmI32.load(WasmI32.fromGrain(value), 8n)
638
+ let ptr = newInt32(WasmI32.ctz(nv))
349
639
  let ret = WasmI32.toGrain(ptr): Int32
350
- Memory.decRef(WasmI32.fromGrain(x))
351
- Memory.decRef(WasmI32.fromGrain(y))
352
- Memory.decRef(WasmI32.fromGrain(shr))
640
+ Memory.decRef(WasmI32.fromGrain(value))
641
+ Memory.decRef(WasmI32.fromGrain(ctz))
353
642
  ret
354
643
  }
355
644
 
645
+ /**
646
+ * Counts the number of bits set to `1` in the value, also known as a population count.
647
+ *
648
+ * @param value: The value to inspect
649
+ * @returns The amount of 1-bits in its operand
650
+ *
651
+ * @since v0.4.0
652
+ */
356
653
  @disableGC
357
- export let rec shrU = (x: Int32, y: Int32) => {
358
- let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
359
- let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
360
- let ptr = newInt32(WasmI32.shrU(xv, yv))
654
+ export let rec popcnt = (value: Int32) => {
655
+ let nv = WasmI32.load(WasmI32.fromGrain(value), 8n)
656
+ let ptr = newInt32(WasmI32.popcnt(nv))
361
657
  let ret = WasmI32.toGrain(ptr): Int32
362
- Memory.decRef(WasmI32.fromGrain(x))
363
- Memory.decRef(WasmI32.fromGrain(y))
364
- Memory.decRef(WasmI32.fromGrain(shrU))
658
+ Memory.decRef(WasmI32.fromGrain(value))
659
+ Memory.decRef(WasmI32.fromGrain(popcnt))
365
660
  ret
366
661
  }