From 6f217d7ccb3857c55f596e593fd955ca81bb5721 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sun, 8 Nov 2026 15:25:37 +0100 Subject: [PATCH 1/3] Handle sNaN in `{max,min}_precise` on Neon --- fearless_simd/src/generated/neon.rs | 16 +++-- fearless_simd/src/generated/simd_trait.rs | 28 ++++----- fearless_simd_gen/src/mk_neon.rs | 16 +++++ fearless_simd_gen/src/ops.rs | 12 ++-- .../tests/harness/ops/max_precise.rs | 63 +++++++++++++------ .../tests/harness/ops/min_precise.rs | 63 +++++++++++++------ 6 files changed, 132 insertions(+), 66 deletions(-) diff --git a/fearless_simd/src/generated/neon.rs b/fearless_simd/src/generated/neon.rs index 4b8fcf037..2017f7e77 100644 --- a/fearless_simd/src/generated/neon.rs +++ b/fearless_simd/src/generated/neon.rs @@ -411,7 +411,9 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: f32x4, b: f32x4) -> f32x4 { - vmaxnmq_f32(a.into(), b.into()).simd_into(token) + let a = vmaxnmq_f32(a.into(), a.into()); + let b = vmaxnmq_f32(b.into(), b.into()); + vmaxnmq_f32(a, b).simd_into(token) } ); kernel(self, a, b) @@ -421,7 +423,9 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: f32x4, b: f32x4) -> f32x4 { - vminnmq_f32(a.into(), b.into()).simd_into(token) + let a = vminnmq_f32(a.into(), a.into()); + let b = vminnmq_f32(b.into(), b.into()); + vminnmq_f32(a, b).simd_into(token) } ); kernel(self, a, b) @@ -5303,7 +5307,9 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: f64x2, b: f64x2) -> f64x2 { - vmaxnmq_f64(a.into(), b.into()).simd_into(token) + let a = vmaxnmq_f64(a.into(), a.into()); + let b = vmaxnmq_f64(b.into(), b.into()); + vmaxnmq_f64(a, b).simd_into(token) } ); kernel(self, a, b) @@ -5313,7 +5319,9 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: f64x2, b: f64x2) -> f64x2 { - vminnmq_f64(a.into(), b.into()).simd_into(token) + let a = vminnmq_f64(a.into(), a.into()); + let b = vminnmq_f64(b.into(), b.into()); + vminnmq_f64(a, b).simd_into(token) } ); kernel(self, a, b) diff --git a/fearless_simd/src/generated/simd_trait.rs b/fearless_simd/src/generated/simd_trait.rs index 8a048cb2d..4ffd58e63 100644 --- a/fearless_simd/src/generated/simd_trait.rs +++ b/fearless_simd/src/generated/simd_trait.rs @@ -344,9 +344,9 @@ pub trait Simd: fn max_f32x4(self, a: f32x4, b: f32x4) -> f32x4; #[doc = "Return the element-wise minimum of two vectors.\n\nFor floating-point vectors, if either operand is NaN, the result for that lane is implementation-defined-- it could be either the first or second operand. See `min_precise` for a version that returns the non-NaN operand if only one is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is also implementation-defined, and it could be either one."] fn min_f32x4(self, a: f32x4, b: f32x4) -> f32x4; - #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] fn max_precise_f32x4(self, a: f32x4, b: f32x4) -> f32x4; - #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] fn min_precise_f32x4(self, a: f32x4, b: f32x4) -> f32x4; #[doc = "Clamp each element to the interval `[b, c]`.\n\nIf `b > c`, or `b` or `c` are NaN, the result is implementation-defined for that lane.\n\nIf `a` is NaN, the result is an implementation-defined non-deterministic NaN value. I.e. the result of the lane will be a NaN value but its exact bit pattern is not specified.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\n# Panics\nPanics with debug assertions if `b > c` for any element or any element of `b` or `c` is NaN."] fn clamp_f32x4(self, a: f32x4, b: f32x4, c: f32x4) -> f32x4; @@ -1851,9 +1851,9 @@ pub trait Simd: fn max_f64x2(self, a: f64x2, b: f64x2) -> f64x2; #[doc = "Return the element-wise minimum of two vectors.\n\nFor floating-point vectors, if either operand is NaN, the result for that lane is implementation-defined-- it could be either the first or second operand. See `min_precise` for a version that returns the non-NaN operand if only one is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is also implementation-defined, and it could be either one."] fn min_f64x2(self, a: f64x2, b: f64x2) -> f64x2; - #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] fn max_precise_f64x2(self, a: f64x2, b: f64x2) -> f64x2; - #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] fn min_precise_f64x2(self, a: f64x2, b: f64x2) -> f64x2; #[doc = "Clamp each element to the interval `[b, c]`.\n\nIf `b > c`, or `b` or `c` are NaN, the result is implementation-defined for that lane.\n\nIf `a` is NaN, the result is an implementation-defined non-deterministic NaN value. I.e. the result of the lane will be a NaN value but its exact bit pattern is not specified.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\n# Panics\nPanics with debug assertions if `b > c` for any element or any element of `b` or `c` is NaN."] fn clamp_f64x2(self, a: f64x2, b: f64x2, c: f64x2) -> f64x2; @@ -2582,7 +2582,7 @@ pub trait Simd: let (b0, b1) = self.split_f32x8(b); self.combine_f32x4(self.min_f32x4(a0, b0), self.min_f32x4(a1, b1)) } - #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] #[inline(always)] fn max_precise_f32x8(self, a: f32x8, b: f32x8) -> f32x8 { let (a0, a1) = self.split_f32x8(a); @@ -2592,7 +2592,7 @@ pub trait Simd: self.max_precise_f32x4(a1, b1), ) } - #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] #[inline(always)] fn min_precise_f32x8(self, a: f32x8, b: f32x8) -> f32x8 { let (a0, a1) = self.split_f32x8(a); @@ -6099,7 +6099,7 @@ pub trait Simd: let (b0, b1) = self.split_f64x4(b); self.combine_f64x2(self.min_f64x2(a0, b0), self.min_f64x2(a1, b1)) } - #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] #[inline(always)] fn max_precise_f64x4(self, a: f64x4, b: f64x4) -> f64x4 { let (a0, a1) = self.split_f64x4(a); @@ -6109,7 +6109,7 @@ pub trait Simd: self.max_precise_f64x2(a1, b1), ) } - #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] #[inline(always)] fn min_precise_f64x4(self, a: f64x4, b: f64x4) -> f64x4 { let (a0, a1) = self.split_f64x4(a); @@ -7532,7 +7532,7 @@ pub trait Simd: let (b0, b1) = self.split_f32x16(b); self.combine_f32x8(self.min_f32x8(a0, b0), self.min_f32x8(a1, b1)) } - #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] #[inline(always)] fn max_precise_f32x16(self, a: f32x16, b: f32x16) -> f32x16 { let (a0, a1) = self.split_f32x16(a); @@ -7542,7 +7542,7 @@ pub trait Simd: self.max_precise_f32x8(a1, b1), ) } - #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] #[inline(always)] fn min_precise_f32x16(self, a: f32x16, b: f32x16) -> f32x16 { let (a0, a1) = self.split_f32x16(a); @@ -11062,7 +11062,7 @@ pub trait Simd: let (b0, b1) = self.split_f64x8(b); self.combine_f64x4(self.min_f64x4(a0, b0), self.min_f64x4(a1, b1)) } - #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] #[inline(always)] fn max_precise_f64x8(self, a: f64x8, b: f64x8) -> f64x8 { let (a0, a1) = self.split_f64x8(a); @@ -11072,7 +11072,7 @@ pub trait Simd: self.max_precise_f64x4(a1, b1), ) } - #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] #[inline(always)] fn min_precise_f64x8(self, a: f64x8, b: f64x8) -> f64x8 { let (a0, a1) = self.split_f64x8(a); @@ -12885,9 +12885,9 @@ pub trait SimdBase: fn max(self, rhs: impl SimdInto) -> Self; #[doc = "Return the element-wise minimum of two vectors.\n\nFor floating-point vectors, if either operand is NaN, the result for that lane is implementation-defined-- it could be either the first or second operand. See `min_precise` for a version that returns the non-NaN operand if only one is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is also implementation-defined, and it could be either one."] fn min(self, rhs: impl SimdInto) -> Self; - #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise maximum of two vectors.\n\nFor integer vectors, this operation is the same as `max`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] fn max_precise(self, rhs: impl SimdInto) -> Self; - #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\nIf a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the element-wise minimum of two vectors.\n\nFor integer vectors, this operation is the same as `min`.\n\nFor floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one."] fn min_precise(self, rhs: impl SimdInto) -> Self; #[doc = "Clamp each element to the interval `[min, max]`.\n\nIf `min > max`, or `min` or `max` are NaN, the result is implementation-defined for that lane.\n\nIf `self` is NaN, the result is an implementation-defined non-deterministic NaN value. I.e. the result of the lane will be a NaN value but its exact bit pattern is not specified.\n\nIf one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\n# Panics\nPanics with debug assertions if `min > max` for any element or any element of `min` or `max` is NaN."] fn clamp(self, min: impl SimdInto, max: impl SimdInto) -> Self; diff --git a/fearless_simd_gen/src/mk_neon.rs b/fearless_simd_gen/src/mk_neon.rs index 0334d002e..7af9b127d 100644 --- a/fearless_simd_gen/src/mk_neon.rs +++ b/fearless_simd_gen/src/mk_neon.rs @@ -573,6 +573,22 @@ impl Level for Neon { #expr.simd_into(#token) } } + "max_precise" | "min_precise" if vec_ty.scalar == ScalarType::Float => { + // max_precise and min_precise operations use vmaxnm and vminnm respectively. + // However, these instructions return qNaN if one or both operands are sNaN. + // To work around this, we use vmaxnm(a, a) (and similar) to turn all + // sNaN values into qNaN values, guaranteeing correct behavior for all possible inputs. + let a = + neon::expr(method, vec_ty, &[quote! { a.into() }, quote! { a.into() }]); + let b = + neon::expr(method, vec_ty, &[quote! { b.into() }, quote! { b.into() }]); + let expr = neon::expr(method, vec_ty, &[quote! { a }, quote! { b }]); + quote! { + let a = #a; + let b = #b; + #expr.simd_into(#token) + } + } "copysign" => { let shift_amt = Literal::usize_unsuffixed(vec_ty.scalar_bits - 1); let unsigned_ty = vec_ty.cast(ScalarType::Unsigned); diff --git a/fearless_simd_gen/src/ops.rs b/fearless_simd_gen/src/ops.rs index 413c4c549..ce56e424a 100644 --- a/fearless_simd_gen/src/ops.rs +++ b/fearless_simd_gen/src/ops.rs @@ -921,10 +921,8 @@ const COMMON_BASE_OPS: &[Op] = &[ OpSig::Binary, "Return the element-wise maximum of two vectors.\n\n\ For integer vectors, this operation is the same as `max`.\n\n\ - For floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\n\ - If one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\n\ - If a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\n\ - Signaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them.", + For floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\n\ + If one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.", ), Op::new( "min_precise", @@ -932,10 +930,8 @@ const COMMON_BASE_OPS: &[Op] = &[ OpSig::Binary, "Return the element-wise minimum of two vectors.\n\n\ For integer vectors, this operation is the same as `min`.\n\n\ - For floating-point vectors, if one operand is a quiet NaN and the other is not, this operation will choose the non-NaN operand.\n\n\ - If one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.\n\n\ - If a floating-point operand is a *signaling* NaN, the result is not just implementation-defined, but fully non-deterministic: it may be either NaN or the non-NaN operand.\n\ - Signaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them.", + For floating-point vectors, if one operand is NaN and the other is not, this operation will choose the non-NaN operand. If both operands are NaN, the result is NaN.\n\n\ + If one floating-point operand is positive zero and the other is negative zero, the result is implementation-defined, and it could be either one.", ), Op::new( "clamp", diff --git a/fearless_simd_tests/tests/harness/ops/max_precise.rs b/fearless_simd_tests/tests/harness/ops/max_precise.rs index e3088c11d..46cb86ac4 100644 --- a/fearless_simd_tests/tests/harness/ops/max_precise.rs +++ b/fearless_simd_tests/tests/harness/ops/max_precise.rs @@ -6,6 +6,11 @@ use fearless_simd_dev_macros::simd_test; // One concrete test row per supported vector type. +const Q_NAN_F32: f32 = f32::NAN; +const Q_NAN_F64: f64 = f64::NAN; +const S_NAN_F32: f32 = f32::from_bits(0x7FFF_FFFF); +const S_NAN_F64: f64 = f64::from_bits(0x7FFF_FFFF_FFFF_FFFF); + #[simd_test] fn max_precise_f32x4(simd: S) { let a = f32x4::from_slice(simd, &[2.0, -3.0, 0.0, 0.5]); @@ -15,8 +20,8 @@ fn max_precise_f32x4(simd: S) { #[simd_test] fn max_precise_f32x4_with_nan(simd: S) { - let a = f32x4::from_slice(simd, &[f32::NAN, -3.0, f32::INFINITY, 0.5]); - let b = f32x4::from_slice(simd, &[1.0, f32::NAN, 7.0, f32::NEG_INFINITY]); + let a = f32x4::from_slice(simd, &[Q_NAN_F32, -3.0, f32::INFINITY, 0.5]); + let b = f32x4::from_slice(simd, &[1.0, S_NAN_F32, 7.0, f32::NEG_INFINITY]); let result = a.max_precise(b); assert_eq!(result[0], 1.0); @@ -34,8 +39,8 @@ fn max_precise_f64x2(simd: S) { #[simd_test] fn max_precise_f64x2_with_nan(simd: S) { - let a = f64x2::from_slice(simd, &[f64::NAN, -3.0]); - let b = f64x2::from_slice(simd, &[1.0, f64::NAN]); + let a = f64x2::from_slice(simd, &[Q_NAN_F64, -3.0]); + let b = f64x2::from_slice(simd, &[1.0, S_NAN_F64]); let result = a.max_precise(b); assert_eq!(result[0], 1.0); @@ -53,16 +58,25 @@ fn max_precise_f32x8(simd: S) { fn max_precise_f32x8_with_nan(simd: S) { let a = f32x8::from_slice( simd, - &[f32::NAN, -3.0, f32::INFINITY, 0.5, 1.0, f32::NAN, 3.0, 7.0], + &[ + Q_NAN_F32, + -3.0, + f32::INFINITY, + 0.5, + 1.0, + S_NAN_F32, + 3.0, + 7.0, + ], ); let b = f32x8::from_slice( simd, &[ 1.0, - f32::NAN, + Q_NAN_F32, 7.0, f32::NEG_INFINITY, - f32::NAN, + S_NAN_F32, 4.0, 6.0, 5.0, @@ -89,8 +103,8 @@ fn max_precise_f64x4(simd: S) { #[simd_test] fn max_precise_f64x4_with_nan(simd: S) { - let a = f64x4::from_slice(simd, &[f64::NAN, -3.0, f64::INFINITY, 0.5]); - let b = f64x4::from_slice(simd, &[1.0, f64::NAN, 7.0, f64::NEG_INFINITY]); + let a = f64x4::from_slice(simd, &[Q_NAN_F64, -3.0, f64::INFINITY, 0.5]); + let b = f64x4::from_slice(simd, &[1.0, S_NAN_F64, 7.0, f64::NEG_INFINITY]); let result = a.max_precise(b); assert_eq!(result[0], 1.0); @@ -126,20 +140,20 @@ fn max_precise_f32x16_with_nan(simd: S) { let a = f32x16::from_slice( simd, &[ - f32::NAN, + Q_NAN_F32, -3.0, f32::INFINITY, 0.5, 1.0, - f32::NAN, + S_NAN_F32, 3.0, 7.0, - f32::NAN, + Q_NAN_F32, -3.0, f32::INFINITY, 0.5, 1.0, - f32::NAN, + S_NAN_F32, 3.0, 7.0, ], @@ -148,18 +162,18 @@ fn max_precise_f32x16_with_nan(simd: S) { simd, &[ 1.0, - f32::NAN, + Q_NAN_F32, 7.0, f32::NEG_INFINITY, - f32::NAN, + S_NAN_F32, 4.0, 6.0, 5.0, 1.0, - f32::NAN, + Q_NAN_F32, 7.0, f32::NEG_INFINITY, - f32::NAN, + S_NAN_F32, 4.0, 6.0, 5.0, @@ -196,16 +210,25 @@ fn max_precise_f64x8(simd: S) { fn max_precise_f64x8_with_nan(simd: S) { let a = f64x8::from_slice( simd, - &[f64::NAN, -3.0, f64::INFINITY, 0.5, 1.0, f64::NAN, 3.0, 7.0], + &[ + Q_NAN_F64, + -3.0, + f64::INFINITY, + 0.5, + 1.0, + S_NAN_F64, + 3.0, + 7.0, + ], ); let b = f64x8::from_slice( simd, &[ 1.0, - f64::NAN, + Q_NAN_F64, 7.0, f64::NEG_INFINITY, - f64::NAN, + S_NAN_F64, 4.0, 6.0, 5.0, diff --git a/fearless_simd_tests/tests/harness/ops/min_precise.rs b/fearless_simd_tests/tests/harness/ops/min_precise.rs index ef1b86728..9a9ac00bb 100644 --- a/fearless_simd_tests/tests/harness/ops/min_precise.rs +++ b/fearless_simd_tests/tests/harness/ops/min_precise.rs @@ -6,6 +6,11 @@ use fearless_simd_dev_macros::simd_test; // One concrete test row per supported vector type. +const Q_NAN_F32: f32 = f32::NAN; +const Q_NAN_F64: f64 = f64::NAN; +const S_NAN_F32: f32 = f32::from_bits(0x7FFF_FFFF); +const S_NAN_F64: f64 = f64::from_bits(0x7FFF_FFFF_FFFF_FFFF); + #[simd_test] fn min_precise_f32x4(simd: S) { let a = f32x4::from_slice(simd, &[2.0, -3.0, 0.0, 0.5]); @@ -15,8 +20,8 @@ fn min_precise_f32x4(simd: S) { #[simd_test] fn min_precise_f32x4_with_nan(simd: S) { - let a = f32x4::from_slice(simd, &[f32::NAN, -3.0, f32::INFINITY, 0.5]); - let b = f32x4::from_slice(simd, &[1.0, f32::NAN, 7.0, f32::NEG_INFINITY]); + let a = f32x4::from_slice(simd, &[Q_NAN_F32, -3.0, f32::INFINITY, 0.5]); + let b = f32x4::from_slice(simd, &[1.0, S_NAN_F32, 7.0, f32::NEG_INFINITY]); let result = a.min_precise(b); assert_eq!(result[0], 1.0); @@ -34,8 +39,8 @@ fn min_precise_f64x2(simd: S) { #[simd_test] fn min_precise_f64x2_with_nan(simd: S) { - let a = f64x2::from_slice(simd, &[f64::NAN, -3.0]); - let b = f64x2::from_slice(simd, &[1.0, f64::NAN]); + let a = f64x2::from_slice(simd, &[Q_NAN_F64, -3.0]); + let b = f64x2::from_slice(simd, &[1.0, S_NAN_F64]); let result = a.min_precise(b); assert_eq!(result[0], 1.0); @@ -53,16 +58,25 @@ fn min_precise_f32x8(simd: S) { fn min_precise_f32x8_with_nan(simd: S) { let a = f32x8::from_slice( simd, - &[f32::NAN, -3.0, f32::INFINITY, 0.5, 1.0, f32::NAN, 3.0, 7.0], + &[ + Q_NAN_F32, + -3.0, + f32::INFINITY, + 0.5, + 1.0, + S_NAN_F32, + 3.0, + 7.0, + ], ); let b = f32x8::from_slice( simd, &[ 1.0, - f32::NAN, + Q_NAN_F32, 7.0, f32::NEG_INFINITY, - f32::NAN, + S_NAN_F32, 4.0, 6.0, 5.0, @@ -89,8 +103,8 @@ fn min_precise_f64x4(simd: S) { #[simd_test] fn min_precise_f64x4_with_nan(simd: S) { - let a = f64x4::from_slice(simd, &[f64::NAN, -3.0, f64::INFINITY, 0.5]); - let b = f64x4::from_slice(simd, &[1.0, f64::NAN, 7.0, f64::NEG_INFINITY]); + let a = f64x4::from_slice(simd, &[Q_NAN_F64, -3.0, f64::INFINITY, 0.5]); + let b = f64x4::from_slice(simd, &[1.0, S_NAN_F64, 7.0, f64::NEG_INFINITY]); let result = a.min_precise(b); assert_eq!(result[0], 1.0); @@ -126,20 +140,20 @@ fn min_precise_f32x16_with_nan(simd: S) { let a = f32x16::from_slice( simd, &[ - f32::NAN, + Q_NAN_F32, -3.0, f32::INFINITY, 0.5, 1.0, - f32::NAN, + S_NAN_F32, 3.0, 7.0, - f32::NAN, + Q_NAN_F32, -3.0, f32::INFINITY, 0.5, 1.0, - f32::NAN, + S_NAN_F32, 3.0, 7.0, ], @@ -148,18 +162,18 @@ fn min_precise_f32x16_with_nan(simd: S) { simd, &[ 1.0, - f32::NAN, + Q_NAN_F32, 7.0, f32::NEG_INFINITY, - f32::NAN, + S_NAN_F32, 4.0, 6.0, 5.0, 1.0, - f32::NAN, + Q_NAN_F32, 7.0, f32::NEG_INFINITY, - f32::NAN, + Q_NAN_F32, 4.0, 6.0, 5.0, @@ -196,16 +210,25 @@ fn min_precise_f64x8(simd: S) { fn min_precise_f64x8_with_nan(simd: S) { let a = f64x8::from_slice( simd, - &[f64::NAN, -3.0, f64::INFINITY, 0.5, 1.0, f64::NAN, 3.0, 7.0], + &[ + Q_NAN_F64, + -3.0, + f64::INFINITY, + 0.5, + 1.0, + S_NAN_F64, + 3.0, + 7.0, + ], ); let b = f64x8::from_slice( simd, &[ 1.0, - f64::NAN, + Q_NAN_F64, 7.0, f64::NEG_INFINITY, - f64::NAN, + S_NAN_F64, 4.0, 6.0, 5.0, From 550203ec1ae603bc2e2ca35289ee8bc7a2625c4e Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sun, 8 Nov 2026 15:34:59 +0100 Subject: [PATCH 2/3] Handle sNaN in `reduce_{max,min}_precise` on Neon --- fearless_simd/src/generated/neon.rs | 4 + fearless_simd_gen/src/mk_neon.rs | 12 ++- fearless_simd_gen/src/ops.rs | 12 +-- .../tests/harness/ops/reduce_max_precise.rs | 79 +++++++++++-------- .../tests/harness/ops/reduce_min_precise.rs | 79 +++++++++++-------- 5 files changed, 111 insertions(+), 75 deletions(-) diff --git a/fearless_simd/src/generated/neon.rs b/fearless_simd/src/generated/neon.rs index 2017f7e77..3b43c3a33 100644 --- a/fearless_simd/src/generated/neon.rs +++ b/fearless_simd/src/generated/neon.rs @@ -348,6 +348,7 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: f32x4) -> f32 { + let a = vmaxnmq_f32(a.into(), a.into()); vmaxnmvq_f32(a.into()) } ); @@ -358,6 +359,7 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: f32x4) -> f32 { + let a = vminnmq_f32(a.into(), a.into()); vminnmvq_f32(a.into()) } ); @@ -5246,6 +5248,7 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: f64x2) -> f64 { + let a = vmaxnmq_f64(a.into(), a.into()); vmaxnmvq_f64(a.into()) } ); @@ -5256,6 +5259,7 @@ impl Simd for Neon { crate::kernel!( #[inline(always)] fn kernel(token: Neon, a: f64x2) -> f64 { + let a = vminnmq_f64(a.into(), a.into()); vminnmvq_f64(a.into()) } ); diff --git a/fearless_simd_gen/src/mk_neon.rs b/fearless_simd_gen/src/mk_neon.rs index 7af9b127d..f8539dcac 100644 --- a/fearless_simd_gen/src/mk_neon.rs +++ b/fearless_simd_gen/src/mk_neon.rs @@ -372,6 +372,16 @@ impl Level for Neon { return self.kernel_method(op, vec_ty, |_| reduce); } + let preprocess = if matches!(lane_op, "min_precise" | "max_precise") { + let expr = + neon::expr(lane_op, vec_ty, &[quote! { a.into() }, quote! { a.into() }]); + quote! { + let a = #expr; + } + } else { + TokenStream::new() + }; + let intrinsic = match lane_op { "add" => "vaddv", "min" => "vminv", @@ -381,7 +391,7 @@ impl Level for Neon { _ => unreachable!("unsupported reduction lane operation"), }; let reduce = simple_intrinsic(intrinsic, vec_ty); - self.kernel_method(op, vec_ty, |_| quote! { #reduce(a.into()) }) + self.kernel_method(op, vec_ty, |_| quote! { #preprocess #reduce(a.into()) }) } OpSig::RotateElements { .. } => integer_lane_mask_rotate(op, vec_ty), OpSig::LoadInterleaved { diff --git a/fearless_simd_gen/src/ops.rs b/fearless_simd_gen/src/ops.rs index ce56e424a..f19e54ac8 100644 --- a/fearless_simd_gen/src/ops.rs +++ b/fearless_simd_gen/src/ops.rs @@ -857,10 +857,8 @@ const COMMON_BASE_OPS: &[Op] = &[ }, "Return the maximum element in the vector, ignoring quiet NaNs.\n\n\ For integer vectors, this operation is the same as `reduce_max`.\n\n\ - For floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\n\ - If the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\n\ - If any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true maximum.\n\ - Signaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them.", + For floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\n\ + If the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.", ), Op::new( "reduce_min_precise", @@ -870,10 +868,8 @@ const COMMON_BASE_OPS: &[Op] = &[ }, "Return the minimum element in the vector, ignoring quiet NaNs.\n\n\ For integer vectors, this operation is the same as `reduce_min`.\n\n\ - For floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\n\ - If the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\n\ - If any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true minimum.\n\ - Signaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them.", + For floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\n\ + If the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.", ), Op::new( "reduce_sum", diff --git a/fearless_simd_tests/tests/harness/ops/reduce_max_precise.rs b/fearless_simd_tests/tests/harness/ops/reduce_max_precise.rs index aa5398b46..235b1d2c2 100644 --- a/fearless_simd_tests/tests/harness/ops/reduce_max_precise.rs +++ b/fearless_simd_tests/tests/harness/ops/reduce_max_precise.rs @@ -4,28 +4,33 @@ use fearless_simd::*; use fearless_simd_dev_macros::simd_test; +const Q_NAN_F32: f32 = f32::NAN; +const Q_NAN_F64: f64 = f64::NAN; +const S_NAN_F32: f32 = f32::from_bits(0x7FFF_FFFF); +const S_NAN_F64: f64 = f64::from_bits(0x7FFF_FFFF_FFFF_FFFF); + #[simd_test] fn reduce_max_precise_f32x4(simd: S) { - let nan = f32::from_bits(0x7fc0_1234); assert_eq!( - f32x4::from_slice(simd, &[nan, 4.0, 12.0, 7.0]).reduce_max_precise(), + f32x4::from_slice(simd, &[Q_NAN_F32, 4.0, 12.0, 7.0]).reduce_max_precise(), 12.0 ); assert_eq!( - f32x4::from_slice(simd, &[4.0, 12.0, 7.0, nan]).reduce_max_precise(), + f32x4::from_slice(simd, &[4.0, 12.0, 7.0, S_NAN_F32]).reduce_max_precise(), 12.0 ); assert_eq!( - f32x4::from_slice(simd, &[nan, 4.0, nan, 12.0]).reduce_max_precise(), + f32x4::from_slice(simd, &[Q_NAN_F32, 4.0, S_NAN_F32, 12.0]).reduce_max_precise(), 12.0 ); assert!( - f32x4::from_slice(simd, &[nan; 4]) + f32x4::from_slice(simd, &[S_NAN_F32; 4]) .reduce_max_precise() .is_nan() ); assert_eq!( - f32x4::from_slice(simd, &[f32::NEG_INFINITY, nan, 3.0, f32::INFINITY]).reduce_max_precise(), + f32x4::from_slice(simd, &[f32::NEG_INFINITY, S_NAN_F32, 3.0, f32::INFINITY]) + .reduce_max_precise(), f32::INFINITY ); assert_eq!( @@ -36,17 +41,16 @@ fn reduce_max_precise_f32x4(simd: S) { #[simd_test] fn reduce_max_precise_f64x2(simd: S) { - let nan = f64::from_bits(0x7ff8_0000_0000_1234); assert_eq!( - f64x2::from_slice(simd, &[nan, 12.0]).reduce_max_precise(), + f64x2::from_slice(simd, &[Q_NAN_F64, 12.0]).reduce_max_precise(), 12.0 ); assert_eq!( - f64x2::from_slice(simd, &[12.0, nan]).reduce_max_precise(), + f64x2::from_slice(simd, &[12.0, S_NAN_F64]).reduce_max_precise(), 12.0 ); assert!( - f64x2::from_slice(simd, &[nan; 2]) + f64x2::from_slice(simd, &[S_NAN_F64; 2]) .reduce_max_precise() .is_nan() ); @@ -62,21 +66,26 @@ fn reduce_max_precise_f64x2(simd: S) { #[simd_test] fn reduce_max_precise_f32x8(simd: S) { - let nan = f32::from_bits(0x7fc0_1234); assert_eq!( - f32x8::from_slice(simd, &[nan, 4.0, 12.0, 7.0, 9.0, 3.0, 6.0, 8.0]).reduce_max_precise(), + f32x8::from_slice(simd, &[Q_NAN_F32, 4.0, 12.0, 7.0, 9.0, 3.0, 6.0, 8.0]) + .reduce_max_precise(), 12.0 ); assert_eq!( - f32x8::from_slice(simd, &[4.0, 12.0, 7.0, 9.0, 3.0, 6.0, 8.0, nan]).reduce_max_precise(), + f32x8::from_slice(simd, &[4.0, 12.0, 7.0, 9.0, 3.0, 6.0, 8.0, S_NAN_F32]) + .reduce_max_precise(), 12.0 ); assert_eq!( - f32x8::from_slice(simd, &[nan, 4.0, nan, 7.0, 12.0, nan, 6.0, 8.0]).reduce_max_precise(), + f32x8::from_slice( + simd, + &[Q_NAN_F32, 4.0, S_NAN_F32, 7.0, 12.0, S_NAN_F32, 6.0, 8.0] + ) + .reduce_max_precise(), 12.0 ); assert!( - f32x8::from_slice(simd, &[nan; 8]) + f32x8::from_slice(simd, &[S_NAN_F32; 8]) .reduce_max_precise() .is_nan() ); @@ -85,7 +94,7 @@ fn reduce_max_precise_f32x8(simd: S) { simd, &[ f32::NEG_INFINITY, - nan, + S_NAN_F32, 3.0, 8.0, 2.0, @@ -103,26 +112,26 @@ fn reduce_max_precise_f32x8(simd: S) { #[simd_test] fn reduce_max_precise_f64x4(simd: S) { - let nan = f64::from_bits(0x7ff8_0000_0000_1234); assert_eq!( - f64x4::from_slice(simd, &[nan, 4.0, 12.0, 7.0]).reduce_max_precise(), + f64x4::from_slice(simd, &[Q_NAN_F64, 4.0, 12.0, 7.0]).reduce_max_precise(), 12.0 ); assert_eq!( - f64x4::from_slice(simd, &[4.0, 12.0, 7.0, nan]).reduce_max_precise(), + f64x4::from_slice(simd, &[4.0, 12.0, 7.0, S_NAN_F64]).reduce_max_precise(), 12.0 ); assert_eq!( - f64x4::from_slice(simd, &[nan, 4.0, nan, 12.0]).reduce_max_precise(), + f64x4::from_slice(simd, &[Q_NAN_F64, 4.0, S_NAN_F64, 12.0]).reduce_max_precise(), 12.0 ); assert!( - f64x4::from_slice(simd, &[nan; 4]) + f64x4::from_slice(simd, &[Q_NAN_F64; 4]) .reduce_max_precise() .is_nan() ); assert_eq!( - f64x4::from_slice(simd, &[f64::NEG_INFINITY, nan, 3.0, f64::INFINITY]).reduce_max_precise(), + f64x4::from_slice(simd, &[f64::NEG_INFINITY, S_NAN_F64, 3.0, f64::INFINITY]) + .reduce_max_precise(), f64::INFINITY ); assert_eq!( @@ -133,21 +142,20 @@ fn reduce_max_precise_f64x4(simd: S) { #[simd_test] fn reduce_max_precise_f32x16(simd: S) { - let nan = f32::from_bits(0x7fc0_1234); let mut first = [4.0; 16]; - first[0] = nan; + first[0] = Q_NAN_F32; first[13] = 12.0; assert_eq!(f32x16::from_slice(simd, &first).reduce_max_precise(), 12.0); let mut last = [4.0; 16]; - last[15] = nan; + last[15] = S_NAN_F32; last[2] = 12.0; assert_eq!(f32x16::from_slice(simd, &last).reduce_max_precise(), 12.0); - let multiple = f32x16::from_fn(simd, |i| if i % 3 == 0 { nan } else { i as f32 }); + let multiple = f32x16::from_fn(simd, |i| if i % 3 == 0 { S_NAN_F32 } else { i as f32 }); assert_eq!(multiple.reduce_max_precise(), 14.0); assert!( - f32x16::from_slice(simd, &[nan; 16]) + f32x16::from_slice(simd, &[S_NAN_F32; 16]) .reduce_max_precise() .is_nan() ); @@ -168,21 +176,26 @@ fn reduce_max_precise_f32x16(simd: S) { #[simd_test] fn reduce_max_precise_f64x8(simd: S) { - let nan = f64::from_bits(0x7ff8_0000_0000_1234); assert_eq!( - f64x8::from_slice(simd, &[nan, 4.0, 12.0, 7.0, 9.0, 3.0, 6.0, 8.0]).reduce_max_precise(), + f64x8::from_slice(simd, &[Q_NAN_F64, 4.0, 12.0, 7.0, 9.0, 3.0, 6.0, 8.0]) + .reduce_max_precise(), 12.0 ); assert_eq!( - f64x8::from_slice(simd, &[4.0, 12.0, 7.0, 9.0, 3.0, 6.0, 8.0, nan]).reduce_max_precise(), + f64x8::from_slice(simd, &[4.0, 12.0, 7.0, 9.0, 3.0, 6.0, 8.0, S_NAN_F64]) + .reduce_max_precise(), 12.0 ); assert_eq!( - f64x8::from_slice(simd, &[nan, 4.0, nan, 7.0, 12.0, nan, 6.0, 8.0]).reduce_max_precise(), + f64x8::from_slice( + simd, + &[Q_NAN_F64, 4.0, S_NAN_F64, 7.0, 12.0, S_NAN_F64, 6.0, 8.0] + ) + .reduce_max_precise(), 12.0 ); assert!( - f64x8::from_slice(simd, &[nan; 8]) + f64x8::from_slice(simd, &[Q_NAN_F64; 8]) .reduce_max_precise() .is_nan() ); @@ -191,7 +204,7 @@ fn reduce_max_precise_f64x8(simd: S) { simd, &[ f64::NEG_INFINITY, - nan, + S_NAN_F64, 3.0, 8.0, 2.0, diff --git a/fearless_simd_tests/tests/harness/ops/reduce_min_precise.rs b/fearless_simd_tests/tests/harness/ops/reduce_min_precise.rs index 79411b681..8424dda60 100644 --- a/fearless_simd_tests/tests/harness/ops/reduce_min_precise.rs +++ b/fearless_simd_tests/tests/harness/ops/reduce_min_precise.rs @@ -4,28 +4,33 @@ use fearless_simd::*; use fearless_simd_dev_macros::simd_test; +const Q_NAN_F32: f32 = f32::NAN; +const Q_NAN_F64: f64 = f64::NAN; +const S_NAN_F32: f32 = f32::from_bits(0x7FFF_FFFF); +const S_NAN_F64: f64 = f64::from_bits(0x7FFF_FFFF_FFFF_FFFF); + #[simd_test] fn reduce_min_precise_f32x4(simd: S) { - let nan = f32::from_bits(0x7fc0_1234); assert_eq!( - f32x4::from_slice(simd, &[nan, 4.0, -2.0, 7.0]).reduce_min_precise(), + f32x4::from_slice(simd, &[Q_NAN_F32, 4.0, -2.0, 7.0]).reduce_min_precise(), -2.0 ); assert_eq!( - f32x4::from_slice(simd, &[4.0, -2.0, 7.0, nan]).reduce_min_precise(), + f32x4::from_slice(simd, &[4.0, -2.0, 7.0, S_NAN_F32]).reduce_min_precise(), -2.0 ); assert_eq!( - f32x4::from_slice(simd, &[nan, 4.0, nan, -2.0]).reduce_min_precise(), + f32x4::from_slice(simd, &[Q_NAN_F32, 4.0, S_NAN_F32, -2.0]).reduce_min_precise(), -2.0 ); assert!( - f32x4::from_slice(simd, &[nan; 4]) + f32x4::from_slice(simd, &[Q_NAN_F32; 4]) .reduce_min_precise() .is_nan() ); assert_eq!( - f32x4::from_slice(simd, &[f32::INFINITY, nan, 3.0, f32::NEG_INFINITY]).reduce_min_precise(), + f32x4::from_slice(simd, &[f32::INFINITY, S_NAN_F32, 3.0, f32::NEG_INFINITY]) + .reduce_min_precise(), f32::NEG_INFINITY ); assert_eq!( @@ -36,17 +41,16 @@ fn reduce_min_precise_f32x4(simd: S) { #[simd_test] fn reduce_min_precise_f64x2(simd: S) { - let nan = f64::from_bits(0x7ff8_0000_0000_1234); assert_eq!( - f64x2::from_slice(simd, &[nan, -2.0]).reduce_min_precise(), + f64x2::from_slice(simd, &[Q_NAN_F64, -2.0]).reduce_min_precise(), -2.0 ); assert_eq!( - f64x2::from_slice(simd, &[-2.0, nan]).reduce_min_precise(), + f64x2::from_slice(simd, &[-2.0, S_NAN_F64]).reduce_min_precise(), -2.0 ); assert!( - f64x2::from_slice(simd, &[nan; 2]) + f64x2::from_slice(simd, &[S_NAN_F64; 2]) .reduce_min_precise() .is_nan() ); @@ -62,21 +66,26 @@ fn reduce_min_precise_f64x2(simd: S) { #[simd_test] fn reduce_min_precise_f32x8(simd: S) { - let nan = f32::from_bits(0x7fc0_1234); assert_eq!( - f32x8::from_slice(simd, &[nan, 4.0, -2.0, 7.0, 9.0, 3.0, 6.0, 8.0]).reduce_min_precise(), + f32x8::from_slice(simd, &[Q_NAN_F32, 4.0, -2.0, 7.0, 9.0, 3.0, 6.0, 8.0]) + .reduce_min_precise(), -2.0 ); assert_eq!( - f32x8::from_slice(simd, &[4.0, -2.0, 7.0, 9.0, 3.0, 6.0, 8.0, nan]).reduce_min_precise(), + f32x8::from_slice(simd, &[4.0, -2.0, 7.0, 9.0, 3.0, 6.0, 8.0, S_NAN_F32]) + .reduce_min_precise(), -2.0 ); assert_eq!( - f32x8::from_slice(simd, &[nan, 4.0, nan, 7.0, -2.0, nan, 6.0, 8.0]).reduce_min_precise(), + f32x8::from_slice( + simd, + &[Q_NAN_F32, 4.0, S_NAN_F32, 7.0, -2.0, S_NAN_F32, 6.0, 8.0] + ) + .reduce_min_precise(), -2.0 ); assert!( - f32x8::from_slice(simd, &[nan; 8]) + f32x8::from_slice(simd, &[Q_NAN_F32; 8]) .reduce_min_precise() .is_nan() ); @@ -85,7 +94,7 @@ fn reduce_min_precise_f32x8(simd: S) { simd, &[ f32::INFINITY, - nan, + S_NAN_F32, 3.0, 8.0, 2.0, @@ -103,26 +112,26 @@ fn reduce_min_precise_f32x8(simd: S) { #[simd_test] fn reduce_min_precise_f64x4(simd: S) { - let nan = f64::from_bits(0x7ff8_0000_0000_1234); assert_eq!( - f64x4::from_slice(simd, &[nan, 4.0, -2.0, 7.0]).reduce_min_precise(), + f64x4::from_slice(simd, &[Q_NAN_F64, 4.0, -2.0, 7.0]).reduce_min_precise(), -2.0 ); assert_eq!( - f64x4::from_slice(simd, &[4.0, -2.0, 7.0, nan]).reduce_min_precise(), + f64x4::from_slice(simd, &[4.0, -2.0, 7.0, S_NAN_F64]).reduce_min_precise(), -2.0 ); assert_eq!( - f64x4::from_slice(simd, &[nan, 4.0, nan, -2.0]).reduce_min_precise(), + f64x4::from_slice(simd, &[Q_NAN_F64, 4.0, S_NAN_F64, -2.0]).reduce_min_precise(), -2.0 ); assert!( - f64x4::from_slice(simd, &[nan; 4]) + f64x4::from_slice(simd, &[S_NAN_F64; 4]) .reduce_min_precise() .is_nan() ); assert_eq!( - f64x4::from_slice(simd, &[f64::INFINITY, nan, 3.0, f64::NEG_INFINITY]).reduce_min_precise(), + f64x4::from_slice(simd, &[f64::INFINITY, S_NAN_F64, 3.0, f64::NEG_INFINITY]) + .reduce_min_precise(), f64::NEG_INFINITY ); assert_eq!( @@ -133,21 +142,20 @@ fn reduce_min_precise_f64x4(simd: S) { #[simd_test] fn reduce_min_precise_f32x16(simd: S) { - let nan = f32::from_bits(0x7fc0_1234); let mut first = [4.0; 16]; - first[0] = nan; + first[0] = S_NAN_F32; first[13] = -2.0; assert_eq!(f32x16::from_slice(simd, &first).reduce_min_precise(), -2.0); let mut last = [4.0; 16]; - last[15] = nan; + last[15] = S_NAN_F32; last[2] = -2.0; assert_eq!(f32x16::from_slice(simd, &last).reduce_min_precise(), -2.0); - let multiple = f32x16::from_fn(simd, |i| if i % 3 == 0 { nan } else { i as f32 }); + let multiple = f32x16::from_fn(simd, |i| if i % 3 == 0 { S_NAN_F32 } else { i as f32 }); assert_eq!(multiple.reduce_min_precise(), 1.0); assert!( - f32x16::from_slice(simd, &[nan; 16]) + f32x16::from_slice(simd, &[S_NAN_F32; 16]) .reduce_min_precise() .is_nan() ); @@ -168,21 +176,26 @@ fn reduce_min_precise_f32x16(simd: S) { #[simd_test] fn reduce_min_precise_f64x8(simd: S) { - let nan = f64::from_bits(0x7ff8_0000_0000_1234); assert_eq!( - f64x8::from_slice(simd, &[nan, 4.0, -2.0, 7.0, 9.0, 3.0, 6.0, 8.0]).reduce_min_precise(), + f64x8::from_slice(simd, &[S_NAN_F64, 4.0, -2.0, 7.0, 9.0, 3.0, 6.0, 8.0]) + .reduce_min_precise(), -2.0 ); assert_eq!( - f64x8::from_slice(simd, &[4.0, -2.0, 7.0, 9.0, 3.0, 6.0, 8.0, nan]).reduce_min_precise(), + f64x8::from_slice(simd, &[4.0, -2.0, 7.0, 9.0, 3.0, 6.0, 8.0, S_NAN_F64]) + .reduce_min_precise(), -2.0 ); assert_eq!( - f64x8::from_slice(simd, &[nan, 4.0, nan, 7.0, -2.0, nan, 6.0, 8.0]).reduce_min_precise(), + f64x8::from_slice( + simd, + &[S_NAN_F64, 4.0, Q_NAN_F64, 7.0, -2.0, S_NAN_F64, 6.0, 8.0] + ) + .reduce_min_precise(), -2.0 ); assert!( - f64x8::from_slice(simd, &[nan; 8]) + f64x8::from_slice(simd, &[S_NAN_F64; 8]) .reduce_min_precise() .is_nan() ); @@ -191,7 +204,7 @@ fn reduce_min_precise_f64x8(simd: S) { simd, &[ f64::INFINITY, - nan, + S_NAN_F64, 3.0, 8.0, 2.0, From 1f5a872f698d6199e40dc828b644d046c968a46e Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sun, 8 Nov 2026 18:10:07 +0100 Subject: [PATCH 3/3] More efficient for 256 and 512-bit vectors --- fearless_simd/src/generated/neon.rs | 144 +++++++++++++++++++++- fearless_simd/src/generated/simd_trait.rs | 28 ++--- fearless_simd_gen/src/mk_neon.rs | 94 +++++++++++--- 3 files changed, 234 insertions(+), 32 deletions(-) diff --git a/fearless_simd/src/generated/neon.rs b/fearless_simd/src/generated/neon.rs index 3b43c3a33..2a5b9b0cb 100644 --- a/fearless_simd/src/generated/neon.rs +++ b/fearless_simd/src/generated/neon.rs @@ -349,7 +349,7 @@ impl Simd for Neon { #[inline(always)] fn kernel(token: Neon, a: f32x4) -> f32 { let a = vmaxnmq_f32(a.into(), a.into()); - vmaxnmvq_f32(a.into()) + vmaxnmvq_f32(a) } ); kernel(self, a) @@ -360,7 +360,7 @@ impl Simd for Neon { #[inline(always)] fn kernel(token: Neon, a: f32x4) -> f32 { let a = vminnmq_f32(a.into(), a.into()); - vminnmvq_f32(a.into()) + vminnmvq_f32(a) } ); kernel(self, a) @@ -5249,7 +5249,7 @@ impl Simd for Neon { #[inline(always)] fn kernel(token: Neon, a: f64x2) -> f64 { let a = vmaxnmq_f64(a.into(), a.into()); - vmaxnmvq_f64(a.into()) + vmaxnmvq_f64(a) } ); kernel(self, a) @@ -5260,7 +5260,7 @@ impl Simd for Neon { #[inline(always)] fn kernel(token: Neon, a: f64x2) -> f64 { let a = vminnmq_f64(a.into(), a.into()); - vminnmvq_f64(a.into()) + vminnmvq_f64(a) } ); kernel(self, a) @@ -6994,6 +6994,34 @@ impl Simd for Neon { )) } #[inline(always)] + fn reduce_max_precise_f32x8(self, a: f32x8) -> f32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: f32x8) -> f32 { + let (a0, a1) = a.split(); + let a0 = vmaxnmq_f32(a0.into(), a0.into()); + let a1 = vmaxnmq_f32(a1.into(), a1.into()); + let a = vmaxnmq_f32(a0, a1); + vmaxnmvq_f32(a) + } + ); + kernel(self, a) + } + #[inline(always)] + fn reduce_min_precise_f32x8(self, a: f32x8) -> f32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: f32x8) -> f32 { + let (a0, a1) = a.split(); + let a0 = vminnmq_f32(a0.into(), a0.into()); + let a1 = vminnmq_f32(a1.into(), a1.into()); + let a = vminnmq_f32(a0, a1); + vminnmvq_f32(a) + } + ); + kernel(self, a) + } + #[inline(always)] fn is_nan_f32x8(self, a: f32x8) -> mask32x8 { a.simd_ne(a) } @@ -8207,6 +8235,34 @@ impl Simd for Neon { )) } #[inline(always)] + fn reduce_max_precise_f64x4(self, a: f64x4) -> f64 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: f64x4) -> f64 { + let (a0, a1) = a.split(); + let a0 = vmaxnmq_f64(a0.into(), a0.into()); + let a1 = vmaxnmq_f64(a1.into(), a1.into()); + let a = vmaxnmq_f64(a0, a1); + vmaxnmvq_f64(a) + } + ); + kernel(self, a) + } + #[inline(always)] + fn reduce_min_precise_f64x4(self, a: f64x4) -> f64 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: f64x4) -> f64 { + let (a0, a1) = a.split(); + let a0 = vminnmq_f64(a0.into(), a0.into()); + let a1 = vminnmq_f64(a1.into(), a1.into()); + let a = vminnmq_f64(a0, a1); + vminnmvq_f64(a) + } + ); + kernel(self, a) + } + #[inline(always)] fn is_nan_f64x4(self, a: f64x4) -> mask64x4 { a.simd_ne(a) } @@ -8676,6 +8732,46 @@ impl Simd for Neon { )) } #[inline(always)] + fn reduce_max_precise_f32x16(self, a: f32x16) -> f32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: f32x16) -> f32 { + let (a0, a1) = a.split(); + let (a00, a01) = a0.split(); + let (a10, a11) = a1.split(); + let a00 = vmaxnmq_f32(a00.into(), a00.into()); + let a01 = vmaxnmq_f32(a01.into(), a01.into()); + let a10 = vmaxnmq_f32(a10.into(), a10.into()); + let a11 = vmaxnmq_f32(a11.into(), a11.into()); + let a0 = vmaxnmq_f32(a00, a01); + let a1 = vmaxnmq_f32(a10, a11); + let a = vmaxnmq_f32(a0, a1); + vmaxnmvq_f32(a) + } + ); + kernel(self, a) + } + #[inline(always)] + fn reduce_min_precise_f32x16(self, a: f32x16) -> f32 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: f32x16) -> f32 { + let (a0, a1) = a.split(); + let (a00, a01) = a0.split(); + let (a10, a11) = a1.split(); + let a00 = vminnmq_f32(a00.into(), a00.into()); + let a01 = vminnmq_f32(a01.into(), a01.into()); + let a10 = vminnmq_f32(a10.into(), a10.into()); + let a11 = vminnmq_f32(a11.into(), a11.into()); + let a0 = vminnmq_f32(a00, a01); + let a1 = vminnmq_f32(a10, a11); + let a = vminnmq_f32(a0, a1); + vminnmvq_f32(a) + } + ); + kernel(self, a) + } + #[inline(always)] fn is_nan_f32x16(self, a: f32x16) -> mask32x16 { a.simd_ne(a) } @@ -9946,6 +10042,46 @@ impl Simd for Neon { )) } #[inline(always)] + fn reduce_max_precise_f64x8(self, a: f64x8) -> f64 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: f64x8) -> f64 { + let (a0, a1) = a.split(); + let (a00, a01) = a0.split(); + let (a10, a11) = a1.split(); + let a00 = vmaxnmq_f64(a00.into(), a00.into()); + let a01 = vmaxnmq_f64(a01.into(), a01.into()); + let a10 = vmaxnmq_f64(a10.into(), a10.into()); + let a11 = vmaxnmq_f64(a11.into(), a11.into()); + let a0 = vmaxnmq_f64(a00, a01); + let a1 = vmaxnmq_f64(a10, a11); + let a = vmaxnmq_f64(a0, a1); + vmaxnmvq_f64(a) + } + ); + kernel(self, a) + } + #[inline(always)] + fn reduce_min_precise_f64x8(self, a: f64x8) -> f64 { + crate::kernel!( + #[inline(always)] + fn kernel(token: Neon, a: f64x8) -> f64 { + let (a0, a1) = a.split(); + let (a00, a01) = a0.split(); + let (a10, a11) = a1.split(); + let a00 = vminnmq_f64(a00.into(), a00.into()); + let a01 = vminnmq_f64(a01.into(), a01.into()); + let a10 = vminnmq_f64(a10.into(), a10.into()); + let a11 = vminnmq_f64(a11.into(), a11.into()); + let a0 = vminnmq_f64(a00, a01); + let a1 = vminnmq_f64(a10, a11); + let a = vminnmq_f64(a0, a1); + vminnmvq_f64(a) + } + ); + kernel(self, a) + } + #[inline(always)] fn is_nan_f64x8(self, a: f64x8) -> mask64x8 { a.simd_ne(a) } diff --git a/fearless_simd/src/generated/simd_trait.rs b/fearless_simd/src/generated/simd_trait.rs index 4ffd58e63..106b60f3d 100644 --- a/fearless_simd/src/generated/simd_trait.rs +++ b/fearless_simd/src/generated/simd_trait.rs @@ -332,9 +332,9 @@ pub trait Simd: fn reduce_max_f32x4(self, a: f32x4) -> f32; #[doc = "Return the minimum element in the vector. Integer vectors always return the exact minimum.\n\nFor floating-point vectors with no NaNs, this returns the true minimum. If any lane is NaN, the entire result is implementation-defined: it may be NaN or a numeric lane that is not the true minimum. See `reduce_min_precise` for a version that ignores quiet NaNs.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] fn reduce_min_f32x4(self, a: f32x4) -> f32; - #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true maximum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] fn reduce_max_precise_f32x4(self, a: f32x4) -> f32; - #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true minimum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] fn reduce_min_precise_f32x4(self, a: f32x4) -> f32; #[doc = "Return the sum of all elements in the vector. Integer addition wraps.\n\n# Floating-point accuracy\n\nFor an input vector with N lanes, any lane's contribution may be rounded at most `log2(N)` times.\n\nFor a fixed vector type and lane count, this operation produces the same result on all platforms and backends down to the bit pattern, except that when the result is NaN, its exact bit pattern is unspecified. This fixed-width guarantee does not make code using native-width associated types such as `S::f32s` independent of the selected SIMD level, because their lane counts can differ.\n\nBecause floating-point addition is not associative, separately reducing smaller vectors and then adding their results can differ from reducing their combined wider vector. See [Taming Floating-Point Sums](https://orlp.net/blog/taming-float-sums/) for more information and for other summation algorithms, including exact summation without accumulated rounding error. In that article's terms, our method has the precision properties of pairwise summation, although the exact pairing of values is different."] fn reduce_sum_f32x4(self, a: f32x4) -> f32; @@ -1839,9 +1839,9 @@ pub trait Simd: fn reduce_max_f64x2(self, a: f64x2) -> f64; #[doc = "Return the minimum element in the vector. Integer vectors always return the exact minimum.\n\nFor floating-point vectors with no NaNs, this returns the true minimum. If any lane is NaN, the entire result is implementation-defined: it may be NaN or a numeric lane that is not the true minimum. See `reduce_min_precise` for a version that ignores quiet NaNs.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] fn reduce_min_f64x2(self, a: f64x2) -> f64; - #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true maximum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] fn reduce_max_precise_f64x2(self, a: f64x2) -> f64; - #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true minimum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] fn reduce_min_precise_f64x2(self, a: f64x2) -> f64; #[doc = "Return the sum of all elements in the vector. Integer addition wraps.\n\n# Floating-point accuracy\n\nFor an input vector with N lanes, any lane's contribution may be rounded at most `log2(N)` times.\n\nFor a fixed vector type and lane count, this operation produces the same result on all platforms and backends down to the bit pattern, except that when the result is NaN, its exact bit pattern is unspecified. This fixed-width guarantee does not make code using native-width associated types such as `S::f32s` independent of the selected SIMD level, because their lane counts can differ.\n\nBecause floating-point addition is not associative, separately reducing smaller vectors and then adding their results can differ from reducing their combined wider vector. See [Taming Floating-Point Sums](https://orlp.net/blog/taming-float-sums/) for more information and for other summation algorithms, including exact summation without accumulated rounding error. In that article's terms, our method has the precision properties of pairwise summation, although the exact pairing of values is different."] fn reduce_sum_f64x2(self, a: f64x2) -> f64; @@ -2544,13 +2544,13 @@ pub trait Simd: let (a0, a1) = self.split_f32x8(a); self.reduce_min_f32x4(self.min_f32x4(a0, a1)) } - #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true maximum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] #[inline(always)] fn reduce_max_precise_f32x8(self, a: f32x8) -> f32 { let (a0, a1) = self.split_f32x8(a); self.reduce_max_precise_f32x4(self.max_precise_f32x4(a0, a1)) } - #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true minimum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] #[inline(always)] fn reduce_min_precise_f32x8(self, a: f32x8) -> f32 { let (a0, a1) = self.split_f32x8(a); @@ -6061,13 +6061,13 @@ pub trait Simd: let (a0, a1) = self.split_f64x4(a); self.reduce_min_f64x2(self.min_f64x2(a0, a1)) } - #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true maximum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] #[inline(always)] fn reduce_max_precise_f64x4(self, a: f64x4) -> f64 { let (a0, a1) = self.split_f64x4(a); self.reduce_max_precise_f64x2(self.max_precise_f64x2(a0, a1)) } - #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true minimum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] #[inline(always)] fn reduce_min_precise_f64x4(self, a: f64x4) -> f64 { let (a0, a1) = self.split_f64x4(a); @@ -7494,13 +7494,13 @@ pub trait Simd: let (a0, a1) = self.split_f32x16(a); self.reduce_min_f32x8(self.min_f32x8(a0, a1)) } - #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true maximum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] #[inline(always)] fn reduce_max_precise_f32x16(self, a: f32x16) -> f32 { let (a0, a1) = self.split_f32x16(a); self.reduce_max_precise_f32x8(self.max_precise_f32x8(a0, a1)) } - #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true minimum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] #[inline(always)] fn reduce_min_precise_f32x16(self, a: f32x16) -> f32 { let (a0, a1) = self.split_f32x16(a); @@ -11024,13 +11024,13 @@ pub trait Simd: let (a0, a1) = self.split_f64x8(a); self.reduce_min_f64x4(self.min_f64x4(a0, a1)) } - #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true maximum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] #[inline(always)] fn reduce_max_precise_f64x8(self, a: f64x8) -> f64 { let (a0, a1) = self.split_f64x8(a); self.reduce_max_precise_f64x4(self.max_precise_f64x4(a0, a1)) } - #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true minimum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] #[inline(always)] fn reduce_min_precise_f64x8(self, a: f64x8) -> f64 { let (a0, a1) = self.split_f64x8(a); @@ -12873,9 +12873,9 @@ pub trait SimdBase: fn reduce_max(self) -> Self::Element; #[doc = "Return the minimum element in the vector. Integer vectors always return the exact minimum.\n\nFor floating-point vectors with no NaNs, this returns the true minimum. If any lane is NaN, the entire result is implementation-defined: it may be NaN or a numeric lane that is not the true minimum. See `reduce_min_precise` for a version that ignores quiet NaNs.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] fn reduce_min(self) -> Self::Element; - #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true maximum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the maximum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_max`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true maximum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] fn reduce_max_precise(self) -> Self::Element; - #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, quiet NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are quiet NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned.\n\nIf any lane is a *signaling* NaN, the result is fully non-deterministic: it may be NaN or a numeric lane and is not guaranteed to be the true minimum.\nSignaling NaN values are not produced by floating-point math operations, only from manual initialization with specific bit patterns. You probably don't need to worry about them."] + #[doc = "Return the minimum element in the vector, ignoring quiet NaNs.\n\nFor integer vectors, this operation is the same as `reduce_min`.\n\nFor floating-point vectors, NaNs are ignored. If there is at least one numeric lane, this returns the true minimum of the numeric lanes. If all lanes are NaNs, this returns NaN, with an unspecified payload and sign.\n\nIf the floating-point vector contains both positive zero and negative zero, either sign of zero may be returned."] fn reduce_min_precise(self) -> Self::Element; #[doc = "Return the sum of all elements in the vector. Integer addition wraps.\n\n# Floating-point accuracy\n\nFor an input vector with N lanes, any lane's contribution may be rounded at most `log2(N)` times.\n\nFor a fixed vector type and lane count, this operation produces the same result on all platforms and backends down to the bit pattern, except that when the result is NaN, its exact bit pattern is unspecified. This fixed-width guarantee does not make code using native-width associated types such as `S::f32s` independent of the selected SIMD level, because their lane counts can differ.\n\nBecause floating-point addition is not associative, separately reducing smaller vectors and then adding their results can differ from reducing their combined wider vector. See [Taming Floating-Point Sums](https://orlp.net/blog/taming-float-sums/) for more information and for other summation algorithms, including exact summation without accumulated rounding error. In that article's terms, our method has the precision properties of pairwise summation, although the exact pairing of values is different."] fn reduce_sum(self) -> Self::Element; diff --git a/fearless_simd_gen/src/mk_neon.rs b/fearless_simd_gen/src/mk_neon.rs index f8539dcac..dfb8ffccc 100644 --- a/fearless_simd_gen/src/mk_neon.rs +++ b/fearless_simd_gen/src/mk_neon.rs @@ -354,6 +354,10 @@ impl Level for Neon { }) } OpSig::Reduce { lane_op } => { + if matches!(lane_op, "min_precise" | "max_precise") { + return self.handle_min_max_precise(op, vec_ty); + } + assert_eq!( vec_ty.n_bits(), 128, @@ -372,26 +376,14 @@ impl Level for Neon { return self.kernel_method(op, vec_ty, |_| reduce); } - let preprocess = if matches!(lane_op, "min_precise" | "max_precise") { - let expr = - neon::expr(lane_op, vec_ty, &[quote! { a.into() }, quote! { a.into() }]); - quote! { - let a = #expr; - } - } else { - TokenStream::new() - }; - let intrinsic = match lane_op { "add" => "vaddv", "min" => "vminv", "max" => "vmaxv", - "min_precise" => "vminnmv", - "max_precise" => "vmaxnmv", _ => unreachable!("unsupported reduction lane operation"), }; let reduce = simple_intrinsic(intrinsic, vec_ty); - self.kernel_method(op, vec_ty, |_| quote! { #preprocess #reduce(a.into()) }) + self.kernel_method(op, vec_ty, |_| quote! { #reduce(a.into()) }) } OpSig::RotateElements { .. } => integer_lane_mask_rotate(op, vec_ty), OpSig::LoadInterleaved { @@ -1059,7 +1051,13 @@ impl Level for Neon { } fn should_use_generic_op(&self, op: &Op, vec_ty: &VecType) -> bool { - if matches!(op.sig, OpSig::MaskToBitmask) { + if matches!( + op.sig, + OpSig::MaskToBitmask + | OpSig::Reduce { + lane_op: "min_precise" | "max_precise" + } + ) { false } else { op.sig.should_use_generic_op(vec_ty, self.native_width()) @@ -1268,6 +1266,74 @@ impl Neon { } }) } + + fn handle_min_max_precise(&self, op: Op, vec_ty: &VecType) -> TokenStream { + let lane_op = match op.sig { + OpSig::Reduce { lane_op } if matches!(lane_op, "min_precise" | "max_precise") => { + lane_op + } + _ => unimplemented!("unsupported reduction lane operation"), + }; + + // native-width 128-bit vector type + let vec_ty_128 = VecType::new(vec_ty.scalar, vec_ty.scalar_bits, 128 / vec_ty.scalar_bits); + + // Instruction for native-width element-wise min/max operation. + // This will return qNaN if one operand is sNaN. + // We work around this using `min_max_128(x, x)` to convert all sNaNs to qNaNs. + let min_max_128 = simple_intrinsic( + match lane_op { + "min_precise" => "vminnm", + "max_precise" => "vmaxnm", + _ => unreachable!("unsupported reduction lane operation"), + }, + &vec_ty_128, + ); + + // Reduce from original vector to native-width 128-bit vector + // The reduced vector is guaranteed to not contain sNaNs. + let vector_reduction = match vec_ty.n_bits() { + 128 => quote! { + // convert sNaNs to qNaNs + let a = #min_max_128(a.into(), a.into()); + }, + 256 => quote! { + let (a0, a1) = a.split(); + // convert sNaNs to qNaNs + let a0 = #min_max_128(a0.into(), a0.into()); + let a1 = #min_max_128(a1.into(), a1.into()); + // min/max + let a = #min_max_128(a0, a1); + }, + 512 => quote! { + let (a0, a1) = a.split(); + let (a00, a01) = a0.split(); + let (a10, a11) = a1.split(); + // convert sNaNs to qNaNs + let a00 = #min_max_128(a00.into(), a00.into()); + let a01 = #min_max_128(a01.into(), a01.into()); + let a10 = #min_max_128(a10.into(), a10.into()); + let a11 = #min_max_128(a11.into(), a11.into()); + // min/max + let a0 = #min_max_128(a00, a01); + let a1 = #min_max_128(a10, a11); + let a = #min_max_128(a0, a1); + }, + _ => unimplemented!("unsupported vector width"), + }; + + // Final reduction from native-width to scalar. + // This relies on the vector not containing sNaNs. + let reduce_128 = simple_intrinsic( + match lane_op { + "min_precise" => "vminnmv", + "max_precise" => "vmaxnmv", + _ => unreachable!("unsupported reduction lane operation"), + }, + &vec_ty_128, + ); + self.kernel_method(op, vec_ty, |_| quote! { #vector_reduction #reduce_128(a) }) + } } fn mk_slide_helpers() -> TokenStream {