swab.h 6.1 KB

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