@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.
- package/CHANGELOG.md +87 -0
- package/LICENSE +1 -1
- package/array.gr +92 -73
- package/array.md +18 -18
- package/bigint.gr +497 -0
- package/bigint.md +811 -0
- package/buffer.gr +56 -217
- package/buffer.md +24 -17
- package/bytes.gr +103 -205
- package/bytes.md +19 -0
- package/char.gr +152 -166
- package/char.md +200 -0
- package/exception.md +6 -0
- package/float32.gr +159 -82
- package/float32.md +315 -0
- package/float64.gr +163 -82
- package/float64.md +315 -0
- package/hash.gr +53 -49
- package/int32.gr +479 -230
- package/int32.md +937 -0
- package/int64.gr +479 -230
- package/int64.md +937 -0
- package/list.gr +530 -116
- package/list.md +1141 -0
- package/map.gr +302 -121
- package/map.md +525 -0
- package/number.gr +51 -57
- package/number.md +37 -3
- package/option.gr +25 -25
- package/option.md +1 -1
- package/package.json +3 -3
- package/pervasives.gr +504 -52
- package/pervasives.md +1116 -0
- package/queue.gr +8 -1
- package/queue.md +10 -0
- package/random.gr +196 -0
- package/random.md +179 -0
- package/range.gr +26 -26
- package/regex.gr +1833 -842
- package/regex.md +11 -11
- package/result.md +1 -1
- package/runtime/bigint.gr +2045 -0
- package/runtime/bigint.md +326 -0
- package/runtime/dataStructures.gr +99 -279
- package/runtime/dataStructures.md +391 -0
- package/runtime/debug.gr +0 -1
- package/runtime/debug.md +6 -0
- package/runtime/equal.gr +40 -37
- package/runtime/equal.md +6 -0
- package/runtime/exception.gr +28 -15
- package/runtime/exception.md +30 -0
- package/runtime/gc.gr +50 -20
- package/runtime/gc.md +36 -0
- package/runtime/malloc.gr +32 -22
- package/runtime/malloc.md +55 -0
- package/runtime/numberUtils.gr +297 -142
- package/runtime/numberUtils.md +54 -0
- package/runtime/numbers.gr +1204 -453
- package/runtime/numbers.md +300 -0
- package/runtime/string.gr +193 -228
- package/runtime/string.md +24 -0
- package/runtime/stringUtils.gr +62 -38
- package/runtime/stringUtils.md +6 -0
- package/runtime/unsafe/constants.gr +17 -0
- package/runtime/unsafe/constants.md +72 -0
- package/runtime/unsafe/conv.gr +10 -10
- package/runtime/unsafe/conv.md +71 -0
- package/runtime/unsafe/errors.md +204 -0
- package/runtime/unsafe/memory.gr +14 -3
- package/runtime/unsafe/memory.md +54 -0
- package/runtime/unsafe/printWasm.gr +4 -4
- package/runtime/unsafe/printWasm.md +24 -0
- package/runtime/unsafe/tags.gr +11 -10
- package/runtime/unsafe/tags.md +120 -0
- package/runtime/unsafe/wasmf32.gr +9 -2
- package/runtime/unsafe/wasmf32.md +168 -0
- package/runtime/unsafe/wasmf64.gr +9 -2
- package/runtime/unsafe/wasmf64.md +168 -0
- package/runtime/unsafe/wasmi32.gr +65 -47
- package/runtime/unsafe/wasmi32.md +282 -0
- package/runtime/unsafe/wasmi64.gr +78 -50
- package/runtime/unsafe/wasmi64.md +300 -0
- package/runtime/utils/printing.gr +62 -0
- package/runtime/utils/printing.md +18 -0
- package/runtime/wasi.gr +200 -46
- package/runtime/wasi.md +839 -0
- package/set.gr +125 -121
- package/set.md +24 -21
- package/stack.gr +29 -29
- package/stack.md +4 -6
- package/string.gr +434 -415
- package/string.md +3 -3
- package/sys/file.gr +477 -482
- package/sys/process.gr +33 -47
- package/sys/random.gr +48 -20
- package/sys/random.md +38 -0
- 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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
38
|
-
|
|
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
|
-
|
|
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
|
-
|
|
50
|
-
|
|
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
|
-
|
|
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
|
-
|
|
62
|
-
|
|
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
|
-
|
|
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
|
-
|
|
74
|
-
|
|
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
|
-
|
|
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
|
-
|
|
86
|
-
|
|
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
|
-
|
|
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
|
-
|
|
98
|
-
|
|
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
|
-
|
|
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
|
-
|
|
110
|
-
|
|
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
|
-
|
|
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
|
-
@
|
|
122
|
-
let abs =
|
|
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
|
-
|
|
128
|
-
|
|
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(
|
|
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
|
-
|
|
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
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
-
|
|
183
|
-
|
|
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
|
-
|
|
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
|
-
|
|
195
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
209
|
-
|
|
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
|
-
|
|
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
|
-
|
|
220
|
-
|
|
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
|
-
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
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
|
-
|
|
240
|
-
|
|
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
|
-
|
|
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
|
-
|
|
251
|
-
|
|
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
|
-
|
|
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
|
-
|
|
262
|
-
|
|
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
|
-
|
|
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
|
-
|
|
273
|
-
|
|
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
|
-
|
|
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
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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
|
|
288
|
-
|
|
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
|
-
|
|
296
|
-
|
|
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
|
-
|
|
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
|
-
|
|
308
|
-
|
|
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
|
-
|
|
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
|
-
|
|
320
|
-
|
|
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
|
-
|
|
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
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
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
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
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
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
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
|
}
|