Skip to content

Commit

Permalink
float32BitsToNumber/numberToFloat32Bits
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x committed Mar 17, 2022
1 parent 0adaa5d commit 0e8bf3d
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/webgpu/util/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ export const kFloat32Format = { signed: 1, exponentBits: 8, mantissaBits: 23, bi
/** FloatFormat defining IEEE754 16-bit float. */
export const kFloat16Format = { signed: 1, exponentBits: 5, mantissaBits: 10, bias: 15 } as const;

const workingData = new ArrayBuffer(4);
const workingDataU32 = new Uint32Array(workingData);
const workingDataF32 = new Float32Array(workingData);
/** Bitcast u32 (represented as integer Number) to f32 (represented as floating-point Number). */
export function float32BitsToNumber(bits: number): number {
workingDataU32[0] = bits;
return workingDataF32[0];
}
/** Bitcast f32 (represented as floating-point Number) to u32 (represented as integer Number). */
export function numberToFloat32Bits(number: number): number {
workingDataF32[0] = number;
return workingDataU32[0];
}

/**
* Decodes an IEEE754 float with the supplied format specification into a JS number.
*
Expand Down

0 comments on commit 0e8bf3d

Please sign in to comment.