diff --git a/crates/metal2vulkan/src/lower/arith/math.rs b/crates/metal2vulkan/src/lower/arith/math.rs index 22a0042ed..b5bd8909d 100644 --- a/crates/metal2vulkan/src/lower/arith/math.rs +++ b/crates/metal2vulkan/src/lower/arith/math.rs @@ -767,8 +767,10 @@ pub enum FloatFn { /// Metal's `median3`: `min(max(c, min(a, b)), max(a, b))` of the device's `NMin` and `NMax` /// ([`median3`]). Median3, - /// `air.fast_fmedian3` on `float`: [`FloatFn::Median3`]'s form, where Metal's answers `b` for - /// two NaN first operands, which the device's picks leave to it. + /// `air.fast_fmedian3` on `float`: [`FloatFn::Median3`]'s form, which answers `c` for two NaN + /// first operands beside a number `c`, as precise `median3` does, where Metal's fast form + /// answers `b` (`PrecisionNote::NativeMinMax`'s bound; a NaN operand is undefined under fast + /// math). FastMedian3, /// `mix(x, y, t)`: the device's `FMix` on `float` and `half`, and on `bfloat` and `double` /// the blend `x * (1 - t) + y * t`. diff --git a/crates/metal2vulkan/src/lower/arith/mod.rs b/crates/metal2vulkan/src/lower/arith/mod.rs index 1065191ea..7b53e156c 100644 --- a/crates/metal2vulkan/src/lower/arith/mod.rs +++ b/crates/metal2vulkan/src/lower/arith/mod.rs @@ -1787,7 +1787,8 @@ fn llvm_math(op: &LlvmIntrinsic) -> Result { /// ([`FloatFn::FastAcosh`]), the device's `Acosh`; /// - `air.fast_fract` is not bounded below one, and with every flag is the canonical NaN at an /// infinity ([`FloatFn::FastFract`]); -/// - `air.fast_fmedian3` answers `b` for two NaN first operands ([`FloatFn::FastMedian3`]); +/// - `air.fast_fmedian3` answers `b` for two NaN first operands beside a number, where precise +/// `median3` answers the number ([`FloatFn::FastMedian3`]); /// - `air.fast_pow`'s magnitude is `exp2(y · log2|x|)` ([`FloatFn::FastPow`]); /// - `air.fast_atan2` is `atan(y / x)` turned by `±π`, NaN at `0 / 0` and `inf / inf` /// ([`FloatFn::FastAtan2`]); @@ -2064,9 +2065,10 @@ fn min_max_family(op: FloatFn) -> bool { /// (`native_min_max`, [`FloatFacts::native_min_max`]), `saturate` on every other float format, /// which no device is measured to answer as Metal's `clamp(x, +0, 1)`, and `median3` and /// `fast::median3` on every device: `min(max(c, min(a, b)), max(a, b))` of the device's `NMin` and -/// `NMax` (`math::median3`), Metal's answer but where two operands are NaN, whose pick is the -/// device's (Metal's `fast::median3` answers `b` for two NaN first operands), and an `f32` -/// denormal, which Metal compares as a zero. +/// `NMax` (`math::median3`), Metal's answer but of two NaNs, whose NaN is the device's, of +/// `fast::median3`'s two NaN first operands beside a number, which it answers as precise `median3` +/// does where Metal's fast form answers `b` (undefined under fast math, MSL section 8.1), and of an +/// `f32` denormal, which Metal compares as a zero. fn native_min_max(op: FloatFn, f: Form, native_min_max: bool) -> bool { let picks = math::metal_picks(f.float_fmt()); (min_max_family(op) && ((picks && native_min_max) || (op == FloatFn::Saturate && !picks))) diff --git a/crates/metal2vulkan/src/lower/arith/tests/specials.rs b/crates/metal2vulkan/src/lower/arith/tests/specials.rs index 37e61b31a..a58eb515d 100644 --- a/crates/metal2vulkan/src/lower/arith/tests/specials.rs +++ b/crates/metal2vulkan/src/lower/arith/tests/specials.rs @@ -558,6 +558,55 @@ fn median3_answers_as_metal_but_at_its_recorded_ties() { metal_answers(FloatFn::Median3, float(16), &half); } +/// The lanes of the public case `nan-model-min-max-of-nan-operands-under-fast-math` (27f7c2fd) +/// whose first two operands are NaNs, quiet and signalling of either sign with payloads, beside +/// `+0`: `(a, b, c, precise::median3, fast::median3)`, Metal's answers on the M3 Max under macOS +/// 27.0.1 (the precise answers from the case's twin 55bca0fd). +const MEDIAN3_NAN_PAIRS: [(u32, u32, u32, u32, u32); 8] = [ + (0x7FC00001, 0xFFEAAAAA, 0, 0, 0xFFEAAAAA), + (0xFFEAAAAA, 0x7FC00001, 0, 0, 0x7FC00001), + (0x7F800002, 0xFFB55555, 0, 0, 0xFFB55555), + (0xFFB55555, 0x7F800002, 0, 0, 0x7F800002), + (0x7FC00001, 0xFFB55555, 0, 0, 0xFFB55555), + (0xFFB55555, 0x7FC00001, 0, 0, 0x7FC00001), + (0x7F800002, 0xFFEAAAAA, 0, 0, 0xFFEAAAAA), + (0xFFEAAAAA, 0x7F800002, 0, 0, 0x7F800002), +]; + +/// `median3` and `fast::median3` of two NaNs beside a number answer the number, precise +/// `median3`'s answer on Metal, at every such lane of [`MEDIAN3`] and [`MEDIAN3_NAN_PAIRS`]. Metal's +/// fast form answers the second operand where the first two are the NaNs, and the number where a +/// NaN is third: `PrecisionNote::NativeMinMax`'s stated bound, which leaves that fast answer to +/// the device's picks (MSL section 8.1: a NaN operand under fast math is undefined). +#[test] +fn median3_of_two_nans_beside_a_number_answers_the_number() { + let nan = |b: u64| b & 0x7FFF_FFFF > 0x7F80_0000; + let table = MEDIAN3 + .iter() + .map(|r| (r.0, r.1, r.2, r.3, r.4)) + .chain(MEDIAN3_NAN_PAIRS); + let mut rows: Vec = Vec::new(); + let mut fast_second = 0; + for (a, b, c, precise, fast) in table { + let ops = [u64::from(a), u64::from(b), u64::from(c)]; + if ops.iter().filter(|o| nan(**o)).count() != 2 { + continue; + } + let number = ops.iter().copied().find(|o| !nan(*o)).expect("a number"); + assert_eq!(u64::from(precise), number, "precise {ops:x?}"); + if nan(ops[0]) && nan(ops[1]) { + assert_eq!(u64::from(fast), ops[1], "fast {ops:x?}"); + fast_second += 1; + } else { + assert_eq!(u64::from(fast), number, "fast {ops:x?}"); + } + rows.push((ops.to_vec(), number)); + } + assert_eq!((rows.len(), fast_second), (12, 10)); + metal_answers(FloatFn::Median3, float(32), &rows); + metal_answers(FloatFn::FastMedian3, float(32), &rows); +} + /// `pow`'s C99 special cases (`math::pow_special`) at the lanes of [`POW`] one of them decides, /// against Metal's answers: the magnitude operand (`GLSL.std.450 Pow(|x|, y)`, which the /// interpreter does not model) is a constant no answer equals, so a lane that read it would fail. diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index ef2142a87..6b8bf639e 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -2010,15 +2010,19 @@ pub enum PrecisionNote { /// `NClamp`, on a device not measured to answer Metal's operand; a `saturate` of a format /// whose operand Metal's other functions do not pick (`bfloat`, `double`) on every device; and /// `median3` on every device, `min(max(c, min(a, b)), max(a, b))` of the device's `NMin` and - /// `NMax`, whose pick of two NaNs and of an `f32` denormal is the device's (Metal's - /// `fast::median3` answers `b` for two NaN first operands). + /// `NMax`, whose pick of two NaNs and of an `f32` denormal is the device's. /// **Recorded bound:** the - /// result is one of the operands, the one Metal answers but at three ties: of two NaNs a NaN - /// (its bits the device's), where Metal answers the second operand's; of `+0` and `-0` either - /// zero, where Metal answers the second; and an `f32` denormal beside a zero or another - /// denormal compared by its value where the device's `NMin` and `NMax` keep denormals, whether - /// or not its arithmetic flushes them, where Metal compares it as a zero and answers the - /// second. Validation derives no tolerance from it. + /// result is one of the operands, the one Metal answers but at three ties and one undefined + /// answer: of two NaNs a NaN (its bits the device's), where Metal answers the second operand's; + /// of `+0` and `-0` either zero, where Metal answers the second; an `f32` denormal beside a + /// zero or another denormal compared by its value where the device's `NMin` and `NMax` keep + /// denormals, whether or not its arithmetic flushes them, where Metal compares it as a zero + /// and answers the second; and a `fast::median3` whose first two operands are NaNs and whose + /// third is not answers the third, where Metal's answers the second, a NaN. The third is + /// precise `median3`'s answer there on Metal and on the device, and MSL leaves a NaN operand + /// under fast math undefined (section 8.1), so the fast form's NaN is one reading of an + /// undefined answer (the M3 Max's under macOS 27.0.1: the eight lanes of `nan_minmax_fast` + /// whose first two operands are NaNs beside a `+0`). Validation derives no tolerance from it. NativeMinMax, /// Native device maths: Metal's `dot` on `float` and `half` is the device's `OpDot` (a `half` /// one summed in `float` and rounded once to `half`), not Metal's fused chain of its products, diff --git a/docs/API.md b/docs/API.md index 4e9a8dfcd..4f7d2e46a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -4293,7 +4293,12 @@ bound. `float` or `half` is the device's `GLSL.std.450` `NMin`, `NMax` or `NClamp`, on a device not measured to answer Metal's operand; and `median3` on every device, `min(max(c, min(a, b)), max(a, b))` of the device's `NMin` and `NMax`, whose pick of two NaNs and of an `f32` denormal is - the device's (Metal's `fast::median3` answers `b` for two NaN first operands). + the device's. The result is one of the operands, Metal's but at three ties (of two NaNs a NaN of + the device's bits, where Metal answers the second operand's; of `+0` and `-0` either zero; an + `f32` denormal beside a zero or a denormal compared by its value, where Metal compares it as a + zero and answers the second) and one undefined answer: `fast::median3` of two NaN first operands + and a number answers the number, as precise `median3` does on Metal, where Metal's fast form + answers the second NaN. MSL leaves a NaN operand under fast math undefined (section 8.1). - `metal2vulkan-degrade-native-dot` (precision). `PrecisionNote::NativeDot`: Native device maths: Metal's `dot` on `float` and `half` is the device's `OpDot` (a `half` one summed in `float` and rounded once to `half`), not Metal's fused chain of its products, on every device but for a