swabb.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. *
  19. ******************************************************************************/
  20. #ifndef _LINUX_BYTEORDER_SWABB_H
  21. #define _LINUX_BYTEORDER_SWABB_H
  22. /*
  23. * linux/byteorder/swabb.h
  24. * SWAp Bytes Bizarrely
  25. * swaHHXX[ps]?(foo)
  26. *
  27. * Support for obNUXIous pdp-endian and other bizarre architectures.
  28. * Will Linux ever run on such ancient beasts? if not, this file
  29. * will be but a programming pearl. Still, it's a reminder that we
  30. * shouldn't be making too many assumptions when trying to be portable.
  31. *
  32. */
  33. /*
  34. * Meaning of the names I chose (vaxlinux people feel free to correct them):
  35. * swahw32 swap 16-bit half-words in a 32-bit word
  36. * swahb32 swap 8-bit halves of each 16-bit half-word in a 32-bit word
  37. *
  38. * No 64-bit support yet. I don't know NUXI conventions for long longs.
  39. * I guarantee it will be a mess when it's there, though :->
  40. * It will be even worse if there are conflicting 64-bit conventions.
  41. * Hopefully, no one ever used 64-bit objects on NUXI machines.
  42. *
  43. */
  44. #define ___swahw32(x) \
  45. ({ \
  46. __u32 __x = (x); \
  47. ((__u32)( \
  48. (((__u32)(__x) & (__u32)0x0000ffffUL) << 16) | \
  49. (((__u32)(__x) & (__u32)0xffff0000UL) >> 16) )); \
  50. })
  51. #define ___swahb32(x) \
  52. ({ \
  53. __u32 __x = (x); \
  54. ((__u32)( \
  55. (((__u32)(__x) & (__u32)0x00ff00ffUL) << 8) | \
  56. (((__u32)(__x) & (__u32)0xff00ff00UL) >> 8) )); \
  57. })
  58. #define ___constant_swahw32(x) \
  59. ((__u32)( \
  60. (((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
  61. (((__u32)(x) & (__u32)0xffff0000UL) >> 16) ))
  62. #define ___constant_swahb32(x) \
  63. ((__u32)( \
  64. (((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
  65. (((__u32)(x) & (__u32)0xff00ff00UL) >> 8) ))
  66. /*
  67. * provide defaults when no architecture-specific optimization is detected
  68. */
  69. #ifndef __arch__swahw32
  70. # define __arch__swahw32(x) ___swahw32(x)
  71. #endif
  72. #ifndef __arch__swahb32
  73. # define __arch__swahb32(x) ___swahb32(x)
  74. #endif
  75. #ifndef __arch__swahw32p
  76. # define __arch__swahw32p(x) __swahw32(*(x))
  77. #endif
  78. #ifndef __arch__swahb32p
  79. # define __arch__swahb32p(x) __swahb32(*(x))
  80. #endif
  81. #ifndef __arch__swahw32s
  82. # define __arch__swahw32s(x) do { *(x) = __swahw32p((x)); } while (0)
  83. #endif
  84. #ifndef __arch__swahb32s
  85. # define __arch__swahb32s(x) do { *(x) = __swahb32p((x)); } while (0)
  86. #endif
  87. /*
  88. * Allow constant folding
  89. */
  90. #if defined(__GNUC__) && (__GNUC__ >= 2) && defined(__OPTIMIZE__)
  91. # define __swahw32(x) \
  92. (__builtin_constant_p((__u32)(x)) ? \
  93. ___swahw32((x)) : \
  94. __fswahw32((x)))
  95. # define __swahb32(x) \
  96. (__builtin_constant_p((__u32)(x)) ? \
  97. ___swahb32((x)) : \
  98. __fswahb32((x)))
  99. #else
  100. # define __swahw32(x) __fswahw32(x)
  101. # define __swahb32(x) __fswahb32(x)
  102. #endif /* OPTIMIZE */
  103. __inline static__ __const__ __u32 __fswahw32(__u32 x)
  104. {
  105. return __arch__swahw32(x);
  106. }
  107. __inline static__ __u32 __swahw32p(__u32 *x)
  108. {
  109. return __arch__swahw32p(x);
  110. }
  111. __inline static__ void __swahw32s(__u32 *addr)
  112. {
  113. __arch__swahw32s(addr);
  114. }
  115. __inline static__ __const__ __u32 __fswahb32(__u32 x)
  116. {
  117. return __arch__swahb32(x);
  118. }
  119. __inline static__ __u32 __swahb32p(__u32 *x)
  120. {
  121. return __arch__swahb32p(x);
  122. }
  123. __inline static__ void __swahb32s(__u32 *addr)
  124. {
  125. __arch__swahb32s(addr);
  126. }
  127. #ifdef __BYTEORDER_HAS_U64__
  128. /*
  129. * Not supported yet
  130. */
  131. #endif /* __BYTEORDER_HAS_U64__ */
  132. #if defined(PLATFORM_LINUX)
  133. #define swahw32 __swahw32
  134. #define swahb32 __swahb32
  135. #define swahw32p __swahw32p
  136. #define swahb32p __swahb32p
  137. #define swahw32s __swahw32s
  138. #define swahb32s __swahb32s
  139. #endif
  140. #endif /* _LINUX_BYTEORDER_SWABB_H */