byteswap.h 5.1 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. #include <bits/wordsize.h>
  22. /* Swap bytes in 16 bit value. */
  23. #define __bswap_constant_16(x) \
  24. ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
  25. #if defined __GNUC__ && __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. /* Swap bytes in 32 bit value. */
  44. #define __bswap_constant_32(x) \
  45. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  46. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  47. #if defined __GNUC__ && __GNUC__ >= 2
  48. # if __WORDSIZE == 64 || (defined __i486__ || defined __pentium__ \
  49. || defined __pentiumpro__ || defined __pentium4__ \
  50. || defined __k8__ || defined __athlon__ \
  51. || defined __k6__)
  52. /* To swap the bytes in a word the i486 processors and up provide the
  53. `bswap' opcode. On i386 we have to use three instructions. */
  54. # define __bswap_32(x) \
  55. (__extension__ \
  56. ({ register unsigned int __v, __x = (x); \
  57. if (__builtin_constant_p (__x)) \
  58. __v = __bswap_constant_32 (__x); \
  59. else \
  60. __asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); \
  61. __v; }))
  62. # else
  63. # define __bswap_32(x) \
  64. (__extension__ \
  65. ({ register unsigned int __v, __x = (x); \
  66. if (__builtin_constant_p (__x)) \
  67. __v = __bswap_constant_32 (__x); \
  68. else \
  69. __asm__ ("rorw $8, %w0;" \
  70. "rorl $16, %0;" \
  71. "rorw $8, %w0" \
  72. : "=r" (__v) \
  73. : "0" (__x) \
  74. : "cc"); \
  75. __v; }))
  76. # endif
  77. #else
  78. # define __bswap_32(x) \
  79. (__extension__ \
  80. ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
  81. #endif
  82. #if defined __GNUC__ && __GNUC__ >= 2
  83. /* Swap bytes in 64 bit value. */
  84. # define __bswap_constant_64(x) \
  85. ((((x) & 0xff00000000000000ull) >> 56) \
  86. | (((x) & 0x00ff000000000000ull) >> 40) \
  87. | (((x) & 0x0000ff0000000000ull) >> 24) \
  88. | (((x) & 0x000000ff00000000ull) >> 8) \
  89. | (((x) & 0x00000000ff000000ull) << 8) \
  90. | (((x) & 0x0000000000ff0000ull) << 24) \
  91. | (((x) & 0x000000000000ff00ull) << 40) \
  92. | (((x) & 0x00000000000000ffull) << 56))
  93. # if __WORDSIZE == 64
  94. # define __bswap_64(x) \
  95. (__extension__ \
  96. ({ register unsigned long __v, __x = (x); \
  97. if (__builtin_constant_p (__x)) \
  98. __v = __bswap_constant_64 (__x); \
  99. else \
  100. __asm__ ("bswap %q0" : "=r" (__v) : "0" (__x)); \
  101. __v; }))
  102. # else
  103. # define __bswap_64(x) \
  104. (__extension__ \
  105. ({ union { __extension__ unsigned long long int __ll; \
  106. unsigned int __l[2]; } __w, __r; \
  107. if (__builtin_constant_p (x)) \
  108. __r.__ll = __bswap_constant_64 (x); \
  109. else \
  110. { \
  111. __w.__ll = (x); \
  112. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  113. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  114. } \
  115. __r.__ll; }))
  116. # endif
  117. #endif
  118. #endif /* _BITS_BYTESWAP_H */