retro_endianness.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Copyright (C) 2010-2018 The RetroArch team
  2. *
  3. * ---------------------------------------------------------------------------------------
  4. * The following license statement only applies to this file (retro_endianness.h).
  5. * ---------------------------------------------------------------------------------------
  6. *
  7. * Permission is hereby granted, free of charge,
  8. * to any person obtaining a copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  11. * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #ifndef __LIBRETRO_SDK_ENDIANNESS_H
  23. #define __LIBRETRO_SDK_ENDIANNESS_H
  24. #include <retro_inline.h>
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #if defined(_MSC_VER) && _MSC_VER > 1200
  28. #define SWAP16 _byteswap_ushort
  29. #define SWAP32 _byteswap_ulong
  30. #else
  31. #define SWAP16(x) ((uint16_t)( \
  32. (((uint16_t)(x) & 0x00ff) << 8) | \
  33. (((uint16_t)(x) & 0xff00) >> 8) \
  34. ))
  35. #define SWAP32(x) ((uint32_t)( \
  36. (((uint32_t)(x) & 0x000000ff) << 24) | \
  37. (((uint32_t)(x) & 0x0000ff00) << 8) | \
  38. (((uint32_t)(x) & 0x00ff0000) >> 8) | \
  39. (((uint32_t)(x) & 0xff000000) >> 24) \
  40. ))
  41. #endif
  42. #if defined(_MSC_VER) && _MSC_VER <= 1200
  43. #define SWAP64(val) \
  44. ((((uint64_t)(val) & 0x00000000000000ff) << 56) \
  45. | (((uint64_t)(val) & 0x000000000000ff00) << 40) \
  46. | (((uint64_t)(val) & 0x0000000000ff0000) << 24) \
  47. | (((uint64_t)(val) & 0x00000000ff000000) << 8) \
  48. | (((uint64_t)(val) & 0x000000ff00000000) >> 8) \
  49. | (((uint64_t)(val) & 0x0000ff0000000000) >> 24) \
  50. | (((uint64_t)(val) & 0x00ff000000000000) >> 40) \
  51. | (((uint64_t)(val) & 0xff00000000000000) >> 56))
  52. #else
  53. #define SWAP64(val) \
  54. ((((uint64_t)(val) & 0x00000000000000ffULL) << 56) \
  55. | (((uint64_t)(val) & 0x000000000000ff00ULL) << 40) \
  56. | (((uint64_t)(val) & 0x0000000000ff0000ULL) << 24) \
  57. | (((uint64_t)(val) & 0x00000000ff000000ULL) << 8) \
  58. | (((uint64_t)(val) & 0x000000ff00000000ULL) >> 8) \
  59. | (((uint64_t)(val) & 0x0000ff0000000000ULL) >> 24) \
  60. | (((uint64_t)(val) & 0x00ff000000000000ULL) >> 40) \
  61. | (((uint64_t)(val) & 0xff00000000000000ULL) >> 56))
  62. #endif
  63. /**
  64. * is_little_endian:
  65. *
  66. * Checks if the system is little endian or big-endian.
  67. *
  68. * Returns: greater than 0 if little-endian,
  69. * otherwise big-endian.
  70. **/
  71. #if defined(MSB_FIRST)
  72. #define is_little_endian() (0)
  73. #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
  74. #define is_little_endian() (1)
  75. #else
  76. static INLINE uint8_t is_little_endian(void)
  77. {
  78. union
  79. {
  80. uint16_t x;
  81. uint8_t y[2];
  82. } u;
  83. u.x = 1;
  84. return u.y[0];
  85. }
  86. #endif
  87. /**
  88. * swap_if_big64:
  89. * @val : unsigned 64-bit value
  90. *
  91. * Byteswap unsigned 64-bit value if system is big-endian.
  92. *
  93. * Returns: Byteswapped value in case system is big-endian,
  94. * otherwise returns same value.
  95. **/
  96. #if defined(MSB_FIRST)
  97. #define swap_if_big64(val) (SWAP64(val))
  98. #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
  99. #define swap_if_big64(val) (val)
  100. #else
  101. static INLINE uint64_t swap_if_big64(uint64_t val)
  102. {
  103. if (is_little_endian())
  104. return val;
  105. return SWAP64(val);
  106. }
  107. #endif
  108. /**
  109. * swap_if_big32:
  110. * @val : unsigned 32-bit value
  111. *
  112. * Byteswap unsigned 32-bit value if system is big-endian.
  113. *
  114. * Returns: Byteswapped value in case system is big-endian,
  115. * otherwise returns same value.
  116. **/
  117. #if defined(MSB_FIRST)
  118. #define swap_if_big32(val) (SWAP32(val))
  119. #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
  120. #define swap_if_big32(val) (val)
  121. #else
  122. static INLINE uint32_t swap_if_big32(uint32_t val)
  123. {
  124. if (is_little_endian())
  125. return val;
  126. return SWAP32(val);
  127. }
  128. #endif
  129. /**
  130. * swap_if_little64:
  131. * @val : unsigned 64-bit value
  132. *
  133. * Byteswap unsigned 64-bit value if system is little-endian.
  134. *
  135. * Returns: Byteswapped value in case system is little-endian,
  136. * otherwise returns same value.
  137. **/
  138. #if defined(MSB_FIRST)
  139. #define swap_if_little64(val) (val)
  140. #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
  141. #define swap_if_little64(val) (SWAP64(val))
  142. #else
  143. static INLINE uint64_t swap_if_little64(uint64_t val)
  144. {
  145. if (is_little_endian())
  146. return SWAP64(val);
  147. return val;
  148. }
  149. #endif
  150. /**
  151. * swap_if_little32:
  152. * @val : unsigned 32-bit value
  153. *
  154. * Byteswap unsigned 32-bit value if system is little-endian.
  155. *
  156. * Returns: Byteswapped value in case system is little-endian,
  157. * otherwise returns same value.
  158. **/
  159. #if defined(MSB_FIRST)
  160. #define swap_if_little32(val) (val)
  161. #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
  162. #define swap_if_little32(val) (SWAP32(val))
  163. #else
  164. static INLINE uint32_t swap_if_little32(uint32_t val)
  165. {
  166. if (is_little_endian())
  167. return SWAP32(val);
  168. return val;
  169. }
  170. #endif
  171. /**
  172. * swap_if_big16:
  173. * @val : unsigned 16-bit value
  174. *
  175. * Byteswap unsigned 16-bit value if system is big-endian.
  176. *
  177. * Returns: Byteswapped value in case system is big-endian,
  178. * otherwise returns same value.
  179. **/
  180. #if defined(MSB_FIRST)
  181. #define swap_if_big16(val) (SWAP16(val))
  182. #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
  183. #define swap_if_big16(val) (val)
  184. #else
  185. static INLINE uint16_t swap_if_big16(uint16_t val)
  186. {
  187. if (is_little_endian())
  188. return val;
  189. return SWAP16(val);
  190. }
  191. #endif
  192. /**
  193. * swap_if_little16:
  194. * @val : unsigned 16-bit value
  195. *
  196. * Byteswap unsigned 16-bit value if system is little-endian.
  197. *
  198. * Returns: Byteswapped value in case system is little-endian,
  199. * otherwise returns same value.
  200. **/
  201. #if defined(MSB_FIRST)
  202. #define swap_if_little16(val) (val)
  203. #elif defined(__x86_64) || defined(__i386) || defined(_M_IX86) || defined(_M_X64)
  204. #define swap_if_little16(val) (SWAP16(val))
  205. #else
  206. static INLINE uint16_t swap_if_little16(uint16_t val)
  207. {
  208. if (is_little_endian())
  209. return SWAP16(val);
  210. return val;
  211. }
  212. #endif
  213. /**
  214. * store32be:
  215. * @addr : pointer to unsigned 32-bit buffer
  216. * @data : unsigned 32-bit value to write
  217. *
  218. * Write data to address. Endian-safe. Byteswaps the data
  219. * first if necessary before storing it.
  220. **/
  221. static INLINE void store32be(uint32_t *addr, uint32_t data)
  222. {
  223. *addr = swap_if_little32(data);
  224. }
  225. /**
  226. * load32be:
  227. * @addr : pointer to unsigned 32-bit buffer
  228. *
  229. * Load value from address. Endian-safe.
  230. *
  231. * Returns: value from address, byte-swapped if necessary.
  232. **/
  233. static INLINE uint32_t load32be(const uint32_t *addr)
  234. {
  235. return swap_if_little32(*addr);
  236. }
  237. #endif