Implement reduce_sum() - #357
Merged
Merged
Conversation
This was referenced Aug 28, 2026
LaurenzV
approved these changes
Aug 30, 2026
| match (vec_ty.scalar, vec_ty.scalar_bits) { | ||
| (ScalarType::Float, 32) => self.kernel_method(op, vec_ty, |_| { | ||
| quote! { | ||
| // _mm_hadd_ps is slower than shuffle followed by add |
Collaborator
There was a problem hiding this comment.
So why does it exist in the first place? 😂
Contributor
Author
There was a problem hiding this comment.
Looks like historical reasons to me.
It was added in ssse3 while shuffles appeared in SSE4, and it doesn't need an extra register for a shuffle mask.
github-merge-queue
Bot
removed this pull request from the merge queue due to a conflict with the base branch
Aug 30, 2026
Shnatsel
added a commit
to Shnatsel/fearless_simd
that referenced
this pull request
Aug 30, 2026
…ender#358) The simpler part of linebender#340 Implementation decisions largely mirror linebender#357 but we don't have to worry about error accumulation in floats. API design follows the existing lane-wise min/max. cc @Mnwa who requested this feature
Shnatsel
enabled auto-merge
August 30, 2026 09:39
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This implements the hardest part of #340
With floats this is very tricky. Their addition is non-trivial due to accumulating precision loss, and there are many algorithms and many precision trade-offs. See https://orlp.net/blog/taming-float-sums/ for more info.
This PR's implementation splits vectors in half and performs lane-wise addition down to 128-bit vectors, then reduces the 128-bit vector down to a single value with pairwise summation. This is a fast and precise-ish algorithm with log2(N) roundings.
I feel this is a good default because it is portable (bit-exact output everywhere, except for NaN payloads) and pretty fast. This leaves us space to add a
_relaxedvariant if higher-performance options with fewer guarantees are ever discovered, and/or a_precisevariant for Kahann summation with a single rounding instead of log2(N) roundings.We handily beat
std::simdwhich sums floats one by one left to right, which is awful for both precision and performance: it forces a long chain of scalar additions, and this is the worst case for precision too, causing N roundings for st::simd as opposed to log2(N) in this PR.Integers are not under any of the same constraints, so I tried looking for faster formulations for integers, but couldn't find anything. This reinforces my hunch that this is about as fast as this op can get, even for floats.
cc @Mnwa who requested this feature