Skip to content

Commit 3515439

Browse files
Put bitwise functions into their own submodule
1 parent e9f8860 commit 3515439

File tree

3 files changed

+100
-92
lines changed

3 files changed

+100
-92
lines changed

src/Core__Int.mjs

+9-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ function fromString(x, radix) {
1111
}
1212
}
1313

14-
function lnot(x) {
15-
return x ^ -1;
16-
}
17-
1814
function abs(x) {
1915
if (x >= 0) {
2016
return x;
@@ -68,6 +64,14 @@ function clamp(min, max, value) {
6864
}
6965
}
7066

67+
function lnot(x) {
68+
return x ^ -1;
69+
}
70+
71+
var Bitwise = {
72+
lnot: lnot
73+
};
74+
7175
var Constants = {
7276
minValue: -2147483648,
7377
maxValue: 2147483647
@@ -76,9 +80,9 @@ var Constants = {
7680
export {
7781
Constants ,
7882
fromString ,
79-
lnot ,
8083
range ,
8184
rangeWithOptions ,
8285
clamp ,
86+
Bitwise ,
8387
}
8488
/* No side effect */

src/Core__Int.res

+12-10
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ let fromString = (x, ~radix=?) => {
4545

4646
external mod: (int, int) => int = "%modint"
4747

48-
external land: (int, int) => int = "%andint"
49-
external lor: (int, int) => int = "%orint"
50-
external lxor: (int, int) => int = "%xorint"
51-
52-
external lsl: (int, int) => int = "%lslint"
53-
external lsr: (int, int) => int = "%lsrint"
54-
external asr: (int, int) => int = "%asrint"
55-
56-
let lnot = x => lxor(x, -1)
57-
5848
type rangeOptions = {step?: int, inclusive?: bool}
5949

6050
let abs = x =>
@@ -100,3 +90,15 @@ let clamp = (~min=?, ~max=?, value): int => {
10090
| _ => value
10191
}
10292
}
93+
94+
module Bitwise = {
95+
external land: (int, int) => int = "%andint"
96+
external lor: (int, int) => int = "%orint"
97+
external lxor: (int, int) => int = "%xorint"
98+
99+
external lsl: (int, int) => int = "%lslint"
100+
external lsr: (int, int) => int = "%lsrint"
101+
external asr: (int, int) => int = "%asrint"
102+
103+
let lnot = x => lxor(x, -1)
104+
}

src/Core__Int.resi

+79-77
Original file line numberDiff line numberDiff line change
@@ -292,83 +292,6 @@ Int.fromString("6", ~radix=2) == None
292292
*/
293293
let fromString: (string, ~radix: int=?) => option<int>
294294

295-
/**
296-
`land(n1, n2)` calculates the bitwise logical AND of two integers.
297-
298-
## Examples
299-
300-
```rescript
301-
Int.land(7, 4) == 4
302-
```
303-
*/
304-
external land: (int, int) => int = "%andint"
305-
306-
/**
307-
`lor(n1, n2)` calculates the bitwise logical OR of two integers.
308-
309-
## Examples
310-
311-
```rescript
312-
Int.lor(7, 4) == 7
313-
```
314-
*/
315-
external lor: (int, int) => int = "%orint"
316-
317-
/**
318-
`lxor(n1, n2)` calculates the bitwise logical XOR of two integers.
319-
320-
## Examples
321-
322-
```rescript
323-
Int.lxor(7, 4) == 3
324-
```
325-
*/
326-
external lxor: (int, int) => int = "%xorint"
327-
328-
/**
329-
`lnot(n)` calculates the bitwise logical NOT of an integer.
330-
331-
## Examples
332-
333-
```rescript
334-
Int.lnot(2) == -3
335-
```
336-
*/
337-
let lnot: int => int
338-
339-
/**
340-
`lsl(n, length)` calculates the bitwise logical left shift of an integer `n` by `length`.
341-
342-
## Examples
343-
344-
```rescript
345-
Int.lsl(4, 1) == 8
346-
```
347-
*/
348-
external lsl: (int, int) => int = "%lslint"
349-
350-
/**
351-
`lsr(n, length)` calculates the bitwise logical right shift of an integer `n` by `length`.
352-
353-
## Examples
354-
355-
```rescript
356-
Int.lsr(8, 1) == 4
357-
```
358-
*/
359-
external lsr: (int, int) => int = "%lsrint"
360-
361-
/**
362-
`asr(n, length)` calculates the bitwise arithmetic right shift of an integer `n` by `length`.
363-
364-
## Examples
365-
366-
```rescript
367-
Int.asr(4, 1) == 2
368-
```
369-
*/
370-
external asr: (int, int) => int = "%asrint"
371-
372295
/**
373296
`mod(n1, n2)` calculates the modulo (remainder after division) of two integers.
374297

@@ -464,3 +387,82 @@ Int.clamp(42, ~min=50, ~max=40) == 50
464387
```
465388
*/
466389
let clamp: (~min: int=?, ~max: int=?, int) => int
390+
391+
module Bitwise: {
392+
/**
393+
`land(n1, n2)` calculates the bitwise logical AND of two integers.
394+
395+
## Examples
396+
397+
```rescript
398+
Int.Bitwise.land(7, 4) == 4
399+
```
400+
*/
401+
external land: (int, int) => int = "%andint"
402+
403+
/**
404+
`lor(n1, n2)` calculates the bitwise logical OR of two integers.
405+
406+
## Examples
407+
408+
```rescript
409+
Int.Bitwise.lor(7, 4) == 7
410+
```
411+
*/
412+
external lor: (int, int) => int = "%orint"
413+
414+
/**
415+
`lxor(n1, n2)` calculates the bitwise logical XOR of two integers.
416+
417+
## Examples
418+
419+
```rescript
420+
Int.Bitwise.lxor(7, 4) == 3
421+
```
422+
*/
423+
external lxor: (int, int) => int = "%xorint"
424+
425+
/**
426+
`lnot(n)` calculates the bitwise logical NOT of an integer.
427+
428+
## Examples
429+
430+
```rescript
431+
Int.Bitwise.lnot(2) == -3
432+
```
433+
*/
434+
let lnot: int => int
435+
436+
/**
437+
`lsl(n, length)` calculates the bitwise logical left shift of an integer `n` by `length`.
438+
439+
## Examples
440+
441+
```rescript
442+
Int.Bitwise.lsl(4, 1) == 8
443+
```
444+
*/
445+
external lsl: (int, int) => int = "%lslint"
446+
447+
/**
448+
`lsr(n, length)` calculates the bitwise logical right shift of an integer `n` by `length`.
449+
450+
## Examples
451+
452+
```rescript
453+
Int.Bitwise.lsr(8, 1) == 4
454+
```
455+
*/
456+
external lsr: (int, int) => int = "%lsrint"
457+
458+
/**
459+
`asr(n, length)` calculates the bitwise arithmetic right shift of an integer `n` by `length`.
460+
461+
## Examples
462+
463+
```rescript
464+
Int.Bitwise.asr(4, 1) == 2
465+
```
466+
*/
467+
external asr: (int, int) => int = "%asrint"
468+
}

0 commit comments

Comments
 (0)