swab.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #ifndef _LINUX_BYTEORDER_SWAB_H
  2. #define _LINUX_BYTEORDER_SWAB_H
  3. /*
  4. * linux/byteorder/swab.h
  5. * Byte-swapping, independently from CPU endianness
  6. * swabXX[ps]?(foo)
  7. *
  8. * Francois-Rene Rideau <fare@tunes.org> 19971205
  9. * separated swab functions from cpu_to_XX,
  10. * to clean up support for bizarre-endian architectures.
  11. *
  12. * See asm-i386/byteorder.h and suches for examples of how to provide
  13. * architecture-dependent optimized versions
  14. *
  15. */
  16. #include <linux/compiler.h>
  17. /* casts are necessary for constants, because we never know how for sure
  18. * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
  19. */
  20. #define ___swab16(x) \
  21. ({ \
  22. __u16 __x = (x); \
  23. ((__u16)( \
  24. (((__u16)(__x) & (__u16)0x00ffU) << 8) | \
  25. (((__u16)(__x) & (__u16)0xff00U) >> 8) )); \
  26. })
  27. #define ___swab32(x) \
  28. ({ \
  29. __u32 __x = (x); \
  30. ((__u32)( \
  31. (((__u32)(__x) & (__u32)0x000000ffUL) << 24) | \
  32. (((__u32)(__x) & (__u32)0x0000ff00UL) << 8) | \
  33. (((__u32)(__x) & (__u32)0x00ff0000UL) >> 8) | \
  34. (((__u32)(__x) & (__u32)0xff000000UL) >> 24) )); \
  35. })
  36. #define ___swab64(x) \
  37. ({ \
  38. __u64 __x = (x); \
  39. ((__u64)( \
  40. (__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \
  41. (__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \
  42. (__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) | \
  43. (__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) << 8) | \
  44. (__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >> 8) | \
  45. (__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
  46. (__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) | \
  47. (__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) )); \
  48. })
  49. #define ___constant_swab16(x) \
  50. ((__u16)( \
  51. (((__u16)(x) & (__u16)0x00ffU) << 8) | \
  52. (((__u16)(x) & (__u16)0xff00U) >> 8) ))
  53. #define ___constant_swab32(x) \
  54. ((__u32)( \
  55. (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
  56. (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
  57. (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
  58. (((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
  59. #define ___constant_swab64(x) \
  60. ((__u64)( \
  61. (__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
  62. (__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
  63. (__u64)(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
  64. (__u64)(((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \
  65. (__u64)(((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \
  66. (__u64)(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
  67. (__u64)(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
  68. (__u64)(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56) ))
  69. /*
  70. * provide defaults when no architecture-specific optimization is detected
  71. */
  72. #ifndef __arch__swab16
  73. # define __arch__swab16(x) ({ __u16 __tmp = (x) ; ___swab16(__tmp); })
  74. #endif
  75. #ifndef __arch__swab32
  76. # define __arch__swab32(x) ({ __u32 __tmp = (x) ; ___swab32(__tmp); })
  77. #endif
  78. #ifndef __arch__swab64
  79. # define __arch__swab64(x) ({ __u64 __tmp = (x) ; ___swab64(__tmp); })
  80. #endif
  81. #ifndef __arch__swab16p
  82. # define __arch__swab16p(x) __arch__swab16(*(x))
  83. #endif
  84. #ifndef __arch__swab32p
  85. # define __arch__swab32p(x) __arch__swab32(*(x))
  86. #endif
  87. #ifndef __arch__swab64p
  88. # define __arch__swab64p(x) __arch__swab64(*(x))
  89. #endif
  90. #ifndef __arch__swab16s
  91. # define __arch__swab16s(x) do { *(x) = __arch__swab16p((x)); } while (0)
  92. #endif
  93. #ifndef __arch__swab32s
  94. # define __arch__swab32s(x) do { *(x) = __arch__swab32p((x)); } while (0)
  95. #endif
  96. #ifndef __arch__swab64s
  97. # define __arch__swab64s(x) do { *(x) = __arch__swab64p((x)); } while (0)
  98. #endif
  99. /*
  100. * Allow constant folding
  101. */
  102. #if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__)
  103. # define __swab16(x) \
  104. (__builtin_constant_p((__u16)(x)) ? \
  105. ___swab16((x)) : \
  106. __fswab16((x)))
  107. # define __swab32(x) \
  108. (__builtin_constant_p((__u32)(x)) ? \
  109. ___swab32((x)) : \
  110. __fswab32((x)))
  111. # define __swab64(x) \
  112. (__builtin_constant_p((__u64)(x)) ? \
  113. ___swab64((x)) : \
  114. __fswab64((x)))
  115. #else
  116. # define __swab16(x) __fswab16(x)
  117. # define __swab32(x) __fswab32(x)
  118. # define __swab64(x) __fswab64(x)
  119. #endif /* OPTIMIZE */
  120. static __inline__ __attribute_const__ __u16 __fswab16(__u16 x)
  121. {
  122. return __arch__swab16(x);
  123. }
  124. static __inline__ __u16 __swab16p(const __u16 *x)
  125. {
  126. return __arch__swab16p(x);
  127. }
  128. static __inline__ void __swab16s(__u16 *addr)
  129. {
  130. __arch__swab16s(addr);
  131. }
  132. static __inline__ __attribute_const__ __u32 __fswab32(__u32 x)
  133. {
  134. return __arch__swab32(x);
  135. }
  136. static __inline__ __u32 __swab32p(const __u32 *x)
  137. {
  138. return __arch__swab32p(x);
  139. }
  140. static __inline__ void __swab32s(__u32 *addr)
  141. {
  142. __arch__swab32s(addr);
  143. }
  144. #ifdef __BYTEORDER_HAS_U64__
  145. static __inline__ __attribute_const__ __u64 __fswab64(__u64 x)
  146. {
  147. # ifdef __SWAB_64_THRU_32__
  148. __u32 h = x >> 32;
  149. __u32 l = x & ((1ULL<<32)-1);
  150. return (((__u64)__swab32(l)) << 32) | ((__u64)(__swab32(h)));
  151. # else
  152. return __arch__swab64(x);
  153. # endif
  154. }
  155. static __inline__ __u64 __swab64p(const __u64 *x)
  156. {
  157. return __arch__swab64p(x);
  158. }
  159. static __inline__ void __swab64s(__u64 *addr)
  160. {
  161. __arch__swab64s(addr);
  162. }
  163. #endif /* __BYTEORDER_HAS_U64__ */
  164. #if defined(__KERNEL__)
  165. #define swab16 __swab16
  166. #define swab32 __swab32
  167. #define swab64 __swab64
  168. #define swab16p __swab16p
  169. #define swab32p __swab32p
  170. #define swab64p __swab64p
  171. #define swab16s __swab16s
  172. #define swab32s __swab32s
  173. #define swab64s __swab64s
  174. #endif
  175. #endif /* _LINUX_BYTEORDER_SWAB_H */