swab.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #ifndef _LINUX_SWAB_H
  2. #define _LINUX_SWAB_H
  3. #include <linux/types.h>
  4. #include <linux/compiler.h>
  5. #include <asm/byteorder.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 __const_swab16(x) ((__u16)( \
  11. (((__u16)(x) & (__u16)0x00ffU) << 8) | \
  12. (((__u16)(x) & (__u16)0xff00U) >> 8)))
  13. #define __const_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 __const_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 __const_swahw32(x) ((__u32)( \
  28. (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
  29. (((__u32)(x) & (__u32)0xffff0000UL) >> 16)))
  30. #define __const_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 ___swab16(__u16 val)
  39. {
  40. #ifdef __arch_swab16
  41. return __arch_swab16(val);
  42. #elif defined(__arch_swab16p)
  43. return __arch_swab16p(&val);
  44. #else
  45. return __const_swab16(val);
  46. #endif
  47. }
  48. static inline __attribute_const__ __u32 ___swab32(__u32 val)
  49. {
  50. #ifdef __arch_swab32
  51. return __arch_swab32(val);
  52. #elif defined(__arch_swab32p)
  53. return __arch_swab32p(&val);
  54. #else
  55. return __const_swab32(val);
  56. #endif
  57. }
  58. static inline __attribute_const__ __u64 ___swab64(__u64 val)
  59. {
  60. #ifdef __arch_swab64
  61. return __arch_swab64(val);
  62. #elif defined(__arch_swab64p)
  63. return __arch_swab64p(&val);
  64. #elif defined(__SWAB_64_THRU_32__)
  65. __u32 h = val >> 32;
  66. __u32 l = val & ((1ULL << 32) - 1);
  67. return (((__u64)___swab32(l)) << 32) | ((__u64)(___swab32(h)));
  68. #else
  69. return __const_swab64(val);
  70. #endif
  71. }
  72. static inline __attribute_const__ __u32 ___swahw32(__u32 val)
  73. {
  74. #ifdef __arch_swahw32
  75. return __arch_swahw32(val);
  76. #elif defined(__arch_swahw32p)
  77. return __arch_swahw32p(&val);
  78. #else
  79. return __const_swahw32(val);
  80. #endif
  81. }
  82. static inline __attribute_const__ __u32 ___swahb32(__u32 val)
  83. {
  84. #ifdef __arch_swahb32
  85. return __arch_swahb32(val);
  86. #elif defined(__arch_swahb32p)
  87. return __arch_swahb32p(&val);
  88. #else
  89. return __const_swahb32(val);
  90. #endif
  91. }
  92. /**
  93. * __swab16 - return a byteswapped 16-bit value
  94. * @x: value to byteswap
  95. */
  96. #define __swab16(x) \
  97. (__builtin_constant_p((__u16)(x)) ? \
  98. __const_swab16((x)) : \
  99. ___swab16((x)))
  100. /**
  101. * __swab32 - return a byteswapped 32-bit value
  102. * @x: value to byteswap
  103. */
  104. #define __swab32(x) \
  105. (__builtin_constant_p((__u32)(x)) ? \
  106. __const_swab32((x)) : \
  107. ___swab32((x)))
  108. /**
  109. * __swab64 - return a byteswapped 64-bit value
  110. * @x: value to byteswap
  111. */
  112. #define __swab64(x) \
  113. (__builtin_constant_p((__u64)(x)) ? \
  114. __const_swab64((x)) : \
  115. ___swab64((x)))
  116. /**
  117. * __swahw32 - return a word-swapped 32-bit value
  118. * @x: value to wordswap
  119. *
  120. * __swahw32(0x12340000) is 0x00001234
  121. */
  122. #define __swahw32(x) \
  123. (__builtin_constant_p((__u32)(x)) ? \
  124. __const_swahw32((x)) : \
  125. ___swahw32((x)))
  126. /**
  127. * __swahb32 - return a high and low byte-swapped 32-bit value
  128. * @x: value to byteswap
  129. *
  130. * __swahb32(0x12345678) is 0x34127856
  131. */
  132. #define __swahb32(x) \
  133. (__builtin_constant_p((__u32)(x)) ? \
  134. __const_swahb32((x)) : \
  135. ___swahb32((x)))
  136. /**
  137. * __swab16p - return a byteswapped 16-bit value from a pointer
  138. * @p: pointer to a naturally-aligned 16-bit value
  139. */
  140. static inline __u16 __swab16p(const __u16 *p)
  141. {
  142. #ifdef __arch_swab16p
  143. return __arch_swab16p(p);
  144. #else
  145. return __swab16(*p);
  146. #endif
  147. }
  148. /**
  149. * __swab32p - return a byteswapped 32-bit value from a pointer
  150. * @p: pointer to a naturally-aligned 32-bit value
  151. */
  152. static inline __u32 __swab32p(const __u32 *p)
  153. {
  154. #ifdef __arch_swab32p
  155. return __arch_swab32p(p);
  156. #else
  157. return __swab32(*p);
  158. #endif
  159. }
  160. /**
  161. * __swab64p - return a byteswapped 64-bit value from a pointer
  162. * @p: pointer to a naturally-aligned 64-bit value
  163. */
  164. static inline __u64 __swab64p(const __u64 *p)
  165. {
  166. #ifdef __arch_swab64p
  167. return __arch_swab64p(p);
  168. #else
  169. return __swab64(*p);
  170. #endif
  171. }
  172. /**
  173. * __swahw32p - return a wordswapped 32-bit value from a pointer
  174. * @p: pointer to a naturally-aligned 32-bit value
  175. *
  176. * See __swahw32() for details of wordswapping.
  177. */
  178. static inline __u32 __swahw32p(const __u32 *p)
  179. {
  180. #ifdef __arch_swahw32p
  181. return __arch_swahw32p(p);
  182. #else
  183. return __swahw32(*p);
  184. #endif
  185. }
  186. /**
  187. * __swahb32p - return a high and low byteswapped 32-bit value from a pointer
  188. * @p: pointer to a naturally-aligned 32-bit value
  189. *
  190. * See __swahb32() for details of high/low byteswapping.
  191. */
  192. static inline __u32 __swahb32p(const __u32 *p)
  193. {
  194. #ifdef __arch_swahb32p
  195. return __arch_swahb32p(p);
  196. #else
  197. return __swahb32(*p);
  198. #endif
  199. }
  200. /**
  201. * __swab16s - byteswap a 16-bit value in-place
  202. * @p: pointer to a naturally-aligned 16-bit value
  203. */
  204. static inline void __swab16s(__u16 *p)
  205. {
  206. #ifdef __arch_swab16s
  207. __arch_swab16s(p);
  208. #else
  209. *p = __swab16p(p);
  210. #endif
  211. }
  212. /**
  213. * __swab32s - byteswap a 32-bit value in-place
  214. * @p: pointer to a naturally-aligned 32-bit value
  215. */
  216. static inline void __swab32s(__u32 *p)
  217. {
  218. #ifdef __arch_swab32s
  219. __arch_swab32s(p);
  220. #else
  221. *p = __swab32p(p);
  222. #endif
  223. }
  224. /**
  225. * __swab64s - byteswap a 64-bit value in-place
  226. * @p: pointer to a naturally-aligned 64-bit value
  227. */
  228. static inline void __swab64s(__u64 *p)
  229. {
  230. #ifdef __arch_swab64s
  231. __arch_swab64s(p);
  232. #else
  233. *p = __swab64p(p);
  234. #endif
  235. }
  236. /**
  237. * __swahw32s - wordswap a 32-bit value in-place
  238. * @p: pointer to a naturally-aligned 32-bit value
  239. *
  240. * See __swahw32() for details of wordswapping
  241. */
  242. static inline void __swahw32s(__u32 *p)
  243. {
  244. #ifdef __arch_swahw32s
  245. __arch_swahw32s(p);
  246. #else
  247. *p = __swahw32p(p);
  248. #endif
  249. }
  250. /**
  251. * __swahb32s - high and low byteswap a 32-bit value in-place
  252. * @p: pointer to a naturally-aligned 32-bit value
  253. *
  254. * See __swahb32() for details of high and low byte swapping
  255. */
  256. static inline void __swahb32s(__u32 *p)
  257. {
  258. #ifdef __arch_swahb32s
  259. __arch_swahb32s(p);
  260. #else
  261. *p = __swahb32p(p);
  262. #endif
  263. }
  264. #ifdef __KERNEL__
  265. # define swab16 __swab16
  266. # define swab32 __swab32
  267. # define swab64 __swab64
  268. # define swahw32 __swahw32
  269. # define swahb32 __swahb32
  270. # define swab16p __swab16p
  271. # define swab32p __swab32p
  272. # define swab64p __swab64p
  273. # define swahw32p __swahw32p
  274. # define swahb32p __swahb32p
  275. # define swab16s __swab16s
  276. # define swab32s __swab32s
  277. # define swab64s __swab64s
  278. # define swahw32s __swahw32s
  279. # define swahb32s __swahb32s
  280. #endif /* __KERNEL__ */
  281. #endif /* _LINUX_SWAB_H */