byteswap.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * libc/sysdeps/linux/v850/bits/byteswap.h -- Macros to swap the order
  3. * of bytes in integer values
  4. *
  5. * Copyright (C) 2001 NEC Corporation
  6. * Copyright (C) 2001 Miles Bader <miles@gnu.org>
  7. * Copyright (C) 1997,1998,2001 Free Software Foundation, Inc.
  8. *
  9. * This file is subject to the terms and conditions of the GNU Lesser
  10. * General Public License. See the file COPYING.LIB in the main
  11. * directory of this archive for more details.
  12. */
  13. #if !defined _BYTESWAP_H && !defined _NETINET_IN_H
  14. # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
  15. #endif
  16. /* Swap bytes in 16 bit value. */
  17. #define __bswap_constant_16(x) \
  18. ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
  19. #ifdef __GNUC__
  20. # define __bswap_16(x) \
  21. (__extension__ \
  22. ({ unsigned long int __bswap_16_v; \
  23. if (__builtin_constant_p (x)) \
  24. __bswap_16_v = __bswap_constant_16 (x); \
  25. else \
  26. __asm__ ("bsh %1, %0" : "=r" (__bswap_16_v) : "r" (x)); \
  27. __bswap_16_v; }))
  28. #else
  29. # define __bswap_16(x) __bswap_constant_16 (x)
  30. #endif
  31. /* Swap bytes in 32 bit value. */
  32. #define __bswap_constant_32(x) \
  33. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  34. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  35. #ifdef __GNUC__
  36. # define __bswap_32(x) \
  37. (__extension__ \
  38. ({ unsigned long int __bswap_32_v; \
  39. if (__builtin_constant_p (x)) \
  40. __bswap_32_v = __bswap_constant_32 (x); \
  41. else \
  42. __asm__ ("bsw %1, %0" : "=r" (__bswap_32_v) : "r" (x)); \
  43. __bswap_32_v; }))
  44. #else
  45. # define __bswap_32(x) __bswap_constant_32 (x)
  46. #endif
  47. #if defined __GNUC__ && __GNUC__ >= 2
  48. /* Swap bytes in 64 bit value. */
  49. # define __bswap_64(x) \
  50. (__extension__ \
  51. ({ union { unsigned long long int __ll; \
  52. unsigned long int __l[2]; } __bswap_64_v, __bswap_64_r; \
  53. __bswap_64_v.__ll = (x); \
  54. __bswap_64_r.__l[0] = __bswap_32 (__bswap_64_v.__l[1]); \
  55. __bswap_64_r.__l[1] = __bswap_32 (__bswap_64_v.__l[0]); \
  56. __bswap_64_r.__ll; }))
  57. #endif