endian.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #ifndef CCAN_ENDIAN_H
  2. #define CCAN_ENDIAN_H
  3. #include <stdint.h>
  4. #include "config.h"
  5. /**
  6. * swab_u16 - reverse bytes in a uint16_t value.
  7. * @val: value whose bytes to swap.
  8. */
  9. static inline uint16_t swab_u16(uint16_t val)
  10. {
  11. return ((val & (uint16_t)0x00ffU) << 8)
  12. | ((val & (uint16_t)0xff00U) >> 8);
  13. }
  14. /**
  15. * swab_u32 - reverse bytes in a uint32_t value.
  16. * @val: value whose bytes to swap.
  17. */
  18. static inline uint32_t swab_u32(uint32_t val)
  19. {
  20. return ((val & (uint32_t)0x000000ffUL) << 24)
  21. | ((val & (uint32_t)0x0000ff00UL) << 8)
  22. | ((val & (uint32_t)0x00ff0000UL) >> 8)
  23. | ((val & (uint32_t)0xff000000UL) >> 24);
  24. }
  25. /**
  26. * swab_u64 - reverse bytes in a uint64_t value.
  27. * @val: value whose bytes to swap.
  28. */
  29. static inline uint64_t swab_u64(uint64_t val)
  30. {
  31. return ((val & (uint64_t)0x00000000000000ffULL) << 56)
  32. | ((val & (uint64_t)0x000000000000ff00ULL) << 40)
  33. | ((val & (uint64_t)0x0000000000ff0000ULL) << 24)
  34. | ((val & (uint64_t)0x00000000ff000000ULL) << 8)
  35. | ((val & (uint64_t)0x000000ff00000000ULL) >> 8)
  36. | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24)
  37. | ((val & (uint64_t)0x00ff000000000000ULL) >> 40)
  38. | ((val & (uint64_t)0xff00000000000000ULL) >> 56);
  39. }
  40. /* Sanity check the defines. We don't handle weird endianness. */
  41. #if !HAVE_LITTLE_ENDIAN && !HAVE_BIG_ENDIAN
  42. #error "Unknown endian"
  43. #elif HAVE_LITTLE_ENDIAN && HAVE_BIG_ENDIAN
  44. #error "Can't compile for both big and little endian."
  45. #endif
  46. /**
  47. * cpu_to_le64 - convert a uint64_t value to little-endian
  48. * @native: value to convert
  49. */
  50. static inline uint64_t cpu_to_le64(uint64_t native)
  51. {
  52. #if HAVE_LITTLE_ENDIAN
  53. return native;
  54. #else
  55. return swab_u64(native);
  56. #endif
  57. }
  58. /**
  59. * cpu_to_le32 - convert a uint32_t value to little-endian
  60. * @native: value to convert
  61. */
  62. static inline uint32_t cpu_to_le32(uint32_t native)
  63. {
  64. #if HAVE_LITTLE_ENDIAN
  65. return native;
  66. #else
  67. return swab_u32(native);
  68. #endif
  69. }
  70. /**
  71. * cpu_to_le16 - convert a uint16_t value to little-endian
  72. * @native: value to convert
  73. */
  74. static inline uint16_t cpu_to_le16(uint16_t native)
  75. {
  76. #if HAVE_LITTLE_ENDIAN
  77. return native;
  78. #else
  79. return swab_u16(native);
  80. #endif
  81. }
  82. /**
  83. * le64_to_cpu - convert a little-endian uint64_t value
  84. * @le_val: little-endian value to convert
  85. */
  86. static inline uint64_t le64_to_cpu(uint64_t le_val)
  87. {
  88. #if HAVE_LITTLE_ENDIAN
  89. return le_val;
  90. #else
  91. return swab_u64(le_val);
  92. #endif
  93. }
  94. /**
  95. * le32_to_cpu - convert a little-endian uint32_t value
  96. * @le_val: little-endian value to convert
  97. */
  98. static inline uint32_t le32_to_cpu(uint32_t le_val)
  99. {
  100. #if HAVE_LITTLE_ENDIAN
  101. return le_val;
  102. #else
  103. return swab_u32(le_val);
  104. #endif
  105. }
  106. /**
  107. * le16_to_cpu - convert a little-endian uint16_t value
  108. * @le_val: little-endian value to convert
  109. */
  110. static inline uint16_t le16_to_cpu(uint16_t le_val)
  111. {
  112. #if HAVE_LITTLE_ENDIAN
  113. return le_val;
  114. #else
  115. return swab_u16(le_val);
  116. #endif
  117. }
  118. /**
  119. * cpu_to_be64 - convert a uint64_t value to big endian.
  120. * @native: value to convert
  121. */
  122. static inline uint64_t cpu_to_be64(uint64_t native)
  123. {
  124. #if HAVE_LITTLE_ENDIAN
  125. return swab_u64(native);
  126. #else
  127. return native;
  128. #endif
  129. }
  130. /**
  131. * cpu_to_be32 - convert a uint32_t value to big endian.
  132. * @native: value to convert
  133. */
  134. static inline uint32_t cpu_to_be32(uint32_t native)
  135. {
  136. #if HAVE_LITTLE_ENDIAN
  137. return swab_u32(native);
  138. #else
  139. return native;
  140. #endif
  141. }
  142. /**
  143. * cpu_to_be16 - convert a uint16_t value to big endian.
  144. * @native: value to convert
  145. */
  146. static inline uint16_t cpu_to_be16(uint16_t native)
  147. {
  148. #if HAVE_LITTLE_ENDIAN
  149. return swab_u16(native);
  150. #else
  151. return native;
  152. #endif
  153. }
  154. /**
  155. * be64_to_cpu - convert a big-endian uint64_t value
  156. * @be_val: big-endian value to convert
  157. */
  158. static inline uint64_t be64_to_cpu(uint64_t be_val)
  159. {
  160. #if HAVE_LITTLE_ENDIAN
  161. return swab_u64(be_val);
  162. #else
  163. return be_val;
  164. #endif
  165. }
  166. /**
  167. * be32_to_cpu - convert a big-endian uint32_t value
  168. * @be_val: big-endian value to convert
  169. */
  170. static inline uint32_t be32_to_cpu(uint32_t be_val)
  171. {
  172. #if HAVE_LITTLE_ENDIAN
  173. return swab_u32(be_val);
  174. #else
  175. return be_val;
  176. #endif
  177. }
  178. /**
  179. * be16_to_cpu - convert a big-endian uint16_t value
  180. * @be_val: big-endian value to convert
  181. */
  182. static inline uint16_t be16_to_cpu(uint16_t be_val)
  183. {
  184. #if HAVE_LITTLE_ENDIAN
  185. return swab_u16(be_val);
  186. #else
  187. return be_val;
  188. #endif
  189. }
  190. #endif /* CCAN_ENDIAN_H */