Open
Conversation
- Use unbounded_shl/unbounded_shr in the generic loop to expose the funnel shift pattern to LLVM (replacing the two-step carry trick) - Add specialized u64/u128/u256 fast paths in wrapping_shl/wrapping_shr via as_primitives! macro - u256 path uses branchless two-i128-half decomposition with mask blending, emitting shld/shrd + cmov with no branches
Merging this PR will degrade performance by 14.14%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | most_significant_bits/4096/4096 |
29 µs | 26.1 µs | +11.11% |
| ❌ | pow/128 |
64.9 µs | 75.6 µs | -14.14% |
| ⚡ | to/f32/4096 |
29.1 µs | 26.2 µs | +11.34% |
| ⚡ | to/f64/4096 |
29.2 µs | 26.3 µs | +11.06% |
| ⚡ | wrapping_shl/128 |
281.9 µs | 249.1 µs | +13.16% |
| ⚡ | wrapping_shr/128 |
292.9 µs | 249 µs | +17.62% |
Comparing DaniPopes:dani/better-shifts (441e078) with main (bff85c8)
Contributor
Author
|
only shr benches are impacted by this change, rest is noise: #573 (comment) |
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.
Optimize shift left/right for better codegen:
(x >> (64 - bits - 1)) >> 1withx.unbounded_shr(64 - bits)in the generic loop, exposing the funnel shift pattern to LLVM.wrapping_shl/wrapping_shrvia theas_primitives!macro.shld/shrd+cmovwith zero branches in the hot path.llvm-mca (Zen 4) for
U256::wrapping_shl:shl i256: 36 instructions, 37 uOps, ~15.1 cycles latency, IPC 2.39shl i256has better throughput due to fewer instructions.