SDL_endian.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifndef _SDL_endian_h
  2. #define _SDL_endian_h
  3. #include "SDL_stdinc.h"
  4. #define SDL_LIL_ENDIAN 1234
  5. #define SDL_BIG_ENDIAN 4321
  6. #ifndef SDL_BYTEORDER
  7. #ifdef __linux__
  8. #include <endian.h>
  9. #define SDL_BYTEORDER __BYTE_ORDER
  10. #else
  11. #if defined(__hppa__) || \
  12. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  13. (defined(__MIPS__) && defined(__MISPEB__)) || \
  14. defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
  15. defined(__sparc__)
  16. #define SDL_BYTEORDER SDL_BIG_ENDIAN
  17. #else
  18. #define SDL_BYTEORDER SDL_LIL_ENDIAN
  19. #endif
  20. #endif
  21. #endif
  22. #include "begin_code.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #if defined(__GNUC__) && defined(__i386__) && \
  27. !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 )
  28. SDL_FORCE_INLINE Uint16
  29. SDL_Swap16(Uint16 x)
  30. {
  31. __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
  32. return x;
  33. }
  34. #elif defined(__GNUC__) && defined(__x86_64__)
  35. SDL_FORCE_INLINE Uint16
  36. SDL_Swap16(Uint16 x)
  37. {
  38. __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
  39. return x;
  40. }
  41. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  42. SDL_FORCE_INLINE Uint16
  43. SDL_Swap16(Uint16 x)
  44. {
  45. int result;
  46. __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
  47. return (Uint16)result;
  48. }
  49. #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
  50. SDL_FORCE_INLINE Uint16
  51. SDL_Swap16(Uint16 x)
  52. {
  53. __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
  54. return x;
  55. }
  56. #else
  57. SDL_FORCE_INLINE Uint16
  58. SDL_Swap16(Uint16 x)
  59. {
  60. return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
  61. }
  62. #endif
  63. #if defined(__GNUC__) && defined(__i386__)
  64. SDL_FORCE_INLINE Uint32
  65. SDL_Swap32(Uint32 x)
  66. {
  67. __asm__("bswap %0": "=r"(x):"0"(x));
  68. return x;
  69. }
  70. #elif defined(__GNUC__) && defined(__x86_64__)
  71. SDL_FORCE_INLINE Uint32
  72. SDL_Swap32(Uint32 x)
  73. {
  74. __asm__("bswapl %0": "=r"(x):"0"(x));
  75. return x;
  76. }
  77. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  78. SDL_FORCE_INLINE Uint32
  79. SDL_Swap32(Uint32 x)
  80. {
  81. Uint32 result;
  82. __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
  83. __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
  84. __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
  85. return result;
  86. }
  87. #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
  88. SDL_FORCE_INLINE Uint32
  89. SDL_Swap32(Uint32 x)
  90. {
  91. __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
  92. return x;
  93. }
  94. #else
  95. SDL_FORCE_INLINE Uint32
  96. SDL_Swap32(Uint32 x)
  97. {
  98. return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
  99. ((x >> 8) & 0x0000FF00) | (x >> 24)));
  100. }
  101. #endif
  102. #if defined(__GNUC__) && defined(__i386__)
  103. SDL_FORCE_INLINE Uint64
  104. SDL_Swap64(Uint64 x)
  105. {
  106. union
  107. {
  108. struct
  109. {
  110. Uint32 a, b;
  111. } s;
  112. Uint64 u;
  113. } v;
  114. v.u = x;
  115. __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
  116. "1"(v.s.
  117. b));
  118. return v.u;
  119. }
  120. #elif defined(__GNUC__) && defined(__x86_64__)
  121. SDL_FORCE_INLINE Uint64
  122. SDL_Swap64(Uint64 x)
  123. {
  124. __asm__("bswapq %0": "=r"(x):"0"(x));
  125. return x;
  126. }
  127. #else
  128. SDL_FORCE_INLINE Uint64
  129. SDL_Swap64(Uint64 x)
  130. {
  131. Uint32 hi, lo;
  132. lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  133. x >>= 32;
  134. hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  135. x = SDL_Swap32(lo);
  136. x <<= 32;
  137. x |= SDL_Swap32(hi);
  138. return (x);
  139. }
  140. #endif
  141. SDL_FORCE_INLINE float
  142. SDL_SwapFloat(float x)
  143. {
  144. union
  145. {
  146. float f;
  147. Uint32 ui32;
  148. } swapper;
  149. swapper.f = x;
  150. swapper.ui32 = SDL_Swap32(swapper.ui32);
  151. return swapper.f;
  152. }
  153. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  154. #define SDL_SwapLE16(X) (X)
  155. #define SDL_SwapLE32(X) (X)
  156. #define SDL_SwapLE64(X) (X)
  157. #define SDL_SwapFloatLE(X) (X)
  158. #define SDL_SwapBE16(X) SDL_Swap16(X)
  159. #define SDL_SwapBE32(X) SDL_Swap32(X)
  160. #define SDL_SwapBE64(X) SDL_Swap64(X)
  161. #define SDL_SwapFloatBE(X) SDL_SwapFloat(X)
  162. #else
  163. #define SDL_SwapLE16(X) SDL_Swap16(X)
  164. #define SDL_SwapLE32(X) SDL_Swap32(X)
  165. #define SDL_SwapLE64(X) SDL_Swap64(X)
  166. #define SDL_SwapFloatLE(X) SDL_SwapFloat(X)
  167. #define SDL_SwapBE16(X) (X)
  168. #define SDL_SwapBE32(X) (X)
  169. #define SDL_SwapBE64(X) (X)
  170. #define SDL_SwapFloatBE(X) (X)
  171. #endif
  172. #ifdef __cplusplus
  173. }
  174. #endif
  175. #include "close_code.h"
  176. #endif