byteswap.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #define ___swab16(x) \
  2. ({ \
  3. unsigned short __x = (x); \
  4. ((unsigned short)( \
  5. (((unsigned short)(__x) & (unsigned short)0x00ffU) << 8) | \
  6. (((unsigned short)(__x) & (unsigned short)0xff00U) >> 8) )); \
  7. })
  8. #define ___swab32(x) \
  9. ({ \
  10. unsigned long __x = (x); \
  11. ((unsigned long)( \
  12. (((unsigned long)(__x) & (unsigned long)0x000000ffUL) << 24) | \
  13. (((unsigned long)(__x) & (unsigned long)0x0000ff00UL) << 8) | \
  14. (((unsigned long)(__x) & (unsigned long)0x00ff0000UL) >> 8) | \
  15. (((unsigned long)(__x) & (unsigned long)0xff000000UL) >> 24) )); \
  16. })
  17. /* these are CRIS specific */
  18. static inline unsigned short __fswab16(unsigned short x)
  19. {
  20. __asm__ ("swapb %0" : "=r" (x) : "0" (x));
  21. return(x);
  22. }
  23. static inline unsigned long __fswab32(unsigned long x)
  24. {
  25. __asm__ ("swapwb %0" : "=r" (x) : "0" (x));
  26. return(x);
  27. }
  28. # define __bswap_16(x) \
  29. (__builtin_constant_p((unsigned short)(x)) ? \
  30. ___swab16((x)) : \
  31. __fswab16((x)))
  32. # define __bswap_32(x) \
  33. (__builtin_constant_p((unsigned long)(x)) ? \
  34. ___swab32((x)) : \
  35. __fswab32((x)))