@thi.ng/binary
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -1,2 +0,2 @@ | ||
import { Pow2 } from "./api"; | ||
import type { Pow2 } from "./api"; | ||
/** | ||
@@ -3,0 +3,0 @@ * Aligns `addr` to next multiple of `size`. The latter must be a power |
@@ -0,1 +1,2 @@ | ||
export declare type Lane16 = 0 | 1; | ||
export declare type Lane8 = 0 | 1 | 2 | 3; | ||
@@ -6,3 +7,2 @@ export declare type Lane4 = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7; | ||
export declare type Pow2 = 0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80 | 0x100 | 0x200 | 0x400 | 0x800 | 0x1000 | 0x2000 | 0x4000 | 0x8000 | 0x10000 | 0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000 | 0x400000 | 0x800000 | 0x1000000 | 0x2000000 | 0x4000000 | 0x8000000 | 0x10000000 | 0x20000000 | 0x40000000 | 0x80000000; | ||
export declare const MASKS: number[]; | ||
//# sourceMappingURL=api.d.ts.map |
@@ -6,13 +6,19 @@ # Change Log | ||
## [1.2.1](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.2.0...@thi.ng/binary@1.2.1) (2020-01-24) | ||
# [1.3.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.2.1...@thi.ng/binary@1.3.0) (2020-02-25) | ||
**Note:** Version bump only for package @thi.ng/binary | ||
### Features | ||
* **binary:** add byte conversions ([564310b](https://github.com/thi-ng/umbrella/commit/564310b45db9f6cfe8788af14e47c7346ef6a576)) | ||
* **binary:** add endianess detection, 64bit float/int/uint conv ([856e035](https://github.com/thi-ng/umbrella/commit/856e035d68d727c717ce1cbb021e171fca81e3a8)) | ||
* **binary:** add float & 64bit byte conversions ([9882196](https://github.com/thi-ng/umbrella/commit/9882196a887c842efda2c835d3b86f491893c6f9)) | ||
* **binary:** add float/int conversions ([2e02d34](https://github.com/thi-ng/umbrella/commit/2e02d345a970eeb783109c6b92b32fda6b322235)) | ||
* **binary:** add lane16/setLane16, flip8/16, mux ([1aa0a5e](https://github.com/thi-ng/umbrella/commit/1aa0a5e665ab067840ade8abdab73bfd2d0e9325)) | ||
# [1.2.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.1.1...@thi.ng/binary@1.2.0) (2019-11-30) | ||
### Features | ||
@@ -22,17 +28,4 @@ | ||
## [1.1.1](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.1.0...@thi.ng/binary@1.1.1) (2019-11-09) | ||
**Note:** Version bump only for package @thi.ng/binary | ||
# [1.1.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.0.8...@thi.ng/binary@1.1.0) (2019-07-31) | ||
### Features | ||
@@ -42,51 +35,4 @@ | ||
## [1.0.8](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.0.7...@thi.ng/binary@1.0.8) (2019-07-12) | ||
**Note:** Version bump only for package @thi.ng/binary | ||
## [1.0.7](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.0.6...@thi.ng/binary@1.0.7) (2019-07-07) | ||
**Note:** Version bump only for package @thi.ng/binary | ||
## [1.0.6](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.0.5...@thi.ng/binary@1.0.6) (2019-04-24) | ||
**Note:** Version bump only for package @thi.ng/binary | ||
## [1.0.5](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.0.4...@thi.ng/binary@1.0.5) (2019-04-02) | ||
**Note:** Version bump only for package @thi.ng/binary | ||
## [1.0.4](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@1.0.3...@thi.ng/binary@1.0.4) (2019-03-28) | ||
**Note:** Version bump only for package @thi.ng/binary | ||
# [1.0.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/binary@0.1.2...@thi.ng/binary@1.0.0) (2019-01-21) | ||
### Build System | ||
@@ -96,3 +42,2 @@ | ||
### BREAKING CHANGES | ||
@@ -108,8 +53,6 @@ | ||
# 0.1.0 (2018-10-17) | ||
### Features | ||
* **binary:** add [@thi](https://github.com/thi).ng/binary package ([458d4a0](https://github.com/thi-ng/umbrella/commit/458d4a0)) |
@@ -1,2 +0,2 @@ | ||
import { Bit } from "./api"; | ||
import type { Bit } from "./api"; | ||
/** | ||
@@ -3,0 +3,0 @@ * Clears bit in given uint `x`. |
@@ -0,1 +1,5 @@ | ||
/** | ||
* This value is true iff the environment is Little Endian. | ||
*/ | ||
export declare const IS_LE: boolean; | ||
export declare const floatToIntBits: (x: number) => number; | ||
@@ -6,5 +10,33 @@ export declare const floatToUintBits: (x: number) => number; | ||
/** | ||
* Converts given float into a sortable integer representation, using | ||
* raw bitwise conversion via {@link floatToIntBits}. | ||
* Returns i32 representation of f64 as [hi, lo] tuple (takes | ||
* environment's Little Endianess into account). | ||
* | ||
* @param x | ||
*/ | ||
export declare const floatToIntBits64: (x: number) => [number, number]; | ||
/** | ||
* Returns u32 representation of f64 as [hi, lo] tuple (takes | ||
* environment's Little Endianess into account). | ||
* | ||
* @param x | ||
*/ | ||
export declare const floatToUintBits64: (x: number) => [number, number]; | ||
/** | ||
* Reverse op of {@link floatToIntBits64}. | ||
* | ||
* @param hi | ||
* @param lo | ||
*/ | ||
export declare const intBitsToFloat64: (hi: number, lo: number) => number; | ||
/** | ||
* Reverse op of {@link floatToUintBits64}. | ||
* | ||
* @param hi | ||
* @param lo | ||
*/ | ||
export declare const uintBitsToFloat64: (hi: number, lo: number) => number; | ||
/** | ||
* Converts given float (f32) into a sortable integer representation, | ||
* using raw bitwise conversion via {@link floatToIntBits}. | ||
* | ||
* {@link https://github.com/tzaeschke/phtree/blob/master/PhTreeRevisited.pdf} | ||
@@ -16,2 +48,50 @@ * (page 3) | ||
export declare const floatToSortableInt: (x: number) => number; | ||
/** | ||
* Converts normalized float ([-1..1] range) to u8. | ||
* | ||
* @param x | ||
*/ | ||
export declare const f32u8: (x: number) => number; | ||
/** | ||
* Converts normalized float ([-1..1] range) to u16. | ||
* | ||
* @param x | ||
*/ | ||
export declare const f32u16: (x: number) => number; | ||
/** | ||
* Converts normalized float ([-1..1] range) to u24. | ||
* | ||
* @param x | ||
*/ | ||
export declare const f32u24: (x: number) => number; | ||
/** | ||
* Converts normalized float ([-1..1] range) to u32. | ||
* | ||
* @param x | ||
*/ | ||
export declare const f32u32: (x: number) => number; | ||
/** | ||
* Reverse op of {@link f32u8}. | ||
* | ||
* @param x | ||
*/ | ||
export declare const u8f32: (x: number) => number; | ||
/** | ||
* Reverse op of {@link f32u16}. | ||
* | ||
* @param x | ||
*/ | ||
export declare const u16f32: (x: number) => number; | ||
/** | ||
* Reverse op of {@link f32u24}. | ||
* | ||
* @param x | ||
*/ | ||
export declare const u24f32: (x: number) => number; | ||
/** | ||
* Reverse op of {@link f32u32}. | ||
* | ||
* @param x | ||
*/ | ||
export declare const u32f32: (x: number) => number; | ||
//# sourceMappingURL=float.d.ts.map |
98
float.js
@@ -1,4 +0,9 @@ | ||
const F32 = new Float32Array(1); | ||
const I32 = new Int32Array(F32.buffer); | ||
const U32 = new Uint32Array(F32.buffer); | ||
const F64 = new Float64Array(1); | ||
const F32 = new Float32Array(F64.buffer); | ||
const I32 = new Int32Array(F64.buffer); | ||
const U32 = new Uint32Array(F64.buffer); | ||
/** | ||
* This value is true iff the environment is Little Endian. | ||
*/ | ||
export const IS_LE = ((F64[0] = 2), U32[1] === 0x40000000); | ||
export const floatToIntBits = (x) => ((F32[0] = x), I32[0]); | ||
@@ -9,5 +14,39 @@ export const floatToUintBits = (x) => ((F32[0] = x), U32[0]); | ||
/** | ||
* Converts given float into a sortable integer representation, using | ||
* raw bitwise conversion via {@link floatToIntBits}. | ||
* Returns i32 representation of f64 as [hi, lo] tuple (takes | ||
* environment's Little Endianess into account). | ||
* | ||
* @param x | ||
*/ | ||
export const floatToIntBits64 = (x) => ((F64[0] = x), IS_LE ? [I32[1], I32[0]] : [I32[0], I32[1]]); | ||
/** | ||
* Returns u32 representation of f64 as [hi, lo] tuple (takes | ||
* environment's Little Endianess into account). | ||
* | ||
* @param x | ||
*/ | ||
export const floatToUintBits64 = (x) => ((F64[0] = x), IS_LE ? [U32[1], U32[0]] : [U32[0], U32[1]]); | ||
/** | ||
* Reverse op of {@link floatToIntBits64}. | ||
* | ||
* @param hi | ||
* @param lo | ||
*/ | ||
export const intBitsToFloat64 = (hi, lo) => { | ||
IS_LE ? ((I32[1] = hi), (I32[0] = lo)) : ((I32[0] = hi), (I32[1] = lo)); | ||
return F64[0]; | ||
}; | ||
/** | ||
* Reverse op of {@link floatToUintBits64}. | ||
* | ||
* @param hi | ||
* @param lo | ||
*/ | ||
export const uintBitsToFloat64 = (hi, lo) => { | ||
IS_LE ? ((U32[1] = hi), (U32[0] = lo)) : ((U32[0] = hi), (U32[1] = lo)); | ||
return F64[0]; | ||
}; | ||
/** | ||
* Converts given float (f32) into a sortable integer representation, | ||
* using raw bitwise conversion via {@link floatToIntBits}. | ||
* | ||
* {@link https://github.com/tzaeschke/phtree/blob/master/PhTreeRevisited.pdf} | ||
@@ -24,1 +63,50 @@ * (page 3) | ||
}; | ||
const clamp11 = (x) => (x < -1 ? -1 : x > 1 ? 1 : x); | ||
/** | ||
* Converts normalized float ([-1..1] range) to u8. | ||
* | ||
* @param x | ||
*/ | ||
export const f32u8 = (x) => (clamp11(x) * 0x7f) & 0xff; | ||
/** | ||
* Converts normalized float ([-1..1] range) to u16. | ||
* | ||
* @param x | ||
*/ | ||
export const f32u16 = (x) => (clamp11(x) * 0x7fff) & 0xffff; | ||
/** | ||
* Converts normalized float ([-1..1] range) to u24. | ||
* | ||
* @param x | ||
*/ | ||
export const f32u24 = (x) => (clamp11(x) * 0x7fffff) & 0xffffff; | ||
/** | ||
* Converts normalized float ([-1..1] range) to u32. | ||
* | ||
* @param x | ||
*/ | ||
export const f32u32 = (x) => (clamp11(x) * 0x7fffffff) >>> 0; | ||
/** | ||
* Reverse op of {@link f32u8}. | ||
* | ||
* @param x | ||
*/ | ||
export const u8f32 = (x) => ((x &= 0xff), (x | ((x >> 7) * 0xffffff00)) / 0x7f); | ||
/** | ||
* Reverse op of {@link f32u16}. | ||
* | ||
* @param x | ||
*/ | ||
export const u16f32 = (x) => ((x &= 0xffff), (x | ((x >> 15) * 0xffff0000)) / 0x7fff); | ||
/** | ||
* Reverse op of {@link f32u24}. | ||
* | ||
* @param x | ||
*/ | ||
export const u24f32 = (x) => ((x &= 0xffffff), (x | ((x >> 23) * 0xff000000)) / 0x7fffff); | ||
/** | ||
* Reverse op of {@link f32u32}. | ||
* | ||
* @param x | ||
*/ | ||
export const u32f32 = (x) => (x | 0) / 0x7fffffff; |
@@ -1,3 +0,5 @@ | ||
export * from "./api"; | ||
export type * from "./api"; | ||
export * from "./align"; | ||
export * from "./bytes"; | ||
export * from "./constants"; | ||
export * from "./count"; | ||
@@ -4,0 +6,0 @@ export * from "./edit"; |
@@ -1,3 +0,4 @@ | ||
export * from "./api"; | ||
export * from "./align"; | ||
export * from "./bytes"; | ||
export * from "./constants"; | ||
export * from "./count"; | ||
@@ -4,0 +5,0 @@ export * from "./edit"; |
111
lib/index.js
@@ -5,7 +5,69 @@ 'use strict'; | ||
const MASKS = new Array(33).fill(0).map((_, i) => Math.pow(2, i) - 1); | ||
const align = (addr, size) => (size--, (addr + size) & ~size); | ||
const isAligned = (addr, size) => !(addr & (size - 1)); | ||
const F64 = new Float64Array(1); | ||
const F32 = new Float32Array(F64.buffer); | ||
const I32 = new Int32Array(F64.buffer); | ||
const U32 = new Uint32Array(F64.buffer); | ||
const IS_LE = ((F64[0] = 2), U32[1] === 0x40000000); | ||
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 floatToIntBits64 = (x) => ((F64[0] = x), IS_LE ? [I32[1], I32[0]] : [I32[0], I32[1]]); | ||
const floatToUintBits64 = (x) => ((F64[0] = x), IS_LE ? [U32[1], U32[0]] : [U32[0], U32[1]]); | ||
const intBitsToFloat64 = (hi, lo) => { | ||
IS_LE ? ((I32[1] = hi), (I32[0] = lo)) : ((I32[0] = hi), (I32[1] = lo)); | ||
return F64[0]; | ||
}; | ||
const uintBitsToFloat64 = (hi, lo) => { | ||
IS_LE ? ((U32[1] = hi), (U32[0] = lo)) : ((U32[0] = hi), (U32[1] = lo)); | ||
return F64[0]; | ||
}; | ||
const floatToSortableInt = (x) => { | ||
if (x === -0) | ||
x = 0; | ||
const i = floatToIntBits(x); | ||
return x < 0 ? ~i | (1 << 31) : i; | ||
}; | ||
const clamp11 = (x) => (x < -1 ? -1 : x > 1 ? 1 : x); | ||
const f32u8 = (x) => (clamp11(x) * 0x7f) & 0xff; | ||
const f32u16 = (x) => (clamp11(x) * 0x7fff) & 0xffff; | ||
const f32u24 = (x) => (clamp11(x) * 0x7fffff) & 0xffffff; | ||
const f32u32 = (x) => (clamp11(x) * 0x7fffffff) >>> 0; | ||
const u8f32 = (x) => ((x &= 0xff), (x | ((x >> 7) * 0xffffff00)) / 0x7f); | ||
const u16f32 = (x) => ((x &= 0xffff), (x | ((x >> 15) * 0xffff0000)) / 0x7fff); | ||
const u24f32 = (x) => ((x &= 0xffffff), (x | ((x >> 23) * 0xff000000)) / 0x7fffff); | ||
const u32f32 = (x) => (x | 0) / 0x7fffffff; | ||
const bytes16 = (x, le = false) => { | ||
const b0 = x & 0xff; | ||
const b1 = (x >> 8) & 0xff; | ||
return le ? [b0, b1] : [b1, b0]; | ||
}; | ||
const bytes24 = (x, le = false) => { | ||
const b0 = x & 0xff; | ||
const b1 = (x >> 8) & 0xff; | ||
const b2 = (x >> 16) & 0xff; | ||
return le ? [b0, b1, b2] : [b2, b1, b0]; | ||
}; | ||
const bytes32 = (x, le = false) => { | ||
const b0 = x & 0xff; | ||
const b1 = (x >> 8) & 0xff; | ||
const b2 = (x >> 16) & 0xff; | ||
const b3 = (x >> 24) & 0xff; | ||
return le ? [b0, b1, b2, b3] : [b3, b2, b1, b0]; | ||
}; | ||
const bytes64 = (hi, lo, le = false) => { | ||
return le | ||
? bytes32(lo, le).concat(bytes32(hi, le)) | ||
: bytes32(hi, le).concat(bytes32(lo, le)); | ||
}; | ||
const bytesF32 = (x, le = false) => bytes32(floatToUintBits(x), le); | ||
const bytesF64 = (x, le = false) => | ||
bytes64(...floatToUintBits64(x), le); | ||
const MASKS = new Array(33).fill(0).map((_, i) => Math.pow(2, i) - 1); | ||
const popCount = (x) => ((x = x - ((x >>> 1) & 0x55555555)), | ||
@@ -42,16 +104,2 @@ (x = (x & 0x33333333) + ((x >>> 2) & 0x33333333)), | ||
const F32 = new Float32Array(1); | ||
const I32 = new Int32Array(F32.buffer); | ||
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 floatToSortableInt = (x) => { | ||
if (x === -0) | ||
x = 0; | ||
const i = floatToIntBits(x); | ||
return x < 0 ? ~i | (1 << 31) : i; | ||
}; | ||
const encodeGray32 = (x) => (x ^ (x >>> 1)) >>> 0; | ||
@@ -113,5 +161,7 @@ const decodeGray32 = (x) => { | ||
const lane16 = (x, lane) => (x >>> ((1 - lane) << 4)) & 0xffff; | ||
const lane8 = (x, lane) => (x >>> ((3 - lane) << 3)) & 0xff; | ||
const lane4 = (x, lane) => (x >>> ((7 - lane) << 2)) & 0xf; | ||
const lane2 = (x, lane) => (x >>> ((15 - lane) << 1)) & 0x3; | ||
const setLane16 = (x, y, lane) => lane ? mux(x, y, 0xffff) : mux(x, y << 16, 0xffff0000); | ||
const setLane8 = (x, y, lane) => { | ||
@@ -143,4 +193,8 @@ const l = (3 - lane) << 3; | ||
0; | ||
const flipBytes = (x) => ((x >>> 24) | ((x >> 8) & 0xff00) | ((x & 0xff00) << 8) | (x << 24)) >>> 0; | ||
const mux = (a, b, mask) => (~mask & a) | (mask & b); | ||
const flip8 = (x) => ((x >>> 24) | ((x >> 8) & 0xff00) | ((x & 0xff00) << 8) | (x << 24)) >>> 0; | ||
const flip16 = (x) => mux(x << 16, x >>> 16, 0xffff); | ||
const flipBytes = flip8; | ||
exports.IS_LE = IS_LE; | ||
exports.MASKS = MASKS; | ||
@@ -168,2 +222,8 @@ exports.align = align; | ||
exports.bitXor = bitXor; | ||
exports.bytes16 = bytes16; | ||
exports.bytes24 = bytes24; | ||
exports.bytes32 = bytes32; | ||
exports.bytes64 = bytes64; | ||
exports.bytesF32 = bytesF32; | ||
exports.bytesF64 = bytesF64; | ||
exports.ceilPow2 = ceilPow2; | ||
@@ -175,11 +235,21 @@ exports.clz32 = clz32; | ||
exports.encodeGray32 = encodeGray32; | ||
exports.f32u16 = f32u16; | ||
exports.f32u24 = f32u24; | ||
exports.f32u32 = f32u32; | ||
exports.f32u8 = f32u8; | ||
exports.flip16 = flip16; | ||
exports.flip8 = flip8; | ||
exports.flipBytes = flipBytes; | ||
exports.floatToIntBits = floatToIntBits; | ||
exports.floatToIntBits64 = floatToIntBits64; | ||
exports.floatToSortableInt = floatToSortableInt; | ||
exports.floatToUintBits = floatToUintBits; | ||
exports.floatToUintBits64 = floatToUintBits64; | ||
exports.floorPow2 = floorPow2; | ||
exports.hammingDist = hammingDist; | ||
exports.intBitsToFloat = intBitsToFloat; | ||
exports.intBitsToFloat64 = intBitsToFloat64; | ||
exports.isAligned = isAligned; | ||
exports.isPow2 = isPow2; | ||
exports.lane16 = lane16; | ||
exports.lane2 = lane2; | ||
@@ -190,2 +260,3 @@ exports.lane4 = lane4; | ||
exports.maskL = maskL; | ||
exports.mux = mux; | ||
exports.popCount = popCount; | ||
@@ -196,2 +267,3 @@ exports.rotateLeft = rotateLeft; | ||
exports.same8 = same8; | ||
exports.setLane16 = setLane16; | ||
exports.setLane2 = setLane2; | ||
@@ -207,2 +279,7 @@ exports.setLane4 = setLane4; | ||
exports.swizzle8 = swizzle8; | ||
exports.u16f32 = u16f32; | ||
exports.u24f32 = u24f32; | ||
exports.u32f32 = u32f32; | ||
exports.u8f32 = u8f32; | ||
exports.uintBitsToFloat = uintBitsToFloat; | ||
exports.uintBitsToFloat64 = uintBitsToFloat64; |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(((t=t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.binary={}))}(this,(function(t){"use strict";const e=new Array(33).fill(0).map((t,e)=>Math.pow(2,e)-1),i=t=>16843009*((t=(858993459&(t-=t>>>1&1431655765))+(t>>>2&858993459))+(t>>>4)&252645135)>>>24,n=(t,i)=>(~e[t]&e[i])>>>0,o=(t,i)=>(i&e[t])>>>0,a=new Float32Array(1),r=new Int32Array(a.buffer),l=new Uint32Array(a.buffer),s=t=>(a[0]=t,r[0]),b=(t,e)=>t>>>(3-e<<3)&255,f=(t,e)=>t>>>(7-e<<2)&15;t.MASKS=e,t.align=(t,e)=>t+--e&~e,t.bitAnd=(t,e,i)=>o(t,e&i),t.bitAoi21=(t,e,i,n)=>o(t,~(e|i&n)),t.bitAoi22=(t,e,i,n,a)=>o(t,~(e&i|n&a)),t.bitClear=(t,e)=>(t&~(1<<e))>>>0,t.bitClearWindow=(t,e,i)=>t&~n(e,i),t.bitDemux=(t,e,i,n)=>[o(t,e&~n),o(t,i&n)],t.bitFlip=(t,e)=>(t^1<<e)>>>0,t.bitImply=(t,e,i)=>o(t,~e|i),t.bitMux=(t,e,i,n)=>o(t,e&~n|i&n),t.bitNand=(t,e,i)=>o(t,~(e&i)),t.bitNor=(t,e,i)=>o(t,~(e&i)),t.bitNot=(t,e)=>o(t,~e),t.bitOai21=(t,e,i,n)=>o(t,~(e&(i|n))),t.bitOai22=(t,e,i,n,a)=>o(t,~((e|i)&(n|a))),t.bitOr=(t,e,i)=>o(t,e|i),t.bitSet=(t,e)=>(t|1<<e)>>>0,t.bitSetWindow=(t,e,i,o)=>{const a=n(i,o);return t&~a|e<<(1<<i)&a},t.bitSize=t=>t>1?Math.ceil(Math.log2(t)):0,t.bitXnor=(t,e,i)=>o(t,~(e^i)),t.bitXor=(t,e,i)=>o(t,e^i),t.ceilPow2=t=>(t+=0===t,--t,t|=t>>>1,t|=t>>>2,t|=t>>>4,t|=t>>>8,(t|=t>>>16)+1),t.clz32=t=>0!==t?31-(Math.log(t>>>0)/Math.LN2|0):32,t.ctz32=t=>{let e=32;return(t&=-t)&&e--,65535&t&&(e-=16),16711935&t&&(e-=8),252645135&t&&(e-=4),858993459&t&&(e-=2),1431655765&t&&(e-=1),e},t.decodeGray32=t=>(t^=t>>>16,t^=t>>>8,t^=t>>>4,t^=t>>>2,(t^=t>>>1)>>>0),t.defMask=n,t.encodeGray32=t=>(t^t>>>1)>>>0,t.flipBytes=t=>(t>>>24|t>>8&65280|(65280&t)<<8|t<<24)>>>0,t.floatToIntBits=s,t.floatToSortableInt=t=>{-0===t&&(t=0);const e=s(t);return t<0?~e|1<<31:e},t.floatToUintBits=t=>(a[0]=t,l[0]),t.floorPow2=t=>(t|=t>>>1,t|=t>>>2,t|=t>>>4,t|=t>>>8,(t|=t>>>16)-(t>>>1)),t.hammingDist=(t,e)=>i(t^e),t.intBitsToFloat=t=>(r[0]=t,a[0]),t.isAligned=(t,e)=>!(t&e-1),t.isPow2=t=>!(!t||t&t-1),t.lane2=(t,e)=>t>>>(15-e<<1)&3,t.lane4=f,t.lane8=b,t.maskH=(t,i)=>(i&~e[t])>>>0,t.maskL=o,t.popCount=i,t.rotateLeft=(t,e)=>(t<<e|t>>>32-e)>>>0,t.rotateRight=(t,e)=>(t>>>e|t<<32-e)>>>0,t.same4=t=>(t>>4&15)==(15&t),t.same8=t=>(t>>8&255)==(255&t),t.setLane2=(t,e,i)=>{const n=15-i<<1;return(~(3<<n)&t|(3&e)<<n)>>>0},t.setLane4=(t,e,i)=>{const n=7-i<<2;return(~(15<<n)&t|(15&e)<<n)>>>0},t.setLane8=(t,e,i)=>{const n=3-i<<3;return(~(255<<n)&t|(255&e)<<n)>>>0},t.splat16_32=t=>((t&=65535)<<16|t)>>>0,t.splat4_24=t=>1118481*(15&t),t.splat4_32=t=>286331153*(15&t)>>>0,t.splat8_24=t=>65793*(255&t),t.splat8_32=t=>16843009*(255&t)>>>0,t.swizzle4=(t,e,i,n,o,a,r,l,s)=>(f(t,e)<<28|f(t,i)<<24|f(t,n)<<20|f(t,o)<<16|f(t,a)<<12|f(t,r)<<8|f(t,l)<<4|f(t,s))>>>0,t.swizzle8=(t,e,i,n,o)=>(b(t,e)<<24|b(t,i)<<16|b(t,n)<<8|b(t,o))>>>0,t.uintBitsToFloat=t=>(l[0]=t,a[0]),Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(((t=t||self).thi=t.thi||{},t.thi.ng=t.thi.ng||{},t.thi.ng.binary={}))}(this,(function(t){"use strict";const e=new Float64Array(1),i=new Float32Array(e.buffer),n=new Int32Array(e.buffer),o=new Uint32Array(e.buffer),a=(e[0]=2,1073741824===o[1]),s=t=>(i[0]=t,n[0]),r=t=>(i[0]=t,o[0]),l=t=>(e[0]=t,a?[o[1],o[0]]:[o[0],o[1]]),f=t=>t<-1?-1:t>1?1:t,u=(t,e=!1)=>{const i=255&t,n=t>>8&255,o=t>>16&255,a=t>>24&255;return e?[i,n,o,a]:[a,o,n,i]},b=(t,e,i=!1)=>i?u(e,i).concat(u(t,i)):u(t,i).concat(u(e,i)),c=new Array(33).fill(0).map((t,e)=>Math.pow(2,e)-1),p=t=>16843009*((t=(858993459&(t-=t>>>1&1431655765))+(t>>>2&858993459))+(t>>>4)&252645135)>>>24,y=(t,e)=>(~c[t]&c[e])>>>0,d=(t,e)=>(e&c[t])>>>0,h=(t,e)=>t>>>(3-e<<3)&255,w=(t,e)=>t>>>(7-e<<2)&15,m=(t,e,i)=>~i&t|i&e,A=t=>(t>>>24|t>>8&65280|(65280&t)<<8|t<<24)>>>0,g=A;t.IS_LE=a,t.MASKS=c,t.align=(t,e)=>t+--e&~e,t.bitAnd=(t,e,i)=>d(t,e&i),t.bitAoi21=(t,e,i,n)=>d(t,~(e|i&n)),t.bitAoi22=(t,e,i,n,o)=>d(t,~(e&i|n&o)),t.bitClear=(t,e)=>(t&~(1<<e))>>>0,t.bitClearWindow=(t,e,i)=>t&~y(e,i),t.bitDemux=(t,e,i,n)=>[d(t,e&~n),d(t,i&n)],t.bitFlip=(t,e)=>(t^1<<e)>>>0,t.bitImply=(t,e,i)=>d(t,~e|i),t.bitMux=(t,e,i,n)=>d(t,e&~n|i&n),t.bitNand=(t,e,i)=>d(t,~(e&i)),t.bitNor=(t,e,i)=>d(t,~(e&i)),t.bitNot=(t,e)=>d(t,~e),t.bitOai21=(t,e,i,n)=>d(t,~(e&(i|n))),t.bitOai22=(t,e,i,n,o)=>d(t,~((e|i)&(n|o))),t.bitOr=(t,e,i)=>d(t,e|i),t.bitSet=(t,e)=>(t|1<<e)>>>0,t.bitSetWindow=(t,e,i,n)=>{const o=y(i,n);return t&~o|e<<(1<<i)&o},t.bitSize=t=>t>1?Math.ceil(Math.log2(t)):0,t.bitXnor=(t,e,i)=>d(t,~(e^i)),t.bitXor=(t,e,i)=>d(t,e^i),t.bytes16=(t,e=!1)=>{const i=255&t,n=t>>8&255;return e?[i,n]:[n,i]},t.bytes24=(t,e=!1)=>{const i=255&t,n=t>>8&255,o=t>>16&255;return e?[i,n,o]:[o,n,i]},t.bytes32=u,t.bytes64=b,t.bytesF32=(t,e=!1)=>u(r(t),e),t.bytesF64=(t,e=!1)=>b(...l(t),e),t.ceilPow2=t=>(t+=0===t,--t,t|=t>>>1,t|=t>>>2,t|=t>>>4,t|=t>>>8,(t|=t>>>16)+1),t.clz32=t=>0!==t?31-(Math.log(t>>>0)/Math.LN2|0):32,t.ctz32=t=>{let e=32;return(t&=-t)&&e--,65535&t&&(e-=16),16711935&t&&(e-=8),252645135&t&&(e-=4),858993459&t&&(e-=2),1431655765&t&&(e-=1),e},t.decodeGray32=t=>(t^=t>>>16,t^=t>>>8,t^=t>>>4,t^=t>>>2,(t^=t>>>1)>>>0),t.defMask=y,t.encodeGray32=t=>(t^t>>>1)>>>0,t.f32u16=t=>32767*f(t)&65535,t.f32u24=t=>8388607*f(t)&16777215,t.f32u32=t=>2147483647*f(t)>>>0,t.f32u8=t=>127*f(t)&255,t.flip16=t=>m(t<<16,t>>>16,65535),t.flip8=A,t.flipBytes=g,t.floatToIntBits=s,t.floatToIntBits64=t=>(e[0]=t,a?[n[1],n[0]]:[n[0],n[1]]),t.floatToSortableInt=t=>{-0===t&&(t=0);const e=s(t);return t<0?~e|1<<31:e},t.floatToUintBits=r,t.floatToUintBits64=l,t.floorPow2=t=>(t|=t>>>1,t|=t>>>2,t|=t>>>4,t|=t>>>8,(t|=t>>>16)-(t>>>1)),t.hammingDist=(t,e)=>p(t^e),t.intBitsToFloat=t=>(n[0]=t,i[0]),t.intBitsToFloat64=(t,i)=>(a?(n[1]=t,n[0]=i):(n[0]=t,n[1]=i),e[0]),t.isAligned=(t,e)=>!(t&e-1),t.isPow2=t=>!(!t||t&t-1),t.lane16=(t,e)=>t>>>(1-e<<4)&65535,t.lane2=(t,e)=>t>>>(15-e<<1)&3,t.lane4=w,t.lane8=h,t.maskH=(t,e)=>(e&~c[t])>>>0,t.maskL=d,t.mux=m,t.popCount=p,t.rotateLeft=(t,e)=>(t<<e|t>>>32-e)>>>0,t.rotateRight=(t,e)=>(t>>>e|t<<32-e)>>>0,t.same4=t=>(t>>4&15)==(15&t),t.same8=t=>(t>>8&255)==(255&t),t.setLane16=(t,e,i)=>i?m(t,e,65535):m(t,e<<16,4294901760),t.setLane2=(t,e,i)=>{const n=15-i<<1;return(~(3<<n)&t|(3&e)<<n)>>>0},t.setLane4=(t,e,i)=>{const n=7-i<<2;return(~(15<<n)&t|(15&e)<<n)>>>0},t.setLane8=(t,e,i)=>{const n=3-i<<3;return(~(255<<n)&t|(255&e)<<n)>>>0},t.splat16_32=t=>((t&=65535)<<16|t)>>>0,t.splat4_24=t=>1118481*(15&t),t.splat4_32=t=>286331153*(15&t)>>>0,t.splat8_24=t=>65793*(255&t),t.splat8_32=t=>16843009*(255&t)>>>0,t.swizzle4=(t,e,i,n,o,a,s,r,l)=>(w(t,e)<<28|w(t,i)<<24|w(t,n)<<20|w(t,o)<<16|w(t,a)<<12|w(t,s)<<8|w(t,r)<<4|w(t,l))>>>0,t.swizzle8=(t,e,i,n,o)=>(h(t,e)<<24|h(t,i)<<16|h(t,n)<<8|h(t,o))>>>0,t.u16f32=t=>((t&=65535)|4294901760*(t>>15))/32767,t.u24f32=t=>((t&=16777215)|4278190080*(t>>23))/8388607,t.u32f32=t=>(0|t)/2147483647,t.u8f32=t=>((t&=255)|4294967040*(t>>7))/127,t.uintBitsToFloat=t=>(o[0]=t,i[0]),t.uintBitsToFloat64=(t,i)=>(a?(o[1]=t,o[0]=i):(o[0]=t,o[1]=i),e[0]),Object.defineProperty(t,"__esModule",{value:!0})})); |
@@ -1,2 +0,2 @@ | ||
import { MASKS } from "./api"; | ||
import { MASKS } from "./constants"; | ||
/** | ||
@@ -3,0 +3,0 @@ * Creates bit mask by enabling bit `a` to bit `b-1`, both in range |
{ | ||
"name": "@thi.ng/binary", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "50+ assorted binary / bitwise operations, conversions, utilities", | ||
@@ -31,10 +31,10 @@ "module": "./index.js", | ||
"@istanbuljs/nyc-config-typescript": "^1.0.1", | ||
"@microsoft/api-extractor": "^7.7.7", | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^13.5.0", | ||
"mocha": "^7.0.0", | ||
"@microsoft/api-extractor": "^7.7.8", | ||
"@types/mocha": "^7.0.1", | ||
"@types/node": "^13.7.4", | ||
"mocha": "^7.0.1", | ||
"nyc": "^15.0.0", | ||
"ts-node": "^8.6.2", | ||
"typedoc": "^0.16.8", | ||
"typescript": "^3.7.5" | ||
"typedoc": "^0.16.10", | ||
"typescript": "^3.8.2" | ||
}, | ||
@@ -58,3 +58,3 @@ "keywords": [ | ||
"sideEffects": false, | ||
"gitHead": "93d8af817724c1c5b06d80ffa2492fe5b4fb7bc4" | ||
"gitHead": "b75ac2e62a828cc6ebf7b2aec4332bdf6f933a59" | ||
} |
@@ -1,2 +0,2 @@ | ||
import { Pow2 } from "./api"; | ||
import type { Pow2 } from "./api"; | ||
export declare const isPow2: (x: number) => x is Pow2; | ||
@@ -3,0 +3,0 @@ export declare const ceilPow2: (x: number) => number; |
<!-- This file is generated - DO NOT EDIT! --> | ||
# @thi.ng/binary | ||
# ![@thi.ng/binary](https://media.thi.ng/umbrella/banners/thing-binary.svg?1582472083) | ||
@@ -35,3 +35,3 @@ [![npm version](https://img.shields.io/npm/v/@thi.ng/binary.svg)](https://www.npmjs.com/package/@thi.ng/binary) | ||
Package sizes (gzipped): ESM: 1.2KB / CJS: 1.5KB / UMD: 1.3KB | ||
Package sizes (gzipped): ESM: 1.7KB / CJS: 2.0KB / UMD: 1.8KB | ||
@@ -38,0 +38,0 @@ ## Dependencies |
@@ -1,2 +0,2 @@ | ||
# ${pkg.name} | ||
# ${pkg.banner} | ||
@@ -3,0 +3,0 @@ [![npm version](https://img.shields.io/npm/v/${pkg.name}.svg)](https://www.npmjs.com/package/${pkg.name}) |
@@ -1,2 +0,2 @@ | ||
import { Bit } from "./api"; | ||
import type { Bit } from "./api"; | ||
/** | ||
@@ -3,0 +3,0 @@ * Rotates `x` `n` bits to the left. |
@@ -1,5 +0,17 @@ | ||
import { Lane2, Lane4, Lane8 } from "./api"; | ||
import type { Lane16, Lane2, Lane4, Lane8 } from "./api"; | ||
/** | ||
* Extracts 8-bit lane from given 32bit uint. | ||
* Extracts 16-bit lane from given 32bit uint and returns as unsigned | ||
* half word [0x0000 .. 0xffff]. | ||
* | ||
* - Lane #0: bits 16-31 | ||
* - Lane #1: bits 0-15 | ||
* | ||
* @param x - | ||
* @param lane - lane ID enum | ||
*/ | ||
export declare const lane16: (x: number, lane: Lane16) => number; | ||
/** | ||
* Extracts 8-bit lane from given 32bit uint and returns as unsigned | ||
* byte [0x00 .. 0xff]. | ||
* | ||
* - Lane #0: bits 24-31 | ||
@@ -15,3 +27,4 @@ * - Lane #1: bits 16-23 | ||
/** | ||
* Extracts 4-bit lane from given 32bit uint. | ||
* Extracts 4-bit lane from given 32bit uint and returns as unsigned | ||
* nibble [0x00 .. 0x0f]. | ||
* | ||
@@ -32,2 +45,3 @@ * - Lane #0: bits 28-31 | ||
export declare const lane2: (x: number, lane: Lane2) => number; | ||
export declare const setLane16: (x: number, y: number, lane: Lane16) => number; | ||
/** | ||
@@ -94,2 +108,20 @@ * Sets 8-bit `lane` with value`y` in `x`. | ||
/** | ||
* Merges bits of `a` and `b`, selecting bits from `b` where `mask` bits | ||
* are set. | ||
* | ||
* @example | ||
* ```ts | ||
* mux(0x12345678, 0xaaaa5555, 0xffff0000) | ||
* // 0xaaaa5678 | ||
* | ||
* mux(0x12345678, 0xaaaa5555, 0x0000ffff) | ||
* // 0x12345555 | ||
* ``` | ||
* | ||
* @param a | ||
* @param b | ||
* @param mask | ||
*/ | ||
export declare const mux: (a: number, b: number, mask: number) => number; | ||
/** | ||
* Same as `swizzle8(x, 3, 2, 1, 0)`, but faster. | ||
@@ -99,3 +131,19 @@ * | ||
*/ | ||
export declare const flip8: (x: number) => number; | ||
/** | ||
* Swaps the highest & lowest 16 bits in `x`. | ||
* | ||
* @example | ||
* ```ts | ||
* flip16(0x12345678) | ||
* // 0x56781234 | ||
* ``` | ||
* | ||
* @param x | ||
*/ | ||
export declare const flip16: (x: number) => number; | ||
/** | ||
* @deprecated renamed to {@link flip8} | ||
*/ | ||
export declare const flipBytes: (x: number) => number; | ||
//# sourceMappingURL=swizzle.d.ts.map |
/** | ||
* Extracts 8-bit lane from given 32bit uint. | ||
* Extracts 16-bit lane from given 32bit uint and returns as unsigned | ||
* half word [0x0000 .. 0xffff]. | ||
* | ||
* - Lane #0: bits 16-31 | ||
* - Lane #1: bits 0-15 | ||
* | ||
* @param x - | ||
* @param lane - lane ID enum | ||
*/ | ||
export const lane16 = (x, lane) => (x >>> ((1 - lane) << 4)) & 0xffff; | ||
/** | ||
* Extracts 8-bit lane from given 32bit uint and returns as unsigned | ||
* byte [0x00 .. 0xff]. | ||
* | ||
* - Lane #0: bits 24-31 | ||
@@ -14,3 +26,4 @@ * - Lane #1: bits 16-23 | ||
/** | ||
* Extracts 4-bit lane from given 32bit uint. | ||
* Extracts 4-bit lane from given 32bit uint and returns as unsigned | ||
* nibble [0x00 .. 0x0f]. | ||
* | ||
@@ -31,2 +44,3 @@ * - Lane #0: bits 28-31 | ||
export const lane2 = (x, lane) => (x >>> ((15 - lane) << 1)) & 0x3; | ||
export const setLane16 = (x, y, lane) => lane ? mux(x, y, 0xffff) : mux(x, y << 16, 0xffff0000); | ||
/** | ||
@@ -114,2 +128,20 @@ * Sets 8-bit `lane` with value`y` in `x`. | ||
/** | ||
* Merges bits of `a` and `b`, selecting bits from `b` where `mask` bits | ||
* are set. | ||
* | ||
* @example | ||
* ```ts | ||
* mux(0x12345678, 0xaaaa5555, 0xffff0000) | ||
* // 0xaaaa5678 | ||
* | ||
* mux(0x12345678, 0xaaaa5555, 0x0000ffff) | ||
* // 0x12345555 | ||
* ``` | ||
* | ||
* @param a | ||
* @param b | ||
* @param mask | ||
*/ | ||
export const mux = (a, b, mask) => (~mask & a) | (mask & b); | ||
/** | ||
* Same as `swizzle8(x, 3, 2, 1, 0)`, but faster. | ||
@@ -119,2 +151,18 @@ * | ||
*/ | ||
export const flipBytes = (x) => ((x >>> 24) | ((x >> 8) & 0xff00) | ((x & 0xff00) << 8) | (x << 24)) >>> 0; | ||
export const flip8 = (x) => ((x >>> 24) | ((x >> 8) & 0xff00) | ((x & 0xff00) << 8) | (x << 24)) >>> 0; | ||
/** | ||
* Swaps the highest & lowest 16 bits in `x`. | ||
* | ||
* @example | ||
* ```ts | ||
* flip16(0x12345678) | ||
* // 0x56781234 | ||
* ``` | ||
* | ||
* @param x | ||
*/ | ||
export const flip16 = (x) => mux(x << 16, x >>> 16, 0xffff); | ||
/** | ||
* @deprecated renamed to {@link flip8} | ||
*/ | ||
export const flipBytes = flip8; |
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
87637
39
1255