diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/endianness.h | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/include/endianness.h b/include/endianness.h new file mode 100644 index 0000000..a34cbf4 --- /dev/null +++ b/include/endianness.h | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | #ifndef ENDIANNESS_H | ||
| 2 | #define ENDIANNESS_H | ||
| 3 | |||
| 4 | #ifndef be16toh | ||
| 5 | #if __BYTE_ORDER == __BIG_ENDIAN | ||
| 6 | #define be16toh(x) (x) | ||
| 7 | #else | ||
| 8 | #define be16toh(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8)) | ||
| 9 | #endif | ||
| 10 | #endif | ||
| 11 | |||
| 12 | #ifndef __bswap_32 | ||
| 13 | #define __bswap_32(x) ((((x) & 0xFF000000) >> 24) \ | ||
| 14 | | (((x) & 0x00FF0000) >> 8) \ | ||
| 15 | | (((x) & 0x0000FF00) << 8) \ | ||
| 16 | | (((x) & 0x000000FF) << 24)) | ||
| 17 | #endif | ||
| 18 | |||
| 19 | #ifndef be32toh | ||
| 20 | #if __BYTE_ORDER == __BIG_ENDIAN | ||
| 21 | #define be32toh(x) (x) | ||
| 22 | #else | ||
| 23 | #define be32toh(x) __bswap_32(x) | ||
| 24 | #endif | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #ifndef htobe32 | ||
| 28 | #define htobe32 be32toh | ||
| 29 | #endif | ||
| 30 | |||
| 31 | #ifndef __bswap_64 | ||
| 32 | #define __bswap_64(x) ((((x) & 0xFF00000000000000ull) >> 56) \ | ||
| 33 | | (((x) & 0x00FF000000000000ull) >> 40) \ | ||
| 34 | | (((x) & 0x0000FF0000000000ull) >> 24) \ | ||
| 35 | | (((x) & 0x000000FF00000000ull) >> 8) \ | ||
| 36 | | (((x) & 0x00000000FF000000ull) << 8) \ | ||
| 37 | | (((x) & 0x0000000000FF0000ull) << 24) \ | ||
| 38 | | (((x) & 0x000000000000FF00ull) << 40) \ | ||
| 39 | | (((x) & 0x00000000000000FFull) << 56)) | ||
| 40 | #endif | ||
| 41 | |||
| 42 | #ifndef htobe64 | ||
| 43 | #if __BYTE_ORDER == __BIG_ENDIAN | ||
| 44 | #define htobe64(x) (x) | ||
| 45 | #else | ||
| 46 | #define htobe64(x) __bswap_64(x) | ||
| 47 | #endif | ||
| 48 | #endif | ||
| 49 | |||
| 50 | #ifndef be64toh | ||
| 51 | #define be64toh htobe64 | ||
| 52 | #endif | ||
| 53 | |||
| 54 | #ifndef le64toh | ||
| 55 | #if __BYTE_ORDER == __LITTLE_ENDIAN | ||
| 56 | #define le64toh(x) (x) | ||
| 57 | #else | ||
| 58 | #define le64toh(x) __bswap_64(x) | ||
| 59 | #endif | ||
| 60 | #endif | ||
| 61 | |||
| 62 | #ifndef htole64 | ||
| 63 | #define htole64 le64toh | ||
| 64 | #endif | ||
| 65 | |||
| 66 | #endif /* ENDIANNESS_H */ | ||
