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

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