You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

byteswap.h 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #ifndef ETHERBOOT_BITS_BYTESWAP_H
  2. #define ETHERBOOT_BITS_BYTESWAP_H
  3. static inline __attribute__ ((always_inline, const)) uint16_t
  4. __i386_bswap_16(uint16_t x)
  5. {
  6. __asm__("xchgb %b0,%h0\n\t"
  7. : "=q" (x)
  8. : "0" (x));
  9. return x;
  10. }
  11. static inline __attribute__ ((always_inline, const)) uint32_t
  12. __i386_bswap_32(uint32_t x)
  13. {
  14. __asm__("xchgb %b0,%h0\n\t"
  15. "rorl $16,%0\n\t"
  16. "xchgb %b0,%h0"
  17. : "=q" (x)
  18. : "0" (x));
  19. return x;
  20. }
  21. static inline __attribute__ ((always_inline, const)) uint64_t
  22. __i386_bswap_64(uint64_t x)
  23. {
  24. union {
  25. uint64_t qword;
  26. uint32_t dword[2];
  27. } u;
  28. u.qword = x;
  29. u.dword[0] = __i386_bswap_32(u.dword[0]);
  30. u.dword[1] = __i386_bswap_32(u.dword[1]);
  31. __asm__("xchgl %0,%1"
  32. : "=r" ( u.dword[0] ), "=r" ( u.dword[1] )
  33. : "0" ( u.dword[0] ), "1" ( u.dword[1] ) );
  34. return u.qword;
  35. }
  36. #define __bswap_constant_16(x) \
  37. ((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
  38. (((uint16_t)(x) & 0xff00) >> 8)))
  39. #define __bswap_constant_32(x) \
  40. ((uint32_t)((((uint32_t)(x) & 0x000000ffU) << 24) | \
  41. (((uint32_t)(x) & 0x0000ff00U) << 8) | \
  42. (((uint32_t)(x) & 0x00ff0000U) >> 8) | \
  43. (((uint32_t)(x) & 0xff000000U) >> 24)))
  44. #define __bswap_constant_64(x) \
  45. ((uint64_t)((((uint64_t)(x) & 0x00000000000000ffULL) << 56) | \
  46. (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
  47. (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
  48. (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
  49. (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
  50. (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
  51. (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
  52. (((uint64_t)(x) & 0xff00000000000000ULL) >> 56)))
  53. #define __bswap_16(x) \
  54. ((uint16_t)(__builtin_constant_p(x) ? \
  55. __bswap_constant_16(x) : \
  56. __i386_bswap_16(x)))
  57. #define __bswap_32(x) \
  58. ((uint32_t)(__builtin_constant_p(x) ? \
  59. __bswap_constant_32(x) : \
  60. __i386_bswap_32(x)))
  61. #define __bswap_64(x) \
  62. ((uint64_t)(__builtin_constant_p(x) ? \
  63. __bswap_constant_64(x) : \
  64. __i386_bswap_64(x)))
  65. #endif /* ETHERBOOT_BITS_BYTESWAP_H */