byteswap.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Macros to swap the order of bytes in integer values.
  2. Copyright (C) 1997, 1998, 2000, 2002, 2003 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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #if !defined _BYTESWAP_H && !defined _NETINET_IN_H
  17. # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
  18. #endif
  19. #ifndef _BITS_BYTESWAP_H
  20. #define _BITS_BYTESWAP_H 1
  21. /* Swap bytes in 16 bit value. */
  22. #define __bswap_constant_16(x) \
  23. ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
  24. #ifdef __GNUC__
  25. # if __GNUC__ >= 2
  26. # define __bswap_16(x) \
  27. (__extension__ \
  28. ({ register unsigned short int __v, __x = (x); \
  29. if (__builtin_constant_p (__x)) \
  30. __v = __bswap_constant_16 (__x); \
  31. else \
  32. __asm__ ("rorw $8, %w0" \
  33. : "=r" (__v) \
  34. : "0" (__x) \
  35. : "cc"); \
  36. __v; }))
  37. # else
  38. /* This is better than nothing. */
  39. # define __bswap_16(x) \
  40. (__extension__ \
  41. ({ register unsigned short int __x = (x); __bswap_constant_16 (__x); }))
  42. # endif
  43. #else
  44. static __inline unsigned short int
  45. __bswap_16 (unsigned short int __bsx)
  46. {
  47. return __bswap_constant_16 (__bsx);
  48. }
  49. #endif
  50. /* Swap bytes in 32 bit value. */
  51. #define __bswap_constant_32(x) \
  52. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  53. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  54. #ifdef __GNUC__
  55. # if __GNUC__ >= 2
  56. /* To swap the bytes in a word the i486 processors and up provide the
  57. `bswap' opcode. On i386 we have to use three instructions. */
  58. # if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__ \
  59. && !defined __pentium4__
  60. # define __bswap_32(x) \
  61. (__extension__ \
  62. ({ register unsigned int __v, __x = (x); \
  63. if (__builtin_constant_p (__x)) \
  64. __v = __bswap_constant_32 (__x); \
  65. else \
  66. __asm__ ("rorw $8, %w0;" \
  67. "rorl $16, %0;" \
  68. "rorw $8, %w0" \
  69. : "=r" (__v) \
  70. : "0" (__x) \
  71. : "cc"); \
  72. __v; }))
  73. # else
  74. # define __bswap_32(x) \
  75. (__extension__ \
  76. ({ register unsigned int __v, __x = (x); \
  77. if (__builtin_constant_p (__x)) \
  78. __v = __bswap_constant_32 (__x); \
  79. else \
  80. __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); \
  81. __v; }))
  82. # endif
  83. # else
  84. # define __bswap_32(x) \
  85. (__extension__ \
  86. ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
  87. # endif
  88. #else
  89. static __inline unsigned int
  90. __bswap_32 (unsigned int __bsx)
  91. {
  92. return __bswap_constant_32 (__bsx);
  93. }
  94. #endif
  95. #if defined __GNUC__ && __GNUC__ >= 2
  96. /* Swap bytes in 64 bit value. */
  97. #define __bswap_constant_64(x) \
  98. ((((x) & 0xff00000000000000ull) >> 56) \
  99. | (((x) & 0x00ff000000000000ull) >> 40) \
  100. | (((x) & 0x0000ff0000000000ull) >> 24) \
  101. | (((x) & 0x000000ff00000000ull) >> 8) \
  102. | (((x) & 0x00000000ff000000ull) << 8) \
  103. | (((x) & 0x0000000000ff0000ull) << 24) \
  104. | (((x) & 0x000000000000ff00ull) << 40) \
  105. | (((x) & 0x00000000000000ffull) << 56))
  106. # define __bswap_64(x) \
  107. (__extension__ \
  108. ({ union { __extension__ unsigned long long int __ll; \
  109. unsigned long int __l[2]; } __w, __r; \
  110. if (__builtin_constant_p (x)) \
  111. __r.__ll = __bswap_constant_64 (x); \
  112. else \
  113. { \
  114. __w.__ll = (x); \
  115. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  116. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  117. } \
  118. __r.__ll; }))
  119. #endif
  120. #endif /* _BITS_BYTESWAP_H */