byteswap-common.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /* Swap bytes in 16 bit value. */
  23. #define __bswap_constant_16(x) \
  24. ((unsigned short int)((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)))
  25. /* Get __bswap_16. */
  26. #include <bits/byteswap-16.h>
  27. /* Swap bytes in 32 bit value. */
  28. #define __bswap_constant_32(x) \
  29. ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
  30. (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
  31. #ifdef __GNUC__
  32. # if __GNUC_PREREQ (4, 3)
  33. static __inline unsigned int
  34. __bswap_32 (unsigned int __bsx)
  35. {
  36. return __builtin_bswap32 (__bsx);
  37. }
  38. # else
  39. # define __bswap_32(x) \
  40. (__extension__ \
  41. ({ unsigned int __bsx = (x); __bswap_constant_32 (__bsx); }))
  42. # endif
  43. #else
  44. static __inline unsigned int
  45. __bswap_32 (unsigned int __bsx)
  46. {
  47. return __bswap_constant_32 (__bsx);
  48. }
  49. #endif
  50. /* Swap bytes in 64 bit value. */
  51. #if __GNUC_PREREQ (2, 0)
  52. # define __bswap_constant_64(x) \
  53. (__extension__ ((((x) & 0xff00000000000000ull) >> 56) \
  54. | (((x) & 0x00ff000000000000ull) >> 40) \
  55. | (((x) & 0x0000ff0000000000ull) >> 24) \
  56. | (((x) & 0x000000ff00000000ull) >> 8) \
  57. | (((x) & 0x00000000ff000000ull) << 8) \
  58. | (((x) & 0x0000000000ff0000ull) << 24) \
  59. | (((x) & 0x000000000000ff00ull) << 40) \
  60. | (((x) & 0x00000000000000ffull) << 56)))
  61. # if __GNUC_PREREQ (4, 3)
  62. static __inline __uint64_t
  63. __bswap_64 (__uint64_t __bsx)
  64. {
  65. return __builtin_bswap64 (__bsx);
  66. }
  67. # else
  68. # define __bswap_64(x) \
  69. (__extension__ \
  70. ({ union { __extension__ __uint64_t __ll; \
  71. unsigned int __l[2]; } __w, __r; \
  72. if (__builtin_constant_p (x)) \
  73. __r.__ll = __bswap_constant_64 (x); \
  74. else \
  75. { \
  76. __w.__ll = (x); \
  77. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  78. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  79. } \
  80. __r.__ll; }))
  81. # endif
  82. #else
  83. # define __bswap_constant_64(x) \
  84. ((((x) & 0xff00000000000000ull) >> 56) \
  85. | (((x) & 0x00ff000000000000ull) >> 40) \
  86. | (((x) & 0x0000ff0000000000ull) >> 24) \
  87. | (((x) & 0x000000ff00000000ull) >> 8) \
  88. | (((x) & 0x00000000ff000000ull) << 8) \
  89. | (((x) & 0x0000000000ff0000ull) << 24) \
  90. | (((x) & 0x000000000000ff00ull) << 40) \
  91. | (((x) & 0x00000000000000ffull) << 56))
  92. static __inline __uint64_t
  93. __bswap_64 (__uint64_t __bsx)
  94. {
  95. return __bswap_constant_64 (__bsx);
  96. }
  97. #endif
  98. #endif /* _BITS_BYTESWAP_H */