Skip to content

Commit

Permalink
Int: Add bitwise functions from Pervasives (#245)
Browse files Browse the repository at this point in the history
* Int: Add bitwise functions from Pervasives

* Changelog

* Put bitwise functions into their own submodule
  • Loading branch information
fhammerschmidt authored Sep 8, 2024
1 parent ddc4a08 commit 98eb2e8
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
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"
}

0 comments on commit 98eb2e8

Please sign in to comment.