Skip to content

Commit 7642f60

Browse files
Generically implement horizontal min-max
1 parent b8d6b68 commit 7642f60

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

crates/core_simd/src/reduction.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::simd::intrinsics::{
22
simd_reduce_add_ordered, simd_reduce_and, simd_reduce_max, simd_reduce_min,
33
simd_reduce_mul_ordered, simd_reduce_or, simd_reduce_xor,
44
};
5-
use crate::simd::{LaneCount, Simd, SupportedLaneCount};
5+
use crate::simd::{LaneCount, Simd, SimdElement, SupportedLaneCount};
66

77
macro_rules! impl_integer_reductions {
88
{ $scalar:ty } => {
@@ -42,18 +42,6 @@ macro_rules! impl_integer_reductions {
4242
pub fn horizontal_xor(self) -> $scalar {
4343
unsafe { simd_reduce_xor(self) }
4444
}
45-
46-
/// Horizontal maximum. Returns the maximum lane in the vector.
47-
#[inline]
48-
pub fn horizontal_max(self) -> $scalar {
49-
unsafe { simd_reduce_max(self) }
50-
}
51-
52-
/// Horizontal minimum. Returns the minimum lane in the vector.
53-
#[inline]
54-
pub fn horizontal_min(self) -> $scalar {
55-
unsafe { simd_reduce_min(self) }
56-
}
5745
}
5846
}
5947
}
@@ -97,13 +85,22 @@ macro_rules! impl_float_reductions {
9785
unsafe { simd_reduce_mul_ordered(self, 1.) }
9886
}
9987
}
88+
}
89+
}
90+
}
91+
10092

93+
impl<T, const LANES: usize> Simd<T, LANES>
94+
where
95+
T: SimdElement + PartialOrd,
96+
LaneCount<LANES>: SupportedLaneCount,
97+
{
10198
/// Horizontal maximum. Returns the maximum lane in the vector.
10299
///
103100
/// Returns values based on equality, so a vector containing both `0.` and `-0.` may
104101
/// return either. This function will not return `NaN` unless all lanes are `NaN`.
105102
#[inline]
106-
pub fn horizontal_max(self) -> $scalar {
103+
pub fn horizontal_max(self) -> T {
107104
unsafe { simd_reduce_max(self) }
108105
}
109106

@@ -112,11 +109,9 @@ macro_rules! impl_float_reductions {
112109
/// Returns values based on equality, so a vector containing both `0.` and `-0.` may
113110
/// return either. This function will not return `NaN` unless all lanes are `NaN`.
114111
#[inline]
115-
pub fn horizontal_min(self) -> $scalar {
112+
pub fn horizontal_min(self) -> T {
116113
unsafe { simd_reduce_min(self) }
117114
}
118-
}
119-
}
120115
}
121116

122117
impl_float_reductions! { f32 }

0 commit comments

Comments
 (0)