swabb.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <linux/types.h>
  26. #define ___swahw32(x) \
  27. ({ \
  28. __u32 __x = (x); \
  29. ((__u32)( \
  30. (((__u32)(__x) & (__u32)0x0000ffffUL) << 16) | \
  31. (((__u32)(__x) & (__u32)0xffff0000UL) >> 16) )); \
  32. })
  33. #define ___swahb32(x) \
  34. ({ \
  35. __u32 __x = (x); \
  36. ((__u32)( \
  37. (((__u32)(__x) & (__u32)0x00ff00ffUL) << 8) | \
  38. (((__u32)(__x) & (__u32)0xff00ff00UL) >> 8) )); \
  39. })
  40. #define ___constant_swahw32(x) \
  41. ((__u32)( \
  42. (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
  43. (((__u32)(x) & (__u32)0xffff0000UL) >> 16) ))
  44. #define ___constant_swahb32(x) \
  45. ((__u32)( \
  46. (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
  47. (((__u32)(x) & (__u32)0xff00ff00UL) >> 8) ))
  48. /*
  49. * provide defaults when no architecture-specific optimization is detected
  50. */
  51. #ifndef __arch__swahw32
  52. # define __arch__swahw32(x) ___swahw32(x)
  53. #endif
  54. #ifndef __arch__swahb32
  55. # define __arch__swahb32(x) ___swahb32(x)
  56. #endif
  57. #ifndef __arch__swahw32p
  58. # define __arch__swahw32p(x) __swahw32(*(x))
  59. #endif
  60. #ifndef __arch__swahb32p
  61. # define __arch__swahb32p(x) __swahb32(*(x))
  62. #endif
  63. #ifndef __arch__swahw32s
  64. # define __arch__swahw32s(x) do { *(x) = __swahw32p((x)); } while (0)
  65. #endif
  66. #ifndef __arch__swahb32s
  67. # define __arch__swahb32s(x) do { *(x) = __swahb32p((x)); } while (0)
  68. #endif
  69. /*
  70. * Allow constant folding
  71. */
  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. static inline __u32 __fswahw32(__u32 x)
  81. {
  82. return __arch__swahw32(x);
  83. }
  84. static inline __u32 __swahw32p(__u32 *x)
  85. {
  86. return __arch__swahw32p(x);
  87. }
  88. static inline void __swahw32s(__u32 *addr)
  89. {
  90. __arch__swahw32s(addr);
  91. }
  92. static inline __u32 __fswahb32(__u32 x)
  93. {
  94. return __arch__swahb32(x);
  95. }
  96. static inline __u32 __swahb32p(__u32 *x)
  97. {
  98. return __arch__swahb32p(x);
  99. }
  100. static inline void __swahb32s(__u32 *addr)
  101. {
  102. __arch__swahb32s(addr);
  103. }
  104. #ifdef __BYTEORDER_HAS_U64__
  105. /*
  106. * Not supported yet
  107. */
  108. #endif /* __BYTEORDER_HAS_U64__ */
  109. #define swahw32 __swahw32
  110. #define swahb32 __swahb32
  111. #define swahw32p __swahw32p
  112. #define swahb32p __swahb32p
  113. #define swahw32s __swahw32s
  114. #define swahb32s __swahb32s
  115. #endif /* _LINUX_BYTEORDER_SWABB_H */