bitcast
On this page

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

Syntax

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
Word in binary: 0
Example 2bitcast<u32>(1.)
Result
0x3f800000
Word in binary: 1065353216
Example 3bitcast<u32>(-2.)
Result
0xc0000000
Word in binary: 3221225472

The 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.

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>): T

A signature may name a type the ambient file declares for its own use. These are those declarations.

BitcastArg
type 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

  • e BitcastArg<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.

Examples

See also

Edit this page Report a problem