byteswap.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Macros to swap the order of bytes in integer values.
  2. Copyright (C) 1997, 1998, 2000, 2002 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. #if defined __GNUC__ && __GNUC__ >= 2
  25. # define __bswap_16(x) \
  26. (__extension__ \
  27. ({ register unsigned short int __v; \
  28. if (__builtin_constant_p (x)) \
  29. __v = __bswap_constant_16 (x); \
  30. else \
  31. __asm__ __volatile__ ("rorw $8, %w0" \
  32. : "=r" (__v) \
  33. : "0" ((unsigned short int) (x)) \
  34. : "cc"); \
  35. __v; }))
  36. #else
  37. /* This is better than nothing. */
  38. # define __bswap_16(x) __bswap_constant_16 (x)
  39. #endif
  40. /* Swap bytes in 32 bit value. */
  41. #define __bswap_constant_32(x) \
  42. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  43. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  44. #if defined __GNUC__ && __GNUC__ >= 2
  45. /* To swap the bytes in a word the i486 processors and up provide the
  46. `bswap' opcode. On i386 we have to use three instructions. */
  47. # if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__
  48. # define __bswap_32(x) \
  49. (__extension__ \
  50. ({ register unsigned int __v; \
  51. if (__builtin_constant_p (x)) \
  52. __v = __bswap_constant_32 (x); \
  53. else \
  54. __asm__ __volatile__ ("rorw $8, %w0;" \
  55. "rorl $16, %0;" \
  56. "rorw $8, %w0" \
  57. : "=r" (__v) \
  58. : "0" ((unsigned int) (x)) \
  59. : "cc"); \
  60. __v; }))
  61. # else
  62. # define __bswap_32(x) \
  63. (__extension__ \
  64. ({ register unsigned int __v; \
  65. if (__builtin_constant_p (x)) \
  66. __v = __bswap_constant_32 (x); \
  67. else \
  68. __asm__ __volatile__ ("bswap %0" \
  69. : "=r" (__v) \
  70. : "0" ((unsigned int) (x))); \
  71. __v; }))
  72. # endif
  73. #else
  74. # define __bswap_32(x) __bswap_constant_32 (x)
  75. #endif
  76. #if defined __GNUC__ && __GNUC__ >= 2
  77. /* Swap bytes in 64 bit value. */
  78. #define __bswap_constant_64(x) \
  79. ((((x) & 0xff00000000000000ull) >> 56) \
  80. | (((x) & 0x00ff000000000000ull) >> 40) \
  81. | (((x) & 0x0000ff0000000000ull) >> 24) \
  82. | (((x) & 0x000000ff00000000ull) >> 8) \
  83. | (((x) & 0x00000000ff000000ull) << 8) \
  84. | (((x) & 0x0000000000ff0000ull) << 24) \
  85. | (((x) & 0x000000000000ff00ull) << 40) \
  86. | (((x) & 0x00000000000000ffull) << 56))
  87. # define __bswap_64(x) \
  88. (__extension__ \
  89. ({ union { __extension__ unsigned long long int __ll; \
  90. unsigned long int __l[2]; } __w, __r; \
  91. if (__builtin_constant_p (x)) \
  92. __r.__ll = __bswap_constant_64 (x); \
  93. else \
  94. { \
  95. __w.__ll = (x); \
  96. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  97. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  98. } \
  99. __r.__ll; }))
  100. #endif
  101. #endif /* _BITS_BYTESWAP_H */