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