On Tue, 2 Jun 2026, Jakub Jelinek wrote:
> Hi!
>
> Clang has added recently type-generic versions of __builtin_bswap{16,32,64}
> and __builtin_bitreverse{8,16,32,64} builtins.
>
> The following patch adds them to GCC as well.
> Unlike the other __builtin_*g type-generic builtins, these are different in
> that the return type is not fixed (usually int or unsigned int), but is the
> same as the argument type, so these can't be handled as normal builtins
> and are handled in the C and C++ parsers instead.
>
> For consistency with the other __builtin_*g builtins, these only accept
> unsigned INTEGER_TYPE (or for C BITINT_TYPE) - users can use various ways
> to request unsigned variant of arbitrary types. For __builtin_bswapg
> additionally this requires the type to have precision which is a multiple of
> 8 (unlike clang, which for some strange reason requires multiple of 16).
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
The C front-end changes are OK (but see note below about missing test
coverage).
> --- gcc/testsuite/c-c++-common/builtin-bswapg-2.c.jj 2026-06-02
> 19:00:30.252144603 +0200
> +++ gcc/testsuite/c-c++-common/builtin-bswapg-2.c 2026-06-02
> 19:28:21.081815725 +0200
> @@ -0,0 +1,21 @@
> +/* Test __builtin_bswapg. */
> +/* { dg-do compile } */
> +/* { dg-options "" } */
> +
> +int
> +main ()
> +{
> + __builtin_bswapg (); /* { dg-error "wrong number of
> arguments to '__builtin_bswapg'" } */
> + __builtin_bswapg (1U, 1U); /* { dg-error "wrong number of arguments to
> '__builtin_bswapg'" } */
> + __builtin_bswapg (1.0); /* { dg-error "'__builtin_bswapg' operand not
> an integral type" } */
> + __builtin_bswapg (1.0f); /* { dg-error "'__builtin_bswapg' operand not
> an integral type" } */
> + __builtin_bswapg (1.0L); /* { dg-error "'__builtin_bswapg' operand not
> an integral type" } */
> + struct S { int a; };
> + __builtin_bswapg ((struct S) { 42 }); /* { dg-error
> "'__builtin_bswapg' operand not an integral type" } */
> + enum E { E0, E1 };
> + __builtin_bswapg (E1); /* { dg-error "argument 1 in call to function
> '__builtin_bswapg' has signed type" "" { target c } } */
> + /* { dg-error "argument 1 in call to function
> '__builtin_bswapg' has enumerated type" "" { target c++ } .-1 } */
The enumerated type error seems to be tested only for C++ (likewise for
__builtin_bitreverseg). Presumably it could be tested for C with a cast
to or variable of enumerated type, or by using an enumeration with fixed
underlying type.
--
Joseph S. Myers
[email protected]