byteswap.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* File: libc/sysdeps/linux/bfin/bits/byteswap.h
  2. *
  3. * Copyright 2004-2006 Analog Devices Inc.
  4. *
  5. * Enter bugs at http://blackfin.uclinux.org/
  6. *
  7. * Licensed under the GPL-2 or later.
  8. */
  9. #if !defined _BYTESWAP_H && !defined _NETINET_IN_H
  10. # error "Never use <bits/byteswap.h> directly; include <byteswap.h> instead."
  11. #endif
  12. #ifndef _BITS_BYTESWAP_H
  13. #define _BITS_BYTESWAP_H 1
  14. /* Swap bytes in 16 bit value. */
  15. #define __bswap_constant_16(x) \
  16. ((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8))
  17. #ifdef __GNUC__
  18. # define __bswap_16(x) \
  19. (__extension__ \
  20. ({ register unsigned short int __v, __x = (x); \
  21. if (__builtin_constant_p (__x)) \
  22. __v = __bswap_constant_16 (__x); \
  23. else \
  24. __asm__ ("%0 = PACK (%1.L, %1.L);" \
  25. "%0 >>= 8;" \
  26. : "=d" (__v) \
  27. : "d" (__x)); \
  28. __v; }))
  29. #else
  30. static __inline unsigned short int
  31. __bswap_16 (unsigned short int __bsx)
  32. {
  33. return __bswap_constant_16 (__bsx);
  34. }
  35. #endif
  36. /* Swap bytes in 32 bit value. */
  37. #define __bswap_constant_32(x) \
  38. ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
  39. (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
  40. #ifdef __GNUC__
  41. # define __bswap_32(x) \
  42. (__extension__ \
  43. ({ register unsigned int __v, __x = (x); \
  44. if (__builtin_constant_p (__x)) \
  45. __v = __bswap_constant_32 (__x); \
  46. else \
  47. __asm__ ("%1 = %0 >> 8 (V);" \
  48. "%0 = %0 << 8 (V);" \
  49. "%0 = %0 | %1;" \
  50. "%1 = PACK(%0.L, %0.H);" \
  51. : "+d"(__x), "=&d"(__v)); \
  52. __v; }))
  53. #else
  54. static __inline unsigned int
  55. __bswap_32 (unsigned int __bsx)
  56. {
  57. return __bswap_constant_32 (__bsx);
  58. }
  59. #endif
  60. #if defined __GNUC__ && __GNUC__ >= 2
  61. /* Swap bytes in 64 bit value. */
  62. # define __bswap_constant_64(x) \
  63. ((((x) & 0xff00000000000000ull) >> 56) \
  64. | (((x) & 0x00ff000000000000ull) >> 40) \
  65. | (((x) & 0x0000ff0000000000ull) >> 24) \
  66. | (((x) & 0x000000ff00000000ull) >> 8) \
  67. | (((x) & 0x00000000ff000000ull) << 8) \
  68. | (((x) & 0x0000000000ff0000ull) << 24) \
  69. | (((x) & 0x000000000000ff00ull) << 40) \
  70. | (((x) & 0x00000000000000ffull) << 56))
  71. # define __bswap_64(x) \
  72. (__extension__ \
  73. ({ union { __extension__ unsigned long long int __ll; \
  74. unsigned int __l[2]; } __w, __r; \
  75. if (__builtin_constant_p (x)) \
  76. __r.__ll = __bswap_constant_64 (x); \
  77. else \
  78. { \
  79. __w.__ll = (x); \
  80. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  81. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  82. } \
  83. __r.__ll; }))
  84. #endif
  85. #endif /* _BITS_BYTESWAP_H */