swab.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #ifndef _UAPI_LINUX_SWAB_H
  2. #define _UAPI_LINUX_SWAB_H
  3. #include <linux/types.h>
  4. #include <linux/compiler.h>
  5. #include <asm/swab.h>
  6. /*
  7. * casts are necessary for constants, because we never know how for sure
  8. * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
  9. */
  10. #define ___constant_swab16(x) ((__u16)( \
  11. (((__u16)(x) & (__u16)0x00ffU) << 8) | \
  12. (((__u16)(x) & (__u16)0xff00U) >> 8)))
  13. #define ___constant_swab32(x) ((__u32)( \
  14. (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
  15. (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
  16. (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
  17. (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
  18. #define ___constant_swab64(x) ((__u64)( \
  19. (((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
  20. (((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
  21. (((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
  22. (((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \
  23. (((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \
  24. (((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
  25. (((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
  26. (((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56)))
  27. #define ___constant_swahw32(x) ((__u32)( \
  28. (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
  29. (((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
  30. #define ___constant_swahb32(x) ((__u32)( \
  31. (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
  32. (((__u32)(x) & (__u32)0xff00ff00UL) >> 8)))
  33. /*
  34. * Implement the following as inlines, but define the interface using
  35. * macros to allow constant folding when possible:
  36. * ___swab16, ___swab32, ___swab64, ___swahw32, ___swahb32
  37. */
  38. static inline __attribute_const__ __u16 __fswab16(__u16 val)
  39. {
  40. #ifdef __arch_swab16
  41. return __arch_swab16(val);
  42. #else
  43. return ___constant_swab16(val);
  44. #endif
  45. }
  46. static inline __attribute_const__ __u32 __fswab32(__u32 val)
  47. {
  48. #ifdef __arch_swab32
  49. return __arch_swab32(val);
  50. #else
  51. return ___constant_swab32(val);
  52. #endif
  53. }
  54. static inline __attribute_const__ __u64 __fswab64(__u64 val)
  55. {
  56. #ifdef __arch_swab64
  57. return __arch_swab64(val);
  58. #elif defined(__SWAB_64_THRU_32__)
  59. __u32 h = val >> 32;
  60. __u32 l = val & ((1ULL << 32) - 1);
  61. return (((__u64)__fswab32(l)) << 32) | ((__u64)(__fswab32(h)));
  62. #else
  63. return ___constant_swab64(val);
  64. #endif
  65. }
  66. static inline __attribute_const__ __u32 __fswahw32(__u32 val)
  67. {
  68. #ifdef __arch_swahw32
  69. return __arch_swahw32(val);
  70. #else
  71. return ___constant_swahw32(val);
  72. #endif
  73. }
  74. static inline __attribute_const__ __u32 __fswahb32(__u32 val)
  75. {
  76. #ifdef __arch_swahb32
  77. return __arch_swahb32(val);
  78. #else
  79. return ___constant_swahb32(val);
  80. #endif
  81. }
  82. /**
  83. * __swab16 - return a byteswapped 16-bit value
  84. * @x: value to byteswap
  85. */
  86. #define __swab16(x) \
  87. (__builtin_constant_p((__u16)(x)) ? \
  88. ___constant_swab16(x) : \
  89. __fswab16(x))
  90. /**
  91. * __swab32 - return a byteswapped 32-bit value
  92. * @x: value to byteswap
  93. */
  94. #define __swab32(x) \
  95. (__builtin_constant_p((__u32)(x)) ? \
  96. ___constant_swab32(x) : \
  97. __fswab32(x))
  98. /**
  99. * __swab64 - return a byteswapped 64-bit value
  100. * @x: value to byteswap
  101. */
  102. #define __swab64(x) \
  103. (__builtin_constant_p((__u64)(x)) ? \
  104. ___constant_swab64(x) : \
  105. __fswab64(x))
  106. /**
  107. * __swahw32 - return a word-swapped 32-bit value
  108. * @x: value to wordswap
  109. *
  110. * __swahw32(0x12340000) is 0x00001234
  111. */
  112. #define __swahw32(x) \
  113. (__builtin_constant_p((__u32)(x)) ? \
  114. ___constant_swahw32(x) : \
  115. __fswahw32(x))
  116. /**
  117. * __swahb32 - return a high and low byte-swapped 32-bit value
  118. * @x: value to byteswap
  119. *
  120. * __swahb32(0x12345678) is 0x34127856
  121. */
  122. #define __swahb32(x) \
  123. (__builtin_constant_p((__u32)(x)) ? \
  124. ___constant_swahb32(x) : \
  125. __fswahb32(x))
  126. /**
  127. * __swab16p - return a byteswapped 16-bit value from a pointer
  128. * @p: pointer to a naturally-aligned 16-bit value
  129. */
  130. static inline __u16 __swab16p(const __u16 *p)
  131. {
  132. #ifdef __arch_swab16p
  133. return __arch_swab16p(p);
  134. #else
  135. return __swab16(*p);
  136. #endif
  137. }
  138. /**
  139. * __swab32p - return a byteswapped 32-bit value from a pointer
  140. * @p: pointer to a naturally-aligned 32-bit value
  141. */
  142. static inline __u32 __swab32p(const __u32 *p)
  143. {
  144. #ifdef __arch_swab32p
  145. return __arch_swab32p(p);
  146. #else
  147. return __swab32(*p);
  148. #endif
  149. }
  150. /**
  151. * __swab64p - return a byteswapped 64-bit value from a pointer
  152. * @p: pointer to a naturally-aligned 64-bit value
  153. */
  154. static inline __u64 __swab64p(const __u64 *p)
  155. {
  156. #ifdef __arch_swab64p
  157. return __arch_swab64p(p);
  158. #else
  159. return __swab64(*p);
  160. #endif
  161. }
  162. /**
  163. * __swahw32p - return a wordswapped 32-bit value from a pointer
  164. * @p: pointer to a naturally-aligned 32-bit value
  165. *
  166. * See __swahw32() for details of wordswapping.
  167. */
  168. static inline __u32 __swahw32p(const __u32 *p)
  169. {
  170. #ifdef __arch_swahw32p
  171. return __arch_swahw32p(p);
  172. #else
  173. return __swahw32(*p);
  174. #endif
  175. }
  176. /**
  177. * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
  178. * @p: pointer to a naturally-aligned 32-bit value
  179. *
  180. * See __swahb32() for details of high/low byteswapping.
  181. */
  182. static inline __u32 __swahb32p(const __u32 *p)
  183. {
  184. #ifdef __arch_swahb32p
  185. return __arch_swahb32p(p);
  186. #else
  187. return __swahb32(*p);
  188. #endif
  189. }
  190. /**
  191. * __swab16s - byteswap a 16-bit value in-place
  192. * @p: pointer to a naturally-aligned 16-bit value
  193. */
  194. static inline void __swab16s(__u16 *p)
  195. {
  196. #ifdef __arch_swab16s
  197. __arch_swab16s(p);
  198. #else
  199. *p = __swab16p(p);
  200. #endif
  201. }
  202. /**
  203. * __swab32s - byteswap a 32-bit value in-place
  204. * @p: pointer to a naturally-aligned 32-bit value
  205. */
  206. static inline void __swab32s(__u32 *p)
  207. {
  208. #ifdef __arch_swab32s
  209. __arch_swab32s(p);
  210. #else
  211. *p = __swab32p(p);
  212. #endif
  213. }
  214. /**
  215. * __swab64s - byteswap a 64-bit value in-place
  216. * @p: pointer to a naturally-aligned 64-bit value
  217. */
  218. static inline void __swab64s(__u64 *p)
  219. {
  220. #ifdef __arch_swab64s
  221. __arch_swab64s(p);
  222. #else
  223. *p = __swab64p(p);
  224. #endif
  225. }
  226. /**
  227. * __swahw32s - wordswap a 32-bit value in-place
  228. * @p: pointer to a naturally-aligned 32-bit value
  229. *
  230. * See __swahw32() for details of wordswapping
  231. */
  232. static inline void __swahw32s(__u32 *p)
  233. {
  234. #ifdef __arch_swahw32s
  235. __arch_swahw32s(p);
  236. #else
  237. *p = __swahw32p(p);
  238. #endif
  239. }
  240. /**
  241. * __swahb32s - high and low byteswap a 32-bit value in-place
  242. * @p: pointer to a naturally-aligned 32-bit value
  243. *
  244. * See __swahb32() for details of high and low byte swapping
  245. */
  246. static inline void __swahb32s(__u32 *p)
  247. {
  248. #ifdef __arch_swahb32s
  249. __arch_swahb32s(p);
  250. #else
  251. *p = __swahb32p(p);
  252. #endif
  253. }
  254. #endif /* _UAPI_LINUX_SWAB_H */