You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.1KB

  1. #ifndef __BSWAP_H__
  2. #define __BSWAP_H__
  3. /* It's need for ffmpeg. Else where will be defined ARCH_X86?*/
  4. #include "config.h"
  5. #ifdef HAVE_BYTESWAP_H
  6. #include <byteswap.h>
  7. #else
  8. #include <inttypes.h>
  9. #ifdef ARCH_X86
  10. inline static unsigned short ByteSwap16(unsigned short x)
  11. {
  12. __asm("xchgb %b0,%h0" :
  13. "=q" (x) :
  14. "0" (x));
  15. return x;
  16. }
  17. #define bswap_16(x) ByteSwap16(x)
  18. inline static unsigned int ByteSwap32(unsigned int x)
  19. {
  20. #if __CPU__ > 386
  21. __asm("bswap %0":
  22. "=r" (x) :
  23. #else
  24. __asm("xchgb %b0,%h0\n"
  25. " rorl $16,%0\n"
  26. " xchgb %b0,%h0":
  27. "=q" (x) :
  28. #endif
  29. "0" (x));
  30. return x;
  31. }
  32. #define bswap_32(x) ByteSwap32(x)
  33. inline static unsigned long long int ByteSwap64(unsigned long long int x)
  34. {
  35. register union { __extension__ unsigned long long int __ll;
  36. unsigned long int __l[2]; } __x;
  37. asm("xchgl %0,%1":
  38. "=r"(__x.__l[0]),"=r"(__x.__l[1]):
  39. "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
  40. return __x.__ll;
  41. }
  42. #define bswap_64(x) ByteSwap64(x)
  43. #else
  44. #define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
  45. // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
  46. #define bswap_32(x) \
  47. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  48. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  49. #define bswap_64(x) \
  50. (__extension__ \
  51. ({ union { __extension__ unsigned long long int __ll; \
  52. unsigned long int __l[2]; } __w, __r; \
  53. __w.__ll = (x); \
  54. __r.__l[0] = bswap_32 (__w.__l[1]); \
  55. __r.__l[1] = bswap_32 (__w.__l[0]); \
  56. __r.__ll; }))
  57. #endif /* !ARCH_X86 */
  58. #endif /* !HAVE_BYTESWAP_H */
  59. // be2me ... BigEndian to MachineEndian
  60. // le2me ... LittleEndian to MachineEndian
  61. #ifdef WORDS_BIGENDIAN
  62. #define be2me_16(x) (x)
  63. #define be2me_32(x) (x)
  64. #define be2me_64(x) (x)
  65. #define le2me_16(x) bswap_16(x)
  66. #define le2me_32(x) bswap_32(x)
  67. #define le2me_64(x) bswap_64(x)
  68. #else
  69. #define be2me_16(x) bswap_16(x)
  70. #define be2me_32(x) bswap_32(x)
  71. #define be2me_64(x) bswap_64(x)
  72. #define le2me_16(x) (x)
  73. #define le2me_32(x) (x)
  74. #define le2me_64(x) (x)
  75. #endif
  76. #endif