1*c9945492SAndroid Build Coastguard Worker #ifndef _ENDIAN_H 2*c9945492SAndroid Build Coastguard Worker #define _ENDIAN_H 3*c9945492SAndroid Build Coastguard Worker 4*c9945492SAndroid Build Coastguard Worker #include <features.h> 5*c9945492SAndroid Build Coastguard Worker 6*c9945492SAndroid Build Coastguard Worker #define __NEED_uint16_t 7*c9945492SAndroid Build Coastguard Worker #define __NEED_uint32_t 8*c9945492SAndroid Build Coastguard Worker #define __NEED_uint64_t 9*c9945492SAndroid Build Coastguard Worker 10*c9945492SAndroid Build Coastguard Worker #include <bits/alltypes.h> 11*c9945492SAndroid Build Coastguard Worker 12*c9945492SAndroid Build Coastguard Worker #define __PDP_ENDIAN 3412 13*c9945492SAndroid Build Coastguard Worker 14*c9945492SAndroid Build Coastguard Worker #define BIG_ENDIAN __BIG_ENDIAN 15*c9945492SAndroid Build Coastguard Worker #define LITTLE_ENDIAN __LITTLE_ENDIAN 16*c9945492SAndroid Build Coastguard Worker #define PDP_ENDIAN __PDP_ENDIAN 17*c9945492SAndroid Build Coastguard Worker #define BYTE_ORDER __BYTE_ORDER 18*c9945492SAndroid Build Coastguard Worker __bswap16(uint16_t __x)19*c9945492SAndroid Build Coastguard Workerstatic __inline uint16_t __bswap16(uint16_t __x) 20*c9945492SAndroid Build Coastguard Worker { 21*c9945492SAndroid Build Coastguard Worker return __x<<8 | __x>>8; 22*c9945492SAndroid Build Coastguard Worker } 23*c9945492SAndroid Build Coastguard Worker __bswap32(uint32_t __x)24*c9945492SAndroid Build Coastguard Workerstatic __inline uint32_t __bswap32(uint32_t __x) 25*c9945492SAndroid Build Coastguard Worker { 26*c9945492SAndroid Build Coastguard Worker return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24; 27*c9945492SAndroid Build Coastguard Worker } 28*c9945492SAndroid Build Coastguard Worker __bswap64(uint64_t __x)29*c9945492SAndroid Build Coastguard Workerstatic __inline uint64_t __bswap64(uint64_t __x) 30*c9945492SAndroid Build Coastguard Worker { 31*c9945492SAndroid Build Coastguard Worker return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32); 32*c9945492SAndroid Build Coastguard Worker } 33*c9945492SAndroid Build Coastguard Worker 34*c9945492SAndroid Build Coastguard Worker #if __BYTE_ORDER == __LITTLE_ENDIAN 35*c9945492SAndroid Build Coastguard Worker #define htobe16(x) __bswap16(x) 36*c9945492SAndroid Build Coastguard Worker #define be16toh(x) __bswap16(x) 37*c9945492SAndroid Build Coastguard Worker #define htobe32(x) __bswap32(x) 38*c9945492SAndroid Build Coastguard Worker #define be32toh(x) __bswap32(x) 39*c9945492SAndroid Build Coastguard Worker #define htobe64(x) __bswap64(x) 40*c9945492SAndroid Build Coastguard Worker #define be64toh(x) __bswap64(x) 41*c9945492SAndroid Build Coastguard Worker #define htole16(x) (uint16_t)(x) 42*c9945492SAndroid Build Coastguard Worker #define le16toh(x) (uint16_t)(x) 43*c9945492SAndroid Build Coastguard Worker #define htole32(x) (uint32_t)(x) 44*c9945492SAndroid Build Coastguard Worker #define le32toh(x) (uint32_t)(x) 45*c9945492SAndroid Build Coastguard Worker #define htole64(x) (uint64_t)(x) 46*c9945492SAndroid Build Coastguard Worker #define le64toh(x) (uint64_t)(x) 47*c9945492SAndroid Build Coastguard Worker #else 48*c9945492SAndroid Build Coastguard Worker #define htobe16(x) (uint16_t)(x) 49*c9945492SAndroid Build Coastguard Worker #define be16toh(x) (uint16_t)(x) 50*c9945492SAndroid Build Coastguard Worker #define htobe32(x) (uint32_t)(x) 51*c9945492SAndroid Build Coastguard Worker #define be32toh(x) (uint32_t)(x) 52*c9945492SAndroid Build Coastguard Worker #define htobe64(x) (uint64_t)(x) 53*c9945492SAndroid Build Coastguard Worker #define be64toh(x) (uint64_t)(x) 54*c9945492SAndroid Build Coastguard Worker #define htole16(x) __bswap16(x) 55*c9945492SAndroid Build Coastguard Worker #define le16toh(x) __bswap16(x) 56*c9945492SAndroid Build Coastguard Worker #define htole32(x) __bswap32(x) 57*c9945492SAndroid Build Coastguard Worker #define le32toh(x) __bswap32(x) 58*c9945492SAndroid Build Coastguard Worker #define htole64(x) __bswap64(x) 59*c9945492SAndroid Build Coastguard Worker #define le64toh(x) __bswap64(x) 60*c9945492SAndroid Build Coastguard Worker #endif 61*c9945492SAndroid Build Coastguard Worker 62*c9945492SAndroid Build Coastguard Worker #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) 63*c9945492SAndroid Build Coastguard Worker #if __BYTE_ORDER == __LITTLE_ENDIAN 64*c9945492SAndroid Build Coastguard Worker #define betoh16(x) __bswap16(x) 65*c9945492SAndroid Build Coastguard Worker #define betoh32(x) __bswap32(x) 66*c9945492SAndroid Build Coastguard Worker #define betoh64(x) __bswap64(x) 67*c9945492SAndroid Build Coastguard Worker #define letoh16(x) (uint16_t)(x) 68*c9945492SAndroid Build Coastguard Worker #define letoh32(x) (uint32_t)(x) 69*c9945492SAndroid Build Coastguard Worker #define letoh64(x) (uint64_t)(x) 70*c9945492SAndroid Build Coastguard Worker #else 71*c9945492SAndroid Build Coastguard Worker #define betoh16(x) (uint16_t)(x) 72*c9945492SAndroid Build Coastguard Worker #define betoh32(x) (uint32_t)(x) 73*c9945492SAndroid Build Coastguard Worker #define betoh64(x) (uint64_t)(x) 74*c9945492SAndroid Build Coastguard Worker #define letoh16(x) __bswap16(x) 75*c9945492SAndroid Build Coastguard Worker #define letoh32(x) __bswap32(x) 76*c9945492SAndroid Build Coastguard Worker #define letoh64(x) __bswap64(x) 77*c9945492SAndroid Build Coastguard Worker #endif 78*c9945492SAndroid Build Coastguard Worker #endif 79*c9945492SAndroid Build Coastguard Worker 80*c9945492SAndroid Build Coastguard Worker #endif 81