byteswap.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #ifdef __GNUC__
  25. # define __bswap_16(x) \
  26. (__extension__ \
  27. ({ unsigned short int __bsx = (x); __bswap_constant_16 (__bsx); }))
  28. #else
  29. static __inline unsigned short int
  30. __bswap_16 (unsigned short int __bsx)
  31. {
  32. return __bswap_constant_16 (__bsx);
  33. }
  34. #endif
  35. /* Swap bytes in 32 bit value. */
  36. #define __bswap_constant_32(x) \
  37. ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
  38. (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
  39. #ifdef __GNUC__
  40. # define __bswap_32(x) \
  41. (__extension__ \
  42. ({ register unsigned int __bsx = (x); __bswap_constant_32 (__bsx); }))
  43. #else
  44. static __inline unsigned int
  45. __bswap_32 (unsigned int __bsx)
  46. {
  47. return __bswap_constant_32 (__bsx);
  48. }
  49. #endif
  50. #if defined __GNUC__ && __GNUC__ >= 2
  51. /* Swap bytes in 64 bit value. */
  52. # define __bswap_constant_64(x) \
  53. ((((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. # define __bswap_64(x) \
  62. (__extension__ \
  63. ({ union { __extension__ unsigned long long int __ll; \
  64. unsigned int __l[2]; } __w, __r; \
  65. if (__builtin_constant_p (x)) \
  66. __r.__ll = __bswap_constant_64 (x); \
  67. else \
  68. { \
  69. __w.__ll = (x); \
  70. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  71. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  72. } \
  73. __r.__ll; }))
  74. #endif
  75. #endif /* _BITS_BYTESWAP_H */