byteswap.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Macros to swap the order of bytes in integer values.
  2. Copyright (C) 1997, 1998 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 Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. 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. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 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. /* Swap bytes in 16 bit value. */
  20. #define __bswap_constant_16(x) \
  21. ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
  22. #if defined __GNUC__ && __GNUC__ >= 2
  23. # define __bswap_16(x) \
  24. (__extension__ \
  25. ({ register unsigned short int __v; \
  26. if (__builtin_constant_p (x)) \
  27. __v = __bswap_constant_16 (x); \
  28. else \
  29. __asm__ __volatile__ ("rorw $8, %w0" \
  30. : "=r" (__v) \
  31. : "0" ((unsigned short int) (x)) \
  32. : "cc"); \
  33. __v; }))
  34. #else
  35. /* This is better than nothing. */
  36. # define __bswap_16(x) __bswap_constant_16 (x)
  37. #endif
  38. /* Swap bytes in 32 bit value. */
  39. #define __bswap_constant_32(x) \
  40. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  41. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  42. #if defined __GNUC__ && __GNUC__ >= 2
  43. /* To swap the bytes in a word the i486 processors and up provide the
  44. `bswap' opcode. On i386 we have to use three instructions. */
  45. # if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__
  46. # define __bswap_32(x) \
  47. (__extension__ \
  48. ({ register unsigned int __v; \
  49. if (__builtin_constant_p (x)) \
  50. __v = __bswap_constant_32 (x); \
  51. else \
  52. __asm__ __volatile__ ("rorw $8, %w0;" \
  53. "rorl $16, %0;" \
  54. "rorw $8, %w0" \
  55. : "=r" (__v) \
  56. : "0" ((unsigned int) (x)) \
  57. : "cc"); \
  58. __v; }))
  59. # else
  60. # define __bswap_32(x) \
  61. (__extension__ \
  62. ({ register unsigned int __v; \
  63. if (__builtin_constant_p (x)) \
  64. __v = __bswap_constant_32 (x); \
  65. else \
  66. __asm__ __volatile__ ("bswap %0" \
  67. : "=r" (__v) \
  68. : "0" ((unsigned int) (x))); \
  69. __v; }))
  70. # endif
  71. #else
  72. # define __bswap_32(x) __bswap_constant_32 (x)
  73. #endif
  74. #if defined __GNUC__ && __GNUC__ >= 2
  75. /* Swap bytes in 64 bit value. */
  76. # define __bswap_64(x) \
  77. (__extension__ \
  78. ({ union { __extension__ unsigned long long int __ll; \
  79. unsigned long int __l[2]; } __w, __r; \
  80. __w.__ll = (x); \
  81. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  82. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  83. __r.__ll; }))
  84. #endif