simba/simd/
simd_partial_ord.rs

1use crate::simd::SimdValue;
2
3/// Lane-wise generalization of the standard `PartialOrd` for SIMD values.
4pub trait SimdPartialOrd: SimdValue {
5    /// Lanewise _greater than_ `>` comparison.
6    fn simd_gt(self, other: Self) -> Self::SimdBool;
7    /// Lanewise _less than_ `<` comparison.
8    fn simd_lt(self, other: Self) -> Self::SimdBool;
9    /// Lanewise _greater or equal_ `>=` comparison.
10    fn simd_ge(self, other: Self) -> Self::SimdBool;
11    /// Lanewise _less or equal_ `<=` comparison.
12    fn simd_le(self, other: Self) -> Self::SimdBool;
13    /// Lanewise _equal_ `==` comparison.
14    fn simd_eq(self, other: Self) -> Self::SimdBool;
15    /// Lanewise _not equal_ `!=` comparison.
16    fn simd_ne(self, other: Self) -> Self::SimdBool;
17
18    /// Lanewise max value.
19    fn simd_max(self, other: Self) -> Self;
20    /// Lanewise min value.
21    fn simd_min(self, other: Self) -> Self;
22    /// Clamps each lane of `self` between the corresponding lane of `min` and `max`.
23    fn simd_clamp(self, min: Self, max: Self) -> Self;
24
25    /// The min value among all lanes of `self`.
26    fn simd_horizontal_min(self) -> Self::Element;
27    /// The max value among all lanes of `self`.
28    fn simd_horizontal_max(self) -> Self::Element;
29}
30
31impl<T: PartialOrd + SimdValue<Element = T, SimdBool = bool>> SimdPartialOrd for T {
32    #[inline(always)]
33    fn simd_gt(self, other: Self) -> Self::SimdBool {
34        self > other
35    }
36
37    #[inline(always)]
38    fn simd_lt(self, other: Self) -> Self::SimdBool {
39        self < other
40    }
41
42    #[inline(always)]
43    fn simd_ge(self, other: Self) -> Self::SimdBool {
44        self >= other
45    }
46
47    #[inline(always)]
48    fn simd_le(self, other: Self) -> Self::SimdBool {
49        self <= other
50    }
51
52    #[inline(always)]
53    fn simd_eq(self, other: Self) -> Self::SimdBool {
54        self == other
55    }
56
57    #[inline(always)]
58    fn simd_ne(self, other: Self) -> Self::SimdBool {
59        self != other
60    }
61
62    #[inline(always)]
63    fn simd_max(self, other: Self) -> Self {
64        if self >= other {
65            self
66        } else {
67            other
68        }
69    }
70
71    #[inline(always)]
72    fn simd_min(self, other: Self) -> Self {
73        if self <= other {
74            self
75        } else {
76            other
77        }
78    }
79
80    #[inline(always)]
81    fn simd_clamp(self, min: Self, max: Self) -> Self {
82        if self < min {
83            min
84        } else if self > max {
85            max
86        } else {
87            self
88        }
89    }
90
91    #[inline(always)]
92    fn simd_horizontal_min(self) -> Self::Element {
93        self
94    }
95
96    #[inline(always)]
97    fn simd_horizontal_max(self) -> Self::Element {
98        self
99    }
100}