bswap.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Lame bswap replacements as we can't assume the host is sane and provides
  3. * working versions of these.
  4. */
  5. #ifndef _BSWAP_H
  6. #define _BSWAP_H 1
  7. #ifdef __linux__
  8. # include <byteswap.h>
  9. #else
  10. static __inline__ uint16_t bswap_16(uint16_t x)
  11. {
  12. return ((((x) & 0xff00) >> 8) | \
  13. (((x) & 0x00ff) << 8));
  14. }
  15. static __inline__ uint32_t bswap_32(uint32_t x)
  16. {
  17. return ((((x) & 0xff000000) >> 24) | \
  18. (((x) & 0x00ff0000) >> 8) | \
  19. (((x) & 0x0000ff00) << 8) | \
  20. (((x) & 0x000000ff) << 24));
  21. }
  22. static __inline__ uint64_t bswap_64(uint64_t x)
  23. {
  24. #define _uswap_64(x, sfx) \
  25. return ((((x) & 0xff00000000000000##sfx) >> 56) | \
  26. (((x) & 0x00ff000000000000##sfx) >> 40) | \
  27. (((x) & 0x0000ff0000000000##sfx) >> 24) | \
  28. (((x) & 0x000000ff00000000##sfx) >> 8) | \
  29. (((x) & 0x00000000ff000000##sfx) << 8) | \
  30. (((x) & 0x0000000000ff0000##sfx) << 24) | \
  31. (((x) & 0x000000000000ff00##sfx) << 40) | \
  32. (((x) & 0x00000000000000ff##sfx) << 56));
  33. #if defined(__GNUC__)
  34. _uswap_64(x, ull)
  35. #else
  36. _uswap_64(x, )
  37. #endif
  38. #undef _uswap_64
  39. }
  40. #endif
  41. #endif