quantizeToF16
On this page

quantizeToF16

Function

Half-precision quantization rounds a value to a binary16-representable value and returns it as f32.

Compare examples

Values between representable binary16 numbers round to the nearest one. A value already representable stays unchanged.

Example 1quantizeToF16(1.0001)
Result
1
Example 2quantizeToF16(1.0007)
Result
1.0009765625
Example 3quantizeToF16(0.5)
Result
0.5

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 previewing precision loss before using half-precision storage.

Inputs and limits

Range and precision shrink. Overflow, subnormal handling, and halfway rounding need target-specific care; the examples avoid these cases.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

WGSL
quantizeToF16(a)
GLSL ES 3.00
unpackHalf2x16(packHalf2x16(vec2(a, 0.0))).x

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

Rounds e to what an IEEE-754 binary16 can hold and returns it as an f32, so a shader can see the precision an f16 pipeline would give it without the shader-f16 extension.

Syntax

declare function quantizeToF16(a0: f32): f32
declare function quantizeToF16<V extends vec2 | vec3 | vec4>(a0: V): V
declare function quantizeToF16(e: f32): f32
declare function quantizeToF16<T extends vec2 | vec3 | vec4>(e: T): T

Parameters

Return value

f32VT

Description

Takes an f32 or a float vector. GLSL ES 3.00 has no such builtin, so it is spelled as a packHalf2x16/unpackHalf2x16 round trip, whose rounding of an exact half that spec does not pin.

Targets

WGSL
quantizeToF16(a)
GLSL ES 3.00
unpackHalf2x16(packHalf2x16(vec2(a, 0.0))).x

Examples

See also

Edit this page Report a problem