swab.h 6.4 KB

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