swab.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #if defined (__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. #if defined(__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. #if defined (__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. #ifdef __HAVE_BUILTIN_BSWAP16__
  87. #define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
  88. #else
  89. #define __swab16(x) \
  90. (__builtin_constant_p((__u16)(x)) ? \
  91. ___constant_swab16(x) : \
  92. __fswab16(x))
  93. #endif
  94. /**
  95. * __swab32 - return a byteswapped 32-bit value
  96. * @x: value to byteswap
  97. */
  98. #ifdef __HAVE_BUILTIN_BSWAP32__
  99. #define __swab32(x) (__u32)__builtin_bswap32((__u32)(x))
  100. #else
  101. #define __swab32(x) \
  102. (__builtin_constant_p((__u32)(x)) ? \
  103. ___constant_swab32(x) : \
  104. __fswab32(x))
  105. #endif
  106. /**
  107. * __swab64 - return a byteswapped 64-bit value
  108. * @x: value to byteswap
  109. */
  110. #ifdef __HAVE_BUILTIN_BSWAP64__
  111. #define __swab64(x) (__u64)__builtin_bswap64((__u64)(x))
  112. #else
  113. #define __swab64(x) \
  114. (__builtin_constant_p((__u64)(x)) ? \
  115. ___constant_swab64(x) : \
  116. __fswab64(x))
  117. #endif
  118. /**
  119. * __swahw32 - return a word-swapped 32-bit value
  120. * @x: value to wordswap
  121. *
  122. * __swahw32(0x12340000) is 0x00001234
  123. */
  124. #define __swahw32(x) \
  125. (__builtin_constant_p((__u32)(x)) ? \
  126. ___constant_swahw32(x) : \
  127. __fswahw32(x))
  128. /**
  129. * __swahb32 - return a high and low byte-swapped 32-bit value
  130. * @x: value to byteswap
  131. *
  132. * __swahb32(0x12345678) is 0x34127856
  133. */
  134. #define __swahb32(x) \
  135. (__builtin_constant_p((__u32)(x)) ? \
  136. ___constant_swahb32(x) : \
  137. __fswahb32(x))
  138. /**
  139. * __swab16p - return a byteswapped 16-bit value from a pointer
  140. * @p: pointer to a naturally-aligned 16-bit value
  141. */
  142. static __always_inline __u16 __swab16p(const __u16 *p)
  143. {
  144. #ifdef __arch_swab16p
  145. return __arch_swab16p(p);
  146. #else
  147. return __swab16(*p);
  148. #endif
  149. }
  150. /**
  151. * __swab32p - return a byteswapped 32-bit value from a pointer
  152. * @p: pointer to a naturally-aligned 32-bit value
  153. */
  154. static __always_inline __u32 __swab32p(const __u32 *p)
  155. {
  156. #ifdef __arch_swab32p
  157. return __arch_swab32p(p);
  158. #else
  159. return __swab32(*p);
  160. #endif
  161. }
  162. /**
  163. * __swab64p - return a byteswapped 64-bit value from a pointer
  164. * @p: pointer to a naturally-aligned 64-bit value
  165. */
  166. static __always_inline __u64 __swab64p(const __u64 *p)
  167. {
  168. #ifdef __arch_swab64p
  169. return __arch_swab64p(p);
  170. #else
  171. return __swab64(*p);
  172. #endif
  173. }
  174. /**
  175. * __swahw32p - return a wordswapped 32-bit value from a pointer
  176. * @p: pointer to a naturally-aligned 32-bit value
  177. *
  178. * See __swahw32() for details of wordswapping.
  179. */
  180. static inline __u32 __swahw32p(const __u32 *p)
  181. {
  182. #ifdef __arch_swahw32p
  183. return __arch_swahw32p(p);
  184. #else
  185. return __swahw32(*p);
  186. #endif
  187. }
  188. /**
  189. * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
  190. * @p: pointer to a naturally-aligned 32-bit value
  191. *
  192. * See __swahb32() for details of high/low byteswapping.
  193. */
  194. static inline __u32 __swahb32p(const __u32 *p)
  195. {
  196. #ifdef __arch_swahb32p
  197. return __arch_swahb32p(p);
  198. #else
  199. return __swahb32(*p);
  200. #endif
  201. }
  202. /**
  203. * __swab16s - byteswap a 16-bit value in-place
  204. * @p: pointer to a naturally-aligned 16-bit value
  205. */
  206. static inline void __swab16s(__u16 *p)
  207. {
  208. #ifdef __arch_swab16s
  209. __arch_swab16s(p);
  210. #else
  211. *p = __swab16p(p);
  212. #endif
  213. }
  214. /**
  215. * __swab32s - byteswap a 32-bit value in-place
  216. * @p: pointer to a naturally-aligned 32-bit value
  217. */
  218. static __always_inline void __swab32s(__u32 *p)
  219. {
  220. #ifdef __arch_swab32s
  221. __arch_swab32s(p);
  222. #else
  223. *p = __swab32p(p);
  224. #endif
  225. }
  226. /**
  227. * __swab64s - byteswap a 64-bit value in-place
  228. * @p: pointer to a naturally-aligned 64-bit value
  229. */
  230. static __always_inline void __swab64s(__u64 *p)
  231. {
  232. #ifdef __arch_swab64s
  233. __arch_swab64s(p);
  234. #else
  235. *p = __swab64p(p);
  236. #endif
  237. }
  238. /**
  239. * __swahw32s - wordswap a 32-bit value in-place
  240. * @p: pointer to a naturally-aligned 32-bit value
  241. *
  242. * See __swahw32() for details of wordswapping
  243. */
  244. static inline void __swahw32s(__u32 *p)
  245. {
  246. #ifdef __arch_swahw32s
  247. __arch_swahw32s(p);
  248. #else
  249. *p = __swahw32p(p);
  250. #endif
  251. }
  252. /**
  253. * __swahb32s - high and low byteswap a 32-bit value in-place
  254. * @p: pointer to a naturally-aligned 32-bit value
  255. *
  256. * See __swahb32() for details of high and low byte swapping
  257. */
  258. static inline void __swahb32s(__u32 *p)
  259. {
  260. #ifdef __arch_swahb32s
  261. __arch_swahb32s(p);
  262. #else
  263. *p = __swahb32p(p);
  264. #endif
  265. }
  266. #endif /* _UAPI_LINUX_SWAB_H */