1*1f5207b7SJohn Levon #ifndef _COMPAT_BSWAP_H_ 2*1f5207b7SJohn Levon #define _COMPAT_BSWAP_H_ 3*1f5207b7SJohn Levon 4*1f5207b7SJohn Levon #if defined(__GNUC__) 5*1f5207b7SJohn Levon #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) 6*1f5207b7SJohn Levon #define __HAS_BUILTIN_BSWAP16 7*1f5207b7SJohn Levon #endif 8*1f5207b7SJohn Levon #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 4)) 9*1f5207b7SJohn Levon #define __HAS_BUILTIN_BSWAP32 10*1f5207b7SJohn Levon #define __HAS_BUILTIN_BSWAP64 11*1f5207b7SJohn Levon #endif 12*1f5207b7SJohn Levon #endif 13*1f5207b7SJohn Levon 14*1f5207b7SJohn Levon #if defined(__clang__) 15*1f5207b7SJohn Levon #if (__clang_major__ > 3) || ((__clang_major__ == 3) && (__clang_minor__ >= 2)) 16*1f5207b7SJohn Levon #define __HAS_BUILTIN_BSWAP16 17*1f5207b7SJohn Levon #endif 18*1f5207b7SJohn Levon #if (__clang_major__ > 3) || ((__clang_major__ == 3) && (__clang_minor__ >= 0)) 19*1f5207b7SJohn Levon #define __HAS_BUILTIN_BSWAP32 20*1f5207b7SJohn Levon #define __HAS_BUILTIN_BSWAP64 21*1f5207b7SJohn Levon #endif 22*1f5207b7SJohn Levon #endif 23*1f5207b7SJohn Levon 24*1f5207b7SJohn Levon #ifdef __HAS_BUILTIN_BSWAP16 25*1f5207b7SJohn Levon #define bswap16(x) __builtin_bswap16(x) 26*1f5207b7SJohn Levon #else 27*1f5207b7SJohn Levon #include <stdint.h> bswap16(uint16_t x)28*1f5207b7SJohn Levonstatic inline uint16_t bswap16(uint16_t x) 29*1f5207b7SJohn Levon { 30*1f5207b7SJohn Levon return x << 8 | x >> 8; 31*1f5207b7SJohn Levon } 32*1f5207b7SJohn Levon #endif 33*1f5207b7SJohn Levon 34*1f5207b7SJohn Levon #ifdef __HAS_BUILTIN_BSWAP32 35*1f5207b7SJohn Levon #define bswap32(x) __builtin_bswap32(x) 36*1f5207b7SJohn Levon #else 37*1f5207b7SJohn Levon #include <stdint.h> bswap32(uint32_t x)38*1f5207b7SJohn Levonstatic inline uint32_t bswap32(uint32_t x) 39*1f5207b7SJohn Levon { 40*1f5207b7SJohn Levon return x >> 24 | (x >> 8 & 0xff00) | (x << 8 & 0xff0000) | x << 24; 41*1f5207b7SJohn Levon } 42*1f5207b7SJohn Levon #endif 43*1f5207b7SJohn Levon 44*1f5207b7SJohn Levon #ifdef __HAS_BUILTIN_BSWAP64 45*1f5207b7SJohn Levon #define bswap64(x) __builtin_bswap64(x) 46*1f5207b7SJohn Levon #else 47*1f5207b7SJohn Levon #include <stdint.h> bswap64(uint64_t x)48*1f5207b7SJohn Levonstatic inline uint64_t bswap64(uint64_t x) 49*1f5207b7SJohn Levon { 50*1f5207b7SJohn Levon return ((uint64_t)bswap32(x)) << 32 | bswap32(x >> 32); 51*1f5207b7SJohn Levon } 52*1f5207b7SJohn Levon #endif 53*1f5207b7SJohn Levon 54*1f5207b7SJohn Levon #endif 55