Skip to content

[Feature] std::bit::leadcount() #2503

@Longor1996

Description

@Longor1996

What feature would you like to see?

Similar to popcount in that it counts set bits, but only counts the consecutive leading bits of the given integer/bitfield(?).

i.e.:

0b00000001 => 0
0b10000001 => 1
0b11000001 => 2
...
0b11111101 => 6
0b11111110 => 7
0b11111111 => 8

Perhaps std::bit::trailcount() for trailing ones could also be added?

How will this feature be useful to you and others?

Some variable-integer encodings, like the one UTF-8 is built on, use the leadcount to determine the amount of bytes to read in advance (instead of a while((byte=read()) & 0x80 != 0)-loop), which is just easier to work with... and removes a branch misprediction! ;)

Request Type

  • I can provide a PoC for this feature or am willing to work on it myself and submit a PR

Additional context?

This could be implemented on top of popcount with some bit-twiddling in a pattern function... but with how basic it is, it ought to be in the stdlib.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions