bswap.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #if !defined(__BYTE_ORDER) && defined(BYTE_ORDER)
  8. # define __BYTE_ORDER BYTE_ORDER
  9. #endif
  10. #ifndef __BYTE_ORDER
  11. # ifdef __linux__
  12. # include <endian.h>
  13. # else
  14. # define __LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */
  15. # define __BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */
  16. # define __PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */
  17. # if defined(sun386) || defined(i386)
  18. # define __BYTE_ORDER __LITTLE_ENDIAN
  19. # endif
  20. # if defined(sparc)
  21. # define __BYTE_ORDER __BIG_ENDIAN
  22. # endif
  23. # endif /* __linux__ */
  24. #endif /* __BYTE_ORDER */
  25. #ifndef __BYTE_ORDER
  26. # error "Undefined __BYTE_ORDER"
  27. #endif
  28. #ifdef __linux__
  29. # include <byteswap.h>
  30. #else
  31. static inline uint16_t bswap_16(uint16_t x)
  32. {
  33. return ((((x) & 0xff00) >> 8) | \
  34. (((x) & 0x00ff) << 8));
  35. }
  36. static inline uint32_t bswap_32(uint32_t x)
  37. {
  38. return ((((x) & 0xff000000) >> 24) | \
  39. (((x) & 0x00ff0000) >> 8) | \
  40. (((x) & 0x0000ff00) << 8) | \
  41. (((x) & 0x000000ff) << 24));
  42. }
  43. #endif
  44. #endif