byte_order.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* byte_order.h */
  2. #ifndef BYTE_ORDER_H
  3. #define BYTE_ORDER_H
  4. #include "ustd.h"
  5. #include <stdlib.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /* if x86 compatible cpu */
  10. #if defined(i386) || defined(__i386__) || defined(__i486__) || \
  11. defined(__i586__) || defined(__i686__) || defined(__pentium__) || \
  12. defined(__pentiumpro__) || defined(__pentium4__) || \
  13. defined(__nocona__) || defined(prescott) || defined(__core2__) || \
  14. defined(__k6__) || defined(__k8__) || defined(__athlon__) || \
  15. defined(__amd64) || defined(__amd64__) || \
  16. defined(__x86_64) || defined(__x86_64__) || defined(_M_IX86) || \
  17. defined(_M_AMD64) || defined(_M_IA64) || defined(_M_X64)
  18. /* detect if x86-64 instruction set is supported */
  19. # if defined(_LP64) || defined(__LP64__) || defined(__x86_64) || \
  20. defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)
  21. # define CPU_X64
  22. # else
  23. # define CPU_IA32
  24. # endif
  25. #endif
  26. /* detect CPU endianness */
  27. #include <cm_kwiml.h>
  28. #if KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_LITTLE
  29. # define CPU_LITTLE_ENDIAN
  30. # define IS_BIG_ENDIAN 0
  31. # define IS_LITTLE_ENDIAN 1
  32. #elif KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_BIG
  33. # define CPU_BIG_ENDIAN
  34. # define IS_BIG_ENDIAN 1
  35. # define IS_LITTLE_ENDIAN 0
  36. #else
  37. # error "Can't detect CPU architechture"
  38. #endif
  39. #define IS_ALIGNED_32(p) (0 == (3 & ((const char*)(p) - (const char*)0)))
  40. #define IS_ALIGNED_64(p) (0 == (7 & ((const char*)(p) - (const char*)0)))
  41. #if defined(_MSC_VER)
  42. #define ALIGN_ATTR(n) __declspec(align(n))
  43. #elif defined(__GNUC__)
  44. #define ALIGN_ATTR(n) __attribute__((aligned (n)))
  45. #else
  46. #define ALIGN_ATTR(n) /* nothing */
  47. #endif
  48. #if defined(_MSC_VER) || defined(__BORLANDC__)
  49. #define I64(x) x##ui64
  50. #else
  51. #define I64(x) x##LL
  52. #endif
  53. /* convert a hash flag to index */
  54. #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) /* GCC < 3.4 */
  55. # define rhash_ctz(x) __builtin_ctz(x)
  56. #else
  57. unsigned rhash_ctz(unsigned); /* define as function */
  58. #endif
  59. void rhash_swap_copy_str_to_u32(void* to, int index, const void* from, size_t length);
  60. void rhash_swap_copy_str_to_u64(void* to, int index, const void* from, size_t length);
  61. void rhash_swap_copy_u64_to_str(void* to, const void* from, size_t length);
  62. void rhash_u32_mem_swap(unsigned *p, int length_in_u32);
  63. #ifndef __has_builtin
  64. # define __has_builtin(x) 0
  65. #endif
  66. /* define bswap_32 */
  67. #if defined(__GNUC__) && defined(CPU_IA32) && !defined(__i386__)
  68. /* for intel x86 CPU */
  69. static inline uint32_t bswap_32(uint32_t x) {
  70. __asm("bswap\t%0" : "=r" (x) : "0" (x));
  71. return x;
  72. }
  73. #elif defined(__GNUC__) && (__GNUC__ >= 4) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3)
  74. /* for GCC >= 4.3 */
  75. # define bswap_32(x) __builtin_bswap32(x)
  76. #elif defined(__clang__) && __has_builtin(__builtin_bswap32)
  77. # define bswap_32(x) __builtin_bswap32(x)
  78. #elif (_MSC_VER > 1300) && (defined(CPU_IA32) || defined(CPU_X64)) /* MS VC */
  79. # define bswap_32(x) _byteswap_ulong((unsigned long)x)
  80. #else
  81. /* general bswap_32 definition */
  82. static uint32_t bswap_32(uint32_t x) {
  83. x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0x00FF00FF);
  84. return (x >> 16) | (x << 16);
  85. }
  86. #endif /* bswap_32 */
  87. #if defined(__GNUC__) && (__GNUC__ >= 4) && (__GNUC__ > 4 || __GNUC_MINOR__ >= 3)
  88. # define bswap_64(x) __builtin_bswap64(x)
  89. #elif defined(__clang__) && __has_builtin(__builtin_bswap64)
  90. # define bswap_64(x) __builtin_bswap64(x)
  91. #elif (_MSC_VER > 1300) && (defined(CPU_IA32) || defined(CPU_X64)) /* MS VC */
  92. # define bswap_64(x) _byteswap_uint64((__int64)x)
  93. #else
  94. static uint64_t bswap_64(uint64_t x) {
  95. union {
  96. uint64_t ll;
  97. uint32_t l[2];
  98. } w, r;
  99. w.ll = x;
  100. r.l[0] = bswap_32(w.l[1]);
  101. r.l[1] = bswap_32(w.l[0]);
  102. return r.ll;
  103. }
  104. #endif
  105. #ifdef CPU_BIG_ENDIAN
  106. # define be2me_32(x) (x)
  107. # define be2me_64(x) (x)
  108. # define le2me_32(x) bswap_32(x)
  109. # define le2me_64(x) bswap_64(x)
  110. # define be32_copy(to, index, from, length) memcpy((to) + (index), (from), (length))
  111. # define le32_copy(to, index, from, length) rhash_swap_copy_str_to_u32((to), (index), (from), (length))
  112. # define be64_copy(to, index, from, length) memcpy((to) + (index), (from), (length))
  113. # define le64_copy(to, index, from, length) rhash_swap_copy_str_to_u64((to), (index), (from), (length))
  114. # define me64_to_be_str(to, from, length) memcpy((to), (from), (length))
  115. # define me64_to_le_str(to, from, length) rhash_swap_copy_u64_to_str((to), (from), (length))
  116. #else /* CPU_BIG_ENDIAN */
  117. # define be2me_32(x) bswap_32(x)
  118. # define be2me_64(x) bswap_64(x)
  119. # define le2me_32(x) (x)
  120. # define le2me_64(x) (x)
  121. # define be32_copy(to, index, from, length) rhash_swap_copy_str_to_u32((to), (index), (from), (length))
  122. # define le32_copy(to, index, from, length) memcpy((to) + (index), (from), (length))
  123. # define be64_copy(to, index, from, length) rhash_swap_copy_str_to_u64((to), (index), (from), (length))
  124. # define le64_copy(to, index, from, length) memcpy((to) + (index), (from), (length))
  125. # define me64_to_be_str(to, from, length) rhash_swap_copy_u64_to_str((to), (from), (length))
  126. # define me64_to_le_str(to, from, length) memcpy((to), (from), (length))
  127. #endif /* CPU_BIG_ENDIAN */
  128. /* ROTL/ROTR macros rotate a 32/64-bit word left/right by n bits */
  129. #define ROTL32(dword, n) ((dword) << (n) ^ ((dword) >> (32 - (n))))
  130. #define ROTR32(dword, n) ((dword) >> (n) ^ ((dword) << (32 - (n))))
  131. #define ROTL64(qword, n) ((qword) << (n) ^ ((qword) >> (64 - (n))))
  132. #define ROTR64(qword, n) ((qword) >> (n) ^ ((qword) << (64 - (n))))
  133. #ifdef __cplusplus
  134. } /* extern "C" */
  135. #endif /* __cplusplus */
  136. #endif /* BYTE_ORDER_H */