|
6 | 6 | * Macros for reversing the byte order of 32-bit 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! |
9 | 10 | * |
10 | 11 | * Note that the GCC built-in functions __builtin_bswap32() and |
11 | 12 | * __builtin_bswap64() are documented as accepting single arguments of type |
|
24 | 25 | #ifdef HAVE__BUILTIN_BSWAP32 |
25 | 26 | #define BSWAP32(x) __builtin_bswap32(x) |
26 | 27 | #else |
27 | | -#define BSWAP32(x) (((x << 24) & 0xff000000) | \ |
28 | | - ((x << 8) & 0x00ff0000) | \ |
29 | | - ((x >> 8) & 0x0000ff00) | \ |
30 | | - ((x >> 24) & 0x000000ff)) |
| 28 | +#define BSWAP32(x) ((((x) << 24) & 0xff000000) | \ |
| 29 | + (((x) << 8) & 0x00ff0000) | \ |
| 30 | + (((x) >> 8) & 0x0000ff00) | \ |
| 31 | + (((x) >> 24) & 0x000000ff)) |
31 | 32 | #endif /* HAVE__BUILTIN_BSWAP32 */ |
32 | 33 |
|
33 | 34 | #ifdef HAVE__BUILTIN_BSWAP64 |
34 | 35 | #define BSWAP64(x) __builtin_bswap64(x) |
35 | 36 | #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)) |
| 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))) |
44 | 45 | #endif /* HAVE__BUILTIN_BSWAP64 */ |
45 | 46 |
|
46 | 47 | /* |
|
0 commit comments