dimforge / simba

Set of mathematical traits to facilitate the use of SIMD-based AoSoA (Array of Struct of Array) storage pattern.
Apache License 2.0
290 stars 29 forks source link

Inconsistent behaviour of `SimdPartialOrd::simd_min` and `simd_max` when values are not comparable #51

Open arscisca opened 9 months ago

arscisca commented 9 months ago

When values are not comparable, these methods always return the second value.

An example of this is calling simd_min on a valid f32 and f32::NAN:

let number = 1.0f32;
let nan = f32::NAN;
println!("{}", number.simd_min(nan)); // Prints "NaN"
println!("{}", nan.simd_min(number)); // Prints "1"

This happens because the current implementation of simd_min is the following:

fn simd_min(self, other: Self) -> Self {
    if self <= other {
        self
    } else {
        other
    }
}

and a <= (or any syntax-sugared comparison in general) between two instances of a PartialOrd type where a.partial_ord(&b) returns None is evaluated to false.

arscisca commented 9 months ago

On a side note, all the other comparison methods under SimdPartialOrd will return a bool regardless of whether the values are actually comparable or not. I imagine this is either SIMD requirement or a convenience choice, but at this point isn't the trait behaving more like a SimdOrd which is implemented for PartialOrd types like f32 and f64 for convenience?