bswap.h 1.4 KB

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