byteswap.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #define __bswap_non_constant_16(x) \
  18. (__extension__ \
  19. ({ register unsigned short int __v; \
  20. __asm__ ("rorw $8, %w0" \
  21. : "=r" (__v) \
  22. : "0" (x) \
  23. : "cc"); \
  24. __v; }))
  25. /* To swap the bytes in a word the i486 processors and up provide the
  26. `bswap' opcode. On i386 we have to use three instructions. */
  27. #if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__ \
  28. && !defined __pentium4__
  29. # define __bswap_non_constant_32(x) \
  30. (__extension__ \
  31. ({ register unsigned int __v; \
  32. __asm__ ("rorw $8, %w0;" \
  33. "rorl $16, %0;" \
  34. "rorw $8, %w0" \
  35. : "=r" (__v) \
  36. : "0" (x) \
  37. : "cc"); \
  38. __v; }))
  39. #else
  40. # define __bswap_non_constant_32(x) \
  41. (__extension__ \
  42. ({ register unsigned int __v; \
  43. __asm__ ("bswap %0" : "=r" (__v) : "0" (x)); \
  44. __v; }))
  45. #endif
  46. #endif
  47. #include <bits/byteswap-common.h>