@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/int8.gr
ADDED
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for working with the Int8 type.
|
|
3
|
+
* @example from "int8" include Int8
|
|
4
|
+
*
|
|
5
|
+
* @example 1s
|
|
6
|
+
* @example -1s
|
|
7
|
+
*
|
|
8
|
+
* @since v0.6.0
|
|
9
|
+
*/
|
|
10
|
+
module Int8
|
|
11
|
+
|
|
12
|
+
from "runtime/unsafe/wasmi32" include WasmI32
|
|
13
|
+
use WasmI32.{
|
|
14
|
+
(+),
|
|
15
|
+
(-),
|
|
16
|
+
(*),
|
|
17
|
+
(/),
|
|
18
|
+
(&),
|
|
19
|
+
(|),
|
|
20
|
+
(^),
|
|
21
|
+
(<<),
|
|
22
|
+
(>>),
|
|
23
|
+
(>>),
|
|
24
|
+
(==),
|
|
25
|
+
(!=),
|
|
26
|
+
(<),
|
|
27
|
+
(<=),
|
|
28
|
+
(>),
|
|
29
|
+
(>=),
|
|
30
|
+
}
|
|
31
|
+
from "runtime/exception" include Exception
|
|
32
|
+
from "runtime/numbers" include Numbers
|
|
33
|
+
use Numbers.{ coerceNumberToInt8 as fromNumber, coerceInt8ToNumber as toNumber }
|
|
34
|
+
from "runtime/dataStructures" include DataStructures
|
|
35
|
+
use DataStructures.{ tagInt8, untagInt8 }
|
|
36
|
+
|
|
37
|
+
@unsafe
|
|
38
|
+
let _TAG_BYTE = 0b1010n
|
|
39
|
+
@unsafe
|
|
40
|
+
let _DATA_MASK = 0xffffff00n
|
|
41
|
+
|
|
42
|
+
provide { fromNumber, toNumber }
|
|
43
|
+
|
|
44
|
+
// Trick: can simply use WasmI32.extendS16 to sign-extend 8-bit ints rather
|
|
45
|
+
// than having to shift right, extendS8, and retag
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Converts a Uint8 to an Int8.
|
|
49
|
+
*
|
|
50
|
+
* @param number: The value to convert
|
|
51
|
+
* @returns The Uint8 represented as an Int8
|
|
52
|
+
*
|
|
53
|
+
* @example Int8.fromUint8(1us) == 1s
|
|
54
|
+
*
|
|
55
|
+
* @since v0.6.0
|
|
56
|
+
*/
|
|
57
|
+
@unsafe
|
|
58
|
+
provide let fromUint8 = (number: Uint8) => {
|
|
59
|
+
let x = WasmI32.fromGrain(number)
|
|
60
|
+
// Trick: convert from Uint8 tag 11010 to Int8 tag 1010
|
|
61
|
+
let result = x ^ 0b10000n
|
|
62
|
+
WasmI32.toGrain(WasmI32.extendS16(result)): Int8
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Increments the value by one.
|
|
67
|
+
*
|
|
68
|
+
* @param value: The value to increment
|
|
69
|
+
* @returns The incremented value
|
|
70
|
+
*
|
|
71
|
+
* @example Int8.incr(1s) == 2s
|
|
72
|
+
* @example Int8.incr(-2s) == -1s
|
|
73
|
+
*
|
|
74
|
+
* @since v0.6.0
|
|
75
|
+
*/
|
|
76
|
+
@unsafe
|
|
77
|
+
provide let incr = (value: Int8) => {
|
|
78
|
+
let value = WasmI32.fromGrain(value)
|
|
79
|
+
// Trick: since the data is at offset 8, can just add 1 << 8 == 0x100
|
|
80
|
+
let result = value + 0x100n
|
|
81
|
+
WasmI32.toGrain(WasmI32.extendS16(result)): Int8
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Decrements the value by one.
|
|
86
|
+
*
|
|
87
|
+
* @param value: The value to decrement
|
|
88
|
+
* @returns The decremented value
|
|
89
|
+
*
|
|
90
|
+
* @example Int8.decr(2s) == 1s
|
|
91
|
+
* @example Int8.decr(0s) == -1s
|
|
92
|
+
*
|
|
93
|
+
* @since v0.6.0
|
|
94
|
+
*/
|
|
95
|
+
@unsafe
|
|
96
|
+
provide let decr = (value: Int8) => {
|
|
97
|
+
let value = WasmI32.fromGrain(value)
|
|
98
|
+
// Trick: since the data is at offset 8, can just subtract 1 << 8 == 0x100
|
|
99
|
+
let result = value - 0x100n
|
|
100
|
+
WasmI32.toGrain(WasmI32.extendS16(result)): Int8
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Computes the sum of its operands.
|
|
105
|
+
*
|
|
106
|
+
* @param x: The first operand
|
|
107
|
+
* @param y: The second operand
|
|
108
|
+
* @returns The sum of the two operands
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* use Int8.{ (+) }
|
|
112
|
+
* assert 1s + 1s == 2s
|
|
113
|
+
*
|
|
114
|
+
* @since v0.6.0
|
|
115
|
+
*/
|
|
116
|
+
@unsafe
|
|
117
|
+
provide let (+) = (x: Int8, y: Int8) => {
|
|
118
|
+
// Trick: add the values as-is without shifting right 8; this will cause
|
|
119
|
+
// the data to be added correctly but the trailing tag bits will be corrupted:
|
|
120
|
+
// 1010 + 1010 = 10100; xor with 11110 to correct to 1010
|
|
121
|
+
let x = WasmI32.fromGrain(x)
|
|
122
|
+
let y = WasmI32.fromGrain(y)
|
|
123
|
+
let val = x + y
|
|
124
|
+
let tagged = val ^ 0b11110n
|
|
125
|
+
WasmI32.toGrain(WasmI32.extendS16(tagged)): Int8
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Computes the difference of its operands.
|
|
130
|
+
*
|
|
131
|
+
* @param x: The first operand
|
|
132
|
+
* @param y: The second operand
|
|
133
|
+
* @returns The difference of the two operands
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* use Int8.{ (-) }
|
|
137
|
+
* assert 2s - 1s == 1s
|
|
138
|
+
*
|
|
139
|
+
* @since v0.6.0
|
|
140
|
+
*/
|
|
141
|
+
@unsafe
|
|
142
|
+
provide let (-) = (x: Int8, y: Int8) => {
|
|
143
|
+
let x = WasmI32.fromGrain(x)
|
|
144
|
+
let y = WasmI32.fromGrain(y)
|
|
145
|
+
let val = x - y
|
|
146
|
+
let tagged = val | _TAG_BYTE
|
|
147
|
+
WasmI32.toGrain(WasmI32.extendS16(tagged)): Int8
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Computes the product of its operands.
|
|
152
|
+
*
|
|
153
|
+
* @param x: The first operand
|
|
154
|
+
* @param y: The second operand
|
|
155
|
+
* @returns The product of the two operands
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* use Int8.{ (*) }
|
|
159
|
+
* assert 2s * 2s == 4s
|
|
160
|
+
*
|
|
161
|
+
* @since v0.6.0
|
|
162
|
+
*/
|
|
163
|
+
@unsafe
|
|
164
|
+
provide let (*) = (x: Int8, y: Int8) => {
|
|
165
|
+
let x = untagInt8(x)
|
|
166
|
+
let y = untagInt8(y)
|
|
167
|
+
let val = WasmI32.extendS8(x * y)
|
|
168
|
+
tagInt8(val)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Computes the quotient of its operands using signed division.
|
|
173
|
+
*
|
|
174
|
+
* @param x: The first operand
|
|
175
|
+
* @param y: The second operand
|
|
176
|
+
* @returns The quotient of its operands
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* use Int8.{ (/) }
|
|
180
|
+
* assert 8s / 2s == 4s
|
|
181
|
+
*
|
|
182
|
+
* @since v0.6.0
|
|
183
|
+
*/
|
|
184
|
+
@unsafe
|
|
185
|
+
provide let (/) = (x: Int8, y: Int8) => {
|
|
186
|
+
let x = untagInt8(x)
|
|
187
|
+
let y = untagInt8(y)
|
|
188
|
+
// No need to re-sign-extend
|
|
189
|
+
let val = x / y
|
|
190
|
+
tagInt8(val)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Computes the remainder of the division of its operands using signed division.
|
|
195
|
+
*
|
|
196
|
+
* @param x: The first operand
|
|
197
|
+
* @param y: The second operand
|
|
198
|
+
* @returns The remainder of its operands
|
|
199
|
+
*
|
|
200
|
+
* @example Int8.rem(8s, 3s) == 2s
|
|
201
|
+
*
|
|
202
|
+
* @since v0.6.0
|
|
203
|
+
*/
|
|
204
|
+
@unsafe
|
|
205
|
+
provide let rem = (x: Int8, y: Int8) => {
|
|
206
|
+
let x = untagInt8(x)
|
|
207
|
+
let y = untagInt8(y)
|
|
208
|
+
// No need to re-sign-extend
|
|
209
|
+
let val = WasmI32.remS(x, y)
|
|
210
|
+
tagInt8(val)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
@unsafe
|
|
214
|
+
let abs = n => {
|
|
215
|
+
use WasmI32.{ (-) }
|
|
216
|
+
let mask = n >> 31n
|
|
217
|
+
(n ^ mask) - mask
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Computes the remainder of the division of the first operand by the second.
|
|
222
|
+
* The result will have the sign of the second operand.
|
|
223
|
+
*
|
|
224
|
+
* @param x: The first operand
|
|
225
|
+
* @param y: The second operand
|
|
226
|
+
* @returns The modulus of its operands
|
|
227
|
+
*
|
|
228
|
+
* @throws ModuloByZero: When `y` is zero
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* use Int8.{ (%) }
|
|
232
|
+
* assert -5s % 3s == 1s
|
|
233
|
+
*
|
|
234
|
+
* @since v0.6.0
|
|
235
|
+
*/
|
|
236
|
+
@unsafe
|
|
237
|
+
provide let (%) = (x: Int8, y: Int8) => {
|
|
238
|
+
use WasmI32.{ (-) }
|
|
239
|
+
let xval = untagInt8(x)
|
|
240
|
+
let yval = untagInt8(y)
|
|
241
|
+
|
|
242
|
+
if (WasmI32.eqz(yval)) {
|
|
243
|
+
throw Exception.ModuloByZero
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
let val = if ((xval ^ yval) < 0n) {
|
|
247
|
+
let xabs = abs(xval)
|
|
248
|
+
let yabs = abs(yval)
|
|
249
|
+
let mval = WasmI32.remS(xabs, yabs)
|
|
250
|
+
let mres = yabs - mval
|
|
251
|
+
if (mval != 0n) (if (yval < 0n) 0n - mres else mres) else 0n
|
|
252
|
+
} else {
|
|
253
|
+
WasmI32.remS(xval, yval)
|
|
254
|
+
}
|
|
255
|
+
tagInt8(val)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Shifts the bits of the value left by the given number of bits.
|
|
260
|
+
*
|
|
261
|
+
* @param value: The value to shift
|
|
262
|
+
* @param amount: The number of bits to shift by
|
|
263
|
+
* @returns The shifted value
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* use Int8.{ (<<) }
|
|
267
|
+
* assert (5s << 1s) == 10s
|
|
268
|
+
*
|
|
269
|
+
* @since v0.6.0
|
|
270
|
+
*/
|
|
271
|
+
@unsafe
|
|
272
|
+
provide let (<<) = (value: Int8, amount: Int8) => {
|
|
273
|
+
// Trick: do not shift `value` right, just correct tag afterwards
|
|
274
|
+
let x = WasmI32.fromGrain(value) & _DATA_MASK
|
|
275
|
+
let y = untagInt8(amount)
|
|
276
|
+
let val = x << y
|
|
277
|
+
let tagged = val | _TAG_BYTE
|
|
278
|
+
WasmI32.toGrain(WasmI32.extendS16(tagged)): Int8
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Shifts the bits of the value right by the given number of bits, preserving the sign bit.
|
|
283
|
+
*
|
|
284
|
+
* @param value: The value to shift
|
|
285
|
+
* @param amount: The amount to shift by
|
|
286
|
+
* @returns The shifted value
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* use Int8.{ (>>) }
|
|
290
|
+
* assert (5s >> 1s) == 2s
|
|
291
|
+
*
|
|
292
|
+
* @since v0.6.0
|
|
293
|
+
*/
|
|
294
|
+
@unsafe
|
|
295
|
+
provide let (>>) = (value: Int8, amount: Int8) => {
|
|
296
|
+
// Trick: do not shift `value` right, just correct tag afterwards
|
|
297
|
+
let x = WasmI32.fromGrain(value)
|
|
298
|
+
let y = untagInt8(amount)
|
|
299
|
+
let val = x >> y
|
|
300
|
+
let tagged = val & _DATA_MASK | _TAG_BYTE
|
|
301
|
+
WasmI32.toGrain(tagged): Int8
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Checks if the first value is equal to the second value.
|
|
306
|
+
*
|
|
307
|
+
* @param x: The first value
|
|
308
|
+
* @param y: The second value
|
|
309
|
+
* @returns `true` if the first value is equal to the second value or `false` otherwise
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* use Int8.{ (==) }
|
|
313
|
+
* assert 1s == 1s
|
|
314
|
+
*
|
|
315
|
+
* @since v0.6.0
|
|
316
|
+
*/
|
|
317
|
+
@unsafe
|
|
318
|
+
provide let (==) = (x: Int8, y: Int8) => {
|
|
319
|
+
let x = WasmI32.fromGrain(x)
|
|
320
|
+
let y = WasmI32.fromGrain(y)
|
|
321
|
+
x == y
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Checks if the first value is not equal to the second value.
|
|
326
|
+
*
|
|
327
|
+
* @param x: The first value
|
|
328
|
+
* @param y: The second value
|
|
329
|
+
* @returns `true` if the first value is not equal to the second value or `false` otherwise
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* use Int8.{ (!=) }
|
|
333
|
+
* assert 1s != 2s
|
|
334
|
+
*
|
|
335
|
+
* @since v0.6.0
|
|
336
|
+
*/
|
|
337
|
+
@unsafe
|
|
338
|
+
provide let (!=) = (x: Int8, y: Int8) => {
|
|
339
|
+
let x = WasmI32.fromGrain(x)
|
|
340
|
+
let y = WasmI32.fromGrain(y)
|
|
341
|
+
x != y
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Checks if the first value is less than the second value.
|
|
346
|
+
*
|
|
347
|
+
* @param x: The first value
|
|
348
|
+
* @param y: The second value
|
|
349
|
+
* @returns `true` if the first value is less than the second value or `false` otherwise
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
* use Int8.{ (<) }
|
|
353
|
+
* assert 1s < 2s
|
|
354
|
+
*
|
|
355
|
+
* @since v0.6.0
|
|
356
|
+
*/
|
|
357
|
+
@unsafe
|
|
358
|
+
provide let (<) = (x: Int8, y: Int8) => {
|
|
359
|
+
let x = WasmI32.fromGrain(x)
|
|
360
|
+
let y = WasmI32.fromGrain(y)
|
|
361
|
+
x < y
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Checks if the first value is greater than the second value.
|
|
366
|
+
*
|
|
367
|
+
* @param x: The first value
|
|
368
|
+
* @param y: The second value
|
|
369
|
+
* @returns `true` if the first value is greater than the second value or `false` otherwise
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* use Int8.{ (>) }
|
|
373
|
+
* assert 2s > 1s
|
|
374
|
+
*
|
|
375
|
+
* @since v0.6.0
|
|
376
|
+
*/
|
|
377
|
+
@unsafe
|
|
378
|
+
provide let (>) = (x: Int8, y: Int8) => {
|
|
379
|
+
let x = WasmI32.fromGrain(x)
|
|
380
|
+
let y = WasmI32.fromGrain(y)
|
|
381
|
+
x > y
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Checks if the first value is less than or equal to the second value.
|
|
386
|
+
*
|
|
387
|
+
* @param x: The first value
|
|
388
|
+
* @param y: The second value
|
|
389
|
+
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* use Int8.{ (<=) }
|
|
393
|
+
* assert 1s <= 2s
|
|
394
|
+
* @example
|
|
395
|
+
* use Int8.{ (<=) }
|
|
396
|
+
* assert 1s <= 1s
|
|
397
|
+
*
|
|
398
|
+
* @since v0.6.0
|
|
399
|
+
*/
|
|
400
|
+
@unsafe
|
|
401
|
+
provide let (<=) = (x: Int8, y: Int8) => {
|
|
402
|
+
let x = WasmI32.fromGrain(x)
|
|
403
|
+
let y = WasmI32.fromGrain(y)
|
|
404
|
+
x <= y
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Checks if the first value is greater than or equal to 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 or equal to the second value or `false` otherwise
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* use Int8.{ (>=) }
|
|
416
|
+
* assert 2s >= 1s
|
|
417
|
+
* @example
|
|
418
|
+
* use Int8.{ (>=) }
|
|
419
|
+
* assert 1s >= 1s
|
|
420
|
+
*
|
|
421
|
+
* @since v0.6.0
|
|
422
|
+
*/
|
|
423
|
+
@unsafe
|
|
424
|
+
provide let (>=) = (x: Int8, y: Int8) => {
|
|
425
|
+
let x = WasmI32.fromGrain(x)
|
|
426
|
+
let y = WasmI32.fromGrain(y)
|
|
427
|
+
x >= y
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Computes the bitwise NOT of the given value.
|
|
432
|
+
*
|
|
433
|
+
* @param value: The given value
|
|
434
|
+
* @returns Containing the inverted bits of the given value
|
|
435
|
+
*
|
|
436
|
+
* @example Int.lnot(-5s) == 4s
|
|
437
|
+
*
|
|
438
|
+
* @since v0.6.0
|
|
439
|
+
*/
|
|
440
|
+
@unsafe
|
|
441
|
+
provide let lnot = (value: Int8) => {
|
|
442
|
+
let x = WasmI32.fromGrain(value)
|
|
443
|
+
WasmI32.toGrain(x ^ _DATA_MASK): Int8
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Computes the bitwise AND (`&`) on the given operands.
|
|
448
|
+
*
|
|
449
|
+
* @param x: The first operand
|
|
450
|
+
* @param y: The second operand
|
|
451
|
+
* @returns Containing a `1` in each bit position for which the corresponding bits of both operands are `1`
|
|
452
|
+
*
|
|
453
|
+
* @example
|
|
454
|
+
* use Int8.{ (&) }
|
|
455
|
+
* assert (3s & 4s) == 0s
|
|
456
|
+
*
|
|
457
|
+
* @since v0.6.0
|
|
458
|
+
*/
|
|
459
|
+
@unsafe
|
|
460
|
+
provide let (&) = (x: Int8, y: Int8) => {
|
|
461
|
+
// Tags getting `and`ed together is not a problem
|
|
462
|
+
let x = WasmI32.fromGrain(x)
|
|
463
|
+
let y = WasmI32.fromGrain(y)
|
|
464
|
+
let val = x & y
|
|
465
|
+
WasmI32.toGrain(val): Int8
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Computes the bitwise OR (`|`) on the given operands.
|
|
470
|
+
*
|
|
471
|
+
* @param x: The first operand
|
|
472
|
+
* @param y: The second operand
|
|
473
|
+
* @returns Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* use Int8.{ (|) }
|
|
477
|
+
* assert (3s | 4s) == 7s
|
|
478
|
+
*
|
|
479
|
+
* @since v0.6.0
|
|
480
|
+
*/
|
|
481
|
+
@unsafe
|
|
482
|
+
provide let (|) = (x: Int8, y: Int8) => {
|
|
483
|
+
// Tags getting `or`ed together is not a problem
|
|
484
|
+
let x = WasmI32.fromGrain(x)
|
|
485
|
+
let y = WasmI32.fromGrain(y)
|
|
486
|
+
let val = x | y
|
|
487
|
+
WasmI32.toGrain(val): Int8
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Computes the bitwise XOR (`^`) on the given operands.
|
|
492
|
+
*
|
|
493
|
+
* @param x: The first operand
|
|
494
|
+
* @param y: The second operand
|
|
495
|
+
* @returns Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`
|
|
496
|
+
*
|
|
497
|
+
* @example
|
|
498
|
+
* use Int8.{ (^) }
|
|
499
|
+
* assert (3s ^ 5s) == 6s
|
|
500
|
+
*
|
|
501
|
+
* @since v0.6.0
|
|
502
|
+
*/
|
|
503
|
+
@unsafe
|
|
504
|
+
provide let (^) = (x: Int8, y: Int8) => {
|
|
505
|
+
use WasmI32.{ (|) }
|
|
506
|
+
// Tags getting `xor`ed together will cancel each other out; add back tag with or
|
|
507
|
+
let x = WasmI32.fromGrain(x)
|
|
508
|
+
let y = WasmI32.fromGrain(y)
|
|
509
|
+
let val = x ^ y
|
|
510
|
+
WasmI32.toGrain(val | _TAG_BYTE): Int8
|
|
511
|
+
}
|