Skip to content

first_set_bit and last_set_bit for integer types #631

@Gnurou

Description

@Gnurou

Proposal

Problem statement

The standard library provides several bit-counting methods for integer types, like the leading_ones, trailing_ones, and their zeros counterparts. Two other common operations that are currently missing are the first_set_bit (ffs) and last_set_bit (fls) operations, which return the 1-based index of the least significant and most significant set bit in the value, respectively (wikipedia article).

These operations are commonly used in operating system kernels (both are used hundreds of times in the Linux kernel for instance), and the Rust for Linux project is currently implementing workarounds due to their absence.

This ACP proposes to integrate them into the standard library as they are likely to be useful in other projects as well.

Motivating examples or use cases

The ffs and fls operations are available in the BSD standard C library, the Linux kernel, and many other low-level libraries. They are hardware-supported on most popular CPUs (x86 for instance has the BSF and BSR instructions).

Looking at the Linux kernel alone, their use cases include memory allocators, schedulers, event handling, networking, audio codecs, and countless drivers.

The first user of last_set_bit in the Rust-for-Linux project will be the Nova GPU driver.

Solution sketch

These methods can be implemented on integer types similarly to e.g. leading_ones. For instance, on u32:

/// Returns the 1-based index of the first (least significant) set bit in the
/// binary representation of `self`, or `0` if no bit is set.
pub const fn first_set_bit(self) -> u32;

/// Returns the 1-based index of the last (most significant) set bit in the
/// binary representation of `self`, or `0` if no bit is set.
pub const fn last_set_bit(self) -> u32;

Implementation-wise, first_set_bit is equivalent to if self == 0 { 0 } else { self.trailing_zeros() + 1 } and last_set_bit to Self::BITS - self.leading_zeros(). Most popular CPUs have hardware support for these operations, so they can also be leveraged at the compiler level.

Alternatives

The alternative is to perform the implementation code described above in-place, which is less explicit about the intent of the code, and potentially error-prone. Also, hardware support is unlikely to be leveraged in this case, potentially leading to performance loss in critical code paths.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions