Review: WebGPU Shading Language, floating-point evaluation¶
Citation
- Baker, A., Derin, M. O. and Neto, D. (eds.). WebGPU Shading Language. W3C Candidate Recommendation Draft, version CRD-WGSL-20261128 (28 November 2026). https://www.w3.org/TR/WGSL/. Bibliography key
w3c2026wgsl.
Sections covered¶
This review covers §15.7, "Floating Point Evaluation", which states what a WGSL implementation must compute for an f32 operation. The WGSL kernels of Hifuku rest their error bounds on these rules. Section numbers are those of the cited version. The specification is a draft and its accuracy tables can change, so a bound that uses one of them names this version.
Rounding (§15.7.2, §15.7.4)¶
WGSL follows IEEE-754 binary32 for f32, with differences that §15.7.2 lists. The first is the rounding mode:
No rounding mode is specified. An implementation may round an intermediate result up or down.
§15.7.4 defines the term "correctly rounded" in the same way. The result is the exact value \( x \) when \( x \) is in the type, and otherwise "the smallest value in T greater than x, or the largest value in T less than x". The section states: "WGSL does not specify a rounding mode."
A correctly rounded f32 result therefore has a relative error below
\( 2u = 2^{-23} \), not below \( u = 2^{-24} \). The error-free
transformations TwoSum and TwoProduct are exact only under rounding to
nearest, and the published bounds of float-float arithmetic assume it.
Hifuku therefore checks the rounding of each device with
hifuku.wgsl.device.rounds_to_nearest, and choose_arithmetic refuses a
device that fails the check.
Accuracy of operations and built-in functions (§15.7.4.1)¶
The table "Accuracy of concrete floating point operations" gives, for f32:
| Expression | Accuracy for f32 |
|---|---|
x + y, x - y, x * y, -x |
Correctly rounded |
x / y |
2.5 ULP for |y| in the range [2^-126, 2^126] |
| comparisons | Correct result |
The table "Accuracy of concrete floating point built-in functions" gives, for the built-in functions that Hifuku's kernels use:
| Built-in function | Accuracy for f32 |
|---|---|
abs, floor, round, ldexp |
Correctly rounded |
max(x, y), min(x, y) |
Correctly rounded. "If both x and y are subnormal, the result may be either input." |
frexp(x) |
"Correctly rounded, when x is zero or normal." |
fma(x, y, z) |
"Inherited from x * y + z" |
exp(x) |
3 + 2 * |x| ULP |
log(x) |
"Absolute error at most 2^-21 when x is in the interval [0.5, 2.0]." 3 ULP outside that interval. |
A correctly rounded result of an exact value is that value. So ldexp,
round and floor are exact whenever their exact result is an f32 value,
and frexp is exact for a zero or normal argument. The table does not cover
frexp of a subnormal argument.
fma inherits its accuracy from x * y + z, so an implementation may compute
it with two roundings. TwoProduct by fma is exact only where the device
fuses it, which hifuku.wgsl.device.fused_fma checks.
The log bound applies to the argument range [0.5, 2.0], which holds the
normalized high part of every float-float product in the WGSL likelihood
kernels.
Subnormal values and signed zero (§15.7.2)¶
Any inputs or outputs of operations listed in § 15.7.4 Floating Point Accuracy may be flushed to zero.
Flushing replaces a subnormal value with a zero. An operation on values near
\( 2^{-126} \) can therefore lose up to \( 2^{-126} \) per input or output.
The absolute terms of Hifuku's float-float bounds, and the domains
FF_TWO_SUM_MIN and FF_TWO_PROD_MIN of the exact transformations, account
for this.
Implementations may ignore the sign field of a floating point zero value.
A computed zero can therefore be either \( +0 \) or \( -0 \).
Overflow, infinity and NaN (§15.7.2, §15.7.3)¶
Under the "Finite Math Assumption", an implementation "may assume that overflow, infinities, and NaNs are not present during shader execution". If a runtime expression overflows, or yields an infinity or a NaN, "the final result will be an indeterminate value of the target type". A kernel thus cannot rely on an infinity or a NaN to report a failure. Hifuku's kernels detect such conditions on finite values, as the exponent limit of the float-float product does.
Reassociation and fusion (§15.7.5)¶
An implementation may reassociate operations.
An implementation may fuse operations if the transformed expression is at least as accurate as the original formulation.
Reassociation can remove the error term that FastTwoSum computes, for example
by rewriting (a + b) - a as b. The float-float library therefore reads an
opaque zero from a runtime buffer, or computes TwoSum in integers, so that the
compiler cannot rewrite the expressions.
Conversion to an integer (§15.7.6)¶
A conversion of an f32 value to i32 or u32 gives the value itself when the
target type holds it exactly. Otherwise it clamps the value to the range of the
target type and rounds toward zero. The conversion of a NaN gives an
indeterminate value.
Uses in Hifuku¶
- The float-float library (
hifuku.wgsl.arithmetic): rounding to nearest, checked per device;fmafusion, checked per device; reassociation, prevented by the opaque zero; flushing, in the exactness domains. - The pruning kernels: exact
frexpandldexpfor the power-of-two scaling of normal values. - The float-float
expm1(ff_expm1.wgsl): exactround,ldexpand conversion of an integer-valuedk; flushing, in its absolute error term. - The walker fitness: the absolute error \( 2^{-21} \) of
logon [0.5, 2.0].