byteswap-common.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Macros to swap the order of bytes in integer values.
  2. Copyright (C) 1997,1998,2000,2001,2002,2005 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) & 0xffu) | (((x) & 0xffu) << 8))
  24. #ifndef __bswap_non_constant_16
  25. # define __bswap_non_constant_16(x) __bswap_constant_16(x)
  26. #endif
  27. #ifdef __GNUC__
  28. # define __bswap_16(x) \
  29. (__extension__ \
  30. ({ unsigned short int __bsv, __bsx = (x); \
  31. if (__builtin_constant_p (__bsx)) \
  32. __bsv = __bswap_constant_16 (__bsx); \
  33. else \
  34. __bsv = __bswap_non_constant_16 (__bsx); \
  35. __bsv; }))
  36. #else
  37. static __inline unsigned short int
  38. __bswap_16 (unsigned short int __bsx)
  39. {
  40. return __bswap_constant_16 (__bsx);
  41. }
  42. #endif
  43. /* Swap bytes in 32 bit value. */
  44. #define __bswap_constant_32(x) \
  45. ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
  46. (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
  47. #ifndef __bswap_non_constant_32
  48. # define __bswap_non_constant_32(x) __bswap_constant_32(x)
  49. #endif
  50. #ifdef __GNUC__
  51. # define __bswap_32(x) \
  52. (__extension__ \
  53. ({ unsigned int __bsv, __bsx = (x); \
  54. if (__builtin_constant_p (__bsx)) \
  55. __bsv = __bswap_constant_32 (__bsx); \
  56. else \
  57. __bsv = __bswap_non_constant_32 (__bsx); \
  58. __bsv; }))
  59. #else
  60. static __inline unsigned int
  61. __bswap_32 (unsigned int __bsx)
  62. {
  63. return __bswap_constant_32 (__bsx);
  64. }
  65. #endif
  66. #if defined __GNUC__ && __GNUC__ >= 2
  67. /* Swap bytes in 64 bit value. */
  68. # define __bswap_constant_64(x) \
  69. ((((x) & 0xff00000000000000ull) >> 56) \
  70. | (((x) & 0x00ff000000000000ull) >> 40) \
  71. | (((x) & 0x0000ff0000000000ull) >> 24) \
  72. | (((x) & 0x000000ff00000000ull) >> 8) \
  73. | (((x) & 0x00000000ff000000ull) << 8) \
  74. | (((x) & 0x0000000000ff0000ull) << 24) \
  75. | (((x) & 0x000000000000ff00ull) << 40) \
  76. | (((x) & 0x00000000000000ffull) << 56))
  77. # ifndef __bswap_non_constant_64
  78. # define __bswap_non_constant_64(x) \
  79. (__extension__ \
  80. ({ union { __extension__ unsigned long long int __ll; \
  81. unsigned int __l[2]; } __w, __r; \
  82. __w.__ll = (x); \
  83. __r.__l[0] = __bswap_non_constant_32 (__w.__l[1]); \
  84. __r.__l[1] = __bswap_non_constant_32 (__w.__l[0]); \
  85. __r.__ll; }))
  86. # endif
  87. # define __bswap_64(x) \
  88. (__extension__ \
  89. ({ __extension__ unsigned long long int __ll; \
  90. if (__builtin_constant_p (x)) \
  91. __ll = __bswap_constant_64 (x); \
  92. else \
  93. __ll = __bswap_non_constant_64 (x); \
  94. __ll; }))
  95. #endif
  96. #endif /* _BITS_BYTESWAP_H */