|
3 | 3 | * pg_bswap.h
|
4 | 4 | * Byte swapping.
|
5 | 5 | *
|
6 |
| - * Macros for reversing the byte order of 32-bit and 64-bit unsigned integers. |
| 6 | + * Macros for reversing the byte order of 16, 32 and 64-bit unsigned integers. |
7 | 7 | * For example, 0xAABBCCDD becomes 0xDDCCBBAA. These are just wrappers for
|
8 | 8 | * built-in functions provided by the compiler where support exists.
|
9 |
| - * Elsewhere, beware of multiple evaluations of the arguments! |
10 | 9 | *
|
11 |
| - * Note that the GCC built-in functions __builtin_bswap32() and |
12 |
| - * __builtin_bswap64() are documented as accepting single arguments of type |
13 |
| - * uint32_t and uint64_t respectively (these are also the respective return |
14 |
| - * types). Use caution when using these wrapper macros with signed integers. |
| 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 |
| 12 | + * integers. |
15 | 13 | *
|
16 | 14 | * Copyright (c) 2015-2017, PostgreSQL Global Development Group
|
17 | 15 | *
|
|
22 | 20 | #ifndef PG_BSWAP_H
|
23 | 21 | #define PG_BSWAP_H
|
24 | 22 |
|
25 |
| -#ifdef HAVE__BUILTIN_BSWAP32 |
26 |
| -#define BSWAP32(x) __builtin_bswap32(x) |
| 23 | + |
| 24 | +/* In all supported versions msvc provides _byteswap_* functions in stdlib.h */ |
| 25 | +#ifdef _MSC_VER |
| 26 | +#include <stdlib.h> |
| 27 | +#endif |
| 28 | + |
| 29 | + |
| 30 | +/* implementation of uint16 pg_bswap16(uint16) */ |
| 31 | +#if defined(HAVE__BUILTIN_BSWAP16) |
| 32 | + |
| 33 | +#define pg_bswap16(x) __builtin_bswap16(x) |
| 34 | + |
| 35 | +#elif defined(_MSC_VER) |
| 36 | + |
| 37 | +#define pg_bswap16(x) _byteswap_ushort(x) |
| 38 | + |
| 39 | +#else |
| 40 | + |
| 41 | +static inline uint16 |
| 42 | +pg_bswap16(uint16 x) |
| 43 | +{ |
| 44 | + return |
| 45 | + ((x << 8) & 0xff00) | |
| 46 | + ((x >> 8) & 0x00ff); |
| 47 | +} |
| 48 | + |
| 49 | +#endif /* HAVE__BUILTIN_BSWAP16 */ |
| 50 | + |
| 51 | + |
| 52 | +/* implementation of uint32 pg_bswap32(uint32) */ |
| 53 | +#if defined(HAVE__BUILTIN_BSWAP32) |
| 54 | + |
| 55 | +#define pg_bswap32(x) __builtin_bswap32(x) |
| 56 | + |
| 57 | +#elif defined(_MSC_VER) |
| 58 | + |
| 59 | +#define pg_bswap32(x) _byteswap_ulong(x) |
| 60 | + |
27 | 61 | #else
|
28 |
| -#define BSWAP32(x) ((((x) << 24) & 0xff000000) | \ |
29 |
| - (((x) << 8) & 0x00ff0000) | \ |
30 |
| - (((x) >> 8) & 0x0000ff00) | \ |
31 |
| - (((x) >> 24) & 0x000000ff)) |
| 62 | + |
| 63 | +static inline uint32 |
| 64 | +pg_bswap32(uint32 x) |
| 65 | +{ |
| 66 | + return |
| 67 | + ((x << 24) & 0xff000000) | |
| 68 | + ((x << 8) & 0x00ff0000) | |
| 69 | + ((x >> 8) & 0x0000ff00) | |
| 70 | + ((x >> 24) & 0x000000ff); |
| 71 | +} |
| 72 | + |
32 | 73 | #endif /* HAVE__BUILTIN_BSWAP32 */
|
33 | 74 |
|
34 |
| -#ifdef HAVE__BUILTIN_BSWAP64 |
35 |
| -#define BSWAP64(x) __builtin_bswap64(x) |
| 75 | + |
| 76 | +/* implementation of uint64 pg_bswap64(uint64) */ |
| 77 | +#if defined(HAVE__BUILTIN_BSWAP64) |
| 78 | + |
| 79 | +#define pg_bswap64(x) __builtin_bswap64(x) |
| 80 | + |
| 81 | + |
| 82 | +#elif defined(_MSC_VER) |
| 83 | + |
| 84 | +#define pg_bswap64(x) _byteswap_uint64(x) |
| 85 | + |
36 | 86 | #else
|
37 |
| -#define BSWAP64(x) ((((x) << 56) & UINT64CONST(0xff00000000000000)) | \ |
38 |
| - (((x) << 40) & UINT64CONST(0x00ff000000000000)) | \ |
39 |
| - (((x) << 24) & UINT64CONST(0x0000ff0000000000)) | \ |
40 |
| - (((x) << 8) & UINT64CONST(0x000000ff00000000)) | \ |
41 |
| - (((x) >> 8) & UINT64CONST(0x00000000ff000000)) | \ |
42 |
| - (((x) >> 24) & UINT64CONST(0x0000000000ff0000)) | \ |
43 |
| - (((x) >> 40) & UINT64CONST(0x000000000000ff00)) | \ |
44 |
| - (((x) >> 56) & UINT64CONST(0x00000000000000ff))) |
| 87 | + |
| 88 | +static inline uint16 |
| 89 | +pg_bswap64(uint16 x) |
| 90 | +{ |
| 91 | + return |
| 92 | + ((x << 56) & UINT64CONST(0xff00000000000000)) | |
| 93 | + ((x << 40) & UINT64CONST(0x00ff000000000000)) | |
| 94 | + ((x << 24) & UINT64CONST(0x0000ff0000000000)) | |
| 95 | + ((x << 8) & UINT64CONST(0x000000ff00000000)) | |
| 96 | + ((x >> 8) & UINT64CONST(0x00000000ff000000)) | |
| 97 | + ((x >> 24) & UINT64CONST(0x0000000000ff0000)) | |
| 98 | + ((x >> 40) & UINT64CONST(0x000000000000ff00)) | |
| 99 | + ((x >> 56) & UINT64CONST(0x00000000000000ff)); |
| 100 | +} |
45 | 101 | #endif /* HAVE__BUILTIN_BSWAP64 */
|
46 | 102 |
|
| 103 | + |
| 104 | +/* |
| 105 | + * Portable and fast equivalents for for ntohs, ntohl, htons, htonl, |
| 106 | + * additionally extended to 64 bits. |
| 107 | + */ |
| 108 | +#ifdef WORDS_BIGENDIAN |
| 109 | + |
| 110 | +#define pg_hton16(x) (x) |
| 111 | +#define pg_hton32(x) (x) |
| 112 | +#define pg_hton64(x) (x) |
| 113 | + |
| 114 | +#define pg_ntoh16(x) (x) |
| 115 | +#define pg_ntoh32(x) (x) |
| 116 | +#define pg_ntoh64(x) (x) |
| 117 | + |
| 118 | +#else |
| 119 | + |
| 120 | +#define pg_hton16(x) pg_bswap16(x) |
| 121 | +#define pg_hton32(x) pg_bswap32(x) |
| 122 | +#define pg_hton64(x) pg_bswap64(x) |
| 123 | + |
| 124 | +#define pg_ntoh16(x) pg_bswap16(x) |
| 125 | +#define pg_ntoh32(x) pg_bswap32(x) |
| 126 | +#define pg_ntoh64(x) pg_bswap64(x) |
| 127 | + |
| 128 | +#endif /* WORDS_BIGENDIAN */ |
| 129 | + |
| 130 | + |
47 | 131 | /*
|
48 | 132 | * Rearrange the bytes of a Datum from big-endian order into the native byte
|
49 | 133 | * order. On big-endian machines, this does nothing at all. Note that the C
|
|
60 | 144 | #define DatumBigEndianToNative(x) (x)
|
61 | 145 | #else /* !WORDS_BIGENDIAN */
|
62 | 146 | #if SIZEOF_DATUM == 8
|
63 |
| -#define DatumBigEndianToNative(x) BSWAP64(x) |
| 147 | +#define DatumBigEndianToNative(x) pg_bswap64(x) |
64 | 148 | #else /* SIZEOF_DATUM != 8 */
|
65 |
| -#define DatumBigEndianToNative(x) BSWAP32(x) |
| 149 | +#define DatumBigEndianToNative(x) pg_bswap32(x) |
66 | 150 | #endif /* SIZEOF_DATUM == 8 */
|
67 | 151 | #endif /* WORDS_BIGENDIAN */
|
68 | 152 |
|
|
0 commit comments