swabb.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef _LINUX_BYTEORDER_SWABB_H
  2. #define _LINUX_BYTEORDER_SWABB_H
  3. /*
  4. * linux/byteorder/swabb.h
  5. * SWAp Bytes Bizarrely
  6. * swaHHXX[ps]?(foo)
  7. *
  8. * Support for obNUXIous pdp-endian and other bizarre architectures.
  9. * Will Linux ever run on such ancient beasts? if not, this file
  10. * will be but a programming pearl. Still, it's a reminder that we
  11. * shouldn't be making too many assumptions when trying to be portable.
  12. *
  13. */
  14. /*
  15. * Meaning of the names I chose (vaxlinux people feel free to correct them):
  16. * swahw32 swap 16-bit half-words in a 32-bit word
  17. * swahb32 swap 8-bit halves of each 16-bit half-word in a 32-bit word
  18. *
  19. * No 64-bit support yet. I don't know NUXI conventions for long longs.
  20. * I guarantee it will be a mess when it's there, though :->
  21. * It will be even worse if there are conflicting 64-bit conventions.
  22. * Hopefully, no one ever used 64-bit objects on NUXI machines.
  23. *
  24. */
  25. #define ___swahw32(x) \
  26. ({ \
  27. __u32 __x = (x); \
  28. ((__u32)( \
  29. (((__u32)(__x) & (__u32)0x0000ffffUL) << 16) | \
  30. (((__u32)(__x) & (__u32)0xffff0000UL) >> 16) )); \
  31. })
  32. #define ___swahb32(x) \
  33. ({ \
  34. __u32 __x = (x); \
  35. ((__u32)( \
  36. (((__u32)(__x) & (__u32)0x00ff00ffUL) << 8) | \
  37. (((__u32)(__x) & (__u32)0xff00ff00UL) >> 8) )); \
  38. })
  39. #define ___constant_swahw32(x) \
  40. ((__u32)( \
  41. (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
  42. (((__u32)(x) & (__u32)0xffff0000UL) >> 16) ))
  43. #define ___constant_swahb32(x) \
  44. ((__u32)( \
  45. (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
  46. (((__u32)(x) & (__u32)0xff00ff00UL) >> 8) ))
  47. /*
  48. * provide defaults when no architecture-specific optimization is detected
  49. */
  50. #ifndef __arch__swahw32
  51. # define __arch__swahw32(x) ___swahw32(x)
  52. #endif
  53. #ifndef __arch__swahb32
  54. # define __arch__swahb32(x) ___swahb32(x)
  55. #endif
  56. #ifndef __arch__swahw32p
  57. # define __arch__swahw32p(x) __swahw32(*(x))
  58. #endif
  59. #ifndef __arch__swahb32p
  60. # define __arch__swahb32p(x) __swahb32(*(x))
  61. #endif
  62. #ifndef __arch__swahw32s
  63. # define __arch__swahw32s(x) do { *(x) = __swahw32p((x)); } while (0)
  64. #endif
  65. #ifndef __arch__swahb32s
  66. # define __arch__swahb32s(x) do { *(x) = __swahb32p((x)); } while (0)
  67. #endif
  68. /*
  69. * Allow constant folding
  70. */
  71. #if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__)
  72. # define __swahw32(x) \
  73. (__builtin_constant_p((__u32)(x)) ? \
  74. ___swahw32((x)) : \
  75. __fswahw32((x)))
  76. # define __swahb32(x) \
  77. (__builtin_constant_p((__u32)(x)) ? \
  78. ___swahb32((x)) : \
  79. __fswahb32((x)))
  80. #else
  81. # define __swahw32(x) __fswahw32(x)
  82. # define __swahb32(x) __fswahb32(x)
  83. #endif /* OPTIMIZE */
  84. static inline __u32 __fswahw32(__u32 x)
  85. {
  86. return __arch__swahw32(x);
  87. }
  88. static inline __u32 __swahw32p(__u32 *x)
  89. {
  90. return __arch__swahw32p(x);
  91. }
  92. static inline void __swahw32s(__u32 *addr)
  93. {
  94. __arch__swahw32s(addr);
  95. }
  96. static inline __u32 __fswahb32(__u32 x)
  97. {
  98. return __arch__swahb32(x);
  99. }
  100. static inline __u32 __swahb32p(__u32 *x)
  101. {
  102. return __arch__swahb32p(x);
  103. }
  104. static inline void __swahb32s(__u32 *addr)
  105. {
  106. __arch__swahb32s(addr);
  107. }
  108. #ifdef __BYTEORDER_HAS_U64__
  109. /*
  110. * Not supported yet
  111. */
  112. #endif /* __BYTEORDER_HAS_U64__ */
  113. #if defined(__KERNEL__)
  114. #define swahw32 __swahw32
  115. #define swahb32 __swahb32
  116. #define swahw32p __swahw32p
  117. #define swahb32p __swahb32p
  118. #define swahw32s __swahw32s
  119. #define swahb32s __swahb32s
  120. #endif
  121. #endif /* _LINUX_BYTEORDER_SWABB_H */