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 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef ETHERBOOT_BYTESWAP_H
  2. #define ETHERBOOT_BYTESWAP_H
  3. #include "endian.h"
  4. #include "bits/byteswap.h"
  5. #define __bswap_constant_16(x) \
  6. ((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
  7. (((uint16_t)(x) & 0xff00) >> 8)))
  8. #define __bswap_constant_32(x) \
  9. ((uint32_t)((((uint32_t)(x) & 0x000000ffU) << 24) | \
  10. (((uint32_t)(x) & 0x0000ff00U) << 8) | \
  11. (((uint32_t)(x) & 0x00ff0000U) >> 8) | \
  12. (((uint32_t)(x) & 0xff000000U) >> 24)))
  13. #define __bswap_constant_64(x) \
  14. ((uint64_t)((((uint64_t)(x) & 0x00000000000000ffULL) << 56) | \
  15. (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
  16. (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
  17. (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
  18. (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
  19. (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
  20. (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
  21. (((uint64_t)(x) & 0xff00000000000000ULL) >> 56)))
  22. #define __bswap_16(x) \
  23. ((uint16_t)(__builtin_constant_p(x) ? \
  24. __bswap_constant_16(x) : \
  25. __bswap_variable_16(x)))
  26. #define __bswap_32(x) \
  27. ((uint32_t)(__builtin_constant_p(x) ? \
  28. __bswap_constant_32(x) : \
  29. __bswap_variable_32(x)))
  30. #define __bswap_64(x) \
  31. ((uint64_t)(__builtin_constant_p(x) ? \
  32. __bswap_constant_64(x) : \
  33. __bswap_variable_64(x)))
  34. #if __BYTE_ORDER == __LITTLE_ENDIAN
  35. #include "little_bswap.h"
  36. #endif
  37. #if __BYTE_ORDER == __BIG_ENDIAN
  38. #include "big_bswap.h"
  39. #endif
  40. /* Make routines available to all */
  41. #define swap64(x) __bswap_64(x)
  42. #define swap32(x) __bswap_32(x)
  43. #define swap16(x) __bswap_16(x)
  44. #define bswap_64(x) __bswap_64(x)
  45. #define bswap_32(x) __bswap_32(x)
  46. #define bswap_16(x) __bswap_16(x)
  47. #endif /* ETHERBOOT_BYTESWAP_H */