swab.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /* casts are necessary for constants, because we never know how for sure
  17. * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
  18. */
  19. #define ___swab16(x) \
  20. ((__u16)( \
  21. (((__u16)(x) & (__u16)0x00ffU) << 8) | \
  22. (((__u16)(x) & (__u16)0xff00U) >> 8) ))
  23. #define ___swab32(x) \
  24. ((__u32)( \
  25. (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
  26. (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
  27. (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
  28. (((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
  29. #define ___swab64(x) \
  30. ((__u64)( \
  31. (__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
  32. (__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
  33. (__u64)(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \
  34. (__u64)(((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \
  35. (__u64)(((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \
  36. (__u64)(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
  37. (__u64)(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \
  38. (__u64)(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56) ))
  39. /*
  40. * provide defaults when no architecture-specific optimization is detected
  41. */
  42. #ifndef __arch__swab16
  43. # define __arch__swab16(x) ___swab16(x)
  44. #endif
  45. #ifndef __arch__swab32
  46. # define __arch__swab32(x) ___swab32(x)
  47. #endif
  48. #ifndef __arch__swab64
  49. # define __arch__swab64(x) ___swab64(x)
  50. #endif
  51. #ifndef __arch__swab16p
  52. # define __arch__swab16p(x) __swab16(*(x))
  53. #endif
  54. #ifndef __arch__swab32p
  55. # define __arch__swab32p(x) __swab32(*(x))
  56. #endif
  57. #ifndef __arch__swab64p
  58. # define __arch__swab64p(x) __swab64(*(x))
  59. #endif
  60. #ifndef __arch__swab16s
  61. # define __arch__swab16s(x) do { *(x) = __swab16p((x)); } while (0)
  62. #endif
  63. #ifndef __arch__swab32s
  64. # define __arch__swab32s(x) do { *(x) = __swab32p((x)); } while (0)
  65. #endif
  66. #ifndef __arch__swab64s
  67. # define __arch__swab64s(x) do { *(x) = __swab64p((x)); } while (0)
  68. #endif
  69. /*
  70. * Allow constant folding
  71. */
  72. #if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__)
  73. # define __swab16(x) \
  74. (__builtin_constant_p((__u16)(x)) ? \
  75. ___swab16((x)) : \
  76. __fswab16((x)))
  77. # define __swab32(x) \
  78. (__builtin_constant_p((__u32)(x)) ? \
  79. ___swab32((x)) : \
  80. __fswab32((x)))
  81. # define __swab64(x) \
  82. (__builtin_constant_p((__u64)(x)) ? \
  83. ___swab64((x)) : \
  84. __fswab64((x)))
  85. #else
  86. # define __swab16(x) __fswab16(x)
  87. # define __swab32(x) __fswab32(x)
  88. # define __swab64(x) __fswab64(x)
  89. #endif /* OPTIMIZE */
  90. static __inline__ __attribute__((const)) __u16 __fswab16(__u16 x)
  91. {
  92. return __arch__swab16(x);
  93. }
  94. static __inline__ __u16 __swab16p(const __u16 *x)
  95. {
  96. return __arch__swab16p(x);
  97. }
  98. static __inline__ void __swab16s(__u16 *addr)
  99. {
  100. __arch__swab16s(addr);
  101. }
  102. static __inline__ __attribute__((const)) __u32 __fswab32(__u32 x)
  103. {
  104. return __arch__swab32(x);
  105. }
  106. static __inline__ __u32 __swab32p(const __u32 *x)
  107. {
  108. return __arch__swab32p(x);
  109. }
  110. static __inline__ void __swab32s(__u32 *addr)
  111. {
  112. __arch__swab32s(addr);
  113. }
  114. static __inline__ __attribute__((const)) __u64 __fswab64(__u64 x)
  115. {
  116. # ifdef __SWAB_64_THRU_32__
  117. __u32 h = x >> 32;
  118. __u32 l = x & ((1ULL<<32)-1);
  119. return (((__u64)__swab32(l)) << 32) | ((__u64)(__swab32(h)));
  120. # else
  121. return __arch__swab64(x);
  122. # endif
  123. }
  124. static __inline__ __u64 __swab64p(const __u64 *x)
  125. {
  126. return __arch__swab64p(x);
  127. }
  128. static __inline__ void __swab64s(__u64 *addr)
  129. {
  130. __arch__swab64s(addr);
  131. }
  132. #if defined(__KERNEL__)
  133. #define swab16 __swab16
  134. #define swab32 __swab32
  135. #define swab64 __swab64
  136. #define swab16p __swab16p
  137. #define swab32p __swab32p
  138. #define swab64p __swab64p
  139. #define swab16s __swab16s
  140. #define swab32s __swab32s
  141. #define swab64s __swab64s
  142. #endif
  143. #endif /* _LINUX_BYTEORDER_SWAB_H */