byteswap.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2016 Andes Technology, Inc.
  3. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. /* Macros to swap the order of bytes in integer values.
  6. Copyright (C) 1997, 1998, 2000, 2002, 2003, 2006, 2007
  7. Free Software Foundation, Inc.
  8. The GNU C Library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12. The GNU C Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with the GNU C Library; if not, write to the Free
  18. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. 02111-1307 USA. */
  20. #if !defined _BYTESWAP_H && !defined _NETINET_IN_H
  21. # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
  22. #endif
  23. #ifndef _BITS_BYTESWAP_H
  24. #define _BITS_BYTESWAP_H 1
  25. /* Swap bytes in 16 bit value. */
  26. #define __bswap_constant_16(x) \
  27. ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
  28. #ifdef __GNUC__
  29. # if __GNUC__ >= 2
  30. # define __bswap_16(x) \
  31. (__extension__ \
  32. ({ register unsigned short int __v, __x = (x); \
  33. if (__builtin_constant_p (__x)) \
  34. __v = __bswap_constant_16 (__x); \
  35. else \
  36. __asm__ ("wsbh %0, %0\n\t" \
  37. : "=r" (__v) \
  38. : "0" (__x)); \
  39. __v; }))
  40. # else
  41. /* This is better than nothing. */
  42. # define __bswap_16(x) \
  43. (__extension__ \
  44. ({ register unsigned short int __x = (x); __bswap_constant_16 (__x); }))
  45. # endif
  46. #else
  47. static __inline unsigned short int
  48. __bswap_16 (unsigned short int __bsx)
  49. {
  50. return __bswap_constant_16 (__bsx);
  51. }
  52. #endif
  53. /* Swap bytes in 32 bit value. */
  54. #define __bswap_constant_32(x) \
  55. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  56. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  57. #ifdef __GNUC__
  58. # if __GNUC__ >= 2
  59. # define __bswap_32(x) \
  60. (__extension__ \
  61. ({ register unsigned int __v, __x = (x); \
  62. if (__builtin_constant_p (__x)) \
  63. __v = __bswap_constant_32 (__x); \
  64. else \
  65. __asm__ ("wsbh %0, %0\n\t" \
  66. "rotri %0, %0, #16\n\t" \
  67. : "=r" (__v) \
  68. : "0" (__x)); \
  69. __v; }))
  70. # else
  71. # define __bswap_32(x) \
  72. (__extension__ \
  73. ({ register unsigned int __x = (x); __bswap_constant_32 (__x); }))
  74. # endif
  75. #else
  76. static __inline unsigned int
  77. __bswap_32 (unsigned int __bsx)
  78. {
  79. return __bswap_constant_32 (__bsx);
  80. }
  81. #endif
  82. #if defined __GNUC__ && __GNUC__ >= 2
  83. /* Swap bytes in 64 bit value. */
  84. #define __bswap_constant_64(x) \
  85. ((((x) & 0xff00000000000000ull) >> 56) \
  86. | (((x) & 0x00ff000000000000ull) >> 40) \
  87. | (((x) & 0x0000ff0000000000ull) >> 24) \
  88. | (((x) & 0x000000ff00000000ull) >> 8) \
  89. | (((x) & 0x00000000ff000000ull) << 8) \
  90. | (((x) & 0x0000000000ff0000ull) << 24) \
  91. | (((x) & 0x000000000000ff00ull) << 40) \
  92. | (((x) & 0x00000000000000ffull) << 56))
  93. # define __bswap_64(x) \
  94. (__extension__ \
  95. ({ union { __extension__ unsigned long long int __ll; \
  96. unsigned long int __l[2]; } __w, __r; \
  97. if (__builtin_constant_p (x)) \
  98. __r.__ll = __bswap_constant_64 (x); \
  99. else \
  100. { \
  101. __w.__ll = (x); \
  102. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  103. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  104. } \
  105. __r.__ll; }))
  106. #endif
  107. #endif /* _BITS_BYTESWAP_H */