linebender / linebender/fearless_simd

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

Open
#340 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
457
Forks
30
Avg merge
1d 10h
Merged PRs (30d)
25

Description

### 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`).

```rust
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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing SimdBase and Simd vector trait architecture, then compare the requested reductions with std::simd's reduce_sum behavior. Define how sum, min, max, any, and all should work across supported vector and mask types; done means the unified API is implemented with coverage for the relevant generic cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.