From 895b3f845a43a5d28a24b010becd13984fc234ae Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 21 Nov 2026 07:17:05 -0800 Subject: [PATCH] PrecisionNote::NativeHalfQuantize: a zero may round to the other zero What: the note's text (its doc comment and the docs/API.md catalogue) says what may differ: where a half rounding's input is a zero or too small for a normal half, the result may be either zero, so a negative zero may come out positive; and what a program sees of it: a reciprocal's or atan2's sign, the bits written to a half target or buffer. The catalogue names the form not taken (copying the input's sign bit into the result by integer operations, about 4 instructions per half rounding on every device without shaderFloat16) and its cost. No code changes; the planner ruled the note (11-21). Why: the 11-21 round at ce998742d7 read the RX 550 (RADV, Polaris) answering +0 for a -0 float attribute rounded to a half parameter (vertex-format cases), where Metal answers -0. The SPIR-V specification defines OpQuantizeToF16 so: "If the magnitude of Value is too small to represent as a normalized 16-bit floating-point value, the result must be either +0 or -0." A zero's magnitude is in that range. So the answer is the quantize's own, documented, and rule 1 gives it a static note. The note already covered a denormal rounding to a zero of either sign, but its text did not name a zero's own sign. SignedZeroInfNanPreserve is not the fix: it only restrains optimizations ("does not perform optimizations on floating-point instructions that do not preserve sign of a zero") and does not narrow what the instruction may answer. How verified: a text change only. The RX 550's vertex-format rows go to the ledger under native-half-quantize with the per-card rows. --- crates/metal2vulkan/src/pipeline/artifact.rs | 10 +++++++--- docs/API.md | 8 +++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index 836b0a6b5..5812b5da5 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -1633,13 +1633,17 @@ pub enum PrecisionNote { /// device's `OpQuantizeToF16` alone, on every device without `shaderFloat16`. An integer's /// `half` is not this rounding: it rounds by integer arithmetic, exactly, around NVIDIA's /// 615.71.09 keeping an integer conversion unrounded under the quantize. Vulkan leaves the - /// quantize's rounding mode to the device, and the SPIR-V specification lets it answer a zero - /// for a magnitude too small for a normal `half`. The recorded bound, against Metal's `half(float)`: a finite result within one unit in the last + /// quantize's rounding mode to the device, and the SPIR-V specification lets it answer either + /// zero for a magnitude too small for a normal `half`, a zero's own included. The recorded bound, against Metal's `half(float)`: a finite result within one unit in the last /// place of the `half`; a value from 65504 up to 65520 may be infinity where Metal's is 65504 /// (NVIDIA's 615.71.09 rounds `0x477FEFFF` to infinity); a value below the smallest normal /// `half` (`2^-14`) may round to a zero, of either sign, where Metal answers the half denormal, /// or `2^-14` from just below it (the quantize tests the value it rounds; NIR's - /// `fquantize2f16` flushes every one, SwiftShader's too); a NaN's encoding is the device's. + /// `fquantize2f16` flushes every one, SwiftShader's too); where the rounding's input is a + /// zero or too small for a normal `half`, the result may be either zero, so a negative zero + /// may come out positive (RADV's on Polaris does), which a program sees in a reciprocal's or + /// `atan2`'s sign and in the bits written to a `half` target or buffer; a NaN's encoding is + /// the device's. /// Validation derives no tolerance from it. NativeHalfQuantize, /// Native device maths: a `float` or `half` operation's NaN result is the device's, its sign diff --git a/docs/API.md b/docs/API.md index 5fec8337b..42e5da2ac 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2434,7 +2434,13 @@ bound. `half` operation's, a function's, a conversion from `float`'s, a texel's) by the device's `OpQuantizeToF16` alone, on every device without `shaderFloat16`, its quantize measured or not: a value below the smallest normal `half` may round to a zero where Metal answers the half denormal - (or `2^-14`, from just below it). An integer's `half` rounds exactly instead. + (or `2^-14`, from just below it). Where the rounding's input is a zero or too small for a normal + `half`, the result may be either zero, so a negative zero may come out positive, as the SPIR-V + specification lets the quantize answer: a program sees it in a reciprocal's or `atan2`'s sign + and in the bits written to a `half` target or buffer. The form not taken, copying the input's + sign bit into the result by integer operations, would keep Metal's sign at about 4 instructions + per `half` rounding on every device without `shaderFloat16`. An integer's `half` rounds exactly + instead. - `metal2vulkan-degrade-native-nan-encoding` (precision). `PrecisionNote::NativeNanEncoding`: Native device maths: a `float` or `half` operation's NaN result is the device's, its sign and payload unselected, where Metal answers a NaN, on every device: a `float`