OSDN Git Service

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