swab.h 7.7 KB

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