byteswap.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #ifdef __GNUC__
  18. # define __bswap_16(x) \
  19. (__extension__ \
  20. ({ unsigned short int __bsh_val = (x); \
  21. __asm__ ("bsh %1, %0" : "=r" (__bsh_val) : "r" (__bsh_val)); \
  22. __bsh_val; }))
  23. #else
  24. static __inline unsigned short int
  25. __bswap_16 (unsigned short int __bsx)
  26. {
  27. return ((((__bsx) >> 8) & 0xff) | (((__bsx) & 0xff) << 8));
  28. }
  29. #endif
  30. /* Swap bytes in 32 bit value. */
  31. #ifdef __GNUC__
  32. # define __bswap_32(x) \
  33. (__extension__ \
  34. ({ unsigned short int __bsw_val = (x); \
  35. __asm__ ("bsw %1, %0" : "=r" (__bsw_val) : "r" (__bsw_val)); \
  36. __bsw_val; }))
  37. #else
  38. static __inline unsigned int
  39. __bswap_32 (unsigned int __bsx)
  40. {
  41. return ((((__bsx) & 0xff000000) >> 24) | (((__bsx) & 0x00ff0000) >> 8) |
  42. (((__bsx) & 0x0000ff00) << 8) | (((__bsx) & 0x000000ff) << 24));
  43. }
  44. #endif
  45. #if defined __GNUC__ && __GNUC__ >= 2
  46. /* Swap bytes in 64 bit value. */
  47. # define __bswap_64(x) \
  48. (__extension__ \
  49. ({ union { unsigned long long int __ll; \
  50. unsigned long int __l[2]; } __v, __r; \
  51. __v.__ll = (x); \
  52. __r.__l[0] = __bswap_32 (__v.__l[1]); \
  53. __r.__l[1] = __bswap_32 (__v.__l[0]); \
  54. __r.__ll; }))
  55. #endif