|
| 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 */ |
0 commit comments