Repository navigation
Description
Problem statement
I want to use funnel shifts, I guess?
Motivating examples or use cases
Funnel shifts are a generalization of bit shifting and bit rotation: they collapse a wide variety of bit twiddling tricks into a simple primitive.
x.rotate_left(k) = x.funnel_shl(x, k)
x.rotate_right(k) = x.funnel_shr(x, k)
x << k = x.funnel_shl(0, k)
x >> k = 0.funnel_shr(x, k)
The shift value is taken modulo T::BITS: there is no possibility of UB (or the need to panic) for shift values equal to or larger than the number of bits in the integer type.
Another way to think about this operation is that it provides a register that is twice as wide for the shifting, so long as you only need half that size of bits at the end.
We already have core::intrinsics::simd::simd_funnel_{shr, shl}, because this primitive provides a flexible way to move bits between lanes.
Using funnel shifts directly can lead to better code generation, for instance in https://godbolt.org/z/dr1b4vMcK where using funnel_shl is lowered to shld. LLVM is not able to recognize this, I suspect because of the potential for overflow in the shifts.
Solution sketch
There is a PR with an implementation at rust-lang/rust#145690.
We'd add the following intrinsics, which for the LLVM backend map directly to llvm.fshl and llvm.fshr respectively. For other backends a fallback using shifts has to be implemented. We don't currently see a way to do that generically, so custom support in each backend is needed.
#[rustc_intrinsic]
#[rustc_nounwind]
#[rustc_const_unstable(feature = "funnel_shifts", issue = "145686")]
#[unstable(feature = "funnel_shifts", issue = "145686")]
pub const fn funnel_shl<T: Copy>(a: T, b: T, shift: u32) -> T;
#[rustc_intrinsic]
#[rustc_nounwind]
#[rustc_const_unstable(feature = "funnel_shifts", issue = "145686")]
#[unstable(feature = "funnel_shifts", issue = "145686")]
pub const fn funnel_shr<T: Copy>(a: T, b: T, shift: u32) -> T;Next we'd add the funnel_shl and funnel_shr methods on the unsigned integers:
impl uN {
pub const fn funnel_shl(self, rhs: Self, n: u32) -> Self {
return intrinsics::funnel_shl(self, rhs, n);
}
pub const fn funnel_shr(self, rhs: Self, n: u32) -> Self {
return intrinsics::funnel_shr(self, rhs, n);
}
}The funnel shifts treat the input as a bag of bits. We could add the methods on signed integers as well, the signedness of the input type has no effect.
Alternatives
Do nothing, or only add the intrinsics, not the methods.
Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
Activity
kennytm commentedon Aug 22, 2025
Specify what happens when
n >= T::BITS?The LLVM intrinsic guarantees that it is equivalent to
n % T::BITS, i.e.5u32.funnel_shl(!0, 32) == 5u32. However5u32 << 32panics in debug mode.sayantn commentedon Aug 22, 2025
Also small nit, the simd intrinsics are
core::intrinsics::simd::simd_funnel_{shl,shr}folkertdev commentedon Aug 22, 2025
As far as I can tell that is mostly a historical artifact? Maybe it catches bugs, but often I really do want
unbounded_shl.I'd propose to follow the LLVM semantics and just modulo the shift amount by the number of bits. No panics, no UB.
programmerjake commentedon Aug 22, 2025
One thing to consider is what happens if/when we get generic integers: if you have
u27::funnel_shldoes it usen % 27as the shift amount? modulus can be slow for non-powers-of-2folkertdev commentedon Aug 22, 2025
Yeah, it should work like that, that is what LLVM specifies anyway.
Lots of operations are slow on generic integers. They are useful for data modelling, not for fast math, right?
programmerjake commentedon Aug 22, 2025
yeah, but they shouldn't be an order of magnitude slower if we can help it -- that said, feel free to ignore, this is far from the only case
hanna-kruppe commentedon Aug 24, 2025
In particular, the existing bit shift operators have the same
rem_euclid(T::BITS)behavior (when overflow checks are disabled or thewrappingvariant is used). The impact on non-pow2 bit widths is mitigated by the usual optimizations for div/rem by constant.Amanieu commentedon Aug 28, 2025
We discussed this in the @rust-lang/libs-api meeting. We are happy to have this general functionality, but one concern that came up was the wrapping-by-default behavior: unlike rotates, funnel shifts don't wrap cleanly when the shift amount wraps. Taking
u32as an example,funnel_shr(a, b, 31) == {b[0..31], a[31]}butfunnel_shr(a, b, 32) == a. This is almost certainly a bug since it would be reasonably to expectfunnel_shr(a, b, 32) == b.We therefore think that the default version of this should panic if the shift overflows, and there should be a separate wrapping version of this.
Please open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature. You can close this ACP once the tracking issue has been created.
tgross35 commentedon Aug 28, 2025
There is a tracking issue at rust-lang/rust#145686
programmerjake commentedon Aug 28, 2025
that needs to be edited to account for the API changes requested by libs-api.
tgross35 commentedon Aug 28, 2025
(requested at rust-lang/rust#145690 (comment))
sayantn commentedon Sep 2, 2025
As the tracking issue has been updated accordingly for the new changes, this ACP can be closed
Rollup merge of rust-lang#145690 - sayantn:integer-funnel-shift, r=tg…
Rollup merge of rust-lang#145690 - sayantn:integer-funnel-shift, r=tg…
Unrolled build for #145690
Rollup merge of #145690 - sayantn:integer-funnel-shift, r=tgross35
Rollup merge of rust-lang#145690 - sayantn:integer-funnel-shift, r=tg…