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