diff options
author | Robert Haas <rhaas@postgresql.org> | 2015-10-08 13:01:36 -0400 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2015-10-08 13:01:36 -0400 |
commit | c171818b275b9dbd6a1f1cf1666e9e72237f75e6 (patch) | |
tree | 6c0edd56a407532f84bc53fddd15d87cb37f665d /src | |
parent | 1e353198612586befd44fe5e87304d08ebfac552 (diff) | |
download | postgresql-c171818b275b9dbd6a1f1cf1666e9e72237f75e6.tar.gz postgresql-c171818b275b9dbd6a1f1cf1666e9e72237f75e6.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/include/pg_config.h.in | 3 | ||||
-rw-r--r-- | src/include/pg_config.h.win32 | 3 | ||||
-rw-r--r-- | src/include/port/pg_bswap.h | 46 | ||||
-rw-r--r-- | src/include/port/pg_crc32c.h | 12 |
4 files changed, 54 insertions, 10 deletions
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index dda73a8b4ce..a20e0cd1d66 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -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 diff --git a/src/include/pg_config.h.win32 b/src/include/pg_config.h.win32 index 6f7a773c5be..8566065bc77 100644 --- a/src/include/pg_config.h.win32 +++ b/src/include/pg_config.h.win32 @@ -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 */ diff --git a/src/include/port/pg_bswap.h b/src/include/port/pg_bswap.h new file mode 100644 index 00000000000..6555942c921 --- /dev/null +++ b/src/include/port/pg_bswap.h @@ -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 */ diff --git a/src/include/port/pg_crc32c.h b/src/include/port/pg_crc32c.h index c925c569014..35589c0c9be 100644 --- a/src/include/port/pg_crc32c.h +++ b/src/include/port/pg_crc32c.h @@ -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) |