@thi.ng/binary 3.3.40 → 3.4.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/CHANGELOG.md +12 -1
- package/README.md +1 -1
- package/align.js +6 -12
- package/api.js +0 -1
- package/bytes.js +30 -23
- package/constants.js +11 -14
- package/count.js +29 -56
- package/edit.js +14 -25
- package/float.js +47 -105
- package/gray.js +12 -26
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/int.d.ts +50 -0
- package/int.js +16 -0
- package/logic.js +62 -32
- package/mask.js +8 -28
- package/one-hot.js +6 -18
- package/package.json +7 -4
- package/pow.js +22 -18
- package/rotate.js +8 -22
- package/splat.js +20 -58
- package/swizzle.js +40 -173
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2023-12-
|
|
3
|
+
- **Last updated**: 2023-12-11T10:07:09Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,17 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
## [3.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/binary@3.4.0) (2023-12-11)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add signed/unsigned int conversions ([9f23ae6](https://github.com/thi-ng/umbrella/commit/9f23ae6))
|
|
17
|
+
|
|
18
|
+
#### 🩹 Bug fixes
|
|
19
|
+
|
|
20
|
+
- update precision for float/uint conversions (both ways) ([5289c40](https://github.com/thi-ng/umbrella/commit/5289c40))
|
|
21
|
+
- add tests
|
|
22
|
+
|
|
12
23
|
### [3.3.36](https://github.com/thi-ng/umbrella/tree/@thi.ng/binary@3.3.36) (2023-11-09)
|
|
13
24
|
|
|
14
25
|
#### ♻️ Refactoring
|
package/README.md
CHANGED
package/align.js
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*/
|
|
8
|
-
export const align = (addr, size) => (size--, (addr + size) & ~size);
|
|
9
|
-
/**
|
|
10
|
-
* Returns true if `addr` is aligned to wordsize `size`.
|
|
11
|
-
*/
|
|
12
|
-
export const isAligned = (addr, size) => !(addr & (size - 1));
|
|
1
|
+
const align = (addr, size) => (size--, addr + size & ~size);
|
|
2
|
+
const isAligned = (addr, size) => !(addr & size - 1);
|
|
3
|
+
export {
|
|
4
|
+
align,
|
|
5
|
+
isAligned
|
|
6
|
+
};
|
package/api.js
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/bytes.js
CHANGED
|
@@ -1,28 +1,35 @@
|
|
|
1
1
|
import { floatToUintBits, floatToUintBits64 } from "./float.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
const bytes16 = (x, le = false) => {
|
|
3
|
+
const b0 = x & 255;
|
|
4
|
+
const b1 = x >> 8 & 255;
|
|
5
|
+
return le ? [b0, b1] : [b1, b0];
|
|
6
6
|
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
const bytes24 = (x, le = false) => {
|
|
8
|
+
const b0 = x & 255;
|
|
9
|
+
const b1 = x >> 8 & 255;
|
|
10
|
+
const b2 = x >> 16 & 255;
|
|
11
|
+
return le ? [b0, b1, b2] : [b2, b1, b0];
|
|
12
12
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
const bytes32 = (x, le = false) => {
|
|
14
|
+
const b0 = x & 255;
|
|
15
|
+
const b1 = x >> 8 & 255;
|
|
16
|
+
const b2 = x >> 16 & 255;
|
|
17
|
+
const b3 = x >> 24 & 255;
|
|
18
|
+
return le ? [b0, b1, b2, b3] : [b3, b2, b1, b0];
|
|
19
19
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
const bytes64 = (hi, lo, le = false) => {
|
|
21
|
+
return le ? bytes32(lo, le).concat(bytes32(hi, le)) : bytes32(hi, le).concat(bytes32(lo, le));
|
|
22
|
+
};
|
|
23
|
+
const bytesF32 = (x, le = false) => bytes32(floatToUintBits(x), le);
|
|
24
|
+
const bytesF64 = (x, le = false) => (
|
|
25
|
+
//@ts-ignore
|
|
26
|
+
bytes64(...floatToUintBits64(x), le)
|
|
27
|
+
);
|
|
28
|
+
export {
|
|
29
|
+
bytes16,
|
|
30
|
+
bytes24,
|
|
31
|
+
bytes32,
|
|
32
|
+
bytes64,
|
|
33
|
+
bytesF32,
|
|
34
|
+
bytesF64
|
|
24
35
|
};
|
|
25
|
-
export const bytesF32 = (x, le = false) => bytes32(floatToUintBits(x), le);
|
|
26
|
-
export const bytesF64 = (x, le = false) =>
|
|
27
|
-
//@ts-ignore
|
|
28
|
-
bytes64(...floatToUintBits64(x), le);
|
package/constants.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
const defBits = (n) => new Array(n).fill(0).map((_, i) => 1 <<
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
*/
|
|
13
|
-
export const MSB_BITS32 = defBits(32);
|
|
14
|
-
export const MASKS = new Array(33).fill(0).map((_, i) => Math.pow(2, i) - 1);
|
|
1
|
+
const defBits = (n) => new Array(n).fill(0).map((_, i) => 1 << n - 1 - i);
|
|
2
|
+
const MSB_BITS8 = defBits(8);
|
|
3
|
+
const MSB_BITS16 = defBits(16);
|
|
4
|
+
const MSB_BITS32 = defBits(32);
|
|
5
|
+
const MASKS = new Array(33).fill(0).map((_, i) => Math.pow(2, i) - 1);
|
|
6
|
+
export {
|
|
7
|
+
MASKS,
|
|
8
|
+
MSB_BITS16,
|
|
9
|
+
MSB_BITS32,
|
|
10
|
+
MSB_BITS8
|
|
11
|
+
};
|
package/count.js
CHANGED
|
@@ -1,58 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Returns number of set bits (1's) in the given array (index range).
|
|
11
|
-
*
|
|
12
|
-
* @param data -
|
|
13
|
-
* @param start -
|
|
14
|
-
* @param n -
|
|
15
|
-
*/
|
|
16
|
-
export const popCountArray = (data, start = 0, n = data.length) => {
|
|
17
|
-
let num = 0;
|
|
18
|
-
for (let end = start + n; start < end; start++) {
|
|
19
|
-
const x = data[start];
|
|
20
|
-
x > 0 && (num += popCount(x));
|
|
21
|
-
}
|
|
22
|
-
return num;
|
|
1
|
+
const popCount = (x) => (x = x - (x >>> 1 & 1431655765), x = (x & 858993459) + (x >>> 2 & 858993459), (x + (x >>> 4) & 252645135) * 16843009 >>> 24);
|
|
2
|
+
const popCountArray = (data, start = 0, n = data.length) => {
|
|
3
|
+
let num = 0;
|
|
4
|
+
for (let end = start + n; start < end; start++) {
|
|
5
|
+
const x = data[start];
|
|
6
|
+
x > 0 && (num += popCount(x));
|
|
7
|
+
}
|
|
8
|
+
return num;
|
|
23
9
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
x & 0x0000ffff && (c -= 16);
|
|
46
|
-
x & 0x00ff00ff && (c -= 8);
|
|
47
|
-
x & 0x0f0f0f0f && (c -= 4);
|
|
48
|
-
x & 0x33333333 && (c -= 2);
|
|
49
|
-
x & 0x55555555 && (c -= 1);
|
|
50
|
-
return c;
|
|
10
|
+
const hammingDist = (x, y) => popCount(x ^ y);
|
|
11
|
+
const clz32 = (x) => x !== 0 ? 31 - (Math.log(x >>> 0) / Math.LN2 | 0) : 32;
|
|
12
|
+
const ctz32 = (x) => {
|
|
13
|
+
let c = 32;
|
|
14
|
+
x &= -x;
|
|
15
|
+
x && c--;
|
|
16
|
+
x & 65535 && (c -= 16);
|
|
17
|
+
x & 16711935 && (c -= 8);
|
|
18
|
+
x & 252645135 && (c -= 4);
|
|
19
|
+
x & 858993459 && (c -= 2);
|
|
20
|
+
x & 1431655765 && (c -= 1);
|
|
21
|
+
return c;
|
|
22
|
+
};
|
|
23
|
+
const bitSize = (x) => x > 1 ? Math.ceil(Math.log2(x)) : 0;
|
|
24
|
+
export {
|
|
25
|
+
bitSize,
|
|
26
|
+
clz32,
|
|
27
|
+
ctz32,
|
|
28
|
+
hammingDist,
|
|
29
|
+
popCount,
|
|
30
|
+
popCountArray
|
|
51
31
|
};
|
|
52
|
-
/**
|
|
53
|
-
* Returns the number of bits required to encode `x`. Returns zero if
|
|
54
|
-
* `x` <= 1.
|
|
55
|
-
*
|
|
56
|
-
* @param x -
|
|
57
|
-
*/
|
|
58
|
-
export const bitSize = (x) => (x > 1 ? Math.ceil(Math.log2(x)) : 0);
|
package/edit.js
CHANGED
|
@@ -1,27 +1,16 @@
|
|
|
1
1
|
import { defMask } from "./mask.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Sets bit in given uint `x`.
|
|
18
|
-
*
|
|
19
|
-
* @param x - value
|
|
20
|
-
* @param bit - bit number (0..31)
|
|
21
|
-
*/
|
|
22
|
-
export const bitSet = (x, bit) => (x | (1 << bit)) >>> 0;
|
|
23
|
-
export const bitSetWindow = (x, y, from, to) => {
|
|
24
|
-
const m = defMask(from, to);
|
|
25
|
-
return (x & ~m) | ((y << (1 << from)) & m);
|
|
2
|
+
const bitClear = (x, bit) => (x & ~(1 << bit)) >>> 0;
|
|
3
|
+
const bitFlip = (x, bit) => (x ^ 1 << bit) >>> 0;
|
|
4
|
+
const bitSet = (x, bit) => (x | 1 << bit) >>> 0;
|
|
5
|
+
const bitSetWindow = (x, y, from, to) => {
|
|
6
|
+
const m = defMask(from, to);
|
|
7
|
+
return x & ~m | y << (1 << from) & m;
|
|
8
|
+
};
|
|
9
|
+
const bitClearWindow = (x, from, to) => x & ~defMask(from, to);
|
|
10
|
+
export {
|
|
11
|
+
bitClear,
|
|
12
|
+
bitClearWindow,
|
|
13
|
+
bitFlip,
|
|
14
|
+
bitSet,
|
|
15
|
+
bitSetWindow
|
|
26
16
|
};
|
|
27
|
-
export const bitClearWindow = (x, from, to) => x & ~defMask(from, to);
|
package/float.js
CHANGED
|
@@ -3,111 +3,53 @@ const F32 = new Float32Array(F64.buffer);
|
|
|
3
3
|
const U8 = new Uint8Array(F64.buffer);
|
|
4
4
|
const I32 = new Int32Array(F64.buffer);
|
|
5
5
|
const U32 = new Uint32Array(F64.buffer);
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
* environment's Little Endianess into account).
|
|
17
|
-
*
|
|
18
|
-
* @param x -
|
|
19
|
-
*/
|
|
20
|
-
export const floatToIntBits64 = (x) => ((F64[0] = x), IS_LE ? [I32[1], I32[0]] : [I32[0], I32[1]]);
|
|
21
|
-
/**
|
|
22
|
-
* Returns u32 representation of f64 as [hi, lo] tuple (takes
|
|
23
|
-
* environment's Little Endianess into account).
|
|
24
|
-
*
|
|
25
|
-
* @param x -
|
|
26
|
-
*/
|
|
27
|
-
export const floatToUintBits64 = (x) => ((F64[0] = x), IS_LE ? [U32[1], U32[0]] : [U32[0], U32[1]]);
|
|
28
|
-
/**
|
|
29
|
-
* Reverse op of {@link floatToIntBits64}.
|
|
30
|
-
*
|
|
31
|
-
* @param hi -
|
|
32
|
-
* @param lo -
|
|
33
|
-
*/
|
|
34
|
-
export const intBitsToFloat64 = (hi, lo) => {
|
|
35
|
-
IS_LE ? ((I32[1] = hi), (I32[0] = lo)) : ((I32[0] = hi), (I32[1] = lo));
|
|
36
|
-
return F64[0];
|
|
6
|
+
const IS_LE = (U32[0] = 1, U8[0] === 1);
|
|
7
|
+
const floatToIntBits = (x) => (F32[0] = x, I32[0]);
|
|
8
|
+
const floatToUintBits = (x) => (F32[0] = x, U32[0]);
|
|
9
|
+
const intBitsToFloat = (x) => (I32[0] = x, F32[0]);
|
|
10
|
+
const uintBitsToFloat = (x) => (U32[0] = x, F32[0]);
|
|
11
|
+
const floatToIntBits64 = (x) => (F64[0] = x, IS_LE ? [I32[1], I32[0]] : [I32[0], I32[1]]);
|
|
12
|
+
const floatToUintBits64 = (x) => (F64[0] = x, IS_LE ? [U32[1], U32[0]] : [U32[0], U32[1]]);
|
|
13
|
+
const intBitsToFloat64 = (hi, lo) => {
|
|
14
|
+
IS_LE ? (I32[1] = hi, I32[0] = lo) : (I32[0] = hi, I32[1] = lo);
|
|
15
|
+
return F64[0];
|
|
37
16
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
* @param hi -
|
|
42
|
-
* @param lo -
|
|
43
|
-
*/
|
|
44
|
-
export const uintBitsToFloat64 = (hi, lo) => {
|
|
45
|
-
IS_LE ? ((U32[1] = hi), (U32[0] = lo)) : ((U32[0] = hi), (U32[1] = lo));
|
|
46
|
-
return F64[0];
|
|
17
|
+
const uintBitsToFloat64 = (hi, lo) => {
|
|
18
|
+
IS_LE ? (U32[1] = hi, U32[0] = lo) : (U32[0] = hi, U32[1] = lo);
|
|
19
|
+
return F64[0];
|
|
47
20
|
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
21
|
+
const floatToSortableInt = (x) => {
|
|
22
|
+
if (x === -0)
|
|
23
|
+
x = 0;
|
|
24
|
+
const i = floatToIntBits(x);
|
|
25
|
+
return x < 0 ? ~i | 1 << 31 : i;
|
|
26
|
+
};
|
|
27
|
+
const __f2u = (x, n, p) => x < 0 ? x < -1 ? n : x * n : x > 1 ? p : x * p;
|
|
28
|
+
const f32u8 = (x) => __f2u(x, 128, 127) & 255;
|
|
29
|
+
const f32u16 = (x) => __f2u(x, 32768, 32767) & 65535;
|
|
30
|
+
const f32u24 = (x) => __f2u(x, 8388608, 8388607) & 16777215;
|
|
31
|
+
const f32u32 = (x) => __f2u(x, 2147483648, 2147483647) >>> 0;
|
|
32
|
+
const u8f32 = (x) => (x &= 255, (x | (x >> 7) * 4294967040) / (127 + (x >> 7)));
|
|
33
|
+
const u16f32 = (x) => (x &= 65535, (x | (x >> 15) * 4294901760) / (32767 + (x >> 15)));
|
|
34
|
+
const u24f32 = (x) => (x &= 16777215, (x | (x >> 23) * 4278190080) / (8388607 + (x >> 23)));
|
|
35
|
+
const u32f32 = (x) => (x | 0) / (2147483647 + (x >>> 31));
|
|
36
|
+
export {
|
|
37
|
+
IS_LE,
|
|
38
|
+
f32u16,
|
|
39
|
+
f32u24,
|
|
40
|
+
f32u32,
|
|
41
|
+
f32u8,
|
|
42
|
+
floatToIntBits,
|
|
43
|
+
floatToIntBits64,
|
|
44
|
+
floatToSortableInt,
|
|
45
|
+
floatToUintBits,
|
|
46
|
+
floatToUintBits64,
|
|
47
|
+
intBitsToFloat,
|
|
48
|
+
intBitsToFloat64,
|
|
49
|
+
u16f32,
|
|
50
|
+
u24f32,
|
|
51
|
+
u32f32,
|
|
52
|
+
u8f32,
|
|
53
|
+
uintBitsToFloat,
|
|
54
|
+
uintBitsToFloat64
|
|
64
55
|
};
|
|
65
|
-
const clamp11 = (x) => (x < -1 ? -1 : x > 1 ? 1 : x);
|
|
66
|
-
/**
|
|
67
|
-
* Converts normalized float ([-1..1] range) to u8.
|
|
68
|
-
*
|
|
69
|
-
* @param x -
|
|
70
|
-
*/
|
|
71
|
-
export const f32u8 = (x) => (clamp11(x) * 0x7f) & 0xff;
|
|
72
|
-
/**
|
|
73
|
-
* Converts normalized float ([-1..1] range) to u16.
|
|
74
|
-
*
|
|
75
|
-
* @param x -
|
|
76
|
-
*/
|
|
77
|
-
export const f32u16 = (x) => (clamp11(x) * 0x7fff) & 0xffff;
|
|
78
|
-
/**
|
|
79
|
-
* Converts normalized float ([-1..1] range) to u24.
|
|
80
|
-
*
|
|
81
|
-
* @param x -
|
|
82
|
-
*/
|
|
83
|
-
export const f32u24 = (x) => (clamp11(x) * 0x7fffff) & 0xffffff;
|
|
84
|
-
/**
|
|
85
|
-
* Converts normalized float ([-1..1] range) to u32.
|
|
86
|
-
*
|
|
87
|
-
* @param x -
|
|
88
|
-
*/
|
|
89
|
-
export const f32u32 = (x) => (clamp11(x) * 0x7fffffff) >>> 0;
|
|
90
|
-
/**
|
|
91
|
-
* Reverse op of {@link f32u8}.
|
|
92
|
-
*
|
|
93
|
-
* @param x -
|
|
94
|
-
*/
|
|
95
|
-
export const u8f32 = (x) => ((x &= 0xff), (x | ((x >> 7) * 0xffffff00)) / 0x7f);
|
|
96
|
-
/**
|
|
97
|
-
* Reverse op of {@link f32u16}.
|
|
98
|
-
*
|
|
99
|
-
* @param x -
|
|
100
|
-
*/
|
|
101
|
-
export const u16f32 = (x) => ((x &= 0xffff), (x | ((x >> 15) * 0xffff0000)) / 0x7fff);
|
|
102
|
-
/**
|
|
103
|
-
* Reverse op of {@link f32u24}.
|
|
104
|
-
*
|
|
105
|
-
* @param x -
|
|
106
|
-
*/
|
|
107
|
-
export const u24f32 = (x) => ((x &= 0xffffff), (x | ((x >> 23) * 0xff000000)) / 0x7fffff);
|
|
108
|
-
/**
|
|
109
|
-
* Reverse op of {@link f32u32}.
|
|
110
|
-
*
|
|
111
|
-
* @param x -
|
|
112
|
-
*/
|
|
113
|
-
export const u32f32 = (x) => (x | 0) / 0x7fffffff;
|
package/gray.js
CHANGED
|
@@ -1,27 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Converts 32bit Gray code to binary / unsigned int.
|
|
15
|
-
*
|
|
16
|
-
* @remarks
|
|
17
|
-
* Reference:
|
|
18
|
-
* - https://en.wikipedia.org/wiki/Gray_code
|
|
19
|
-
*/
|
|
20
|
-
export const decodeGray32 = (x) => {
|
|
21
|
-
x = x ^ (x >>> 16);
|
|
22
|
-
x = x ^ (x >>> 8);
|
|
23
|
-
x = x ^ (x >>> 4);
|
|
24
|
-
x = x ^ (x >>> 2);
|
|
25
|
-
x = x ^ (x >>> 1);
|
|
26
|
-
return x >>> 0;
|
|
1
|
+
const encodeGray32 = (x) => (x ^ x >>> 1) >>> 0;
|
|
2
|
+
const decodeGray32 = (x) => {
|
|
3
|
+
x = x ^ x >>> 16;
|
|
4
|
+
x = x ^ x >>> 8;
|
|
5
|
+
x = x ^ x >>> 4;
|
|
6
|
+
x = x ^ x >>> 2;
|
|
7
|
+
x = x ^ x >>> 1;
|
|
8
|
+
return x >>> 0;
|
|
9
|
+
};
|
|
10
|
+
export {
|
|
11
|
+
decodeGray32,
|
|
12
|
+
encodeGray32
|
|
27
13
|
};
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/int.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { FnN } from "@thi.ng/api";
|
|
2
|
+
/**
|
|
3
|
+
* Converts signed i8 to unsigned u8 value. Input is assumed to be in
|
|
4
|
+
* [-0x80,0x7f] range. Does NOT perform clamping. Branchless.
|
|
5
|
+
*
|
|
6
|
+
* @param x
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare const i8u8: FnN;
|
|
10
|
+
/**
|
|
11
|
+
* Converts signed i16 to unsigned u16 value. Input is assumed to be in
|
|
12
|
+
* [-0x8000,0x7fff] range. Does NOT perform clamping. Branchless.
|
|
13
|
+
*
|
|
14
|
+
* @param x
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
export declare const i16u16: FnN;
|
|
18
|
+
/**
|
|
19
|
+
* Converts signed i32 to unsigned u32 value. Input is assumed to be in i32
|
|
20
|
+
* range.
|
|
21
|
+
*
|
|
22
|
+
* @param x
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
export declare const i32u32: FnN;
|
|
26
|
+
/**
|
|
27
|
+
* Converts unsigned u8 to signed i8 value. Branchless.
|
|
28
|
+
*
|
|
29
|
+
* @param x
|
|
30
|
+
*/
|
|
31
|
+
export declare const u8i8: FnN;
|
|
32
|
+
/**
|
|
33
|
+
* Converts unsigned u16 to signed i16 value. Branchless.
|
|
34
|
+
*
|
|
35
|
+
* @param x
|
|
36
|
+
*/
|
|
37
|
+
export declare const u16i16: FnN;
|
|
38
|
+
/**
|
|
39
|
+
* Converts unsigned u24 to signed i24 value. Branchless.
|
|
40
|
+
*
|
|
41
|
+
* @param x
|
|
42
|
+
*/
|
|
43
|
+
export declare const u24i24: FnN;
|
|
44
|
+
/**
|
|
45
|
+
* Converts unsigned u32 to signed i32 value.
|
|
46
|
+
*
|
|
47
|
+
* @param x
|
|
48
|
+
*/
|
|
49
|
+
export declare const u32i32: FnN;
|
|
50
|
+
//# sourceMappingURL=int.d.ts.map
|
package/int.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const i8u8 = (x) => x >>> 0 & 255;
|
|
2
|
+
const i16u16 = (x) => x >>> 0 & 65535;
|
|
3
|
+
const i32u32 = (x) => x >>> 0;
|
|
4
|
+
const u8i8 = (x) => (x &= 255, x - (x >> 7 << 8));
|
|
5
|
+
const u16i16 = (x) => (x &= 65535, x - (x >> 15 << 16));
|
|
6
|
+
const u24i24 = (x) => (x &= 16777215, x - (x >> 23 << 24));
|
|
7
|
+
const u32i32 = (x) => x | 0;
|
|
8
|
+
export {
|
|
9
|
+
i16u16,
|
|
10
|
+
i32u32,
|
|
11
|
+
i8u8,
|
|
12
|
+
u16i16,
|
|
13
|
+
u24i24,
|
|
14
|
+
u32i32,
|
|
15
|
+
u8i8
|
|
16
|
+
};
|
package/logic.js
CHANGED
|
@@ -1,35 +1,65 @@
|
|
|
1
1
|
import { maskL } from "./mask.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
2
|
+
const bitNot = (x) => ~x;
|
|
3
|
+
const bitAnd = (a, b) => a & b;
|
|
4
|
+
const bitNand = (a, b) => ~(a & b);
|
|
5
|
+
const bitOr = (a, b) => a | b;
|
|
6
|
+
const bitNor = (a, b) => ~(a | b);
|
|
7
|
+
const bitXor = (a, b) => a ^ b;
|
|
8
|
+
const bitXnor = (a, b) => ~(a ^ b);
|
|
9
|
+
const bitImply = (a, b) => ~a | b;
|
|
10
|
+
const bitAoi21 = (a, b, c) => ~(a | b & c);
|
|
11
|
+
const bitOai21 = (a, b, c) => ~(a & (b | c));
|
|
12
|
+
const bitAoi22 = (a, b, c, d) => ~(a & b | c & d);
|
|
13
|
+
const bitOai22 = (a, b, c, d) => ~((a | b) & (c | d));
|
|
14
|
+
const bitMux = (a, b, s) => (a & ~s | b & s) >>> 0;
|
|
15
|
+
const bitDemux = (a, b, s) => [
|
|
16
|
+
(a & ~s) >>> 0,
|
|
17
|
+
(b & s) >>> 0
|
|
18
18
|
];
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
19
|
+
const bitNotM = (n, x) => maskL(n, ~x);
|
|
20
|
+
const bitAndM = (n, a, b) => maskL(n, a & b);
|
|
21
|
+
const bitNandM = (n, a, b) => maskL(n, ~(a & b));
|
|
22
|
+
const bitOrM = (n, a, b) => maskL(n, a | b);
|
|
23
|
+
const bitNorM = (n, a, b) => maskL(n, ~(a | b));
|
|
24
|
+
const bitXorM = (n, a, b) => maskL(n, a ^ b);
|
|
25
|
+
const bitXnorM = (n, a, b) => maskL(n, ~(a ^ b));
|
|
26
|
+
const bitImplyM = (n, a, b) => maskL(n, ~a | b);
|
|
27
|
+
const bitAoi21M = (n, a, b, c) => maskL(n, ~(a | b & c));
|
|
28
|
+
const bitOai21M = (n, a, b, c) => maskL(n, ~(a & (b | c)));
|
|
29
|
+
const bitAoi22M = (n, a, b, c, d) => maskL(n, ~(a & b | c & d));
|
|
30
|
+
const bitOai22M = (n, a, b, c, d) => maskL(n, ~((a | b) & (c | d)));
|
|
31
|
+
const bitMuxM = (n, a, b, s) => maskL(n, a & ~s | b & s);
|
|
32
|
+
const bitDemuxM = (n, a, b, s) => [
|
|
33
|
+
maskL(n, a & ~s),
|
|
34
|
+
maskL(n, b & s)
|
|
35
35
|
];
|
|
36
|
+
export {
|
|
37
|
+
bitAnd,
|
|
38
|
+
bitAndM,
|
|
39
|
+
bitAoi21,
|
|
40
|
+
bitAoi21M,
|
|
41
|
+
bitAoi22,
|
|
42
|
+
bitAoi22M,
|
|
43
|
+
bitDemux,
|
|
44
|
+
bitDemuxM,
|
|
45
|
+
bitImply,
|
|
46
|
+
bitImplyM,
|
|
47
|
+
bitMux,
|
|
48
|
+
bitMuxM,
|
|
49
|
+
bitNand,
|
|
50
|
+
bitNandM,
|
|
51
|
+
bitNor,
|
|
52
|
+
bitNorM,
|
|
53
|
+
bitNot,
|
|
54
|
+
bitNotM,
|
|
55
|
+
bitOai21,
|
|
56
|
+
bitOai21M,
|
|
57
|
+
bitOai22,
|
|
58
|
+
bitOai22M,
|
|
59
|
+
bitOr,
|
|
60
|
+
bitOrM,
|
|
61
|
+
bitXnor,
|
|
62
|
+
bitXnorM,
|
|
63
|
+
bitXor,
|
|
64
|
+
bitXorM
|
|
65
|
+
};
|
package/mask.js
CHANGED
|
@@ -1,29 +1,9 @@
|
|
|
1
1
|
import { MASKS } from "./constants.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* ```
|
|
11
|
-
*
|
|
12
|
-
* @param a - first bit
|
|
13
|
-
* @param b - last bit
|
|
14
|
-
*/
|
|
15
|
-
export const defMask = (a, b) => (~MASKS[a] & MASKS[b]) >>> 0;
|
|
16
|
-
/**
|
|
17
|
-
* Returns unsigned version of `x` with only lowest `n` bits.
|
|
18
|
-
*
|
|
19
|
-
* @param n - number of LSB bits
|
|
20
|
-
* @param x - value
|
|
21
|
-
*/
|
|
22
|
-
export const maskL = (n, x) => (x & MASKS[n]) >>> 0;
|
|
23
|
-
/**
|
|
24
|
-
* Returns unsigned version of `x` with only highest `n` bits.
|
|
25
|
-
*
|
|
26
|
-
* @param n - number of MSB bits
|
|
27
|
-
* @param x - value
|
|
28
|
-
*/
|
|
29
|
-
export const maskH = (n, x) => (x & ~MASKS[n]) >>> 0;
|
|
2
|
+
const defMask = (a, b) => (~MASKS[a] & MASKS[b]) >>> 0;
|
|
3
|
+
const maskL = (n, x) => (x & MASKS[n]) >>> 0;
|
|
4
|
+
const maskH = (n, x) => (x & ~MASKS[n]) >>> 0;
|
|
5
|
+
export {
|
|
6
|
+
defMask,
|
|
7
|
+
maskH,
|
|
8
|
+
maskL
|
|
9
|
+
};
|
package/one-hot.js
CHANGED
|
@@ -1,19 +1,7 @@
|
|
|
1
1
|
import { clz32 } from "./count.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* @param x -
|
|
9
|
-
*/
|
|
10
|
-
export const binaryOneHot = (x) => (1 << x) >>> 0;
|
|
11
|
-
/**
|
|
12
|
-
* Converts one-hot `x` into binary, i.e. the position of the hot bit.
|
|
13
|
-
*
|
|
14
|
-
* @remarks
|
|
15
|
-
* Reference: https://en.wikipedia.org/wiki/One-hot
|
|
16
|
-
*
|
|
17
|
-
* @param x -
|
|
18
|
-
*/
|
|
19
|
-
export const oneHotBinary = (x) => 31 - clz32(x);
|
|
2
|
+
const binaryOneHot = (x) => 1 << x >>> 0;
|
|
3
|
+
const oneHotBinary = (x) => 31 - clz32(x);
|
|
4
|
+
export {
|
|
5
|
+
binaryOneHot,
|
|
6
|
+
oneHotBinary
|
|
7
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/binary",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.4.0",
|
|
4
4
|
"description": "100+ assorted binary / bitwise operations, conversions, utilities, lookup tables",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
28
30
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -33,10 +35,11 @@
|
|
|
33
35
|
"test": "bun test"
|
|
34
36
|
},
|
|
35
37
|
"dependencies": {
|
|
36
|
-
"@thi.ng/api": "^8.9.
|
|
38
|
+
"@thi.ng/api": "^8.9.12"
|
|
37
39
|
},
|
|
38
40
|
"devDependencies": {
|
|
39
41
|
"@microsoft/api-extractor": "^7.38.3",
|
|
42
|
+
"esbuild": "^0.19.8",
|
|
40
43
|
"rimraf": "^5.0.5",
|
|
41
44
|
"tools": "^0.0.1",
|
|
42
45
|
"typedoc": "^0.25.4",
|
|
@@ -127,5 +130,5 @@
|
|
|
127
130
|
"transducers-binary"
|
|
128
131
|
]
|
|
129
132
|
},
|
|
130
|
-
"gitHead": "
|
|
133
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
|
|
131
134
|
}
|
package/pow.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return x + 1;
|
|
1
|
+
const isPow2 = (x) => !!x && !(x & x - 1);
|
|
2
|
+
const ceilPow2 = (x) => {
|
|
3
|
+
x += x === 0;
|
|
4
|
+
--x;
|
|
5
|
+
x |= x >>> 1;
|
|
6
|
+
x |= x >>> 2;
|
|
7
|
+
x |= x >>> 4;
|
|
8
|
+
x |= x >>> 8;
|
|
9
|
+
x |= x >>> 16;
|
|
10
|
+
return x + 1;
|
|
12
11
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
const floorPow2 = (x) => {
|
|
13
|
+
x |= x >>> 1;
|
|
14
|
+
x |= x >>> 2;
|
|
15
|
+
x |= x >>> 4;
|
|
16
|
+
x |= x >>> 8;
|
|
17
|
+
x |= x >>> 16;
|
|
18
|
+
return x - (x >>> 1);
|
|
19
|
+
};
|
|
20
|
+
export {
|
|
21
|
+
ceilPow2,
|
|
22
|
+
floorPow2,
|
|
23
|
+
isPow2
|
|
20
24
|
};
|
package/rotate.js
CHANGED
|
@@ -1,22 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* Rotates `x` `n` bits to the right.
|
|
10
|
-
*
|
|
11
|
-
* @param x - value
|
|
12
|
-
* @param n - rotation step
|
|
13
|
-
*/
|
|
14
|
-
export const rotateRight = (x, n) => ((x >>> n) | (x << (32 - n))) >>> 0;
|
|
15
|
-
/**
|
|
16
|
-
* Shifts `x` by `n` bits left or right. If `n` >= 0, the value will be `>>>`
|
|
17
|
-
* shifted to right, if `n` < 0 the value will be shifted left.
|
|
18
|
-
*
|
|
19
|
-
* @param x -
|
|
20
|
-
* @param n -
|
|
21
|
-
*/
|
|
22
|
-
export const shiftRL = (x, n) => (n < 0 ? x << -n : x >>> n);
|
|
1
|
+
const rotateLeft = (x, n) => (x << n | x >>> 32 - n) >>> 0;
|
|
2
|
+
const rotateRight = (x, n) => (x >>> n | x << 32 - n) >>> 0;
|
|
3
|
+
const shiftRL = (x, n) => n < 0 ? x << -n : x >>> n;
|
|
4
|
+
export {
|
|
5
|
+
rotateLeft,
|
|
6
|
+
rotateRight,
|
|
7
|
+
shiftRL
|
|
8
|
+
};
|
package/splat.js
CHANGED
|
@@ -1,58 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
*
|
|
22
|
-
* @param x -
|
|
23
|
-
*/
|
|
24
|
-
export const splat8_32 = (x) => ((x & 0xff) * 0x01010101) >>> 0;
|
|
25
|
-
/**
|
|
26
|
-
* Repeats lowest 16bit of `x` as 32 bit uint.
|
|
27
|
-
*
|
|
28
|
-
* @param x -
|
|
29
|
-
*/
|
|
30
|
-
export const splat16_32 = (x) => ((x &= 0xffff), ((x << 16) | x) >>> 0);
|
|
31
|
-
/**
|
|
32
|
-
* Returns true if bits 0-3 are same as bits 4-7.
|
|
33
|
-
*
|
|
34
|
-
* @param x -
|
|
35
|
-
*/
|
|
36
|
-
export const same4 = (x) => ((x >> 4) & 0xf) === (x & 0xf);
|
|
37
|
-
/**
|
|
38
|
-
* Returns true if bits 0-7 are same as bits 8-15.
|
|
39
|
-
*
|
|
40
|
-
* @param x -
|
|
41
|
-
*/
|
|
42
|
-
export const same8 = (x) => ((x >> 8) & 0xff) === (x & 0xff);
|
|
43
|
-
/**
|
|
44
|
-
* Expands 3x4bit value like `0xabc` to 24bits: `0xaabbcc`
|
|
45
|
-
*
|
|
46
|
-
* @param x -
|
|
47
|
-
*/
|
|
48
|
-
export const interleave4_12_24 = (x) => ((x & 0xf00) * 0x1100) | ((x & 0xf0) * 0x110) | ((x & 0xf) * 0x11);
|
|
49
|
-
/**
|
|
50
|
-
* Expands 4x4bit value like `0xabcd` to 32bits: `0xaabbccdd`
|
|
51
|
-
*
|
|
52
|
-
* @param x -
|
|
53
|
-
*/
|
|
54
|
-
export const interleave4_16_32 = (x) => (((x & 0xf000) * 0x11000) |
|
|
55
|
-
((x & 0xf00) * 0x1100) |
|
|
56
|
-
((x & 0xf0) * 0x110) |
|
|
57
|
-
((x & 0xf) * 0x11)) >>>
|
|
58
|
-
0;
|
|
1
|
+
const splat4_24 = (x) => (x & 15) * 1118481;
|
|
2
|
+
const splat4_32 = (x) => (x & 15) * 286331153 >>> 0;
|
|
3
|
+
const splat8_24 = (x) => (x & 255) * 65793;
|
|
4
|
+
const splat8_32 = (x) => (x & 255) * 16843009 >>> 0;
|
|
5
|
+
const splat16_32 = (x) => (x &= 65535, (x << 16 | x) >>> 0);
|
|
6
|
+
const same4 = (x) => (x >> 4 & 15) === (x & 15);
|
|
7
|
+
const same8 = (x) => (x >> 8 & 255) === (x & 255);
|
|
8
|
+
const interleave4_12_24 = (x) => (x & 3840) * 4352 | (x & 240) * 272 | (x & 15) * 17;
|
|
9
|
+
const interleave4_16_32 = (x) => ((x & 61440) * 69632 | (x & 3840) * 4352 | (x & 240) * 272 | (x & 15) * 17) >>> 0;
|
|
10
|
+
export {
|
|
11
|
+
interleave4_12_24,
|
|
12
|
+
interleave4_16_32,
|
|
13
|
+
same4,
|
|
14
|
+
same8,
|
|
15
|
+
splat16_32,
|
|
16
|
+
splat4_24,
|
|
17
|
+
splat4_32,
|
|
18
|
+
splat8_24,
|
|
19
|
+
splat8_32
|
|
20
|
+
};
|
package/swizzle.js
CHANGED
|
@@ -1,176 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* @param lane - lane ID enum
|
|
10
|
-
*/
|
|
11
|
-
export const lane16 = (x, lane) => (x >>> ((1 - lane) << 4)) & 0xffff;
|
|
12
|
-
/**
|
|
13
|
-
* Extracts 8-bit lane from given 32bit uint and returns as unsigned
|
|
14
|
-
* byte [0x00 .. 0xff].
|
|
15
|
-
*
|
|
16
|
-
* - Lane #0: bits 24-31
|
|
17
|
-
* - Lane #1: bits 16-23
|
|
18
|
-
* - Lane #2: bits 8-15
|
|
19
|
-
* - Lane #3: bits 0-7
|
|
20
|
-
*
|
|
21
|
-
* @param x -
|
|
22
|
-
* @param lane - lane ID enum
|
|
23
|
-
*/
|
|
24
|
-
export const lane8 = (x, lane) => (x >>> ((3 - lane) << 3)) & 0xff;
|
|
25
|
-
/**
|
|
26
|
-
* Extracts 4-bit lane from given 32bit uint and returns as unsigned
|
|
27
|
-
* nibble [0x00 .. 0x0f].
|
|
28
|
-
*
|
|
29
|
-
* - Lane #0: bits 28-31
|
|
30
|
-
* - Lane #1: bits 24-27
|
|
31
|
-
* - Lane #2: bits 20-23
|
|
32
|
-
* - Lane #3: bits 16-19
|
|
33
|
-
* - Lane #4: bits 12-15
|
|
34
|
-
* - Lane #5: bits 8-11
|
|
35
|
-
* - Lane #6: bits 4-7
|
|
36
|
-
* - Lane #7: bits 0-3
|
|
37
|
-
*
|
|
38
|
-
* @param x -
|
|
39
|
-
* @param lane - lane ID enum
|
|
40
|
-
*/
|
|
41
|
-
export const lane4 = (x, lane) => (x >>> ((7 - lane) << 2)) & 0xf;
|
|
42
|
-
export const lane2 = (x, lane) => (x >>> ((15 - lane) << 1)) & 0x3;
|
|
43
|
-
export const setLane16 = (x, y, lane) => lane ? mux(x, y, 0xffff) : mux(x, y << 16, 0xffff0000);
|
|
44
|
-
/**
|
|
45
|
-
* Sets 8-bit `lane` with value`y` in `x`.
|
|
46
|
-
*
|
|
47
|
-
* {@link lane8}
|
|
48
|
-
*
|
|
49
|
-
* @param x -
|
|
50
|
-
* @param y -
|
|
51
|
-
* @param lane - lane ID enum
|
|
52
|
-
*/
|
|
53
|
-
export const setLane8 = (x, y, lane) => {
|
|
54
|
-
const l = (3 - lane) << 3;
|
|
55
|
-
return ((~(0xff << l) & x) | ((y & 0xff) << l)) >>> 0;
|
|
1
|
+
const lane16 = (x, lane) => x >>> (1 - lane << 4) & 65535;
|
|
2
|
+
const lane8 = (x, lane) => x >>> (3 - lane << 3) & 255;
|
|
3
|
+
const lane4 = (x, lane) => x >>> (7 - lane << 2) & 15;
|
|
4
|
+
const lane2 = (x, lane) => x >>> (15 - lane << 1) & 3;
|
|
5
|
+
const setLane16 = (x, y, lane) => lane ? mux(x, y, 65535) : mux(x, y << 16, 4294901760);
|
|
6
|
+
const setLane8 = (x, y, lane) => {
|
|
7
|
+
const l = 3 - lane << 3;
|
|
8
|
+
return (~(255 << l) & x | (y & 255) << l) >>> 0;
|
|
56
9
|
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
* {@link lane4}
|
|
61
|
-
*
|
|
62
|
-
* @param x -
|
|
63
|
-
* @param y -
|
|
64
|
-
* @param lane - lane ID enum
|
|
65
|
-
*/
|
|
66
|
-
export const setLane4 = (x, y, lane) => {
|
|
67
|
-
const l = (7 - lane) << 2;
|
|
68
|
-
return ((~(0xf << l) & x) | ((y & 0xf) << l)) >>> 0;
|
|
10
|
+
const setLane4 = (x, y, lane) => {
|
|
11
|
+
const l = 7 - lane << 2;
|
|
12
|
+
return (~(15 << l) & x | (y & 15) << l) >>> 0;
|
|
69
13
|
};
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
14
|
+
const setLane2 = (x, y, lane) => {
|
|
15
|
+
const l = 15 - lane << 1;
|
|
16
|
+
return (~(3 << l) & x | (y & 3) << l) >>> 0;
|
|
17
|
+
};
|
|
18
|
+
const swizzle8 = (x, a, b, c, d) => (lane8(x, a) << 24 | lane8(x, b) << 16 | lane8(x, c) << 8 | lane8(x, d)) >>> 0;
|
|
19
|
+
const swizzle4 = (x, a, b, c, d, e, f, g, h) => (lane4(x, a) << 28 | lane4(x, b) << 24 | lane4(x, c) << 20 | lane4(x, d) << 16 | lane4(x, e) << 12 | lane4(x, f) << 8 | lane4(x, g) << 4 | lane4(x, h)) >>> 0;
|
|
20
|
+
const mux = (a, b, mask) => ~mask & a | mask & b;
|
|
21
|
+
const flip8 = (x) => (x >>> 24 | x >> 8 & 65280 | (x & 65280) << 8 | x << 24) >>> 0;
|
|
22
|
+
const flip16 = (x) => mux(x << 16, x >>> 16, 65535);
|
|
23
|
+
const flipBytes = flip8;
|
|
24
|
+
const swapLane02 = (x) => (x & 65280) << 16 | x >>> 16 & 65280 | x & 16711935;
|
|
25
|
+
const swapLane13 = (x) => (x & 255) << 16 | x >> 16 & 255 | x & 4278255360;
|
|
26
|
+
export {
|
|
27
|
+
flip16,
|
|
28
|
+
flip8,
|
|
29
|
+
flipBytes,
|
|
30
|
+
lane16,
|
|
31
|
+
lane2,
|
|
32
|
+
lane4,
|
|
33
|
+
lane8,
|
|
34
|
+
mux,
|
|
35
|
+
setLane16,
|
|
36
|
+
setLane2,
|
|
37
|
+
setLane4,
|
|
38
|
+
setLane8,
|
|
39
|
+
swapLane02,
|
|
40
|
+
swapLane13,
|
|
41
|
+
swizzle4,
|
|
42
|
+
swizzle8
|
|
82
43
|
};
|
|
83
|
-
/**
|
|
84
|
-
* Re-orders byte lanes in given order (MSB).
|
|
85
|
-
*
|
|
86
|
-
* @example
|
|
87
|
-
* ```ts
|
|
88
|
-
* swizzle(0x12345678, 3, 2, 1, 0) // 0x78563412
|
|
89
|
-
* swizzle(0x12345678, 1, 0, 3, 2) // 0x34127856
|
|
90
|
-
* swizzle(0x12345678, 2, 2, 0, 0) // 0x56561212
|
|
91
|
-
* ```
|
|
92
|
-
*
|
|
93
|
-
* @param x - value
|
|
94
|
-
* @param a - lane ID enum
|
|
95
|
-
* @param b - lane ID enum
|
|
96
|
-
* @param c - lane ID enum
|
|
97
|
-
* @param d - lane ID enum
|
|
98
|
-
*/
|
|
99
|
-
export const swizzle8 = (x, a, b, c, d) => ((lane8(x, a) << 24) |
|
|
100
|
-
(lane8(x, b) << 16) |
|
|
101
|
-
(lane8(x, c) << 8) |
|
|
102
|
-
lane8(x, d)) >>>
|
|
103
|
-
0;
|
|
104
|
-
/**
|
|
105
|
-
*
|
|
106
|
-
* @param x - value
|
|
107
|
-
* @param a - lane ID enum
|
|
108
|
-
* @param b - lane ID enum
|
|
109
|
-
* @param c - lane ID enum
|
|
110
|
-
* @param d - lane ID enum
|
|
111
|
-
* @param e - lane ID enum
|
|
112
|
-
* @param f - lane ID enum
|
|
113
|
-
* @param g - lane ID enum
|
|
114
|
-
* @param h - lane ID enum
|
|
115
|
-
*/
|
|
116
|
-
export const swizzle4 = (x, a, b, c, d, e, f, g, h) => ((lane4(x, a) << 28) |
|
|
117
|
-
(lane4(x, b) << 24) |
|
|
118
|
-
(lane4(x, c) << 20) |
|
|
119
|
-
(lane4(x, d) << 16) |
|
|
120
|
-
(lane4(x, e) << 12) |
|
|
121
|
-
(lane4(x, f) << 8) |
|
|
122
|
-
(lane4(x, g) << 4) |
|
|
123
|
-
lane4(x, h)) >>>
|
|
124
|
-
0;
|
|
125
|
-
/**
|
|
126
|
-
* Merges bits of `a` and `b`, selecting bits from `b` where `mask` bits
|
|
127
|
-
* are set.
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* ```ts
|
|
131
|
-
* mux(0x12345678, 0xaaaa5555, 0xffff0000)
|
|
132
|
-
* // 0xaaaa5678
|
|
133
|
-
*
|
|
134
|
-
* mux(0x12345678, 0xaaaa5555, 0x0000ffff)
|
|
135
|
-
* // 0x12345555
|
|
136
|
-
* ```
|
|
137
|
-
*
|
|
138
|
-
* @param a -
|
|
139
|
-
* @param b -
|
|
140
|
-
* @param mask -
|
|
141
|
-
*/
|
|
142
|
-
export const mux = (a, b, mask) => (~mask & a) | (mask & b);
|
|
143
|
-
/**
|
|
144
|
-
* Same as `swizzle8(x, 3, 2, 1, 0)`, but faster.
|
|
145
|
-
*
|
|
146
|
-
* @param x -
|
|
147
|
-
*/
|
|
148
|
-
export const flip8 = (x) => ((x >>> 24) | ((x >> 8) & 0xff00) | ((x & 0xff00) << 8) | (x << 24)) >>> 0;
|
|
149
|
-
/**
|
|
150
|
-
* Swaps the highest & lowest 16 bits in `x`.
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
* ```ts
|
|
154
|
-
* flip16(0x12345678)
|
|
155
|
-
* // 0x56781234
|
|
156
|
-
* ```
|
|
157
|
-
*
|
|
158
|
-
* @param x -
|
|
159
|
-
*/
|
|
160
|
-
export const flip16 = (x) => mux(x << 16, x >>> 16, 0xffff);
|
|
161
|
-
/**
|
|
162
|
-
* @deprecated renamed to {@link flip8}
|
|
163
|
-
*/
|
|
164
|
-
export const flipBytes = flip8;
|
|
165
|
-
/**
|
|
166
|
-
* Swaps bytes lanes 0 & 2 (i.e. bits 24-31 with bits 8-15)
|
|
167
|
-
*
|
|
168
|
-
* @param x -
|
|
169
|
-
*/
|
|
170
|
-
export const swapLane02 = (x) => ((x & 0xff00) << 16) | ((x >>> 16) & 0xff00) | (x & 0x00ff00ff);
|
|
171
|
-
/**
|
|
172
|
-
* Swaps bytes lanes 1 & 3 (i.e. bits 16-23 with bits 0-7)
|
|
173
|
-
*
|
|
174
|
-
* @param x -
|
|
175
|
-
*/
|
|
176
|
-
export const swapLane13 = (x) => ((x & 0xff) << 16) | ((x >> 16) & 0xff) | (x & 0xff00ff00);
|