Files
rust/tests
Jonathan Brouwer 4679857002 Rollup merge of #161209 - scottmcm:redo-npot, r=clarfonthey
Rework `next_power_of_two` to always be `1 << …`

r? @clarfonthey
Who accidentally nerd-sniped me by mentioning rust-lang/rust#161069

This obviates that PR by reworking the `(checked_)next_power_of_two` logic to calculate the necessary exponent e, then return 2ᵉ, so it's structurally obvious from the IR -- the `shl nuw 1, …` instruction -- that it's always a power of two.

As always, the reason this is tricky is because shifts don't work for `<< Self::BITS`.  The previous code handled that by checking `self <= 1` first, and thus doing a `-1 >> n` where `n < BITS`.  This code instead flips the check: it looks up-front for a value that will wrap (an input above `1 << (BITS - 1)`) and thus by excluding those cases the calculated `1 << n` always has `n < BITS`.  And the codegen tests show that, without needing any `llvm.assume`s, LLVM can take advantage of it to do things like rewriting `%` to masking.  Plus the overflow check optimizes out for constrained inputs like slice lengths.
2026-09-01 21:26:16 +02:00
..