extra-bit 1.0.64 → 2.0.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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020-21 Subhajit Sahu
3
+ Copyright (c) 2020-22 Subhajit Sahu
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/index.d.ts CHANGED
@@ -1,295 +1,134 @@
1
- declare module "extra-bit" {
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
- * Counts bits set.
5
- * [📘](https://github.com/nodef/extra-bit/wiki/count)
11
+ * Get bits as per mask.
6
12
  * @param x an int32
7
- * @example
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
- export function count(x: number): number;
15
+ declare function getAs(x: number, m: number): number;
16
+ //# sourceMappingURL=getAs.d.ts.map
17
+
20
18
  /**
21
- * Gets a bit.
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
25
- * @example
26
- * ```javascript
27
- * get(6, 0);
28
- * // 0 (110,0 ⇒ 0)
29
- *
30
- * get(6, 1);
31
- * // 1 (110,1 ⇒ 1)
32
- *
33
- * get(6, 2);
34
- * // 1 (110,2 ⇒ 1)
35
- * ```
22
+ * @param v bit value (1)
23
+ * @param w bit width (1)
36
24
  */
37
- export function get(x: number, i: number): number;
25
+ declare function set(x: number, i: number, v?: number, w?: number): number;
26
+ //# sourceMappingURL=set.d.ts.map
27
+
38
28
  /**
39
- * Gets bits as per mask.
40
- * [📘](https://github.com/nodef/extra-bit/wiki/getAs)
29
+ * Set bits as per mask.
41
30
  * @param x an int32
42
31
  * @param m bit mask
43
- * @example
44
- * ```javascript
45
- * getAs(6, 4);
46
- * // 4 (110,100 ⇒ 100)
47
- *
48
- * getAs(6, 7);
49
- * // 6 (110,111 ⇒ 110)
50
- *
51
- * getAs(6, 5);
52
- * // 4 (110,101 ⇒ 100)
53
- * ```
54
- */
55
- export function getAs(x: number, m: number): number;
56
- /**
57
- * Interleaves bits of two int16s.
58
- * [📘](https://github.com/nodef/extra-bit/wiki/interleave)
59
- * @param x first int16
60
- * @param y second int16
61
- * @returns int32
62
- * @example
63
- * ```javascript
64
- * interleave(0x0000, 0xFFFF);
65
- * // 1431655765 (0x55555555)
66
- *
67
- * interleave(0x1234, 0x1234);
68
- * // 51121968 (0x030C0F30)
69
- *
70
- * interleave(0x1234, 0x4321);
71
- * // 302845473 (0x120D0E21)
72
- * ```
32
+ * @param f bit value (1)
73
33
  */
74
- export function interleave(x: number, y: number): number;
34
+ declare function setAs(x: number, m: number, f?: number): number;
35
+ //# sourceMappingURL=setAs.d.ts.map
36
+
75
37
  /**
76
- * Merges bits as per mask.
77
- * [📘](https://github.com/nodef/extra-bit/wiki/merge)
78
- * @param x first int32
79
- * @param y second int32
80
- * @param m bit mask (0 ⇒ from x)
81
- * @example
82
- * ```javascript
83
- * merge(0x12, 0x24, 0x0F);
84
- * // 20 (0x14)
85
- *
86
- * merge(0x1234, 0xABCD, 0x0F0F);
87
- * // 6973 (0x1B3D)
88
- *
89
- * merge(0xAAAA, 0xBBBB, 0x3333);
90
- * // 48059 (0xBBBB)
91
- * ```
38
+ * Toggle a bit.
39
+ * @param x an int32
40
+ * @param i bit index
41
+ * @param w bit width (1)
92
42
  */
93
- export function merge(x: number, y: number, m: number): number;
43
+ declare function toggle(x: number, i: number, w?: number): number;
44
+ //# sourceMappingURL=toggle.d.ts.map
45
+
94
46
  /**
95
- * Finds n-bit parity.
96
- * [📘](https://github.com/nodef/extra-bit/wiki/parity)
47
+ * Toggle bits as per mask.
97
48
  * @param x an int32
98
- * @param n number of bits (1)
99
- * @example
100
- * ```javascript
101
- * parity(7);
102
- * // 1 (1,1,1 ⇒ 1)
103
- *
104
- * parity(5);
105
- * // 0 (1,0,1 ⇒ 0)
106
- *
107
- * parity(8, 2);
108
- * // 2 (10,00 ⇒ 10)
109
- *
110
- * parity(63, 4);
111
- * // 12 (11,1111 ⇒ 1100)
112
- * ```
49
+ * @param m bit mask
113
50
  */
114
- export function parity(x: number, n?: number): number;
51
+ declare function toggleAs(x: number, m: number): number;
52
+ //# sourceMappingURL=toggleAs.d.ts.map
53
+
115
54
  /**
116
- * Reverses all bits.
117
- * [📘](https://github.com/nodef/extra-bit/wiki/reverse)
55
+ * Swap bit sequences.
118
56
  * @param x an int32
119
- * @example
120
- * ```javascript
121
- * reverse(0xFFFF0000);
122
- * // 65535 (0x0000FFFF)
123
- *
124
- * reverse(0x00AABBCC);
125
- * // 870143232 (0x33DD5500)
126
- *
127
- * reverse(0x1234);
128
- * // 742916096 (0x2C480000)
129
- * ```
57
+ * @param i first bit index
58
+ * @param j second bit index
59
+ * @param w bit width (1)
130
60
  */
131
- export function reverse(x: number): number;
61
+ declare function swap(x: number, i: number, j: number, w?: number): number;
62
+ //# sourceMappingURL=swap.d.ts.map
63
+
132
64
  /**
133
- * Rotates bits.
134
- * [📘](https://github.com/nodef/extra-bit/wiki/rotate)
65
+ * Find index of first set bit from LSB.
135
66
  * @param x an int32
136
- * @param n rotate amount (+ve: left, -ve: right)
137
- * @example
138
- * ```javascript
139
- * rotate(0x11112222, 4);
140
- * // 286401057 (0x11122221)
141
- *
142
- * rotate(0x11112222, -4);
143
- * // 554766882 (0x21111222)
144
- * ```
145
67
  */
146
- export function rotate(x: number, n?: number): number;
68
+ declare function scan(x: number): number;
69
+ //# sourceMappingURL=scan.d.ts.map
70
+
147
71
  /**
148
- * Finds index of first set bit from LSB.
149
- * [📘](https://github.com/nodef/extra-bit/wiki/scan)
72
+ * Find index of first set bit from MSB.
150
73
  * @param x an int32
151
- * @example
152
- * ```javascript
153
- * scan(7);
154
- * // 0 (111 ⇒ 0)
155
- *
156
- * scan(12);
157
- * // 2 (1100 ⇒ 2)
158
- *
159
- * scan(64);
160
- * // 6 (1000000 ⇒ 6)
161
- * ```
162
74
  */
163
- export function scan(x: number): number;
75
+ declare function scanReverse(x: number): number;
76
+ //# sourceMappingURL=scanReverse.d.ts.map
77
+
164
78
  /**
165
- * Finds index of first set bit from MSB.
166
- * [📘](https://github.com/nodef/extra-bit/wiki/scanReverse)
79
+ * Count bits set.
167
80
  * @param x an int32
168
- * @example
169
- * ```javascript
170
- * scanReverse(13);
171
- * // 13 (1101 ⇒ 3)
172
- *
173
- * scanReverse(5);
174
- * // 2 (101 ⇒ 2)
175
- *
176
- * scanReverse(1);
177
- * // 0 (1 ⇒ 0)
178
- * ```
179
81
  */
180
- export function scanReverse(x: number): number;
82
+ declare function count(x: number): number;
83
+ //# sourceMappingURL=count.d.ts.map
84
+
181
85
  /**
182
- * Sets a bit.
183
- * [📘](https://github.com/nodef/extra-bit/wiki/set)
86
+ * Find n-bit parity.
184
87
  * @param x an int32
185
- * @param i bit index
186
- * @param f bit value (1)
187
- * @example
188
- * ```javascript
189
- * set(6, 0, 1);
190
- * // 7 (110,0,1 ⇒ 111)
191
- *
192
- * set(6, 2, 1);
193
- * // 6 (110,2,1 ⇒ 110)
194
- *
195
- * set(6, 2, 0);
196
- * // 2 (110,2,0 ⇒ 010)
197
- * ```
88
+ * @param n number of bits (1)
198
89
  */
199
- export function set(x: number, i: number, f?: number): number;
90
+ declare function parity(x: number, n?: number): number;
91
+ //# sourceMappingURL=parity.d.ts.map
92
+
200
93
  /**
201
- * Sets bits as per mask.
202
- * [📘](https://github.com/nodef/extra-bit/wiki/setAs)
94
+ * Rotate bits.
203
95
  * @param x an int32
204
- * @param m bit mask
205
- * @param f bit value (1)
206
- * @example
207
- * ```javascript
208
- * setAs(8, 2, 1);
209
- * // 10 (0x8 set 0x2 ⇒ 0xA)
210
- *
211
- * setAs(15, 3, 0);
212
- * // 12 (0xF clear 0x3 ⇒ 0xC)
213
- *
214
- * setAs(0x1234, 0x430, 1);
215
- * // 5684 (0x1234 set 0x430 ⇒ 0x1634)
216
- * ```
96
+ * @param n rotate amount (+ve: left, -ve: right)
217
97
  */
218
- export function setAs(x: number, m: number, f?: number): number;
98
+ declare function rotate(x: number, n?: number): number;
99
+ //# sourceMappingURL=rotate.d.ts.map
100
+
219
101
  /**
220
- * Sign extends variable bit-width integer.
221
- * [📘](https://github.com/nodef/extra-bit/wiki/signExtend)
222
- * @param x variable bit-width integer
223
- * @param w bit width (32)
224
- * @example
225
- * ```javascript
226
- * signExtend(15, 4);
227
- * // -1 (1111 ⇒ -1)
228
- *
229
- * signExtend(3, 3);
230
- * // 3 (011 ⇒ 3)
231
- *
232
- * signExtend(4, 3);
233
- * // -4 (100 ⇒ -4)
234
- * ```
102
+ * Reverse all bits.
103
+ * @param x an int32
235
104
  */
236
- export function signExtend(x: number, w?: number): number;
105
+ declare function reverse(x: number): number;
106
+ //# sourceMappingURL=reverse.d.ts.map
107
+
237
108
  /**
238
- * Swaps bit sequences.
239
- * [📘](https://github.com/nodef/extra-bit/wiki/swap)
240
- * @param x an int32
241
- * @param i first bit index
242
- * @param j second bit index
243
- * @param n bit width (1)
244
- * @example
245
- * ```javascript
246
- * swap(6, 1, 0);
247
- * // 5 (110 ⇒ 101)
248
- *
249
- * swap(0x1234, 8, 4, 4);
250
- * // 4900 (0x1234 ⇒ 0x1324)
251
- *
252
- * swap(0x4AAB, 8, 0, 8);
253
- * // 43850 (0x4AAB ⇒ 0xAB4A)
254
- * ```
109
+ * Merge bits as per mask.
110
+ * @param x first int32
111
+ * @param y second int32
112
+ * @param m bit mask (0 ⇒ from x)
255
113
  */
256
- export function swap(x: number, i: number, j: number, n?: number): number;
114
+ declare function merge(x: number, y: number, m: number): number;
115
+ //# sourceMappingURL=merge.d.ts.map
116
+
257
117
  /**
258
- * Toggles a bit.
259
- * [📘](https://github.com/nodef/extra-bit/wiki/toggle)
260
- * @param x an int32
261
- * @param i bit index
262
- * @example
263
- * ```javascript
264
- * toggle(6, 0);
265
- * // 7 (110,0 ⇒ 111)
266
- *
267
- * toggle(6, 1);
268
- * // 4 (110,1 ⇒ 100)
269
- *
270
- * toggle(6, 2);
271
- * // 2 (110,2 ⇒ 010)
272
- * ```
118
+ * Interleave bits of two int16s.
119
+ * @param x first int16
120
+ * @param y second int16
121
+ * @returns int32
273
122
  */
274
- export function toggle(x: number, i: number): number;
123
+ declare function interleave(x: number, y: number): number;
124
+ //# sourceMappingURL=interleave.d.ts.map
125
+
275
126
  /**
276
- * Toggles bits as per mask.
277
- * [📘](https://github.com/nodef/extra-bit/wiki/toggleAs)
278
- * @param x an int32
279
- * @param m bit mask
280
- * @example
281
- * ```javascript
282
- * toggleAs(6, 1);
283
- * // 7 (110,000 ⇒ 111)
284
- *
285
- * toggleAs(6, 7);
286
- * // 1 (110,111 ⇒ 001)
287
- *
288
- * toggleAs(6, 3);
289
- * // 5 (110,011 ⇒ 101)
290
- * ```
127
+ * Sign extend variable bit-width integer.
128
+ * @param x variable bit-width integer
129
+ * @param w bit width (32)
291
130
  */
292
- export function toggleAs(x: number, m: number): number;
131
+ declare function signExtend(x: number, w?: number): number;
132
+ //# sourceMappingURL=signExtend.d.ts.map
293
133
 
294
- export {};
295
- }
134
+ export { count, get, getAs, interleave, merge, parity, reverse, rotate, scan, scanReverse, set, setAs, signExtend, swap, toggle, toggleAs };
package/index.js CHANGED
@@ -1,30 +1,71 @@
1
1
  'use strict';
2
+
2
3
  Object.defineProperty(exports, '__esModule', { value: true });
3
- function count(x) {
4
- x = x - ((x >>> 1) & 0x55555555);
5
- x = (x & 0x33333333) + ((x >>> 2) & 0x33333333);
6
- return ((x + (x >>> 4) & 0x0F0F0F0F) * 0x01010101) >>> 24;
4
+
5
+ function ones(w) {
6
+ return (1 << w) - 1;
7
7
  }
8
- function get(x, i) {
9
- return (x >>> i) & 1;
8
+
9
+ function get(x, i, w = 1) {
10
+ return (x >>> i) & ones(w);
10
11
  }
12
+
11
13
  function getAs(x, m) {
12
14
  return x & m;
13
15
  }
14
- function interleave(x, y) {
15
- x = (x | (x << 8)) & 0x00FF00FF;
16
- x = (x | (x << 4)) & 0x0F0F0F0F;
17
- x = (x | (x << 2)) & 0x33333333;
18
- x = (x | (x << 1)) & 0x55555555;
19
- y = (y | (y << 8)) & 0x00FF00FF;
20
- y = (y | (y << 4)) & 0x0F0F0F0F;
21
- y = (y | (y << 2)) & 0x33333333;
22
- y = (y | (y << 1)) & 0x55555555;
23
- return y | (x << 1);
16
+
17
+ function set(x, i, v = 1, w = 1) {
18
+ return (x & ~(ones(w) << i)) | (v << i);
24
19
  }
25
- function merge(x, y, m) {
26
- return x ^ ((x ^ y) & m);
20
+
21
+ function setAs(x, m, f = 1) {
22
+ return (x & ~m) | (-f & m);
27
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;
67
+ }
68
+
28
69
  function parity1(x) {
29
70
  x ^= x >>> 16;
30
71
  x ^= x >>> 8;
@@ -42,6 +83,11 @@ function parity(x, n = 1) {
42
83
  }
43
84
  return a;
44
85
  }
86
+
87
+ function rotate(x, n = 0) {
88
+ return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
89
+ }
90
+
45
91
  function reverse(x) {
46
92
  x = ((x >>> 1) & 0x55555555) | ((x & 0x55555555) << 1);
47
93
  x = ((x >>> 2) & 0x33333333) | ((x & 0x33333333) << 2);
@@ -49,48 +95,28 @@ function reverse(x) {
49
95
  x = ((x >>> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
50
96
  return (x >>> 16) | (x << 16);
51
97
  }
52
- function rotate(x, n = 0) {
53
- return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
54
- }
55
- const MOD37_POS32 = [
56
- 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
57
- 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
58
- ];
59
- function scan(x) {
60
- return MOD37_POS32[(-x & x) % 37];
61
- }
62
- const DEBRUIJN_POS32 = [
63
- 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
64
- 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
65
- ];
66
- function scanReverse(x) {
67
- x |= x >>> 1;
68
- x |= x >>> 2;
69
- x |= x >>> 4;
70
- x |= x >>> 8;
71
- x |= x >>> 16;
72
- return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
73
- }
74
- function set(x, i, f = 1) {
75
- return (x & ~(1 << i)) | (f << i);
98
+
99
+ function merge(x, y, m) {
100
+ return x ^ ((x ^ y) & m);
76
101
  }
77
- function setAs(x, m, f = 1) {
78
- return (x & ~m) | (-f & m);
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);
79
113
  }
114
+
80
115
  function signExtend(x, w = 32) {
81
116
  w = 32 - w;
82
117
  return (x << w) >> w;
83
118
  }
84
- function swap(x, i, j, n = 1) {
85
- var t = ((x >>> i) ^ (x >>> j)) & ((1 << n) - 1);
86
- return x ^ ((t << i) | (t << j));
87
- }
88
- function toggle(x, i) {
89
- return x ^ (1 << i);
90
- }
91
- function toggleAs(x, m) {
92
- return x ^ m;
93
- }
119
+
94
120
  exports.count = count;
95
121
  exports.get = get;
96
122
  exports.getAs = getAs;
package/index.mjs CHANGED
@@ -1,28 +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;
1
+ function ones(w) {
2
+ return (1 << w) - 1;
5
3
  }
6
- function get(x, i) {
7
- return (x >>> i) & 1;
4
+
5
+ function get(x, i, w = 1) {
6
+ return (x >>> i) & ones(w);
8
7
  }
8
+
9
9
  function getAs(x, m) {
10
10
  return x & m;
11
11
  }
12
- function interleave(x, y) {
13
- x = (x | (x << 8)) & 0x00FF00FF;
14
- x = (x | (x << 4)) & 0x0F0F0F0F;
15
- x = (x | (x << 2)) & 0x33333333;
16
- x = (x | (x << 1)) & 0x55555555;
17
- y = (y | (y << 8)) & 0x00FF00FF;
18
- y = (y | (y << 4)) & 0x0F0F0F0F;
19
- y = (y | (y << 2)) & 0x33333333;
20
- y = (y | (y << 1)) & 0x55555555;
21
- return y | (x << 1);
12
+
13
+ function set(x, i, v = 1, w = 1) {
14
+ return (x & ~(ones(w) << i)) | (v << i);
22
15
  }
23
- function merge(x, y, m) {
24
- return x ^ ((x ^ y) & m);
16
+
17
+ function setAs(x, m, f = 1) {
18
+ return (x & ~m) | (-f & m);
25
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;
63
+ }
64
+
26
65
  function parity1(x) {
27
66
  x ^= x >>> 16;
28
67
  x ^= x >>> 8;
@@ -40,6 +79,11 @@ function parity(x, n = 1) {
40
79
  }
41
80
  return a;
42
81
  }
82
+
83
+ function rotate(x, n = 0) {
84
+ return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
85
+ }
86
+
43
87
  function reverse(x) {
44
88
  x = ((x >>> 1) & 0x55555555) | ((x & 0x55555555) << 1);
45
89
  x = ((x >>> 2) & 0x33333333) | ((x & 0x33333333) << 2);
@@ -47,46 +91,26 @@ function reverse(x) {
47
91
  x = ((x >>> 8) & 0x00FF00FF) | ((x & 0x00FF00FF) << 8);
48
92
  return (x >>> 16) | (x << 16);
49
93
  }
50
- function rotate(x, n = 0) {
51
- return n < 0 ? x << 32 + n | x >>> -n : x << n | x >> 32 - n;
52
- }
53
- const MOD37_POS32 = [
54
- 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,
55
- 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
56
- ];
57
- function scan(x) {
58
- return MOD37_POS32[(-x & x) % 37];
59
- }
60
- const DEBRUIJN_POS32 = [
61
- 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
62
- 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
63
- ];
64
- function scanReverse(x) {
65
- x |= x >>> 1;
66
- x |= x >>> 2;
67
- x |= x >>> 4;
68
- x |= x >>> 8;
69
- x |= x >>> 16;
70
- return DEBRUIJN_POS32[(x * 0x07C4ACDD) >>> 27];
71
- }
72
- function set(x, i, f = 1) {
73
- return (x & ~(1 << i)) | (f << i);
94
+
95
+ function merge(x, y, m) {
96
+ return x ^ ((x ^ y) & m);
74
97
  }
75
- function setAs(x, m, f = 1) {
76
- return (x & ~m) | (-f & m);
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);
77
109
  }
110
+
78
111
  function signExtend(x, w = 32) {
79
112
  w = 32 - w;
80
113
  return (x << w) >> w;
81
114
  }
82
- function swap(x, i, j, n = 1) {
83
- var t = ((x >>> i) ^ (x >>> j)) & ((1 << n) - 1);
84
- return x ^ ((t << i) | (t << j));
85
- }
86
- function toggle(x, i) {
87
- return x ^ (1 << i);
88
- }
89
- function toggleAs(x, m) {
90
- return x ^ m;
91
- }
115
+
92
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": "1.0.64",
3
+ "version": "2.0.2",
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": "ebuild export+main+example+wiki+readme --wikiExample=false --asciinema=false",
14
- "deploy": "ebuild jsdoc+export+main+meta+example+github+publish"
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
- "extra-build": "^1.3.30"
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
  }
package/example.js DELETED
@@ -1,11 +0,0 @@
1
- const bit = require("extra-bit");
2
-
3
- bit.count(7);
4
-
5
- bit.parity(8, 2);
6
-
7
- bit.swap(6, 1, 0);
8
-
9
- bit.reverse(0xFFFF0000);
10
-
11
- bit.signExtend(15, 4);