@grain/stdlib 0.5.13 → 0.6.1
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 +201 -0
- package/LICENSE +1 -1
- package/README.md +25 -2
- package/array.gr +1512 -199
- package/array.md +2032 -94
- package/bigint.gr +239 -140
- package/bigint.md +450 -106
- package/buffer.gr +595 -102
- package/buffer.md +903 -145
- package/bytes.gr +401 -110
- package/bytes.md +551 -63
- package/char.gr +228 -49
- package/char.md +373 -7
- package/exception.gr +26 -12
- package/exception.md +29 -5
- package/float32.gr +130 -109
- package/float32.md +185 -57
- package/float64.gr +112 -99
- package/float64.md +185 -57
- package/hash.gr +62 -40
- package/hash.md +27 -3
- package/int16.gr +430 -0
- package/int16.md +618 -0
- package/int32.gr +200 -269
- package/int32.md +254 -289
- package/int64.gr +142 -225
- package/int64.md +254 -289
- package/int8.gr +511 -0
- package/int8.md +786 -0
- package/json.gr +2071 -0
- package/json.md +646 -0
- package/list.gr +120 -68
- package/list.md +125 -80
- package/map.gr +560 -57
- package/map.md +672 -56
- package/marshal.gr +239 -227
- package/marshal.md +36 -4
- package/number.gr +626 -676
- package/number.md +738 -153
- package/option.gr +33 -35
- package/option.md +58 -42
- package/package.json +2 -2
- package/path.gr +148 -187
- package/path.md +47 -96
- package/pervasives.gr +75 -416
- package/pervasives.md +85 -180
- package/priorityqueue.gr +433 -74
- package/priorityqueue.md +422 -54
- package/queue.gr +362 -80
- package/queue.md +433 -38
- package/random.gr +67 -75
- package/random.md +68 -40
- package/range.gr +135 -63
- package/range.md +198 -43
- package/rational.gr +284 -0
- package/rational.md +545 -0
- package/regex.gr +933 -1066
- package/regex.md +59 -60
- package/result.gr +23 -25
- package/result.md +54 -39
- package/runtime/atof/common.gr +78 -82
- package/runtime/atof/common.md +22 -10
- package/runtime/atof/decimal.gr +102 -127
- package/runtime/atof/decimal.md +28 -7
- package/runtime/atof/lemire.gr +56 -71
- package/runtime/atof/lemire.md +9 -1
- package/runtime/atof/parse.gr +83 -110
- package/runtime/atof/parse.md +12 -2
- package/runtime/atof/slow.gr +28 -35
- package/runtime/atof/slow.md +9 -1
- package/runtime/atof/table.gr +19 -18
- package/runtime/atof/table.md +10 -2
- package/runtime/atoi/parse.gr +153 -136
- package/runtime/atoi/parse.md +50 -1
- package/runtime/bigint.gr +410 -517
- package/runtime/bigint.md +71 -57
- package/runtime/compare.gr +176 -85
- package/runtime/compare.md +31 -1
- package/runtime/dataStructures.gr +144 -32
- package/runtime/dataStructures.md +267 -31
- package/runtime/debugPrint.gr +34 -15
- package/runtime/debugPrint.md +37 -5
- package/runtime/equal.gr +53 -52
- package/runtime/equal.md +30 -1
- package/runtime/exception.gr +38 -47
- package/runtime/exception.md +10 -8
- package/runtime/gc.gr +23 -152
- package/runtime/gc.md +13 -17
- package/runtime/malloc.gr +31 -31
- package/runtime/malloc.md +11 -3
- package/runtime/numberUtils.gr +193 -174
- package/runtime/numberUtils.md +29 -9
- package/runtime/numbers.gr +1695 -1021
- package/runtime/numbers.md +1098 -134
- package/runtime/string.gr +543 -245
- package/runtime/string.md +76 -6
- package/runtime/unsafe/constants.gr +30 -13
- package/runtime/unsafe/constants.md +80 -0
- package/runtime/unsafe/conv.gr +55 -28
- package/runtime/unsafe/conv.md +41 -9
- package/runtime/unsafe/memory.gr +10 -30
- package/runtime/unsafe/memory.md +15 -19
- package/runtime/unsafe/tags.gr +37 -21
- package/runtime/unsafe/tags.md +88 -8
- package/runtime/unsafe/wasmf32.gr +30 -36
- package/runtime/unsafe/wasmf32.md +64 -56
- package/runtime/unsafe/wasmf64.gr +30 -36
- package/runtime/unsafe/wasmf64.md +64 -56
- package/runtime/unsafe/wasmi32.gr +49 -66
- package/runtime/unsafe/wasmi32.md +102 -94
- package/runtime/unsafe/wasmi64.gr +52 -79
- package/runtime/unsafe/wasmi64.md +108 -100
- package/runtime/utils/printing.gr +13 -15
- package/runtime/utils/printing.md +11 -3
- package/runtime/wasi.gr +294 -295
- package/runtime/wasi.md +62 -42
- package/set.gr +574 -64
- package/set.md +634 -54
- package/stack.gr +181 -64
- package/stack.md +271 -42
- package/string.gr +453 -533
- package/string.md +241 -151
- package/uint16.gr +369 -0
- package/uint16.md +585 -0
- package/uint32.gr +470 -0
- package/uint32.md +737 -0
- package/uint64.gr +471 -0
- package/uint64.md +737 -0
- package/uint8.gr +369 -0
- package/uint8.md +585 -0
- package/uri.gr +1093 -0
- package/uri.md +477 -0
- package/{sys → wasi}/file.gr +914 -500
- package/{sys → wasi}/file.md +454 -50
- package/wasi/process.gr +292 -0
- package/{sys → wasi}/process.md +164 -6
- package/wasi/random.gr +77 -0
- package/wasi/random.md +80 -0
- package/{sys → wasi}/time.gr +15 -22
- package/{sys → wasi}/time.md +5 -5
- package/immutablearray.gr +0 -929
- package/immutablearray.md +0 -1038
- package/immutablemap.gr +0 -493
- package/immutablemap.md +0 -479
- package/immutablepriorityqueue.gr +0 -360
- package/immutablepriorityqueue.md +0 -291
- package/immutableset.gr +0 -498
- package/immutableset.md +0 -449
- package/runtime/debug.gr +0 -2
- package/runtime/debug.md +0 -6
- package/runtime/unsafe/errors.gr +0 -36
- package/runtime/unsafe/errors.md +0 -204
- package/sys/process.gr +0 -254
- package/sys/random.gr +0 -79
- package/sys/random.md +0 -66
package/int32.gr
CHANGED
|
@@ -1,46 +1,61 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Utilities for working with the Int32 type.
|
|
3
|
+
*
|
|
4
|
+
* @example from "int32" include Int32
|
|
4
5
|
*
|
|
5
6
|
* @since v0.2.0
|
|
6
7
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
module Int32
|
|
9
|
+
|
|
10
|
+
from "runtime/unsafe/wasmi32" include WasmI32
|
|
11
|
+
use WasmI32.{
|
|
12
|
+
(+),
|
|
13
|
+
(-),
|
|
14
|
+
(*),
|
|
15
|
+
(/),
|
|
16
|
+
(&),
|
|
17
|
+
(|),
|
|
18
|
+
(^),
|
|
19
|
+
(<<),
|
|
20
|
+
(>>),
|
|
21
|
+
(==),
|
|
22
|
+
(!=),
|
|
23
|
+
(<),
|
|
24
|
+
(<=),
|
|
25
|
+
(>),
|
|
26
|
+
(>=),
|
|
27
|
+
}
|
|
28
|
+
from "runtime/unsafe/wasmi64" include WasmI64
|
|
29
|
+
from "runtime/exception" include Exception
|
|
30
|
+
|
|
31
|
+
from "runtime/dataStructures" include DataStructures
|
|
32
|
+
use DataStructures.{ newInt32 }
|
|
33
|
+
|
|
34
|
+
from "runtime/numbers" include Numbers
|
|
35
|
+
use Numbers.{
|
|
13
36
|
coerceNumberToInt32 as fromNumber,
|
|
14
37
|
coerceInt32ToNumber as toNumber,
|
|
15
|
-
}
|
|
38
|
+
}
|
|
16
39
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*/
|
|
40
|
+
@unsafe
|
|
41
|
+
let _VALUE_OFFSET = 4n
|
|
20
42
|
|
|
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
|
-
*/
|
|
29
|
-
export fromNumber
|
|
43
|
+
provide { fromNumber, toNumber }
|
|
30
44
|
|
|
31
45
|
/**
|
|
32
|
-
* Converts
|
|
46
|
+
* Converts a Uint32 to an Int32.
|
|
33
47
|
*
|
|
34
|
-
* @param
|
|
35
|
-
* @returns The
|
|
48
|
+
* @param number: The value to convert
|
|
49
|
+
* @returns The Uint32 represented as an Int32
|
|
36
50
|
*
|
|
37
|
-
* @since v0.
|
|
38
|
-
*/
|
|
39
|
-
export toNumber
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @section Operations: Mathematical operations for Int32 values.
|
|
51
|
+
* @since v0.6.0
|
|
43
52
|
*/
|
|
53
|
+
@unsafe
|
|
54
|
+
provide let fromUint32 = (number: Uint32) => {
|
|
55
|
+
let x = WasmI32.load(WasmI32.fromGrain(number), _VALUE_OFFSET)
|
|
56
|
+
let result = newInt32(x)
|
|
57
|
+
WasmI32.toGrain(result): Int32
|
|
58
|
+
}
|
|
44
59
|
|
|
45
60
|
/**
|
|
46
61
|
* Increments the value by one.
|
|
@@ -51,9 +66,9 @@ export toNumber
|
|
|
51
66
|
* @since v0.2.0
|
|
52
67
|
*/
|
|
53
68
|
@unsafe
|
|
54
|
-
|
|
69
|
+
provide let incr = (value: Int32) => {
|
|
55
70
|
let value = WasmI32.fromGrain(value)
|
|
56
|
-
let ptr = newInt32(WasmI32.
|
|
71
|
+
let ptr = newInt32(WasmI32.load(value, _VALUE_OFFSET) + 1n)
|
|
57
72
|
WasmI32.toGrain(ptr): Int32
|
|
58
73
|
}
|
|
59
74
|
|
|
@@ -66,9 +81,9 @@ export let incr = (value: Int32) => {
|
|
|
66
81
|
* @since v0.2.0
|
|
67
82
|
*/
|
|
68
83
|
@unsafe
|
|
69
|
-
|
|
84
|
+
provide let decr = (value: Int32) => {
|
|
70
85
|
let value = WasmI32.fromGrain(value)
|
|
71
|
-
let ptr = newInt32(WasmI32.
|
|
86
|
+
let ptr = newInt32(WasmI32.load(value, _VALUE_OFFSET) - 1n)
|
|
72
87
|
WasmI32.toGrain(ptr): Int32
|
|
73
88
|
}
|
|
74
89
|
|
|
@@ -79,13 +94,14 @@ export let decr = (value: Int32) => {
|
|
|
79
94
|
* @param y: The second operand
|
|
80
95
|
* @returns The sum of the two operands
|
|
81
96
|
*
|
|
82
|
-
* @since v0.
|
|
97
|
+
* @since v0.6.0
|
|
98
|
+
* @history v0.2.0: Originally named `add`
|
|
83
99
|
*/
|
|
84
100
|
@unsafe
|
|
85
|
-
|
|
86
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
87
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
88
|
-
let ptr = newInt32(
|
|
101
|
+
provide let (+) = (x: Int32, y: Int32) => {
|
|
102
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
103
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
104
|
+
let ptr = newInt32(xv + yv)
|
|
89
105
|
WasmI32.toGrain(ptr): Int32
|
|
90
106
|
}
|
|
91
107
|
|
|
@@ -96,13 +112,14 @@ export let add = (x: Int32, y: Int32) => {
|
|
|
96
112
|
* @param y: The second operand
|
|
97
113
|
* @returns The difference of the two operands
|
|
98
114
|
*
|
|
99
|
-
* @since v0.
|
|
115
|
+
* @since v0.6.0
|
|
116
|
+
* @history v0.2.0: Originally named `sub`
|
|
100
117
|
*/
|
|
101
118
|
@unsafe
|
|
102
|
-
|
|
103
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
104
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
105
|
-
let ptr = newInt32(
|
|
119
|
+
provide let (-) = (x: Int32, y: Int32) => {
|
|
120
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
121
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
122
|
+
let ptr = newInt32(xv - yv)
|
|
106
123
|
WasmI32.toGrain(ptr): Int32
|
|
107
124
|
}
|
|
108
125
|
|
|
@@ -113,13 +130,14 @@ export let sub = (x: Int32, y: Int32) => {
|
|
|
113
130
|
* @param y: The second operand
|
|
114
131
|
* @returns The product of the two operands
|
|
115
132
|
*
|
|
116
|
-
* @since v0.
|
|
133
|
+
* @since v0.6.0
|
|
134
|
+
* @history v0.2.0: Originally named `mul`
|
|
117
135
|
*/
|
|
118
136
|
@unsafe
|
|
119
|
-
|
|
120
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
121
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
122
|
-
let ptr = newInt32(
|
|
137
|
+
provide let (*) = (x: Int32, y: Int32) => {
|
|
138
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
139
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
140
|
+
let ptr = newInt32(xv * yv)
|
|
123
141
|
WasmI32.toGrain(ptr): Int32
|
|
124
142
|
}
|
|
125
143
|
|
|
@@ -130,30 +148,14 @@ export let mul = (x: Int32, y: Int32) => {
|
|
|
130
148
|
* @param y: The second operand
|
|
131
149
|
* @returns The quotient of its operands
|
|
132
150
|
*
|
|
133
|
-
* @since v0.
|
|
134
|
-
|
|
135
|
-
@unsafe
|
|
136
|
-
export let div = (x: Int32, y: Int32) => {
|
|
137
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
|
|
138
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
|
|
139
|
-
let ptr = newInt32(WasmI32.divS(xv, yv))
|
|
140
|
-
WasmI32.toGrain(ptr): Int32
|
|
141
|
-
}
|
|
142
|
-
|
|
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
|
+
* @since v0.6.0
|
|
152
|
+
* @history v0.2.0: Originally named `div`
|
|
151
153
|
*/
|
|
152
154
|
@unsafe
|
|
153
|
-
|
|
154
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
155
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
156
|
-
let ptr = newInt32(
|
|
155
|
+
provide let (/) = (x: Int32, y: Int32) => {
|
|
156
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
157
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
158
|
+
let ptr = newInt32(xv / yv)
|
|
157
159
|
WasmI32.toGrain(ptr): Int32
|
|
158
160
|
}
|
|
159
161
|
|
|
@@ -167,34 +169,18 @@ export let divU = (x: Int32, y: Int32) => {
|
|
|
167
169
|
* @since v0.2.0
|
|
168
170
|
*/
|
|
169
171
|
@unsafe
|
|
170
|
-
|
|
171
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
172
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
172
|
+
provide let rem = (x: Int32, y: Int32) => {
|
|
173
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
174
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
173
175
|
let ptr = newInt32(WasmI32.remS(xv, yv))
|
|
174
176
|
WasmI32.toGrain(ptr): Int32
|
|
175
177
|
}
|
|
176
178
|
|
|
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) => {
|
|
188
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
|
|
189
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
|
|
190
|
-
let ptr = newInt32(WasmI32.remU(xv, yv))
|
|
191
|
-
WasmI32.toGrain(ptr): Int32
|
|
192
|
-
}
|
|
193
|
-
|
|
194
179
|
@unsafe
|
|
195
180
|
let abs = n => {
|
|
196
|
-
|
|
197
|
-
|
|
181
|
+
use WasmI32.{ (-) }
|
|
182
|
+
let mask = n >> 31n
|
|
183
|
+
(n ^ mask) - mask
|
|
198
184
|
}
|
|
199
185
|
|
|
200
186
|
/**
|
|
@@ -207,37 +193,31 @@ let abs = n => {
|
|
|
207
193
|
*
|
|
208
194
|
* @throws ModuloByZero: When `y` is zero
|
|
209
195
|
*
|
|
210
|
-
* @since v0.
|
|
196
|
+
* @since v0.6.0
|
|
197
|
+
* @history v0.2.0: Originally named `mod`
|
|
211
198
|
*/
|
|
212
199
|
@unsafe
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
let
|
|
200
|
+
provide let (%) = (x: Int32, y: Int32) => {
|
|
201
|
+
use WasmI32.{ (-) }
|
|
202
|
+
let xval = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
203
|
+
let yval = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
216
204
|
|
|
217
205
|
if (WasmI32.eqz(yval)) {
|
|
218
206
|
throw Exception.ModuloByZero
|
|
219
207
|
}
|
|
220
208
|
|
|
221
|
-
let ptr = if (
|
|
209
|
+
let ptr = if ((xval ^ yval) < 0n) {
|
|
222
210
|
let xabs = abs(xval)
|
|
223
211
|
let yabs = abs(yval)
|
|
224
212
|
let mval = WasmI32.remS(xabs, yabs)
|
|
225
|
-
let mres =
|
|
226
|
-
newInt32(
|
|
227
|
-
if (WasmI32.ne(mval, 0n)) (
|
|
228
|
-
if (WasmI32.ltS(yval, 0n)) WasmI32.sub(0n, mres) else mres
|
|
229
|
-
) else 0n
|
|
230
|
-
)
|
|
213
|
+
let mres = yabs - mval
|
|
214
|
+
newInt32(if (mval != 0n) (if (yval < 0n) 0n - mres else mres) else 0n)
|
|
231
215
|
} else {
|
|
232
216
|
newInt32(WasmI32.remS(xval, yval))
|
|
233
217
|
}
|
|
234
218
|
WasmI32.toGrain(ptr): Int32
|
|
235
219
|
}
|
|
236
220
|
|
|
237
|
-
/**
|
|
238
|
-
* @section Bitwise operations: Functions for operating on bits of Int32 values.
|
|
239
|
-
*/
|
|
240
|
-
|
|
241
221
|
/**
|
|
242
222
|
* Rotates the bits of the value left by the given number of bits.
|
|
243
223
|
*
|
|
@@ -248,9 +228,9 @@ export let mod = (x: Int32, y: Int32) => {
|
|
|
248
228
|
* @since v0.4.0
|
|
249
229
|
*/
|
|
250
230
|
@unsafe
|
|
251
|
-
|
|
252
|
-
let xv = WasmI32.load(WasmI32.fromGrain(value),
|
|
253
|
-
let yv = WasmI32.load(WasmI32.fromGrain(amount),
|
|
231
|
+
provide let rotl = (value: Int32, amount: Int32) => {
|
|
232
|
+
let xv = WasmI32.load(WasmI32.fromGrain(value), _VALUE_OFFSET)
|
|
233
|
+
let yv = WasmI32.load(WasmI32.fromGrain(amount), _VALUE_OFFSET)
|
|
254
234
|
let ptr = newInt32(WasmI32.rotl(xv, yv))
|
|
255
235
|
WasmI32.toGrain(ptr): Int32
|
|
256
236
|
}
|
|
@@ -265,9 +245,9 @@ export let rotl = (value: Int32, amount: Int32) => {
|
|
|
265
245
|
* @since v0.4.0
|
|
266
246
|
*/
|
|
267
247
|
@unsafe
|
|
268
|
-
|
|
269
|
-
let xv = WasmI32.load(WasmI32.fromGrain(value),
|
|
270
|
-
let yv = WasmI32.load(WasmI32.fromGrain(amount),
|
|
248
|
+
provide let rotr = (value: Int32, amount: Int32) => {
|
|
249
|
+
let xv = WasmI32.load(WasmI32.fromGrain(value), _VALUE_OFFSET)
|
|
250
|
+
let yv = WasmI32.load(WasmI32.fromGrain(amount), _VALUE_OFFSET)
|
|
271
251
|
let ptr = newInt32(WasmI32.rotr(xv, yv))
|
|
272
252
|
WasmI32.toGrain(ptr): Int32
|
|
273
253
|
}
|
|
@@ -279,13 +259,14 @@ export let rotr = (value: Int32, amount: Int32) => {
|
|
|
279
259
|
* @param amount: The number of bits to shift by
|
|
280
260
|
* @returns The shifted value
|
|
281
261
|
*
|
|
282
|
-
* @since v0.
|
|
262
|
+
* @since v0.6.0
|
|
263
|
+
* @history v0.2.0: Originally named `shl`
|
|
283
264
|
*/
|
|
284
265
|
@unsafe
|
|
285
|
-
|
|
286
|
-
let xv = WasmI32.load(WasmI32.fromGrain(value),
|
|
287
|
-
let yv = WasmI32.load(WasmI32.fromGrain(amount),
|
|
288
|
-
let ptr = newInt32(
|
|
266
|
+
provide let (<<) = (value: Int32, amount: Int32) => {
|
|
267
|
+
let xv = WasmI32.load(WasmI32.fromGrain(value), _VALUE_OFFSET)
|
|
268
|
+
let yv = WasmI32.load(WasmI32.fromGrain(amount), _VALUE_OFFSET)
|
|
269
|
+
let ptr = newInt32(xv << yv)
|
|
289
270
|
WasmI32.toGrain(ptr): Int32
|
|
290
271
|
}
|
|
291
272
|
|
|
@@ -296,37 +277,17 @@ export let shl = (value: Int32, amount: Int32) => {
|
|
|
296
277
|
* @param amount: The amount to shift by
|
|
297
278
|
* @returns The shifted value
|
|
298
279
|
*
|
|
299
|
-
* @since v0.
|
|
300
|
-
|
|
301
|
-
@unsafe
|
|
302
|
-
export let shr = (value: Int32, amount: Int32) => {
|
|
303
|
-
let xv = WasmI32.load(WasmI32.fromGrain(value), 8n)
|
|
304
|
-
let yv = WasmI32.load(WasmI32.fromGrain(amount), 8n)
|
|
305
|
-
let ptr = newInt32(WasmI32.shrS(xv, yv))
|
|
306
|
-
WasmI32.toGrain(ptr): Int32
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Shifts the bits of the value right by the given number of bits.
|
|
311
|
-
*
|
|
312
|
-
* @param value: The value to shift
|
|
313
|
-
* @param amount: The amount to shift by
|
|
314
|
-
* @returns The shifted value
|
|
315
|
-
*
|
|
316
|
-
* @since v0.2.0
|
|
280
|
+
* @since v0.6.0
|
|
281
|
+
* @history v0.2.0: Originally named `shr`
|
|
317
282
|
*/
|
|
318
283
|
@unsafe
|
|
319
|
-
|
|
320
|
-
let xv = WasmI32.load(WasmI32.fromGrain(value),
|
|
321
|
-
let yv = WasmI32.load(WasmI32.fromGrain(amount),
|
|
322
|
-
let ptr = newInt32(
|
|
284
|
+
provide let (>>) = (value: Int32, amount: Int32) => {
|
|
285
|
+
let xv = WasmI32.load(WasmI32.fromGrain(value), _VALUE_OFFSET)
|
|
286
|
+
let yv = WasmI32.load(WasmI32.fromGrain(amount), _VALUE_OFFSET)
|
|
287
|
+
let ptr = newInt32(xv >> yv)
|
|
323
288
|
WasmI32.toGrain(ptr): Int32
|
|
324
289
|
}
|
|
325
290
|
|
|
326
|
-
/**
|
|
327
|
-
* @section Comparisons: Functions for comparing Int32 values.
|
|
328
|
-
*/
|
|
329
|
-
|
|
330
291
|
/**
|
|
331
292
|
* Checks if the first value is equal to the second value.
|
|
332
293
|
*
|
|
@@ -334,13 +295,14 @@ export let shrU = (value: Int32, amount: Int32) => {
|
|
|
334
295
|
* @param y: The second value
|
|
335
296
|
* @returns `true` if the first value is equal to the second value or `false` otherwise
|
|
336
297
|
*
|
|
337
|
-
* @since v0.
|
|
298
|
+
* @since v0.6.0
|
|
299
|
+
* @history v0.4.0: Originally named `eq`
|
|
338
300
|
*/
|
|
339
301
|
@unsafe
|
|
340
|
-
|
|
341
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
342
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
343
|
-
|
|
302
|
+
provide let (==) = (x: Int32, y: Int32) => {
|
|
303
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
304
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
305
|
+
xv == yv
|
|
344
306
|
}
|
|
345
307
|
|
|
346
308
|
/**
|
|
@@ -350,13 +312,14 @@ export let eq = (x: Int32, y: Int32) => {
|
|
|
350
312
|
* @param y: The second value
|
|
351
313
|
* @returns `true` if the first value is not equal to the second value or `false` otherwise
|
|
352
314
|
*
|
|
353
|
-
* @since v0.
|
|
315
|
+
* @since v0.6.0
|
|
316
|
+
* @history v0.4.0: Originally named `ne`
|
|
354
317
|
*/
|
|
355
318
|
@unsafe
|
|
356
|
-
|
|
357
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
358
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
359
|
-
|
|
319
|
+
provide let (!=) = (x: Int32, y: Int32) => {
|
|
320
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
321
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
322
|
+
xv != yv
|
|
360
323
|
}
|
|
361
324
|
|
|
362
325
|
/**
|
|
@@ -368,8 +331,8 @@ export let ne = (x: Int32, y: Int32) => {
|
|
|
368
331
|
* @since v0.4.0
|
|
369
332
|
*/
|
|
370
333
|
@unsafe
|
|
371
|
-
|
|
372
|
-
let xv = WasmI32.load(WasmI32.fromGrain(value),
|
|
334
|
+
provide let eqz = (value: Int32) => {
|
|
335
|
+
let xv = WasmI32.load(WasmI32.fromGrain(value), _VALUE_OFFSET)
|
|
373
336
|
WasmI32.eqz(xv)
|
|
374
337
|
}
|
|
375
338
|
|
|
@@ -380,29 +343,14 @@ export let eqz = (value: Int32) => {
|
|
|
380
343
|
* @param y: The second value
|
|
381
344
|
* @returns `true` if the first value is less than the second value or `false` otherwise
|
|
382
345
|
*
|
|
383
|
-
* @since v0.
|
|
346
|
+
* @since v0.6.0
|
|
347
|
+
* @history v0.2.0: Originally named `lt`
|
|
384
348
|
*/
|
|
385
349
|
@unsafe
|
|
386
|
-
|
|
387
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
388
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
389
|
-
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* Checks if the first unsigned value is less than the second unsigned value.
|
|
394
|
-
*
|
|
395
|
-
* @param x: The first value
|
|
396
|
-
* @param y: The second value
|
|
397
|
-
* @returns `true` if the first value is less than the second value or `false` otherwise
|
|
398
|
-
*
|
|
399
|
-
* @since v0.5.0
|
|
400
|
-
*/
|
|
401
|
-
@unsafe
|
|
402
|
-
export let ltU = (x: Int32, y: Int32) => {
|
|
403
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
|
|
404
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
|
|
405
|
-
WasmI32.ltU(xv, yv)
|
|
350
|
+
provide let (<) = (x: Int32, y: Int32) => {
|
|
351
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
352
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
353
|
+
xv < yv
|
|
406
354
|
}
|
|
407
355
|
|
|
408
356
|
/**
|
|
@@ -412,29 +360,14 @@ export let ltU = (x: Int32, y: Int32) => {
|
|
|
412
360
|
* @param y: The second value
|
|
413
361
|
* @returns `true` if the first value is greater than the second value or `false` otherwise
|
|
414
362
|
*
|
|
415
|
-
* @since v0.
|
|
363
|
+
* @since v0.6.0
|
|
364
|
+
* @history v0.2.0: Originally named `gt`
|
|
416
365
|
*/
|
|
417
366
|
@unsafe
|
|
418
|
-
|
|
419
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
420
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
421
|
-
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* Checks if the first unsigned value is greater than the second unsigned value.
|
|
426
|
-
*
|
|
427
|
-
* @param x: The first value
|
|
428
|
-
* @param y: The second value
|
|
429
|
-
* @returns `true` if the first value is greater than the second value or `false` otherwise
|
|
430
|
-
*
|
|
431
|
-
* @since v0.5.0
|
|
432
|
-
*/
|
|
433
|
-
@unsafe
|
|
434
|
-
export let gtU = (x: Int32, y: Int32) => {
|
|
435
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
|
|
436
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
|
|
437
|
-
WasmI32.gtU(xv, yv)
|
|
367
|
+
provide let (>) = (x: Int32, y: Int32) => {
|
|
368
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
369
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
370
|
+
xv > yv
|
|
438
371
|
}
|
|
439
372
|
|
|
440
373
|
/**
|
|
@@ -444,29 +377,14 @@ export let gtU = (x: Int32, y: Int32) => {
|
|
|
444
377
|
* @param y: The second value
|
|
445
378
|
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
|
|
446
379
|
*
|
|
447
|
-
* @since v0.
|
|
380
|
+
* @since v0.6.0
|
|
381
|
+
* @history v0.2.0: Originally named `lte`
|
|
448
382
|
*/
|
|
449
383
|
@unsafe
|
|
450
|
-
|
|
451
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
452
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
453
|
-
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
/**
|
|
457
|
-
* Checks if the first unsigned value is less than or equal to the second unsigned value.
|
|
458
|
-
*
|
|
459
|
-
* @param x: The first value
|
|
460
|
-
* @param y: The second value
|
|
461
|
-
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
|
|
462
|
-
*
|
|
463
|
-
* @since v0.5.0
|
|
464
|
-
*/
|
|
465
|
-
@unsafe
|
|
466
|
-
export let lteU = (x: Int32, y: Int32) => {
|
|
467
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
|
|
468
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
|
|
469
|
-
WasmI32.leU(xv, yv)
|
|
384
|
+
provide let (<=) = (x: Int32, y: Int32) => {
|
|
385
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
386
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
387
|
+
xv <= yv
|
|
470
388
|
}
|
|
471
389
|
|
|
472
390
|
/**
|
|
@@ -476,35 +394,16 @@ export let lteU = (x: Int32, y: Int32) => {
|
|
|
476
394
|
* @param y: The second value
|
|
477
395
|
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
|
|
478
396
|
*
|
|
479
|
-
* @since v0.
|
|
480
|
-
|
|
481
|
-
@unsafe
|
|
482
|
-
export let gte = (x: Int32, y: Int32) => {
|
|
483
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
|
|
484
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
|
|
485
|
-
WasmI32.geS(xv, yv)
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
/**
|
|
489
|
-
* Checks if the first unsigned value is greater than or equal to the second unsigned value.
|
|
490
|
-
*
|
|
491
|
-
* @param x: The first value
|
|
492
|
-
* @param y: The second value
|
|
493
|
-
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
|
|
494
|
-
*
|
|
495
|
-
* @since v0.5.0
|
|
397
|
+
* @since v0.6.0
|
|
398
|
+
* @history v0.2.0: Originally named `gte`
|
|
496
399
|
*/
|
|
497
400
|
@unsafe
|
|
498
|
-
|
|
499
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
500
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
501
|
-
|
|
401
|
+
provide let (>=) = (x: Int32, y: Int32) => {
|
|
402
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
403
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
404
|
+
xv >= yv
|
|
502
405
|
}
|
|
503
406
|
|
|
504
|
-
/**
|
|
505
|
-
* @section Bitwise logic: Boolean operations on the bits of Int32 values.
|
|
506
|
-
*/
|
|
507
|
-
|
|
508
407
|
/**
|
|
509
408
|
* Computes the bitwise NOT of the given value.
|
|
510
409
|
*
|
|
@@ -514,9 +413,9 @@ export let gteU = (x: Int32, y: Int32) => {
|
|
|
514
413
|
* @since v0.2.0
|
|
515
414
|
*/
|
|
516
415
|
@unsafe
|
|
517
|
-
|
|
518
|
-
let xv = WasmI32.load(WasmI32.fromGrain(value),
|
|
519
|
-
let ptr = newInt32(
|
|
416
|
+
provide let lnot = (value: Int32) => {
|
|
417
|
+
let xv = WasmI32.load(WasmI32.fromGrain(value), _VALUE_OFFSET)
|
|
418
|
+
let ptr = newInt32(xv ^ 0xffffffffn)
|
|
520
419
|
WasmI32.toGrain(ptr): Int32
|
|
521
420
|
}
|
|
522
421
|
|
|
@@ -527,13 +426,14 @@ export let lnot = (value: Int32) => {
|
|
|
527
426
|
* @param y: The second operand
|
|
528
427
|
* @returns Containing a `1` in each bit position for which the corresponding bits of both operands are `1`
|
|
529
428
|
*
|
|
530
|
-
* @since v0.
|
|
429
|
+
* @since v0.6.0
|
|
430
|
+
* @history v0.2.0: Originally named `land`
|
|
531
431
|
*/
|
|
532
432
|
@unsafe
|
|
533
|
-
|
|
534
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
535
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
536
|
-
let ptr = newInt32(
|
|
433
|
+
provide let (&) = (x: Int32, y: Int32) => {
|
|
434
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
435
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
436
|
+
let ptr = newInt32(xv & yv)
|
|
537
437
|
WasmI32.toGrain(ptr): Int32
|
|
538
438
|
}
|
|
539
439
|
|
|
@@ -544,13 +444,14 @@ export let land = (x: Int32, y: Int32) => {
|
|
|
544
444
|
* @param y: The second operand
|
|
545
445
|
* @returns Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`
|
|
546
446
|
*
|
|
547
|
-
* @since v0.
|
|
447
|
+
* @since v0.6.0
|
|
448
|
+
* @history v0.2.0: Originally named `lor`
|
|
548
449
|
*/
|
|
549
450
|
@unsafe
|
|
550
|
-
|
|
551
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
552
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
553
|
-
let ptr = newInt32(
|
|
451
|
+
provide let (|) = (x: Int32, y: Int32) => {
|
|
452
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
453
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
454
|
+
let ptr = newInt32(xv | yv)
|
|
554
455
|
WasmI32.toGrain(ptr): Int32
|
|
555
456
|
}
|
|
556
457
|
|
|
@@ -561,13 +462,14 @@ export let lor = (x: Int32, y: Int32) => {
|
|
|
561
462
|
* @param y: The second operand
|
|
562
463
|
* @returns Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`
|
|
563
464
|
*
|
|
564
|
-
* @since v0.
|
|
465
|
+
* @since v0.6.0
|
|
466
|
+
* @history v0.2.0: Originally named `lxor`
|
|
565
467
|
*/
|
|
566
468
|
@unsafe
|
|
567
|
-
|
|
568
|
-
let xv = WasmI32.load(WasmI32.fromGrain(x),
|
|
569
|
-
let yv = WasmI32.load(WasmI32.fromGrain(y),
|
|
570
|
-
let ptr = newInt32(
|
|
469
|
+
provide let (^) = (x: Int32, y: Int32) => {
|
|
470
|
+
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
|
|
471
|
+
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
|
|
472
|
+
let ptr = newInt32(xv ^ yv)
|
|
571
473
|
WasmI32.toGrain(ptr): Int32
|
|
572
474
|
}
|
|
573
475
|
|
|
@@ -580,8 +482,8 @@ export let lxor = (x: Int32, y: Int32) => {
|
|
|
580
482
|
* @since v0.4.0
|
|
581
483
|
*/
|
|
582
484
|
@unsafe
|
|
583
|
-
|
|
584
|
-
let nv = WasmI32.load(WasmI32.fromGrain(value),
|
|
485
|
+
provide let clz = (value: Int32) => {
|
|
486
|
+
let nv = WasmI32.load(WasmI32.fromGrain(value), _VALUE_OFFSET)
|
|
585
487
|
let ptr = newInt32(WasmI32.clz(nv))
|
|
586
488
|
WasmI32.toGrain(ptr): Int32
|
|
587
489
|
}
|
|
@@ -595,8 +497,8 @@ export let clz = (value: Int32) => {
|
|
|
595
497
|
* @since v0.4.0
|
|
596
498
|
*/
|
|
597
499
|
@unsafe
|
|
598
|
-
|
|
599
|
-
let nv = WasmI32.load(WasmI32.fromGrain(value),
|
|
500
|
+
provide let ctz = (value: Int32) => {
|
|
501
|
+
let nv = WasmI32.load(WasmI32.fromGrain(value), _VALUE_OFFSET)
|
|
600
502
|
let ptr = newInt32(WasmI32.ctz(nv))
|
|
601
503
|
WasmI32.toGrain(ptr): Int32
|
|
602
504
|
}
|
|
@@ -610,8 +512,37 @@ export let ctz = (value: Int32) => {
|
|
|
610
512
|
* @since v0.4.0
|
|
611
513
|
*/
|
|
612
514
|
@unsafe
|
|
613
|
-
|
|
614
|
-
let nv = WasmI32.load(WasmI32.fromGrain(value),
|
|
515
|
+
provide let popcnt = (value: Int32) => {
|
|
516
|
+
let nv = WasmI32.load(WasmI32.fromGrain(value), _VALUE_OFFSET)
|
|
615
517
|
let ptr = newInt32(WasmI32.popcnt(nv))
|
|
616
518
|
WasmI32.toGrain(ptr): Int32
|
|
617
519
|
}
|
|
520
|
+
|
|
521
|
+
// Exponentiation by squaring https://en.wikipedia.org/wiki/Exponentiation_by_squaring special path for int^int
|
|
522
|
+
let rec expBySquaring = (y, x, n) => {
|
|
523
|
+
if (n == 0l) {
|
|
524
|
+
1l
|
|
525
|
+
} else if (n == 1l) {
|
|
526
|
+
x * y
|
|
527
|
+
} else if (n % 2l == 0l) {
|
|
528
|
+
expBySquaring(y, x * x, n / 2l)
|
|
529
|
+
} else {
|
|
530
|
+
expBySquaring(x * y, x * x, (n - 1l) / 2l)
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Computes the exponentiation of the given base and power.
|
|
536
|
+
*
|
|
537
|
+
* @param base: The base number
|
|
538
|
+
* @param power: The exponent number
|
|
539
|
+
* @returns The base raised to the given power
|
|
540
|
+
*
|
|
541
|
+
* @since v0.6.0
|
|
542
|
+
*/
|
|
543
|
+
provide let (**) = (base, power) => {
|
|
544
|
+
if (power < 0l)
|
|
545
|
+
return expBySquaring(1l, 1l / base, power * -1l)
|
|
546
|
+
else
|
|
547
|
+
return expBySquaring(1l, base, power)
|
|
548
|
+
}
|