common_funcs.h 7.3 KB

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