45 lines
876 B
JavaScript
45 lines
876 B
JavaScript
let F32 = new Float32Array( 1);
|
|
let S32 = new Int32Array (F32.buffer, 0, 1);
|
|
let U32 = new Uint32Array (F32.buffer, 0, 1);
|
|
|
|
// Interpret a floating short as a 32-bit integer
|
|
function fromF32(x) {
|
|
F32[0] = x;
|
|
return S32[0];
|
|
}
|
|
|
|
// Interpret a 32-bit integer as a floating short
|
|
function toF32(x) {
|
|
S32[0] = x;
|
|
return F32[0];
|
|
}
|
|
|
|
// Represent a value as a signed 32-bit integer
|
|
function s32(x) {
|
|
S32[0] = x;
|
|
return S32[0];
|
|
}
|
|
|
|
// Sign-extend a value with a given number of bits
|
|
function signExtend(value, bits) {
|
|
bits = 32 - bits;
|
|
S32[0] = value << bits;
|
|
return S32[0] >> bits;
|
|
}
|
|
|
|
// Represent a value as an unsigned 32-bit integer
|
|
function u32(x) {
|
|
U32[0] = x;
|
|
return U32[0];
|
|
}
|
|
|
|
|
|
|
|
export let Util = {
|
|
fromF32 : fromF32,
|
|
toF32 : toF32,
|
|
s32 : s32,
|
|
signExtend: signExtend,
|
|
u32 : u32
|
|
};
|