mtd_swab.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef MTD_SWAB_H
  2. #define MTD_SWAB_H
  3. #include <endian.h>
  4. #define swab16(x) \
  5. ((uint16_t)( \
  6. (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
  7. (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) ))
  8. #define swab32(x) \
  9. ((uint32_t)( \
  10. (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
  11. (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
  12. (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
  13. (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) ))
  14. #define swab64(x) \
  15. ((uint64_t)( \
  16. (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
  17. (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
  18. (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
  19. (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
  20. (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
  21. (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
  22. (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
  23. (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) ))
  24. #if __BYTE_ORDER == __BIG_ENDIAN
  25. #define cpu_to_le16(x) ({ uint16_t _x = x; swab16(_x); })
  26. #define cpu_to_le32(x) ({ uint32_t _x = x; swab32(_x); })
  27. #define cpu_to_le64(x) ({ uint64_t _x = x; swab64(_x); })
  28. #define cpu_to_be16(x) (x)
  29. #define cpu_to_be32(x) (x)
  30. #define cpu_to_be64(x) (x)
  31. #else
  32. #define cpu_to_le16(x) (x)
  33. #define cpu_to_le32(x) (x)
  34. #define cpu_to_le64(x) (x)
  35. #define cpu_to_be16(x) ({ uint16_t _x = x; swab16(_x); })
  36. #define cpu_to_be32(x) ({ uint32_t _x = x; swab32(_x); })
  37. #define cpu_to_be64(x) ({ uint64_t _x = x; swab64(_x); })
  38. #endif
  39. #define le16_to_cpu(x) cpu_to_le16(x)
  40. #define be16_to_cpu(x) cpu_to_be16(x)
  41. #define le32_to_cpu(x) cpu_to_le32(x)
  42. #define be32_to_cpu(x) cpu_to_be32(x)
  43. #define le64_to_cpu(x) cpu_to_le64(x)
  44. #define be64_to_cpu(x) cpu_to_be64(x)
  45. #endif