swab.h 6.6 KB

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