byteswap.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Macros to swap the order of bytes in integer values.
  2. Copyright (C) 1997, 1998, 2000, 2002, 2003 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, __x = (x); \
  28. if (__builtin_constant_p (x)) \
  29. __v = __bswap_constant_16 (__x); \
  30. else \
  31. __asm__ __volatile__ ("shl %0 = %1, 48 ;;" \
  32. "mux1 %0 = %0, @rev ;;" \
  33. : "=r" (__v) \
  34. : "r" ((unsigned short int) (__x))); \
  35. __v; }))
  36. #else
  37. /* This is better than nothing. */
  38. static __inline unsigned short int
  39. __bswap_16 (unsigned short int __bsx)
  40. {
  41. return __bswap_constant_16 (__bsx);
  42. }
  43. #endif
  44. /* Swap bytes in 32 bit value. */
  45. #define __bswap_constant_32(x) \
  46. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  47. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  48. #if defined __GNUC__ && __GNUC__ >= 2
  49. # define __bswap_32(x) \
  50. (__extension__ \
  51. ({ register unsigned int __v, __x = (x); \
  52. if (__builtin_constant_p (x)) \
  53. __v = __bswap_constant_32 (__x); \
  54. else \
  55. __asm__ __volatile__ ("shl %0 = %1, 32 ;;" \
  56. "mux1 %0 = %0, @rev ;;" \
  57. : "=r" (__v) \
  58. : "r" ((unsigned int) (__x))); \
  59. __v; }))
  60. #else
  61. static __inline unsigned int
  62. __bswap_32 (unsigned int __bsx)
  63. {
  64. return __bswap_constant_32 (__bsx);
  65. }
  66. #endif
  67. /* Swap bytes in 64 bit value. */
  68. #define __bswap_constant_64(x) \
  69. ((((x) & 0xff00000000000000ul) >> 56) \
  70. | (((x) & 0x00ff000000000000ul) >> 40) \
  71. | (((x) & 0x0000ff0000000000ul) >> 24) \
  72. | (((x) & 0x000000ff00000000ul) >> 8) \
  73. | (((x) & 0x00000000ff000000ul) << 8) \
  74. | (((x) & 0x0000000000ff0000ul) << 24) \
  75. | (((x) & 0x000000000000ff00ul) << 40) \
  76. | (((x) & 0x00000000000000fful) << 56))
  77. #if defined __GNUC__ && __GNUC__ >= 2
  78. # define __bswap_64(x) \
  79. (__extension__ \
  80. ({ register unsigned long int __v, __x = (x); \
  81. if (__builtin_constant_p (x)) \
  82. __v = __bswap_constant_64 (__x); \
  83. else \
  84. __asm__ __volatile__ ("mux1 %0 = %1, @rev ;;" \
  85. : "=r" (__v) \
  86. : "r" ((unsigned long int) (__x))); \
  87. __v; }))
  88. #else
  89. static __inline unsigned long int
  90. __bswap_64 (unsigned long int __bsx)
  91. {
  92. return __bswap_constant_64 (__bsx);
  93. }
  94. #endif
  95. #endif /* _BITS_BYTESWAP_H */