byteswap.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Macros to swap the order of bytes in integer values.
  2. Copyright (C) 1997-2016 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #if !defined _BYTESWAP_H && !defined _NETINET_IN_H && !defined _ENDIAN_H
  16. # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
  17. #endif
  18. #ifndef _BITS_BYTESWAP_H
  19. #define _BITS_BYTESWAP_H 1
  20. #include <features.h>
  21. #include <bits/types.h>
  22. #include <bits/wordsize.h>
  23. /* Swap bytes in 16 bit value. */
  24. #define __bswap_constant_16(x) \
  25. ((unsigned short int) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
  26. /* Get __bswap_16. */
  27. #include <bits/byteswap-16.h>
  28. /* Swap bytes in 32 bit value. */
  29. #define __bswap_constant_32(x) \
  30. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  31. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  32. #ifdef __GNUC__
  33. # if __GNUC_PREREQ (4, 3)
  34. static __inline unsigned int
  35. __bswap_32 (unsigned int __bsx)
  36. {
  37. return __builtin_bswap32 (__bsx);
  38. }
  39. # elif __GNUC__ >= 2
  40. # if __WORDSIZE == 64 || (defined __i486__ || defined __pentium__ \
  41. || defined __pentiumpro__ || defined __pentium4__ \
  42. || defined __k8__ || defined __athlon__ \
  43. || defined __k6__ || defined __nocona__ \
  44. || defined __core2__ || defined __geode__ \
  45. || defined __amdfam10__)
  46. /* To swap the bytes in a word the i486 processors and up provide the
  47. `bswap' opcode. On i386 we have to use three instructions. */
  48. # define __bswap_32(x) \
  49. (__extension__ \
  50. ({ unsigned int __v, __x = (x); \
  51. if (__builtin_constant_p (__x)) \
  52. __v = __bswap_constant_32 (__x); \
  53. else \
  54. __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); \
  55. __v; }))
  56. # else
  57. # define __bswap_32(x) \
  58. (__extension__ \
  59. ({ unsigned int __v, __x = (x); \
  60. if (__builtin_constant_p (__x)) \
  61. __v = __bswap_constant_32 (__x); \
  62. else \
  63. __asm__ ("rorw $8, %w0;" \
  64. "rorl $16, %0;" \
  65. "rorw $8, %w0" \
  66. : "=r" (__v) \
  67. : "0" (__x) \
  68. : "cc"); \
  69. __v; }))
  70. # endif
  71. # else
  72. # define __bswap_32(x) \
  73. (__extension__ \
  74. ({ unsigned int __x = (x); __bswap_constant_32 (__x); }))
  75. # endif
  76. #else
  77. static __inline unsigned int
  78. __bswap_32 (unsigned int __bsx)
  79. {
  80. return __bswap_constant_32 (__bsx);
  81. }
  82. #endif
  83. #if __GNUC_PREREQ (2, 0)
  84. /* Swap bytes in 64 bit value. */
  85. # define __bswap_constant_64(x) \
  86. (__extension__ ((((x) & 0xff00000000000000ull) >> 56) \
  87. | (((x) & 0x00ff000000000000ull) >> 40) \
  88. | (((x) & 0x0000ff0000000000ull) >> 24) \
  89. | (((x) & 0x000000ff00000000ull) >> 8) \
  90. | (((x) & 0x00000000ff000000ull) << 8) \
  91. | (((x) & 0x0000000000ff0000ull) << 24) \
  92. | (((x) & 0x000000000000ff00ull) << 40) \
  93. | (((x) & 0x00000000000000ffull) << 56)))
  94. # if __GNUC_PREREQ (4, 3)
  95. static __inline __uint64_t
  96. __bswap_64 (__uint64_t __bsx)
  97. {
  98. return __builtin_bswap64 (__bsx);
  99. }
  100. # elif __WORDSIZE == 64
  101. # define __bswap_64(x) \
  102. (__extension__ \
  103. ({ __uint64_t __v, __x = (x); \
  104. if (__builtin_constant_p (__x)) \
  105. __v = __bswap_constant_64 (__x); \
  106. else \
  107. __asm__ ("bswap %q0" : "=r" (__v) : "0" (__x)); \
  108. __v; }))
  109. # else
  110. # define __bswap_64(x) \
  111. (__extension__ \
  112. ({ union { __extension__ __uint64_t __ll; \
  113. unsigned int __l[2]; } __w, __r; \
  114. if (__builtin_constant_p (x)) \
  115. __r.__ll = __bswap_constant_64 (x); \
  116. else \
  117. { \
  118. __w.__ll = (x); \
  119. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  120. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  121. } \
  122. __r.__ll; }))
  123. # endif
  124. #else
  125. # define __bswap_constant_64(x) \
  126. ((((x) & 0xff00000000000000ull) >> 56) \
  127. | (((x) & 0x00ff000000000000ull) >> 40) \
  128. | (((x) & 0x0000ff0000000000ull) >> 24) \
  129. | (((x) & 0x000000ff00000000ull) >> 8) \
  130. | (((x) & 0x00000000ff000000ull) << 8) \
  131. | (((x) & 0x0000000000ff0000ull) << 24) \
  132. | (((x) & 0x000000000000ff00ull) << 40) \
  133. | (((x) & 0x00000000000000ffull) << 56))
  134. static __inline __uint64_t
  135. __bswap_64 (__uint64_t __bsx)
  136. {
  137. return __bswap_constant_64 (__bsx);
  138. }
  139. #endif
  140. #endif /* _BITS_BYTESWAP_H */