bswap.h 1.0 KB

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