38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
import { BitWidth } from './bit-width.js';
|
|
export function toByteWidth(bitWidth) {
|
|
return 1 << bitWidth;
|
|
}
|
|
export function iwidth(value) {
|
|
if (value >= -128 && value <= 127)
|
|
return BitWidth.WIDTH8;
|
|
if (value >= -32768 && value <= 32767)
|
|
return BitWidth.WIDTH16;
|
|
if (value >= -2147483648 && value <= 2147483647)
|
|
return BitWidth.WIDTH32;
|
|
return BitWidth.WIDTH64;
|
|
}
|
|
export function fwidth(value) {
|
|
return value === Math.fround(value) ? BitWidth.WIDTH32 : BitWidth.WIDTH64;
|
|
}
|
|
export function uwidth(value) {
|
|
if (value <= 255)
|
|
return BitWidth.WIDTH8;
|
|
if (value <= 65535)
|
|
return BitWidth.WIDTH16;
|
|
if (value <= 4294967295)
|
|
return BitWidth.WIDTH32;
|
|
return BitWidth.WIDTH64;
|
|
}
|
|
export function fromByteWidth(value) {
|
|
if (value === 1)
|
|
return BitWidth.WIDTH8;
|
|
if (value === 2)
|
|
return BitWidth.WIDTH16;
|
|
if (value === 4)
|
|
return BitWidth.WIDTH32;
|
|
return BitWidth.WIDTH64;
|
|
}
|
|
export function paddingSize(bufSize, scalarSize) {
|
|
return (~bufSize + 1) & (scalarSize - 1);
|
|
}
|