Skip to content

Commit c171818

Browse files
committed
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
1 parent 1e35319 commit c171818

File tree

7 files changed

+97
-10
lines changed

7 files changed

+97
-10
lines changed

config/c-compiler.m4

+18
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ fi])# PGAC_C_BUILTIN_BSWAP32
214214

215215

216216

217+
# PGAC_C_BUILTIN_BSWAP64
218+
# -------------------------
219+
# Check if the C compiler understands __builtin_bswap64(),
220+
# and define HAVE__BUILTIN_BSWAP64 if so.
221+
AC_DEFUN([PGAC_C_BUILTIN_BSWAP64],
222+
[AC_CACHE_CHECK(for __builtin_bswap64, pgac_cv__builtin_bswap64,
223+
[AC_COMPILE_IFELSE([AC_LANG_SOURCE(
224+
[static unsigned long int x = __builtin_bswap64(0xaabbccddeeff0011);]
225+
)],
226+
[pgac_cv__builtin_bswap64=yes],
227+
[pgac_cv__builtin_bswap64=no])])
228+
if test x"$pgac_cv__builtin_bswap64" = xyes ; then
229+
AC_DEFINE(HAVE__BUILTIN_BSWAP64, 1,
230+
[Define to 1 if your compiler understands __builtin_bswap64.])
231+
fi])# PGAC_C_BUILTIN_BSWAP64
232+
233+
234+
217235
# PGAC_C_BUILTIN_CONSTANT_P
218236
# -------------------------
219237
# Check if the C compiler understands __builtin_constant_p(),

configure

+24
Original file line numberDiff line numberDiff line change
@@ -11258,6 +11258,30 @@ if test x"$pgac_cv__builtin_bswap32" = xyes ; then
1125811258

1125911259
$as_echo "#define HAVE__BUILTIN_BSWAP32 1" >>confdefs.h
1126011260

11261+
fi
11262+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for __builtin_bswap64" >&5
11263+
$as_echo_n "checking for __builtin_bswap64... " >&6; }
11264+
if ${pgac_cv__builtin_bswap64+:} false; then :
11265+
$as_echo_n "(cached) " >&6
11266+
else
11267+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
11268+
/* end confdefs.h. */
11269+
static unsigned long int x = __builtin_bswap64(0xaabbccddeeff0011);
11270+
11271+
_ACEOF
11272+
if ac_fn_c_try_compile "$LINENO"; then :
11273+
pgac_cv__builtin_bswap64=yes
11274+
else
11275+
pgac_cv__builtin_bswap64=no
11276+
fi
11277+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
11278+
fi
11279+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv__builtin_bswap64" >&5
11280+
$as_echo "$pgac_cv__builtin_bswap64" >&6; }
11281+
if test x"$pgac_cv__builtin_bswap64" = xyes ; then
11282+
11283+
$as_echo "#define HAVE__BUILTIN_BSWAP64 1" >>confdefs.h
11284+
1126111285
fi
1126211286
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for __builtin_constant_p" >&5
1126311287
$as_echo_n "checking for __builtin_constant_p... " >&6; }

configure.in

+1
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,7 @@ PGAC_C_FUNCNAME_SUPPORT
13171317
PGAC_C_STATIC_ASSERT
13181318
PGAC_C_TYPES_COMPATIBLE
13191319
PGAC_C_BUILTIN_BSWAP32
1320+
PGAC_C_BUILTIN_BSWAP64
13201321
PGAC_C_BUILTIN_CONSTANT_P
13211322
PGAC_C_BUILTIN_UNREACHABLE
13221323
PGAC_C_VA_ARGS

src/include/pg_config.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,9 @@
660660
/* Define to 1 if your compiler understands __builtin_bswap32. */
661661
#undef HAVE__BUILTIN_BSWAP32
662662

663+
/* Define to 1 if your compiler understands __builtin_bswap64. */
664+
#undef HAVE__BUILTIN_BSWAP64
665+
663666
/* Define to 1 if your compiler understands __builtin_constant_p. */
664667
#undef HAVE__BUILTIN_CONSTANT_P
665668

src/include/pg_config.h.win32

+3
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@
508508
/* Define to 1 if your compiler understands __builtin_bswap32. */
509509
/* #undef HAVE__BUILTIN_BSWAP32 */
510510

511+
/* Define to 1 if your compiler understands __builtin_bswap64. */
512+
/* #undef HAVE__BUILTIN_BSWAP64 */
513+
511514
/* Define to 1 if your compiler understands __builtin_constant_p. */
512515
/* #undef HAVE__BUILTIN_CONSTANT_P */
513516

src/include/port/pg_bswap.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* pg_bswap.h
4+
* Byte swapping.
5+
*
6+
* Macros for reversing the byte order of 32-bit and 64-bit unsigned integers.
7+
* For example, 0xAABBCCDD becomes 0xDDCCBBAA. These are just wrappers for
8+
* built-in functions provided by the compiler where support exists.
9+
*
10+
* Note that the GCC built-in functions __builtin_bswap32() and
11+
* __builtin_bswap64() are documented as accepting single arguments of type
12+
* uint32_t and uint64_t respectively (these are also the respective return
13+
* types). Use caution when using these wrapper macros with signed integers.
14+
*
15+
* Copyright (c) 2015, PostgreSQL Global Development Group
16+
*
17+
* src/include/port/pg_bswap.h
18+
*
19+
*-------------------------------------------------------------------------
20+
*/
21+
#ifndef PG_BSWAP_H
22+
#define PG_BSWAP_H
23+
24+
#ifdef HAVE__BUILTIN_BSWAP32
25+
#define BSWAP32(x) __builtin_bswap32(x)
26+
#else
27+
#define BSWAP32(x) (((x << 24) & 0xff000000) | \
28+
((x << 8) & 0x00ff0000) | \
29+
((x >> 8) & 0x0000ff00) | \
30+
((x >> 24) & 0x000000ff))
31+
#endif /* HAVE__BUILTIN_BSWAP32 */
32+
33+
#ifdef HAVE__BUILTIN_BSWAP64
34+
#define BSWAP64(x) __builtin_bswap64(x)
35+
#else
36+
#define BSWAP64(x) (((x << 56) & 0xff00000000000000UL) | \
37+
((x << 40) & 0x00ff000000000000UL) | \
38+
((x << 24) & 0x0000ff0000000000UL) | \
39+
((x << 8) & 0x000000ff00000000UL) | \
40+
((x >> 8) & 0x00000000ff000000UL) | \
41+
((x >> 24) & 0x0000000000ff0000UL) | \
42+
((x >> 40) & 0x000000000000ff00UL) | \
43+
((x >> 56) & 0x00000000000000ffUL))
44+
#endif /* HAVE__BUILTIN_BSWAP64 */
45+
46+
#endif /* PG_BSWAP_H */

src/include/port/pg_crc32c.h

+2-10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#ifndef PG_CRC32C_H
3434
#define PG_CRC32C_H
3535

36+
#include "port/pg_bswap.h"
37+
3638
typedef uint32 pg_crc32c;
3739

3840
/* 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)
7173
#define COMP_CRC32C(crc, data, len) \
7274
((crc) = pg_comp_crc32c_sb8((crc), (data), (len)))
7375
#ifdef WORDS_BIGENDIAN
74-
75-
#ifdef HAVE__BUILTIN_BSWAP32
76-
#define BSWAP32(x) __builtin_bswap32(x)
77-
#else
78-
#define BSWAP32(x) (((x << 24) & 0xff000000) | \
79-
((x << 8) & 0x00ff0000) | \
80-
((x >> 8) & 0x0000ff00) | \
81-
((x >> 24) & 0x000000ff))
82-
#endif
83-
8476
#define FIN_CRC32C(crc) ((crc) = BSWAP32(crc) ^ 0xFFFFFFFF)
8577
#else
8678
#define FIN_CRC32C(crc) ((crc) ^= 0xFFFFFFFF)

0 commit comments

Comments
 (0)