byteswap.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _ASM_BITS_BYTESWAP_H
  16. #define _ASM_BITS_BYTESWAP_H 1
  17. #include <bits/wordsize.h>
  18. #define __bswap_non_constant_16(x) \
  19. (__extension__ \
  20. ({ register unsigned short int __v; \
  21. __asm__ ("rorw $8, %w0" \
  22. : "=r" (__v) \
  23. : "0" (x) \
  24. : "cc"); \
  25. __v; }))
  26. #if __WORDSIZE == 64 || (defined __i486__ || defined __pentium__ \
  27. || defined __pentiumpro__ || defined __pentium4__ \
  28. || defined __k8__ || defined __athlon__ \
  29. || defined __k6__)
  30. /* To swap the bytes in a word the i486 processors and up provide the
  31. `bswap' opcode. On i386 we have to use three instructions. */
  32. # define __bswap_non_constant_32(x) \
  33. (__extension__ \
  34. ({ register unsigned int __v; \
  35. __asm__ ("bswap %0" : "=r" (__v) : "0" (x)); \
  36. __v; }))
  37. #else
  38. # define __bswap_non_constant_32(x) \
  39. (__extension__ \
  40. ({ register unsigned int __v; \
  41. __asm__ ("rorw $8, %w0;" \
  42. "rorl $16, %0;" \
  43. "rorw $8, %w0" \
  44. : "=r" (__v) \
  45. : "0" (x) \
  46. : "cc"); \
  47. __v; }))
  48. #endif
  49. #if __WORDSIZE == 64
  50. # define __bswap_non_constant_64(x) \
  51. (__extension__ \
  52. ({ register unsigned long __v; \
  53. __asm__ ("bswap %q0" : "=r" (__v) : "0" (x)); \
  54. __v; }))
  55. #endif
  56. #endif
  57. #include <bits/byteswap-common.h>