Skip to content

Implement reduce_product() - #361

Merged
Shnatsel merged 9 commits into
linebender:mainfrom
Shnatsel:reduce-product
Sep 3, 2026
Merged

Implement reduce_product()#361
Shnatsel merged 9 commits into
linebender:mainfrom
Shnatsel:reduce-product

Conversation

@Shnatsel

@Shnatsel Shnatsel commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Matches std::simd in precision (same number of roundings), but uses a more SIMD-friendly multiplication order. It uses the same order across all backends. The implementation is similar to reduce_sum().

The result is not guaranteed to be equal to v.to_slice().sum(), but that is only achievable with fully scalar multiplication; std::simd scalarizes to uphold this behavior which it doesn't even document.

Unlike #357 we can't reduce precision loss to log2(N) rather than N, at least not without complex algorithms that compute the precise sum in one rounding by separately accounting for the accumulated error. A precise variant which does that could be added later.

This is the final part of #340.

@Shnatsel Shnatsel mentioned this pull request Aug 30, 2026
22 tasks
Comment thread fearless_simd_gen/src/ops.rs Outdated
Comment thread fearless_simd_gen/src/ops.rs Outdated
Comment on lines +3632 to +3638
let mul_level_0: [i32; 2usize] = [
a[0usize].wrapping_mul(a[1usize]),
a[2usize].wrapping_mul(a[3usize]),
];
let mul_level_1: [i32; 1usize] = [mul_level_0[0usize].wrapping_mul(mul_level_0[1usize])];
mul_level_1[0]
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering whether the compiler will actually compile this just into a sequence of multiplications or whether there will be loads/stores?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all these are local stack values with very clearly no aliasing, the intermediate stores should in theory be eliminated.

The reason it's emitted like this with consecutive arrays is to make autovectorization easier on platforms we don't already support via intrinsics.

@Shnatsel
Shnatsel enabled auto-merge September 3, 2026 20:40
@Shnatsel
Shnatsel added this pull request to the merge queue Sep 3, 2026
Merged via the queue into linebender:main with commit bee8e95 Sep 3, 2026
22 checks passed
@Shnatsel
Shnatsel deleted the reduce-product branch September 3, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants