byteorder.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-25 GuEe-GUI the first version
  9. */
  10. #ifndef __BYTEORDER__
  11. #define __BYTEORDER__
  12. #ifdef __CHECKER__
  13. #define __bitwise __attribute__((bitwise))
  14. #else
  15. #define __bitwise
  16. #endif
  17. typedef rt_uint16_t __bitwise rt_le16_t;
  18. typedef rt_uint32_t __bitwise rt_le32_t;
  19. typedef rt_uint64_t __bitwise rt_le64_t;
  20. typedef rt_uint16_t __bitwise rt_be16_t;
  21. typedef rt_uint32_t __bitwise rt_be32_t;
  22. typedef rt_uint64_t __bitwise rt_be64_t;
  23. /* gcc defines __BIG_ENDIAN__ on big endian targets */
  24. #if defined(__BIG_ENDIAN__) || defined(ARCH_CPU_BIG_ENDIAN)
  25. #define rt_cpu_to_be16(x) (x)
  26. #define rt_cpu_to_be32(x) (x)
  27. #define rt_cpu_to_be64(x) (x)
  28. #define rt_be16_to_cpu(x) (x)
  29. #define rt_be32_to_cpu(x) (x)
  30. #define rt_be64_to_cpu(x) (x)
  31. #define rt_le16_to_cpu(x) __builtin_bswap16(x)
  32. #define rt_le32_to_cpu(x) __builtin_bswap32(x)
  33. #define rt_le64_to_cpu(x) __builtin_bswap64(x)
  34. #define rt_cpu_to_le16(x) __builtin_bswap16(x)
  35. #define rt_cpu_to_le32(x) __builtin_bswap32(x)
  36. #define rt_cpu_to_le64(x) __builtin_bswap64(x)
  37. #else
  38. #define rt_cpu_to_be16(x) __builtin_bswap16(x)
  39. #define rt_cpu_to_be32(x) __builtin_bswap32(x)
  40. #define rt_cpu_to_be64(x) __builtin_bswap64(x)
  41. #define rt_be16_to_cpu(x) __builtin_bswap16(x)
  42. #define rt_be32_to_cpu(x) __builtin_bswap32(x)
  43. #define rt_be64_to_cpu(x) __builtin_bswap64(x)
  44. #define rt_le16_to_cpu(x) (x)
  45. #define rt_le32_to_cpu(x) (x)
  46. #define rt_le64_to_cpu(x) (x)
  47. #define rt_cpu_to_le16(x) (x)
  48. #define rt_cpu_to_le32(x) (x)
  49. #define rt_cpu_to_le64(x) (x)
  50. #endif /* __BIG_ENDIAN__ || ARCH_CPU_BIG_ENDIAN */
  51. #undef __bitwise
  52. #endif /* __BYTEORDER__ */