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