Skip to content

Commit 8a69dd3

Browse files
authored
feat(stdlib)!: Replace Int32 arithmatic functions with operators (#1936)
1 parent a4016f1 commit 8a69dd3

File tree

3 files changed

+287
-153
lines changed

3 files changed

+287
-153
lines changed

compiler/test/stdlib/int32.test.gr

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Int32Test
33
include "int32"
44
from Int32 use *
55

6+
from Pervasives use { (==) }
67
// Suppress warnings about using `fromNumber` on constants, since that's what we want to test.
78
let fromNumber = fromNumber
89
assert fromNumber(5) == 5l
@@ -16,50 +17,53 @@ assert toNumber(0l) == 0
1617
assert fromUint32(1ul) == 1l
1718
assert fromUint32(0xfffffffful) == -1l
1819

20+
from Int32 use { (==) }
21+
1922
assert lnot(0xffffffffl) == 0l
2023
assert lnot(0l) == 0xffffffffl
2124
assert lnot(0xf0f0f0f0l) == 0x0f0f0f0fl
2225

23-
assert land(0b1010l, 0b10l) == 0b10l
24-
assert land(0b1010l, 0l) == 0l
26+
assert (0b1010l & 0b10l) == 0b10l
27+
assert (0b1010l & 0l) == 0l
2528

26-
assert lor(0b1010l, 0b0101l) == 0b1111l
27-
assert lor(0b1010l, 0l) == 0b1010l
29+
assert (0b1010l | 0b0101l) == 0b1111l
30+
assert (0b1010l | 0l) == 0b1010l
2831

29-
assert lxor(0b1010l, 0b1101l) == 0b0111l
30-
assert lxor(0b1010l, 0l) == 0b1010l
32+
assert (0b1010l ^ 0b1101l) == 0b0111l
33+
assert (0b1010l ^ 0l) == 0b1010l
3134

32-
assert shl(-1l, 1l) == -2l
33-
assert shl(-1l, 2l) == -4l
34-
assert shl(-1l, 3l) == -8l
35-
assert shl(-2l, 63l) == 0l
36-
assert shl(24l, 1l) == 48l
35+
assert -1l << 1l == -2l
36+
assert -1l << 2l == -4l
37+
assert -1l << 3l == -8l
38+
assert -2l << 63l == 0l
39+
assert 24l << 1l == 48l
3740

38-
assert shr(-1l, 63l) == -1l
39-
assert shr(-24l, 1l) == -12l
41+
assert -1l >> 63l == -1l
42+
assert -24l >> 1l == -12l
4043

41-
assert gt(5l, 4l)
42-
assert gte(5l, 5l)
43-
assert lt(5l, 17l)
44-
assert lte(5l, 5l)
45-
assert !gt(5l, 5l)
46-
assert !gte(5l, 22l)
47-
assert !lt(5l, -17l)
48-
assert !lte(5l, 4l)
44+
assert 5l > 4l
45+
assert 5l >= 5l
46+
assert 5l < 17l
47+
assert 5l <= 5l
48+
assert !(5l > 5l)
49+
assert !(5l >= 22l)
50+
assert !(5l < -17l)
51+
assert !(5l <= 4l)
4952

5053
assert clz(0b11l) == 30l
5154
assert ctz(0b11000l) == 3l
5255
assert popcnt(0b1100110011l) == 6l
5356
assert rotl(0b11l, 3l) == 0b11000l
5457
assert rotr(0b110000l, 3l) == 0b110l
5558

56-
assert eq(5l, 5l)
57-
assert !eq(5l, 55l)
58-
assert ne(5l, 55l)
59-
assert !ne(5l, 5l)
59+
assert 5l == 5l
60+
assert !(5l == 55l)
61+
assert 5l != 55l
62+
assert !(5l != 5l)
6063
assert eqz(0l)
6164
assert !eqz(-42l)
6265

66+
from Pervasives use { (==) }
6367
// Regression #1339
6468
let arr = [> 1, 2, 3]
6569
assert arr[toNumber(1l)] == 2

stdlib/int32.gr

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ provide let decr = (value: Int32) => {
9595
* @param y: The second operand
9696
* @returns The sum of the two operands
9797
*
98-
* @since v0.2.0
98+
* @since v0.6.0
99+
* @history v0.2.0: Originally named `add`
99100
*/
100101
@unsafe
101-
provide let add = (x: Int32, y: Int32) => {
102+
provide let (+) = (x: Int32, y: Int32) => {
102103
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
103104
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
104105
let ptr = newInt32(xv + yv)
@@ -112,10 +113,11 @@ provide let add = (x: Int32, y: Int32) => {
112113
* @param y: The second operand
113114
* @returns The difference of the two operands
114115
*
115-
* @since v0.2.0
116+
* @since v0.6.0
117+
* @history v0.2.0: Originally named `sub`
116118
*/
117119
@unsafe
118-
provide let sub = (x: Int32, y: Int32) => {
120+
provide let (-) = (x: Int32, y: Int32) => {
119121
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
120122
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
121123
let ptr = newInt32(xv - yv)
@@ -129,10 +131,11 @@ provide let sub = (x: Int32, y: Int32) => {
129131
* @param y: The second operand
130132
* @returns The product of the two operands
131133
*
132-
* @since v0.2.0
134+
* @since v0.6.0
135+
* @history v0.2.0: Originally named `mul`
133136
*/
134137
@unsafe
135-
provide let mul = (x: Int32, y: Int32) => {
138+
provide let (*) = (x: Int32, y: Int32) => {
136139
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
137140
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
138141
let ptr = newInt32(xv * yv)
@@ -146,10 +149,11 @@ provide let mul = (x: Int32, y: Int32) => {
146149
* @param y: The second operand
147150
* @returns The quotient of its operands
148151
*
149-
* @since v0.2.0
152+
* @since v0.6.0
153+
* @history v0.2.0: Originally named `div`
150154
*/
151155
@unsafe
152-
provide let div = (x: Int32, y: Int32) => {
156+
provide let (/) = (x: Int32, y: Int32) => {
153157
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
154158
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
155159
let ptr = newInt32(xv / yv)
@@ -175,6 +179,7 @@ provide let rem = (x: Int32, y: Int32) => {
175179

176180
@unsafe
177181
let abs = n => {
182+
from WasmI32 use { (-) }
178183
let mask = n >> 31n
179184
(n ^ mask) - mask
180185
}
@@ -189,10 +194,12 @@ let abs = n => {
189194
*
190195
* @throws ModuloByZero: When `y` is zero
191196
*
192-
* @since v0.2.0
197+
* @since v0.6.0
198+
* @history v0.2.0: Originally named `mod`
193199
*/
194200
@unsafe
195-
provide let mod = (x: Int32, y: Int32) => {
201+
provide let (%) = (x: Int32, y: Int32) => {
202+
from WasmI32 use { (-) }
196203
let xval = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
197204
let yval = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
198205

@@ -253,10 +260,11 @@ provide let rotr = (value: Int32, amount: Int32) => {
253260
* @param amount: The number of bits to shift by
254261
* @returns The shifted value
255262
*
256-
* @since v0.2.0
263+
* @since v0.6.0
264+
* @history v0.2.0: Originally named `shl`
257265
*/
258266
@unsafe
259-
provide let shl = (value: Int32, amount: Int32) => {
267+
provide let (<<) = (value: Int32, amount: Int32) => {
260268
let xv = WasmI32.load(WasmI32.fromGrain(value), _VALUE_OFFSET)
261269
let yv = WasmI32.load(WasmI32.fromGrain(amount), _VALUE_OFFSET)
262270
let ptr = newInt32(xv << yv)
@@ -270,10 +278,11 @@ provide let shl = (value: Int32, amount: Int32) => {
270278
* @param amount: The amount to shift by
271279
* @returns The shifted value
272280
*
273-
* @since v0.2.0
281+
* @since v0.6.0
282+
* @history v0.2.0: Originally named `shr`
274283
*/
275284
@unsafe
276-
provide let shr = (value: Int32, amount: Int32) => {
285+
provide let (>>) = (value: Int32, amount: Int32) => {
277286
let xv = WasmI32.load(WasmI32.fromGrain(value), _VALUE_OFFSET)
278287
let yv = WasmI32.load(WasmI32.fromGrain(amount), _VALUE_OFFSET)
279288
let ptr = newInt32(xv >> yv)
@@ -287,10 +296,11 @@ provide let shr = (value: Int32, amount: Int32) => {
287296
* @param y: The second value
288297
* @returns `true` if the first value is equal to the second value or `false` otherwise
289298
*
290-
* @since v0.4.0
299+
* @since v0.6.0
300+
* @history v0.4.0: Originally named `eq`
291301
*/
292302
@unsafe
293-
provide let eq = (x: Int32, y: Int32) => {
303+
provide let (==) = (x: Int32, y: Int32) => {
294304
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
295305
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
296306
xv == yv
@@ -303,10 +313,11 @@ provide let eq = (x: Int32, y: Int32) => {
303313
* @param y: The second value
304314
* @returns `true` if the first value is not equal to the second value or `false` otherwise
305315
*
306-
* @since v0.4.0
316+
* @since v0.6.0
317+
* @history v0.4.0: Originally named `ne`
307318
*/
308319
@unsafe
309-
provide let ne = (x: Int32, y: Int32) => {
320+
provide let (!=) = (x: Int32, y: Int32) => {
310321
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
311322
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
312323
xv != yv
@@ -333,10 +344,11 @@ provide let eqz = (value: Int32) => {
333344
* @param y: The second value
334345
* @returns `true` if the first value is less than the second value or `false` otherwise
335346
*
336-
* @since v0.2.0
347+
* @since v0.6.0
348+
* @history v0.2.0: Originally named `lt`
337349
*/
338350
@unsafe
339-
provide let lt = (x: Int32, y: Int32) => {
351+
provide let (<) = (x: Int32, y: Int32) => {
340352
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
341353
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
342354
xv < yv
@@ -349,10 +361,11 @@ provide let lt = (x: Int32, y: Int32) => {
349361
* @param y: The second value
350362
* @returns `true` if the first value is greater than the second value or `false` otherwise
351363
*
352-
* @since v0.2.0
364+
* @since v0.6.0
365+
* @history v0.2.0: Originally named `gt`
353366
*/
354367
@unsafe
355-
provide let gt = (x: Int32, y: Int32) => {
368+
provide let (>) = (x: Int32, y: Int32) => {
356369
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
357370
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
358371
xv > yv
@@ -365,10 +378,11 @@ provide let gt = (x: Int32, y: Int32) => {
365378
* @param y: The second value
366379
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
367380
*
368-
* @since v0.2.0
381+
* @since v0.6.0
382+
* @history v0.2.0: Originally named `lte`
369383
*/
370384
@unsafe
371-
provide let lte = (x: Int32, y: Int32) => {
385+
provide let (<=) = (x: Int32, y: Int32) => {
372386
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
373387
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
374388
xv <= yv
@@ -381,10 +395,11 @@ provide let lte = (x: Int32, y: Int32) => {
381395
* @param y: The second value
382396
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
383397
*
384-
* @since v0.2.0
398+
* @since v0.6.0
399+
* @history v0.2.0: Originally named `gte`
385400
*/
386401
@unsafe
387-
provide let gte = (x: Int32, y: Int32) => {
402+
provide let (>=) = (x: Int32, y: Int32) => {
388403
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
389404
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
390405
xv >= yv
@@ -412,10 +427,11 @@ provide let lnot = (value: Int32) => {
412427
* @param y: The second operand
413428
* @returns Containing a `1` in each bit position for which the corresponding bits of both operands are `1`
414429
*
415-
* @since v0.2.0
430+
* @since v0.6.0
431+
* @history v0.2.0: Originally named `land`
416432
*/
417433
@unsafe
418-
provide let land = (x: Int32, y: Int32) => {
434+
provide let (&) = (x: Int32, y: Int32) => {
419435
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
420436
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
421437
let ptr = newInt32(xv & yv)
@@ -429,10 +445,11 @@ provide let land = (x: Int32, y: Int32) => {
429445
* @param y: The second operand
430446
* @returns Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`
431447
*
432-
* @since v0.2.0
448+
* @since v0.6.0
449+
* @history v0.2.0: Originally named `lor`
433450
*/
434451
@unsafe
435-
provide let lor = (x: Int32, y: Int32) => {
452+
provide let (|) = (x: Int32, y: Int32) => {
436453
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
437454
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
438455
let ptr = newInt32(xv | yv)
@@ -446,10 +463,11 @@ provide let lor = (x: Int32, y: Int32) => {
446463
* @param y: The second operand
447464
* @returns Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`
448465
*
449-
* @since v0.2.0
466+
* @since v0.6.0
467+
* @history v0.2.0: Originally named `lxor`
450468
*/
451469
@unsafe
452-
provide let lxor = (x: Int32, y: Int32) => {
470+
provide let (^) = (x: Int32, y: Int32) => {
453471
let xv = WasmI32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
454472
let yv = WasmI32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
455473
let ptr = newInt32(xv ^ yv)

0 commit comments

Comments
 (0)