SDL_endian.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * \file SDL_endian.h
  20. *
  21. * Functions for reading and writing endian-specific values
  22. */
  23. #ifndef _SDL_endian_h
  24. #define _SDL_endian_h
  25. #include "SDL_stdinc.h"
  26. /**
  27. * \name The two types of endianness
  28. */
  29. /* @{ */
  30. #define SDL_LIL_ENDIAN 1234
  31. #define SDL_BIG_ENDIAN 4321
  32. /* @} */
  33. #ifndef SDL_BYTEORDER /* Not defined in SDL_config.h? */
  34. #ifdef __linux__
  35. #include <endian.h>
  36. #define SDL_BYTEORDER __BYTE_ORDER
  37. #else /* __linux __ */
  38. #if defined(__hppa__) || \
  39. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  40. (defined(__MIPS__) && defined(__MISPEB__)) || \
  41. defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
  42. defined(__sparc__)
  43. #define SDL_BYTEORDER SDL_BIG_ENDIAN
  44. #else
  45. #define SDL_BYTEORDER SDL_LIL_ENDIAN
  46. #endif
  47. #endif /* __linux __ */
  48. #endif /* !SDL_BYTEORDER */
  49. #include "begin_code.h"
  50. /* Set up for C function definitions, even when using C++ */
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /**
  55. * \file SDL_endian.h
  56. */
  57. #if defined(__GNUC__) && defined(__i386__) && \
  58. !(__GNUC__ == 2 && __GNUC_MINOR__ == 95 /* broken gcc version */)
  59. SDL_FORCE_INLINE Uint16
  60. SDL_Swap16(Uint16 x)
  61. {
  62. __asm__("xchgb %b0,%h0": "=q"(x):"0"(x));
  63. return x;
  64. }
  65. #elif defined(__GNUC__) && defined(__x86_64__)
  66. SDL_FORCE_INLINE Uint16
  67. SDL_Swap16(Uint16 x)
  68. {
  69. __asm__("xchgb %b0,%h0": "=Q"(x):"0"(x));
  70. return x;
  71. }
  72. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  73. SDL_FORCE_INLINE Uint16
  74. SDL_Swap16(Uint16 x)
  75. {
  76. int result;
  77. __asm__("rlwimi %0,%2,8,16,23": "=&r"(result):"0"(x >> 8), "r"(x));
  78. return (Uint16)result;
  79. }
  80. #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
  81. SDL_FORCE_INLINE Uint16
  82. SDL_Swap16(Uint16 x)
  83. {
  84. __asm__("rorw #8,%0": "=d"(x): "0"(x):"cc");
  85. return x;
  86. }
  87. #else
  88. SDL_FORCE_INLINE Uint16
  89. SDL_Swap16(Uint16 x)
  90. {
  91. return SDL_static_cast(Uint16, ((x << 8) | (x >> 8)));
  92. }
  93. #endif
  94. #if defined(__GNUC__) && defined(__i386__)
  95. SDL_FORCE_INLINE Uint32
  96. SDL_Swap32(Uint32 x)
  97. {
  98. __asm__("bswap %0": "=r"(x):"0"(x));
  99. return x;
  100. }
  101. #elif defined(__GNUC__) && defined(__x86_64__)
  102. SDL_FORCE_INLINE Uint32
  103. SDL_Swap32(Uint32 x)
  104. {
  105. __asm__("bswapl %0": "=r"(x):"0"(x));
  106. return x;
  107. }
  108. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  109. SDL_FORCE_INLINE Uint32
  110. SDL_Swap32(Uint32 x)
  111. {
  112. Uint32 result;
  113. __asm__("rlwimi %0,%2,24,16,23": "=&r"(result):"0"(x >> 24), "r"(x));
  114. __asm__("rlwimi %0,%2,8,8,15": "=&r"(result):"0"(result), "r"(x));
  115. __asm__("rlwimi %0,%2,24,0,7": "=&r"(result):"0"(result), "r"(x));
  116. return result;
  117. }
  118. #elif defined(__GNUC__) && (defined(__M68000__) || defined(__M68020__)) && !defined(__mcoldfire__)
  119. SDL_FORCE_INLINE Uint32
  120. SDL_Swap32(Uint32 x)
  121. {
  122. __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0": "=d"(x): "0"(x):"cc");
  123. return x;
  124. }
  125. #else
  126. SDL_FORCE_INLINE Uint32
  127. SDL_Swap32(Uint32 x)
  128. {
  129. return SDL_static_cast(Uint32, ((x << 24) | ((x << 8) & 0x00FF0000) |
  130. ((x >> 8) & 0x0000FF00) | (x >> 24)));
  131. }
  132. #endif
  133. #if defined(__GNUC__) && defined(__i386__)
  134. SDL_FORCE_INLINE Uint64
  135. SDL_Swap64(Uint64 x)
  136. {
  137. union
  138. {
  139. struct
  140. {
  141. Uint32 a, b;
  142. } s;
  143. Uint64 u;
  144. } v;
  145. v.u = x;
  146. __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1": "=r"(v.s.a), "=r"(v.s.b):"0"(v.s.a),
  147. "1"(v.s.
  148. b));
  149. return v.u;
  150. }
  151. #elif defined(__GNUC__) && defined(__x86_64__)
  152. SDL_FORCE_INLINE Uint64
  153. SDL_Swap64(Uint64 x)
  154. {
  155. __asm__("bswapq %0": "=r"(x):"0"(x));
  156. return x;
  157. }
  158. #else
  159. SDL_FORCE_INLINE Uint64
  160. SDL_Swap64(Uint64 x)
  161. {
  162. Uint32 hi, lo;
  163. /* Separate into high and low 32-bit values and swap them */
  164. lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  165. x >>= 32;
  166. hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  167. x = SDL_Swap32(lo);
  168. x <<= 32;
  169. x |= SDL_Swap32(hi);
  170. return (x);
  171. }
  172. #endif
  173. SDL_FORCE_INLINE float
  174. SDL_SwapFloat(float x)
  175. {
  176. union
  177. {
  178. float f;
  179. Uint32 ui32;
  180. } swapper;
  181. swapper.f = x;
  182. swapper.ui32 = SDL_Swap32(swapper.ui32);
  183. return swapper.f;
  184. }
  185. /**
  186. * \name Swap to native
  187. * Byteswap item from the specified endianness to the native endianness.
  188. */
  189. /* @{ */
  190. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  191. #define SDL_SwapLE16(X) (X)
  192. #define SDL_SwapLE32(X) (X)
  193. #define SDL_SwapLE64(X) (X)
  194. #define SDL_SwapFloatLE(X) (X)
  195. #define SDL_SwapBE16(X) SDL_Swap16(X)
  196. #define SDL_SwapBE32(X) SDL_Swap32(X)
  197. #define SDL_SwapBE64(X) SDL_Swap64(X)
  198. #define SDL_SwapFloatBE(X) SDL_SwapFloat(X)
  199. #else
  200. #define SDL_SwapLE16(X) SDL_Swap16(X)
  201. #define SDL_SwapLE32(X) SDL_Swap32(X)
  202. #define SDL_SwapLE64(X) SDL_Swap64(X)
  203. #define SDL_SwapFloatLE(X) SDL_SwapFloat(X)
  204. #define SDL_SwapBE16(X) (X)
  205. #define SDL_SwapBE32(X) (X)
  206. #define SDL_SwapBE64(X) (X)
  207. #define SDL_SwapFloatBE(X) (X)
  208. #endif
  209. /* @} *//* Swap to native */
  210. /* Ends C function definitions when using C++ */
  211. #ifdef __cplusplus
  212. }
  213. #endif
  214. #include "close_code.h"
  215. #endif /* _SDL_endian_h */
  216. /* vi: set ts=4 sw=4 expandtab: */