builtin_bswap plus enhancements

Ian Lance Taylor iant@google.com
Tue Aug 8 16:52:00 GMT 2006


Eric Christopher <echristo@apple.com> writes:

> Index: gcc/doc/extend.texi
> ===================================================================
> --- gcc/doc/extend.texi	(revision 115913)
> +++ gcc/doc/extend.texi	(working copy)
> @@ -6058,6 +6058,16 @@ Similar to @code{__builtin_powi}, except
>  are @code{long double}.
>  @end deftypefn
>  
> +@deftypefn {Built-in Function} int32_t __builtin_bswap32 (int32_t x)
> +Returns @var{x} with the order of the bytes reversed; for example,
> +@code{0xaabbccdd} becomes @code{0xddccbbaa}.  Byte here always means
> +exactly 8 bits.
> +@end deftypefn
> +
> +@deftypefn {Built-in Function} int64_t __builtin_bswap64 (int64_t x)
> +Similar to @code{__builtin_bswap32}, except the argument and return types
> +are 64-bit.
> +@end deftypefn

You are defining the functions to use unsigned types, so I think you
should document them to use uint32_t and uint64_t.

> +@deftypefn {Runtime Function} int32_t __bswapsi2 (int32_t @var{a})
> +@deftypefnx {Runtime Function} int64_t __bswapdi2 (int64_t @var{a})
> +These functions return the @var{a} byteswapped.
> +@end deftypefn

Here also.

> Index: gcc/testsuite/gcc.dg/builtin-bswap-1.c
> ===================================================================
> --- gcc/testsuite/gcc.dg/builtin-bswap-1.c	(revision 0)
> +++ gcc/testsuite/gcc.dg/builtin-bswap-1.c	(revision 0)
> @@ -0,0 +1,14 @@
> +/* { dg-do compile } */
> +/* { dg-options "" } */
> +/* { dg-final { scan-assembler-not "__builtin_" } } */
> +
> +#include <stdint.h>
> +
> +uint32_t foo (uint32_t a)
> +{
> +  int b;
> +
> +  b = __builtin_bswap32 (a);
> +
> +  return b;
> +}

Is this test really going to pass on a processor which does not
support a byte swap instruction?  I must be missing something.

> Index: gcc/builtin-types.def
> ===================================================================
> --- gcc/builtin-types.def	(revision 115913)
> +++ gcc/builtin-types.def	(working copy)
> @@ -74,6 +74,8 @@ DEF_PRIMITIVE_TYPE (BT_LONGLONG, long_lo
>  DEF_PRIMITIVE_TYPE (BT_ULONGLONG, long_long_unsigned_type_node)
>  DEF_PRIMITIVE_TYPE (BT_INTMAX, intmax_type_node)
>  DEF_PRIMITIVE_TYPE (BT_UINTMAX, uintmax_type_node)
> +DEF_PRIMITIVE_TYPE (BT_UINT32, uint32_type_node)
> +DEF_PRIMITIVE_TYPE (BT_UINT64, uint64_type_node)
>  DEF_PRIMITIVE_TYPE (BT_WORD, (*lang_hooks.types.type_for_mode) (word_mode, 0))
>  DEF_PRIMITIVE_TYPE (BT_FLOAT, float_type_node)
>  DEF_PRIMITIVE_TYPE (BT_DOUBLE, double_type_node)
> @@ -203,6 +205,10 @@ DEF_FUNCTION_TYPE_1 (BT_FN_DFLOAT128_DFL
>  DEF_FUNCTION_TYPE_1 (BT_FN_VOID_VPTR, BT_VOID, BT_VOLATILE_PTR)
>  DEF_FUNCTION_TYPE_1 (BT_FN_VOID_PTRPTR, BT_VOID, BT_PTR_PTR)
>  DEF_FUNCTION_TYPE_1 (BT_FN_UINT_UINT, BT_UINT, BT_UINT)
> +DEF_FUNCTION_TYPE_1 (BT_FN_ULONG_ULONG, BT_ULONG, BT_ULONG)
> +DEF_FUNCTION_TYPE_1 (BT_FN_ULONGLONG_ULONGLONG, BT_ULONGLONG, BT_ULONGLONG)
> +DEF_FUNCTION_TYPE_1 (BT_FN_UINT32_UINT32, BT_UINT32, BT_UINT32)
> +DEF_FUNCTION_TYPE_1 (BT_FN_UINT64_UINT64, BT_UINT64, BT_UINT64)

Why are you defining BT_FN_ULONG_ULONG and BT_FN_ULONGLONG_ULONGLONG
here?  If they are not needed, please remove them.

> Index: gcc/libgcc-std.ver
> ===================================================================
> --- gcc/libgcc-std.ver	(revision 115913)
> +++ gcc/libgcc-std.ver	(working copy)
> @@ -273,4 +273,6 @@ GCC_4.2.0 {
>    __floatuntixf
>    __floatuntitf
>    _Unwind_GetIPInfo
> +  __bswapsi2
> +  __bswapdi2
>  }

As you said, this should be GCC_4.3.0 assuming this is checked in
after 4.2 branches.

Patch is OK for stage 1 with those changes.

Thanks.

Ian



More information about the Gcc-patches mailing list