byteswap.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifndef _BITS_BYTESWAP_H
  2. #define _BITS_BYTESWAP_H 1
  3. /* CRIS specific byte swap operations: 16, 32 and 64-bit */
  4. /* Swap bytes in 16 bit value. */
  5. #define __bswap_constant_16(x) \
  6. ({ \
  7. unsigned short __x = (x); \
  8. ((unsigned short)( \
  9. (((unsigned short)(__x) & (unsigned short)0x00ffu) << 8) | \
  10. (((unsigned short)(__x) & (unsigned short)0xff00u) >> 8) )); \
  11. })
  12. #if defined __GNUC__ && __GNUC__ >= 2
  13. # define __bswap_16(x) \
  14. __extension__ \
  15. ({ unsigned short __bswap_16_v; \
  16. if (__builtin_constant_p (x)) \
  17. __bswap_16_v = __bswap_constant_16 (x); \
  18. else \
  19. __asm__ ("swapb %0" : "=r" (__bswap_16_v) : "0" (x)); \
  20. __bswap_16_v; })
  21. #else
  22. # define __bswap_16(x) __bswap_constant_16 (x)
  23. #endif
  24. /* Swap bytes in 32 bit value. */
  25. #define __bswap_constant_32(x) \
  26. ({ \
  27. unsigned long __x = (x); \
  28. ((unsigned long)( \
  29. (((unsigned long)(__x) & (unsigned long)0x000000fful) << 24) | \
  30. (((unsigned long)(__x) & (unsigned long)0x0000ff00ul) << 8) | \
  31. (((unsigned long)(__x) & (unsigned long)0x00ff0000ul) >> 8) | \
  32. (((unsigned long)(__x) & (unsigned long)0xff000000ul) >> 24) )); \
  33. })
  34. #if defined __GNUC__ && __GNUC__ >= 2
  35. # define __bswap_32(x) \
  36. __extension__ \
  37. ({ unsigned long __bswap_32_v; \
  38. if (__builtin_constant_p (x)) \
  39. __bswap_32_v = __bswap_constant_32 (x); \
  40. else \
  41. __asm__ ("swapwb %0" : "=r" (__bswap_32_v) : "0" (x)); \
  42. __bswap_32_v; })
  43. #else
  44. # define __bswap_32(x) __bswap_constant_32 (x)
  45. #endif
  46. /* Swap bytes in 64 bit value. */
  47. # define __bswap_constant_64(x) \
  48. ((((x) & 0xff00000000000000ull) >> 56) \
  49. | (((x) & 0x00ff000000000000ull) >> 40) \
  50. | (((x) & 0x0000ff0000000000ull) >> 24) \
  51. | (((x) & 0x000000ff00000000ull) >> 8) \
  52. | (((x) & 0x00000000ff000000ull) << 8) \
  53. | (((x) & 0x0000000000ff0000ull) << 24) \
  54. | (((x) & 0x000000000000ff00ull) << 40) \
  55. | (((x) & 0x00000000000000ffull) << 56))
  56. #if defined __GNUC__ && __GNUC__ >= 2
  57. # define __bswap_64(x) \
  58. (__extension__ \
  59. ({ union { __extension__ unsigned long long int __ll; \
  60. unsigned int __l[2]; } __w, __r; \
  61. if (__builtin_constant_p (x)) \
  62. __r.__ll = __bswap_constant_64 (x); \
  63. else \
  64. { \
  65. __w.__ll = (x); \
  66. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  67. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  68. } \
  69. __r.__ll; }))
  70. #else
  71. # define __bswap_64(x) __bswap_constant_64 (x)
  72. #endif
  73. #endif /* _BITS_BYTESWAP_H */