Skip to content

ACP: add funnel shifts #642

Closed
Closed

Description

@folkertdev

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

  1. added
    api-change-proposal
    on Aug 22, 2025
  2. kennytm commented on Aug 22, 2025

    @kennytm
    Member

    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. However 5u32 << 32 panics in debug mode.

  3. sayantn commented on Aug 22, 2025

    @sayantn

    Also small nit, the simd intrinsics are core::intrinsics::simd::simd_funnel_{shl,shr}

  4. folkertdev commented on Aug 22, 2025

    @folkertdev
    Author

    However 5u32 << 32 panics in debug mode.

    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.

  5. programmerjake commented on Aug 22, 2025

    @programmerjake
    Member

    One thing to consider is what happens if/when we get generic integers: if you have u27::funnel_shl does it use n % 27 as the shift amount? modulus can be slow for non-powers-of-2

  6. folkertdev commented on Aug 22, 2025

    @folkertdev
    Author

    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?

  7. programmerjake commented on Aug 22, 2025

    @programmerjake
    Member

    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

  8. hanna-kruppe commented on Aug 24, 2025

    @hanna-kruppe

    In particular, the existing bit shift operators have the same rem_euclid(T::BITS) behavior (when overflow checks are disabled or the wrapping variant is used). The impact on non-pow2 bit widths is mitigated by the usual optimizations for div/rem by constant.

  9. Amanieu commented on Aug 28, 2025

    @Amanieu
    Member

    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 u32 as an example, funnel_shr(a, b, 31) == {b[0..31], a[31]} but funnel_shr(a, b, 32) == a. This is almost certainly a bug since it would be reasonably to expect funnel_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.

  10. added
    ACP-accepted
    on Aug 28, 2025
  11. tgross35 commented on Aug 28, 2025

    @tgross35
    Member

    There is a tracking issue at rust-lang/rust#145686

  12. programmerjake commented on Aug 28, 2025

    @programmerjake
    Member

    There is a tracking issue at rust-lang/rust#145686

    that needs to be edited to account for the API changes requested by libs-api.

  13. tgross35 commented on Aug 28, 2025

    @tgross35
    Member
  14. sayantn commented on Sep 2, 2025

    @sayantn

    As the tracking issue has been updated accordingly for the new changes, this ACP can be closed

  15. added a commit that references this issue on Sep 4, 2025
  16. added a commit that references this issue on Sep 4, 2025
  17. added a commit that references this issue on Sep 4, 2025
  18. added a commit that references this issue on Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedapi-change-proposal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions