extra-bit 2.0.0 → 2.0.3
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/LICENSE +1 -1
- package/README.md +47 -36
- package/index.d.ts +89 -253
- package/index.js +76 -53
- package/index.mjs +74 -53
- package/package.json +14 -9
- package/example.js +0 -11
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
The [bit] is a basic unit of information in information theory, computing.
|
|
2
2
|
This package includes [bit twiddling hacks] by Sean Eron Anderson and many others.<br>
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
:scroll: [Files](https://unpkg.com/extra-bit/),
|
|
9
|
-
:newspaper: [JSDoc](https://nodef.github.io/extra-bit/),
|
|
10
|
-
:blue_book: [Wiki](https://github.com/nodef/extra-bit/wiki/).
|
|
3
|
+
📦 [Node.js](https://www.npmjs.com/package/extra-bit),
|
|
4
|
+
🌐 [Web](https://www.npmjs.com/package/extra-bit.web),
|
|
5
|
+
📜 [Files](https://unpkg.com/extra-bit/),
|
|
6
|
+
📰 [Docs](https://nodef.github.io/extra-bit/).
|
|
7
|
+
📘 [Wiki](https://github.com/nodef/extra-bit/wiki/).
|
|
11
8
|
|
|
12
|
-
|
|
9
|
+
This package is available in *Node.js* and *Web* formats. The web format
|
|
10
|
+
is exposed as `extra_bit` standalone variable and can be loaded from
|
|
11
|
+
[jsDelivr CDN].
|
|
12
|
+
|
|
13
|
+
> Stability: [Experimental](https://www.youtube.com/watch?v=L1j93RnIxEo).
|
|
14
|
+
|
|
15
|
+
[jsDelivr CDN]: https://cdn.jsdelivr.net/npm/extra-bit.web/index.js
|
|
13
16
|
|
|
14
17
|
<br>
|
|
15
18
|
|
|
16
19
|
```javascript
|
|
17
|
-
const bit = require(
|
|
20
|
+
const bit = require('extra-bit');
|
|
18
21
|
// import * as bit from "extra-bit";
|
|
19
22
|
// import * as bit from "https://unpkg.com/extra-bit@1.0.50/index.mjs"; (deno)
|
|
20
23
|
|
|
@@ -40,20 +43,24 @@ bit.signExtend(15, 4);
|
|
|
40
43
|
|
|
41
44
|
## Index
|
|
42
45
|
|
|
43
|
-
|
|
|
44
|
-
|
|
|
45
|
-
| [get]
|
|
46
|
-
| [
|
|
47
|
-
| [
|
|
48
|
-
| [
|
|
49
|
-
| [
|
|
50
|
-
| [
|
|
51
|
-
| [
|
|
52
|
-
| [
|
|
53
|
-
| [
|
|
54
|
-
| [
|
|
55
|
-
| [
|
|
56
|
-
| [
|
|
46
|
+
| Property | Description |
|
|
47
|
+
| ---- | ---- |
|
|
48
|
+
| [get] | Get a bit. |
|
|
49
|
+
| [getAs] | Get bits as per mask. |
|
|
50
|
+
| [set] | Set a bit. |
|
|
51
|
+
| [setAs] | Set bits as per mask. |
|
|
52
|
+
| [toggle] | Toggle a bit. |
|
|
53
|
+
| [toggleAs] | Toggle bits as per mask. |
|
|
54
|
+
| [swap] | Swap bit sequences. |
|
|
55
|
+
| [scan] | Find index of first set bit from LSB. |
|
|
56
|
+
| [scanReverse] | Find index of first set bit from MSB. |
|
|
57
|
+
| [count] | Count bits set. |
|
|
58
|
+
| [parity] | Find n-bit parity. |
|
|
59
|
+
| [rotate] | Rotate bits. |
|
|
60
|
+
| [reverse] | Reverse all bits. |
|
|
61
|
+
| [merge] | Merge bits as per mask. |
|
|
62
|
+
| [interleave] | Interleave bits of two int16s. |
|
|
63
|
+
| [signExtend] | Sign extend variable bit-width integer. |
|
|
57
64
|
|
|
58
65
|
<br>
|
|
59
66
|
<br>
|
|
@@ -62,15 +69,19 @@ bit.signExtend(15, 4);
|
|
|
62
69
|
|
|
63
70
|
[bit]: https://en.wikipedia.org/wiki/Bit
|
|
64
71
|
[bit twiddling hacks]: https://graphics.stanford.edu/~seander/bithacks.html
|
|
65
|
-
[get]: https://github.
|
|
66
|
-
[
|
|
67
|
-
[
|
|
68
|
-
[
|
|
69
|
-
[
|
|
70
|
-
[
|
|
71
|
-
[
|
|
72
|
-
[
|
|
73
|
-
[
|
|
74
|
-
[
|
|
75
|
-
[
|
|
76
|
-
[rotate]: https://github.
|
|
72
|
+
[get]: https://nodef.github.io/extra-bit/modules.html#get
|
|
73
|
+
[getAs]: https://nodef.github.io/extra-bit/modules.html#getAs
|
|
74
|
+
[set]: https://nodef.github.io/extra-bit/modules.html#set
|
|
75
|
+
[setAs]: https://nodef.github.io/extra-bit/modules.html#setAs
|
|
76
|
+
[toggle]: https://nodef.github.io/extra-bit/modules.html#toggle
|
|
77
|
+
[toggleAs]: https://nodef.github.io/extra-bit/modules.html#toggleAs
|
|
78
|
+
[swap]: https://nodef.github.io/extra-bit/modules.html#swap
|
|
79
|
+
[scan]: https://nodef.github.io/extra-bit/modules.html#scan
|
|
80
|
+
[scanReverse]: https://nodef.github.io/extra-bit/modules.html#scanReverse
|
|
81
|
+
[count]: https://nodef.github.io/extra-bit/modules.html#count
|
|
82
|
+
[parity]: https://nodef.github.io/extra-bit/modules.html#parity
|
|
83
|
+
[rotate]: https://nodef.github.io/extra-bit/modules.html#rotate
|
|
84
|
+
[reverse]: https://nodef.github.io/extra-bit/modules.html#reverse
|
|
85
|
+
[merge]: https://nodef.github.io/extra-bit/modules.html#merge
|
|
86
|
+
[interleave]: https://nodef.github.io/extra-bit/modules.html#interleave
|
|
87
|
+
[signExtend]: https://nodef.github.io/extra-bit/modules.html#signExtend
|
package/index.d.ts
CHANGED
|
@@ -1,298 +1,134 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Get a bit.
|
|
3
|
+
* @param x an int32
|
|
4
|
+
* @param i bit index
|
|
5
|
+
* @param w bit width (1)
|
|
6
|
+
*/
|
|
7
|
+
declare function get(x: number, i: number, w?: number): number;
|
|
8
|
+
//# sourceMappingURL=get.d.ts.map
|
|
2
9
|
|
|
3
10
|
/**
|
|
4
|
-
*
|
|
5
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/count)
|
|
11
|
+
* Get bits as per mask.
|
|
6
12
|
* @param x an int32
|
|
7
|
-
* @
|
|
8
|
-
* ```javascript
|
|
9
|
-
* count(7);
|
|
10
|
-
* // 3 (111 ⇒ 3)
|
|
11
|
-
*
|
|
12
|
-
* count(12);
|
|
13
|
-
* // 2 (1100 ⇒ 2)
|
|
14
|
-
*
|
|
15
|
-
* count(63);
|
|
16
|
-
* // 6 (111111 ⇒ 6)
|
|
17
|
-
* ```
|
|
13
|
+
* @param m bit mask
|
|
18
14
|
*/
|
|
19
|
-
|
|
15
|
+
declare function getAs(x: number, m: number): number;
|
|
16
|
+
//# sourceMappingURL=getAs.d.ts.map
|
|
17
|
+
|
|
20
18
|
/**
|
|
21
|
-
*
|
|
22
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/get)
|
|
19
|
+
* Set a bit.
|
|
23
20
|
* @param x an int32
|
|
24
21
|
* @param i bit index
|
|
22
|
+
* @param v bit value (1)
|
|
25
23
|
* @param w bit width (1)
|
|
26
|
-
* @example
|
|
27
|
-
* ```javascript
|
|
28
|
-
* get(6, 0);
|
|
29
|
-
* // 0 (110,0 ⇒ 0)
|
|
30
|
-
*
|
|
31
|
-
* get(6, 1);
|
|
32
|
-
* // 1 (110,1 ⇒ 1)
|
|
33
|
-
*
|
|
34
|
-
* get(6, 2);
|
|
35
|
-
* // 1 (110,2 ⇒ 1)
|
|
36
|
-
* ```
|
|
37
24
|
*/
|
|
38
|
-
|
|
25
|
+
declare function set(x: number, i: number, v?: number, w?: number): number;
|
|
26
|
+
//# sourceMappingURL=set.d.ts.map
|
|
27
|
+
|
|
39
28
|
/**
|
|
40
|
-
*
|
|
41
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/getAs)
|
|
29
|
+
* Set bits as per mask.
|
|
42
30
|
* @param x an int32
|
|
43
31
|
* @param m bit mask
|
|
44
|
-
* @
|
|
45
|
-
* ```javascript
|
|
46
|
-
* getAs(6, 4);
|
|
47
|
-
* // 4 (110,100 ⇒ 100)
|
|
48
|
-
*
|
|
49
|
-
* getAs(6, 7);
|
|
50
|
-
* // 6 (110,111 ⇒ 110)
|
|
51
|
-
*
|
|
52
|
-
* getAs(6, 5);
|
|
53
|
-
* // 4 (110,101 ⇒ 100)
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
export function getAs(x: number, m: number): number;
|
|
57
|
-
/**
|
|
58
|
-
* Interleaves bits of two int16s.
|
|
59
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/interleave)
|
|
60
|
-
* @param x first int16
|
|
61
|
-
* @param y second int16
|
|
62
|
-
* @returns int32
|
|
63
|
-
* @example
|
|
64
|
-
* ```javascript
|
|
65
|
-
* interleave(0x0000, 0xFFFF);
|
|
66
|
-
* // 1431655765 (0x55555555)
|
|
67
|
-
*
|
|
68
|
-
* interleave(0x1234, 0x1234);
|
|
69
|
-
* // 51121968 (0x030C0F30)
|
|
70
|
-
*
|
|
71
|
-
* interleave(0x1234, 0x4321);
|
|
72
|
-
* // 302845473 (0x120D0E21)
|
|
73
|
-
* ```
|
|
32
|
+
* @param f bit value (1)
|
|
74
33
|
*/
|
|
75
|
-
|
|
34
|
+
declare function setAs(x: number, m: number, f?: number): number;
|
|
35
|
+
//# sourceMappingURL=setAs.d.ts.map
|
|
36
|
+
|
|
76
37
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* @param
|
|
80
|
-
* @param
|
|
81
|
-
* @param m bit mask (0 ⇒ from x)
|
|
82
|
-
* @example
|
|
83
|
-
* ```javascript
|
|
84
|
-
* merge(0x12, 0x24, 0x0F);
|
|
85
|
-
* // 20 (0x14)
|
|
86
|
-
*
|
|
87
|
-
* merge(0x1234, 0xABCD, 0x0F0F);
|
|
88
|
-
* // 6973 (0x1B3D)
|
|
89
|
-
*
|
|
90
|
-
* merge(0xAAAA, 0xBBBB, 0x3333);
|
|
91
|
-
* // 48059 (0xBBBB)
|
|
92
|
-
* ```
|
|
38
|
+
* Toggle a bit.
|
|
39
|
+
* @param x an int32
|
|
40
|
+
* @param i bit index
|
|
41
|
+
* @param w bit width (1)
|
|
93
42
|
*/
|
|
94
|
-
|
|
43
|
+
declare function toggle(x: number, i: number, w?: number): number;
|
|
44
|
+
//# sourceMappingURL=toggle.d.ts.map
|
|
45
|
+
|
|
95
46
|
/**
|
|
96
|
-
*
|
|
97
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/parity)
|
|
47
|
+
* Toggle bits as per mask.
|
|
98
48
|
* @param x an int32
|
|
99
|
-
* @param
|
|
100
|
-
* @example
|
|
101
|
-
* ```javascript
|
|
102
|
-
* parity(7);
|
|
103
|
-
* // 1 (1,1,1 ⇒ 1)
|
|
104
|
-
*
|
|
105
|
-
* parity(5);
|
|
106
|
-
* // 0 (1,0,1 ⇒ 0)
|
|
107
|
-
*
|
|
108
|
-
* parity(8, 2);
|
|
109
|
-
* // 2 (10,00 ⇒ 10)
|
|
110
|
-
*
|
|
111
|
-
* parity(63, 4);
|
|
112
|
-
* // 12 (11,1111 ⇒ 1100)
|
|
113
|
-
* ```
|
|
49
|
+
* @param m bit mask
|
|
114
50
|
*/
|
|
115
|
-
|
|
51
|
+
declare function toggleAs(x: number, m: number): number;
|
|
52
|
+
//# sourceMappingURL=toggleAs.d.ts.map
|
|
53
|
+
|
|
116
54
|
/**
|
|
117
|
-
*
|
|
118
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/reverse)
|
|
55
|
+
* Swap bit sequences.
|
|
119
56
|
* @param x an int32
|
|
120
|
-
* @
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
* // 65535 (0x0000FFFF)
|
|
124
|
-
*
|
|
125
|
-
* reverse(0x00AABBCC);
|
|
126
|
-
* // 870143232 (0x33DD5500)
|
|
127
|
-
*
|
|
128
|
-
* reverse(0x1234);
|
|
129
|
-
* // 742916096 (0x2C480000)
|
|
130
|
-
* ```
|
|
57
|
+
* @param i first bit index
|
|
58
|
+
* @param j second bit index
|
|
59
|
+
* @param w bit width (1)
|
|
131
60
|
*/
|
|
132
|
-
|
|
61
|
+
declare function swap(x: number, i: number, j: number, w?: number): number;
|
|
62
|
+
//# sourceMappingURL=swap.d.ts.map
|
|
63
|
+
|
|
133
64
|
/**
|
|
134
|
-
*
|
|
135
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/rotate)
|
|
65
|
+
* Find index of first set bit from LSB.
|
|
136
66
|
* @param x an int32
|
|
137
|
-
* @param n rotate amount (+ve: left, -ve: right)
|
|
138
|
-
* @example
|
|
139
|
-
* ```javascript
|
|
140
|
-
* rotate(0x11112222, 4);
|
|
141
|
-
* // 286401057 (0x11122221)
|
|
142
|
-
*
|
|
143
|
-
* rotate(0x11112222, -4);
|
|
144
|
-
* // 554766882 (0x21111222)
|
|
145
|
-
* ```
|
|
146
67
|
*/
|
|
147
|
-
|
|
68
|
+
declare function scan(x: number): number;
|
|
69
|
+
//# sourceMappingURL=scan.d.ts.map
|
|
70
|
+
|
|
148
71
|
/**
|
|
149
|
-
*
|
|
150
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/scan)
|
|
72
|
+
* Find index of first set bit from MSB.
|
|
151
73
|
* @param x an int32
|
|
152
|
-
* @example
|
|
153
|
-
* ```javascript
|
|
154
|
-
* scan(7);
|
|
155
|
-
* // 0 (111 ⇒ 0)
|
|
156
|
-
*
|
|
157
|
-
* scan(12);
|
|
158
|
-
* // 2 (1100 ⇒ 2)
|
|
159
|
-
*
|
|
160
|
-
* scan(64);
|
|
161
|
-
* // 6 (1000000 ⇒ 6)
|
|
162
|
-
* ```
|
|
163
74
|
*/
|
|
164
|
-
|
|
75
|
+
declare function scanReverse(x: number): number;
|
|
76
|
+
//# sourceMappingURL=scanReverse.d.ts.map
|
|
77
|
+
|
|
165
78
|
/**
|
|
166
|
-
*
|
|
167
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/scanReverse)
|
|
79
|
+
* Count bits set.
|
|
168
80
|
* @param x an int32
|
|
169
|
-
* @example
|
|
170
|
-
* ```javascript
|
|
171
|
-
* scanReverse(13);
|
|
172
|
-
* // 13 (1101 ⇒ 3)
|
|
173
|
-
*
|
|
174
|
-
* scanReverse(5);
|
|
175
|
-
* // 2 (101 ⇒ 2)
|
|
176
|
-
*
|
|
177
|
-
* scanReverse(1);
|
|
178
|
-
* // 0 (1 ⇒ 0)
|
|
179
|
-
* ```
|
|
180
81
|
*/
|
|
181
|
-
|
|
82
|
+
declare function count(x: number): number;
|
|
83
|
+
//# sourceMappingURL=count.d.ts.map
|
|
84
|
+
|
|
182
85
|
/**
|
|
183
|
-
*
|
|
184
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/set)
|
|
86
|
+
* Find n-bit parity.
|
|
185
87
|
* @param x an int32
|
|
186
|
-
* @param
|
|
187
|
-
* @param v bit value (1)
|
|
188
|
-
* @param w bit width (1)
|
|
189
|
-
* @example
|
|
190
|
-
* ```javascript
|
|
191
|
-
* set(6, 0, 1);
|
|
192
|
-
* // 7 (110,0,1 ⇒ 111)
|
|
193
|
-
*
|
|
194
|
-
* set(6, 2, 1);
|
|
195
|
-
* // 6 (110,2,1 ⇒ 110)
|
|
196
|
-
*
|
|
197
|
-
* set(6, 2, 0);
|
|
198
|
-
* // 2 (110,2,0 ⇒ 010)
|
|
199
|
-
* ```
|
|
88
|
+
* @param n number of bits (1)
|
|
200
89
|
*/
|
|
201
|
-
|
|
90
|
+
declare function parity(x: number, n?: number): number;
|
|
91
|
+
//# sourceMappingURL=parity.d.ts.map
|
|
92
|
+
|
|
202
93
|
/**
|
|
203
|
-
*
|
|
204
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/setAs)
|
|
94
|
+
* Rotate bits.
|
|
205
95
|
* @param x an int32
|
|
206
|
-
* @param
|
|
207
|
-
* @param f bit value (1)
|
|
208
|
-
* @example
|
|
209
|
-
* ```javascript
|
|
210
|
-
* setAs(8, 2, 1);
|
|
211
|
-
* // 10 (0x8 set 0x2 ⇒ 0xA)
|
|
212
|
-
*
|
|
213
|
-
* setAs(15, 3, 0);
|
|
214
|
-
* // 12 (0xF clear 0x3 ⇒ 0xC)
|
|
215
|
-
*
|
|
216
|
-
* setAs(0x1234, 0x430, 1);
|
|
217
|
-
* // 5684 (0x1234 set 0x430 ⇒ 0x1634)
|
|
218
|
-
* ```
|
|
96
|
+
* @param n rotate amount (+ve: left, -ve: right)
|
|
219
97
|
*/
|
|
220
|
-
|
|
98
|
+
declare function rotate(x: number, n?: number): number;
|
|
99
|
+
//# sourceMappingURL=rotate.d.ts.map
|
|
100
|
+
|
|
221
101
|
/**
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* @param x variable bit-width integer
|
|
225
|
-
* @param w bit width (32)
|
|
226
|
-
* @example
|
|
227
|
-
* ```javascript
|
|
228
|
-
* signExtend(15, 4);
|
|
229
|
-
* // -1 (1111 ⇒ -1)
|
|
230
|
-
*
|
|
231
|
-
* signExtend(3, 3);
|
|
232
|
-
* // 3 (011 ⇒ 3)
|
|
233
|
-
*
|
|
234
|
-
* signExtend(4, 3);
|
|
235
|
-
* // -4 (100 ⇒ -4)
|
|
236
|
-
* ```
|
|
102
|
+
* Reverse all bits.
|
|
103
|
+
* @param x an int32
|
|
237
104
|
*/
|
|
238
|
-
|
|
105
|
+
declare function reverse(x: number): number;
|
|
106
|
+
//# sourceMappingURL=reverse.d.ts.map
|
|
107
|
+
|
|
239
108
|
/**
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
* @param
|
|
243
|
-
* @param
|
|
244
|
-
* @param j second bit index
|
|
245
|
-
* @param w bit width (1)
|
|
246
|
-
* @example
|
|
247
|
-
* ```javascript
|
|
248
|
-
* swap(6, 1, 0);
|
|
249
|
-
* // 5 (110 ⇒ 101)
|
|
250
|
-
*
|
|
251
|
-
* swap(0x1234, 8, 4, 4);
|
|
252
|
-
* // 4900 (0x1234 ⇒ 0x1324)
|
|
253
|
-
*
|
|
254
|
-
* swap(0x4AAB, 8, 0, 8);
|
|
255
|
-
* // 43850 (0x4AAB ⇒ 0xAB4A)
|
|
256
|
-
* ```
|
|
109
|
+
* Merge bits as per mask.
|
|
110
|
+
* @param x first int32
|
|
111
|
+
* @param y second int32
|
|
112
|
+
* @param m bit mask (0 ⇒ from x)
|
|
257
113
|
*/
|
|
258
|
-
|
|
114
|
+
declare function merge(x: number, y: number, m: number): number;
|
|
115
|
+
//# sourceMappingURL=merge.d.ts.map
|
|
116
|
+
|
|
259
117
|
/**
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
* @param
|
|
263
|
-
* @
|
|
264
|
-
* @param w bit width (1)
|
|
265
|
-
* @example
|
|
266
|
-
* ```javascript
|
|
267
|
-
* toggle(6, 0);
|
|
268
|
-
* // 7 (110,0 ⇒ 111)
|
|
269
|
-
*
|
|
270
|
-
* toggle(6, 1);
|
|
271
|
-
* // 4 (110,1 ⇒ 100)
|
|
272
|
-
*
|
|
273
|
-
* toggle(6, 2);
|
|
274
|
-
* // 2 (110,2 ⇒ 010)
|
|
275
|
-
* ```
|
|
118
|
+
* Interleave bits of two int16s.
|
|
119
|
+
* @param x first int16
|
|
120
|
+
* @param y second int16
|
|
121
|
+
* @returns int32
|
|
276
122
|
*/
|
|
277
|
-
|
|
123
|
+
declare function interleave(x: number, y: number): number;
|
|
124
|
+
//# sourceMappingURL=interleave.d.ts.map
|
|
125
|
+
|
|
278
126
|
/**
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
* @param
|
|
282
|
-
* @param m bit mask
|
|
283
|
-
* @example
|
|
284
|
-
* ```javascript
|
|
285
|
-
* toggleAs(6, 1);
|
|
286
|
-
* // 7 (110,000 ⇒ 111)
|
|
287
|
-
*
|
|
288
|
-
* toggleAs(6, 7);
|
|
289
|
-
* // 1 (110,111 ⇒ 001)
|
|
290
|
-
*
|
|
291
|
-
* toggleAs(6, 3);
|
|
292
|
-
* // 5 (110,011 ⇒ 101)
|
|
293
|
-
* ```
|
|
127
|
+
* Sign extend variable bit-width integer.
|
|
128
|
+
* @param x variable bit-width integer
|
|
129
|
+
* @param w bit width (32)
|
|
294
130
|
*/
|
|
295
|
-
|
|
131
|
+
declare function signExtend(x: number, w?: number): number;
|
|
132
|
+
//# sourceMappingURL=signExtend.d.ts.map
|
|
296
133
|
|
|
297
|
-
export {};
|
|
298
|
-
}
|
|
134
|
+
export { count, get, getAs, interleave, merge, parity, reverse, rotate, scan, scanReverse, set, setAs, signExtend, swap, toggle, toggleAs };
|
package/index.js
CHANGED
|
@@ -1,33 +1,71 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
|
|
2
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
-
|
|
4
|
-
x = x - ((x >>> 1) & 0x55555555);
|
|
5
|
-
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
|
|
6
|
-
return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
|
7
|
-
}
|
|
4
|
+
|
|
8
5
|
function ones(w) {
|
|
9
6
|
return (1 << w) - 1;
|
|
10
7
|
}
|
|
8
|
+
|
|
11
9
|
function get(x, i, w = 1) {
|
|
12
10
|
return (x >>> i) & ones(w);
|
|
13
11
|
}
|
|
12
|
+
|
|
14
13
|
function getAs(x, m) {
|
|
15
14
|
return x & m;
|
|
16
15
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
x = (x | (x << 2)) & 0x33333333;
|
|
21
|
-
x = (x | (x << 1)) & 0x55555555;
|
|
22
|
-
y = (y | (y << 8)) & 0x00FF00FF;
|
|
23
|
-
y = (y | (y << 4)) & 0x0F0F0F0F;
|
|
24
|
-
y = (y | (y << 2)) & 0x33333333;
|
|
25
|
-
y = (y | (y << 1)) & 0x55555555;
|
|
26
|
-
return y | (x << 1);
|
|
16
|
+
|
|
17
|
+
function set(x, i, v = 1, w = 1) {
|
|
18
|
+
return (x & ~(ones(w) << i)) | (v << i);
|
|
27
19
|
}
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
|
|
21
|
+
function setAs(x, m, f = 1) {
|
|
22
|
+
return (x & ~m) | (-f & m);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function toggle(x, i, w = 1) {
|
|
26
|
+
return x ^ (ones(w) << i);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function toggleAs(x, m) {
|
|
30
|
+
return x ^ m;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function swap(x, i, j, w = 1) {
|
|
34
|
+
var t = ((x >>> i) ^ (x >>> j)) & ones(w);
|
|
35
|
+
return x ^ ((t << i) | (t << j));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const MOD37_POS32 = [
|
|
39
|
+
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
|
|
40
|
+
25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
|
|
41
|
+
];
|
|
42
|
+
var MOD37_POS32$1 = MOD37_POS32;
|
|
43
|
+
|
|
44
|
+
function scan(x) {
|
|
45
|
+
return MOD37_POS32$1[(-x & x) % 37];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const DEBRUIJN_POS32 = [
|
|
49
|
+
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
50
|
+
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
51
|
+
];
|
|
52
|
+
var DEBRUIJN_POS32$1 = DEBRUIJN_POS32;
|
|
53
|
+
|
|
54
|
+
function scanReverse(x) {
|
|
55
|
+
x |= x >>> 1;
|
|
56
|
+
x |= x >>> 2;
|
|
57
|
+
x |= x >>> 4;
|
|
58
|
+
x |= x >>> 8;
|
|
59
|
+
x |= x >>> 16;
|
|
60
|
+
return DEBRUIJN_POS32$1[(x * 0x07C4ACDD) >>> 27];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function count(x) {
|
|
64
|
+
x = x - ((x >>> 1) & 0x55555555);
|
|
65
|
+
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
|
|
66
|
+
return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
|
30
67
|
}
|
|
68
|
+
|
|
31
69
|
function parity1(x) {
|
|
32
70
|
x ^= x >>> 16;
|
|
33
71
|
x ^= x >>> 8;
|
|
@@ -45,6 +83,11 @@ function parity(x, n = 1) {
|
|
|
45
83
|
}
|
|
46
84
|
return a;
|
|
47
85
|
}
|
|
86
|
+
|
|
87
|
+
function rotate(x, n = 0) {
|
|
88
|
+
return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
|
|
89
|
+
}
|
|
90
|
+
|
|
48
91
|
function reverse(x) {
|
|
49
92
|
x = ((x >>> 1) & 0x55555555) | ((x & 0x55555555) << 1);
|
|
50
93
|
x = ((x >>> 2) & 0x33333333) | ((x & 0x33333333) << 2);
|
|
@@ -52,48 +95,28 @@ function reverse(x) {
|
|
|
52
95
|
x = ((x >>> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
|
|
53
96
|
return (x >>> 16) | (x << 16);
|
|
54
97
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const MOD37_POS32 = [
|
|
59
|
-
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
|
|
60
|
-
25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
|
|
61
|
-
];
|
|
62
|
-
function scan(x) {
|
|
63
|
-
return MOD37_POS32[(-x & x) % 37];
|
|
64
|
-
}
|
|
65
|
-
const DEBRUIJN_POS32 = [
|
|
66
|
-
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
67
|
-
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
68
|
-
];
|
|
69
|
-
function scanReverse(x) {
|
|
70
|
-
x |= x >>> 1;
|
|
71
|
-
x |= x >>> 2;
|
|
72
|
-
x |= x >>> 4;
|
|
73
|
-
x |= x >>> 8;
|
|
74
|
-
x |= x >>> 16;
|
|
75
|
-
return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
|
|
76
|
-
}
|
|
77
|
-
function set(x, i, v = 1, w = 1) {
|
|
78
|
-
return (x & ~(ones(w) << i)) | (v << i);
|
|
98
|
+
|
|
99
|
+
function merge(x, y, m) {
|
|
100
|
+
return x ^ ((x ^ y) & m);
|
|
79
101
|
}
|
|
80
|
-
|
|
81
|
-
|
|
102
|
+
|
|
103
|
+
function interleave(x, y) {
|
|
104
|
+
x = (x | (x << 8)) & 0x00FF00FF;
|
|
105
|
+
x = (x | (x << 4)) & 0x0F0F0F0F;
|
|
106
|
+
x = (x | (x << 2)) & 0x33333333;
|
|
107
|
+
x = (x | (x << 1)) & 0x55555555;
|
|
108
|
+
y = (y | (y << 8)) & 0x00FF00FF;
|
|
109
|
+
y = (y | (y << 4)) & 0x0F0F0F0F;
|
|
110
|
+
y = (y | (y << 2)) & 0x33333333;
|
|
111
|
+
y = (y | (y << 1)) & 0x55555555;
|
|
112
|
+
return y | (x << 1);
|
|
82
113
|
}
|
|
114
|
+
|
|
83
115
|
function signExtend(x, w = 32) {
|
|
84
116
|
w = 32 - w;
|
|
85
117
|
return (x << w) >> w;
|
|
86
118
|
}
|
|
87
|
-
|
|
88
|
-
var t = ((x >>> i) ^ (x >>> j)) & ones(w);
|
|
89
|
-
return x ^ ((t << i) | (t << j));
|
|
90
|
-
}
|
|
91
|
-
function toggle(x, i, w = 1) {
|
|
92
|
-
return x ^ (ones(w) << i);
|
|
93
|
-
}
|
|
94
|
-
function toggleAs(x, m) {
|
|
95
|
-
return x ^ m;
|
|
96
|
-
}
|
|
119
|
+
|
|
97
120
|
exports.count = count;
|
|
98
121
|
exports.get = get;
|
|
99
122
|
exports.getAs = getAs;
|
package/index.mjs
CHANGED
|
@@ -1,31 +1,67 @@
|
|
|
1
|
-
function count(x) {
|
|
2
|
-
x = x - ((x >>> 1) & 0x55555555);
|
|
3
|
-
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
|
|
4
|
-
return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
|
5
|
-
}
|
|
6
1
|
function ones(w) {
|
|
7
2
|
return (1 << w) - 1;
|
|
8
3
|
}
|
|
4
|
+
|
|
9
5
|
function get(x, i, w = 1) {
|
|
10
6
|
return (x >>> i) & ones(w);
|
|
11
7
|
}
|
|
8
|
+
|
|
12
9
|
function getAs(x, m) {
|
|
13
10
|
return x & m;
|
|
14
11
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
x = (x | (x << 2)) & 0x33333333;
|
|
19
|
-
x = (x | (x << 1)) & 0x55555555;
|
|
20
|
-
y = (y | (y << 8)) & 0x00FF00FF;
|
|
21
|
-
y = (y | (y << 4)) & 0x0F0F0F0F;
|
|
22
|
-
y = (y | (y << 2)) & 0x33333333;
|
|
23
|
-
y = (y | (y << 1)) & 0x55555555;
|
|
24
|
-
return y | (x << 1);
|
|
12
|
+
|
|
13
|
+
function set(x, i, v = 1, w = 1) {
|
|
14
|
+
return (x & ~(ones(w) << i)) | (v << i);
|
|
25
15
|
}
|
|
26
|
-
|
|
27
|
-
|
|
16
|
+
|
|
17
|
+
function setAs(x, m, f = 1) {
|
|
18
|
+
return (x & ~m) | (-f & m);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function toggle(x, i, w = 1) {
|
|
22
|
+
return x ^ (ones(w) << i);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function toggleAs(x, m) {
|
|
26
|
+
return x ^ m;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function swap(x, i, j, w = 1) {
|
|
30
|
+
var t = ((x >>> i) ^ (x >>> j)) & ones(w);
|
|
31
|
+
return x ^ ((t << i) | (t << j));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const MOD37_POS32 = [
|
|
35
|
+
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
|
|
36
|
+
25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
|
|
37
|
+
];
|
|
38
|
+
var MOD37_POS32$1 = MOD37_POS32;
|
|
39
|
+
|
|
40
|
+
function scan(x) {
|
|
41
|
+
return MOD37_POS32$1[(-x & x) % 37];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const DEBRUIJN_POS32 = [
|
|
45
|
+
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
46
|
+
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
47
|
+
];
|
|
48
|
+
var DEBRUIJN_POS32$1 = DEBRUIJN_POS32;
|
|
49
|
+
|
|
50
|
+
function scanReverse(x) {
|
|
51
|
+
x |= x >>> 1;
|
|
52
|
+
x |= x >>> 2;
|
|
53
|
+
x |= x >>> 4;
|
|
54
|
+
x |= x >>> 8;
|
|
55
|
+
x |= x >>> 16;
|
|
56
|
+
return DEBRUIJN_POS32$1[(x * 0x07C4ACDD) >>> 27];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function count(x) {
|
|
60
|
+
x = x - ((x >>> 1) & 0x55555555);
|
|
61
|
+
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
|
|
62
|
+
return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
|
28
63
|
}
|
|
64
|
+
|
|
29
65
|
function parity1(x) {
|
|
30
66
|
x ^= x >>> 16;
|
|
31
67
|
x ^= x >>> 8;
|
|
@@ -43,6 +79,11 @@ function parity(x, n = 1) {
|
|
|
43
79
|
}
|
|
44
80
|
return a;
|
|
45
81
|
}
|
|
82
|
+
|
|
83
|
+
function rotate(x, n = 0) {
|
|
84
|
+
return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
|
|
85
|
+
}
|
|
86
|
+
|
|
46
87
|
function reverse(x) {
|
|
47
88
|
x = ((x >>> 1) & 0x55555555) | ((x & 0x55555555) << 1);
|
|
48
89
|
x = ((x >>> 2) & 0x33333333) | ((x & 0x33333333) << 2);
|
|
@@ -50,46 +91,26 @@ function reverse(x) {
|
|
|
50
91
|
x = ((x >>> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
|
|
51
92
|
return (x >>> 16) | (x << 16);
|
|
52
93
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const MOD37_POS32 = [
|
|
57
|
-
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
|
|
58
|
-
25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
|
|
59
|
-
];
|
|
60
|
-
function scan(x) {
|
|
61
|
-
return MOD37_POS32[(-x & x) % 37];
|
|
62
|
-
}
|
|
63
|
-
const DEBRUIJN_POS32 = [
|
|
64
|
-
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
|
|
65
|
-
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
|
|
66
|
-
];
|
|
67
|
-
function scanReverse(x) {
|
|
68
|
-
x |= x >>> 1;
|
|
69
|
-
x |= x >>> 2;
|
|
70
|
-
x |= x >>> 4;
|
|
71
|
-
x |= x >>> 8;
|
|
72
|
-
x |= x >>> 16;
|
|
73
|
-
return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
|
|
74
|
-
}
|
|
75
|
-
function set(x, i, v = 1, w = 1) {
|
|
76
|
-
return (x & ~(ones(w) << i)) | (v << i);
|
|
94
|
+
|
|
95
|
+
function merge(x, y, m) {
|
|
96
|
+
return x ^ ((x ^ y) & m);
|
|
77
97
|
}
|
|
78
|
-
|
|
79
|
-
|
|
98
|
+
|
|
99
|
+
function interleave(x, y) {
|
|
100
|
+
x = (x | (x << 8)) & 0x00FF00FF;
|
|
101
|
+
x = (x | (x << 4)) & 0x0F0F0F0F;
|
|
102
|
+
x = (x | (x << 2)) & 0x33333333;
|
|
103
|
+
x = (x | (x << 1)) & 0x55555555;
|
|
104
|
+
y = (y | (y << 8)) & 0x00FF00FF;
|
|
105
|
+
y = (y | (y << 4)) & 0x0F0F0F0F;
|
|
106
|
+
y = (y | (y << 2)) & 0x33333333;
|
|
107
|
+
y = (y | (y << 1)) & 0x55555555;
|
|
108
|
+
return y | (x << 1);
|
|
80
109
|
}
|
|
110
|
+
|
|
81
111
|
function signExtend(x, w = 32) {
|
|
82
112
|
w = 32 - w;
|
|
83
113
|
return (x << w) >> w;
|
|
84
114
|
}
|
|
85
|
-
|
|
86
|
-
var t = ((x >>> i) ^ (x >>> j)) & ones(w);
|
|
87
|
-
return x ^ ((t << i) | (t << j));
|
|
88
|
-
}
|
|
89
|
-
function toggle(x, i, w = 1) {
|
|
90
|
-
return x ^ (ones(w) << i);
|
|
91
|
-
}
|
|
92
|
-
function toggleAs(x, m) {
|
|
93
|
-
return x ^ m;
|
|
94
|
-
}
|
|
115
|
+
|
|
95
116
|
export { count, get, getAs, interleave, merge, parity, reverse, rotate, scan, scanReverse, set, setAs, signExtend, swap, toggle, toggleAs };
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extra-bit",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "The bit is a basic unit of information in information theory, computing.",
|
|
5
|
+
"sideEffects": false,
|
|
5
6
|
"main": "index.js",
|
|
6
7
|
"module": "index.mjs",
|
|
7
8
|
"exports": {
|
|
@@ -10,13 +11,8 @@
|
|
|
10
11
|
},
|
|
11
12
|
"scripts": {
|
|
12
13
|
"test": "exit",
|
|
13
|
-
"build": "
|
|
14
|
-
"deploy": "
|
|
15
|
-
},
|
|
16
|
-
"runkitExampleFilename": "example.js",
|
|
17
|
-
"repository": {
|
|
18
|
-
"type": "git",
|
|
19
|
-
"url": "git+https://github.com/nodef/extra-bit.git"
|
|
14
|
+
"build": "node ./build",
|
|
15
|
+
"deploy": "node ./build deploy"
|
|
20
16
|
},
|
|
21
17
|
"keywords": [
|
|
22
18
|
"extra",
|
|
@@ -40,11 +36,20 @@
|
|
|
40
36
|
],
|
|
41
37
|
"author": "wolfram77@gmail.com",
|
|
42
38
|
"license": "MIT",
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/nodef/extra-bit.git"
|
|
42
|
+
},
|
|
43
43
|
"bugs": {
|
|
44
44
|
"url": "https://github.com/nodef/extra-bit/issues"
|
|
45
45
|
},
|
|
46
46
|
"homepage": "https://github.com/nodef/extra-bit#readme",
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"
|
|
48
|
+
"@rollup/plugin-commonjs": "^21.0.3",
|
|
49
|
+
"@rollup/plugin-node-resolve": "^13.2.0",
|
|
50
|
+
"@types/node": "^17.0.23",
|
|
51
|
+
"extra-build": "^2.0.31",
|
|
52
|
+
"rollup-plugin-cleanup": "^3.2.1",
|
|
53
|
+
"rollup-plugin-dts": "^4.2.1"
|
|
49
54
|
}
|
|
50
55
|
}
|