Intrinsics1 min read

f32tof16

Converts a 32-bit float to 16-bit floating-point bits.

Reading Time
1 min
Word Count
82
Sections
7
Try It Live

Test f32tof16 in a live shader

Open the playground, start from a visual preset, and wire f32tof16 into the fragment stage to see how it behaves with real values.

Open Playground

The f32tof16 function converts a 32-bit float to a 16-bit floating-point representation stored in a uint.

Live Demo

Signature

bwsl
f32tof16 :: (float x) -> uint

Parameters

ParameterTypeDescription
xfloatFloat value to encode as half precision

Return Value

Returns a uint containing the 16-bit half-float bits.

Example

bwsl
float value = saturate(input.roughness);
uint halfBits = f32tof16(value);

Compiled Output

When compiled portably:

bwsl
pack_half2x16(float2(x, 0.0)) & 0xffffu

See Also