common_funcs.h 6.7 KB

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