byteswap.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <<= 8;" \
  25. "%1.L = %0.L + %0.H (NS);" \
  26. : "+d" (__x), "=d" (__v)); \
  27. __v; }))
  28. #else
  29. static __inline unsigned short int
  30. __bswap_16 (unsigned short int __bsx)
  31. {
  32. return __bswap_constant_16 (__bsx);
  33. }
  34. #endif
  35. /* Swap bytes in 32 bit value. */
  36. #define __bswap_constant_32(x) \
  37. ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) | \
  38. (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))
  39. #ifdef __GNUC__
  40. # define __bswap_32(x) \
  41. (__extension__ \
  42. ({ register unsigned int __v, __x = (x); \
  43. if (__builtin_constant_p (__x)) \
  44. __v = __bswap_constant_32 (__x); \
  45. else \
  46. __asm__ ("%1 = %0 >> 8 (V);" \
  47. "%0 = %0 << 8 (V);" \
  48. "%0 = %0 | %1;" \
  49. "%1 = PACK(%0.L, %0.H);" \
  50. : "+d"(__x), "=&d"(__v)); \
  51. __v; }))
  52. #else
  53. static __inline unsigned int
  54. __bswap_32 (unsigned int __bsx)
  55. {
  56. return __bswap_constant_32 (__bsx);
  57. }
  58. #endif
  59. #if defined __GNUC__ && __GNUC__ >= 2
  60. /* Swap bytes in 64 bit value. */
  61. # define __bswap_constant_64(x) \
  62. ((((x) & 0xff00000000000000ull) >> 56) \
  63. | (((x) & 0x00ff000000000000ull) >> 40) \
  64. | (((x) & 0x0000ff0000000000ull) >> 24) \
  65. | (((x) & 0x000000ff00000000ull) >> 8) \
  66. | (((x) & 0x00000000ff000000ull) << 8) \
  67. | (((x) & 0x0000000000ff0000ull) << 24) \
  68. | (((x) & 0x000000000000ff00ull) << 40) \
  69. | (((x) & 0x00000000000000ffull) << 56))
  70. # define __bswap_64(x) \
  71. (__extension__ \
  72. ({ union { __extension__ unsigned long long int __ll; \
  73. unsigned int __l[2]; } __w, __r; \
  74. if (__builtin_constant_p (x)) \
  75. __r.__ll = __bswap_constant_64 (x); \
  76. else \
  77. { \
  78. __w.__ll = (x); \
  79. __r.__l[0] = __bswap_32 (__w.__l[1]); \
  80. __r.__l[1] = __bswap_32 (__w.__l[0]); \
  81. } \
  82. __r.__ll; }))
  83. #endif
  84. #endif /* _BITS_BYTESWAP_H */