common_funcs.h 6.7 KB

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