Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

test(compose): compare NaN as NaN in the vector span test, not by its bits

Akos Arintondfeba9e97497
diff --git a/crates/compose/src/psblend.rs b/crates/compose/src/psblend.rs
index b5eeaa79d..4c0556be2 100644
--- a/crates/compose/src/psblend.rs
+++ b/crates/compose/src/psblend.rs
@@ -238,7 +238,7 @@ pub(crate) fn composite_normal_rgb(backdrop: [f32; 4], source: [f32; 4], opacity
normal_rgb(backdrop, source, as_, ao)
}
-/// [`composite_normal_rgb`] over a row, bit for bit, in blocks the compiler vectorizes (see
+/// [`composite_normal_rgb`] over a row, with the same bits, in blocks the compiler vectorizes (see
/// [`normal_lanes`]); the tail that doesn't fill a block goes through the scalar kernel.
pub(crate) fn composite_normal_rgb_span(dst: &mut [[f32; 4]], src: &[[f32; 4]], opacity: f32) {
let n = dst.len().min(src.len());
@@ -262,7 +262,8 @@ const LANES: usize = 8;
/// [`composite_normal_rgb`] (after the caller's `source alpha > 0` test) on one block of pixels,
/// without branches: one array per channel, fixed-length loops the compiler vectorizes, and a
/// select per pixel between the backdrop, zero and the mix. Same operations in the same order
-/// (Rust never fuses them into multiply-adds) and the same tests, NaN included: bit-identical.
+/// (Rust never fuses them into multiply-adds) and the same tests: the same bits, except that a
+/// NaN's sign and payload may differ (IEEE 754 doesn't fix which operand's NaN is passed on).
#[inline]
#[allow(clippy::needless_range_loop)] // lanes index several arrays in lockstep
fn normal_lanes(b: &mut [[f32; 4]; LANES], s: &[[f32; 4]; LANES], opacity: f32) {
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index 4cc6e7925..5043657c1 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -82,7 +82,7 @@ fn normal_rgb_kernel_preserves_scalar_bits() {
/// The vectorized row kernel against the scalar loop it replaced, over row lengths that cover
/// whole blocks, tails and both, with samples full of NaN, signed zeros, infinities, subnormals
-/// and values outside 0..1.
+/// and values outside 0..1: the same bits, except that a NaN only has to stay NaN.
#[test]
fn normal_rgb_span_preserves_scalar_bits() {
let specials = [0.0, -0.0, 1.0, 0.5, 0.03, -0.5, 2.0, f32::NAN, f32::INFINITY, f32::NEG_INFINITY, f32::from_bits(1), f32::MIN_POSITIVE, 1e-30];
@@ -111,8 +111,11 @@ fn normal_rgb_span_preserves_scalar_bits() {
}
let mut got = backdrop;
psblend::composite_normal_rgb_span(&mut got, &source, opacity);
+ // IEEE 754 leaves open which operand's NaN an operation passes on (x86 and the
+ // vector units differ in its sign), so any NaN counts as the same NaN.
+ let bits = |p: &[f32; 4]| p.map(|v| if v.is_nan() { u32::MAX } else { v.to_bits() });
for (i, (g, w)) in got.iter().zip(&want).enumerate() {
- assert_eq!(g.map(f32::to_bits), w.map(f32::to_bits), "len {len}, pixel {i}, opacity {opacity}: {g:?} vs {w:?}");
+ assert_eq!(bits(g), bits(w), "len {len}, pixel {i}, opacity {opacity}: {g:?} vs {w:?}");
}
}
}