common_funcs.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2013 Dolphin Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #ifdef _WIN32
  6. #define SLEEP(x) Sleep(x)
  7. #else
  8. #include <unistd.h>
  9. #define SLEEP(x) usleep(x*1000)
  10. #endif
  11. template <bool> struct CompileTimeAssert;
  12. template<> struct CompileTimeAssert<true> {};
  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 MIN(a, b) ((a)<(b)?(a):(b))
  20. #define MAX(a, b) ((a)>(b)?(a):(b))
  21. #define CLAMP(x, min, max) (((x) > max) ? max : (((x) < min) ? min : (x)))
  22. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  23. #ifndef _WIN32
  24. #include <errno.h>
  25. #ifdef __linux__
  26. #include <byteswap.h>
  27. #elif defined __FreeBSD__
  28. #include <sys/endian.h>
  29. #endif
  30. // go to debugger mode
  31. #ifdef GEKKO
  32. #define Crash()
  33. #elif defined _M_GENERIC
  34. #define Crash() { exit(1); }
  35. #else
  36. #define Crash() {asm ("int $3");}
  37. #endif
  38. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  39. // GCC 4.8 defines all the rotate functions now
  40. // Small issue with GCC's lrotl/lrotr intrinsics is they are still 32bit while we require 64bit
  41. #ifndef _rotl
  42. inline u32 _rotl(u32 x, int shift) {
  43. shift &= 31;
  44. if (!shift) return x;
  45. return (x << shift) | (x >> (32 - shift));
  46. }
  47. inline u32 _rotr(u32 x, int shift) {
  48. shift &= 31;
  49. if (!shift) return x;
  50. return (x >> shift) | (x << (32 - shift));
  51. }
  52. #endif
  53. inline u64 _rotl64(u64 x, unsigned int shift){
  54. unsigned int n = shift % 64;
  55. return (x << n) | (x >> (64 - n));
  56. }
  57. inline u64 _rotr64(u64 x, unsigned int shift){
  58. unsigned int n = shift % 64;
  59. return (x >> n) | (x << (64 - n));
  60. }
  61. #else // WIN32
  62. // Function Cross-Compatibility
  63. #define strcasecmp _stricmp
  64. #define strncasecmp _strnicmp
  65. #define unlink _unlink
  66. #define snprintf _snprintf
  67. #define vscprintf _vscprintf
  68. // Locale Cross-Compatibility
  69. #define locale_t _locale_t
  70. #define freelocale _free_locale
  71. #define newlocale(mask, locale, base) _create_locale(mask, locale)
  72. #define LC_GLOBAL_LOCALE ((locale_t)-1)
  73. #define LC_ALL_MASK LC_ALL
  74. #define LC_COLLATE_MASK LC_COLLATE
  75. #define LC_CTYPE_MASK LC_CTYPE
  76. #define LC_MONETARY_MASK LC_MONETARY
  77. #define LC_NUMERIC_MASK LC_NUMERIC
  78. #define LC_TIME_MASK LC_TIME
  79. inline locale_t uselocale(locale_t new_locale)
  80. {
  81. // Retrieve the current per thread locale setting
  82. bool bIsPerThread = (_configthreadlocale(0) == _ENABLE_PER_THREAD_LOCALE);
  83. // Retrieve the current thread-specific locale
  84. locale_t old_locale = bIsPerThread ? _get_current_locale() : LC_GLOBAL_LOCALE;
  85. if(new_locale == LC_GLOBAL_LOCALE)
  86. {
  87. // Restore the global locale
  88. _configthreadlocale(_DISABLE_PER_THREAD_LOCALE);
  89. }
  90. else if(new_locale != NULL)
  91. {
  92. // Configure the thread to set the locale only for this thread
  93. _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
  94. // Set all locale categories
  95. for(int i = LC_MIN; i <= LC_MAX; i++)
  96. setlocale(i, new_locale->locinfo->lc_category[i].locale);
  97. }
  98. return old_locale;
  99. }
  100. // 64 bit offsets for windows
  101. #define fseeko _fseeki64
  102. #define ftello _ftelli64
  103. #define atoll _atoi64
  104. #define stat64 _stat64
  105. #define fstat64 _fstat64
  106. #define fileno _fileno
  107. #if _M_IX86
  108. #define Crash() {__asm int 3}
  109. #else
  110. extern "C" {
  111. __declspec(dllimport) void __stdcall DebugBreak(void);
  112. }
  113. #define Crash() {DebugBreak();}
  114. #endif // M_IX86
  115. #endif // WIN32 ndef
  116. // Dolphin's min and max functions
  117. #undef min
  118. #undef max
  119. template<class T>
  120. inline T min(const T& a, const T& b) {return a > b ? b : a;}
  121. template<class T>
  122. inline T max(const T& a, const T& b) {return a > b ? a : b;}
  123. // Generic function to get last error message.
  124. // Call directly after the command or use the error num.
  125. // This function might change the error code.
  126. // Defined in Misc.cpp.
  127. const char* GetLastErrorMsg();
  128. namespace Common
  129. {
  130. inline u8 swap8(u8 _data) {return _data;}
  131. inline u32 swap24(const u8* _data) {return (_data[0] << 16) | (_data[1] << 8) | _data[2];}
  132. #ifdef ANDROID
  133. #undef swap16
  134. #undef swap32
  135. #undef swap64
  136. #endif
  137. #ifdef _WIN32
  138. inline u16 swap16(u16 _data) {return _byteswap_ushort(_data);}
  139. inline u32 swap32(u32 _data) {return _byteswap_ulong (_data);}
  140. inline u64 swap64(u64 _data) {return _byteswap_uint64(_data);}
  141. #elif _M_ARM
  142. inline u16 swap16 (u16 _data) { u32 data = _data; __asm__ ("rev16 %0, %1\n" : "=l" (data) : "l" (data)); return (u16)data;}
  143. inline u32 swap32 (u32 _data) {__asm__ ("rev %0, %1\n" : "=l" (_data) : "l" (_data)); return _data;}
  144. inline u64 swap64(u64 _data) {return ((u64)swap32(_data) << 32) | swap32(_data >> 32);}
  145. #elif __linux__
  146. inline u16 swap16(u16 _data) {return bswap_16(_data);}
  147. inline u32 swap32(u32 _data) {return bswap_32(_data);}
  148. inline u64 swap64(u64 _data) {return bswap_64(_data);}
  149. #elif __APPLE__
  150. inline __attribute__((always_inline)) u16 swap16(u16 _data)
  151. {return (_data >> 8) | (_data << 8);}
  152. inline __attribute__((always_inline)) u32 swap32(u32 _data)
  153. {return __builtin_bswap32(_data);}
  154. inline __attribute__((always_inline)) u64 swap64(u64 _data)
  155. {return __builtin_bswap64(_data);}
  156. #elif __FreeBSD__
  157. inline u16 swap16(u16 _data) {return bswap16(_data);}
  158. inline u32 swap32(u32 _data) {return bswap32(_data);}
  159. inline u64 swap64(u64 _data) {return bswap64(_data);}
  160. #else
  161. // Slow generic implementation.
  162. inline u16 swap16(u16 data) {return (data >> 8) | (data << 8);}
  163. inline u32 swap32(u32 data) {return (swap16(data) << 16) | swap16(data >> 16);}
  164. inline u64 swap64(u64 data) {return ((u64)swap32(data) << 32) | swap32(data >> 32);}
  165. #endif
  166. inline u16 swap16(const u8* _pData) {return swap16(*(const u16*)_pData);}
  167. inline u32 swap32(const u8* _pData) {return swap32(*(const u32*)_pData);}
  168. inline u64 swap64(const u8* _pData) {return swap64(*(const u64*)_pData);}
  169. template <int count>
  170. void swap(u8*);
  171. template <>
  172. inline void swap<1>(u8* data)
  173. {}
  174. template <>
  175. inline void swap<2>(u8* data)
  176. {
  177. *reinterpret_cast<u16*>(data) = swap16(data);
  178. }
  179. template <>
  180. inline void swap<4>(u8* data)
  181. {
  182. *reinterpret_cast<u32*>(data) = swap32(data);
  183. }
  184. template <>
  185. inline void swap<8>(u8* data)
  186. {
  187. *reinterpret_cast<u64*>(data) = swap64(data);
  188. }
  189. template <typename T>
  190. inline T FromBigEndian(T data)
  191. {
  192. //static_assert(std::is_arithmetic<T>::value, "function only makes sense with arithmetic types");
  193. swap<sizeof(data)>(reinterpret_cast<u8*>(&data));
  194. return data;
  195. }
  196. } // Namespace Common