Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 1 | #ifndef _ENDIAN_H |
| 2 | #define _ENDIAN_H |
| 3 | |
| 4 | #define __LITTLE_ENDIAN 1234 |
| 5 | #define __BIG_ENDIAN 4321 |
| 6 | #define __PDP_ENDIAN 3412 |
| 7 | |
| 8 | #include <bits/endian.h> |
| 9 | |
| 10 | #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) |
Rich Felker | 8705a0f | 2012-04-22 11:08:01 -0400 | [diff] [blame] | 11 | |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 12 | #define BIG_ENDIAN __BIG_ENDIAN |
| 13 | #define LITTLE_ENDIAN __LITTLE_ENDIAN |
| 14 | #define PDP_ENDIAN __PDP_ENDIAN |
| 15 | #define BYTE_ORDER __BYTE_ORDER |
Rich Felker | 8705a0f | 2012-04-22 11:08:01 -0400 | [diff] [blame] | 16 | |
| 17 | #include <stdint.h> |
| 18 | |
| 19 | static inline uint16_t __bswap16(uint16_t __x) |
| 20 | { |
| 21 | return __x<<8 | __x>>8; |
| 22 | } |
| 23 | |
| 24 | static inline uint32_t __bswap32(uint32_t __x) |
| 25 | { |
| 26 | return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24; |
| 27 | } |
| 28 | |
| 29 | static inline uint64_t __bswap64(uint64_t __x) |
| 30 | { |
Rich Felker | 3f4de35 | 2012-04-22 11:19:17 -0400 | [diff] [blame^] | 31 | return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32); |
Rich Felker | 8705a0f | 2012-04-22 11:08:01 -0400 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 35 | #define htobe16(x) __bswap16(x) |
| 36 | #define be16toh(x) __bswap16(x) |
| 37 | #define betoh16(x) __bswap16(x) |
| 38 | #define htobe32(x) __bswap32(x) |
| 39 | #define be32toh(x) __bswap32(x) |
| 40 | #define betoh32(x) __bswap32(x) |
| 41 | #define htobe64(x) __bswap64(x) |
| 42 | #define be64toh(x) __bswap64(x) |
| 43 | #define betoh64(x) __bswap64(x) |
| 44 | #define htole16(x) (uint16_t)(x) |
| 45 | #define le16toh(x) (uint16_t)(x) |
| 46 | #define letoh16(x) (uint16_t)(x) |
| 47 | #define htole32(x) (uint32_t)(x) |
| 48 | #define le32toh(x) (uint32_t)(x) |
| 49 | #define letoh32(x) (uint32_t)(x) |
| 50 | #define htole64(x) (uint64_t)(x) |
| 51 | #define le64toh(x) (uint64_t)(x) |
| 52 | #define letoh64(x) (uint64_t)(x) |
| 53 | #else |
| 54 | #define htobe16(x) (uint16_t)(x) |
| 55 | #define be16toh(x) (uint16_t)(x) |
| 56 | #define betoh16(x) (uint16_t)(x) |
| 57 | #define htobe32(x) (uint32_t)(x) |
| 58 | #define be32toh(x) (uint32_t)(x) |
| 59 | #define betoh32(x) (uint32_t)(x) |
| 60 | #define htobe64(x) (uint64_t)(x) |
| 61 | #define be64toh(x) (uint64_t)(x) |
| 62 | #define betoh64(x) (uint64_t)(x) |
| 63 | #define htole16(x) __bswap16(x) |
| 64 | #define le16toh(x) __bswap16(x) |
| 65 | #define letoh16(x) __bswap16(x) |
| 66 | #define htole32(x) __bswap32(x) |
| 67 | #define le32toh(x) __bswap32(x) |
| 68 | #define letoh32(x) __bswap32(x) |
| 69 | #define htole64(x) __bswap64(x) |
| 70 | #define le64toh(x) __bswap64(x) |
| 71 | #define letoh64(x) __bswap64(x) |
| 72 | #endif |
| 73 | |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 74 | #endif |
| 75 | |
| 76 | #endif |