byteswap.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * libc/sysdeps/linux/microblaze/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. # define __bswap_16(x) __bswap_constant_16 (x)
  20. /* Swap bytes in 32 bit value. */
  21. #define __bswap_constant_32(x) \
  22. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  23. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  24. # define __bswap_32(x) __bswap_constant_32 (x)
  25. #if defined __GNUC__ && __GNUC__ >= 2
  26. /* Swap bytes in 64 bit value. */
  27. # define __bswap_64(x) \
  28. (__extension__ \
  29. ({ union { unsigned long long int __ll; \
  30. unsigned long int __l[2]; } __bswap_64_v, __bswap_64_r; \
  31. __bswap_64_v.__ll = (x); \
  32. __bswap_64_r.__l[0] = __bswap_32 (__bswap_64_v.__l[1]); \
  33. __bswap_64_r.__l[1] = __bswap_32 (__bswap_64_v.__l[0]); \
  34. __bswap_64_r.__ll; }))
  35. #endif