Skip to content

Optimized implementation for uN::{gather,scatter}_bits - #149663

Merged
bors merged 2 commits into
rust-lang:mainfrom
quaternic:gather-scatter-bits-opt
Dec 29, 2025
Merged

Optimized implementation for uN::{gather,scatter}_bits#149663
bors merged 2 commits into
rust-lang:mainfrom
quaternic:gather-scatter-bits-opt

Conversation

@quaternic

@quaternicquaternic commented Dec 4, 2025

Copy link
Copy Markdown
Contributor

Feature gate: #![feature(uint_gather_scatter_bits)]
Tracking issue: #149069
Accepted ACP: rust-lang/libs-team#695

Implements the methods using the parallel suffix strategy mentioned in the ACP discussion. The referenced source material provides C implementations, though this PR makes improvements over those, cutting the instruction count by a third:
https://rust.godbolt.org/z/rn5naYnK4 (this PR)
https://c.godbolt.org/z/WzYd5WbsY (Hacker's delight)

This was initially based on the code for gather_bits that @okaneco provided in rust-lang/libs-team#695 (comment) . I wanted to understand how it worked, and later on noticed some opportunities for improvement, which eventually led to this PR.

@rustbotrustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 4, 2025
@rust-log-analyzer

This comment has been minimized.

@quaternic
quaternicforce-pushed the gather-scatter-bits-opt branch from 9549004 to 2c752afCompareDecember 4, 2025 23:05
@rust-log-analyzer

This comment has been minimized.

@quaternic
quaternicforce-pushed the gather-scatter-bits-opt branch from 2c752af to ac7e6c6CompareDecember 4, 2025 23:49
@quaternic

quaternic commented Dec 5, 2025

Copy link
Copy Markdown
ContributorAuthor

Do take this with a grain of salt, since I authored the benchmarks with this in mind. In particular, since the new implementation doesn't have any input-dependent control-flow, it is easily vectorized which all of these benchmarks allow for.

Benchmarked locally on an Intel Core i7 920 @ 2.67GHz (from ~2009)

old (ns/iter)new (ns/iter)old/new
num::int_bits::u8::constant::gather_bits1341930.69
num::int_bits::u8::constant::scatter_bits1331430.93
num::int_bits::u8::invariant::gather_bits872319345.11
num::int_bits::u8::invariant::scatter_bits1082718757.85
num::int_bits::u8::variable::gather_bits1793873424.43
num::int_bits::u8::variable::scatter_bits1980983823.64
num::int_bits::u16::constant::gather_bits2782900.96
num::int_bits::u16::constant::scatter_bits2791981.41
num::int_bits::u16::invariant::gather_bits940123140.66
num::int_bits::u16::invariant::scatter_bits948023340.71
num::int_bits::u16::variable::gather_bits1639893217.59
num::int_bits::u16::variable::scatter_bits14793108313.66
num::int_bits::u32::constant::gather_bits5283731.42
num::int_bits::u32::constant::scatter_bits5203011.73
num::int_bits::u32::invariant::gather_bits769928427.11
num::int_bits::u32::invariant::scatter_bits667029522.6
num::int_bits::u32::variable::gather_bits999313947.17
num::int_bits::u32::variable::scatter_bits905116205.59
num::int_bits::u64::constant::gather_bits10083872.6
num::int_bits::u64::constant::scatter_bits10153772.69
num::int_bits::u64::invariant::gather_bits789234722.78
num::int_bits::u64::invariant::scatter_bits673036218.58
num::int_bits::u64::variable::gather_bits893019304.63
num::int_bits::u64::variable::scatter_bits798122383.57
num::int_bits::u128::constant::gather_bits1569072121.77
num::int_bits::u128::constant::scatter_bits1137465517.35
num::int_bits::u128::invariant::gather_bits1654285619.33
num::int_bits::u128::invariant::scatter_bits1331686415.4
num::int_bits::u128::variable::gather_bits1698444033.86
num::int_bits::u128::variable::scatter_bits1372146742.94

@quaternic
quaternic marked this pull request as ready for review December 5, 2025 11:08
@rustbotrustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 5, 2025
@rustbotrustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Dec 5, 2025
@rustbot

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@okanecookaneco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice work.

I noticed spot-checking some random constant masks that this implementation and the HD implementation flip-flop on which has more instructions, but it's only a small difference count. On simpler masks, they seem to optimize similarly.

The dynamic mask output is a drastic improvement with this by a quarter/third reduction of instructions for gather and scatter.

alive2 showing that this implementation and the current implementation appear to be equivalent functions
gather - https://alive2.llvm.org/ce/z/wKxY2Z
scatter - https://alive2.llvm.org/ce/z/_98DbY
scratchpad for the LLVM IR output - https://rust.godbolt.org/z/brbePx44T

View changes since this review

Comment threadlibrary/core/src/num/int_bits.rs
Comment threadlibrary/core/src/num/int_bits.rs
@Mark-Simulacrum

Copy link
Copy Markdown
Member

@bors r+ rollup

@bors

bors commented Dec 28, 2025

Copy link
Copy Markdown
Collaborator

📌 Commit 79d792f has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@borsbors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 28, 2025
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Dec 28, 2025
… r=Mark-Simulacrum
Optimized implementation for uN::{gather,scatter}_bits
Feature gate: #![feature(uint_gather_scatter_bits)]
Tracking issue: rust-lang#149069
Accepted ACP: rust-lang/libs-team#695
Implements the methods using the parallel suffix strategy mentioned in the ACP discussion. The referenced source material provides C implementations, though this PR makes improvements over those, cutting the instruction count by a third:
https://rust.godbolt.org/z/rn5naYnK4 (this PR)
https://c.godbolt.org/z/WzYd5WbsY (Hacker's delight)
This was initially based on the code for `gather_bits` that `@okaneco` provided in rust-lang/libs-team#695 (comment) . I wanted to understand how it worked, and later on noticed some opportunities for improvement, which eventually led to this PR.
This was referenced Dec 28, 2025
bors added a commit that referenced this pull request Dec 28, 2025
…uwer
Rollup of 8 pull requests
Successful merges:
- #148321 (parser/lexer: bump to Unicode 17, use faster unicode-ident)
- #149540 (std: sys: fs: uefi: Implement readdir)
- #149582 (Implement `Duration::div_duration_{floor,ceil}`)
- #149663 (Optimized implementation for uN::{gather,scatter}_bits)
- #149667 (Fix ICE by rejecting const blocks in patterns during AST lowering (closes#148138))
- #149947 (add several older crashtests)
- #150011 (Add more `unbounded_sh[lr]` examples)
- #150411 (refactor `destructure_const`)
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit 8bd4d04 into rust-lang:mainDec 29, 2025
11 checks passed
@rustbotrustbot added this to the 1.94.0 milestone Dec 29, 2025
rust-timer added a commit that referenced this pull request Dec 29, 2025
Rollup merge of #149663 - quaternic:gather-scatter-bits-opt, r=Mark-Simulacrum
Optimized implementation for uN::{gather,scatter}_bits
Feature gate: #![feature(uint_gather_scatter_bits)]
Tracking issue: #149069
Accepted ACP: rust-lang/libs-team#695
Implements the methods using the parallel suffix strategy mentioned in the ACP discussion. The referenced source material provides C implementations, though this PR makes improvements over those, cutting the instruction count by a third:
https://rust.godbolt.org/z/rn5naYnK4 (this PR)
https://c.godbolt.org/z/WzYd5WbsY (Hacker's delight)
This was initially based on the code for `gather_bits` that ``@okaneco`` provided in rust-lang/libs-team#695 (comment) . I wanted to understand how it worked, and later on noticed some opportunities for improvement, which eventually led to this PR.
@quaternic
quaternic deleted the gather-scatter-bits-opt branch January 20, 2026 22:51
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Aug 17, 2026
…uwer
Rollup of 8 pull requests
Successful merges:
- rust-lang/rust#148321 (parser/lexer: bump to Unicode 17, use faster unicode-ident)
- rust-lang/rust#149540 (std: sys: fs: uefi: Implement readdir)
- rust-lang/rust#149582 (Implement `Duration::div_duration_{floor,ceil}`)
- rust-lang/rust#149663 (Optimized implementation for uN::{gather,scatter}_bits)
- rust-lang/rust#149667 (Fix ICE by rejecting const blocks in patterns during AST lowering (closesrust-lang/rust#148138))
- rust-lang/rust#149947 (add several older crashtests)
- rust-lang/rust#150011 (Add more `unbounded_sh[lr]` examples)
- rust-lang/rust#150411 (refactor `destructure_const`)
r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@quaternic@rust-log-analyzer@rustbot@Mark-Simulacrum@bors@okaneco