1/*-------------------------------------------------------------------------
6 * Macros for reversing the byte order of 16, 32 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.
10 * Note that all of these functions accept unsigned integers as arguments and
11 * return the same. Use caution when using these wrapper macros with signed
14 * Copyright (c) 2015-2025, PostgreSQL Global Development Group
16 * src/include/port/pg_bswap.h
18 *-------------------------------------------------------------------------
25 * In all supported versions msvc provides _byteswap_* functions in stdlib.h,
26 * already included by c.h.
30/* implementation of uint16 pg_bswap16(uint16) */
31#if defined(HAVE__BUILTIN_BSWAP16)
33#define pg_bswap16(x) __builtin_bswap16(x)
35#elif defined(_MSC_VER)
37#define pg_bswap16(x) _byteswap_ushort(x)
49#endif /* HAVE__BUILTIN_BSWAP16 */
52/* implementation of uint32 pg_bswap32(uint32) */
53#if defined(HAVE__BUILTIN_BSWAP32)
55#define pg_bswap32(x) __builtin_bswap32(x)
57#elif defined(_MSC_VER)
59#define pg_bswap32(x) _byteswap_ulong(x)
67 ((
x << 24) & 0xff000000) |
68 ((
x << 8) & 0x00ff0000) |
69 ((
x >> 8) & 0x0000ff00) |
70 ((
x >> 24) & 0x000000ff);
73#endif /* HAVE__BUILTIN_BSWAP32 */
76/* implementation of uint64 pg_bswap64(uint64) */
77#if defined(HAVE__BUILTIN_BSWAP64)
79#define pg_bswap64(x) __builtin_bswap64(x)
82#elif defined(_MSC_VER)
84#define pg_bswap64(x) _byteswap_uint64(x)
101#endif /* HAVE__BUILTIN_BSWAP64 */
105 * Portable and fast equivalents for ntohs, ntohl, htons, htonl,
106 * additionally extended to 64 bits.
108#ifdef WORDS_BIGENDIAN
110#define pg_hton16(x) (x)
111#define pg_hton32(x) (x)
112#define pg_hton64(x) (x)
114#define pg_ntoh16(x) (x)
115#define pg_ntoh32(x) (x)
116#define pg_ntoh64(x) (x)
120 #define pg_hton16(x) pg_bswap16(x)
121 #define pg_hton32(x) pg_bswap32(x)
122 #define pg_hton64(x) pg_bswap64(x)
124 #define pg_ntoh16(x) pg_bswap16(x)
125 #define pg_ntoh32(x) pg_bswap32(x)
126 #define pg_ntoh64(x) pg_bswap64(x)
128#endif /* WORDS_BIGENDIAN */
132 * Rearrange the bytes of a Datum from big-endian order into the native byte
133 * order. On big-endian machines, this does nothing at all.
135 * One possible application of the DatumBigEndianToNative() macro is to make
136 * bitwise comparisons cheaper. A simple 3-way comparison of Datums
137 * transformed by the macro (based on native, unsigned comparisons) will return
138 * the same result as a memcmp() of the corresponding original Datums, but can
139 * be much cheaper. It's generally safe to do this on big-endian systems
140 * without any special transformation occurring first.
142#ifdef WORDS_BIGENDIAN
143#define DatumBigEndianToNative(x) (x)
144#else /* !WORDS_BIGENDIAN */
145 #define DatumBigEndianToNative(x) UInt64GetDatum(pg_bswap64(DatumGetUInt64(x)))
146#endif /* WORDS_BIGENDIAN */
148#endif /* PG_BSWAP_H */
static uint16 pg_bswap16(uint16 x)
static uint64 pg_bswap64(uint64 x)
static uint32 pg_bswap32(uint32 x)