Skip to content

Int: Add bitwise functions from Pervasives #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Optimize compare and equal functions. https://github.com/rescript-association/rescript-core/pull/238
- Add `make` and `done` + `value` functions to `AsyncIterator`. https://github.com/rescript-association/rescript-core/pull/243
- Int: Add bitwise functions from Pervasives. https://github.com/rescript-association/rescript-core/pull/245

## 1.5.2

Expand Down
9 changes: 9 additions & 0 deletions src/Core__Int.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ function clamp(min, max, value) {
}
}

function lnot(x) {
return x ^ -1;
}

var Bitwise = {
lnot: lnot
};

var Constants = {
minValue: -2147483648,
maxValue: 2147483647
Expand All @@ -75,5 +83,6 @@ export {
range ,
rangeWithOptions ,
clamp ,
Bitwise ,
}
/* No side effect */
12 changes: 12 additions & 0 deletions src/Core__Int.res
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,15 @@ let clamp = (~min=?, ~max=?, value): int => {
| _ => value
}
}

module Bitwise = {
external land: (int, int) => int = "%andint"
external lor: (int, int) => int = "%orint"
external lxor: (int, int) => int = "%xorint"

external lsl: (int, int) => int = "%lslint"
external lsr: (int, int) => int = "%lsrint"
external asr: (int, int) => int = "%asrint"

let lnot = x => lxor(x, -1)
}
79 changes: 79 additions & 0 deletions src/Core__Int.resi
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,82 @@ Int.clamp(42, ~min=50, ~max=40) == 50
```
*/
let clamp: (~min: int=?, ~max: int=?, int) => int

module Bitwise: {
/**
`land(n1, n2)` calculates the bitwise logical AND of two integers.

## Examples

```rescript
Int.Bitwise.land(7, 4) == 4
```
*/
external land: (int, int) => int = "%andint"

/**
`lor(n1, n2)` calculates the bitwise logical OR of two integers.

## Examples

```rescript
Int.Bitwise.lor(7, 4) == 7
```
*/
external lor: (int, int) => int = "%orint"

/**
`lxor(n1, n2)` calculates the bitwise logical XOR of two integers.

## Examples

```rescript
Int.Bitwise.lxor(7, 4) == 3
```
*/
external lxor: (int, int) => int = "%xorint"

/**
`lnot(n)` calculates the bitwise logical NOT of an integer.

## Examples

```rescript
Int.Bitwise.lnot(2) == -3
```
*/
let lnot: int => int

/**
`lsl(n, length)` calculates the bitwise logical left shift of an integer `n` by `length`.

## Examples

```rescript
Int.Bitwise.lsl(4, 1) == 8
```
*/
external lsl: (int, int) => int = "%lslint"

/**
`lsr(n, length)` calculates the bitwise logical right shift of an integer `n` by `length`.

## Examples

```rescript
Int.Bitwise.lsr(8, 1) == 4
```
*/
external lsr: (int, int) => int = "%lsrint"

/**
`asr(n, length)` calculates the bitwise arithmetic right shift of an integer `n` by `length`.

## Examples

```rescript
Int.Bitwise.asr(4, 1) == 2
```
*/
external asr: (int, int) => int = "%asrint"
}
Loading