OSDN Git Service

[VM][FM7][VRAM] Add __DECL_VECTORIZE_LOOP decl. to indicate expect to use vectorize...
[csp-qt/common_source_project-fm7.git] / source / src / common.h
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.18 -
6
7         [ common header ]
8 */
9
10 #ifndef _COMMON_H_
11 #define _COMMON_H_
12
13 // move shared codes to DLL???
14 //#ifdef _USE_QT
15 //      #define USE_SHARED_DLL
16 //#endif
17
18 // use zlib to decompress gzip file???
19 #ifdef _WIN32
20         #if defined(_MSC_VER) && (_MSC_VER >= 1500)
21                 #ifndef _ANY2D88
22                         #define USE_ZLIB
23                 #endif
24         #endif
25 #endif
26
27 // check environemnt/language
28 #ifdef _WIN32
29         #ifdef _MSC_VER
30                 // Microsoft Visual C++
31                 #if _MSC_VER == 1200
32                         // variable scope of 'for' loop for Microsoft Visual C++ 6.0
33                         #define for if(0);else for
34                 #endif
35                 #if _MSC_VER >= 1200
36                         // Microsoft Visual C++ 6.0 or later
37                         #define SUPPORT_TCHAR_TYPE
38                 #endif
39                 #if _MSC_VER >= 1400
40                         // Microsoft Visual C++ 8.0 (2005) or later
41                         #define SUPPORT_SECURE_FUNCTIONS
42                         #pragma warning( disable : 4819 )
43                         //#pragma warning( disable : 4995 )
44                         #pragma warning( disable : 4996 )
45                 #endif
46                 #if _MSC_VER >= 1800
47                         // Microsoft Visual C++ 12.0 (2013) or later
48                         #define SUPPORT_CPLUSPLUS_11
49                 #endif
50                 #define CSP_OS_WINDOWS
51         #else
52                 // Win32, but not Microsoft Visual C++
53                 #define SUPPORT_TCHAR_TYPE
54 //              #define SUPPORT_SECURE_FUNCTIONS
55         #endif
56 #endif
57 #ifdef __GNUC__
58         #if defined(Q_OS_CYGWIN) 
59                 #define CSP_OS_GCC_CYGWIN
60                 #define CSP_OS_WINDOWS
61         #elif defined(Q_OS_WIN) || defined(__WIN32) || defined(__WIN64)
62                 #define CSP_OS_GCC_WINDOWS
63                 #define CSP_OS_WINDOWS
64                 #ifdef USE_SHARED_DLL
65                         #define DLL_PREFIX   __declspec(dllexport)
66                         #define DLL_PREFIX_I __declspec(dllimport)
67                 #endif
68         #else
69                 #define CSP_OS_GCC_GENERIC
70                 #define CSP_OS_GENERIC
71                 #define DLL_PREFIX
72                 #define DLL_PREFIX_I
73         #endif
74         #if defined(__clang__)
75                 #define __CSP_COMPILER_CLANG
76         #else
77                 #define __CSP_COMPILER_GCC
78         #endif
79         #define SUPPORT_CPLUSPLUS_11
80 #else
81                 #define DLL_PREFIX
82                 #define DLL_PREFIX_I
83 #endif
84
85 #ifndef SUPPORT_CPLUSPLUS_11
86         #if defined(__cplusplus) && (__cplusplus > 199711L)
87                 #define SUPPORT_CPLUSPLUS_11
88         #endif
89 #endif
90 #ifndef SUPPORT_TCHAR_TYPE
91         // secure functions need tchar type
92         #undef SUPPORT_SECURE_FUNCTIONS
93 #endif
94 #ifndef DLL_PREFIX
95         #define DLL_PREFIX
96 #endif
97 #ifndef DLL_PREFIX_I
98         #define DLL_PREFIX_I
99 #endif
100
101 // include common header files
102 #ifdef SUPPORT_TCHAR_TYPE
103         #include <tchar.h>
104 #endif
105 #ifdef SUPPORT_CPLUSPLUS_11
106         #include <stdint.h>
107 #endif
108 #include <stdio.h>
109 #include <stdlib.h>
110 #include <string.h>
111 #ifdef _MSC_VER
112 #include <typeinfo.h>
113 #else
114 #include <typeinfo>
115 #endif
116 #include <assert.h>
117 #include <errno.h>
118
119 // include environment/language dependent header files
120 #ifdef _WIN32
121         #include <windows.h>
122         #include <windowsx.h>
123         #include <winuser.h>
124         #include <mmsystem.h>
125         #include <process.h>
126 #endif
127 #ifdef __GNUC__
128         #include <stdarg.h>
129 #endif
130 #ifdef _USE_QT
131         #ifdef _USE_QT5
132                 #include <QString>
133                 #include <QFile>
134                 #include <QtEndian>
135                 #if defined(__MINGW32__) || (__MINGW64__)
136                         #include <windows.h>
137                         #include <winbase.h>
138                 #endif
139         #else
140                 #include <QtCore/QString>
141                 #include <QtCore/QFile>
142         #endif
143         #include <sys/param.h>
144 #endif
145 #ifndef _MAX_PATH
146         #define _MAX_PATH 2048
147 #endif
148
149 // endian
150 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
151         #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
152                 #if __BYTE_ORDER == __LITTLE_ENDIAN
153                         #define __LITTLE_ENDIAN__
154                 #elif __BYTE_ORDER == __BIG_ENDIAN
155                         #define __BIG_ENDIAN__
156                 #endif
157         #elif defined(SDL_BYTEORDER) && (defined(SDL_LIL_ENDIAN) || defined(SDL_BIG_ENDIAN))
158                 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
159                         #define __LITTLE_ENDIAN__
160                 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
161                         #define __BIG_ENDIAN__
162                 #endif
163         #elif defined(WORDS_LITTLEENDIAN)
164                 #define __LITTLE_ENDIAN__
165         #elif defined(WORDS_BIGENDIAN)
166                 #define __BIG_ENDIAN__
167         #endif
168 #endif
169 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
170         // may be Microsoft Visual C++
171         #define __LITTLE_ENDIAN__
172 #endif
173
174 // type definition
175 #ifndef SUPPORT_TCHAR_TYPE
176         #ifndef _TCHAR
177                 typedef char _TCHAR;
178         #endif
179 #endif
180
181 #ifndef SUPPORT_CPLUSPLUS_11
182         #ifndef int8_t
183                 typedef signed char int8_t;
184         #endif
185         #ifndef int16_t
186                 typedef signed short int16_t;
187         #endif
188         #ifndef int32_t
189                 typedef signed int int32_t;
190         #endif
191         #ifndef int64_t
192                 typedef signed long long int64_t;
193         #endif
194         #ifndef uint8_t
195                 typedef unsigned char uint8_t;
196         #endif
197         #ifndef uint16_t
198                 typedef unsigned short uint16_t;
199         #endif
200         #ifndef uint32_t
201                 typedef unsigned int uint32_t;
202         #endif
203         #ifndef uint64_t
204                 typedef unsigned long long uint64_t;
205         #endif
206 #endif
207
208 #ifndef _WIN32
209         #ifndef LPTSTR
210                 typedef _TCHAR* LPTSTR;
211         #endif
212         #ifndef LPCTSTR
213                 typedef const _TCHAR* LPCTSTR;
214         #endif
215         #ifndef BOOL
216                 typedef int BOOL;
217         #endif
218         #ifndef TRUE
219                 #define TRUE 1
220         #endif
221         #ifndef FALSE
222                 #define FALSE 0
223         #endif
224         #ifndef BYTE
225                 typedef uint8_t BYTE;
226         #endif
227         #ifndef WORD
228                 typedef uint16_t WORD;
229         #endif
230         #ifndef DWORD
231                 typedef uint32_t DWORD;
232         #endif
233         #ifndef QWORD
234                 typedef uint64_t QWORD;
235         #endif
236         #ifndef INT8
237                 typedef int8_t INT8;
238         #endif
239         #ifndef INT16
240                 typedef int16_t INT16;
241         #endif
242         #ifndef INT32
243                 typedef int32_t INT32;
244         #endif
245         #ifndef INT64
246                 typedef int64_t INT64;
247         #endif
248         #ifndef UINT8
249                 typedef uint8_t UINT8;
250         #endif
251         #ifndef UINT16
252                 typedef uint16_t UINT16;
253         #endif
254         #ifndef UINT32
255                 typedef uint32_t UINT32;
256         #endif
257         #ifndef UINT64
258                 typedef uint64_t UINT64;
259         #endif
260         #ifndef INT
261                 typedef int INT;
262         #endif
263         #ifndef UINT
264                 typedef unsigned int UINT;
265         #endif
266 #endif
267
268 typedef union {
269         struct {
270 #ifdef __BIG_ENDIAN__
271                 uint8_t h3, h2, h, l;
272 #else
273                 uint8_t l, h, h2, h3;
274 #endif
275         } b;
276         struct {
277 #ifdef __BIG_ENDIAN__
278                 int8_t h3, h2, h, l;
279 #else
280                 int8_t l, h, h2, h3;
281 #endif
282         } sb;
283         struct {
284 #ifdef __BIG_ENDIAN__
285                 uint16_t h, l;
286 #else
287                 uint16_t l, h;
288 #endif
289         } w;
290         struct {
291 #ifdef __BIG_ENDIAN__
292                 int16_t h, l;
293 #else
294                 int16_t l, h;
295 #endif
296         } sw;
297         uint32_t d;
298         int32_t sd;
299         inline void read_2bytes_le_from(uint8_t *t)
300         {
301                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
302         }
303         inline void write_2bytes_le_to(uint8_t *t)
304         {
305                 t[0] = b.l; t[1] = b.h;
306         }
307         inline void read_2bytes_be_from(uint8_t *t)
308         {
309                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
310         }
311         inline void write_2bytes_be_to(uint8_t *t)
312         {
313                 t[0] = b.h; t[1] = b.l;
314         }
315         inline void read_4bytes_le_from(uint8_t *t)
316         {
317                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
318         }
319         inline void write_4bytes_le_to(uint8_t *t)
320         {
321                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
322         }
323         inline void read_4bytes_be_from(uint8_t *t)
324         {
325                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
326         }
327         inline void write_4bytes_be_to(uint8_t *t)
328         {
329                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
330         }
331 } pair_t;
332
333 uint32_t DLL_PREFIX EndianToLittle_DWORD(uint32_t x);
334 uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x);
335
336 // max/min
337 #ifndef _MSC_VER
338         #undef max
339         #undef min
340         int DLL_PREFIX max(int a, int b);
341         unsigned int DLL_PREFIX max(unsigned int a, unsigned int b);
342         int DLL_PREFIX min(int a, int b);
343         unsigned int DLL_PREFIX min(unsigned int a, unsigned int b);
344 #endif
345
346 // string
347 #if defined(__GNUC__) || defined(__CYGWIN__) || defined(Q_OS_CYGWIN)
348         #define stricmp(a,b) strcasecmp(a,b)
349         #define strnicmp(a,b,n) strncasecmp(a,b,n)
350 #endif
351
352 #ifndef SUPPORT_TCHAR_TYPE
353         #ifndef _fgetts
354                 #define _fgetts fgets
355         #endif
356         #ifndef _ftprintf
357                 #define _ftprintf printf
358         #endif
359         #ifndef _tfopen
360                 #define _tfopen fopen
361         #endif
362         #ifndef _tcscmp
363                 #define _tcscmp strcmp
364         #endif
365         #ifndef _tcscpy
366                 #define _tcscpy strcpy
367         #endif
368         #ifndef _tcsicmp
369                 #define _tcsicmp stricmp
370         #endif
371         #ifndef _tcslen
372                 #define _tcslen strlen
373         #endif
374         #ifndef _tcscat
375                 #define _tcscat strcat
376         #endif
377         #ifndef _tcsncat
378                 #define _tcsncat strncat
379         #endif
380         #ifndef _tcsncpy
381                 #define _tcsncpy strncpy
382         #endif
383         #ifndef _tcsncicmp
384                 #define _tcsncicmp strnicmp
385         #endif
386         #ifndef _tcschr
387                 #define _tcschr strchr
388         #endif
389         #ifndef _tcsrchr
390                 #define _tcsrchr strrchr
391         #endif
392         #ifndef _tcsstr
393                 #define _tcsstr strstr
394         #endif
395         #ifndef _tcstok
396                 #define _tcstok strtok
397         #endif
398         #ifndef _tstoi
399                 #define _tstoi atoi
400         #endif
401         #ifndef _tcstol
402                 #define _tcstol strtol
403         #endif
404         #ifndef _tcstoul
405                 #define _tcstoul strtoul
406         #endif
407         #ifndef _stprintf
408                 #define _stprintf sprintf
409         #endif
410         #ifndef _vstprintf
411                 #define _vstprintf vsprintf
412         #endif
413         #ifndef _taccess
414                 #define _taccess access
415         #endif
416         #ifndef _tremove
417                 #define _tremove remove
418         #endif
419         #ifndef _trename
420                 #define _trename rename
421         #endif
422         #define __T(x) x
423         #define _T(x) __T(x)
424         #define _TEXT(x) __T(x)
425 #endif
426
427 #ifndef SUPPORT_SECURE_FUNCTIONS
428         #ifndef errno_t
429                 typedef int errno_t;
430         #endif
431 //      errno_t DLL_PREFIX my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);
432         errno_t DLL_PREFIX my_tcscat_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
433         errno_t DLL_PREFIX my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
434         errno_t DLL_PREFIX my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
435         errno_t DLL_PREFIX my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count);
436         errno_t DLL_PREFIX my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count);
437         char * DLL_PREFIX my_strtok_s(char *strToken, const char *strDelimit, char **context);
438         _TCHAR * DLL_PREFIX my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
439         #define my_fprintf_s fprintf
440         #define my_ftprintf_s fprintf
441         int DLL_PREFIX my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
442         int DLL_PREFIX my_swprintf_s(wchar_t *buffer, size_t sizeOfBuffer, const wchar_t *format, ...);
443         int DLL_PREFIX my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
444         int DLL_PREFIX my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr);
445         int DLL_PREFIX my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
446 #else
447 //      #define my_tfopen_s _tfopen_s
448         #define my_tcscat_s _tcscat_s
449         #define my_strcpy_s strcpy_s
450         #define my_tcscpy_s _tcscpy_s
451         #define my_strncpy_s strncpy_s
452         #define my_tcsncpy_s _tcsncpy_s
453         #define my_strtok_s strtok_s
454         #define my_tcstok_s _tcstok_s
455         #define my_fprintf_s fprintf_s
456         #define my_ftprintf_s _ftprintf_s
457         #define my_sprintf_s sprintf_s
458         #define my_swprintf_s swprintf_s
459         #define my_stprintf_s _stprintf_s
460         #define my_vsprintf_s vsprintf_s
461         #define my_vstprintf_s _vstprintf_s
462 #endif
463
464 // C99 math functions
465 #ifdef _MSC_VER
466         #define my_isfinite _finite
467         #define my_log2(v) (log((double)(v)) / log(2.0))
468 #else
469         #define my_isfinite isfinite
470         #define my_log2 log2
471 #endif
472
473 // win32 api
474 #ifndef _WIN32
475         BOOL MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);
476         DWORD MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName);
477         UINT MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName);
478         // used only in winmain and win32 osd class
479 //      #define ZeroMemory(p,s) memset(p,0x00,s)
480 //      #define CopyMemory(t,f,s) memcpy(t,f,s)
481 #else
482         #define MyWritePrivateProfileString WritePrivateProfileString
483         #define MyGetPrivateProfileString GetPrivateProfileString
484         #define MyGetPrivateProfileInt GetPrivateProfileInt
485 #endif
486
487 // rgb color
488 #if !defined(_RGB555) && !defined(_RGB565) && !defined(_RGB888)
489         #define _RGB888
490 #endif
491
492 #if defined(_RGB555) || defined(_RGB565)
493         typedef uint16_t scrntype_t;
494         scrntype_t DLL_PREFIX RGB_COLOR(uint32_t r, uint32_t g, uint32_t b);
495         scrntype_t DLL_PREFIX RGBA_COLOR(uint32_t r, uint32_t g, uint32_t b, uint32_t a);
496         uint8_t DLL_PREFIX R_OF_COLOR(scrntype_t c);
497         uint8_t DLL_PREFIX G_OF_COLOR(scrntype_t c);
498         uint8_t DLL_PREFIX B_OF_COLOR(scrntype_t c);
499         uint8_t DLL_PREFIX A_OF_COLOR(scrntype_t c);
500 #elif defined(_RGB888)
501         typedef uint32_t scrntype_t;
502         #define RGB_COLOR(r, g, b)      (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0))
503         #define RGBA_COLOR(r, g, b, a)  (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | ((uint32_t)(a) << 24))
504         #define R_OF_COLOR(c)           (((c) >> 16) & 0xff)
505         #define G_OF_COLOR(c)           (((c) >>  8) & 0xff)
506         #define B_OF_COLOR(c)           (((c)      ) & 0xff)
507         #define A_OF_COLOR(c)           (((c) >> 24) & 0xff)
508 #endif
509 /*
510  * Below macros are hint for SIMD.
511  */
512 #if defined(__clang__)
513         #define __DECL_VECTORIZED_LOOP   _Pragma("clang loop vectorize(enable) interleave(enable)")
514 #elif defined(__GNUC__)
515         #define __DECL_VECTORIZED_LOOP  _Pragma("GCC ivdep")
516 #else
517         #define __DECL_VECTORIZED_LOOP
518 #endif
519
520 // wav file header
521 #pragma pack(1)
522 typedef struct {
523         char id[4];
524         uint32_t size;
525 } wav_chunk_t;
526 #pragma pack()
527
528 #pragma pack(1)
529 typedef struct {
530         wav_chunk_t riff_chunk;
531         char wave[4];
532         wav_chunk_t fmt_chunk;
533         uint16_t format_id;
534         uint16_t channels;
535         uint32_t sample_rate;
536         uint32_t data_speed;
537         uint16_t block_size;
538         uint16_t sample_bits;
539 } wav_header_t;
540 #pragma pack()
541
542 // file path
543 const _TCHAR *DLL_PREFIX get_application_path();
544 const _TCHAR *DLL_PREFIX create_local_path(const _TCHAR *format, ...);
545 void DLL_PREFIX create_local_path(_TCHAR *file_path, int length, const _TCHAR *format, ...);
546 const _TCHAR *DLL_PREFIX create_date_file_path(const _TCHAR *extension);
547 void DLL_PREFIX create_date_file_path(_TCHAR *file_path, int length, const _TCHAR *extension);
548 bool DLL_PREFIX check_file_extension(const _TCHAR *file_path, const _TCHAR *ext);
549 const _TCHAR *DLL_PREFIX get_file_path_without_extensiton(const _TCHAR *file_path);
550 void DLL_PREFIX get_long_full_path_name(const _TCHAR* src, _TCHAR* dst, size_t dst_len);
551 const _TCHAR* DLL_PREFIX get_parent_dir(const _TCHAR* file);
552
553 // string
554 const _TCHAR *DLL_PREFIX create_string(const _TCHAR* format, ...);
555 const wchar_t *DLL_PREFIX char_to_wchar(const char *cs);
556 const char *DLL_PREFIX wchar_to_char(const wchar_t *ws);
557 const _TCHAR *DLL_PREFIX char_to_tchar(const char *cs);
558 const char *DLL_PREFIX tchar_to_char(const _TCHAR *ts);
559 const _TCHAR *DLL_PREFIX wchar_to_tchar(const wchar_t *ws);
560 const wchar_t *DLL_PREFIX tchar_to_wchar(const _TCHAR *ts);
561
562 void *DLL_PREFIX my_memcpy(void *dst, void *src, size_t len);
563
564 // misc
565 int32_t DLL_PREFIX muldiv_s32(int32_t nNumber, int32_t nNumerator, int32_t nDenominator);
566 uint32_t DLL_PREFIX muldiv_u32(uint32_t nNumber, uint32_t nNumerator, uint32_t nDenominator);
567
568 uint32_t DLL_PREFIX get_crc32(uint8_t data[], int size);
569 uint16_t DLL_PREFIX jis_to_sjis(uint16_t jis);
570
571 int DLL_PREFIX decibel_to_volume(int decibel);
572 int32_t DLL_PREFIX apply_volume(int32_t sample, int volume);
573
574 #define array_length(array) (sizeof(array) / sizeof(array[0]))
575
576 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
577 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
578 #define TO_BCD_LO(v)    ((v) % 10)
579 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)
580
581 // time
582 #define LEAP_YEAR(y)    (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
583
584 #define dll_cur_time_t DLL_PREFIX_I struct cur_time_s
585
586 typedef DLL_PREFIX struct cur_time_s {
587         int year, month, day, day_of_week, hour, minute, second;
588         bool initialized;
589         cur_time_s()
590         {
591                 initialized = false;
592         }
593         void increment();
594         void update_year();
595         void update_day_of_week();
596         void save_state(void *f);
597         bool load_state(void *f);
598 } cur_time_t;
599
600 void DLL_PREFIX get_host_time(cur_time_t* cur_time);
601
602 // symbol
603 typedef struct symbol_s {
604         uint32_t addr;
605         _TCHAR *name;
606         struct symbol_s *next_symbol;
607 } symbol_t;
608
609 const _TCHAR* DLL_PREFIX get_symbol(symbol_t *first_symbol, uint32_t addr);
610 const _TCHAR* DLL_PREFIX get_value_or_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);
611 const _TCHAR* DLL_PREFIX get_value_and_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);
612
613 #endif