swab.h 6.4 KB

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