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