extra-bit 2.0.3 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/index.d.ts +6 -37
- package/index.js +2 -22
- package/index.mjs +2 -22
- package/package.json +5 -6
package/README.md
CHANGED
|
@@ -19,22 +19,22 @@ is exposed as `extra_bit` standalone variable and can be loaded from
|
|
|
19
19
|
```javascript
|
|
20
20
|
const bit = require('extra-bit');
|
|
21
21
|
// import * as bit from "extra-bit";
|
|
22
|
-
// import * as bit from "https://unpkg.com/extra-bit
|
|
22
|
+
// import * as bit from "https://unpkg.com/extra-bit/index.mjs"; (deno)
|
|
23
23
|
|
|
24
24
|
bit.count(7);
|
|
25
|
-
// 3 (111 ⇒ 3)
|
|
25
|
+
// → 3 (111 ⇒ 3)
|
|
26
26
|
|
|
27
27
|
bit.parity(8, 2);
|
|
28
|
-
// 2 (10,00 ⇒ 10)
|
|
28
|
+
// → 2 (10,00 ⇒ 10)
|
|
29
29
|
|
|
30
30
|
bit.swap(6, 1, 0);
|
|
31
|
-
// 5 (110 ⇒ 101)
|
|
31
|
+
// → 5 (110 ⇒ 101)
|
|
32
32
|
|
|
33
33
|
bit.reverse(0xFFFF0000);
|
|
34
|
-
// 65535 (0x0000FFFF)
|
|
34
|
+
// → 65535 (0x0000FFFF)
|
|
35
35
|
|
|
36
36
|
bit.signExtend(15, 4);
|
|
37
|
-
// -1
|
|
37
|
+
// → -1
|
|
38
38
|
```
|
|
39
39
|
|
|
40
40
|
<br>
|
package/index.d.ts
CHANGED
|
@@ -1,30 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Get
|
|
2
|
+
* Get bits at specified index.
|
|
3
3
|
* @param x an int32
|
|
4
4
|
* @param i bit index
|
|
5
5
|
* @param w bit width (1)
|
|
6
6
|
*/
|
|
7
7
|
declare function get(x: number, i: number, w?: number): number;
|
|
8
|
-
//# sourceMappingURL=get.d.ts.map
|
|
9
|
-
|
|
10
8
|
/**
|
|
11
9
|
* Get bits as per mask.
|
|
12
10
|
* @param x an int32
|
|
13
11
|
* @param m bit mask
|
|
14
12
|
*/
|
|
15
13
|
declare function getAs(x: number, m: number): number;
|
|
16
|
-
//# sourceMappingURL=getAs.d.ts.map
|
|
17
|
-
|
|
18
14
|
/**
|
|
19
|
-
* Set
|
|
15
|
+
* Set bits at specified index.
|
|
20
16
|
* @param x an int32
|
|
21
17
|
* @param i bit index
|
|
22
18
|
* @param v bit value (1)
|
|
23
19
|
* @param w bit width (1)
|
|
24
20
|
*/
|
|
25
21
|
declare function set(x: number, i: number, v?: number, w?: number): number;
|
|
26
|
-
//# sourceMappingURL=set.d.ts.map
|
|
27
|
-
|
|
28
22
|
/**
|
|
29
23
|
* Set bits as per mask.
|
|
30
24
|
* @param x an int32
|
|
@@ -32,79 +26,59 @@ declare function set(x: number, i: number, v?: number, w?: number): number;
|
|
|
32
26
|
* @param f bit value (1)
|
|
33
27
|
*/
|
|
34
28
|
declare function setAs(x: number, m: number, f?: number): number;
|
|
35
|
-
//# sourceMappingURL=setAs.d.ts.map
|
|
36
|
-
|
|
37
29
|
/**
|
|
38
|
-
* Toggle
|
|
30
|
+
* Toggle bits at specified index.
|
|
39
31
|
* @param x an int32
|
|
40
32
|
* @param i bit index
|
|
41
33
|
* @param w bit width (1)
|
|
42
34
|
*/
|
|
43
35
|
declare function toggle(x: number, i: number, w?: number): number;
|
|
44
|
-
//# sourceMappingURL=toggle.d.ts.map
|
|
45
|
-
|
|
46
36
|
/**
|
|
47
37
|
* Toggle bits as per mask.
|
|
48
38
|
* @param x an int32
|
|
49
39
|
* @param m bit mask
|
|
50
40
|
*/
|
|
51
41
|
declare function toggleAs(x: number, m: number): number;
|
|
52
|
-
//# sourceMappingURL=toggleAs.d.ts.map
|
|
53
|
-
|
|
54
42
|
/**
|
|
55
|
-
* Swap bit sequences.
|
|
43
|
+
* Swap bit sequences at specified indices.
|
|
56
44
|
* @param x an int32
|
|
57
45
|
* @param i first bit index
|
|
58
46
|
* @param j second bit index
|
|
59
47
|
* @param w bit width (1)
|
|
60
48
|
*/
|
|
61
49
|
declare function swap(x: number, i: number, j: number, w?: number): number;
|
|
62
|
-
//# sourceMappingURL=swap.d.ts.map
|
|
63
|
-
|
|
64
50
|
/**
|
|
65
51
|
* Find index of first set bit from LSB.
|
|
66
52
|
* @param x an int32
|
|
67
53
|
*/
|
|
68
54
|
declare function scan(x: number): number;
|
|
69
|
-
//# sourceMappingURL=scan.d.ts.map
|
|
70
|
-
|
|
71
55
|
/**
|
|
72
56
|
* Find index of first set bit from MSB.
|
|
73
57
|
* @param x an int32
|
|
74
58
|
*/
|
|
75
59
|
declare function scanReverse(x: number): number;
|
|
76
|
-
//# sourceMappingURL=scanReverse.d.ts.map
|
|
77
|
-
|
|
78
60
|
/**
|
|
79
61
|
* Count bits set.
|
|
80
62
|
* @param x an int32
|
|
81
63
|
*/
|
|
82
64
|
declare function count(x: number): number;
|
|
83
|
-
//# sourceMappingURL=count.d.ts.map
|
|
84
|
-
|
|
85
65
|
/**
|
|
86
|
-
*
|
|
66
|
+
* Calculate n-bit parity.
|
|
87
67
|
* @param x an int32
|
|
88
68
|
* @param n number of bits (1)
|
|
89
69
|
*/
|
|
90
70
|
declare function parity(x: number, n?: number): number;
|
|
91
|
-
//# sourceMappingURL=parity.d.ts.map
|
|
92
|
-
|
|
93
71
|
/**
|
|
94
|
-
* Rotate bits.
|
|
72
|
+
* Rotate bits by a certain amount.
|
|
95
73
|
* @param x an int32
|
|
96
74
|
* @param n rotate amount (+ve: left, -ve: right)
|
|
97
75
|
*/
|
|
98
76
|
declare function rotate(x: number, n?: number): number;
|
|
99
|
-
//# sourceMappingURL=rotate.d.ts.map
|
|
100
|
-
|
|
101
77
|
/**
|
|
102
78
|
* Reverse all bits.
|
|
103
79
|
* @param x an int32
|
|
104
80
|
*/
|
|
105
81
|
declare function reverse(x: number): number;
|
|
106
|
-
//# sourceMappingURL=reverse.d.ts.map
|
|
107
|
-
|
|
108
82
|
/**
|
|
109
83
|
* Merge bits as per mask.
|
|
110
84
|
* @param x first int32
|
|
@@ -112,8 +86,6 @@ declare function reverse(x: number): number;
|
|
|
112
86
|
* @param m bit mask (0 ⇒ from x)
|
|
113
87
|
*/
|
|
114
88
|
declare function merge(x: number, y: number, m: number): number;
|
|
115
|
-
//# sourceMappingURL=merge.d.ts.map
|
|
116
|
-
|
|
117
89
|
/**
|
|
118
90
|
* Interleave bits of two int16s.
|
|
119
91
|
* @param x first int16
|
|
@@ -121,14 +93,11 @@ declare function merge(x: number, y: number, m: number): number;
|
|
|
121
93
|
* @returns int32
|
|
122
94
|
*/
|
|
123
95
|
declare function interleave(x: number, y: number): number;
|
|
124
|
-
//# sourceMappingURL=interleave.d.ts.map
|
|
125
|
-
|
|
126
96
|
/**
|
|
127
97
|
* Sign extend variable bit-width integer.
|
|
128
98
|
* @param x variable bit-width integer
|
|
129
99
|
* @param w bit width (32)
|
|
130
100
|
*/
|
|
131
101
|
declare function signExtend(x: number, w?: number): number;
|
|
132
|
-
//# sourceMappingURL=signExtend.d.ts.map
|
|
133
102
|
|
|
134
103
|
export { count, get, getAs, interleave, merge, parity, reverse, rotate, scan, scanReverse, set, setAs, signExtend, swap, toggle, toggleAs };
|
package/index.js
CHANGED
|
@@ -5,67 +5,52 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
function ones(w) {
|
|
6
6
|
return (1 << w) - 1;
|
|
7
7
|
}
|
|
8
|
-
|
|
9
8
|
function get(x, i, w = 1) {
|
|
10
9
|
return (x >>> i) & ones(w);
|
|
11
10
|
}
|
|
12
|
-
|
|
13
11
|
function getAs(x, m) {
|
|
14
12
|
return x & m;
|
|
15
13
|
}
|
|
16
|
-
|
|
17
14
|
function set(x, i, v = 1, w = 1) {
|
|
18
15
|
return (x & ~(ones(w) << i)) | (v << i);
|
|
19
16
|
}
|
|
20
|
-
|
|
21
17
|
function setAs(x, m, f = 1) {
|
|
22
18
|
return (x & ~m) | (-f & m);
|
|
23
19
|
}
|
|
24
|
-
|
|
25
20
|
function toggle(x, i, w = 1) {
|
|
26
21
|
return x ^ (ones(w) << i);
|
|
27
22
|
}
|
|
28
|
-
|
|
29
23
|
function toggleAs(x, m) {
|
|
30
24
|
return x ^ m;
|
|
31
25
|
}
|
|
32
|
-
|
|
33
26
|
function swap(x, i, j, w = 1) {
|
|
34
27
|
var t = ((x >>> i) ^ (x >>> j)) & ones(w);
|
|
35
28
|
return x ^ ((t << i) | (t << j));
|
|
36
29
|
}
|
|
37
|
-
|
|
38
30
|
const MOD37_POS32 = [
|
|
39
31
|
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
|
|
40
32
|
25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
|
|
41
33
|
];
|
|
42
|
-
var MOD37_POS32$1 = MOD37_POS32;
|
|
43
|
-
|
|
44
34
|
function scan(x) {
|
|
45
|
-
return MOD37_POS32
|
|
35
|
+
return MOD37_POS32[(-x & x) % 37];
|
|
46
36
|
}
|
|
47
|
-
|
|
48
37
|
const DEBRUIJN_POS32 = [
|
|
49
38
|
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
50
39
|
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
51
40
|
];
|
|
52
|
-
var DEBRUIJN_POS32$1 = DEBRUIJN_POS32;
|
|
53
|
-
|
|
54
41
|
function scanReverse(x) {
|
|
55
42
|
x |= x >>> 1;
|
|
56
43
|
x |= x >>> 2;
|
|
57
44
|
x |= x >>> 4;
|
|
58
45
|
x |= x >>> 8;
|
|
59
46
|
x |= x >>> 16;
|
|
60
|
-
return DEBRUIJN_POS32
|
|
47
|
+
return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
|
|
61
48
|
}
|
|
62
|
-
|
|
63
49
|
function count(x) {
|
|
64
50
|
x = x - ((x >>> 1) & 0x55555555);
|
|
65
51
|
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
|
|
66
52
|
return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
|
67
53
|
}
|
|
68
|
-
|
|
69
54
|
function parity1(x) {
|
|
70
55
|
x ^= x >>> 16;
|
|
71
56
|
x ^= x >>> 8;
|
|
@@ -83,11 +68,9 @@ function parity(x, n = 1) {
|
|
|
83
68
|
}
|
|
84
69
|
return a;
|
|
85
70
|
}
|
|
86
|
-
|
|
87
71
|
function rotate(x, n = 0) {
|
|
88
72
|
return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
|
|
89
73
|
}
|
|
90
|
-
|
|
91
74
|
function reverse(x) {
|
|
92
75
|
x = ((x >>> 1) & 0x55555555) | ((x & 0x55555555) << 1);
|
|
93
76
|
x = ((x >>> 2) & 0x33333333) | ((x & 0x33333333) << 2);
|
|
@@ -95,11 +78,9 @@ function reverse(x) {
|
|
|
95
78
|
x = ((x >>> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
|
|
96
79
|
return (x >>> 16) | (x << 16);
|
|
97
80
|
}
|
|
98
|
-
|
|
99
81
|
function merge(x, y, m) {
|
|
100
82
|
return x ^ ((x ^ y) & m);
|
|
101
83
|
}
|
|
102
|
-
|
|
103
84
|
function interleave(x, y) {
|
|
104
85
|
x = (x | (x << 8)) & 0x00FF00FF;
|
|
105
86
|
x = (x | (x << 4)) & 0x0F0F0F0F;
|
|
@@ -111,7 +92,6 @@ function interleave(x, y) {
|
|
|
111
92
|
y = (y | (y << 1)) & 0x55555555;
|
|
112
93
|
return y | (x << 1);
|
|
113
94
|
}
|
|
114
|
-
|
|
115
95
|
function signExtend(x, w = 32) {
|
|
116
96
|
w = 32 - w;
|
|
117
97
|
return (x << w) >> w;
|
package/index.mjs
CHANGED
|
@@ -1,67 +1,52 @@
|
|
|
1
1
|
function ones(w) {
|
|
2
2
|
return (1 << w) - 1;
|
|
3
3
|
}
|
|
4
|
-
|
|
5
4
|
function get(x, i, w = 1) {
|
|
6
5
|
return (x >>> i) & ones(w);
|
|
7
6
|
}
|
|
8
|
-
|
|
9
7
|
function getAs(x, m) {
|
|
10
8
|
return x & m;
|
|
11
9
|
}
|
|
12
|
-
|
|
13
10
|
function set(x, i, v = 1, w = 1) {
|
|
14
11
|
return (x & ~(ones(w) << i)) | (v << i);
|
|
15
12
|
}
|
|
16
|
-
|
|
17
13
|
function setAs(x, m, f = 1) {
|
|
18
14
|
return (x & ~m) | (-f & m);
|
|
19
15
|
}
|
|
20
|
-
|
|
21
16
|
function toggle(x, i, w = 1) {
|
|
22
17
|
return x ^ (ones(w) << i);
|
|
23
18
|
}
|
|
24
|
-
|
|
25
19
|
function toggleAs(x, m) {
|
|
26
20
|
return x ^ m;
|
|
27
21
|
}
|
|
28
|
-
|
|
29
22
|
function swap(x, i, j, w = 1) {
|
|
30
23
|
var t = ((x >>> i) ^ (x >>> j)) & ones(w);
|
|
31
24
|
return x ^ ((t << i) | (t << j));
|
|
32
25
|
}
|
|
33
|
-
|
|
34
26
|
const MOD37_POS32 = [
|
|
35
27
|
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
|
|
36
28
|
25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
|
|
37
29
|
];
|
|
38
|
-
var MOD37_POS32$1 = MOD37_POS32;
|
|
39
|
-
|
|
40
30
|
function scan(x) {
|
|
41
|
-
return MOD37_POS32
|
|
31
|
+
return MOD37_POS32[(-x & x) % 37];
|
|
42
32
|
}
|
|
43
|
-
|
|
44
33
|
const DEBRUIJN_POS32 = [
|
|
45
34
|
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
46
35
|
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
47
36
|
];
|
|
48
|
-
var DEBRUIJN_POS32$1 = DEBRUIJN_POS32;
|
|
49
|
-
|
|
50
37
|
function scanReverse(x) {
|
|
51
38
|
x |= x >>> 1;
|
|
52
39
|
x |= x >>> 2;
|
|
53
40
|
x |= x >>> 4;
|
|
54
41
|
x |= x >>> 8;
|
|
55
42
|
x |= x >>> 16;
|
|
56
|
-
return DEBRUIJN_POS32
|
|
43
|
+
return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
|
|
57
44
|
}
|
|
58
|
-
|
|
59
45
|
function count(x) {
|
|
60
46
|
x = x - ((x >>> 1) & 0x55555555);
|
|
61
47
|
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
|
|
62
48
|
return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
|
63
49
|
}
|
|
64
|
-
|
|
65
50
|
function parity1(x) {
|
|
66
51
|
x ^= x >>> 16;
|
|
67
52
|
x ^= x >>> 8;
|
|
@@ -79,11 +64,9 @@ function parity(x, n = 1) {
|
|
|
79
64
|
}
|
|
80
65
|
return a;
|
|
81
66
|
}
|
|
82
|
-
|
|
83
67
|
function rotate(x, n = 0) {
|
|
84
68
|
return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
|
|
85
69
|
}
|
|
86
|
-
|
|
87
70
|
function reverse(x) {
|
|
88
71
|
x = ((x >>> 1) & 0x55555555) | ((x & 0x55555555) << 1);
|
|
89
72
|
x = ((x >>> 2) & 0x33333333) | ((x & 0x33333333) << 2);
|
|
@@ -91,11 +74,9 @@ function reverse(x) {
|
|
|
91
74
|
x = ((x >>> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
|
|
92
75
|
return (x >>> 16) | (x << 16);
|
|
93
76
|
}
|
|
94
|
-
|
|
95
77
|
function merge(x, y, m) {
|
|
96
78
|
return x ^ ((x ^ y) & m);
|
|
97
79
|
}
|
|
98
|
-
|
|
99
80
|
function interleave(x, y) {
|
|
100
81
|
x = (x | (x << 8)) & 0x00FF00FF;
|
|
101
82
|
x = (x | (x << 4)) & 0x0F0F0F0F;
|
|
@@ -107,7 +88,6 @@ function interleave(x, y) {
|
|
|
107
88
|
y = (y | (y << 1)) & 0x55555555;
|
|
108
89
|
return y | (x << 1);
|
|
109
90
|
}
|
|
110
|
-
|
|
111
91
|
function signExtend(x, w = 32) {
|
|
112
92
|
w = 32 - w;
|
|
113
93
|
return (x << w) >> w;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extra-bit",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.2",
|
|
4
4
|
"description": "The bit is a basic unit of information in information theory, computing.",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "index.js",
|
|
@@ -45,11 +45,10 @@
|
|
|
45
45
|
},
|
|
46
46
|
"homepage": "https://github.com/nodef/extra-bit#readme",
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@rollup/plugin-commonjs": "^
|
|
49
|
-
"@rollup/plugin-node-resolve": "^13.
|
|
50
|
-
"
|
|
51
|
-
"extra-build": "^2.0.31",
|
|
48
|
+
"@rollup/plugin-commonjs": "^22.0.1",
|
|
49
|
+
"@rollup/plugin-node-resolve": "^13.3.0",
|
|
50
|
+
"extra-build": "^2.1.31",
|
|
52
51
|
"rollup-plugin-cleanup": "^3.2.1",
|
|
53
|
-
"rollup-plugin-dts": "^4.2.
|
|
52
|
+
"rollup-plugin-dts": "^4.2.2"
|
|
54
53
|
}
|
|
55
54
|
}
|