SDL_endian.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. SDL - Simple DirectMedia Layer
  3. Copyright (C) 1997-2012 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Sam Lantinga
  16. slouken@libsdl.org
  17. */
  18. /**
  19. * @file SDL_endian.h
  20. * Functions for reading and writing endian-specific values
  21. */
  22. #ifndef _SDL_endian_h
  23. #define _SDL_endian_h
  24. #include "SDL_stdinc.h"
  25. /** @name SDL_ENDIANs
  26. * The two types of endianness
  27. */
  28. /*@{*/
  29. #define SDL_LIL_ENDIAN 1234
  30. #define SDL_BIG_ENDIAN 4321
  31. /*@}*/
  32. #ifndef SDL_BYTEORDER /* Not defined in SDL_config.h? */
  33. #ifdef __linux__
  34. #include <endian.h>
  35. #define SDL_BYTEORDER __BYTE_ORDER
  36. #else /* __linux __ */
  37. #if defined(__hppa__) || \
  38. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  39. (defined(__MIPS__) && defined(__MISPEB__)) || \
  40. defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
  41. defined(__sparc__)
  42. #define SDL_BYTEORDER SDL_BIG_ENDIAN
  43. #else
  44. #define SDL_BYTEORDER SDL_LIL_ENDIAN
  45. #endif
  46. #endif /* __linux __ */
  47. #endif /* !SDL_BYTEORDER */
  48. #include "begin_code.h"
  49. /* Set up for C function definitions, even when using C++ */
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53. /**
  54. * @name SDL_Swap Functions
  55. * Use inline functions for compilers that support them, and static
  56. * functions for those that do not. Because these functions become
  57. * static for compilers that do not support inline functions, this
  58. * header should only be included in files that actually use them.
  59. */
  60. /*@{*/
  61. #if defined(__GNUC__) && defined(__i386__) && \
  62. !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
  63. static __inline__ Uint16 SDL_Swap16(Uint16 x)
  64. {
  65. __asm__("xchgb %b0,%h0" : "=q" (x) : "0" (x));
  66. return x;
  67. }
  68. #elif defined(__GNUC__) && defined(__x86_64__)
  69. static __inline__ Uint16 SDL_Swap16(Uint16 x)
  70. {
  71. __asm__("xchgb %b0,%h0" : "=Q" (x) : "0" (x));
  72. return x;
  73. }
  74. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  75. static __inline__ Uint16 SDL_Swap16(Uint16 x)
  76. {
  77. Uint16 result;
  78. __asm__("rlwimi %0,%2,8,16,23" : "=&r" (result) : "0" (x >> 8), "r" (x));
  79. return result;
  80. }
  81. #elif defined(__GNUC__) && (defined(__m68k__) && !defined(__mcoldfire__))
  82. static __inline__ Uint16 SDL_Swap16(Uint16 x)
  83. {
  84. __asm__("rorw #8,%0" : "=d" (x) : "0" (x) : "cc");
  85. return x;
  86. }
  87. #else
  88. static __inline__ Uint16 SDL_Swap16(Uint16 x) {
  89. return SDL_static_cast(Uint16, ((x<<8)|(x>>8)));
  90. }
  91. #endif
  92. #if defined(__GNUC__) && defined(__i386__) && \
  93. !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
  94. static __inline__ Uint32 SDL_Swap32(Uint32 x)
  95. {
  96. __asm__("bswap %0" : "=r" (x) : "0" (x));
  97. return x;
  98. }
  99. #elif defined(__GNUC__) && defined(__x86_64__)
  100. static __inline__ Uint32 SDL_Swap32(Uint32 x)
  101. {
  102. __asm__("bswapl %0" : "=r" (x) : "0" (x));
  103. return x;
  104. }
  105. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  106. static __inline__ Uint32 SDL_Swap32(Uint32 x)
  107. {
  108. Uint32 result;
  109. __asm__("rlwimi %0,%2,24,16,23" : "=&r" (result) : "0" (x>>24), "r" (x));
  110. __asm__("rlwimi %0,%2,8,8,15" : "=&r" (result) : "0" (result), "r" (x));
  111. __asm__("rlwimi %0,%2,24,0,7" : "=&r" (result) : "0" (result), "r" (x));
  112. return result;
  113. }
  114. #elif defined(__GNUC__) && (defined(__m68k__) && !defined(__mcoldfire__))
  115. static __inline__ Uint32 SDL_Swap32(Uint32 x)
  116. {
  117. __asm__("rorw #8,%0\n\tswap %0\n\trorw #8,%0" : "=d" (x) : "0" (x) : "cc");
  118. return x;
  119. }
  120. #else
  121. static __inline__ Uint32 SDL_Swap32(Uint32 x) {
  122. return SDL_static_cast(Uint32, ((x<<24)|((x<<8)&0x00FF0000)|((x>>8)&0x0000FF00)|(x>>24)));
  123. }
  124. #endif
  125. #ifdef SDL_HAS_64BIT_TYPE
  126. #if defined(__GNUC__) && defined(__i386__) && \
  127. !(__GNUC__ == 2 && __GNUC_MINOR__ <= 95 /* broken gcc version */)
  128. static __inline__ Uint64 SDL_Swap64(Uint64 x)
  129. {
  130. union {
  131. struct { Uint32 a,b; } s;
  132. Uint64 u;
  133. } v;
  134. v.u = x;
  135. __asm__("bswapl %0 ; bswapl %1 ; xchgl %0,%1"
  136. : "=r" (v.s.a), "=r" (v.s.b)
  137. : "0" (v.s.a), "1" (v.s.b));
  138. return v.u;
  139. }
  140. #elif defined(__GNUC__) && defined(__x86_64__)
  141. static __inline__ Uint64 SDL_Swap64(Uint64 x)
  142. {
  143. __asm__("bswapq %0" : "=r" (x) : "0" (x));
  144. return x;
  145. }
  146. #else
  147. static __inline__ Uint64 SDL_Swap64(Uint64 x)
  148. {
  149. Uint32 hi, lo;
  150. /* Separate into high and low 32-bit values and swap them */
  151. lo = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  152. x >>= 32;
  153. hi = SDL_static_cast(Uint32, x & 0xFFFFFFFF);
  154. x = SDL_Swap32(lo);
  155. x <<= 32;
  156. x |= SDL_Swap32(hi);
  157. return (x);
  158. }
  159. #endif
  160. #else
  161. /* This is mainly to keep compilers from complaining in SDL code.
  162. * If there is no real 64-bit datatype, then compilers will complain about
  163. * the fake 64-bit datatype that SDL provides when it compiles user code.
  164. */
  165. #define SDL_Swap64(X) (X)
  166. #endif /* SDL_HAS_64BIT_TYPE */
  167. /*@}*/
  168. /**
  169. * @name SDL_SwapLE and SDL_SwapBE Functions
  170. * Byteswap item from the specified endianness to the native endianness
  171. */
  172. /*@{*/
  173. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  174. #define SDL_SwapLE16(X) (X)
  175. #define SDL_SwapLE32(X) (X)
  176. #define SDL_SwapLE64(X) (X)
  177. #define SDL_SwapBE16(X) SDL_Swap16(X)
  178. #define SDL_SwapBE32(X) SDL_Swap32(X)
  179. #define SDL_SwapBE64(X) SDL_Swap64(X)
  180. #else
  181. #define SDL_SwapLE16(X) SDL_Swap16(X)
  182. #define SDL_SwapLE32(X) SDL_Swap32(X)
  183. #define SDL_SwapLE64(X) SDL_Swap64(X)
  184. #define SDL_SwapBE16(X) (X)
  185. #define SDL_SwapBE32(X) (X)
  186. #define SDL_SwapBE64(X) (X)
  187. #endif
  188. /*@}*/
  189. /* Ends C function definitions when using C++ */
  190. #ifdef __cplusplus
  191. }
  192. #endif
  193. #include "close_code.h"
  194. #endif /* _SDL_endian_h */