OSDN Git Service

[COMMON] Fix unaligned SIMD variables.Fix crash built with "-msse2" at Win32.
[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 #ifdef _USE_QT
19 #include <SDL.h>
20 #endif
21
22 // use zlib to decompress gzip file???
23 #ifdef _WIN32
24         #if defined(_MSC_VER) && (_MSC_VER >= 1500)
25                 #ifndef _ANY2D88
26                         #define USE_ZLIB
27                 #endif
28         #endif
29 #endif
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 u16; // ToDo: Remove
287         int16_t s16; // ToDo: Remove
288         uint16_t w;
289         int16_t sw;
290
291         inline void read_2bytes_le_from(uint8_t *t)
292         {
293                 b.l = t[0]; b.h = t[1];
294         }
295         inline void write_2bytes_le_to(uint8_t *t)
296         {
297                 t[0] = b.l; t[1] = b.h;
298         }
299         inline void read_2bytes_be_from(uint8_t *t)
300         {
301                 b.h = t[0]; b.l = t[1];
302         }
303         inline void write_2bytes_be_to(uint8_t *t)
304         {
305                 t[0] = b.h; t[1] = b.l;
306         }
307         
308         inline void set_2bytes_be_from(uint16_t n)
309         {
310                 union {
311                         uint16_t w;
312                         struct {
313                                 uint8_t h, l;
314                         }b;
315                 } bigv;
316                 bigv.w = n;
317                 b.l = bigv.b.l; b.h = bigv.b.h;
318         }
319         inline void set_2bytes_le_from(uint16_t n)
320         {
321                 union {
322                         uint16_t w;
323                         struct {
324                                 uint8_t l, h;
325                         }b;
326                 } littlev;
327                 littlev.w = n;
328                 b.l = littlev.b.l; b.h = littlev.b.h;
329         }
330         inline uint16_t get_2bytes_be_to()
331         {
332                 union {
333                         uint16_t w;
334                         struct {
335                                 uint8_t h, l;
336                         }b;
337                 } bigv;
338                 bigv.b.l = b.l; bigv.b.h = b.h;
339                 return bigv.w;
340         }
341         inline uint16_t get_2bytes_le_to()
342         {
343                 union {
344                         uint16_t w;
345                         struct {
346                                 uint8_t l, h;
347                         }b;
348                 } littlev;
349                 littlev.b.l = b.l; littlev.b.h = b.h;
350                 return littlev.w;
351         }
352
353 } pair16_t;
354
355 typedef union {
356         struct {
357 #ifdef __BIG_ENDIAN__
358                 uint8_t h3, h2, h, l;
359 #else
360                 uint8_t l, h, h2, h3;
361 #endif
362         } b;
363         struct {
364 #ifdef __BIG_ENDIAN__
365                 int8_t h3, h2, h, l;
366 #else
367                 int8_t l, h, h2, h3;
368 #endif
369         } sb;
370         struct {
371 #ifdef __BIG_ENDIAN__
372                 uint16_t h, l;
373 #else
374                 uint16_t l, h;
375 #endif
376         } w;
377         struct {
378 #ifdef __BIG_ENDIAN__
379                 int16_t h, l;
380 #else
381                 int16_t l, h;
382 #endif
383         } sw;
384         struct {
385 #ifdef __BIG_ENDIAN__
386                 pair16_t h, l;
387 #else
388                 pair16_t l, h;
389 #endif
390         } p16;
391         uint32_t d;
392         int32_t sd;
393         float f; // single float
394   
395         inline void read_2bytes_le_from(uint8_t *t)
396         {
397                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
398         }
399         inline void write_2bytes_le_to(uint8_t *t)
400         {
401                 t[0] = b.l; t[1] = b.h;
402         }
403         inline void read_2bytes_be_from(uint8_t *t)
404         {
405                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
406         }
407         inline void write_2bytes_be_to(uint8_t *t)
408         {
409                 t[0] = b.h; t[1] = b.l;
410         }
411         inline void read_4bytes_le_from(uint8_t *t)
412         {
413                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
414         }
415         inline void write_4bytes_le_to(uint8_t *t)
416         {
417                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
418         }
419         inline void read_4bytes_be_from(uint8_t *t)
420         {
421                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
422         }
423         inline void write_4bytes_be_to(uint8_t *t)
424         {
425                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
426         }
427
428         inline void set_2bytes_be_from(uint16_t n)
429         {
430                 union {
431                         uint16_t w;
432                         struct {
433                                 uint8_t h, l;
434                         }b;
435                 } bigv;
436                 bigv.w = n;
437                 b.l = bigv.b.l; b.h = bigv.b.h;
438                 b.h2 = 0; b.h3 = 0;
439         }
440         inline void set_2bytes_le_from(uint16_t n)
441         {
442                 union {
443                         uint16_t w;
444                         struct {
445                                 uint8_t l, h;
446                         }b;
447                 } littlev;
448                 littlev.w = n;
449                 b.l = littlev.b.l; b.h = littlev.b.h;
450                 b.h2 = 0; b.h3 = 0;
451         }
452         inline uint16_t get_2bytes_be_to()
453         {
454                 union {
455                         uint16_t w;
456                         struct {
457                                 uint8_t h, l;
458                         }b;
459                 } bigv;
460                 bigv.b.l = b.l; bigv.b.h = b.h;
461                 return bigv.w;
462         }
463         inline uint16_t get_2bytes_le_to()
464         {
465                 union {
466                         uint16_t w;
467                         struct {
468                                 uint8_t l, h;
469                         }b;
470                 } littlev;
471                 littlev.b.l = b.l; littlev.b.h = b.h;
472                 return littlev.w;
473         }
474         
475         inline void set_4bytes_be_from(uint32_t n)
476         {
477                 union {
478                         uint32_t dw;
479                         struct {
480                                 uint8_t h3, h2, h, l;
481                         }b;
482                 } bigv;
483                 bigv.dw = n;
484                 b.l = bigv.b.l; b.h = bigv.b.h; b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
485         }
486         inline void set_4bytes_le_from(uint32_t n)
487         {
488                 union {
489                         uint32_t dw;
490                         struct {
491                                 uint8_t l, h, h2, h3;
492                         }b;
493                 } littlev;
494                 littlev.dw = n;
495                 b.l = littlev.b.l; b.h = littlev.b.h; b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
496         }
497         inline uint32_t get_4bytes_be_to()
498         {
499                 union {
500                         uint32_t dw;
501                         struct {
502                                 uint8_t h3, h2, h, l;
503                         }b;
504                 } bigv;
505                 bigv.b.l = b.l; bigv.b.h = b.h; bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
506                 return bigv.dw;
507         }
508         inline uint32_t get_4bytes_le_to()
509         {
510                 union {
511                         uint32_t dw;
512                         struct {
513                                 uint8_t l, h, h2, h3;
514                         }b;
515                 } littlev;
516                 littlev.b.l = b.l; littlev.b.h = b.h; littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
517                 return littlev.dw;
518         }
519 } pair32_t;
520
521
522 typedef union {
523         struct {
524 #ifdef __BIG_ENDIAN__
525                 uint8_t h7, h6, h5, h4, h3, h2, h, l;
526 #else
527                 uint8_t l, h, h2, h3, h4, h5, h6, h7;
528 #endif
529         } b;
530         struct {
531 #ifdef __BIG_ENDIAN__
532                 int8_t h7, h6, h5, h4, h3, h2, h, l;
533 #else
534                 int8_t l, h, h2, h3, h4, h5, h6, h7;
535 #endif
536         } sb;
537         struct {
538 #ifdef __BIG_ENDIAN__
539                 uint16_t h3, h2, h, l;
540 #else
541                 uint16_t l, h, h2, h3;
542 #endif
543         } w;
544         struct {
545 #ifdef __BIG_ENDIAN__
546                 int16_t h3, h2, h, l;
547 #else
548                 int16_t l, h, h2, h3;
549 #endif
550         } sw;
551         struct {
552 #ifdef __BIG_ENDIAN__
553                 pair16_t h3, h2, h, l;
554 #else
555                 pair16_t l, h, h2, h3;
556 #endif
557         } p16;
558         struct {
559 #ifdef __BIG_ENDIAN__
560                 uint32_t h, l;
561 #else
562                 uint32_t l, h;
563 #endif
564         } d;
565         struct {
566 #ifdef __BIG_ENDIAN__
567                 int32_t h, l;
568 #else
569                 int32_t l, h;
570 #endif
571         } sd;
572         struct {
573 #ifdef __BIG_ENDIAN__
574                 pair32_t h, l;
575 #else
576                 pair32_t l, h;
577 #endif
578         } p32;
579         struct {
580 #ifdef __BIG_ENDIAN__
581                 float h, l;
582 #else
583                 float l, h;
584 #endif
585         } f32;
586         uint64_t q;
587         int64_t sq;
588         double df; // double float
589         inline void read_2bytes_le_from(uint8_t *t)
590         {
591                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
592                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
593         }
594         inline void write_2bytes_le_to(uint8_t *t)
595         {
596                 t[0] = b.l; t[1] = b.h;
597         }
598         inline void read_2bytes_be_from(uint8_t *t)
599         {
600                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
601                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
602         }
603         inline void write_2bytes_be_to(uint8_t *t)
604         {
605                 t[0] = b.h; t[1] = b.l;
606         }
607         inline void read_4bytes_le_from(uint8_t *t)
608         {
609                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
610                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
611         }
612         inline void write_4bytes_le_to(uint8_t *t)
613         {
614                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
615         }
616         inline void read_4bytes_be_from(uint8_t *t)
617         {
618                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
619                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
620         }
621         inline void write_4bytes_be_to(uint8_t *t)
622         {
623                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
624         }
625         
626         inline void read_8bytes_le_from(uint8_t *t)
627         {
628                 b.l = t[0];  b.h = t[1];  b.h2 = t[2]; b.h3 = t[3];
629                 b.h4 = t[4]; b.h5 = t[5]; b.h6 = t[6]; b.h7 = t[7];
630         }
631         inline void write_8bytes_le_to(uint8_t *t)
632         {
633                 t[0] = b.l;  t[1] = b.h;  t[2] = b.h2; t[3] = b.h3;
634                 t[4] = b.h4; t[5] = b.h5; t[6] = b.h6; t[7] = b.h7;
635         }
636         inline void read_8bytes_be_from(uint8_t *t)
637         {
638                 b.h7 = t[0]; b.h6 = t[1]; b.h5 = t[2]; b.h4 = t[3];
639                 b.h3 = t[4]; b.h2 = t[5]; b.h = t[6];  b.l = t[7];
640         }
641         inline void write_8bytes_be_to(uint8_t *t)
642         {
643                 t[0] = b.h7; t[1] = b.h6; t[2] = b.h5; t[3] = b.h4;
644                 t[4] = b.h3; t[5] = b.h2; t[6] = b.h;  t[7] = b.l;
645         }
646
647         inline void set_2bytes_be_from(uint16_t n)
648         {
649                 union {
650                         uint16_t w;
651                         struct {
652                                 uint8_t h, l;
653                         }b;
654                 } bigv;
655                 bigv.w = n;
656                 b.l = bigv.b.l; b.h = bigv.b.h;
657                 b.h2 = 0; b.h3 = 0;
658                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
659         }
660         inline void set_2bytes_le_from(uint16_t n)
661         {
662                 union {
663                         uint16_t w;
664                         struct {
665                                 uint8_t l, h;
666                         }b;
667                 } littlev;
668                 littlev.w = n;
669                 b.l = littlev.b.l; b.h = littlev.b.h;
670                 b.h2 = 0; b.h3 = 0;
671                 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
672         }
673         inline uint16_t get_2bytes_be_to()
674         {
675                 union {
676                         uint16_t w;
677                         struct {
678                                 uint8_t h, l;
679                         }b;
680                 } bigv;
681                 bigv.b.l = b.l; bigv.b.h = b.h;
682                 return bigv.w;
683         }
684         inline uint16_t get_2bytes_le_to()
685         {
686                 union {
687                         uint16_t w;
688                         struct {
689                                 uint8_t l, h;
690                         }b;
691                 } littlev;
692                 littlev.b.l = b.l; littlev.b.h = b.h;
693                 return littlev.w;
694         }
695         
696         inline void set_4bytes_be_from(uint32_t n)
697         {
698                 union {
699                         uint32_t dw;
700                         struct {
701                                 uint8_t h3, h2, h, l;
702                         }b;
703                 } bigv;
704                 bigv.dw = n;
705                 b.l = bigv.b.l; b.h = bigv.b.h; b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
706                 b.h4 = 0;       b.h5 = 0;       b.h6 = 0;         b.h7 = 0;
707         }
708         inline void set_4bytes_le_from(uint32_t n)
709         {
710                 union {
711                         uint32_t dw;
712                         struct {
713                                 uint8_t l, h, h2, h3;
714                         }b;
715                 } littlev;
716                 littlev.dw = n;
717                 b.l = littlev.b.l; b.h = littlev.b.h; b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
718                 b.h4 = 0;          b.h5 = 0;          b.h6 = 0;            b.h7 = 0;
719         }
720         inline uint32_t get_4bytes_be_to()
721         {
722                 union {
723                         uint32_t dw;
724                         struct {
725                                 uint8_t h3, h2, h, l;
726                         }b;
727                 } bigv;
728                 bigv.b.l = b.l; bigv.b.h = b.h; bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
729                 return bigv.dw;
730         }
731         inline uint32_t get_4bytes_le_to()
732         {
733                 union {
734                         uint32_t dw;
735                         struct {
736                                 uint8_t l, h, h2, h3;
737                         }b;
738                 } littlev;
739                 littlev.b.l = b.l; littlev.b.h = b.h; littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
740                 return littlev.dw;
741         }
742
743         inline void set_8bytes_be_from(uint64_t n)
744         {
745                 union {
746                         uint64_t qw;
747                         struct {
748                                 uint8_t h7, h6, h5, h4, h3, h2, h, l;
749                         }b;
750                 } bigv;
751                 bigv.qw = n;
752                 b.l = bigv.b.l;   b.h = bigv.b.h;   b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
753                 b.h4 = bigv.b.h4; b.h5 = bigv.b.h5; b.h6 = bigv.b.h6; b.h7 = bigv.b.h7;
754         }
755         inline void set_8bytes_le_from(uint64_t n)
756         {
757                 union {
758                         uint64_t qw;
759                         struct {
760                                 uint8_t l, h, h2, h3, h4, h5, h6, h7;
761                         }b;
762                 } littlev;
763                 littlev.qw = n;
764                 b.l = littlev.b.l;   b.h = littlev.b.h;   b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
765                 b.h4 = littlev.b.h4; b.h5 = littlev.b.h5; b.h6 = littlev.b.h6; b.h7 = littlev.b.h7;
766         }
767         inline uint64_t get_8bytes_be_to()
768         {
769                 union {
770                         uint64_t qw;
771                         struct {
772                                 uint8_t h7, h6, h5, h4, h3, h2, h, l;
773                         }b;
774                 } bigv;
775                 bigv.b.l = b.l;   bigv.b.h = b.h;   bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
776                 bigv.b.h4 = b.h4; bigv.b.h5 = b.h5; bigv.b.h6 = b.h6; bigv.b.h7 = b.h7;
777                 return bigv.qw;
778         }
779         inline uint64_t get_8bytes_le_to()
780         {
781                 union {
782                         uint64_t qw;
783                         struct {
784                                 uint8_t l, h, h2, h3, h4, h5, h6, h7;
785                         }b;
786                 } littlev;
787                 littlev.b.l = b.l;   littlev.b.h = b.h;   littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
788                 littlev.b.h4 = b.h4; littlev.b.h5 = b.h5; littlev.b.h6 = b.h6; littlev.b.h7 = b.h7;
789                 return littlev.qw;
790         }
791
792 } pair64_t;
793
794 uint32_t DLL_PREFIX EndianToLittle_DWORD(uint32_t x);
795 uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x);
796 uint32_t DLL_PREFIX EndianFromLittle_DWORD(uint32_t x);
797 uint16_t DLL_PREFIX EndianFromLittle_WORD(uint16_t x);
798
799 uint32_t DLL_PREFIX EndianToBig_DWORD(uint32_t x);
800 uint16_t DLL_PREFIX EndianToBig_WORD(uint16_t x);
801 uint32_t DLL_PREFIX EndianFromBig_DWORD(uint32_t x);
802 uint16_t DLL_PREFIX EndianFromBig_WORD(uint16_t x);
803 // max/min
804 #ifndef _MSC_VER
805         #undef max
806         #undef min
807         int DLL_PREFIX max(int a, int b);
808         unsigned int DLL_PREFIX max(int a, unsigned int b);
809         unsigned int DLL_PREFIX max(unsigned int a, int b);
810         unsigned int DLL_PREFIX max(unsigned int a, unsigned int b);
811         int DLL_PREFIX min(int a, int b);
812         int DLL_PREFIX min(unsigned int a, int b);
813         int DLL_PREFIX min(int a, unsigned int b);
814         unsigned int DLL_PREFIX min(unsigned int a, unsigned int b);
815 #endif
816
817 // string
818 #if defined(__GNUC__) || defined(__CYGWIN__) || defined(Q_OS_CYGWIN)
819         #define stricmp(a,b) strcasecmp(a,b)
820         #define strnicmp(a,b,n) strncasecmp(a,b,n)
821 #endif
822
823 #ifndef SUPPORT_TCHAR_TYPE
824         #ifndef _fgetts
825                 #define _fgetts fgets
826         #endif
827         #ifndef _ftprintf
828                 #define _ftprintf printf
829         #endif
830         #ifndef _tfopen
831                 #define _tfopen fopen
832         #endif
833         #ifndef _tcscmp
834                 #define _tcscmp strcmp
835         #endif
836         #ifndef _tcscpy
837                 #define _tcscpy strcpy
838         #endif
839         #ifndef _tcsicmp
840                 #define _tcsicmp stricmp
841         #endif
842         #ifndef _tcslen
843                 #define _tcslen strlen
844         #endif
845         #ifndef _tcscat
846                 #define _tcscat strcat
847         #endif
848         #ifndef _tcsncat
849                 #define _tcsncat strncat
850         #endif
851         #ifndef _tcsncpy
852                 #define _tcsncpy strncpy
853         #endif
854         #ifndef _tcsncicmp
855                 #define _tcsncicmp strnicmp
856         #endif
857         #ifndef _tcschr
858                 #define _tcschr strchr
859         #endif
860         #ifndef _tcsrchr
861                 #define _tcsrchr strrchr
862         #endif
863         #ifndef _tcsstr
864                 #define _tcsstr strstr
865         #endif
866         #ifndef _tcstok
867                 #define _tcstok strtok
868         #endif
869         #ifndef _tstoi
870                 #define _tstoi atoi
871         #endif
872         #ifndef _tcstol
873                 #define _tcstol strtol
874         #endif
875         #ifndef _tcstoul
876                 #define _tcstoul strtoul
877         #endif
878         #ifndef _stprintf
879                 #define _stprintf sprintf
880         #endif
881         #ifndef _vstprintf
882                 #define _vstprintf vsprintf
883         #endif
884         #ifndef _taccess
885                 #define _taccess access
886         #endif
887         #ifndef _tremove
888                 #define _tremove remove
889         #endif
890         #ifndef _trename
891                 #define _trename rename
892         #endif
893         #define __T(x) x
894         #define _T(x) __T(x)
895         #define _TEXT(x) __T(x)
896 #endif
897
898 #ifndef SUPPORT_SECURE_FUNCTIONS
899         #ifndef errno_t
900                 typedef int errno_t;
901         #endif
902 //      errno_t DLL_PREFIX my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);
903         errno_t DLL_PREFIX my_tcscat_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
904         errno_t DLL_PREFIX my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
905         errno_t DLL_PREFIX my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
906         errno_t DLL_PREFIX my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count);
907         errno_t DLL_PREFIX my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count);
908         char * DLL_PREFIX my_strtok_s(char *strToken, const char *strDelimit, char **context);
909         _TCHAR *DLL_PREFIX my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
910         #define my_fprintf_s fprintf
911         #define my_ftprintf_s fprintf
912         int DLL_PREFIX my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
913         int DLL_PREFIX my_swprintf_s(wchar_t *buffer, size_t sizeOfBuffer, const wchar_t *format, ...);
914         int DLL_PREFIX my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
915         int DLL_PREFIX my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr);
916         int DLL_PREFIX my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
917 #else
918 //      #define my_tfopen_s _tfopen_s
919         #define my_tcscat_s _tcscat_s
920         #define my_strcpy_s strcpy_s
921         #define my_tcscpy_s _tcscpy_s
922         #define my_strncpy_s strncpy_s
923         #define my_tcsncpy_s _tcsncpy_s
924         #define my_strtok_s strtok_s
925         #define my_tcstok_s _tcstok_s
926         #define my_fprintf_s fprintf_s
927         #define my_ftprintf_s _ftprintf_s
928         #define my_sprintf_s sprintf_s
929         #define my_swprintf_s swprintf_s
930         #define my_stprintf_s _stprintf_s
931         #define my_vsprintf_s vsprintf_s
932         #define my_vstprintf_s _vstprintf_s
933 #endif
934
935 // memory
936 #ifndef _MSC_VER
937         void *DLL_PREFIX my_memcpy(void *dst, void *src, size_t len);
938 #else
939         #define my_memcpy memcpy
940 #endif
941
942 // hint for SIMD
943 #if defined(__clang__)
944         #define __DECL_VECTORIZED_LOOP   _Pragma("clang loop vectorize(enable) distribute(enable)")
945 #elif defined(__GNUC__)
946         #define __DECL_VECTORIZED_LOOP  _Pragma("GCC ivdep")
947 #else
948         #define __DECL_VECTORIZED_LOOP
949 #endif
950
951 // C99 math functions
952 #ifdef _MSC_VER
953         #define my_isfinite  _finite
954         #define my_log2(v) (log((double)(v)) / log(2.0))
955 #else
956         #include <cmath>
957         #define my_isfinite std::isfinite
958         #define my_log2 log2
959 #endif
960
961 // win32 api
962 #ifndef _WIN32
963         BOOL MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);
964         DWORD MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName);
965         UINT MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName);
966         // used only in winmain and win32 osd class
967 //      #define ZeroMemory(p,s) memset(p,0x00,s)
968 //      #define CopyMemory(t,f,s) memcpy(t,f,s)
969 #else
970         #define MyWritePrivateProfileString WritePrivateProfileString
971         #define MyGetPrivateProfileString GetPrivateProfileString
972         #define MyGetPrivateProfileInt GetPrivateProfileInt
973 #endif
974
975 // rgb color
976 #if !defined(_RGB555) && !defined(_RGB565) && !defined(_RGB888)
977         #define _RGB888
978 #endif
979
980 #if defined(_RGB555) || defined(_RGB565)
981         typedef uint16_t scrntype_t;
982         scrntype_t DLL_PREFIX RGB_COLOR(uint32_t r, uint32_t g, uint32_t b);
983         scrntype_t DLL_PREFIX RGBA_COLOR(uint32_t r, uint32_t g, uint32_t b, uint32_t a);
984         uint8_t DLL_PREFIX R_OF_COLOR(scrntype_t c);
985         uint8_t DLL_PREFIX G_OF_COLOR(scrntype_t c);
986         uint8_t DLL_PREFIX B_OF_COLOR(scrntype_t c);
987         uint8_t DLL_PREFIX A_OF_COLOR(scrntype_t c);
988 #elif defined(_RGB888)
989         typedef uint32_t scrntype_t;
990 #if defined(__LITTLE_ENDIAN__)
991         #define RGB_COLOR(r, g, b)      (((uint32_t)(b) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(r) << 0) | (0xff << 24))
992         #define RGBA_COLOR(r, g, b, a)  (((uint32_t)(b) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(r) << 0) | ((uint32_t)(a) << 24))
993         #define R_OF_COLOR(c)           (((c)      ) & 0xff)
994         #define G_OF_COLOR(c)           (((c) >>  8) & 0xff)
995         #define B_OF_COLOR(c)           (((c) >> 16) & 0xff)
996         #define A_OF_COLOR(c)           (((c) >> 24) & 0xff)
997 #else
998         #define RGB_COLOR(r, g, b)      (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | (0xff << 24))
999         #define RGBA_COLOR(r, g, b, a)  (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | ((uint32_t)(a) << 24))
1000         #define R_OF_COLOR(c)           (((c) >> 16) & 0xff)
1001         #define G_OF_COLOR(c)           (((c) >>  8) & 0xff)
1002         #define B_OF_COLOR(c)           (((c)      ) & 0xff)
1003         #define A_OF_COLOR(c)           (((c) >> 24) & 0xff)
1004 #endif
1005 #endif
1006
1007 // 20181104 K.O:
1008 // Below routines aims to render common routine.
1009
1010 #ifdef _MSC_VER
1011         #define __DECL_ALIGNED(foo) __declspec(align(foo))
1012         #ifndef __builtin_assume_aligned
1013                 #define __builtin_assume_aligned(foo, a) foo
1014         #endif
1015 #elif defined(__GNUC__)
1016         #define __DECL_ALIGNED(foo) __attribute__((aligned(foo)))
1017 #else
1018         // ToDo
1019         #define __builtin_assume_aligned(foo, a) foo
1020         #define __DECL_ALIGNED(foo)
1021 #endif
1022
1023 // ToDo: for MSVC
1024 #if defined(_RGB555) || defined(_RGBA565)
1025 typedef __DECL_ALIGNED(16) union {
1026         scrntype_t w[8];
1027         __v8hi v;
1028 } scrntype_vec8_t;
1029 typedef __DECL_ALIGNED(16) union {
1030         scrntype_t w[16];
1031         __v8hi v[2];
1032 } scrntype_vec16_t;
1033 #else
1034 typedef __DECL_ALIGNED(32) union {
1035         scrntype_t w[8];
1036         __v16hi v;
1037 } scrntype_vec8_t;
1038 typedef __DECL_ALIGNED(32) union {
1039         scrntype_t w[16];
1040         __v16hi v[2];
1041 } scrntype_vec16_t;
1042 #endif
1043
1044 typedef __DECL_ALIGNED(16) union {
1045         __v4hi v;
1046         uint8_t w[8];
1047 } uint8_vec8_t;
1048
1049 typedef __DECL_ALIGNED(16) union {
1050         __v8hi v;
1051         uint16_t w[8];
1052 } uint16_vec8_t;
1053
1054 typedef __DECL_ALIGNED(16) union {
1055         __v16hi v;
1056         uint32_t w[8];
1057 } uint32_vec8_t;
1058
1059 typedef __DECL_ALIGNED(16) struct {
1060         uint16_vec8_t plane_table[256];
1061 } _bit_trans_table_t;
1062
1063 typedef __DECL_ALIGNED(sizeof(scrntype_vec8_t)) struct {
1064         scrntype_vec8_t plane_table[256];
1065 } _bit_trans_table_scrn_t;
1066
1067 typedef struct {
1068         scrntype_t* palette; // Must be 2^planes entries. If NULL, assume RGB.
1069         _bit_trans_table_t* bit_trans_table[16]; // Must be exist >= planes. Must be aligned with sizeof(uint16_vec8_t).
1070         int xzoom; // 1 - 4?
1071         bool is_render[16];
1072         int shift;
1073         uint8_t* data[16];
1074         uint32_t baseaddress[16];
1075         uint32_t voffset[16];
1076         uint32_t addrmask;  // For global increment.
1077         uint32_t addrmask2; // For local increment.
1078         uint32_t begin_pos;
1079         uint32_t render_width;
1080 } _render_command_data_t;
1081
1082
1083 inline scrntype_vec8_t ConvertByteToMonochromePackedPixel(uint8_t src, _bit_trans_table_t *tbl,scrntype_t on_val, scrntype_t off_val)
1084 {
1085         __DECL_ALIGNED(16) uint16_vec8_t   tmpd;
1086         __DECL_ALIGNED(32) scrntype_vec8_t tmpdd;
1087         _bit_trans_table_t*  vt = (_bit_trans_table_t*)__builtin_assume_aligned(tbl, sizeof(uint16_vec8_t));
1088
1089         tmpd.v = vt->plane_table[src].v;
1090 __DECL_VECTORIZED_LOOP
1091         for(int i = 0; i < 8; i++) {
1092                 tmpdd.w[i] = (tmpd.w[i] == 0) ? off_val: on_val;
1093         }
1094         return tmpdd;
1095 }
1096
1097 // Note: Pls. read Note(s) of common.cpp -- 20181105 K.Ohta.
1098 // Tables for below functions must be aligned by 16 (_bit_trans_table_t) or 32(_bit_trans_table_scrn_t).  
1099 void DLL_PREFIX ConvertByteToPackedPixelByColorTable(uint8_t *src, scrntype_t* dst, int bytes, _bit_trans_table_t *tbl, scrntype_t *on_color_table, scrntype_t* off_color_table);
1100 void DLL_PREFIX ConvertByteToPackedPixelByColorTable2(uint8_t *src, scrntype_t* dst, int bytes, _bit_trans_table_scrn_t *tbl, scrntype_t *on_color_table, scrntype_t* off_color_table);
1101 void DLL_PREFIX ConvertByteToSparceUint16(uint8_t *src, uint16_t* dst, int bytes, _bit_trans_table_t *tbl, uint16_t mask);
1102 void DLL_PREFIX ConvertByteToSparceUint8(uint8_t *src, uint16_t* dst, int bytes, _bit_trans_table_t *tbl, uint16_t mask);
1103
1104 // Table must be (ON_VAL_COLOR : OFF_VAL_COLOR)[256].
1105 inline scrntype_vec8_t ConvertByteToPackedPixel_PixelTbl(uint8_t src, _bit_trans_table_scrn_t *tbl)
1106 {
1107         __DECL_ALIGNED(32) scrntype_vec8_t tmpdd;
1108         _bit_trans_table_scrn_t*  vt = (_bit_trans_table_scrn_t*)__builtin_assume_aligned(tbl, sizeof(uint16_vec8_t));
1109
1110         tmpdd.v = vt->plane_table[src].v;
1111         return tmpdd;
1112 }
1113
1114 // Table must be (ON_VAL_COLOR : OFF_VAL_COLOR)[256].
1115 inline scrntype_vec16_t ConvertByteToDoublePackedPixel_PixelTbl(uint8_t src, _bit_trans_table_scrn_t *tbl)
1116 {
1117         __DECL_ALIGNED(32) scrntype_vec16_t tmpdd;
1118         __DECL_ALIGNED(32) scrntype_vec8_t tmpd;
1119         _bit_trans_table_scrn_t*  vt = (_bit_trans_table_scrn_t*)__builtin_assume_aligned(tbl, sizeof(uint16_vec8_t));
1120         tmpd.v = vt->plane_table[src].v;
1121         int j = 0;
1122 __DECL_VECTORIZED_LOOP
1123         for(int i = 0; i < 16; i += 2) {
1124                 tmpdd.w[i]     = tmpd.w[j];
1125                 tmpdd.w[i + 1] = tmpd.w[j];
1126                 j++;
1127         }
1128         return tmpdd;
1129 }
1130
1131 // Table must be initialize ON_COLOR : OFF_COLOR
1132 inline void ConvertByteToDoubleMonochromeUint8(uint8_t src, uint8_t* dst, _bit_trans_table_t* tbl)
1133 {
1134         __DECL_ALIGNED(16) uint16_vec8_t   tmpd;
1135         uint16_vec8_t*  vt = (uint16_vec8_t*)__builtin_assume_aligned(&(tbl->plane_table[0]), sizeof(uint16_vec8_t));
1136
1137         __DECL_ALIGNED(16) uint8_t d[16];
1138         tmpd = vt[src];
1139         int j = 0;
1140 __DECL_VECTORIZED_LOOP
1141         for(int i = 0; i < 16; i += 2) {
1142                 d[i]     = (uint8_t)(tmpd.w[j]);
1143                 d[i + 1] = (uint8_t)(tmpd.w[j]);
1144                 j++;
1145         }
1146 __DECL_VECTORIZED_LOOP
1147         for(int i = 0; i < 16; i++) {
1148                 dst[i] = d[i];
1149         }
1150 }
1151
1152 inline void ConvertByteToMonochromeUint8(uint8_t src, uint8_t* dst, _bit_trans_table_t* tbl)
1153 {
1154         __DECL_ALIGNED(16) uint16_vec8_t   tmpd;
1155         uint16_vec8_t*  vt = (uint16_vec8_t*)__builtin_assume_aligned(&(tbl->plane_table[0]), sizeof(uint16_vec8_t));
1156
1157         tmpd = vt[src];
1158 __DECL_VECTORIZED_LOOP
1159         for(int i = 0; i < 8; i++) {
1160                 dst[i] = (uint8_t)(tmpd.w[i]);
1161         }
1162 }
1163
1164 inline void ConvertRGBTo8ColorsUint8(uint8_t r, uint8_t g, uint8_t b, uint8_t* dst, _bit_trans_table_t* rtbl, _bit_trans_table_t* gtbl, _bit_trans_table_t* btbl, int shift)
1165 {
1166         __DECL_ALIGNED(16) uint16_vec8_t   tmpd;
1167         uint16_vec8_t*  rvt = (uint16_vec8_t*)__builtin_assume_aligned(&(rtbl->plane_table[0]), sizeof(uint16_vec8_t));
1168         uint16_vec8_t*  gvt = (uint16_vec8_t*)__builtin_assume_aligned(&(gtbl->plane_table[0]), sizeof(uint16_vec8_t));
1169         uint16_vec8_t*  bvt = (uint16_vec8_t*)__builtin_assume_aligned(&(btbl->plane_table[0]), sizeof(uint16_vec8_t));
1170
1171         tmpd.v = rvt[r].v;
1172         tmpd.v = tmpd.v | gvt[g].v;
1173         tmpd.v = tmpd.v | bvt[b].v;
1174         tmpd.v = tmpd.v >> shift;
1175 __DECL_VECTORIZED_LOOP
1176         for(int i = 0; i < 8; i++) {
1177                 dst[i] = (uint8_t)(tmpd.w[i]);
1178         }
1179 }
1180
1181 inline void ConvertRGBTo8ColorsUint8_Zoom2Left(uint8_t r, uint8_t g, uint8_t b, uint8_t* dst, _bit_trans_table_t* rtbl, _bit_trans_table_t* gtbl, _bit_trans_table_t* btbl, int shift)
1182 {
1183         __DECL_ALIGNED(16) uint16_vec8_t   tmpd;
1184         uint16_vec8_t*  rvt = (uint16_vec8_t*)__builtin_assume_aligned(&(rtbl->plane_table[0]), sizeof(uint16_vec8_t));
1185         uint16_vec8_t*  gvt = (uint16_vec8_t*)__builtin_assume_aligned(&(gtbl->plane_table[0]), sizeof(uint16_vec8_t));
1186         uint16_vec8_t*  bvt = (uint16_vec8_t*)__builtin_assume_aligned(&(btbl->plane_table[0]), sizeof(uint16_vec8_t));
1187
1188         tmpd.v = rvt[r].v;
1189         tmpd.v = tmpd.v | gvt[g].v;
1190         tmpd.v = tmpd.v | bvt[b].v;
1191         tmpd.v = tmpd.v >> shift;
1192 __DECL_VECTORIZED_LOOP
1193         for(int i = 0, j = 0; i < 8; i += 2, j++) {
1194                 dst[i]     = (uint8_t)(tmpd.w[j]);
1195                 dst[i + 1] = (uint8_t)(tmpd.w[j]);
1196         }
1197 }
1198
1199 inline void ConvertRGBTo8ColorsUint8_Zoom2Right(uint8_t r, uint8_t g, uint8_t b, uint8_t* dst, _bit_trans_table_t* rtbl, _bit_trans_table_t* gtbl, _bit_trans_table_t* btbl, int shift)
1200 {
1201         __DECL_ALIGNED(16) uint16_vec8_t   tmpd;
1202         uint16_vec8_t*  rvt = (uint16_vec8_t*)__builtin_assume_aligned(&(rtbl->plane_table[0]), sizeof(uint16_vec8_t));
1203         uint16_vec8_t*  gvt = (uint16_vec8_t*)__builtin_assume_aligned(&(gtbl->plane_table[0]), sizeof(uint16_vec8_t));
1204         uint16_vec8_t*  bvt = (uint16_vec8_t*)__builtin_assume_aligned(&(btbl->plane_table[0]), sizeof(uint16_vec8_t));
1205
1206         tmpd.v = rvt[r].v;
1207         tmpd.v = tmpd.v | gvt[g].v;
1208         tmpd.v = tmpd.v | bvt[b].v;
1209         tmpd.v = tmpd.v >> shift;
1210 __DECL_VECTORIZED_LOOP
1211         for(int i = 0, j = 4; i < 8; i += 2, j++) {
1212                 dst[i]     = (uint8_t)(tmpd.w[j]);
1213                 dst[i + 1] = (uint8_t)(tmpd.w[j]);
1214         }
1215 }
1216
1217 inline void ConvertRGBTo8ColorsUint8_Zoom2Double(uint8_t r, uint8_t g, uint8_t b, uint8_t* dst, _bit_trans_table_t* rtbl, _bit_trans_table_t* gtbl, _bit_trans_table_t* btbl, int shift)
1218 {
1219         __DECL_ALIGNED(16) uint16_vec8_t   tmpd;
1220         uint16_vec8_t*  rvt = (uint16_vec8_t*)__builtin_assume_aligned(&(rtbl->plane_table[0]), sizeof(uint16_vec8_t));
1221         uint16_vec8_t*  gvt = (uint16_vec8_t*)__builtin_assume_aligned(&(gtbl->plane_table[0]), sizeof(uint16_vec8_t));
1222         uint16_vec8_t*  bvt = (uint16_vec8_t*)__builtin_assume_aligned(&(btbl->plane_table[0]), sizeof(uint16_vec8_t));
1223
1224         tmpd.v = rvt[r].v;
1225         tmpd.v = tmpd.v | gvt[g].v;
1226         tmpd.v = tmpd.v | bvt[b].v;
1227         tmpd.v = tmpd.v >> shift;
1228 __DECL_VECTORIZED_LOOP
1229         for(int i = 0, j = 0; i < 16; i += 2, j++) {
1230                 dst[i]     = (uint8_t)(tmpd.w[j]);
1231                 dst[i + 1] = (uint8_t)(tmpd.w[j]);
1232         }
1233 }
1234
1235 inline void ConvertByteToMonochromeUint8Cond_Zoom2(uint8_t src, uint8_t* dst, _bit_trans_table_t* tbl, uint8_t on_color, uint8_t off_color)
1236 {
1237         __DECL_ALIGNED(16) uint16_vec8_t   tmpd;
1238         uint16_vec8_t*  vt = (uint16_vec8_t*)__builtin_assume_aligned(&(tbl->plane_table[0]), sizeof(uint16_vec8_t));
1239
1240         __DECL_ALIGNED(16) uint8_t d[16];
1241         tmpd = vt[src];
1242         int j = 0;
1243 __DECL_VECTORIZED_LOOP
1244         for(int i = 0; i < 16; i += 2) {
1245                 d[i]     = (tmpd.w[j] == 0) ? off_color : on_color;
1246                 d[i + 1] = (tmpd.w[j] == 0) ? off_color : on_color;
1247                 j++;
1248         }
1249 __DECL_VECTORIZED_LOOP
1250         for(int i = 0; i < 16; i++) {
1251                 dst[i] = d[i];
1252         }
1253 }
1254
1255 inline void ConvertByteToMonochromeUint8Cond(uint8_t src, uint8_t* dst, _bit_trans_table_t* tbl, uint8_t on_color, uint8_t off_color)
1256 {
1257         __DECL_ALIGNED(16) uint16_vec8_t   tmpd;
1258         uint16_vec8_t*  vt = (uint16_vec8_t*)__builtin_assume_aligned(&(tbl->plane_table[0]), sizeof(uint16_vec8_t));
1259
1260         tmpd = vt[src];
1261 __DECL_VECTORIZED_LOOP
1262         for(int i = 0; i < 8; i++) {
1263                 dst[i]     = (tmpd.w[i] == 0) ? off_color : on_color;
1264         }
1265 }
1266
1267 void DLL_PREFIX PrepareBitTransTableUint16(_bit_trans_table_t *tbl, uint16_t on_val, uint16_t off_val);
1268 void DLL_PREFIX PrepareBitTransTableScrnType(_bit_trans_table_scrn_t *tbl, scrntype_t on_val, scrntype_t off_val);
1269 void DLL_PREFIX PrepareReverseBitTransTableUint16(_bit_trans_table_t *tbl, uint16_t on_val, uint16_t off_val);
1270 void DLL_PREFIX PrepareReverseBitTransTableScrnType(_bit_trans_table_scrn_t *tbl, scrntype_t on_val, scrntype_t off_val);
1271
1272 void DLL_PREFIX Render8Colors_Line(_render_command_data_t *src, scrntype_t *dst, scrntype_t *dst2, bool scan_line);
1273
1274 void DLL_PREFIX Render16Colors_Line(_render_command_data_t *src, scrntype_t *dst, scrntype_t *dst2, bool scan_line);
1275 void DLL_PREFIX Render2NColors_Line(_render_command_data_t *src, scrntype_t *dst, scrntype_t* dst2, bool scan_line, int planes);
1276
1277 void DLL_PREFIX Convert8ColorsToByte_Line(_render_command_data_t *src, uint8_t *dst);
1278 void DLL_PREFIX Convert2NColorsToByte_Line(_render_command_data_t *src, uint8_t *dst, int planes);
1279 void DLL_PREFIX Convert2NColorsToByte_LineZoom2(_render_command_data_t *src, uint8_t *dst, int planes);
1280
1281 inline uint64_t ExchangeEndianU64(uint64_t __in)
1282 {
1283         pair64_t __i, __o;
1284         __i.q = __in;
1285         __o.b.h7  = __i.b.l;
1286         __o.b.h6  = __i.b.h;
1287         __o.b.h5  = __i.b.h2;
1288         __o.b.h4  = __i.b.h3;
1289         __o.b.h3  = __i.b.h4;
1290         __o.b.h2  = __i.b.h5;
1291         __o.b.h   = __i.b.h6;
1292         __o.b.l   = __i.b.h7;
1293         return __o.q;
1294 }
1295
1296 inline int64_t ExchangeEndianS64(uint64_t __in)
1297 {
1298         pair64_t __i, __o;
1299         __i.q = __in;
1300         __o.b.h7  = __i.b.l;
1301         __o.b.h6  = __i.b.h;
1302         __o.b.h5  = __i.b.h2;
1303         __o.b.h4  = __i.b.h3;
1304         __o.b.h3  = __i.b.h4;
1305         __o.b.h2  = __i.b.h5;
1306         __o.b.h   = __i.b.h6;
1307         __o.b.l   = __i.b.h7;
1308         return __o.sq;
1309 }
1310 inline uint32_t ExchangeEndianU32(uint32_t __in)
1311 {
1312         pair32_t __i, __o;
1313         __i.d = __in;
1314         __o.b.h3 = __i.b.l;
1315         __o.b.h2 = __i.b.h;
1316         __o.b.h  = __i.b.h2;
1317         __o.b.l  = __i.b.h3;
1318         return __o.d;
1319 }
1320
1321 inline int32_t ExchangeEndianS32(uint32_t __in)
1322 {
1323         pair32_t __i, __o;
1324         __i.d = __in;
1325         __o.b.h3 = __i.b.l;
1326         __o.b.h2 = __i.b.h;
1327         __o.b.h  = __i.b.h2;
1328         __o.b.l  = __i.b.h3;
1329         return __o.sd;
1330 }
1331
1332 inline uint16_t ExchangeEndianU16(uint16_t __in)
1333 {
1334         pair16_t __i, __o;
1335         __i.u16 = __in;
1336         __o.b.h = __i.b.l;
1337         __o.b.l  = __i.b.h;
1338         return __o.u16;
1339 }
1340
1341 inline int16_t ExchangeEndianS16(uint16_t __in)
1342 {
1343         pair16_t __i, __o;
1344         __i.u16 = __in;
1345         __o.b.h = __i.b.l;
1346         __o.b.l = __i.b.h;
1347         return __o.s16;
1348 }
1349
1350 // wav file header
1351 #pragma pack(1)
1352 typedef struct {
1353         char id[4];
1354         uint32_t size;
1355 } wav_chunk_t;
1356 #pragma pack()
1357
1358 #pragma pack(1)
1359 typedef struct {
1360         wav_chunk_t riff_chunk;
1361         char wave[4];
1362         wav_chunk_t fmt_chunk;
1363         uint16_t format_id;
1364         uint16_t channels;
1365         uint32_t sample_rate;
1366         uint32_t data_speed;
1367         uint16_t block_size;
1368         uint16_t sample_bits;
1369 } wav_header_t;
1370 #pragma pack()
1371
1372 //  See http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html.
1373 #pragma pack(1)
1374 typedef struct {
1375         wav_chunk_t riff_chunk;
1376         char wave[4];
1377         wav_chunk_t fmt_chunk;
1378         uint16_t format_id;
1379         uint16_t channels;
1380         uint32_t sample_rate;
1381         uint32_t data_speed;
1382         uint16_t block_size;
1383         uint16_t sample_bits;
1384         uint16_t cbsize; // Extension size.Normaly set to 0.
1385         wav_chunk_t fact_chunk; // "fact", 4.
1386 } wav_header_float_t;
1387 #pragma pack()
1388
1389 // Use this before writing wav_data.
1390 bool DLL_PREFIX write_dummy_wav_header(void *__fio);
1391 // Use this after writng wav_data.
1392 bool DLL_PREFIX set_wav_header(wav_header_t *header, wav_chunk_t *first_chunk, uint16_t channels, uint32_t rate,
1393                                                            uint16_t bits, size_t file_length);
1394 bool DLL_PREFIX load_wav_to_stereo(void *__fio, int16_t **left_buf, int16_t **right_buf, uint32_t *rate, int *got_samples);
1395 bool DLL_PREFIX load_wav_to_monoral(void *__fio, int16_t **buffer, uint32_t *rate, int *got_samples);
1396
1397 // file path
1398 const _TCHAR *DLL_PREFIX get_application_path();
1399 const _TCHAR *DLL_PREFIX get_initial_current_path();
1400 const _TCHAR *DLL_PREFIX create_local_path(const _TCHAR *format, ...);
1401 void DLL_PREFIX create_local_path(_TCHAR *file_path, int length, const _TCHAR *format, ...);
1402 const _TCHAR *DLL_PREFIX create_date_file_path(const _TCHAR *extension);
1403 bool DLL_PREFIX is_absolute_path(const _TCHAR *file_path);
1404 void DLL_PREFIX create_date_file_path(_TCHAR *file_path, int length, const _TCHAR *extension);
1405 const _TCHAR *DLL_PREFIX create_date_file_name(const _TCHAR *extension);
1406 void DLL_PREFIX create_date_file_name(_TCHAR *file_path, int length, const _TCHAR *extension);
1407 bool DLL_PREFIX check_file_extension(const _TCHAR *file_path, const _TCHAR *ext);
1408 const _TCHAR *DLL_PREFIX get_file_path_without_extensiton(const _TCHAR *file_path);
1409 void DLL_PREFIX get_long_full_path_name(const _TCHAR* src, _TCHAR* dst, size_t dst_len);
1410 const _TCHAR* DLL_PREFIX get_parent_dir(const _TCHAR* file);
1411
1412 // string
1413 const _TCHAR *DLL_PREFIX create_string(const _TCHAR* format, ...);
1414 const wchar_t *DLL_PREFIX char_to_wchar(const char *cs);
1415 const char *DLL_PREFIX wchar_to_char(const wchar_t *ws);
1416 const _TCHAR *DLL_PREFIX char_to_tchar(const char *cs);
1417 const char *DLL_PREFIX tchar_to_char(const _TCHAR *ts);
1418 const _TCHAR *DLL_PREFIX wchar_to_tchar(const wchar_t *ws);
1419 const wchar_t *DLL_PREFIX tchar_to_wchar(const _TCHAR *ts);
1420
1421
1422 // misc
1423 void DLL_PREFIX common_initialize();
1424
1425 int32_t DLL_PREFIX muldiv_s32(int32_t nNumber, int32_t nNumerator, int32_t nDenominator);
1426 uint32_t DLL_PREFIX muldiv_u32(uint32_t nNumber, uint32_t nNumerator, uint32_t nDenominator);
1427
1428 uint32_t DLL_PREFIX get_crc32(uint8_t data[], int size);
1429 uint32_t DLL_PREFIX calc_crc32(uint32_t seed, uint8_t data[], int size);
1430 uint16_t DLL_PREFIX jis_to_sjis(uint16_t jis);
1431
1432 int DLL_PREFIX decibel_to_volume(int decibel);
1433 int32_t DLL_PREFIX apply_volume(int32_t sample, int volume);
1434
1435 #define array_length(array) (sizeof(array) / sizeof(array[0]))
1436
1437 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
1438 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
1439 #define TO_BCD_LO(v)    ((v) % 10)
1440 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)
1441
1442 // time
1443 #define LEAP_YEAR(y)    (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
1444
1445 #define dll_cur_time_t DLL_PREFIX_I struct cur_time_s
1446
1447 typedef DLL_PREFIX struct cur_time_s {
1448         int year, month, day, day_of_week, hour, minute, second;
1449         bool initialized;
1450         cur_time_s()
1451         {
1452                 initialized = false;
1453         }
1454         void increment();
1455         void update_year();
1456         void update_day_of_week();
1457         bool process_state(void *f, bool loading);
1458 } cur_time_t;
1459
1460 void DLL_PREFIX get_host_time(cur_time_t* cur_time);
1461 const _TCHAR DLL_PREFIX *get_lib_common_version();
1462
1463 // symbol
1464 typedef struct symbol_s {
1465         uint32_t addr;
1466         _TCHAR *name;
1467         struct symbol_s *next_symbol;
1468 } symbol_t;
1469
1470 const _TCHAR* DLL_PREFIX get_symbol(symbol_t *first_symbol, uint32_t addr);
1471 const _TCHAR* DLL_PREFIX get_value_or_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);
1472 const _TCHAR* DLL_PREFIX get_value_and_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);
1473
1474 #endif