Skip to content

Commit

Permalink
feat(stdlib): Add unsigned versions of Int32/Int64 comparison operati…
Browse files Browse the repository at this point in the history
…ons (#831)
  • Loading branch information
peblair authored Apr 20, 2022
1 parent 0fb29c4 commit 5f20868
Show file tree
Hide file tree
Showing 6 changed files with 356 additions and 0 deletions.
10 changes: 10 additions & 0 deletions compiler/test/stdlib/int32.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ assert !gte(5l, 22l)
assert !lt(5l, -17l)
assert !lte(5l, 4l)

// unsigned comparisons:
assert gtU(5l, 4l)
assert gteU(5l, 5l)
assert ltU(5l, 17l)
assert lteU(5l, 5l)
assert !gtU(5l, 5l)
assert !gteU(5l, 22l)
assert ltU(5l, -17l)
assert !lteU(5l, 4l)

assert clz(0b11l) == 30l
assert ctz(0b11000l) == 3l
assert popcnt(0b1100110011l) == 6l
Expand Down
10 changes: 10 additions & 0 deletions compiler/test/stdlib/int64.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ assert !gte(5L, 22L)
assert !lt(5L, -17L)
assert !lte(5L, 4L)

// Unsigned comparisons
assert gtU(5L, 4L)
assert gteU(5L, 5L)
assert ltU(5L, 17L)
assert lteU(5L, 5L)
assert !gtU(5L, 5L)
assert !gteU(5L, 22L)
assert ltU(5L, -17L)
assert !lteU(5L, 4L)

assert clz(0b11L) == 62L
assert ctz(0b11000L) == 3L
assert popcnt(0b1100110011L) == 6L
Expand Down
64 changes: 64 additions & 0 deletions stdlib/int32.gr
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,22 @@ export let lt = (x: Int32, y: Int32) => {
WasmI32.ltS(xv, yv)
}

/**
* Checks if the first unsigned value is less than the second unsigned value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is less than the second value or `false` otherwise
*
* @since v0.5.0
*/
@unsafe
export let rec ltU = (x: Int32, y: Int32) => {
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
WasmI32.ltU(xv, yv)
}

/**
* Checks if the first value is greater than the second value.
*
Expand All @@ -403,6 +419,22 @@ export let gt = (x: Int32, y: Int32) => {
WasmI32.gtS(xv, yv)
}

/**
* Checks if the first unsigned value is greater than the second unsigned value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is greater than the second value or `false` otherwise
*
* @since v0.5.0
*/
@unsafe
export let rec gtU = (x: Int32, y: Int32) => {
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
WasmI32.gtU(xv, yv)
}

/**
* Checks if the first value is less than or equal to the second value.
*
Expand All @@ -419,6 +451,22 @@ export let lte = (x: Int32, y: Int32) => {
WasmI32.leS(xv, yv)
}

/**
* Checks if the first unsigned value is less than or equal to the second unsigned value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
*
* @since v0.5.0
*/
@unsafe
export let rec lteU = (x: Int32, y: Int32) => {
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
WasmI32.leU(xv, yv)
}

/**
* Checks if the first value is greater than or equal to the second value.
*
Expand All @@ -435,6 +483,22 @@ export let gte = (x: Int32, y: Int32) => {
WasmI32.geS(xv, yv)
}

/**
* Checks if the first unsigned value is greater than or equal to the second unsigned value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
*
* @since v0.5.0
*/
@unsafe
export let rec gteU = (x: Int32, y: Int32) => {
let xv = WasmI32.load(WasmI32.fromGrain(x), 8n)
let yv = WasmI32.load(WasmI32.fromGrain(y), 8n)
WasmI32.geU(xv, yv)
}

/**
* @section Bitwise logic: Boolean operations on the bits of Int32 values.
*/
Expand Down
104 changes: 104 additions & 0 deletions stdlib/int32.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,32 @@ Returns:
|----|-----------|
|`Bool`|`true` if the first value is less than the second value or `false` otherwise|

### Int32.**ltU**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
ltU : (Int32, Int32) -> Bool
```

Checks if the first unsigned value is less than the second unsigned value.

Parameters:

|param|type|description|
|-----|----|-----------|
|`x`|`Int32`|The first value|
|`y`|`Int32`|The second value|

Returns:

|type|description|
|----|-----------|
|`Bool`|`true` if the first value is less than the second value or `false` otherwise|

### Int32.**gt**

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

### Int32.**gtU**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
gtU : (Int32, Int32) -> Bool
```

Checks if the first unsigned value is greater than the second unsigned value.

Parameters:

|param|type|description|
|-----|----|-----------|
|`x`|`Int32`|The first value|
|`y`|`Int32`|The second value|

Returns:

|type|description|
|----|-----------|
|`Bool`|`true` if the first value is greater than the second value or `false` otherwise|

### Int32.**lte**

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

### Int32.**lteU**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
lteU : (Int32, Int32) -> Bool
```

Checks if the first unsigned value is less than or equal to the second unsigned value.

Parameters:

|param|type|description|
|-----|----|-----------|
|`x`|`Int32`|The first value|
|`y`|`Int32`|The second value|

Returns:

|type|description|
|----|-----------|
|`Bool`|`true` if the first value is less than or equal to the second value or `false` otherwise|

### Int32.**gte**

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

### Int32.**gteU**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
gteU : (Int32, Int32) -> Bool
```

Checks if the first unsigned value is greater than or equal to the second unsigned value.

Parameters:

|param|type|description|
|-----|----|-----------|
|`x`|`Int32`|The first value|
|`y`|`Int32`|The second value|

Returns:

|type|description|
|----|-----------|
|`Bool`|`true` if the first value is greater than or equal to the second value or `false` otherwise|

## Bitwise logic

Boolean operations on the bits of Int32 values.
Expand Down
64 changes: 64 additions & 0 deletions stdlib/int64.gr
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,22 @@ export let lt = (x: Int64, y: Int64) => {
WasmI64.ltS(xv, yv)
}

/**
* Checks if the first unsigned value is less than the second unsigned value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is less than the second value or `false` otherwise
*
* @since v0.5.0
*/
@unsafe
export let rec ltU = (x: Int64, y: Int64) => {
let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
WasmI64.ltU(xv, yv)
}

/**
* Checks if the first value is greater than the second value.
*
Expand All @@ -404,6 +420,22 @@ export let gt = (x: Int64, y: Int64) => {
WasmI64.gtS(xv, yv)
}

/**
* Checks if the first unsigned value is greater than the second unsigned value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is greater than the second value or `false` otherwise
*
* @since v0.5.0
*/
@unsafe
export let rec gtU = (x: Int64, y: Int64) => {
let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
WasmI64.gtU(xv, yv)
}

/**
* Checks if the first value is less than or equal to the second value.
*
Expand All @@ -420,6 +452,22 @@ export let lte = (x: Int64, y: Int64) => {
WasmI64.leS(xv, yv)
}

/**
* Checks if the first unsigned value is less than or equal to the second unsigned value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
*
* @since v0.5.0
*/
@unsafe
export let rec lteU = (x: Int64, y: Int64) => {
let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
WasmI64.leU(xv, yv)
}

/**
* Checks if the first value is greater than or equal to the second value.
*
Expand All @@ -436,6 +484,22 @@ export let gte = (x: Int64, y: Int64) => {
WasmI64.geS(xv, yv)
}

/**
* Checks if the first unsigned value is greater than or equal to the second unsigned value.
*
* @param x: The first value
* @param y: The second value
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
*
* @since v0.5.0
*/
@unsafe
export let rec gteU = (x: Int64, y: Int64) => {
let xv = WasmI64.load(WasmI32.fromGrain(x), 8n)
let yv = WasmI64.load(WasmI32.fromGrain(y), 8n)
WasmI64.geU(xv, yv)
}

/**
* @section Bitwise logic: Boolean operations on the bits of Int64 values.
*/
Expand Down
Loading

0 comments on commit 5f20868

Please sign in to comment.