common_funcs.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common_types.h"
  6. #include <cstdlib>
  7. #ifdef _WIN32
  8. #define SLEEP(x) Sleep(x)
  9. #else
  10. #include <unistd.h>
  11. #define SLEEP(x) usleep(x*1000)
  12. #endif
  13. #define b2(x) ( (x) | ( (x) >> 1) )
  14. #define b4(x) ( b2(x) | ( b2(x) >> 2) )
  15. #define b8(x) ( b4(x) | ( b4(x) >> 4) )
  16. #define b16(x) ( b8(x) | ( b8(x) >> 8) )
  17. #define b32(x) (b16(x) | (b16(x) >>16) )
  18. #define ROUND_UP_POW2(x) (b32(x - 1) + 1)
  19. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  20. /// Textually concatenates two tokens. The double-expansion is required by the C preprocessor.
  21. #define CONCAT2(x, y) DO_CONCAT2(x, y)
  22. #define DO_CONCAT2(x, y) x ## y
  23. // helper macro to properly align structure members.
  24. // Calling INSERT_PADDING_BYTES will add a new member variable with a name like "pad121",
  25. // depending on the current source line to make sure variable names are unique.
  26. #define INSERT_PADDING_BYTES(num_bytes) u8 CONCAT2(pad, __LINE__)[(num_bytes)]
  27. #define INSERT_PADDING_WORDS(num_words) u32 CONCAT2(pad, __LINE__)[(num_words)]
  28. #ifndef _MSC_VER
  29. #include <errno.h>
  30. #ifdef __linux__
  31. #include <byteswap.h>
  32. #elif defined __FreeBSD__
  33. #include <sys/endian.h>
  34. #endif
  35. #if defined(__x86_64__) || defined(_M_X64)
  36. #define Crash() __asm__ __volatile__("int $3")
  37. #elif defined(_M_ARM)
  38. #define Crash() __asm__ __volatile__("trap")
  39. #else
  40. #define Crash() exit(1)
  41. #endif
  42. // GCC 4.8 defines all the rotate functions now
  43. // Small issue with GCC's lrotl/lrotr intrinsics is they are still 32bit while we require 64bit
  44. #ifndef _rotl
  45. inline u32 _rotl(u32 x, int shift) {
  46. shift &= 31;
  47. if (!shift) return x;
  48. return (x << shift) | (x >> (32 - shift));
  49. }
  50. inline u32 _rotr(u32 x, int shift) {
  51. shift &= 31;
  52. if (!shift) return x;
  53. return (x >> shift) | (x << (32 - shift));
  54. }
  55. #endif
  56. inline u64 _rotl64(u64 x, unsigned int shift){
  57. unsigned int n = shift % 64;
  58. return (x << n) | (x >> (64 - n));
  59. }
  60. inline u64 _rotr64(u64 x, unsigned int shift){
  61. unsigned int n = shift % 64;
  62. return (x >> n) | (x << (64 - n));
  63. }
  64. #else // _MSC_VER
  65. #include <locale.h>
  66. // Function Cross-Compatibility
  67. #define strcasecmp _stricmp
  68. #define strncasecmp _strnicmp
  69. #define unlink _unlink
  70. #define snprintf _snprintf
  71. #define vscprintf _vscprintf
  72. // Locale Cross-Compatibility
  73. #define locale_t _locale_t
  74. #define freelocale _free_locale
  75. #define newlocale(mask, locale, base) _create_locale(mask, locale)
  76. #define LC_GLOBAL_LOCALE ((locale_t)-1)
  77. #define LC_ALL_MASK LC_ALL
  78. #define LC_COLLATE_MASK LC_COLLATE
  79. #define LC_CTYPE_MASK LC_CTYPE
  80. #define LC_MONETARY_MASK LC_MONETARY
  81. #define LC_NUMERIC_MASK LC_NUMERIC
  82. #define LC_TIME_MASK LC_TIME
  83. inline locale_t uselocale(locale_t new_locale)
  84. {
  85. // Retrieve the current per thread locale setting
  86. bool bIsPerThread = (_configthreadlocale(0) == _ENABLE_PER_THREAD_LOCALE);
  87. // Retrieve the current thread-specific locale
  88. locale_t old_locale = bIsPerThread ? _get_current_locale() : LC_GLOBAL_LOCALE;
  89. if(new_locale == LC_GLOBAL_LOCALE)
  90. {
  91. // Restore the global locale
  92. _configthreadlocale(_DISABLE_PER_THREAD_LOCALE);
  93. }
  94. else if(new_locale != nullptr)
  95. {
  96. // Configure the thread to set the locale only for this thread
  97. _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
  98. // Set all locale categories
  99. for(int i = LC_MIN; i <= LC_MAX; i++)
  100. setlocale(i, new_locale->locinfo->lc_category[i].locale);
  101. }
  102. return old_locale;
  103. }
  104. // 64 bit offsets for windows
  105. #define fseeko _fseeki64
  106. #define ftello _ftelli64
  107. #define atoll _atoi64
  108. #define stat64 _stat64
  109. #define fstat64 _fstat64
  110. #define fileno _fileno
  111. extern "C" {
  112. __declspec(dllimport) void __stdcall DebugBreak(void);
  113. }
  114. #define Crash() {DebugBreak();}
  115. #endif // _MSC_VER ndef
  116. // Generic function to get last error message.
  117. // Call directly after the command or use the error num.
  118. // This function might change the error code.
  119. // Defined in Misc.cpp.
  120. const char* GetLastErrorMsg();
  121. namespace Common
  122. {
  123. inline u8 swap8(u8 _data) {return _data;}
  124. inline u32 swap24(const u8* _data) {return (_data[0] << 16) | (_data[1] << 8) | _data[2];}
  125. #ifdef ANDROID
  126. #undef swap16
  127. #undef swap32
  128. #undef swap64
  129. #endif
  130. #ifdef _MSC_VER
  131. inline u16 swap16(u16 _data) {return _byteswap_ushort(_data);}
  132. inline u32 swap32(u32 _data) {return _byteswap_ulong (_data);}
  133. inline u64 swap64(u64 _data) {return _byteswap_uint64(_data);}
  134. #elif _M_ARM
  135. inline u16 swap16 (u16 _data) { u32 data = _data; __asm__ ("rev16 %0, %1\n" : "=l" (data) : "l" (data)); return (u16)data;}
  136. inline u32 swap32 (u32 _data) {__asm__ ("rev %0, %1\n" : "=l" (_data) : "l" (_data)); return _data;}
  137. inline u64 swap64(u64 _data) {return ((u64)swap32(_data) << 32) | swap32(_data >> 32);}
  138. #elif __linux__
  139. inline u16 swap16(u16 _data) {return bswap_16(_data);}
  140. inline u32 swap32(u32 _data) {return bswap_32(_data);}
  141. inline u64 swap64(u64 _data) {return bswap_64(_data);}
  142. #elif __APPLE__
  143. inline __attribute__((always_inline)) u16 swap16(u16 _data)
  144. {return (_data >> 8) | (_data << 8);}
  145. inline __attribute__((always_inline)) u32 swap32(u32 _data)
  146. {return __builtin_bswap32(_data);}
  147. inline __attribute__((always_inline)) u64 swap64(u64 _data)
  148. {return __builtin_bswap64(_data);}
  149. #elif __FreeBSD__
  150. inline u16 swap16(u16 _data) {return bswap16(_data);}
  151. inline u32 swap32(u32 _data) {return bswap32(_data);}
  152. inline u64 swap64(u64 _data) {return bswap64(_data);}
  153. #else
  154. // Slow generic implementation.
  155. inline u16 swap16(u16 data) {return (data >> 8) | (data << 8);}
  156. inline u32 swap32(u32 data) {return (swap16(data) << 16) | swap16(data >> 16);}
  157. inline u64 swap64(u64 data) {return ((u64)swap32(data) << 32) | swap32(data >> 32);}
  158. #endif
  159. inline u16 swap16(const u8* _pData) {return swap16(*(const u16*)_pData);}
  160. inline u32 swap32(const u8* _pData) {return swap32(*(const u32*)_pData);}
  161. inline u64 swap64(const u8* _pData) {return swap64(*(const u64*)_pData);}
  162. template <int count>
  163. void swap(u8*);
  164. template <>
  165. inline void swap<1>(u8* data)
  166. {}
  167. template <>
  168. inline void swap<2>(u8* data)
  169. {
  170. *reinterpret_cast<u16*>(data) = swap16(data);
  171. }
  172. template <>
  173. inline void swap<4>(u8* data)
  174. {
  175. *reinterpret_cast<u32*>(data) = swap32(data);
  176. }
  177. template <>
  178. inline void swap<8>(u8* data)
  179. {
  180. *reinterpret_cast<u64*>(data) = swap64(data);
  181. }
  182. } // Namespace Common