swabb.h 3.9 KB

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