GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gcc

C and C++ now support `__builtin_bswapg` and `__builtin_bitreverseg` for generic types

GCC now provides type-generic built-in functions `__builtin_bswapg` and `__builtin_bitreverseg` for byte-swapping and bit-reversal, similar to Clang's offering…

GCC now introduces __builtin_bswapg and __builtin_bitreverseg, new type-generic built-in functions for byte-swapping and bit-reversal operations, respectively. These built-ins address PR c/122731 and align GCC’s functionality with Clang’s, promoting better cross-compiler compatibility. Unlike other type-generic built-ins, these return the same type as their argument and specifically require unsigned integer or _BitInt types with precision divisible by 8 for __builtin_bswapg.

In Details

This commit introduces __builtin_bswapg and __builtin_bitreverseg as type-generic built-ins within the C and C++ frontends, addressing PR c/122731. These built-ins differ from standard __builtin_*g functions, as their return type matches the argument type, necessitating special handling within the C and C++ parsers rather than the generic built-in expansion mechanism. The implementation involves new DEF_INTERNAL_INTSZ_FN and DEF_INTERNAL_INTSZ_EXT_FN definitions, corresponding expand_BSWAP and expand_BITREVERSE internal functions, and modifications to `fold_build_builtin_bswap…

For Context

Programmers often need to perform operations like 'byte-swapping' (reversing the order of bytes in a number) or 'bit-reversal' (reversing the order of individual bits). These are common in networking, low-level programming, or when converting data between systems. Compilers like GCC provide "built-in" functions to do these very efficiently. This update adds two new, more flexible built-in functions, __builtin_bswapg and __builtin_bitreverseg, to GCC. The 'g' stands for 'generic,' meaning they can work with various integer types, including the specialized _BitInt type (for arbitrary-precision integers) and standard unsigned integers. This makes it easier for developers to write portable code that works across different compilers and platforms, as these functions are similar to those already found in other compilers like Clang. For __builtin_bswapg, the size of the number must be a multiple of 8 bits.

Filed Under: cc++builtinstype-genericbit-manipulation