BytesOrder.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Copyright @ 2014 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2014-12
  4. //
  5. #ifndef __BYTESORDER_H
  6. #define __BYTESORDER_H
  7. #define hton_16(x) cpu_to_be16(x)
  8. #define ntoh_16(x) be16_to_cpu(x)
  9. #define hton_32(x) cpu_to_be32(x)
  10. #define ntoh_32(x) be32_to_cpu(x)
  11. #define hton_64(x) cpu_to_be64(x)
  12. #define ntoh_64(x) be64_to_cpu(x)
  13. #define htole_16(x) cpu_to_le16(x)
  14. #define htole_32(x) cpu_to_le32(x)
  15. #define htole_64(x) cpu_to_le64(x)
  16. #define htobe_16(x) cpu_to_be16(x)
  17. #define htobe_32(x) cpu_to_be32(x)
  18. #define htobe_64(x) cpu_to_be64(x)
  19. #define letoh_16(x) le16_to_cpu(x)
  20. #define letoh_32(x) le32_to_cpu(x)
  21. #define letoh_64(x) le64_to_cpu(x)
  22. #define betoh_16(x) be16_to_cpu(x)
  23. #define betoh_32(x) be32_to_cpu(x)
  24. #define betoh_64(x) be64_to_cpu(x)
  25. #include "BaseTypes.h"
  26. #define swap_int16(x) \
  27. ( \
  28. (TZ_Uint16)( \
  29. (((TZ_Uint16)(x) & (TZ_Uint16)0x00ffU) << 8) | \
  30. (((TZ_Uint16)(x) & (TZ_Uint16)0xff00U) >> 8) ) \
  31. )
  32. #define swap_int32(x) \
  33. ( \
  34. (TZ_Uint32)( \
  35. (((TZ_Uint32)(x) & (TZ_Uint32)0x000000ffUL) << 24) | \
  36. (((TZ_Uint32)(x) & (TZ_Uint32)0x0000ff00UL) << 8) | \
  37. (((TZ_Uint32)(x) & (TZ_Uint32)0x00ff0000UL) >> 8) | \
  38. (((TZ_Uint32)(x) & (TZ_Uint32)0xff000000UL) >> 24) ) \
  39. )
  40. #define swap_int64(x) \
  41. ( \
  42. (TZ_Uint64)( \
  43. (TZ_Uint64)(((TZ_Uint64)(x) & (TZ_Uint64)0x00000000000000ffULL) << 56) | \
  44. (TZ_Uint64)(((TZ_Uint64)(x) & (TZ_Uint64)0x000000000000ff00ULL) << 40) | \
  45. (TZ_Uint64)(((TZ_Uint64)(x) & (TZ_Uint64)0x0000000000ff0000ULL) << 24) | \
  46. (TZ_Uint64)(((TZ_Uint64)(x) & (TZ_Uint64)0x00000000ff000000ULL) << 8) | \
  47. (TZ_Uint64)(((TZ_Uint64)(x) & (TZ_Uint64)0x000000ff00000000ULL) >> 8) | \
  48. (TZ_Uint64)(((TZ_Uint64)(x) & (TZ_Uint64)0x0000ff0000000000ULL) >> 24) | \
  49. (TZ_Uint64)(((TZ_Uint64)(x) & (TZ_Uint64)0x00ff000000000000ULL) >> 40) | \
  50. (TZ_Uint64)(((TZ_Uint64)(x) & (TZ_Uint64)0xff00000000000000ULL) >> 56) ) \
  51. )
  52. #ifdef BIG_ENDIAN_CPU // big-endian
  53. #define be16_to_cpu(x) ((TZ_Uint16)(x))
  54. #define cpu_to_be16(x) ((TZ_Uint16)(x))
  55. #define be32_to_cpu(x) ((TZ_Uint32)(x))
  56. #define cpu_to_be32(x) ((TZ_Uint32)(x))
  57. #define be64_to_cpu(x) ((TZ_Uint64)(x))
  58. #define cpu_to_be64(x) ((TZ_Uint64)(x))
  59. #define le16_to_cpu(x) (swap_int16((x)))
  60. #define cpu_to_le16(x) (swap_int16((x)))
  61. #define le32_to_cpu(x) (swap_int32((x)))
  62. #define cpu_to_le32(x) (swap_int32((x)))
  63. #define le64_to_cpu(x) (swap_int64((x)))
  64. #define cpu_to_le64(x) (swap_int64((x)))
  65. #else // little-endian
  66. #define be16_to_cpu(x) (swap_int16((x)))
  67. #define cpu_to_be16(x) (swap_int16((x)))
  68. #define be32_to_cpu(x) (swap_int32((x)))
  69. #define cpu_to_be32(x) (swap_int32((x)))
  70. #define be64_to_cpu(x) (swap_int64((x)))
  71. #define cpu_to_be64(x) (swap_int64((x)))
  72. #define le16_to_cpu(x) ((TZ_Uint16)(x))
  73. #define cpu_to_le16(x) ((TZ_Uint16)(x))
  74. #define le32_to_cpu(x) ((TZ_Uint32)(x))
  75. #define cpu_to_le32(x) ((TZ_Uint32)(x))
  76. #define le64_to_cpu(x) ((TZ_Uint64)(x))
  77. #define cpu_to_le64(x) ((TZ_Uint64)(x))
  78. #endif
  79. #endif /* ----- #ifndef __BYTESORDER_H ----- */