Skip to content

util or toolchain: functions for reversing bits #30010

@cfriedt

Description

@cfriedt

Is your enhancement proposal related to a problem? Please describe.
Most CPUs include instructions for bit-reversal of a word. e.g. in ARM the instruction is RBIT. This has been a feature request in GCC since 2011.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50481

Describe the solution you'd like
Just for convenience, it would be nice to have some constexpr functions for doing bit-reversal. Something like the following:

#ifdef __cplusplus
#define CONSTEXPR constexpr
#endif
CONSTEXPR static inline uint8_t bitrev4(uint8_t) {
//... simple LUT-driven implementation
}
CONSTEXPR static inline uint8_t bitrev8(uint8_t) {
//...
}
CONSTEXPR static inline uint16_t bitrev16(uint16_t) {
//...
}
CONSTEXPR static inline uint32_t bitrev32(uint32_t) {
//...
}
CONSTEXPR static inline uint64_t bitrev64(uint64_t) {
//...
}

For architectures that do not have a builtin, a really simple fallback would be to use a LUT-driven nibble-wise reversal.

Eventually, when GCC has a builtin, that could be used instead.

Describe alternatives you've considered

Additional context
Fairly useful for dealing with CRCs

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementChanges/Updates/Additions to existing features

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions