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