byteswap.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Macros to swap the order of bytes in integer values.
  2. Copyright (C) 1997, 1998, 2000, 2001, 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. #ifdef __GNUC__
  23. # define __bswap_16(x) \
  24. (__extension__ \
  25. ({ unsigned short int __bsx = (x); \
  26. ((((__bsx) >> 8) & 0xff) | (((__bsx) & 0xff) << 8)); }))
  27. #else
  28. static __inline unsigned short int
  29. __bswap_16 (unsigned short int __bsx)
  30. {
  31. return ((((__bsx) >> 8) & 0xff) | (((__bsx) & 0xff) << 8));
  32. }
  33. #endif
  34. /* Swap bytes in 32 bit value. */
  35. #ifdef __GNUC__
  36. # define __bswap_32(x) \
  37. (__extension__ \
  38. ({ unsigned int __bsx = (x); \
  39. ((((__bsx) & 0xff000000) >> 24) | (((__bsx) & 0x00ff0000) >> 8) | \
  40. (((__bsx) & 0x0000ff00) << 8) | (((__bsx) & 0x000000ff) << 24)); }))
  41. #else
  42. static __inline unsigned int
  43. __bswap_32 (unsigned int __bsx)
  44. {
  45. return ((((__bsx) & 0xff000000) >> 24) | (((__bsx) & 0x00ff0000) >> 8) |
  46. (((__bsx) & 0x0000ff00) << 8) | (((__bsx) & 0x000000ff) << 24));
  47. }
  48. #endif
  49. #if defined __GNUC__ && __GNUC__ >= 2
  50. /* Swap bytes in 64 bit value. */
  51. # define __bswap_constant_64(x) \
  52. ((((x) & 0xff00000000000000ull) >> 56) \
  53. | (((x) & 0x00ff000000000000ull) >> 40) \
  54. | (((x) & 0x0000ff0000000000ull) >> 24) \
  55. | (((x) & 0x000000ff00000000ull) >> 8) \
  56. | (((x) & 0x00000000ff000000ull) << 8) \
  57. | (((x) & 0x0000000000ff0000ull) << 24) \
  58. | (((x) & 0x000000000000ff00ull) << 40) \
  59. | (((x) & 0x00000000000000ffull) << 56))
  60. # define __bswap_64(x) \
  61. (__extension__ \
  62. ({ union { __extension__ unsigned long long int __ll; \
  63. unsigned int __l[2]; } __w, __r; \
  64. if (__builtin_constant_p (x)) \
  65. __r.__ll = __bswap_constant_64 (x); \
  66. else \
  67. { \
  68. __w.__ll = (x); \
  69. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  70. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  71. } \
  72. __r.__ll; }))
  73. #endif
  74. #endif /* _BITS_BYTESWAP_H */