@thi.ng/binary
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -6,2 +6,10 @@ # Change Log | ||
## [1.0.3](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.0.2...@thi.ng/binary@1.0.3) (2019-03-01) | ||
**Note:** Version bump only for package @thi.ng/binary | ||
## [1.0.2](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.0.1...@thi.ng/binary@1.0.2) (2019-02-05) | ||
@@ -8,0 +16,0 @@ |
22
count.js
@@ -6,5 +6,5 @@ /** | ||
*/ | ||
export const popCount = (x) => (x = x - ((x >>> 1) & 0x55555555), | ||
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333), | ||
((x + (x >>> 4) & 0xf0f0f0f) * 0x1010101) >>> 24); | ||
export const popCount = (x) => ((x = x - ((x >>> 1) & 0x55555555)), | ||
(x = (x & 0x33333333) + ((x >>> 2) & 0x33333333)), | ||
(((x + (x >>> 4)) & 0xf0f0f0f) * 0x1010101) >>> 24); | ||
/** | ||
@@ -24,15 +24,13 @@ * https://en.wikipedia.org/wiki/Hamming_distance | ||
*/ | ||
export const clz32 = (x) => x !== 0 ? | ||
31 - ((Math.log(x >>> 0) / Math.LN2) | 0) : | ||
32; | ||
export const clz32 = (x) => x !== 0 ? 31 - ((Math.log(x >>> 0) / Math.LN2) | 0) : 32; | ||
export const ctz32 = (x) => { | ||
let c = 32; | ||
x &= -x; | ||
x && (c--); | ||
(x & 0x0000ffff) && (c -= 16); | ||
(x & 0x00ff00ff) && (c -= 8); | ||
(x & 0x0f0f0f0f) && (c -= 4); | ||
(x & 0x33333333) && (c -= 2); | ||
(x & 0x55555555) && (c -= 1); | ||
x && c--; | ||
x & 0x0000ffff && (c -= 16); | ||
x & 0x00ff00ff && (c -= 8); | ||
x & 0x0f0f0f0f && (c -= 4); | ||
x & 0x33333333 && (c -= 2); | ||
x & 0x55555555 && (c -= 1); | ||
return c; | ||
}; |
@@ -27,2 +27,2 @@ import { defMask } from "./mask"; | ||
}; | ||
export const bitClearWindow = (x, from, to) => (x & ~defMask(from, to)); | ||
export const bitClearWindow = (x, from, to) => x & ~defMask(from, to); |
10
float.js
const F32 = new Float32Array(1); | ||
const I32 = new Int32Array(F32.buffer); | ||
const U32 = new Uint32Array(F32.buffer); | ||
export const floatToIntBits = (x) => (F32[0] = x, I32[0]); | ||
export const floatToUintBits = (x) => (F32[0] = x, U32[0]); | ||
export const intBitsToFloat = (x) => (I32[0] = x, F32[0]); | ||
export const uintBitsToFloat = (x) => (U32[0] = x, F32[0]); | ||
export const floatToIntBits = (x) => ((F32[0] = x), I32[0]); | ||
export const floatToUintBits = (x) => ((F32[0] = x), U32[0]); | ||
export const intBitsToFloat = (x) => ((I32[0] = x), F32[0]); | ||
export const uintBitsToFloat = (x) => ((U32[0] = x), F32[0]); | ||
/** | ||
@@ -21,3 +21,3 @@ * Converts given float into a sortable integer representation, using | ||
const i = floatToIntBits(x); | ||
return x < 0 ? (~i) | (1 << 31) : i; | ||
return x < 0 ? ~i | (1 << 31) : i; | ||
}; |
@@ -10,22 +10,20 @@ 'use strict'; | ||
const popCount = (x) => (x = x - ((x >>> 1) & 0x55555555), | ||
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333), | ||
((x + (x >>> 4) & 0xf0f0f0f) * 0x1010101) >>> 24); | ||
const popCount = (x) => ((x = x - ((x >>> 1) & 0x55555555)), | ||
(x = (x & 0x33333333) + ((x >>> 2) & 0x33333333)), | ||
(((x + (x >>> 4)) & 0xf0f0f0f) * 0x1010101) >>> 24); | ||
const hammingDist = (x, y) => popCount(x ^ y); | ||
const clz32 = (x) => x !== 0 ? | ||
31 - ((Math.log(x >>> 0) / Math.LN2) | 0) : | ||
32; | ||
const clz32 = (x) => x !== 0 ? 31 - ((Math.log(x >>> 0) / Math.LN2) | 0) : 32; | ||
const ctz32 = (x) => { | ||
let c = 32; | ||
x &= -x; | ||
x && (c--); | ||
(x & 0x0000ffff) && (c -= 16); | ||
(x & 0x00ff00ff) && (c -= 8); | ||
(x & 0x0f0f0f0f) && (c -= 4); | ||
(x & 0x33333333) && (c -= 2); | ||
(x & 0x55555555) && (c -= 1); | ||
x && c--; | ||
x & 0x0000ffff && (c -= 16); | ||
x & 0x00ff00ff && (c -= 8); | ||
x & 0x0f0f0f0f && (c -= 4); | ||
x & 0x33333333 && (c -= 2); | ||
x & 0x55555555 && (c -= 1); | ||
return c; | ||
}; | ||
const defMask = (a, b) => ((~MASKS[a]) & MASKS[b]) >>> 0; | ||
const defMask = (a, b) => (~MASKS[a] & MASKS[b]) >>> 0; | ||
const maskL = (n, x) => (x & MASKS[n]) >>> 0; | ||
@@ -41,3 +39,3 @@ const maskH = (n, x) => (x & ~MASKS[n]) >>> 0; | ||
}; | ||
const bitClearWindow = (x, from, to) => (x & ~defMask(from, to)); | ||
const bitClearWindow = (x, from, to) => x & ~defMask(from, to); | ||
@@ -47,6 +45,6 @@ const F32 = new Float32Array(1); | ||
const U32 = new Uint32Array(F32.buffer); | ||
const floatToIntBits = (x) => (F32[0] = x, I32[0]); | ||
const floatToUintBits = (x) => (F32[0] = x, U32[0]); | ||
const intBitsToFloat = (x) => (I32[0] = x, F32[0]); | ||
const uintBitsToFloat = (x) => (U32[0] = x, F32[0]); | ||
const floatToIntBits = (x) => ((F32[0] = x), I32[0]); | ||
const floatToUintBits = (x) => ((F32[0] = x), U32[0]); | ||
const intBitsToFloat = (x) => ((I32[0] = x), F32[0]); | ||
const uintBitsToFloat = (x) => ((U32[0] = x), F32[0]); | ||
const floatToSortableInt = (x) => { | ||
@@ -56,3 +54,3 @@ if (x === -0) | ||
const i = floatToIntBits(x); | ||
return x < 0 ? (~i) | (1 << 31) : i; | ||
return x < 0 ? ~i | (1 << 31) : i; | ||
}; | ||
@@ -77,3 +75,3 @@ | ||
const bitXnor = (n, a, b) => maskL(n, ~(a ^ b)); | ||
const bitImply = (n, a, b) => maskL(n, (~a) | b); | ||
const bitImply = (n, a, b) => maskL(n, ~a | b); | ||
const bitAoi21 = (n, a, b, c) => maskL(n, ~(a | (b & c))); | ||
@@ -86,3 +84,3 @@ const bitOai21 = (n, a, b, c) => maskL(n, ~(a & (b | c))); | ||
const isPow2 = (x) => (!!x) && !(x & (x - 1)); | ||
const isPow2 = (x) => !!x && !(x & (x - 1)); | ||
const ceilPow2 = (x) => { | ||
@@ -110,9 +108,9 @@ x += (x === 0); | ||
const splat4_24 = (x) => (x &= 0xf, splat8_24(x | x << 4)); | ||
const splat4_32 = (x) => (x &= 0xf, splat8_32(x | x << 4)); | ||
const splat4_24 = (x) => ((x &= 0xf), splat8_24(x | (x << 4))); | ||
const splat4_32 = (x) => ((x &= 0xf), splat8_32(x | (x << 4))); | ||
const splat8_24 = (x) => (x & 0xff) * 0x010101; | ||
const splat8_32 = (x) => ((x & 0xff) * 0x01010101) >>> 0; | ||
const splat16_32 = (x) => (x &= 0xffff, ((x << 16) | x) >>> 0); | ||
const same4 = (x) => (x >> 4 & 0xf) === (x & 0xf); | ||
const same8 = (x) => (x >> 8 & 0xff) === (x & 0xff); | ||
const splat16_32 = (x) => ((x &= 0xffff), ((x << 16) | x) >>> 0); | ||
const same4 = (x) => ((x >> 4) & 0xf) === (x & 0xf); | ||
const same8 = (x) => ((x >> 8) & 0xff) === (x & 0xff); | ||
@@ -122,15 +120,17 @@ const lane8 = (x, l) => (x >>> ((3 - l) << 3)) & 0xff; | ||
const lane2 = (x, l) => (x >>> ((15 - l) << 1)) & 0x3; | ||
const swizzle8 = (x, a, b, c, d) => (lane8(x, a) << 24 | | ||
lane8(x, b) << 16 | | ||
lane8(x, c) << 8 | | ||
lane8(x, d)) >>> 0; | ||
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; | ||
const flipBytes = (x) => ((x >>> 24) | (x >> 8 & 0xff00) | (x & 0xff00) << 8 | x << 24) >>> 0; | ||
const swizzle8 = (x, a, b, c, d) => ((lane8(x, a) << 24) | | ||
(lane8(x, b) << 16) | | ||
(lane8(x, c) << 8) | | ||
lane8(x, d)) >>> | ||
0; | ||
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; | ||
const flipBytes = (x) => ((x >>> 24) | ((x >> 8) & 0xff00) | ((x & 0xff00) << 8) | (x << 24)) >>> 0; | ||
@@ -137,0 +137,0 @@ exports.MASKS = MASKS; |
@@ -12,22 +12,20 @@ (function (global, factory) { | ||
const popCount = (x) => (x = x - ((x >>> 1) & 0x55555555), | ||
x = (x & 0x33333333) + ((x >>> 2) & 0x33333333), | ||
((x + (x >>> 4) & 0xf0f0f0f) * 0x1010101) >>> 24); | ||
const popCount = (x) => ((x = x - ((x >>> 1) & 0x55555555)), | ||
(x = (x & 0x33333333) + ((x >>> 2) & 0x33333333)), | ||
(((x + (x >>> 4)) & 0xf0f0f0f) * 0x1010101) >>> 24); | ||
const hammingDist = (x, y) => popCount(x ^ y); | ||
const clz32 = (x) => x !== 0 ? | ||
31 - ((Math.log(x >>> 0) / Math.LN2) | 0) : | ||
32; | ||
const clz32 = (x) => x !== 0 ? 31 - ((Math.log(x >>> 0) / Math.LN2) | 0) : 32; | ||
const ctz32 = (x) => { | ||
let c = 32; | ||
x &= -x; | ||
x && (c--); | ||
(x & 0x0000ffff) && (c -= 16); | ||
(x & 0x00ff00ff) && (c -= 8); | ||
(x & 0x0f0f0f0f) && (c -= 4); | ||
(x & 0x33333333) && (c -= 2); | ||
(x & 0x55555555) && (c -= 1); | ||
x && c--; | ||
x & 0x0000ffff && (c -= 16); | ||
x & 0x00ff00ff && (c -= 8); | ||
x & 0x0f0f0f0f && (c -= 4); | ||
x & 0x33333333 && (c -= 2); | ||
x & 0x55555555 && (c -= 1); | ||
return c; | ||
}; | ||
const defMask = (a, b) => ((~MASKS[a]) & MASKS[b]) >>> 0; | ||
const defMask = (a, b) => (~MASKS[a] & MASKS[b]) >>> 0; | ||
const maskL = (n, x) => (x & MASKS[n]) >>> 0; | ||
@@ -43,3 +41,3 @@ const maskH = (n, x) => (x & ~MASKS[n]) >>> 0; | ||
}; | ||
const bitClearWindow = (x, from, to) => (x & ~defMask(from, to)); | ||
const bitClearWindow = (x, from, to) => x & ~defMask(from, to); | ||
@@ -49,6 +47,6 @@ const F32 = new Float32Array(1); | ||
const U32 = new Uint32Array(F32.buffer); | ||
const floatToIntBits = (x) => (F32[0] = x, I32[0]); | ||
const floatToUintBits = (x) => (F32[0] = x, U32[0]); | ||
const intBitsToFloat = (x) => (I32[0] = x, F32[0]); | ||
const uintBitsToFloat = (x) => (U32[0] = x, F32[0]); | ||
const floatToIntBits = (x) => ((F32[0] = x), I32[0]); | ||
const floatToUintBits = (x) => ((F32[0] = x), U32[0]); | ||
const intBitsToFloat = (x) => ((I32[0] = x), F32[0]); | ||
const uintBitsToFloat = (x) => ((U32[0] = x), F32[0]); | ||
const floatToSortableInt = (x) => { | ||
@@ -58,3 +56,3 @@ if (x === -0) | ||
const i = floatToIntBits(x); | ||
return x < 0 ? (~i) | (1 << 31) : i; | ||
return x < 0 ? ~i | (1 << 31) : i; | ||
}; | ||
@@ -79,3 +77,3 @@ | ||
const bitXnor = (n, a, b) => maskL(n, ~(a ^ b)); | ||
const bitImply = (n, a, b) => maskL(n, (~a) | b); | ||
const bitImply = (n, a, b) => maskL(n, ~a | b); | ||
const bitAoi21 = (n, a, b, c) => maskL(n, ~(a | (b & c))); | ||
@@ -88,3 +86,3 @@ const bitOai21 = (n, a, b, c) => maskL(n, ~(a & (b | c))); | ||
const isPow2 = (x) => (!!x) && !(x & (x - 1)); | ||
const isPow2 = (x) => !!x && !(x & (x - 1)); | ||
const ceilPow2 = (x) => { | ||
@@ -112,9 +110,9 @@ x += (x === 0); | ||
const splat4_24 = (x) => (x &= 0xf, splat8_24(x | x << 4)); | ||
const splat4_32 = (x) => (x &= 0xf, splat8_32(x | x << 4)); | ||
const splat4_24 = (x) => ((x &= 0xf), splat8_24(x | (x << 4))); | ||
const splat4_32 = (x) => ((x &= 0xf), splat8_32(x | (x << 4))); | ||
const splat8_24 = (x) => (x & 0xff) * 0x010101; | ||
const splat8_32 = (x) => ((x & 0xff) * 0x01010101) >>> 0; | ||
const splat16_32 = (x) => (x &= 0xffff, ((x << 16) | x) >>> 0); | ||
const same4 = (x) => (x >> 4 & 0xf) === (x & 0xf); | ||
const same8 = (x) => (x >> 8 & 0xff) === (x & 0xff); | ||
const splat16_32 = (x) => ((x &= 0xffff), ((x << 16) | x) >>> 0); | ||
const same4 = (x) => ((x >> 4) & 0xf) === (x & 0xf); | ||
const same8 = (x) => ((x >> 8) & 0xff) === (x & 0xff); | ||
@@ -124,15 +122,17 @@ const lane8 = (x, l) => (x >>> ((3 - l) << 3)) & 0xff; | ||
const lane2 = (x, l) => (x >>> ((15 - l) << 1)) & 0x3; | ||
const swizzle8 = (x, a, b, c, d) => (lane8(x, a) << 24 | | ||
lane8(x, b) << 16 | | ||
lane8(x, c) << 8 | | ||
lane8(x, d)) >>> 0; | ||
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; | ||
const flipBytes = (x) => ((x >>> 24) | (x >> 8 & 0xff00) | (x & 0xff00) << 8 | x << 24) >>> 0; | ||
const swizzle8 = (x, a, b, c, d) => ((lane8(x, a) << 24) | | ||
(lane8(x, b) << 16) | | ||
(lane8(x, c) << 8) | | ||
lane8(x, d)) >>> | ||
0; | ||
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; | ||
const flipBytes = (x) => ((x >>> 24) | ((x >> 8) & 0xff00) | ((x & 0xff00) << 8) | (x << 24)) >>> 0; | ||
@@ -139,0 +139,0 @@ exports.MASKS = MASKS; |
@@ -9,3 +9,3 @@ import { maskL } from "./mask"; | ||
export const bitXnor = (n, a, b) => maskL(n, ~(a ^ b)); | ||
export const bitImply = (n, a, b) => maskL(n, (~a) | b); | ||
export const bitImply = (n, a, b) => maskL(n, ~a | b); | ||
export const bitAoi21 = (n, a, b, c) => maskL(n, ~(a | (b & c))); | ||
@@ -12,0 +12,0 @@ export const bitOai21 = (n, a, b, c) => maskL(n, ~(a & (b | c))); |
@@ -14,3 +14,3 @@ import { MASKS } from "./api"; | ||
*/ | ||
export const defMask = (a, b) => ((~MASKS[a]) & MASKS[b]) >>> 0; | ||
export const defMask = (a, b) => (~MASKS[a] & MASKS[b]) >>> 0; | ||
/** | ||
@@ -17,0 +17,0 @@ * Returns unsigned version of `x` with only lowest `n` bits. |
{ | ||
"name": "@thi.ng/binary", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Assorted binary / bitwise operations, conversions, utilities.", | ||
@@ -51,3 +51,3 @@ "module": "./index.js", | ||
"sideEffects": false, | ||
"gitHead": "74fb4d83013785ce7a95357c565426a046657e74" | ||
"gitHead": "e43f57c7554fd78380bba58d37ae62ca01221eeb" | ||
} |
// http://graphics.stanford.edu/~seander/bithacks.html | ||
export const isPow2 = (x) => (!!x) && !(x & (x - 1)); | ||
export const isPow2 = (x) => !!x && !(x & (x - 1)); | ||
export const ceilPow2 = (x) => { | ||
@@ -4,0 +4,0 @@ x += (x === 0); |
10
splat.js
@@ -6,3 +6,3 @@ /** | ||
*/ | ||
export const splat4_24 = (x) => (x &= 0xf, splat8_24(x | x << 4)); | ||
export const splat4_24 = (x) => ((x &= 0xf), splat8_24(x | (x << 4))); | ||
/** | ||
@@ -13,3 +13,3 @@ * Repeats lowest nibble of `x` as 32 bit uint. | ||
*/ | ||
export const splat4_32 = (x) => (x &= 0xf, splat8_32(x | x << 4)); | ||
export const splat4_32 = (x) => ((x &= 0xf), splat8_32(x | (x << 4))); | ||
/** | ||
@@ -32,3 +32,3 @@ * Repeats lowest byte of `x` as 24 bit uint. | ||
*/ | ||
export const splat16_32 = (x) => (x &= 0xffff, ((x << 16) | x) >>> 0); | ||
export const splat16_32 = (x) => ((x &= 0xffff), ((x << 16) | x) >>> 0); | ||
/** | ||
@@ -39,3 +39,3 @@ * Returns true if bits 0-3 are same as bits 4-7. | ||
*/ | ||
export const same4 = (x) => (x >> 4 & 0xf) === (x & 0xf); | ||
export const same4 = (x) => ((x >> 4) & 0xf) === (x & 0xf); | ||
/** | ||
@@ -46,2 +46,2 @@ * Returns true if bits 0-7 are same as bits 8-15. | ||
*/ | ||
export const same8 = (x) => (x >> 8 & 0xff) === (x & 0xff); | ||
export const same8 = (x) => ((x >> 8) & 0xff) === (x & 0xff); |
@@ -45,6 +45,7 @@ /** | ||
*/ | ||
export const swizzle8 = (x, a, b, c, d) => (lane8(x, a) << 24 | | ||
lane8(x, b) << 16 | | ||
lane8(x, c) << 8 | | ||
lane8(x, d)) >>> 0; | ||
export const swizzle8 = (x, a, b, c, d) => ((lane8(x, a) << 24) | | ||
(lane8(x, b) << 16) | | ||
(lane8(x, c) << 8) | | ||
lane8(x, d)) >>> | ||
0; | ||
/** | ||
@@ -62,10 +63,11 @@ * | ||
*/ | ||
export 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; | ||
export 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; | ||
/** | ||
@@ -76,2 +78,2 @@ * Same as `swizzle8(x, 3, 2, 1, 0)`, but faster. | ||
*/ | ||
export const flipBytes = (x) => ((x >>> 24) | (x >> 8 & 0xff00) | (x & 0xff00) << 8 | x << 24) >>> 0; | ||
export const flipBytes = (x) => ((x >>> 24) | ((x >> 8) & 0xff00) | ((x & 0xff00) << 8) | (x << 24)) >>> 0; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62531