Feature Description
With fearless_simd solidifying its generic traits and racing toward a stable v1.0 release, it would be extremely valuable to have native, cross-lane horizontal reduction operations built directly into the crate's vector APIs.
Specifically, adding methods such as .reduce_sum(), .reduce_min(), .reduce_max(), and logical reductions like .any() / .all() on boolean masks.
Motivation & Use Case
When performing numerical algorithms (like calculating dot products, vector averages, or image processing bounds), the standard optimization pattern relies on accumulating data vertically across independent vector lanes within the loop, followed by a single horizontal reduction to a scalar at the very end.
Currently, std::simd supports this directly via vector.reduce_sum(). For fearless_simd to serve as a complete, drop-in replacement on stable Rust, users shouldn't have to break abstraction or manually write platform-specific shuffles/hadds to extract a single scalar sum from a generic vector type.
Providing this inside SimdBase or via a dedicated reduction trait would significantly improve ergonomics for generic SIMD code.
Proposed API / Design
Ideally, these operations would be exposed through the recently unified trait architecture (like SimdBase).
pub trait SimdBase {
type Element;
// ... existing items
/// Sums all lanes in the vector horizontally.
fn reduce_sum(self) -> Self::Element;
/// Finds the maximum value among all lanes.
fn reduce_max(self) -> Self::Element;
/// Finds the minimum value among all lanes.
fn reduce_min(self) -> Self::Element;
}
Alternatives Considered
- Manual Array Reinterpretation: Extracting the vector to an array via
.to_array() (or using upcoming as_array methods) and using .iter().sum(). However, this relies heavily on the compiler to optimize out memory operations, which can sometimes fail to lower cleanly to native hardware horizontal addition instructions (like HADDPS on x86 or ADDV on AArch64).
- Platform-Specific Intrinsics: Writing custom matching logic per SIMD architecture level (
Sse2, Avx2, Neon), which completely defeats the purpose of the beautiful generic abstractions fearless_simd provides.
Feature Description
With
fearless_simdsolidifying its generic traits and racing toward a stable v1.0 release, it would be extremely valuable to have native, cross-lane horizontal reduction operations built directly into the crate's vector APIs.Specifically, adding methods such as
.reduce_sum(),.reduce_min(),.reduce_max(), and logical reductions like.any()/.all()on boolean masks.Motivation & Use Case
When performing numerical algorithms (like calculating dot products, vector averages, or image processing bounds), the standard optimization pattern relies on accumulating data vertically across independent vector lanes within the loop, followed by a single horizontal reduction to a scalar at the very end.
Currently,
std::simdsupports this directly viavector.reduce_sum(). Forfearless_simdto serve as a complete, drop-in replacement on stable Rust, users shouldn't have to break abstraction or manually write platform-specific shuffles/hadds to extract a single scalar sum from a generic vector type.Providing this inside
SimdBaseor via a dedicated reduction trait would significantly improve ergonomics for generic SIMD code.Proposed API / Design
Ideally, these operations would be exposed through the recently unified trait architecture (like
SimdBase).Alternatives Considered
.to_array()(or using upcomingas_arraymethods) and using.iter().sum(). However, this relies heavily on the compiler to optimize out memory operations, which can sometimes fail to lower cleanly to native hardware horizontal addition instructions (likeHADDPSon x86 orADDVon AArch64).Sse2,Avx2,Neon), which completely defeats the purpose of the beautiful generic abstractionsfearless_simdprovides.