bitcast
Function
A bitcast reinterprets the same bits as a different type without numeric conversion.
Related names in other languages
These related operations can differ in arguments, support, and semantics. The foreign names are not accepted TypeShade aliases. Check the target forms and restrictions.
- GLSL:
floatBitsToUint - GLSL:
floatBitsToInt - GLSL:
uintBitsToFloat - GLSL:
intBitsToFloat - HLSL:
asuint - HLSL:
asfloat
Compare examples
The display shows the same bit pattern as an unsigned integer in base 16. This differs from converting the input's numeric value to an integer.
Example 1bitcast<u32>(0.)
- Result
0x00000000
0Example 2bitcast<u32>(1.)
- Result
0x3f800000
1065353216Example 3bitcast<u32>(-2.)
- Result
0xc0000000
3221225472The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.
When to use it
Use it for inspecting floating-point encoding or decoding packed data.
Inputs and limits
The examples use finite normal floats and zero. NaN and subnormal bit patterns are not portable storage encodings in this compiler.
For shader input rules and precision, consult WGSL and GLSL ES 3.00.
Related functions
The signature, the sentence and any message are the compiler's own text at commit af54ddad5.
Reads the same 32 bits as another type, each component of a vector the same way: bitcast<u32>(x) on an f32, bitcast<f32>(x) on a u32, bitcast<vec4u>(v) on a vec4 and bitcast<vec4>(w) on a vec4u, floatBitsToUint / uintBitsToFloat on GLSL ES 3.00.
Syntax
declare function bitcast<T extends u32 | f32 | vec2u | vec3u | vec4u | vec2 | vec3 | vec4>(e: BitcastArg<T>): TA signature may name a type the ambient file declares for its own use. These are those declarations.
BitcastArgtype BitcastArg<T> = T extends vec2u ? vec2 : T extends vec3u ? vec3 : T extends vec4u ? vec4 : T extends vec2 ? vec2u : T extends vec3 ? vec3u : T extends vec4 ? vec4u : typeof u32Tag extends keyof T ? f32 : u32
Parameters
-
eBitcastArg<T>
Return value
T
Description
It reinterprets, it does not convert; u32(x) is the conversion. A NaN or subnormal bit pattern has no portable answer, so keep an integer word in a storage<array<u32>> or storage<array<vec4u>> binding, not in the bits of an f32.