| 
									
										
										
										
											2009-07-28 21:32:10 +00:00
										 |  |  | // Copyright (C) 2003 Dolphin Project.
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, version 2.0.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU General Public License 2.0 for more details.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // A copy of the GPL 2.0 should have been included with the program.
 | 
					
						
							|  |  |  | // If not, see http://www.gnu.org/licenses/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Official SVN repository and contact information can be found at
 | 
					
						
							|  |  |  | // http://code.google.com/p/dolphin-emu/
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | #ifndef _COMMONFUNCS_H_
 | 
					
						
							|  |  |  | #define _COMMONFUNCS_H_
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | #define SLEEP(x) Sleep(x)
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #define SLEEP(x) usleep(x*1000)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-17 21:44:03 +00:00
										 |  |  | template <bool> struct CompileTimeAssert; | 
					
						
							|  |  |  | template<> struct CompileTimeAssert<true> {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-31 14:40:01 +00:00
										 |  |  | #if defined __GNUC__ && !defined __SSSE3__
 | 
					
						
							|  |  |  | #include <emmintrin.h>
 | 
					
						
							|  |  |  | static __inline __m128i __attribute__((__always_inline__)) | 
					
						
							|  |  |  | _mm_shuffle_epi8(__m128i a, __m128i mask) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	__m128i result; | 
					
						
							|  |  |  | 	__asm__("pshufb %1, %0" | 
					
						
							|  |  |  | 		: "=x" (result) | 
					
						
							|  |  |  | 		: "xm" (mask), "0" (a)); | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | #ifndef _WIN32
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-22 03:29:35 +00:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2009-05-01 15:17:03 +00:00
										 |  |  | #ifdef __linux__
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | #include <byteswap.h>
 | 
					
						
							| 
									
										
										
										
											2010-07-22 03:29:35 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | char * strndup(char const *s, size_t n); | 
					
						
							| 
									
										
										
										
											2009-05-01 15:17:03 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-07-22 03:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | // go to debugger mode
 | 
					
						
							| 
									
										
										
										
											2009-05-03 11:15:17 +00:00
										 |  |  | 	#ifdef GEKKO
 | 
					
						
							|  |  |  | 		#define Crash()
 | 
					
						
							|  |  |  | 	#else
 | 
					
						
							|  |  |  | 		#define Crash() {asm ("int $3");}
 | 
					
						
							|  |  |  | 	#endif
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	#define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
 | 
					
						
							| 
									
										
										
										
											2010-07-31 14:40:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | inline u32 _rotl(u32 x, int shift) { | 
					
						
							|  |  |  |     shift &= 31; | 
					
						
							|  |  |  |     if (!shift) return x; | 
					
						
							|  |  |  |     return (x << shift) | (x >> (32 - shift)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-08 23:23:04 +00:00
										 |  |  | inline u64 _rotl64(u64 x, unsigned int shift){ | 
					
						
							|  |  |  | 	unsigned int n = shift % 64; | 
					
						
							|  |  |  | 	return (x << n) | (x >> (64 - n)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | inline u32 _rotr(u32 x, int shift) { | 
					
						
							|  |  |  |     shift &= 31; | 
					
						
							|  |  |  |     if (!shift) return x; | 
					
						
							|  |  |  |     return (x >> shift) | (x << (32 - shift)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-02-08 23:23:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | inline u64 _rotr64(u64 x, unsigned int shift){ | 
					
						
							|  |  |  | 	unsigned int n = shift % 64; | 
					
						
							|  |  |  | 	return (x >> n) | (x << (64 - n)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-07-31 14:40:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | #else // WIN32
 | 
					
						
							|  |  |  | // Function Cross-Compatibility
 | 
					
						
							|  |  |  | 	#define strcasecmp _stricmp
 | 
					
						
							|  |  |  | 	#define strncasecmp _strnicmp
 | 
					
						
							|  |  |  | 	#define unlink _unlink
 | 
					
						
							|  |  |  | 	#define snprintf _snprintf
 | 
					
						
							| 
									
										
										
										
											2009-11-08 08:54:09 +00:00
										 |  |  | 	#define vscprintf _vscprintf
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | char* strndup (char const *s, size_t n);	 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 64 bit offsets for windows
 | 
					
						
							| 
									
										
										
										
											2010-12-04 03:50:55 +00:00
										 |  |  | 	#define fseeko _fseeki64
 | 
					
						
							|  |  |  | 	#define ftello _ftelli64
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 	#define atoll _atoi64
 | 
					
						
							|  |  |  | 	#define stat64 _stat64
 | 
					
						
							| 
									
										
										
										
											2010-12-03 12:42:01 +00:00
										 |  |  | 	#define fstat64 _fstat64
 | 
					
						
							|  |  |  | 	#define fileno _fileno
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	#if _M_IX86
 | 
					
						
							|  |  |  | 		#define Crash() {__asm int 3}
 | 
					
						
							|  |  |  | 	#else
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | 	__declspec(dllimport) void __stdcall DebugBreak(void); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 		#define Crash() {DebugBreak();}
 | 
					
						
							|  |  |  | 	#endif // M_IX86
 | 
					
						
							|  |  |  | #endif // WIN32 ndef
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Dolphin's min and max functions
 | 
					
						
							|  |  |  | #undef min
 | 
					
						
							|  |  |  | #undef max
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<class T> | 
					
						
							|  |  |  | inline T min(const T& a, const T& b) {return a > b ? b : a;} | 
					
						
							|  |  |  | template<class T> | 
					
						
							|  |  |  | inline T max(const T& a, const T& b) {return a > b ? a : b;} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Generic function to get last error message.
 | 
					
						
							|  |  |  | // Call directly after the command or use the error num.
 | 
					
						
							|  |  |  | // This function might change the error code.
 | 
					
						
							|  |  |  | // Defined in Misc.cpp.
 | 
					
						
							|  |  |  | const char* GetLastErrorMsg(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Common | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | inline u8 swap8(u8 _data) {return _data;} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | inline u16 swap16(u16 _data) {return _byteswap_ushort(_data);} | 
					
						
							|  |  |  | inline u32 swap32(u32 _data) {return _byteswap_ulong (_data);} | 
					
						
							|  |  |  | inline u64 swap64(u64 _data) {return _byteswap_uint64(_data);} | 
					
						
							|  |  |  | #elif __linux__
 | 
					
						
							|  |  |  | inline u16 swap16(u16 _data) {return bswap_16(_data);} | 
					
						
							|  |  |  | inline u32 swap32(u32 _data) {return bswap_32(_data);} | 
					
						
							|  |  |  | inline u64 swap64(u64 _data) {return bswap_64(_data);} | 
					
						
							| 
									
										
										
										
											2010-02-16 08:46:21 +00:00
										 |  |  | #elif __APPLE__
 | 
					
						
							| 
									
										
										
										
											2010-07-18 08:30:40 +00:00
										 |  |  | inline __attribute__((always_inline)) u16 swap16(u16 _data) | 
					
						
							|  |  |  | 	{return (_data >> 8) | (_data << 8);} | 
					
						
							|  |  |  | inline __attribute__((always_inline)) u32 swap32(u32 _data) | 
					
						
							|  |  |  | 	{return __builtin_bswap32(_data);} | 
					
						
							|  |  |  | inline __attribute__((always_inline)) u64 swap64(u64 _data) | 
					
						
							|  |  |  | 	{return __builtin_bswap64(_data);} | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | // Slow generic implementation.
 | 
					
						
							|  |  |  | inline u16 swap16(u16 data) {return (data >> 8) | (data << 8);} | 
					
						
							|  |  |  | inline u32 swap32(u32 data) {return (swap16(data) << 16) | swap16(data >> 16);} | 
					
						
							|  |  |  | inline u64 swap64(u64 data) {return ((u64)swap32(data) << 32) | swap32(data >> 32);} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | inline u16 swap16(const u8* _pData) {return swap16(*(const u16*)_pData);} | 
					
						
							|  |  |  | inline u32 swap32(const u8* _pData) {return swap32(*(const u32*)_pData);} | 
					
						
							|  |  |  | inline u64 swap64(const u8* _pData) {return swap64(*(const u64*)_pData);} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | }  // Namespace Common
 | 
					
						
							| 
									
										
										
										
											2009-03-07 08:35:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | #endif // _COMMONFUNCS_H_
 |