rust icon indicating copy to clipboard operation
rust copied to clipboard

Tracking Issue for `uint_gather_scatter_bits`

Open okaneco opened this issue 1 month ago • 2 comments

Feature gate: #![feature(uint_gather_scatter_bits)]

This is a tracking issue for two functions that operate on masked bits: gather_bits consolidates masked bits into the least significant bits of an integer, and scatter_bits distributes bits from the least significant bits of an integer to the bits specified by a mask.

The functions are implemented on unsigned integers and correspond to the hardware instructions known on some platforms as "bit extract" and "bit deposit" (PEXT/PDEP).

Public API

impl uint {
    pub const fn gather_bits(self, mut mask: Self) -> Self;
    pub const fn scatter_bits(mut self, mut mask: Self) -> Self;
}

Steps / History

  • [x] ACP: https://github.com/rust-lang/libs-team/issues/695
  • [x] Implementation: https://github.com/rust-lang/rust/pull/149097
    • https://github.com/rust-lang/rust/pull/149539
    • https://github.com/rust-lang/rust/pull/149610
  • [ ] Final comment period (FCP)^1
  • [ ] Stabilization PR

Unresolved Questions

  1. No stabilization before the existence of explicit LLVM support: As of November 2025, there is currently no LLVM intrinsic to guarantee that the code generated is optimal for the target or generates the target architecture instruction if present and the target feature is specified. This is a roadblock for stabilization.
  2. Naming: The libs team prefers gather/scatter_bits while that is at odds with how SIMD instructions use the terms gather/scatter. https://github.com/rust-lang/libs-team/issues/695#issuecomment-3549356128:
    ...by analogy to SIMD, SIMD gather/scatter allows you to arbitrarily reorder elements (it is a fully-general load/store to independently-determined addresses for each element), whereas SIMD compress/expand does not allow reordering the loaded/stored elements, just inserting/removing elements.

okaneco avatar Nov 18 '25 21:11 okaneco

@rustbot label -S-tracking-unimplemented

okaneco avatar Nov 27 '25 14:11 okaneco

Nominating this for libs-api discussion to reconsider the naming, based on @programmerjake's comment: https://github.com/rust-lang/libs-team/issues/695#issuecomment-3549356128

If "scatter" and "gather" don't have the expected meaning for folks familiar with SIMD, should we call them something else? For instance, deposit_bits and extract_bits?

joshtriplett avatar Dec 06 '25 19:12 joshtriplett

We discussed this in today's @rust-lang/libs-api meeting. We would like to rename these to deposit_bits/extract_bits, to make more sense to SIMD users.

We considered both deposit/extract and compress/expand. We mildly favored deposit/extract, on the basis that "compress" is also widely used for data compression.

(We did observe the C++ rationale for its naming of these, and still felt that we mildly preferred these names instead.)

We would also like these to have doc aliases for both the x86 and arm instructions.

We're looking forward to having the appropriate underlying intrinsics so we can consider stabilization.

joshtriplett avatar Dec 09 '25 18:12 joshtriplett

@joshtriplett Looks like there is a typo with regard to expand/extract (We would like to rename these to [...] expand_bits [...]. We mildly favored [...] extract). Which was the actual preferred name? expand to avoid alluding to compression?

niklasf avatar Dec 09 '25 20:12 niklasf

That should have been deposit_bits/extract_bits.

Amanieu avatar Dec 09 '25 21:12 Amanieu