Skip to content

Feature Request: Add cross-lane horizontal reduction methods (e.g., .reduce_sum(), .reduce_max()) to SimdBase / Simd vector types #340

Description

@Mnwa

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

  1. 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).
  2. 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions