Add BSWAP64 macro.

This is like BSWAP32, but for 64-bit values.  Since we've got two of
them now and they have use cases (like sortsupport) beyond CRCs, move
the definitions to their own header file.

Peter Geoghegan
This commit is contained in:
Robert Haas 2015-10-08 13:01:36 -04:00
parent 1e35319861
commit c171818b27
7 changed files with 97 additions and 10 deletions

View file

@ -214,6 +214,24 @@ fi])# PGAC_C_BUILTIN_BSWAP32
# PGAC_C_BUILTIN_BSWAP64
# -------------------------
# Check if the C compiler understands __builtin_bswap64(),
# and define HAVE__BUILTIN_BSWAP64 if so.
AC_DEFUN([PGAC_C_BUILTIN_BSWAP64],
[AC_CACHE_CHECK(for __builtin_bswap64, pgac_cv__builtin_bswap64,
[AC_COMPILE_IFELSE([AC_LANG_SOURCE(
[static unsigned long int x = __builtin_bswap64(0xaabbccddeeff0011);]
)],
[pgac_cv__builtin_bswap64=yes],
[pgac_cv__builtin_bswap64=no])])
if test x"$pgac_cv__builtin_bswap64" = xyes ; then
AC_DEFINE(HAVE__BUILTIN_BSWAP64, 1,
[Define to 1 if your compiler understands __builtin_bswap64.])
fi])# PGAC_C_BUILTIN_BSWAP64
# PGAC_C_BUILTIN_CONSTANT_P
# -------------------------
# Check if the C compiler understands __builtin_constant_p(),

24
configure vendored
View file

@ -11258,6 +11258,30 @@ if test x"$pgac_cv__builtin_bswap32" = xyes ; then
$as_echo "#define HAVE__BUILTIN_BSWAP32 1" >>confdefs.h
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for __builtin_bswap64" >&5
$as_echo_n "checking for __builtin_bswap64... " >&6; }
if ${pgac_cv__builtin_bswap64+:} false; then :
$as_echo_n "(cached) " >&6
else
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
static unsigned long int x = __builtin_bswap64(0xaabbccddeeff0011);
_ACEOF
if ac_fn_c_try_compile "$LINENO"; then :
pgac_cv__builtin_bswap64=yes
else
pgac_cv__builtin_bswap64=no
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv__builtin_bswap64" >&5
$as_echo "$pgac_cv__builtin_bswap64" >&6; }
if test x"$pgac_cv__builtin_bswap64" = xyes ; then
$as_echo "#define HAVE__BUILTIN_BSWAP64 1" >>confdefs.h
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for __builtin_constant_p" >&5
$as_echo_n "checking for __builtin_constant_p... " >&6; }

View file

@ -1317,6 +1317,7 @@ PGAC_C_FUNCNAME_SUPPORT
PGAC_C_STATIC_ASSERT
PGAC_C_TYPES_COMPATIBLE
PGAC_C_BUILTIN_BSWAP32
PGAC_C_BUILTIN_BSWAP64
PGAC_C_BUILTIN_CONSTANT_P
PGAC_C_BUILTIN_UNREACHABLE
PGAC_C_VA_ARGS

View file

@ -660,6 +660,9 @@
/* Define to 1 if your compiler understands __builtin_bswap32. */
#undef HAVE__BUILTIN_BSWAP32
/* Define to 1 if your compiler understands __builtin_bswap64. */
#undef HAVE__BUILTIN_BSWAP64
/* Define to 1 if your compiler understands __builtin_constant_p. */
#undef HAVE__BUILTIN_CONSTANT_P

View file

@ -508,6 +508,9 @@
/* Define to 1 if your compiler understands __builtin_bswap32. */
/* #undef HAVE__BUILTIN_BSWAP32 */
/* Define to 1 if your compiler understands __builtin_bswap64. */
/* #undef HAVE__BUILTIN_BSWAP64 */
/* Define to 1 if your compiler understands __builtin_constant_p. */
/* #undef HAVE__BUILTIN_CONSTANT_P */

View file

@ -0,0 +1,46 @@
/*-------------------------------------------------------------------------
*
* pg_bswap.h
* Byte swapping.
*
* Macros for reversing the byte order of 32-bit and 64-bit unsigned integers.
* For example, 0xAABBCCDD becomes 0xDDCCBBAA. These are just wrappers for
* built-in functions provided by the compiler where support exists.
*
* Note that the GCC built-in functions __builtin_bswap32() and
* __builtin_bswap64() are documented as accepting single arguments of type
* uint32_t and uint64_t respectively (these are also the respective return
* types). Use caution when using these wrapper macros with signed integers.
*
* Copyright (c) 2015, PostgreSQL Global Development Group
*
* src/include/port/pg_bswap.h
*
*-------------------------------------------------------------------------
*/
#ifndef PG_BSWAP_H
#define PG_BSWAP_H
#ifdef HAVE__BUILTIN_BSWAP32
#define BSWAP32(x) __builtin_bswap32(x)
#else
#define BSWAP32(x) (((x << 24) & 0xff000000) | \
((x << 8) & 0x00ff0000) | \
((x >> 8) & 0x0000ff00) | \
((x >> 24) & 0x000000ff))
#endif /* HAVE__BUILTIN_BSWAP32 */
#ifdef HAVE__BUILTIN_BSWAP64
#define BSWAP64(x) __builtin_bswap64(x)
#else
#define BSWAP64(x) (((x << 56) & 0xff00000000000000UL) | \
((x << 40) & 0x00ff000000000000UL) | \
((x << 24) & 0x0000ff0000000000UL) | \
((x << 8) & 0x000000ff00000000UL) | \
((x >> 8) & 0x00000000ff000000UL) | \
((x >> 24) & 0x0000000000ff0000UL) | \
((x >> 40) & 0x000000000000ff00UL) | \
((x >> 56) & 0x00000000000000ffUL))
#endif /* HAVE__BUILTIN_BSWAP64 */
#endif /* PG_BSWAP_H */

View file

@ -33,6 +33,8 @@
#ifndef PG_CRC32C_H
#define PG_CRC32C_H
#include "port/pg_bswap.h"
typedef uint32 pg_crc32c;
/* The INIT and EQ macros are the same for all implementations. */
@ -71,16 +73,6 @@ extern pg_crc32c (*pg_comp_crc32c) (pg_crc32c crc, const void *data, size_t len)
#define COMP_CRC32C(crc, data, len) \
((crc) = pg_comp_crc32c_sb8((crc), (data), (len)))
#ifdef WORDS_BIGENDIAN
#ifdef HAVE__BUILTIN_BSWAP32
#define BSWAP32(x) __builtin_bswap32(x)
#else
#define BSWAP32(x) (((x << 24) & 0xff000000) | \
((x << 8) & 0x00ff0000) | \
((x >> 8) & 0x0000ff00) | \
((x >> 24) & 0x000000ff))
#endif
#define FIN_CRC32C(crc) ((crc) = BSWAP32(crc) ^ 0xFFFFFFFF)
#else
#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)