1 /*
2 	Copyright (C) 2005 Guillaume Duhamel
3 	Copyright (C) 2008-2015 DeSmuME team
4 
5 	This file is free software: you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation, either version 2 of the License, or
8 	(at your option) any later version.
9 
10 	This file is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with the this software.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef TYPES_HPP
20 #define TYPES_HPP
21 
22 //analyze microsoft compilers
23 #ifdef _MSC_VER
24 #define HOST_WINDOWS
25 #endif //_MSC_VER
26 
27 // Determine CPU architecture for platforms that don't use the autoconf script
28 #if defined(__x86_64__) || defined(__LP64) || defined(__IA64__) || defined(_M_X64) || defined(_WIN64)
29 #define HOST_64
30 #else
31 #define HOST_32
32 #endif
33 
34 #ifdef DEVELOPER
35 #define IF_DEVELOPER(X) X
36 #else
37 #define IF_DEVELOPER(X)
38 #endif
39 
40 #ifdef HOST_WINDOWS
41 	#define ENABLE_SSE
42 	#define ENABLE_SSE2
43 	#define HAVE_JIT
44 #endif
45 
46 #ifdef __GNUC__
47 #ifdef __SSE__
48 		#define ENABLE_SSE
49 	#endif
50 
51 	#ifdef __SSE2__
52 		#define ENABLE_SSE2
53 	#endif
54 
55 	#ifdef __SSE3__
56 		#define ENABLE_SSE3
57 	#endif
58 
59 	#ifdef __SSSE3__
60 		#define ENABLE_SSSE3
61 	#endif
62 #endif
63 
64 #ifdef _MSC_VER
65 #define strcasecmp(x,y) _stricmp(x,y)
66 #define strncasecmp(x, y, l) strnicmp(x, y, l)
67 #define snprintf _snprintf
68 #else
69 #ifndef _WIN32
70 #define WINAPI
71 #endif
72 #endif
73 
74 #include <retro_miscellaneous.h>
75 
76 //------------alignment macros-------------
77 //dont apply these to types without further testing. it only works portably here on declarations of variables
78 //cant we find a pattern other people use more successfully?
79 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
80 #define DS_ALIGN(X) __declspec(align(X))
81 #elif defined(__GNUC__)
82 #define DS_ALIGN(X) __attribute__ ((aligned (X)))
83 #else
84 #define DS_ALIGN(X)
85 #endif
86 
87 #ifdef HOST_64
88 #define CACHE_ALIGN DS_ALIGN(64)
89 #else
90 #define CACHE_ALIGN DS_ALIGN(32)
91 #endif
92 
93 //use this for example when you want a byte value to be better-aligned
94 #define FAST_ALIGN DS_ALIGN(4)
95 //---------------------------------------------
96 
97 #ifdef __MINGW32__
98 #define FASTCALL __attribute__((fastcall))
99 #define ASMJIT_CALL_CONV kX86FuncConvCompatFastCall
100 #elif defined (__i386__) && !defined(__clang__)
101 #define FASTCALL __attribute__((regparm(3)))
102 #define ASMJIT_CALL_CONV kX86FuncConvGccRegParm3
103 #elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
104 #define FASTCALL
105 #define ASMJIT_CALL_CONV kX86FuncConvDefault
106 #else
107 #define FASTCALL
108 #define ASMJIT_CALL_CONV kX86FuncConvDefault
109 #endif
110 
111 #ifdef _MSC_VER
112 #define _CDECL_ __cdecl
113 #else
114 #define _CDECL_
115 #endif
116 
117 #ifndef FORCEINLINE
118 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
119 #define FORCEINLINE __forceinline
120 #define MSC_FORCEINLINE __forceinline
121 #else
122 #define FORCEINLINE inline __attribute__((always_inline))
123 #define MSC_FORCEINLINE
124 #endif
125 #endif
126 
127 #ifndef NOINLINE
128 #ifdef __GNUC__
129 #define NOINLINE __attribute__((noinline))
130 #else
131 #define NOINLINE
132 #endif
133 #endif
134 
135 #if defined(__LP64__)
136 typedef unsigned char u8;
137 typedef unsigned short u16;
138 typedef unsigned int u32;
139 typedef unsigned long long u64;
140 
141 typedef signed char s8;
142 typedef signed short s16;
143 typedef signed int s32;
144 typedef signed long long s64;
145 #else
146 typedef unsigned char u8;
147 typedef unsigned short u16;
148 typedef unsigned int u32;
149 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
150 typedef unsigned __int64 u64;
151 #else
152 typedef unsigned long long u64;
153 #endif
154 
155 typedef signed char s8;
156 typedef signed short s16;
157 typedef signed int s32;
158 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
159 typedef __int64 s64;
160 #else
161 typedef signed long long s64;
162 #endif
163 #endif
164 
165 typedef u8  uint8;
166 typedef u16 uint16;
167 
168 #ifndef OBJ_C
169 typedef u32 uint32;
170 #else
171 #define uint32 u32 //uint32 is defined in Leopard somewhere, avoid conflicts
172 #endif
173 
174 /*---------- GPU3D fixed-points types -----------*/
175 
176 typedef s32 f32;
177 #define inttof32(n)          ((n) << 12)
178 #define f32toint(n)          ((n) >> 12)
179 #define floattof32(n)        ((int32)((n) * (1 << 12)))
180 #define f32tofloat(n)        (((float)(n)) / (float)(1<<12))
181 
182 typedef s16 t16;
183 #define f32tot16(n)          ((t16)(n >> 8))
184 #define inttot16(n)          ((n) << 4)
185 #define t16toint(n)          ((n) >> 4)
186 #define floattot16(n)        ((t16)((n) * (1 << 4)))
187 #define t16ofloat(n)         (((float)(n)) / (float)(1<<4))
188 
189 typedef s16 v16;
190 #define inttov16(n)          ((n) << 12)
191 #define f32tov16(n)          (n)
192 #define floattov16(n)        ((v16)((n) * (1 << 12)))
193 #define v16toint(n)          ((n) >> 12)
194 #define v16tofloat(n)        (((float)(n)) / (float)(1<<12))
195 
196 typedef s16 v10;
197 #define inttov10(n)          ((n) << 9)
198 #define f32tov10(n)          ((v10)(n >> 3))
199 #define v10toint(n)          ((n) >> 9)
200 #define floattov10(n)        ((v10)((n) * (1 << 9)))
201 #define v10tofloat(n)        (((float)(n)) / (float)(1<<9))
202 
203 /*----------------------*/
204 
205 #ifndef OBJ_C
206 typedef int BOOL;
207 #else
208 //apple also defines BOOL
209 typedef int desmume_BOOL;
210 #define BOOL desmume_BOOL
211 #endif
212 
213 #ifndef TRUE
214 #define TRUE 1
215 #endif
216 
217 #ifndef FALSE
218 #define FALSE 0
219 #endif
220 
221 /* little endian (ds' endianess) to local endianess convert macros */
222 #ifdef MSB_FIRST	/* local arch is big endian */
223 # define LE_TO_LOCAL_16(x) ((((x)&0xff)<<8)|(((x)>>8)&0xff))
224 # define LE_TO_LOCAL_32(x) ((((x)&0xff)<<24)|(((x)&0xff00)<<8)|(((x)>>8)&0xff00)|(((x)>>24)&0xff))
225 # define LE_TO_LOCAL_64(x) ((((x)&0xff)<<56)|(((x)&0xff00)<<40)|(((x)&0xff0000)<<24)|(((x)&0xff000000)<<8)|(((x)>>8)&0xff000000)|(((x)>>24)&0xff0000)|(((x)>>40)&0xff00)|(((x)>>56)&0xff))
226 # define LOCAL_TO_LE_16(x) ((((x)&0xff)<<8)|(((x)>>8)&0xff))
227 # define LOCAL_TO_LE_32(x) ((((x)&0xff)<<24)|(((x)&0xff00)<<8)|(((x)>>8)&0xff00)|(((x)>>24)&0xff))
228 # define LOCAL_TO_LE_64(x) ((((x)&0xff)<<56)|(((x)&0xff00)<<40)|(((x)&0xff0000)<<24)|(((x)&0xff000000)<<8)|(((x)>>8)&0xff000000)|(((x)>>24)&0xff0000)|(((x)>>40)&0xff00)|(((x)>>56)&0xff))
229 #else		/* local arch is little endian */
230 # define LE_TO_LOCAL_16(x) (x)
231 # define LE_TO_LOCAL_32(x) (x)
232 # define LE_TO_LOCAL_64(x) (x)
233 # define LOCAL_TO_LE_16(x) (x)
234 # define LOCAL_TO_LE_32(x) (x)
235 # define LOCAL_TO_LE_64(x) (x)
236 #endif
237 
238 // kilobytes and megabytes macro
239 #define MB(x) ((x)*1024*1024)
240 #define KB(x) ((x)*1024)
241 
242 #define CPU_STR(c) ((c==ARM9)?"ARM9":"ARM7")
243 typedef enum
244 {
245 	ARM9 = 0,
246 	ARM7 = 1
247 } cpu_id_t;
248 
double_to_u64(double d)249 static INLINE u64 double_to_u64(double d)
250 {
251 	union {
252 		u64 a;
253 		double b;
254 	} fuxor;
255 	fuxor.b = d;
256 	return fuxor.a;
257 }
258 
u64_to_double(u64 u)259 static INLINE double u64_to_double(u64 u)
260 {
261 	union {
262 		u64 a;
263 		double b;
264 	} fuxor;
265 	fuxor.a = u;
266 	return fuxor.b;
267 }
268 
269 //fairly standard for loop macros
270 #define MACRODO1(TRICK,TODO) { const size_t X = TRICK; TODO; }
271 #define MACRODO2(X,TODO)   { MACRODO1((X),TODO)   MACRODO1(((X)+1),TODO) }
272 #define MACRODO4(X,TODO)   { MACRODO2((X),TODO)   MACRODO2(((X)+2),TODO) }
273 #define MACRODO8(X,TODO)   { MACRODO4((X),TODO)   MACRODO4(((X)+4),TODO) }
274 #define MACRODO16(X,TODO)  { MACRODO8((X),TODO)   MACRODO8(((X)+8),TODO) }
275 #define MACRODO32(X,TODO)  { MACRODO16((X),TODO)  MACRODO16(((X)+16),TODO) }
276 #define MACRODO64(X,TODO)  { MACRODO32((X),TODO)  MACRODO32(((X)+32),TODO) }
277 #define MACRODO128(X,TODO) { MACRODO64((X),TODO)  MACRODO64(((X)+64),TODO) }
278 #define MACRODO256(X,TODO) { MACRODO128((X),TODO) MACRODO128(((X)+128),TODO) }
279 
280 //this one lets you loop any number of times (as long as N<256)
281 #define MACRODO_N(N,TODO) {\
282 	if((N)&0x100) MACRODO256(0,TODO); \
283 	if((N)&0x080) MACRODO128((N)&(0x100),TODO); \
284 	if((N)&0x040) MACRODO64((N)&(0x100|0x080),TODO); \
285 	if((N)&0x020) MACRODO32((N)&(0x100|0x080|0x040),TODO); \
286 	if((N)&0x010) MACRODO16((N)&(0x100|0x080|0x040|0x020),TODO); \
287 	if((N)&0x008) MACRODO8((N)&(0x100|0x080|0x040|0x020|0x010),TODO); \
288 	if((N)&0x004) MACRODO4((N)&(0x100|0x080|0x040|0x020|0x010|0x008),TODO); \
289 	if((N)&0x002) MACRODO2((N)&(0x100|0x080|0x040|0x020|0x010|0x008|0x004),TODO); \
290 	if((N)&0x001) MACRODO1((N)&(0x100|0x080|0x040|0x020|0x010|0x008|0x004|0x002),TODO); \
291 }
292 
293 //---------------------------
294 //Binary constant generator macro By Tom Torfs - donated to the public domain
295 
296 //turn a numeric literal into a hex constant
297 //(avoids problems with leading zeroes)
298 //8-bit constants max value 0x11111111, always fits in unsigned long
299 #define HEX__(n) 0x##n##LU
300 
301 //8-bit conversion function
302 #define B8__(x) ((x&0x0000000FLU)?1:0) \
303 +((x&0x000000F0LU)?2:0) \
304 +((x&0x00000F00LU)?4:0) \
305 +((x&0x0000F000LU)?8:0) \
306 +((x&0x000F0000LU)?16:0) \
307 +((x&0x00F00000LU)?32:0) \
308 +((x&0x0F000000LU)?64:0) \
309 +((x&0xF0000000LU)?128:0)
310 
311 //for upto 8-bit binary constants
312 #define B8(d) ((unsigned char)B8__(HEX__(d)))
313 
314 // for upto 16-bit binary constants, MSB first
315 #define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) \
316 + B8(dlsb))
317 
318 // for upto 32-bit binary constants, MSB first */
319 #define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24) \
320 + ((unsigned long)B8(db2)<<16) \
321 + ((unsigned long)B8(db3)<<8) \
322 + B8(dlsb))
323 
324 //Sample usage:
325 //B8(01010101) = 85
326 //B16(10101010,01010101) = 43605
327 //B32(10000000,11111111,10101010,01010101) = 2164238933
328 //---------------------------
329 
330 static const char hexValid[23] = {"0123456789ABCDEFabcdef"};
331 
reconstruct(T * t)332 template<typename T> inline void reconstruct(T* t) {
333 	t->~T();
334 	new(t) T();
335 }
336 
337 #endif
338