common_funcs.h 6.7 KB

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