swab.h 6.8 KB

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