Skip to content

Commit 5f20868

Browse files
authored
feat(stdlib): Add unsigned versions of Int32/Int64 comparison operations (#831)
1 parent 0fb29c4 commit 5f20868

File tree

6 files changed

+356
-0
lines changed

6 files changed

+356
-0
lines changed

compiler/test/stdlib/int32.test.gr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ assert !gte(5l, 22l)
4242
assert !lt(5l, -17l)
4343
assert !lte(5l, 4l)
4444

45+
// unsigned comparisons:
46+
assert gtU(5l, 4l)
47+
assert gteU(5l, 5l)
48+
assert ltU(5l, 17l)
49+
assert lteU(5l, 5l)
50+
assert !gtU(5l, 5l)
51+
assert !gteU(5l, 22l)
52+
assert ltU(5l, -17l)
53+
assert !lteU(5l, 4l)
54+
4555
assert clz(0b11l) == 30l
4656
assert ctz(0b11000l) == 3l
4757
assert popcnt(0b1100110011l) == 6l

compiler/test/stdlib/int64.test.gr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ assert !gte(5L, 22L)
4141
assert !lt(5L, -17L)
4242
assert !lte(5L, 4L)
4343

44+
// Unsigned comparisons
45+
assert gtU(5L, 4L)
46+
assert gteU(5L, 5L)
47+
assert ltU(5L, 17L)
48+
assert lteU(5L, 5L)
49+
assert !gtU(5L, 5L)
50+
assert !gteU(5L, 22L)
51+
assert ltU(5L, -17L)
52+
assert !lteU(5L, 4L)
53+
4454
assert clz(0b11L) == 62L
4555
assert ctz(0b11000L) == 3L
4656
assert popcnt(0b1100110011L) == 6L

stdlib/int32.gr

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,22 @@ export let lt = (x: Int32, y: Int32) => {
387387
WasmI32.ltS(xv, yv)
388388
}
389389

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) => {
401+
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
402+
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
403+
WasmI32.ltU(xv, yv)
404+
}
405+
390406
/**
391407
* Checks if the first value is greater than the second value.
392408
*
@@ -403,6 +419,22 @@ export let gt = (x: Int32, y: Int32) => {
403419
WasmI32.gtS(xv, yv)
404420
}
405421

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) => {
433+
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
434+
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
435+
WasmI32.gtU(xv, yv)
436+
}
437+
406438
/**
407439
* Checks if the first value is less than or equal to the second value.
408440
*
@@ -419,6 +451,22 @@ export let lte = (x: Int32, y: Int32) => {
419451
WasmI32.leS(xv, yv)
420452
}
421453

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) => {
465+
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
466+
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
467+
WasmI32.leU(xv, yv)
468+
}
469+
422470
/**
423471
* Checks if the first value is greater than or equal to the second value.
424472
*
@@ -435,6 +483,22 @@ export let gte = (x: Int32, y: Int32) => {
435483
WasmI32.geS(xv, yv)
436484
}
437485

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) => {
497+
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
498+
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
499+
WasmI32.geU(xv, yv)
500+
}
501+
438502
/**
439503
* @section Bitwise logic: Boolean operations on the bits of Int32 values.
440504
*/

stdlib/int32.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,32 @@ Returns:
571571
|----|-----------|
572572
|`Bool`|`true` if the first value is less than the second value or `false` otherwise|
573573

574+
### Int32.**ltU**
575+
576+
<details disabled>
577+
<summary tabindex="-1">Added in <code>next</code></summary>
578+
No other changes yet.
579+
</details>
580+
581+
```grain
582+
ltU : (Int32, Int32) -> Bool
583+
```
584+
585+
Checks if the first unsigned value is less than the second unsigned value.
586+
587+
Parameters:
588+
589+
|param|type|description|
590+
|-----|----|-----------|
591+
|`x`|`Int32`|The first value|
592+
|`y`|`Int32`|The second value|
593+
594+
Returns:
595+
596+
|type|description|
597+
|----|-----------|
598+
|`Bool`|`true` if the first value is less than the second value or `false` otherwise|
599+
574600
### Int32.**gt**
575601

576602
<details disabled>
@@ -597,6 +623,32 @@ Returns:
597623
|----|-----------|
598624
|`Bool`|`true` if the first value is greater than the second value or `false` otherwise|
599625

626+
### Int32.**gtU**
627+
628+
<details disabled>
629+
<summary tabindex="-1">Added in <code>next</code></summary>
630+
No other changes yet.
631+
</details>
632+
633+
```grain
634+
gtU : (Int32, Int32) -> Bool
635+
```
636+
637+
Checks if the first unsigned value is greater than the second unsigned value.
638+
639+
Parameters:
640+
641+
|param|type|description|
642+
|-----|----|-----------|
643+
|`x`|`Int32`|The first value|
644+
|`y`|`Int32`|The second value|
645+
646+
Returns:
647+
648+
|type|description|
649+
|----|-----------|
650+
|`Bool`|`true` if the first value is greater than the second value or `false` otherwise|
651+
600652
### Int32.**lte**
601653

602654
<details disabled>
@@ -623,6 +675,32 @@ Returns:
623675
|----|-----------|
624676
|`Bool`|`true` if the first value is less than or equal to the second value or `false` otherwise|
625677

678+
### Int32.**lteU**
679+
680+
<details disabled>
681+
<summary tabindex="-1">Added in <code>next</code></summary>
682+
No other changes yet.
683+
</details>
684+
685+
```grain
686+
lteU : (Int32, Int32) -> Bool
687+
```
688+
689+
Checks if the first unsigned value is less than or equal to the second unsigned value.
690+
691+
Parameters:
692+
693+
|param|type|description|
694+
|-----|----|-----------|
695+
|`x`|`Int32`|The first value|
696+
|`y`|`Int32`|The second value|
697+
698+
Returns:
699+
700+
|type|description|
701+
|----|-----------|
702+
|`Bool`|`true` if the first value is less than or equal to the second value or `false` otherwise|
703+
626704
### Int32.**gte**
627705

628706
<details disabled>
@@ -649,6 +727,32 @@ Returns:
649727
|----|-----------|
650728
|`Bool`|`true` if the first value is greater than or equal to the second value or `false` otherwise|
651729

730+
### Int32.**gteU**
731+
732+
<details disabled>
733+
<summary tabindex="-1">Added in <code>next</code></summary>
734+
No other changes yet.
735+
</details>
736+
737+
```grain
738+
gteU : (Int32, Int32) -> Bool
739+
```
740+
741+
Checks if the first unsigned value is greater than or equal to the second unsigned value.
742+
743+
Parameters:
744+
745+
|param|type|description|
746+
|-----|----|-----------|
747+
|`x`|`Int32`|The first value|
748+
|`y`|`Int32`|The second value|
749+
750+
Returns:
751+
752+
|type|description|
753+
|----|-----------|
754+
|`Bool`|`true` if the first value is greater than or equal to the second value or `false` otherwise|
755+
652756
## Bitwise logic
653757

654758
Boolean operations on the bits of Int32 values.

stdlib/int64.gr

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,22 @@ export let lt = (x: Int64, y: Int64) => {
388388
WasmI64.ltS(xv, yv)
389389
}
390390

391+
/**
392+
* Checks if the first unsigned value is less than the second unsigned value.
393+
*
394+
* @param x: The first value
395+
* @param y: The second value
396+
* @returns `true` if the first value is less than the second value or `false` otherwise
397+
*
398+
* @since v0.5.0
399+
*/
400+
@unsafe
401+
export let rec ltU = (x: Int64, y: Int64) => {
402+
let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
403+
let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
404+
WasmI64.ltU(xv, yv)
405+
}
406+
391407
/**
392408
* Checks if the first value is greater than the second value.
393409
*
@@ -404,6 +420,22 @@ export let gt = (x: Int64, y: Int64) => {
404420
WasmI64.gtS(xv, yv)
405421
}
406422

423+
/**
424+
* Checks if the first unsigned value is greater than the second unsigned value.
425+
*
426+
* @param x: The first value
427+
* @param y: The second value
428+
* @returns `true` if the first value is greater than the second value or `false` otherwise
429+
*
430+
* @since v0.5.0
431+
*/
432+
@unsafe
433+
export let rec gtU = (x: Int64, y: Int64) => {
434+
let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
435+
let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
436+
WasmI64.gtU(xv, yv)
437+
}
438+
407439
/**
408440
* Checks if the first value is less than or equal to the second value.
409441
*
@@ -420,6 +452,22 @@ export let lte = (x: Int64, y: Int64) => {
420452
WasmI64.leS(xv, yv)
421453
}
422454

455+
/**
456+
* Checks if the first unsigned value is less than or equal to the second unsigned value.
457+
*
458+
* @param x: The first value
459+
* @param y: The second value
460+
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
461+
*
462+
* @since v0.5.0
463+
*/
464+
@unsafe
465+
export let rec lteU = (x: Int64, y: Int64) => {
466+
let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
467+
let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
468+
WasmI64.leU(xv, yv)
469+
}
470+
423471
/**
424472
* Checks if the first value is greater than or equal to the second value.
425473
*
@@ -436,6 +484,22 @@ export let gte = (x: Int64, y: Int64) => {
436484
WasmI64.geS(xv, yv)
437485
}
438486

487+
/**
488+
* Checks if the first unsigned value is greater than or equal to the second unsigned value.
489+
*
490+
* @param x: The first value
491+
* @param y: The second value
492+
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
493+
*
494+
* @since v0.5.0
495+
*/
496+
@unsafe
497+
export let rec gteU = (x: Int64, y: Int64) => {
498+
let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
499+
let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
500+
WasmI64.geU(xv, yv)
501+
}
502+
439503
/**
440504
* @section Bitwise logic: Boolean operations on the bits of Int64 values.
441505
*/

0 commit comments

Comments
 (0)