OSDN Git Service

[COMMON][Qt] Fix BYTEORDER of PIXEL@RGB888.Updated SOVERSION.
[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 #ifdef _USE_QT
27                         #define USE_ZLIB
28 #endif
29
30 // check environemnt/language
31 #ifdef _WIN32
32         #ifdef _MSC_VER
33                 // Microsoft Visual C++
34                 #if _MSC_VER == 1200
35                         // variable scope of 'for' loop for Microsoft Visual C++ 6.0
36                         #define for if(0);else for
37                 #endif
38                 #if _MSC_VER >= 1200
39                         // Microsoft Visual C++ 6.0 or later
40                         #define SUPPORT_TCHAR_TYPE
41                 #endif
42                 #if _MSC_VER >= 1400
43                         // Microsoft Visual C++ 8.0 (2005) or later
44                         #define SUPPORT_SECURE_FUNCTIONS
45                         #pragma warning( disable : 4819 )
46                         //#pragma warning( disable : 4995 )
47                         #pragma warning( disable : 4996 )
48                 #endif
49                 #if _MSC_VER >= 1800
50                         // Microsoft Visual C++ 12.0 (2013) or later
51                         #define SUPPORT_CPLUSPLUS_11
52                 #endif
53                 #define CSP_OS_WINDOWS
54         #else
55                 // Win32, but not Microsoft Visual C++
56                 #define SUPPORT_TCHAR_TYPE
57 //              #define SUPPORT_SECURE_FUNCTIONS
58         #endif
59 #endif
60 #ifdef __GNUC__
61         #if defined(Q_OS_CYGWIN) 
62                 #define CSP_OS_GCC_CYGWIN
63                 #define CSP_OS_WINDOWS
64         #elif defined(Q_OS_WIN) || defined(__WIN32) || defined(__WIN64)
65                 #define CSP_OS_GCC_WINDOWS
66                 #define CSP_OS_WINDOWS
67                 #ifdef USE_SHARED_DLL
68                         #define DLL_PREFIX   __declspec(dllexport)
69                         #define DLL_PREFIX_I __declspec(dllimport)
70                 #endif
71         #else
72                 #define CSP_OS_GCC_GENERIC
73                 #define CSP_OS_GENERIC
74                 #define DLL_PREFIX
75                 #define DLL_PREFIX_I
76         #endif
77         #if defined(__clang__)
78                 #define __CSP_COMPILER_CLANG
79         #else
80                 #define __CSP_COMPILER_GCC
81         #endif
82         #define SUPPORT_CPLUSPLUS_11
83 #else
84                 #define DLL_PREFIX
85                 #define DLL_PREFIX_I
86 #endif
87
88 #ifndef SUPPORT_CPLUSPLUS_11
89         #if defined(__cplusplus) && (__cplusplus > 199711L)
90                 #define SUPPORT_CPLUSPLUS_11
91         #endif
92 #endif
93 #ifndef SUPPORT_TCHAR_TYPE
94         // secure functions need tchar type
95         #undef SUPPORT_SECURE_FUNCTIONS
96 #endif
97 #ifndef DLL_PREFIX
98         #define DLL_PREFIX
99 #endif
100 #ifndef DLL_PREFIX_I
101         #define DLL_PREFIX_I
102 #endif
103
104 // include common header files
105 #ifdef SUPPORT_TCHAR_TYPE
106         #include <tchar.h>
107 #endif
108 #ifdef SUPPORT_CPLUSPLUS_11
109         #include <stdint.h>
110 #endif
111 #include <stdio.h>
112 #include <stdlib.h>
113 #include <string.h>
114 #ifdef _MSC_VER
115 #include <typeinfo.h>
116 #else
117 #include <typeinfo>
118 #endif
119 #include <assert.h>
120 #include <errno.h>
121
122 // include environment/language dependent header files
123 #ifdef _WIN32
124         #include <windows.h>
125         #include <windowsx.h>
126         #include <winuser.h>
127         #include <mmsystem.h>
128         #include <process.h>
129 #endif
130 #ifdef __GNUC__
131         #include <stdarg.h>
132 #endif
133 #ifdef _USE_QT
134         #ifdef _USE_QT5
135                 #include <QString>
136                 #include <QFile>
137                 #include <QtEndian>
138                 #if defined(__MINGW32__) || (__MINGW64__)
139                         #include <windows.h>
140                         #include <winbase.h>
141                 #endif
142         #else
143                 #include <QtCore/QString>
144                 #include <QtCore/QFile>
145         #endif
146         #include <sys/param.h>
147 #endif
148 #ifndef _MAX_PATH
149         #define _MAX_PATH 2048
150 #endif
151
152 // endian
153 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
154         #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
155                 #if __BYTE_ORDER == __LITTLE_ENDIAN
156                         #define __LITTLE_ENDIAN__
157                 #elif __BYTE_ORDER == __BIG_ENDIAN
158                         #define __BIG_ENDIAN__
159                 #endif
160         #elif defined(SDL_BYTEORDER) && (defined(SDL_LIL_ENDIAN) || defined(SDL_BIG_ENDIAN))
161                 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
162                         #define __LITTLE_ENDIAN__
163                 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
164                         #define __BIG_ENDIAN__
165                 #endif
166         #elif defined(WORDS_LITTLEENDIAN)
167                 #define __LITTLE_ENDIAN__
168         #elif defined(WORDS_BIGENDIAN)
169                 #define __BIG_ENDIAN__
170         #endif
171 #endif
172 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
173         // may be Microsoft Visual C++
174         #define __LITTLE_ENDIAN__
175 #endif
176
177 // type definition
178 #ifndef SUPPORT_TCHAR_TYPE
179         #ifndef _TCHAR
180                 typedef char _TCHAR;
181         #endif
182 #endif
183
184 #ifndef SUPPORT_CPLUSPLUS_11
185         #ifndef int8_t
186                 typedef signed char int8_t;
187         #endif
188         #ifndef int16_t
189                 typedef signed short int16_t;
190         #endif
191         #ifndef int32_t
192                 typedef signed int int32_t;
193         #endif
194         #ifndef int64_t
195                 typedef signed long long int64_t;
196         #endif
197         #ifndef uint8_t
198                 typedef unsigned char uint8_t;
199         #endif
200         #ifndef uint16_t
201                 typedef unsigned short uint16_t;
202         #endif
203         #ifndef uint32_t
204                 typedef unsigned int uint32_t;
205         #endif
206         #ifndef uint64_t
207                 typedef unsigned long long uint64_t;
208         #endif
209 #endif
210
211 #ifndef _WIN32
212         #ifndef LPTSTR
213                 typedef _TCHAR* LPTSTR;
214         #endif
215         #ifndef LPCTSTR
216                 typedef const _TCHAR* LPCTSTR;
217         #endif
218         #ifndef BOOL
219                 typedef int BOOL;
220         #endif
221         #ifndef TRUE
222                 #define TRUE 1
223         #endif
224         #ifndef FALSE
225                 #define FALSE 0
226         #endif
227         #ifndef BYTE
228                 typedef uint8_t BYTE;
229         #endif
230         #ifndef WORD
231                 typedef uint16_t WORD;
232         #endif
233         #ifndef DWORD
234                 typedef uint32_t DWORD;
235         #endif
236         #ifndef QWORD
237                 typedef uint64_t QWORD;
238         #endif
239         #ifndef INT8
240                 typedef int8_t INT8;
241         #endif
242         #ifndef INT16
243                 typedef int16_t INT16;
244         #endif
245         #ifndef INT32
246                 typedef int32_t INT32;
247         #endif
248         #ifndef INT64
249                 typedef int64_t INT64;
250         #endif
251         #ifndef UINT8
252                 typedef uint8_t UINT8;
253         #endif
254         #ifndef UINT16
255                 typedef uint16_t UINT16;
256         #endif
257         #ifndef UINT32
258                 typedef uint32_t UINT32;
259         #endif
260         #ifndef UINT64
261                 typedef uint64_t UINT64;
262         #endif
263         #ifndef INT
264                 typedef int INT;
265         #endif
266         #ifndef UINT
267                 typedef unsigned int UINT;
268         #endif
269 #endif
270
271 typedef union {
272         struct {
273 #ifdef __BIG_ENDIAN__
274                 uint8_t h, l;
275 #else
276                 uint8_t l, h;
277 #endif
278         } b;
279         struct {
280 #ifdef __BIG_ENDIAN__
281                 int8_t h, l;
282 #else
283                 int8_t l, h;
284 #endif
285         } sb;
286         uint16_t w;
287         int16_t sw;
288
289         inline void read_2bytes_le_from(uint8_t *t)
290         {
291                 b.l = t[0]; b.h = t[1];
292         }
293         inline void write_2bytes_le_to(uint8_t *t)
294         {
295                 t[0] = b.l; t[1] = b.h;
296         }
297         inline void read_2bytes_be_from(uint8_t *t)
298         {
299                 b.h = t[0]; b.l = t[1];
300         }
301         inline void write_2bytes_be_to(uint8_t *t)
302         {
303                 t[0] = b.h; t[1] = b.l;
304         }
305         
306         inline void set_2bytes_be_from(uint16_t n)
307         {
308                 union {
309                         uint16_t w;
310                         struct {
311                                 uint8_t h, l;
312                         }b;
313                 } bigv;
314                 bigv.w = n;
315                 b.l = bigv.b.l; b.h = bigv.b.h;
316         }
317         inline void set_2bytes_le_from(uint16_t n)
318         {
319                 union {
320                         uint16_t w;
321                         struct {
322                                 uint8_t l, h;
323                         }b;
324                 } littlev;
325                 littlev.w = n;
326                 b.l = littlev.b.l; b.h = littlev.b.h;
327         }
328         inline uint16_t get_2bytes_be_to()
329         {
330                 union {
331                         uint16_t w;
332                         struct {
333                                 uint8_t h, l;
334                         }b;
335                 } bigv;
336                 bigv.b.l = b.l; bigv.b.h = b.h;
337                 return bigv.w;
338         }
339         inline uint16_t get_2bytes_le_to()
340         {
341                 union {
342                         uint16_t w;
343                         struct {
344                                 uint8_t l, h;
345                         }b;
346                 } littlev;
347                 littlev.b.l = b.l; littlev.b.h = b.h;
348                 return littlev.w;
349         }
350
351 } pair16_t;
352
353 typedef union {
354         struct {
355 #ifdef __BIG_ENDIAN__
356                 uint8_t h3, h2, h, l;
357 #else
358                 uint8_t l, h, h2, h3;
359 #endif
360         } b;
361         struct {
362 #ifdef __BIG_ENDIAN__
363                 int8_t h3, h2, h, l;
364 #else
365                 int8_t l, h, h2, h3;
366 #endif
367         } sb;
368         struct {
369 #ifdef __BIG_ENDIAN__
370                 uint16_t h, l;
371 #else
372                 uint16_t l, h;
373 #endif
374         } w;
375         struct {
376 #ifdef __BIG_ENDIAN__
377                 int16_t h, l;
378 #else
379                 int16_t l, h;
380 #endif
381         } sw;
382         struct {
383 #ifdef __BIG_ENDIAN__
384                 pair16_t h, l;
385 #else
386                 pair16_t l, h;
387 #endif
388         } p16;
389         uint32_t d;
390         int32_t sd;
391         inline void read_2bytes_le_from(uint8_t *t)
392         {
393                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
394         }
395         inline void write_2bytes_le_to(uint8_t *t)
396         {
397                 t[0] = b.l; t[1] = b.h;
398         }
399         inline void read_2bytes_be_from(uint8_t *t)
400         {
401                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
402         }
403         inline void write_2bytes_be_to(uint8_t *t)
404         {
405                 t[0] = b.h; t[1] = b.l;
406         }
407         inline void read_4bytes_le_from(uint8_t *t)
408         {
409                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
410         }
411         inline void write_4bytes_le_to(uint8_t *t)
412         {
413                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
414         }
415         inline void read_4bytes_be_from(uint8_t *t)
416         {
417                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
418         }
419         inline void write_4bytes_be_to(uint8_t *t)
420         {
421                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
422         }
423
424         inline void set_2bytes_be_from(uint16_t n)
425         {
426                 union {
427                         uint16_t w;
428                         struct {
429                                 uint8_t h, l;
430                         }b;
431                 } bigv;
432                 bigv.w = n;
433                 b.l = bigv.b.l; b.h = bigv.b.h;
434                 b.h2 = 0; b.h3 = 0;
435         }
436         inline void set_2bytes_le_from(uint16_t n)
437         {
438                 union {
439                         uint16_t w;
440                         struct {
441                                 uint8_t l, h;
442                         }b;
443                 } littlev;
444                 littlev.w = n;
445                 b.l = littlev.b.l; b.h = littlev.b.h;
446                 b.h2 = 0; b.h3 = 0;
447         }
448         inline uint16_t get_2bytes_be_to()
449         {
450                 union {
451                         uint16_t w;
452                         struct {
453                                 uint8_t h, l;
454                         }b;
455                 } bigv;
456                 bigv.b.l = b.l; bigv.b.h = b.h;
457                 return bigv.w;
458         }
459         inline uint16_t get_2bytes_le_to()
460         {
461                 union {
462                         uint16_t w;
463                         struct {
464                                 uint8_t l, h;
465                         }b;
466                 } littlev;
467                 littlev.b.l = b.l; littlev.b.h = b.h;
468                 return littlev.w;
469         }
470         
471         inline void set_4bytes_be_from(uint32_t n)
472         {
473                 union {
474                         uint32_t dw;
475                         struct {
476                                 uint8_t h3, h2, h, l;
477                         }b;
478                 } bigv;
479                 bigv.dw = n;
480                 b.l = bigv.b.l; b.h = bigv.b.h; b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
481         }
482         inline void set_4bytes_le_from(uint32_t n)
483         {
484                 union {
485                         uint32_t dw;
486                         struct {
487                                 uint8_t l, h, h2, h3;
488                         }b;
489                 } littlev;
490                 littlev.dw = n;
491                 b.l = littlev.b.l; b.h = littlev.b.h; b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
492         }
493         inline uint32_t get_4bytes_be_to()
494         {
495                 union {
496                         uint32_t dw;
497                         struct {
498                                 uint8_t h3, h2, h, l;
499                         }b;
500                 } bigv;
501                 bigv.b.l = b.l; bigv.b.h = b.h; bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
502                 return bigv.dw;
503         }
504         inline uint32_t get_4bytes_le_to()
505         {
506                 union {
507                         uint32_t dw;
508                         struct {
509                                 uint8_t l, h, h2, h3;
510                         }b;
511                 } littlev;
512                 littlev.b.l = b.l; littlev.b.h = b.h; littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
513                 return littlev.dw;
514         }
515 } pair_t;
516
517
518 typedef union {
519         struct {
520 #ifdef __BIG_ENDIAN__
521                 uint8_t h7, h6, h5, h4, h3, h2, h, l;
522 #else
523                 uint8_t l, h, h2, h3, h4, h5, h6, h7;
524 #endif
525         } b;
526         struct {
527 #ifdef __BIG_ENDIAN__
528                 int8_t h7, h6, h5, h4, h3, h2, h, l;
529 #else
530                 int8_t l, h, h2, h3, h4, h5, h6, h7;
531 #endif
532         } sb;
533         struct {
534 #ifdef __BIG_ENDIAN__
535                 uint16_t h3, h2, h, l;
536 #else
537                 uint16_t l, h, h2, h3;
538 #endif
539         } w;
540         struct {
541 #ifdef __BIG_ENDIAN__
542                 int16_t h3, h2, h, l;
543 #else
544                 int16_t l, h, h2, h3;
545 #endif
546         } sw;
547         struct {
548 #ifdef __BIG_ENDIAN__
549                 pair16_t h3, h2, h, l;
550 #else
551                 pair16_t l, h, h2, h3;
552 #endif
553         } p16;
554         struct {
555 #ifdef __BIG_ENDIAN__
556                 uint32_t h, l;
557 #else
558                 uint32_t l, h;
559 #endif
560         } d;
561         struct {
562 #ifdef __BIG_ENDIAN__
563                 int32_t h, l;
564 #else
565                 int32_t l, h;
566 #endif
567         } sd;
568         struct {
569 #ifdef __BIG_ENDIAN__
570                 pair_t h, l;
571 #else
572                 pair_t l, h;
573 #endif
574         } p32;
575         uint64_t q;
576         int64_t sq;
577         
578         inline void read_2bytes_le_from(uint8_t *t)
579         {
580                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
581                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
582         }
583         inline void write_2bytes_le_to(uint8_t *t)
584         {
585                 t[0] = b.l; t[1] = b.h;
586         }
587         inline void read_2bytes_be_from(uint8_t *t)
588         {
589                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
590                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
591         }
592         inline void write_2bytes_be_to(uint8_t *t)
593         {
594                 t[0] = b.h; t[1] = b.l;
595         }
596         inline void read_4bytes_le_from(uint8_t *t)
597         {
598                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
599                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
600         }
601         inline void write_4bytes_le_to(uint8_t *t)
602         {
603                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
604         }
605         inline void read_4bytes_be_from(uint8_t *t)
606         {
607                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
608                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
609         }
610         inline void write_4bytes_be_to(uint8_t *t)
611         {
612                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
613         }
614         
615         inline void read_8bytes_le_from(uint8_t *t)
616         {
617                 b.l = t[0];  b.h = t[1];  b.h2 = t[2]; b.h3 = t[3];
618                 b.h4 = t[4]; b.h5 = t[5]; b.h6 = t[6]; b.h7 = t[7];
619         }
620         inline void write_8bytes_le_to(uint8_t *t)
621         {
622                 t[0] = b.l;  t[1] = b.h;  t[2] = b.h2; t[3] = b.h3;
623                 t[4] = b.h4; t[5] = b.h5; t[6] = b.h6; t[7] = b.h7;
624         }
625         inline void read_8bytes_be_from(uint8_t *t)
626         {
627                 b.h7 = t[0]; b.h6 = t[1]; b.h5 = t[2]; b.h4 = t[3];
628                 b.h3 = t[4]; b.h2 = t[5]; b.h = t[6];  b.l = t[7];
629         }
630         inline void write_8bytes_be_to(uint8_t *t)
631         {
632                 t[0] = b.h7; t[1] = b.h6; t[2] = b.h5; t[3] = b.h4;
633                 t[4] = b.h3; t[5] = b.h2; t[6] = b.h;  t[7] = b.l;
634         }
635
636         inline void set_2bytes_be_from(uint16_t n)
637         {
638                 union {
639                         uint16_t w;
640                         struct {
641                                 uint8_t h, l;
642                         }b;
643                 } bigv;
644                 bigv.w = n;
645                 b.l = bigv.b.l; b.h = bigv.b.h;
646                 b.h2 = 0; b.h3 = 0;
647                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
648         }
649         inline void set_2bytes_le_from(uint16_t n)
650         {
651                 union {
652                         uint16_t w;
653                         struct {
654                                 uint8_t l, h;
655                         }b;
656                 } littlev;
657                 littlev.w = n;
658                 b.l = littlev.b.l; b.h = littlev.b.h;
659                 b.h2 = 0; b.h3 = 0;
660                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
661         }
662         inline uint16_t get_2bytes_be_to()
663         {
664                 union {
665                         uint16_t w;
666                         struct {
667                                 uint8_t h, l;
668                         }b;
669                 } bigv;
670                 bigv.b.l = b.l; bigv.b.h = b.h;
671                 return bigv.w;
672         }
673         inline uint16_t get_2bytes_le_to()
674         {
675                 union {
676                         uint16_t w;
677                         struct {
678                                 uint8_t l, h;
679                         }b;
680                 } littlev;
681                 littlev.b.l = b.l; littlev.b.h = b.h;
682                 return littlev.w;
683         }
684         
685         inline void set_4bytes_be_from(uint32_t n)
686         {
687                 union {
688                         uint32_t dw;
689                         struct {
690                                 uint8_t h3, h2, h, l;
691                         }b;
692                 } bigv;
693                 bigv.dw = n;
694                 b.l = bigv.b.l; b.h = bigv.b.h; b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
695                 b.h4 = 0;       b.h5 = 0;       b.h6 = 0;         b.h7 = 0;
696         }
697         inline void set_4bytes_le_from(uint32_t n)
698         {
699                 union {
700                         uint32_t dw;
701                         struct {
702                                 uint8_t l, h, h2, h3;
703                         }b;
704                 } littlev;
705                 littlev.dw = n;
706                 b.l = littlev.b.l; b.h = littlev.b.h; b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
707                 b.h4 = 0;          b.h5 = 0;          b.h6 = 0;            b.h7 = 0;
708         }
709         inline uint32_t get_4bytes_be_to()
710         {
711                 union {
712                         uint32_t dw;
713                         struct {
714                                 uint8_t h3, h2, h, l;
715                         }b;
716                 } bigv;
717                 bigv.b.l = b.l; bigv.b.h = b.h; bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
718                 return bigv.dw;
719         }
720         inline uint32_t get_4bytes_le_to()
721         {
722                 union {
723                         uint32_t dw;
724                         struct {
725                                 uint8_t l, h, h2, h3;
726                         }b;
727                 } littlev;
728                 littlev.b.l = b.l; littlev.b.h = b.h; littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
729                 return littlev.dw;
730         }
731
732         inline void set_8bytes_be_from(uint64_t n)
733         {
734                 union {
735                         uint64_t qw;
736                         struct {
737                                 uint8_t h7, h6, h5, h4, h3, h2, h, l;
738                         }b;
739                 } bigv;
740                 bigv.qw = n;
741                 b.l = bigv.b.l;   b.h = bigv.b.h;   b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
742                 b.h4 = bigv.b.h4; b.h5 = bigv.b.h5; b.h6 = bigv.b.h6; b.h7 = bigv.b.h7;
743         }
744         inline void set_8bytes_le_from(uint64_t n)
745         {
746                 union {
747                         uint64_t qw;
748                         struct {
749                                 uint8_t l, h, h2, h3, h4, h5, h6, h7;
750                         }b;
751                 } littlev;
752                 littlev.qw = n;
753                 b.l = littlev.b.l;   b.h = littlev.b.h;   b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
754                 b.h4 = littlev.b.h4; b.h5 = littlev.b.h5; b.h6 = littlev.b.h6; b.h7 = littlev.b.h7;
755         }
756         inline uint64_t get_8bytes_be_to()
757         {
758                 union {
759                         uint64_t qw;
760                         struct {
761                                 uint8_t h7, h6, h5, h4, h3, h2, h, l;
762                         }b;
763                 } bigv;
764                 bigv.b.l = b.l;   bigv.b.h = b.h;   bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
765                 bigv.b.h4 = b.h4; bigv.b.h5 = b.h5; bigv.b.h6 = b.h6; bigv.b.h7 = b.h7;
766                 return bigv.qw;
767         }
768         inline uint64_t get_8bytes_le_to()
769         {
770                 union {
771                         uint64_t qw;
772                         struct {
773                                 uint8_t l, h, h2, h3, h4, h5, h6, h7;
774                         }b;
775                 } littlev;
776                 littlev.b.l = b.l;   littlev.b.h = b.h;   littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
777                 littlev.b.h4 = b.h4; littlev.b.h5 = b.h5; littlev.b.h6 = b.h6; littlev.b.h7 = b.h7;
778                 return littlev.qw;
779         }
780
781 } pair64_t;
782
783 uint32_t DLL_PREFIX EndianToLittle_DWORD(uint32_t x);
784 uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x);
785 uint32_t DLL_PREFIX EndianFromLittle_DWORD(uint32_t x);
786 uint16_t DLL_PREFIX EndianFromLittle_WORD(uint16_t x);
787
788 uint32_t DLL_PREFIX EndianToBig_DWORD(uint32_t x);
789 uint16_t DLL_PREFIX EndianToBig_WORD(uint16_t x);
790 uint32_t DLL_PREFIX EndianFromBig_DWORD(uint32_t x);
791 uint16_t DLL_PREFIX EndianFromBig_WORD(uint16_t x);
792 // max/min
793 #ifndef _MSC_VER
794         #undef max
795         #undef min
796         int DLL_PREFIX max(int a, int b);
797         unsigned int DLL_PREFIX max(int a, unsigned int b);
798         unsigned int DLL_PREFIX max(unsigned int a, int b);
799         unsigned int DLL_PREFIX max(unsigned int a, unsigned int b);
800         int DLL_PREFIX min(int a, int b);
801         int DLL_PREFIX min(unsigned int a, int b);
802         int DLL_PREFIX min(int a, unsigned int b);
803         unsigned int DLL_PREFIX min(unsigned int a, unsigned int b);
804 #endif
805
806 // string
807 #if defined(__GNUC__) || defined(__CYGWIN__) || defined(Q_OS_CYGWIN)
808         #define stricmp(a,b) strcasecmp(a,b)
809         #define strnicmp(a,b,n) strncasecmp(a,b,n)
810 #endif
811
812 #ifndef SUPPORT_TCHAR_TYPE
813         #ifndef _fgetts
814                 #define _fgetts fgets
815         #endif
816         #ifndef _ftprintf
817                 #define _ftprintf printf
818         #endif
819         #ifndef _tfopen
820                 #define _tfopen fopen
821         #endif
822         #ifndef _tcscmp
823                 #define _tcscmp strcmp
824         #endif
825         #ifndef _tcscpy
826                 #define _tcscpy strcpy
827         #endif
828         #ifndef _tcsicmp
829                 #define _tcsicmp stricmp
830         #endif
831         #ifndef _tcslen
832                 #define _tcslen strlen
833         #endif
834         #ifndef _tcscat
835                 #define _tcscat strcat
836         #endif
837         #ifndef _tcsncat
838                 #define _tcsncat strncat
839         #endif
840         #ifndef _tcsncpy
841                 #define _tcsncpy strncpy
842         #endif
843         #ifndef _tcsncicmp
844                 #define _tcsncicmp strnicmp
845         #endif
846         #ifndef _tcschr
847                 #define _tcschr strchr
848         #endif
849         #ifndef _tcsrchr
850                 #define _tcsrchr strrchr
851         #endif
852         #ifndef _tcsstr
853                 #define _tcsstr strstr
854         #endif
855         #ifndef _tcstok
856                 #define _tcstok strtok
857         #endif
858         #ifndef _tstoi
859                 #define _tstoi atoi
860         #endif
861         #ifndef _tcstol
862                 #define _tcstol strtol
863         #endif
864         #ifndef _tcstoul
865                 #define _tcstoul strtoul
866         #endif
867         #ifndef _stprintf
868                 #define _stprintf sprintf
869         #endif
870         #ifndef _vstprintf
871                 #define _vstprintf vsprintf
872         #endif
873         #ifndef _taccess
874                 #define _taccess access
875         #endif
876         #ifndef _tremove
877                 #define _tremove remove
878         #endif
879         #ifndef _trename
880                 #define _trename rename
881         #endif
882         #define __T(x) x
883         #define _T(x) __T(x)
884         #define _TEXT(x) __T(x)
885 #endif
886
887 #ifndef SUPPORT_SECURE_FUNCTIONS
888         #ifndef errno_t
889                 typedef int errno_t;
890         #endif
891 //      errno_t DLL_PREFIX my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);
892         errno_t DLL_PREFIX my_tcscat_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
893         errno_t DLL_PREFIX my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
894         errno_t DLL_PREFIX my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
895         errno_t DLL_PREFIX my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count);
896         errno_t DLL_PREFIX my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count);
897         char * DLL_PREFIX my_strtok_s(char *strToken, const char *strDelimit, char **context);
898         _TCHAR *DLL_PREFIX my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
899         #define my_fprintf_s fprintf
900         #define my_ftprintf_s fprintf
901         int DLL_PREFIX my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
902         int DLL_PREFIX my_swprintf_s(wchar_t *buffer, size_t sizeOfBuffer, const wchar_t *format, ...);
903         int DLL_PREFIX my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
904         int DLL_PREFIX my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr);
905         int DLL_PREFIX my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
906 #else
907 //      #define my_tfopen_s _tfopen_s
908         #define my_tcscat_s _tcscat_s
909         #define my_strcpy_s strcpy_s
910         #define my_tcscpy_s _tcscpy_s
911         #define my_strncpy_s strncpy_s
912         #define my_tcsncpy_s _tcsncpy_s
913         #define my_strtok_s strtok_s
914         #define my_tcstok_s _tcstok_s
915         #define my_fprintf_s fprintf_s
916         #define my_ftprintf_s _ftprintf_s
917         #define my_sprintf_s sprintf_s
918         #define my_swprintf_s swprintf_s
919         #define my_stprintf_s _stprintf_s
920         #define my_vsprintf_s vsprintf_s
921         #define my_vstprintf_s _vstprintf_s
922 #endif
923
924 // memory
925 #ifndef _MSC_VER
926         void *DLL_PREFIX my_memcpy(void *dst, void *src, size_t len);
927 #else
928         #define my_memcpy memcpy
929 #endif
930
931 // hint for SIMD
932 #if defined(__clang__)
933         #define __DECL_VECTORIZED_LOOP   _Pragma("clang loop vectorize(enable) interleave(enable)")
934 #elif defined(__GNUC__)
935         #define __DECL_VECTORIZED_LOOP  _Pragma("GCC ivdep")
936 #else
937         #define __DECL_VECTORIZED_LOOP
938 #endif
939
940 // C99 math functions
941 #ifdef _MSC_VER
942         #define my_isfinite _finite
943         #define my_log2(v) (log((double)(v)) / log(2.0))
944 #else
945         #include <cmath>
946         #define my_isfinite std::isfinite
947         #define my_log2 log2
948 #endif
949
950 // win32 api
951 #ifndef _WIN32
952         BOOL MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);
953         DWORD MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName);
954         UINT MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName);
955         // used only in winmain and win32 osd class
956 //      #define ZeroMemory(p,s) memset(p,0x00,s)
957 //      #define CopyMemory(t,f,s) memcpy(t,f,s)
958 #else
959         #define MyWritePrivateProfileString WritePrivateProfileString
960         #define MyGetPrivateProfileString GetPrivateProfileString
961         #define MyGetPrivateProfileInt GetPrivateProfileInt
962 #endif
963
964 // rgb color
965 #if !defined(_RGB555) && !defined(_RGB565) && !defined(_RGB888)
966         #define _RGB888
967 #endif
968
969 #if defined(_RGB555) || defined(_RGB565)
970         typedef uint16_t scrntype_t;
971         scrntype_t DLL_PREFIX RGB_COLOR(uint32_t r, uint32_t g, uint32_t b);
972         scrntype_t DLL_PREFIX RGBA_COLOR(uint32_t r, uint32_t g, uint32_t b, uint32_t a);
973         uint8_t DLL_PREFIX R_OF_COLOR(scrntype_t c);
974         uint8_t DLL_PREFIX G_OF_COLOR(scrntype_t c);
975         uint8_t DLL_PREFIX B_OF_COLOR(scrntype_t c);
976         uint8_t DLL_PREFIX A_OF_COLOR(scrntype_t c);
977 #elif defined(_RGB888)
978         typedef uint32_t scrntype_t;
979 #if defined(__LITTLE_ENDIAN__)
980         #define RGB_COLOR(r, g, b)      (((uint32_t)(b) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(r) << 0) | (0xff << 24))
981         #define RGBA_COLOR(r, g, b, a)  (((uint32_t)(b) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(r) << 0) | ((uint32_t)(a) << 24))
982         #define R_OF_COLOR(c)           (((c)      ) & 0xff)
983         #define G_OF_COLOR(c)           (((c) >>  8) & 0xff)
984         #define B_OF_COLOR(c)           (((c) >> 16) & 0xff)
985         #define A_OF_COLOR(c)           (((c) >> 24) & 0xff)
986 #else
987         #define RGB_COLOR(r, g, b)      (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | (0xff << 24))
988         #define RGBA_COLOR(r, g, b, a)  (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | ((uint32_t)(a) << 24))
989         #define R_OF_COLOR(c)           (((c) >> 16) & 0xff)
990         #define G_OF_COLOR(c)           (((c) >>  8) & 0xff)
991         #define B_OF_COLOR(c)           (((c)      ) & 0xff)
992         #define A_OF_COLOR(c)           (((c) >> 24) & 0xff)
993 #endif
994 #endif
995
996 inline uint64_t ExchangeEndianU64(uint64_t __in)
997 {
998         pair64_t __i, __o;
999         __i.q = __in;
1000         __o.b.h7  = __i.b.l;
1001         __o.b.h6  = __i.b.h;
1002         __o.b.h5  = __i.b.h2;
1003         __o.b.h4  = __i.b.h3;
1004         __o.b.h3  = __i.b.h4;
1005         __o.b.h2  = __i.b.h5;
1006         __o.b.h   = __i.b.h6;
1007         __o.b.l   = __i.b.h7;
1008         return __o.q;
1009 }
1010
1011 inline int64_t ExchangeEndianS64(uint64_t __in)
1012 {
1013         pair64_t __i, __o;
1014         __i.q = __in;
1015         __o.b.h7  = __i.b.l;
1016         __o.b.h6  = __i.b.h;
1017         __o.b.h5  = __i.b.h2;
1018         __o.b.h4  = __i.b.h3;
1019         __o.b.h3  = __i.b.h4;
1020         __o.b.h2  = __i.b.h5;
1021         __o.b.h   = __i.b.h6;
1022         __o.b.l   = __i.b.h7;
1023         return __o.sq;
1024 }
1025 inline uint32_t ExchangeEndianU32(uint32_t __in)
1026 {
1027         pair_t __i, __o;
1028         __i.d = __in;
1029         __o.b.h3 = __i.b.l;
1030         __o.b.h2 = __i.b.h;
1031         __o.b.h  = __i.b.h2;
1032         __o.b.l  = __i.b.h3;
1033         return __o.d;
1034 }
1035
1036 inline int32_t ExchangeEndianS32(uint32_t __in)
1037 {
1038         pair_t __i, __o;
1039         __i.d = __in;
1040         __o.b.h3 = __i.b.l;
1041         __o.b.h2 = __i.b.h;
1042         __o.b.h  = __i.b.h2;
1043         __o.b.l  = __i.b.h3;
1044         return __o.sd;
1045 }
1046
1047 inline uint16_t ExchangeEndianU16(uint16_t __in)
1048 {
1049         pair16_t __i, __o;
1050         __i.w = __in;
1051         __o.b.h = __i.b.l;
1052         __o.b.l  = __i.b.h;
1053         return __o.w;
1054 }
1055
1056 inline int16_t ExchangeEndianS16(uint16_t __in)
1057 {
1058         pair16_t __i, __o;
1059         __i.w = __in;
1060         __o.b.h = __i.b.l;
1061         __o.b.l = __i.b.h;
1062         return __o.sw;
1063 }
1064
1065 // wav file header
1066 #pragma pack(1)
1067 typedef struct {
1068         char id[4];
1069         uint32_t size;
1070 } wav_chunk_t;
1071 #pragma pack()
1072
1073 #pragma pack(1)
1074 typedef struct {
1075         wav_chunk_t riff_chunk;
1076         char wave[4];
1077         wav_chunk_t fmt_chunk;
1078         uint16_t format_id;
1079         uint16_t channels;
1080         uint32_t sample_rate;
1081         uint32_t data_speed;
1082         uint16_t block_size;
1083         uint16_t sample_bits;
1084 } wav_header_t;
1085 #pragma pack()
1086
1087 //  See http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html.
1088 #pragma pack(1)
1089 typedef struct {
1090         wav_chunk_t riff_chunk;
1091         char wave[4];
1092         wav_chunk_t fmt_chunk;
1093         uint16_t format_id;
1094         uint16_t channels;
1095         uint32_t sample_rate;
1096         uint32_t data_speed;
1097         uint16_t block_size;
1098         uint16_t sample_bits;
1099         uint16_t cbsize; // Extension size.Normaly set to 0.
1100         wav_chunk_t fact_chunk; // "fact", 4.
1101 } wav_header_float_t;
1102 #pragma pack()
1103
1104 // Use this before writing wav_data.
1105 bool DLL_PREFIX write_dummy_wav_header(void *__fio);
1106 // Use this after writng wav_data.
1107 bool DLL_PREFIX set_wav_header(wav_header_t *header, wav_chunk_t *first_chunk, uint16_t channels, uint32_t rate,
1108                                                            uint16_t bits, size_t file_length);
1109 bool DLL_PREFIX load_wav_to_stereo(void *__fio, int16_t **left_buf, int16_t **right_buf, uint32_t *rate, int *got_samples);
1110 bool DLL_PREFIX load_wav_to_monoral(void *__fio, int16_t **buffer, uint32_t *rate, int *got_samples);
1111
1112 // file path
1113 const _TCHAR *DLL_PREFIX get_application_path();
1114 const _TCHAR *DLL_PREFIX create_local_path(const _TCHAR *format, ...);
1115 void DLL_PREFIX create_local_path(_TCHAR *file_path, int length, const _TCHAR *format, ...);
1116 const _TCHAR *DLL_PREFIX create_date_file_path(const _TCHAR *extension);
1117 bool DLL_PREFIX is_absolute_path(const _TCHAR *file_path);
1118 const _TCHAR *DLL_PREFIX create_absolute_path(const _TCHAR *file_name);
1119 void DLL_PREFIX create_absolute_path(_TCHAR *file_path, int length, const _TCHAR *file_name);
1120 void DLL_PREFIX create_date_file_path(_TCHAR *file_path, int length, const _TCHAR *extension);
1121 bool DLL_PREFIX check_file_extension(const _TCHAR *file_path, const _TCHAR *ext);
1122 const _TCHAR *DLL_PREFIX get_file_path_without_extensiton(const _TCHAR *file_path);
1123 void DLL_PREFIX get_long_full_path_name(const _TCHAR* src, _TCHAR* dst, size_t dst_len);
1124 const _TCHAR* DLL_PREFIX get_parent_dir(const _TCHAR* file);
1125
1126 // string
1127 const _TCHAR *DLL_PREFIX create_string(const _TCHAR* format, ...);
1128 const wchar_t *DLL_PREFIX char_to_wchar(const char *cs);
1129 const char *DLL_PREFIX wchar_to_char(const wchar_t *ws);
1130 const _TCHAR *DLL_PREFIX char_to_tchar(const char *cs);
1131 const char *DLL_PREFIX tchar_to_char(const _TCHAR *ts);
1132 const _TCHAR *DLL_PREFIX wchar_to_tchar(const wchar_t *ws);
1133 const wchar_t *DLL_PREFIX tchar_to_wchar(const _TCHAR *ts);
1134
1135
1136 // misc
1137 int32_t DLL_PREFIX muldiv_s32(int32_t nNumber, int32_t nNumerator, int32_t nDenominator);
1138 uint32_t DLL_PREFIX muldiv_u32(uint32_t nNumber, uint32_t nNumerator, uint32_t nDenominator);
1139
1140 uint32_t DLL_PREFIX get_crc32(uint8_t data[], int size);
1141 uint32_t DLL_PREFIX calc_crc32(uint32_t seed, uint8_t data[], int size);
1142 uint16_t DLL_PREFIX jis_to_sjis(uint16_t jis);
1143
1144 int DLL_PREFIX decibel_to_volume(int decibel);
1145 int32_t DLL_PREFIX apply_volume(int32_t sample, int volume);
1146
1147 #define array_length(array) (sizeof(array) / sizeof(array[0]))
1148
1149 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
1150 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
1151 #define TO_BCD_LO(v)    ((v) % 10)
1152 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)
1153
1154 // time
1155 #define LEAP_YEAR(y)    (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
1156
1157 #define dll_cur_time_t DLL_PREFIX_I struct cur_time_s
1158
1159 typedef DLL_PREFIX struct cur_time_s {
1160         int year, month, day, day_of_week, hour, minute, second;
1161         bool initialized;
1162         cur_time_s()
1163         {
1164                 initialized = false;
1165         }
1166         void increment();
1167         void update_year();
1168         void update_day_of_week();
1169         void save_state(void *f);
1170         bool load_state(void *f);
1171         void save_state_helper(void *f, uint32_t *sumseed, bool *__stat);
1172         bool load_state_helper(void *f, uint32_t *sumseed, bool *__stat);
1173 } cur_time_t;
1174
1175 void DLL_PREFIX get_host_time(cur_time_t* cur_time);
1176
1177 // symbol
1178 typedef struct symbol_s {
1179         uint32_t addr;
1180         _TCHAR *name;
1181         struct symbol_s *next_symbol;
1182 } symbol_t;
1183
1184 const _TCHAR* DLL_PREFIX get_symbol(symbol_t *first_symbol, uint32_t addr);
1185 const _TCHAR* DLL_PREFIX get_value_or_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);
1186 const _TCHAR* DLL_PREFIX get_value_and_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);
1187
1188 #endif