byteswap.h 2.4 KB

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