extra-bit.web 2.3.1 → 2.5.0
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 +25 -36
- package/deno.lock +37 -0
- package/index.d.ts +16 -50
- package/index.js +2 -1
- package/index.js.map +7 -0
- package/index.test.ts +239 -0
- package/package.json +12 -57
- package/index.mjs +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
The [bit] is a basic unit of information in information theory, computing.<br>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
|
|
3
|
+
▌
|
|
4
|
+
📦 [JSR](https://jsr.io/@nodef/extra-bit),
|
|
5
|
+
📦 [NPM](https://www.npmjs.com/package/@nodef/extra-bit),
|
|
6
|
+
🌐 [Web](https://cdn.jsdelivr.net/npm/@nodef/extra-bit.web),
|
|
7
|
+
📰 [Docs](https://jsr.io/@nodef/extra-bit/doc).
|
|
7
8
|
|
|
8
9
|
The concept of a **bit**, which stands for *binary digit*, has been a
|
|
9
10
|
fundamental unit of information in the fields of information theory and
|
|
@@ -15,22 +16,13 @@ operators (such as `|`) are applied. All bitwise operations are performed on
|
|
|
15
16
|
**32-bit integers**. This package includes [bit twiddling hacks] by *Sean Eron*
|
|
16
17
|
*Anderson* and many others.
|
|
17
18
|
|
|
18
|
-
This package is available in *Node.js* and *Web* formats. To use it on the web,
|
|
19
|
-
simply use the `extra_bit` global variable after loading with a `<script>`
|
|
20
|
-
tag from the [jsDelivr CDN].
|
|
21
|
-
|
|
22
|
-
> Stability: [Experimental](https://www.youtube.com/watch?v=L1j93RnIxEo).
|
|
23
|
-
|
|
24
19
|
[bit]: https://en.wikipedia.org/wiki/Bit
|
|
25
20
|
[bit twiddling hacks]: https://graphics.stanford.edu/~seander/bithacks.html
|
|
26
|
-
[jsDelivr CDN]: https://cdn.jsdelivr.net/npm/extra-bit.web/index.js
|
|
27
21
|
|
|
28
22
|
<br>
|
|
29
23
|
|
|
30
24
|
```javascript
|
|
31
|
-
|
|
32
|
-
// import * as xbit from "extra-bit";
|
|
33
|
-
// import * as xbit from "https://unpkg.com/extra-bit/index.mjs"; (deno)
|
|
25
|
+
import * as xbit from "jsr:@nodef/extra-bit";
|
|
34
26
|
|
|
35
27
|
xbit.count(7);
|
|
36
28
|
// → 3 (111 ⇒ 3)
|
|
@@ -86,27 +78,24 @@ xbit.signExtend(15, 4);
|
|
|
86
78
|
<br>
|
|
87
79
|
|
|
88
80
|
|
|
89
|
-
[](https://wolfram77.github.io)<br>
|
|
90
82
|
[](https://nodef.github.io)
|
|
91
|
-
[](https://zenodo.org/badge/latestdoi/249481796)
|
|
92
|
-
[](https://coveralls.io/github/nodef/extra-bit)
|
|
93
|
-
[](https://codeclimate.com/github/nodef/extra-bit/test_coverage)
|
|
94
|
-
[](https://codeclimate.com/github/nodef/extra-bit/maintainability)
|
|
95
83
|

|
|
96
84
|
|
|
97
|
-
|
|
98
|
-
[
|
|
99
|
-
[
|
|
100
|
-
[
|
|
101
|
-
[
|
|
102
|
-
[
|
|
103
|
-
[
|
|
104
|
-
[
|
|
105
|
-
[
|
|
106
|
-
[
|
|
107
|
-
[
|
|
108
|
-
[
|
|
109
|
-
[
|
|
110
|
-
[
|
|
111
|
-
[
|
|
112
|
-
[
|
|
85
|
+
|
|
86
|
+
[get]: https://jsr.io/@nodef/extra-bit/doc/~/get
|
|
87
|
+
[getAs]: https://jsr.io/@nodef/extra-bit/doc/~/getAs
|
|
88
|
+
[set]: https://jsr.io/@nodef/extra-bit/doc/~/set
|
|
89
|
+
[setAs]: https://jsr.io/@nodef/extra-bit/doc/~/setAs
|
|
90
|
+
[toggle]: https://jsr.io/@nodef/extra-bit/doc/~/toggle
|
|
91
|
+
[toggleAs]: https://jsr.io/@nodef/extra-bit/doc/~/toggleAs
|
|
92
|
+
[swap]: https://jsr.io/@nodef/extra-bit/doc/~/swap
|
|
93
|
+
[scan]: https://jsr.io/@nodef/extra-bit/doc/~/scan
|
|
94
|
+
[scanReverse]: https://jsr.io/@nodef/extra-bit/doc/~/scanReverse
|
|
95
|
+
[count]: https://jsr.io/@nodef/extra-bit/doc/~/count
|
|
96
|
+
[parity]: https://jsr.io/@nodef/extra-bit/doc/~/parity
|
|
97
|
+
[rotate]: https://jsr.io/@nodef/extra-bit/doc/~/rotate
|
|
98
|
+
[reverse]: https://jsr.io/@nodef/extra-bit/doc/~/reverse
|
|
99
|
+
[merge]: https://jsr.io/@nodef/extra-bit/doc/~/merge
|
|
100
|
+
[interleave]: https://jsr.io/@nodef/extra-bit/doc/~/interleave
|
|
101
|
+
[signExtend]: https://jsr.io/@nodef/extra-bit/doc/~/signExtend
|
package/deno.lock
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "5",
|
|
3
|
+
"specifiers": {
|
|
4
|
+
"jsr:@std/assert@^1.0.19": "1.0.19",
|
|
5
|
+
"jsr:@std/cli@^1.0.32": "1.0.32",
|
|
6
|
+
"jsr:@std/fmt@^1.0.10": "1.0.10",
|
|
7
|
+
"jsr:@std/internal@^1.0.12": "1.0.14",
|
|
8
|
+
"jsr:@std/internal@^1.0.14": "1.0.14"
|
|
9
|
+
},
|
|
10
|
+
"jsr": {
|
|
11
|
+
"@std/assert@1.0.19": {
|
|
12
|
+
"integrity": "eaada96ee120cb980bc47e040f82814d786fe8162ecc53c91d8df60b8755991e",
|
|
13
|
+
"dependencies": [
|
|
14
|
+
"jsr:@std/internal@^1.0.12"
|
|
15
|
+
]
|
|
16
|
+
},
|
|
17
|
+
"@std/cli@1.0.32": {
|
|
18
|
+
"integrity": "188b3a100d6202d64e3f5bd3d799c7fa4f6d77f92cc65eb7f641c1fa0aa92a66",
|
|
19
|
+
"dependencies": [
|
|
20
|
+
"jsr:@std/fmt",
|
|
21
|
+
"jsr:@std/internal@^1.0.14"
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
"@std/fmt@1.0.10": {
|
|
25
|
+
"integrity": "90dfba288802ac6de82fb31d0917eb9e4450b9925b954d5e51fc29ac07419db5"
|
|
26
|
+
},
|
|
27
|
+
"@std/internal@1.0.14": {
|
|
28
|
+
"integrity": "291516b3d4c35024d6ffbc0a9df5bf4c64116e05b50012cf846710152d2ffdf7"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"workspace": {
|
|
32
|
+
"dependencies": [
|
|
33
|
+
"jsr:@std/assert@^1.0.19",
|
|
34
|
+
"jsr:@std/cli@^1.0.32"
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -1,119 +1,85 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Get bits at specified index.
|
|
3
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/get)
|
|
4
3
|
* @param x an int32
|
|
5
4
|
* @param i bit index
|
|
6
5
|
* @param w bit width [1]
|
|
7
|
-
*/
|
|
8
|
-
declare function get(x: number, i: number, w?: number): number;
|
|
6
|
+
*/ export declare function get(x: number, i: number, w?: number): number;
|
|
9
7
|
/**
|
|
10
8
|
* Get bits as per mask.
|
|
11
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/getAs)
|
|
12
9
|
* @param x an int32
|
|
13
10
|
* @param m bit mask
|
|
14
|
-
*/
|
|
15
|
-
declare function getAs(x: number, m: number): number;
|
|
11
|
+
*/ export declare function getAs(x: number, m: number): number;
|
|
16
12
|
/**
|
|
17
13
|
* Set bits at specified index.
|
|
18
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/set)
|
|
19
14
|
* @param x an int32
|
|
20
15
|
* @param i bit index
|
|
21
16
|
* @param v bit value [1]
|
|
22
17
|
* @param w bit width [1]
|
|
23
|
-
*/
|
|
24
|
-
declare function set(x: number, i: number, v?: number, w?: number): number;
|
|
18
|
+
*/ export declare function set(x: number, i: number, v?: number, w?: number): number;
|
|
25
19
|
/**
|
|
26
20
|
* Set bits as per mask.
|
|
27
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/setAs)
|
|
28
21
|
* @param x an int32
|
|
29
22
|
* @param m bit mask
|
|
30
23
|
* @param f bit value [1]
|
|
31
|
-
*/
|
|
32
|
-
declare function setAs(x: number, m: number, f?: number): number;
|
|
24
|
+
*/ export declare function setAs(x: number, m: number, f?: number): number;
|
|
33
25
|
/**
|
|
34
26
|
* Toggle bits at specified index.
|
|
35
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/toggle)
|
|
36
27
|
* @param x an int32
|
|
37
28
|
* @param i bit index
|
|
38
29
|
* @param w bit width [1]
|
|
39
|
-
*/
|
|
40
|
-
declare function toggle(x: number, i: number, w?: number): number;
|
|
30
|
+
*/ export declare function toggle(x: number, i: number, w?: number): number;
|
|
41
31
|
/**
|
|
42
32
|
* Toggle bits as per mask.
|
|
43
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/toggleAs)
|
|
44
33
|
* @param x an int32
|
|
45
34
|
* @param m bit mask
|
|
46
|
-
*/
|
|
47
|
-
declare function toggleAs(x: number, m: number): number;
|
|
35
|
+
*/ export declare function toggleAs(x: number, m: number): number;
|
|
48
36
|
/**
|
|
49
37
|
* Swap bit sequences at specified indices.
|
|
50
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/swap)
|
|
51
38
|
* @param x an int32
|
|
52
39
|
* @param i first bit index
|
|
53
40
|
* @param j second bit index
|
|
54
41
|
* @param w bit width [1]
|
|
55
|
-
*/
|
|
56
|
-
declare function swap(x: number, i: number, j: number, w?: number): number;
|
|
42
|
+
*/ export declare function swap(x: number, i: number, j: number, w?: number): number;
|
|
57
43
|
/**
|
|
58
44
|
* Find index of first set bit from LSB.
|
|
59
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/scan)
|
|
60
45
|
* @param x an int32
|
|
61
|
-
*/
|
|
62
|
-
declare function scan(x: number): number;
|
|
46
|
+
*/ export declare function scan(x: number): number;
|
|
63
47
|
/**
|
|
64
48
|
* Find index of first set bit from MSB.
|
|
65
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/scanReverse)
|
|
66
49
|
* @param x an int32
|
|
67
|
-
*/
|
|
68
|
-
declare function scanReverse(x: number): number;
|
|
50
|
+
*/ export declare function scanReverse(x: number): number;
|
|
69
51
|
/**
|
|
70
52
|
* Count bits set.
|
|
71
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/count)
|
|
72
53
|
* @param x an int32
|
|
73
|
-
*/
|
|
74
|
-
declare function count(x: number): number;
|
|
54
|
+
*/ export declare function count(x: number): number;
|
|
75
55
|
/**
|
|
76
56
|
* Calculate n-bit parity.
|
|
77
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/parity)
|
|
78
57
|
* @param x an int32
|
|
79
58
|
* @param n number of bits [1]
|
|
80
|
-
*/
|
|
81
|
-
declare function parity(x: number, n?: number): number;
|
|
59
|
+
*/ export declare function parity(x: number, n?: number): number;
|
|
82
60
|
/**
|
|
83
61
|
* Rotate bits by a certain amount.
|
|
84
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/rotate)
|
|
85
62
|
* @param x an int32
|
|
86
63
|
* @param n rotate amount (+ve: left, -ve: right)
|
|
87
|
-
*/
|
|
88
|
-
declare function rotate(x: number, n?: number): number;
|
|
64
|
+
*/ export declare function rotate(x: number, n?: number): number;
|
|
89
65
|
/**
|
|
90
66
|
* Reverse all bits.
|
|
91
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/reverse)
|
|
92
67
|
* @param x an int32
|
|
93
|
-
*/
|
|
94
|
-
declare function reverse(x: number): number;
|
|
68
|
+
*/ export declare function reverse(x: number): number;
|
|
95
69
|
/**
|
|
96
70
|
* Merge bits as per mask.
|
|
97
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/merge)
|
|
98
71
|
* @param x first int32
|
|
99
72
|
* @param y second int32
|
|
100
73
|
* @param m bit mask (0 ⇒ from x)
|
|
101
|
-
*/
|
|
102
|
-
declare function merge(x: number, y: number, m: number): number;
|
|
74
|
+
*/ export declare function merge(x: number, y: number, m: number): number;
|
|
103
75
|
/**
|
|
104
76
|
* Interleave bits of two int16s.
|
|
105
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/interleave)
|
|
106
77
|
* @param x first int16
|
|
107
78
|
* @param y second int16
|
|
108
79
|
* @returns int32
|
|
109
|
-
*/
|
|
110
|
-
declare function interleave(x: number, y: number): number;
|
|
80
|
+
*/ export declare function interleave(x: number, y: number): number;
|
|
111
81
|
/**
|
|
112
82
|
* Sign extend variable bit-width integer.
|
|
113
|
-
* [📘](https://github.com/nodef/extra-bit/wiki/signExtend)
|
|
114
83
|
* @param x variable bit-width integer
|
|
115
84
|
* @param w bit width (32)
|
|
116
|
-
*/
|
|
117
|
-
declare function signExtend(x: number, w?: number): number;
|
|
118
|
-
|
|
119
|
-
export { count, get, getAs, interleave, merge, parity, reverse, rotate, scan, scanReverse, set, setAs, signExtend, swap, toggle, toggleAs };
|
|
85
|
+
*/ export declare function signExtend(x: number, w?: number): number;
|
package/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
function o(t){return(1<<t)-1}function f(t,r,n=1){return t>>>r&o(n)}function p(t,r){return t&r}function s(t,r,n=1,e=1){return t&~(o(e)<<r)|n<<r}function a(t,r,n=1){return t&~r|-n&r}function g(t,r,n=1){return t^o(n)<<r}function l(t,r){return t^r}function A(t,r,n,e=1){let u=(t>>>r^t>>>n)&o(e);return t^(u<<r|u<<n)}var F=[32,0,1,26,2,23,27,0,3,16,24,30,28,11,0,13,4,7,17,0,25,22,31,15,29,10,12,6,0,21,14,9,5,20,8,19,18];function D(t){return F[(-t&t)%37]}var c=[0,9,1,10,13,21,2,29,11,14,16,18,22,25,3,30,8,12,20,28,15,17,24,7,19,27,23,6,26,5,4,31];function v(t){return t|=t>>>1,t|=t>>>2,t|=t>>>4,t|=t>>>8,t|=t>>>16,c[t*130329821>>>27]}function O(t){return t=t-(t>>>1&1431655765),t=(t&858993459)+(t>>>2&858993459),(t+(t>>>4)&252645135)*16843009>>>24}function i(t){return t^=t>>>16,t^=t>>>8,t^=t>>>4,t&=15,27030>>>t&1}function C(t,r=1){if(r===1)return i(t);let n=(1<<r)-1,e=0;for(;t;)e^=t&n,t>>>=r;return e}function E(t,r=0){return r<0?t<<32+r|t>>>-r:t<<r|t>>32-r}function P(t){return t=t>>>1&1431655765|(t&1431655765)<<1,t=t>>>2&858993459|(t&858993459)<<2,t=t>>>4&252645135|(t&252645135)<<4,t=t>>>8&16711935|(t&16711935)<<8,t>>>16|t<<16}function R(t,r,n){return t^(t^r)&n}function S(t,r){return t=(t|t<<8)&16711935,t=(t|t<<4)&252645135,t=(t|t<<2)&858993459,t=(t|t<<1)&1431655765,r=(r|r<<8)&16711935,r=(r|r<<4)&252645135,r=(r|r<<2)&858993459,r=(r|r<<1)&1431655765,r|t<<1}function _(t,r=32){return r=32-r,t<<r>>r}export{O as count,f as get,p as getAs,S as interleave,R as merge,C as parity,P as reverse,E as rotate,D as scan,v as scanReverse,s as set,a as setAs,_ as signExtend,A as swap,g as toggle,l as toggleAs};
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../index.ts"],
|
|
4
|
+
"sourcesContent": ["// METHODS\n// =======\n\n// Get integer with 1s of specified width in bits.\nfunction ones(w: number) {\n return (1<<w) - 1;\n}\n\n\n/**\n * Get bits at specified index.\n * @param x an int32\n * @param i bit index\n * @param w bit width [1]\n */\nexport function get(x: number, i: number, w: number=1): number {\n return (x>>>i) & ones(w);\n}\n\n\n/**\n * Get bits as per mask.\n * @param x an int32\n * @param m bit mask\n */\nexport function getAs(x: number, m: number): number {\n return x & m;\n}\n\n\n/**\n * Set bits at specified index.\n * @param x an int32\n * @param i bit index\n * @param v bit value [1]\n * @param w bit width [1]\n */\nexport function set(x: number, i: number, v: number=1, w: number=1): number {\n return (x & ~(ones(w)<<i)) | (v<<i);\n}\n\n\n/**\n * Set bits as per mask.\n * @param x an int32\n * @param m bit mask\n * @param f bit value [1]\n */\nexport function setAs(x: number, m: number, f: number=1): number {\n return (x & ~m) | (-f & m);\n}\n\n\n/**\n * Toggle bits at specified index.\n * @param x an int32\n * @param i bit index\n * @param w bit width [1]\n */\nexport function toggle(x: number, i: number, w: number=1): number {\n return x ^ (ones(w)<<i);\n}\n\n\n/**\n * Toggle bits as per mask.\n * @param x an int32\n * @param m bit mask\n */\nexport function toggleAs(x: number, m: number): number {\n return x ^ m;\n}\n\n\n/**\n * Swap bit sequences at specified indices.\n * @param x an int32\n * @param i first bit index\n * @param j second bit index\n * @param w bit width [1]\n */\nexport function swap(x: number, i: number, j: number, w: number=1): number {\n const t = ((x>>>i)^(x>>>j)) & ones(w);\n return x ^ ((t<<i)|(t<<j));\n}\n\n\nconst MOD37_POS32: number[] = [\n 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0,\n 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18\n];\n\n\n/**\n * Find index of first set bit from LSB.\n * @param x an int32\n */\nexport function scan(x: number): number {\n return MOD37_POS32[(-x & x) % 37];\n}\n\n\nconst DEBRUIJN_POS32: number[] = [\n 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,\n 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31\n];\n\n\n/**\n * Find index of first set bit from MSB.\n * @param x an int32\n */\nexport function scanReverse(x: number): number {\n x |= x>>>1;\n x |= x>>>2;\n x |= x>>>4;\n x |= x>>>8;\n x |= x>>>16;\n return DEBRUIJN_POS32[(x*0x07C4ACDD)>>>27];\n}\n\n\n/**\n * Count bits set.\n * @param x an int32\n */\nexport function count(x: number): number {\n x = x - ((x>>>1) & 0x55555555);\n x = (x & 0x33333333) + ((x>>>2) & 0x33333333);\n return ((x + (x>>>4) & 0x0F0F0F0F) * 0x01010101)>>>24;\n}\n\n\n// Calculate 1-bit parity.\nfunction parity1(x: number): number {\n x ^= x>>>16;\n x ^= x>>>8;\n x ^= x>>>4;\n x &= 0xF;\n return (0x6996>>>x) & 1;\n}\n\n/**\n * Calculate n-bit parity.\n * @param x an int32\n * @param n number of bits [1]\n */\nexport function parity(x: number, n: number=1): number {\n if (n===1) return parity1(x);\n const m = (1<<n)-1; let a = 0|0;\n while (x) {\n a ^= x&m;\n x >>>= n;\n }\n return a;\n}\n\n\n/**\n * Rotate bits by a certain amount.\n * @param x an int32\n * @param n rotate amount (+ve: left, -ve: right)\n */\nexport function rotate(x: number, n: number=0): number {\n return n<0? x<<32+n | x>>>-n : x<<n | x>>32-n;\n}\n\n\n/**\n * Reverse all bits.\n * @param x an int32\n */\nexport function reverse(x: number): number {\n x = ((x>>>1) & 0x55555555) | ((x & 0x55555555)<<1);\n x = ((x>>>2) & 0x33333333) | ((x & 0x33333333)<<2);\n x = ((x>>>4) & 0x0F0F0F0F) | ((x & 0x0F0F0F0F)<<4);\n x = ((x>>>8) & 0x00FF00FF) | ((x & 0x00FF00FF)<<8);\n return (x>>>16) | (x<<16);\n}\n\n\n/**\n * Merge bits as per mask.\n * @param x first int32\n * @param y second int32\n * @param m bit mask (0 ⇒ from x)\n */\nexport function merge(x: number, y: number, m: number): number {\n return x ^ ((x^y) & m);\n}\n\n\n/**\n * Interleave bits of two int16s.\n * @param x first int16\n * @param y second int16\n * @returns int32\n */\nexport function interleave(x: number, y: number): number {\n x = (x | (x<<8)) & 0x00FF00FF;\n x = (x | (x<<4)) & 0x0F0F0F0F;\n x = (x | (x<<2)) & 0x33333333;\n x = (x | (x<<1)) & 0x55555555;\n y = (y | (y<<8)) & 0x00FF00FF;\n y = (y | (y<<4)) & 0x0F0F0F0F;\n y = (y | (y<<2)) & 0x33333333;\n y = (y | (y<<1)) & 0x55555555;\n return y | (x<<1);\n}\n\n\n/**\n * Sign extend variable bit-width integer.\n * @param x variable bit-width integer\n * @param w bit width (32)\n */\nexport function signExtend(x: number, w: number=32): number {\n w = 32-w;\n return (x<<w)>>w;\n}\n"],
|
|
5
|
+
"mappings": "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",
|
|
6
|
+
"names": ["ones", "w", "get", "x", "i", "getAs", "m", "set", "v", "setAs", "f", "toggle", "toggleAs", "swap", "j", "t", "MOD37_POS32", "scan", "DEBRUIJN_POS32", "scanReverse", "count", "parity1", "parity", "n", "a", "rotate", "reverse", "merge", "y", "interleave", "signExtend"]
|
|
7
|
+
}
|
package/index.test.ts
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import {assertEquals} from "@std/assert";
|
|
2
|
+
import {
|
|
3
|
+
get,
|
|
4
|
+
getAs,
|
|
5
|
+
set,
|
|
6
|
+
setAs,
|
|
7
|
+
toggle,
|
|
8
|
+
toggleAs,
|
|
9
|
+
swap,
|
|
10
|
+
scan,
|
|
11
|
+
scanReverse,
|
|
12
|
+
count,
|
|
13
|
+
parity,
|
|
14
|
+
rotate,
|
|
15
|
+
reverse,
|
|
16
|
+
merge,
|
|
17
|
+
interleave,
|
|
18
|
+
signExtend,
|
|
19
|
+
} from "./index.ts";
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
Deno.test("example1", () => {
|
|
25
|
+
let a;
|
|
26
|
+
a = count(7);
|
|
27
|
+
assertEquals(a, 3);
|
|
28
|
+
// → 3 (111 ⇒ 3)
|
|
29
|
+
|
|
30
|
+
a = parity(8, 2);
|
|
31
|
+
assertEquals(a, 2);
|
|
32
|
+
// → 2 (10,00 ⇒ 10)
|
|
33
|
+
|
|
34
|
+
a = swap(6, 1, 0);
|
|
35
|
+
assertEquals(a, 5);
|
|
36
|
+
// → 5 (110 ⇒ 101)
|
|
37
|
+
|
|
38
|
+
a = reverse(0xFFFF0000);
|
|
39
|
+
assertEquals(a, 65535);
|
|
40
|
+
// → 65535 (0x0000FFFF)
|
|
41
|
+
|
|
42
|
+
a = signExtend(15, 4);
|
|
43
|
+
assertEquals(a, -1);
|
|
44
|
+
// → -1
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
// Get bits at specified index.
|
|
51
|
+
Deno.test("get", () => {
|
|
52
|
+
let a;
|
|
53
|
+
a = get(6, 0);
|
|
54
|
+
assertEquals(a, 0); // 110,0 ⇒ 0
|
|
55
|
+
a = get(6, 1);
|
|
56
|
+
assertEquals(a, 1); // 110,1 ⇒ 1
|
|
57
|
+
a = get(6, 2);
|
|
58
|
+
assertEquals(a, 1); // 110,2 ⇒ 1
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
// Get bits as per mask.
|
|
63
|
+
Deno.test("getAs", () => {
|
|
64
|
+
let a;
|
|
65
|
+
a = getAs(6, 4);
|
|
66
|
+
assertEquals(a, 4); // 110,100 ⇒ 100
|
|
67
|
+
a = getAs(6, 7);
|
|
68
|
+
assertEquals(a, 6); // 110,111 ⇒ 110
|
|
69
|
+
a = getAs(6, 5);
|
|
70
|
+
assertEquals(a, 4); // 110,101 ⇒ 100
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
// Set bits at specified index.
|
|
75
|
+
Deno.test("set", () => {
|
|
76
|
+
let a;
|
|
77
|
+
a = set(6, 0, 1);
|
|
78
|
+
assertEquals(a, 7); // 110,0,1 ⇒ 111
|
|
79
|
+
a = set(6, 2, 1);
|
|
80
|
+
assertEquals(a, 6); // 110,2,1 ⇒ 110
|
|
81
|
+
a = set(6, 2, 0);
|
|
82
|
+
assertEquals(a, 2); // 110,2,0 ⇒ 010
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
// Set bits as per mask.
|
|
87
|
+
Deno.test("setAs", () => {
|
|
88
|
+
let a;
|
|
89
|
+
a = setAs(8, 2, 1);
|
|
90
|
+
assertEquals(a, 10); // 0x8 set 0x2 ⇒ 0xA
|
|
91
|
+
a = setAs(15, 3, 0);
|
|
92
|
+
assertEquals(a, 12); // 0xF clear 0x3 ⇒ 0xC
|
|
93
|
+
a = setAs(0x1234, 0x430, 1);
|
|
94
|
+
assertEquals(a, 5684); // 0x1234 set 0x430 ⇒ 0x1634
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
// Toggle bits at specified index.
|
|
99
|
+
Deno.test("toggle", () => {
|
|
100
|
+
let a;
|
|
101
|
+
a = toggle(6, 0);
|
|
102
|
+
assertEquals(a, 7); // 110,0 ⇒ 111
|
|
103
|
+
a = toggle(6, 1);
|
|
104
|
+
assertEquals(a, 4); // 110,1 ⇒ 100
|
|
105
|
+
a = toggle(6, 2);
|
|
106
|
+
assertEquals(a, 2); // 110,2 ⇒ 010
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
// Toggle bits as per mask.
|
|
111
|
+
Deno.test("toggleAs", () => {
|
|
112
|
+
let a;
|
|
113
|
+
a = toggleAs(6, 1);
|
|
114
|
+
assertEquals(a, 7); // 110,000 ⇒ 111
|
|
115
|
+
a = toggleAs(6, 7);
|
|
116
|
+
assertEquals(a, 1); // 110,111 ⇒ 001
|
|
117
|
+
a = toggleAs(6, 3);
|
|
118
|
+
assertEquals(a, 5); // 110,011 ⇒ 101
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
// Swap bit sequences at specified indices.
|
|
123
|
+
Deno.test("swap", () => {
|
|
124
|
+
let a;
|
|
125
|
+
a = swap(6, 1, 0);
|
|
126
|
+
assertEquals(a, 5); // 110 ⇒ 101
|
|
127
|
+
a = swap(0x1234, 8, 4, 4);
|
|
128
|
+
assertEquals(a, 4900); // 0x1234 ⇒ 0x1324
|
|
129
|
+
a = swap(0x4AAB, 8, 0, 8);
|
|
130
|
+
assertEquals(a, 43850); // 0x4AAB ⇒ 0xAB4A
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
// Find index of first set bit from LSB.
|
|
135
|
+
Deno.test("scan", () => {
|
|
136
|
+
let a;
|
|
137
|
+
a = scan(7);
|
|
138
|
+
assertEquals(a, 0); // 111 ⇒ 0
|
|
139
|
+
a = scan(12);
|
|
140
|
+
assertEquals(a, 2); // 1100 ⇒ 2
|
|
141
|
+
a = scan(64);
|
|
142
|
+
assertEquals(a, 6); // 1000000 ⇒ 6
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
// Find index of first set bit from MSB.
|
|
147
|
+
Deno.test("scanReverse", () => {
|
|
148
|
+
let a;
|
|
149
|
+
a = scanReverse(13);
|
|
150
|
+
assertEquals(a, 3); // 1101 ⇒ 3
|
|
151
|
+
a = scanReverse(5);
|
|
152
|
+
assertEquals(a, 2); // 101 ⇒ 2
|
|
153
|
+
a = scanReverse(1);
|
|
154
|
+
assertEquals(a, 0); // 1 ⇒ 0
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
// Count bits set.
|
|
159
|
+
Deno.test("count", () => {
|
|
160
|
+
let a;
|
|
161
|
+
a = count(7);
|
|
162
|
+
assertEquals(a, 3); // 111 ⇒ 3
|
|
163
|
+
a = count(12);
|
|
164
|
+
assertEquals(a, 2); // 1100 ⇒ 2
|
|
165
|
+
a = count(63);
|
|
166
|
+
assertEquals(a, 6); // 111111 ⇒ 6
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
// Calculate n-bit parity.
|
|
171
|
+
Deno.test("parity", () => {
|
|
172
|
+
let a;
|
|
173
|
+
a = parity(7);
|
|
174
|
+
assertEquals(a, 1); // 1,1,1 ⇒ 1
|
|
175
|
+
a = parity(5);
|
|
176
|
+
assertEquals(a, 0); // 1,0,1 ⇒ 0
|
|
177
|
+
a = parity(8, 2);
|
|
178
|
+
assertEquals(a, 2); // 10,00 ⇒ 10
|
|
179
|
+
a = parity(63, 4);
|
|
180
|
+
assertEquals(a, 12); // 11,1111 ⇒ 1100
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
// Rotate bits by a certain amount.
|
|
185
|
+
Deno.test("rotate", () => {
|
|
186
|
+
let a;
|
|
187
|
+
a = rotate(0x11112222, 4);
|
|
188
|
+
assertEquals(a, 286401057); // 0x11122221
|
|
189
|
+
a = rotate(0x11112222, -4);
|
|
190
|
+
assertEquals(a, 554766882); // 0x21111222
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
// Reverse all bits.
|
|
195
|
+
Deno.test("reverse", () => {
|
|
196
|
+
let a;
|
|
197
|
+
a = reverse(0xFFFF0000);
|
|
198
|
+
assertEquals(a, 65535); // 0x0000FFFF
|
|
199
|
+
a = reverse(0x00AABBCC);
|
|
200
|
+
assertEquals(a, 870143232); // 0x33DD5500
|
|
201
|
+
a = reverse(0x1234);
|
|
202
|
+
assertEquals(a, 742916096); // 0x2C480000
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
// Merge bits as per mask.
|
|
207
|
+
Deno.test("merge", () => {
|
|
208
|
+
let a;
|
|
209
|
+
a = merge(0x12, 0x24, 0x0F);
|
|
210
|
+
assertEquals(a, 20); // 0x14
|
|
211
|
+
a = merge(0x1234, 0xABCD, 0x0F0F);
|
|
212
|
+
assertEquals(a, 6973); // 0x1B3D
|
|
213
|
+
a = merge(0xAAAA, 0xBBBB, 0x3333);
|
|
214
|
+
assertEquals(a, 48059); // 0xBBBB
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
// Interleave bits of two int16s.
|
|
219
|
+
Deno.test("interleave", () => {
|
|
220
|
+
let a;
|
|
221
|
+
a = interleave(0x0000, 0xFFFF);
|
|
222
|
+
assertEquals(a, 1431655765); // 0x55555555
|
|
223
|
+
a = interleave(0x1234, 0x1234);
|
|
224
|
+
assertEquals(a, 51121968); // 0x030C0F30
|
|
225
|
+
a = interleave(0x1234, 0x4321);
|
|
226
|
+
assertEquals(a, 302845473); // 0x120D0E21
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
// Sign extend variable bit-width integer.
|
|
231
|
+
Deno.test("signExtend", () => {
|
|
232
|
+
let a;
|
|
233
|
+
a = signExtend(15, 4);
|
|
234
|
+
assertEquals(a, -1); // 1111 ⇒ -1
|
|
235
|
+
a = signExtend(3, 3);
|
|
236
|
+
assertEquals(a, 3); // 011 ⇒ 3
|
|
237
|
+
a = signExtend(4, 3);
|
|
238
|
+
assertEquals(a, -4); // 100 ⇒ -4
|
|
239
|
+
});
|
package/package.json
CHANGED
|
@@ -1,61 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extra-bit.web",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"
|
|
5
|
-
"sideEffects": false,
|
|
6
|
-
"main": "index.js",
|
|
7
|
-
"module": "index.mjs",
|
|
8
|
-
"exports": {
|
|
9
|
-
"require": "./index.js",
|
|
10
|
-
"import": "./index.mjs"
|
|
11
|
-
},
|
|
12
|
-
"scripts": {
|
|
13
|
-
"test": "jest",
|
|
14
|
-
"build": "node ./build",
|
|
15
|
-
"publish-docs": "node ./build publish-docs",
|
|
16
|
-
"publish-packages": "node ./build publish-packages"
|
|
17
|
-
},
|
|
18
|
-
"keywords": [
|
|
19
|
-
"extra",
|
|
20
|
-
"bit",
|
|
21
|
-
"bitwise",
|
|
22
|
-
"manipulation",
|
|
23
|
-
"operation",
|
|
24
|
-
"count",
|
|
25
|
-
"get",
|
|
26
|
-
"getAs",
|
|
27
|
-
"interleave",
|
|
28
|
-
"merge",
|
|
29
|
-
"parity",
|
|
30
|
-
"reverse",
|
|
31
|
-
"rotate",
|
|
32
|
-
"scan",
|
|
33
|
-
"scanReverse",
|
|
34
|
-
"set",
|
|
35
|
-
"setAs",
|
|
36
|
-
"signExtend",
|
|
37
|
-
"swap",
|
|
38
|
-
"toggle",
|
|
39
|
-
"toggleAs"
|
|
40
|
-
],
|
|
41
|
-
"author": "wolfram77@gmail.com",
|
|
3
|
+
"version": "2.5.0",
|
|
4
|
+
"type": "module",
|
|
42
5
|
"license": "MIT",
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"import": "./index.js",
|
|
12
|
+
"default": "./index.js"
|
|
13
|
+
}
|
|
49
14
|
},
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
"@rollup/plugin-commonjs": "^25.0.8",
|
|
53
|
-
"@rollup/plugin-node-resolve": "^15.3.0",
|
|
54
|
-
"@types/jest": "^29.5.13",
|
|
55
|
-
"extra-build": "^2.2.46",
|
|
56
|
-
"jest": "^29.7.0",
|
|
57
|
-
"rollup-plugin-cleanup": "^3.2.1",
|
|
58
|
-
"rollup-plugin-dts": "^6.1.1",
|
|
59
|
-
"ts-jest": "^29.2.5"
|
|
60
|
-
}
|
|
61
|
-
}
|
|
15
|
+
"dependencies": {}
|
|
16
|
+
}
|
package/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
function ones(n){return(1<<n)-1}function get(n,t,e=1){return n>>>t&ones(e)}function getAs(n,t){return n&t}function set(n,t,e=1,r=1){return n&~(ones(r)<<t)|e<<t}function setAs(n,t,e=1){return n&~t|-e&t}function toggle(n,t,e=1){return n^ones(e)<<t}function toggleAs(n,t){return n^t}function swap(n,t,e,r=1){var u=(n>>>t^n>>>e)&ones(r);return n^(u<<t|u<<e)}const MOD37_POS32=[32,0,1,26,2,23,27,0,3,16,24,30,28,11,0,13,4,7,17,0,25,22,31,15,29,10,12,6,0,21,14,9,5,20,8,19,18];function scan(n){return MOD37_POS32[(-n&n)%37]}const DEBRUIJN_POS32=[0,9,1,10,13,21,2,29,11,14,16,18,22,25,3,30,8,12,20,28,15,17,24,7,19,27,23,6,26,5,4,31];function scanReverse(n){return n|=n>>>1,n|=n>>>2,n|=n>>>4,n|=n>>>8,DEBRUIJN_POS32[130329821*(n|=n>>>16)>>>27]}function count(n){return 16843009*((n=(858993459&(n-=n>>>1&1431655765))+(n>>>2&858993459))+(n>>>4)&252645135)>>>24}function parity1(n){return n^=n>>>16,n^=n>>>8,n^=n>>>4,27030>>>(n&=15)&1}function parity(n,t=1){if(1===t)return parity1(n);for(var e=(1<<t)-1,r=0;n;)r^=n&e,n>>>=t;return r}function rotate(n,t=0){return t<0?n<<32+t|n>>>-t:n<<t|n>>32-t}function reverse(n){return(n=(n=(n=(n=n>>>1&1431655765|(1431655765&n)<<1)>>>2&858993459|(858993459&n)<<2)>>>4&252645135|(252645135&n)<<4)>>>8&16711935|(16711935&n)<<8)>>>16|n<<16}function merge(n,t,e){return n^(n^t)&e}function interleave(n,t){return(t=1431655765&((t=858993459&((t=252645135&((t=16711935&(t|t<<8))|t<<4))|t<<2))|t<<1))|(n=1431655765&((n=858993459&((n=252645135&((n=16711935&(n|n<<8))|n<<4))|n<<2))|n<<1))<<1}function signExtend(n,t=32){return n<<(t=32-t)>>t}export{count,get,getAs,interleave,merge,parity,reverse,rotate,scan,scanReverse,set,setAs,signExtend,swap,toggle,toggleAs};
|