-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPlatform.hpp
More file actions
831 lines (646 loc) · 23 KB
/
Platform.hpp
File metadata and controls
831 lines (646 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/*
Copyright (c) 2013 Chris Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CAT_PLATFORM_HPP
#define CAT_PLATFORM_HPP
#include "Config.hpp"
namespace cat {
// Used to detect whether or not libcat is included
#define CAT_HAS_TOYS
//// Compiler ////
// Mac OS X additional compilation flags
#if defined(__APPLE__)
# include <TargetConditionals.h>
#endif
#if defined(__MINGW32__) || defined(__MINGW64__)
# define CAT_COMPILER_MINGW
#endif
//-----------------------------------------------------------------------------
// Intel C++ Compiler : Interoperates with MSVC and GCC
#if defined(__INTEL_COMPILER) || defined(__ICL) || defined(__ICC) || defined(__ECC)
# define CAT_COMPILER_ICC
# define CAT_FENCE_COMPILER __memory_barrier();
#endif
//-----------------------------------------------------------------------------
// Borland C++ Compiler : Compatible with MSVC syntax
#if defined(__BORLANDC__)
# define CAT_COMPILER_BORLAND
# define CAT_COMPILER_COMPAT_MSVC
# define CAT_INLINE __inline
# define CAT_ASM_EMIT __emit__
//-----------------------------------------------------------------------------
// Digital Mars C++ Compiler (previously known as Symantec C++)
#elif defined(__DMC__) || defined(__SC__) || defined(__SYMANTECC__)
# define CAT_COMPILER_DMARS
# define CAT_COMPILER_COMPAT_MSVC
# define CAT_INLINE __inline
# define CAT_ASM_EMIT __emit__
//-----------------------------------------------------------------------------
// Codeplay VectorC C++ Compiler : Compatible with GCC and MSVC syntax, prefer GCC
#elif defined(__VECTORC__)
# define CAT_COMPILER_CODEPLAY
# define CAT_COMPILER_COMPAT_GCC
//-----------------------------------------------------------------------------
// Pathscale C++ Compiler : Compatible with GCC syntax
#elif defined(__PATHSCALE__)
# define CAT_COMPILER_PATHSCALE
# define CAT_COMPILER_COMPAT_GCC
//-----------------------------------------------------------------------------
// Watcom C++ Compiler : Compatible with GCC and MSVC syntax, prefer GCC
#elif defined(__WATCOMC__)
# define CAT_COMPILER_WATCOM
# define CAT_COMPILER_COMPAT_GCC
//-----------------------------------------------------------------------------
// SUN C++ Compiler : Compatible with GCC syntax
#elif defined(__SUNPRO_CC)
# define CAT_COMPILER_SUN
# define CAT_COMPILER_COMPAT_GCC
//-----------------------------------------------------------------------------
// Metrowerks C++ Compiler : Compatible with MSVC syntax
#elif defined(__MWERKS__)
# define CAT_COMPILER_MWERKS
# define CAT_COMPILER_COMPAT_MSVC
# define CAT_INLINE inline
# define CAT_ASM_BEGIN _asm {
# define CAT_ASM_BEGIN_VOLATILE _asm {
# define CAT_ASM_EMIT __emit__
//-----------------------------------------------------------------------------
// GNU C++ Compiler
// SN Systems ProDG C++ Compiler : Compatible with GCC
#elif defined(__GNUC__) || defined(__APPLE_CC__) || defined(__SNC__) || defined(__clang__)
# define CAT_COMPILER_GCC
# define CAT_COMPILER_COMPAT_GCC
# define CAT_FASTCALL __attribute__ ((fastcall))
//-----------------------------------------------------------------------------
// Microsoft Visual Studio C++ Compiler
#elif defined(_MSC_VER)
# define CAT_COMPILER_MSVC
# define CAT_COMPILER_COMPAT_MSVC
# define CAT_FASTCALL __fastcall
# if _MSC_VER >= 1300
# define CAT_FUNCTION __FUNCTION__
# endif
} // namespace cat
// Intrinsics:
# include <cstdlib>
# include <intrin.h>
# include <mmintrin.h>
namespace cat {
//-----------------------------------------------------------------------------
// Otherwise unknown compiler
#else
# define CAT_COMPILER_UNKNOWN
# define CAT_ALIGNED(n) /* no way to detect alignment syntax */
# define CAT_PACKED /* no way to detect packing syntax */
# define CAT_INLINE inline
// No way to support inline assembly code here
# define CAT_RESTRICT
#endif
/*
A lot of compilers have similar syntax to MSVC or GCC,
so for simplicity I have those two defined below, and
any deviations are implemented with overrides above.
*/
// MSVC-compatible compilers
#if defined(CAT_COMPILER_COMPAT_MSVC)
#if !defined(CAT_ALIGNED)
# define CAT_ALIGNED(n) __declspec(align(n))
#endif
#if !defined(CAT_PACKED)
# define CAT_PACKED
# define CAT_PRAGMA_PACK
#endif
#if !defined(CAT_INLINE)
# define CAT_INLINE __forceinline
#endif
#if !defined(CAT_ASM_INTEL)
# define CAT_ASM_INTEL
#endif
#if !defined(CAT_ASM_BEGIN)
# define CAT_ASM_BEGIN __asm {
# define CAT_ASM_BEGIN_VOLATILE __asm {
#endif
#if !defined(CAT_ASM_EMIT)
# define CAT_ASM_EMIT _emit
#endif
#if !defined(CAT_ASM_END)
# define CAT_ASM_END }
#endif
#if !defined(CAT_TLS)
# define CAT_TLS __declspec( thread )
#endif
#if defined(CAT_COMPILER_MSVC)
# pragma warning(disable: 4227) // Squelch annoying warning from MSVC when using __restrict
#endif
#if !defined(CAT_RESTRICT)
# define CAT_RESTRICT __restrict
#endif
#if !defined(CAT_FENCE_COMPILER)
# if defined(CAT_COMPILER_MSVC)
# pragma intrinsic(_ReadWriteBarrier)
# endif
# define CAT_FENCE_COMPILER _ReadWriteBarrier();
#endif
#if !defined(CAT_DLL_EXPORT)
# define CAT_DLL_EXPORT __declspec(dllexport)
#endif
#if !defined(CAT_DLL_IMPORT)
# define CAT_DLL_IMPORT __declspec(dllimport)
#endif
#if !defined(CAT_FUNCTION)
# define CAT_FUNCTION "???"
#endif
#if !defined(CAT_LIKELY)
# define CAT_LIKELY(expr) ( (expr) != 0 )
#endif
#if !defined(CAT_UNLIKELY)
# define CAT_UNLIKELY(expr) ( (expr) != 0 )
#endif
// GCC-compatible compilers
#elif defined(CAT_COMPILER_COMPAT_GCC)
#if !defined(CAT_ALIGNED)
# define CAT_ALIGNED(n) __attribute__ ((aligned (n)))
#endif
#if !defined(CAT_PACKED)
# define CAT_PACKED __attribute__ ((packed))
#endif
#if !defined(CAT_INLINE)
# define CAT_INLINE inline /* __inline__ __attribute__((always_inline)) */
#endif
#if !defined(CAT_ASM_ATT)
# define CAT_ASM_ATT
#endif
#if !defined(CAT_ASM_BEGIN)
# define CAT_ASM_BEGIN __asm__ (
# define CAT_ASM_BEGIN_VOLATILE __asm__ __volatile__ (
#endif
#if !defined(CAT_ASM_EMIT)
# define CAT_ASM_EMIT .byte
#endif
#if !defined(CAT_ASM_END)
# define CAT_ASM_END );
#endif
#if !defined(CAT_TLS)
# define CAT_TLS __thread
#endif
#if !defined(CAT_RESTRICT)
# define CAT_RESTRICT __restrict__
#endif
#if !defined(CAT_FENCE_COMPILER)
# define CAT_FENCE_COMPILER CAT_ASM_BEGIN "" ::: "memory" CAT_ASM_END
#endif
#if !defined(CAT_DLL_EXPORT)
# define CAT_DLL_EXPORT __attribute__((dllexport))
#endif
#if !defined(CAT_DLL_IMPORT)
# define CAT_DLL_IMPORT __attribute__((dllimport))
#endif
#if !defined(CAT_FUNCTION)
# define CAT_FUNCTION "???"
#endif
#if !defined(CAT_LIKELY)
# define CAT_LIKELY(expr) ( __builtin_expect (( (expr) != 0 ), 1) )
#endif
#if !defined(CAT_UNLIKELY)
# define CAT_UNLIKELY(expr) ( __builtin_expect (( (expr) != 0 ), 0) )
#endif
#endif // CAT_COMPILER_COMPAT_*
//// Debug Flag ////
# if defined(_DEBUG) || defined(DEBUG)
# define CAT_DEBUG
# endif
//// Instruction Set Architecture ////
#if defined(__powerpc__) || defined(__ppc__) || defined(_M_PPC) || \
defined(_M_MPPC) || defined(__POWERPC) || defined(powerpc) || defined(__ppc64__) || \
defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3) || defined(__POWERPC__)
# define CAT_ISA_PPC
#elif defined(_POWER)
# define CAT_ISA_PPC
# define CAT_ISA_IBM_POWER /* Define IBM Power in addition to PowerPC */
#elif defined(__i386__) || defined(i386) || defined(intel) || defined(_M_IX86) || \
defined(__ia64) || defined(__ia64__) || defined(__x86_64) || defined(_M_IA64) || \
defined(_M_X64) || defined(_M_I86) || defined(sun386) || defined(__OS2__)
# define CAT_ISA_X86
#elif defined(TARGET_CPU_ARM) || defined(__ARMEL__) || defined(__ARM__) || \
defined(__arm__)
# define CAT_ISA_ARM
#ifdef __ANDROID__
#if (__ARM_ARCH >= 6)
# define CAT_ISA_ARMV6
#endif
#if (__ARM_ARCH >= 7)
# define CAT_ISA_ARMV7
#endif
#else // Assumes iOS
#include <arm/arch.h>
#ifdef _ARM_ARCH_8
# define CAT_ISA_ARMV8
# define CAT_ISA_ARMV7
# define CAT_ISA_ARMV6
#endif
#ifdef _ARM_ARCH_7
# define CAT_ISA_ARMV7
# define CAT_ISA_ARMV6
#endif
#ifdef _ARM_ARCH_6
# define CAT_ISA_ARMV6
#endif
#endif
#elif defined(__mips__)
# define CAT_ISA_MIPS
#elif defined(__alpha__) || defined(__alpha)
# define CAT_ISA_ALPHA
#elif defined(vax) || defined(vms) || defined(VMS) || defined(__VMS)
# define CAT_ISA_VMS
#elif defined(AVR)
# define CAT_ISA_ATMEGA
#elif defined(AMIGA)
# define CAT_ISA_AMIGA
#else
# define CAT_ISA_UNKNOWN
#endif
//// Memory Access Alignment ////
#if !defined(CAT_ISA_PPC) && !defined(CAT_ISA_X86)
# define CAT_NEED_ALIGNED_READS
#endif
//// Endianness ////
// If no override for endianness from Config.hpp,
#if !defined(CAT_ENDIAN_BIG) && !defined(CAT_ENDIAN_LITTLE)
#if defined(BIG_ENDIAN) && defined(LITTLE_ENDIAN)
# if defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN
# define CAT_ENDIAN_BIG
# elif defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN
# define CAT_ENDIAN_LITTLE
# endif
#elif defined(BIG_ENDIAN)
# define CAT_ENDIAN_BIG
#elif defined(LITTLE_ENDIAN)
# define CAT_ENDIAN_LITTLE
#endif
#if defined(_BIG_ENDIAN) && defined(_LITTLE_ENDIAN)
# if defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN
# define CAT_ENDIAN_BIG
# elif defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN
# define CAT_ENDIAN_LITTLE
# endif
#elif defined(_BIG_ENDIAN)
# define CAT_ENDIAN_BIG
#elif defined(_LITTLE_ENDIAN)
# define CAT_ENDIAN_LITTLE
#endif
#if defined(__BIG_ENDIAN) && defined(__LITTLE_ENDIAN)
# if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN
# define CAT_ENDIAN_BIG
# elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN
# define CAT_ENDIAN_LITTLE
# endif
#elif defined(__BIG_ENDIAN)
# define CAT_ENDIAN_BIG
#elif defined(__LITTLE_ENDIAN)
# define CAT_ENDIAN_LITTLE
#endif
#if defined(__BIG_ENDIAN__) && defined(__LITTLE_ENDIAN__)
# if defined(__BYTE_ORDER__) && __BYTE_ORDER__== __BIG_ENDIAN__
# define CAT_ENDIAN_BIG
# elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __LITTLE_ENDIAN__
# define CAT_ENDIAN_LITTLE
# endif
#elif defined(__BIG_ENDIAN__)
# define CAT_ENDIAN_BIG
#elif defined(__LITTLE_ENDIAN__)
# define CAT_ENDIAN_LITTLE
#endif
// If no overrides defined, try to base it on ISA
// Okay -- Technically IA64 and PPC can switch endianness with an MSR bit
// But come on no one does that! ...Right?
// If it's not right, make sure that one of the override flags in Config.hpp are defined.
#if !defined(CAT_ENDIAN_BIG) && !defined(CAT_ENDIAN_LITTLE)
# if defined(CAT_ISA_X86) || defined(CAT_ISA_VMS) || defined(CAT_ISA_ATMEGA)
# define CAT_ENDIAN_LITTLE
# elif defined(CAT_ISA_PPC) || defined(CAT_ISA_AMIGA)
# define CAT_ENDIAN_BIG
// Here are some other ISA that are not explicitly defined above:
# elif defined(applec) || defined(__AS400__) || defined(_CRAY) || defined(__hppa) || \
defined(__hp9000) || defined(ibm370) || defined(mc68000) || defined(m68k) || \
defined(__MRC__) || defined(__MVS__) || defined(sparc) || defined(__sparc) || \
defined(__VOS__) || defined(__TANDEM) || defined(__VMCMS__)
# define CAT_ENDIAN_BIG
# else
# define CAT_ENDIAN_UNKNOWN /* Must be detected at runtime */
# endif
#endif
#endif // #if !defined(CAT_ENDIAN_BIG) && !defined(CAT_ENDIAN_LITTLE)
//// Word Size ////
#if defined(_LP64) || defined(__LP64__) || defined(__arch64__) || \
defined(_WIN64) || defined(_M_X64) || defined(__ia64) || \
defined(__ia64__) || defined(__x86_64) || defined(_M_IA64) || \
defined(__mips64) || defined(__amd64__)
# define CAT_WORD_64
// 64-bit MSVC does not support inline assembly
# if defined(CAT_COMPILER_MSVC)
# undef CAT_ASM_INTEL
# endif
#else // Assuming 32-bit otherwise!
# define CAT_WORD_32
#endif
// __fastcall calling convention is rarely supported, and doesn't make sense for 64-bit targets
#if !defined(CAT_FASTCALL)
# define CAT_FASTCALL
#elif !defined(CAT_ISA_X86) || defined(CAT_WORD_64)
# undef CAT_FASTCALL
# define CAT_FASTCALL
#endif
//// Operating System ////
#if defined(__SVR4) && defined(__sun)
# define CAT_OS_SOLARIS
#elif defined(__APPLE__) && (defined(__MACH__) || defined(__DARWIN__))
# define CAT_OS_OSX
# define CAT_OS_APPLE
#elif defined(__APPLE__) && defined(TARGET_OS_IPHONE)
# define CAT_OS_IPHONE
# define CAT_OS_APPLE
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
# define CAT_OS_BSD
#elif defined(__linux__) || defined(__unix__) || defined(__unix)
# define CAT_OS_LINUX
#ifdef __ANDROID__
# define CAT_OS_ANDROID
#endif
#elif defined(_WIN32_WCE)
# define CAT_OS_WINDOWS_CE
# define CAT_OS_WINDOWS /* Also defined */
#elif defined(_WIN32)
# define CAT_OS_WINDOWS
#elif defined(_XBOX) || defined(_X360)
# define CAT_OS_XBOX
#elif defined(_PS3) || defined(__PS3__) || defined(SN_TARGET_PS3)
# define CAT_OS_PS3
#elif defined(_PS4) || defined(__PS4__) || defined(SN_TARGET_PS4)
# define CAT_OS_PS4
#elif defined(__OS2__)
# define CAT_OS_OS2
#elif defined(__APPLE__)
# define CAT_OS_APPLE
#elif defined(_aix) || defined(aix)
# define CAT_OS_AIX
#elif defined(HPUX)
# define CAT_OS_HPUX
#elif defined(IRIX)
# define CAT_OS_IRIX
#else
# define CAT_OS_UNKNOWN
#endif
// Detect CYGWIN environment
#if defined(__CYGWIN__) || defined(__CYGWIN32__)
# define CAT_CYGWIN
#endif
//// Dynamic Linkage ////
#if defined(CAT_OS_WINDOWS) || defined(CAT_CYGWIN)
# if defined(CAT_NEUTER_EXPORT)
# define CAT_EXPORT /* Do not import or export any symbols */
# elif defined(CAT_BUILD_DLL)
# define CAT_EXPORT CAT_DLL_EXPORT /* Implementing a DLL so export this symbol */
# else
# define CAT_EXPORT CAT_DLL_IMPORT /* Using a DLL so import this symbol, faster on Windows */
# endif
#else
# undef CAT_DLL_EXPORT
# undef CAT_DLL_IMPORT
# define CAT_DLL_EXPORT
# define CAT_DLL_IMPORT
# define CAT_EXPORT
#endif
// Extern tag for dynamic linkage:
#if defined(CAT_NEUTER_EXPORT)
# define CAT_EXPORT_EXTERN
#elif defined(CAT_BUILD_DLL)
# define CAT_EXPORT_EXTERN
#else
# define CAT_EXPORT_EXTERN extern
#endif
// Export statically-linked templates: CAT_EXPORT_TEMPLATE(cat::Dictionary<int, int>);
#define CAT_EXPORT_TEMPLATE(T) CAT_EXPORT_EXTERN template class CAT_EXPORT T;
//// Basic types ////
#if defined(CAT_COMPILER_MSVC)
// MSVC does not ship with stdint.h (C99 standard...)
typedef unsigned __int8 u8;
typedef signed __int8 s8;
typedef unsigned __int16 u16;
typedef signed __int16 s16;
typedef unsigned __int32 u32;
typedef signed __int32 s32;
typedef unsigned __int64 u64;
typedef signed __int64 s64;
#else
} // namespace cat
#include <stdint.h>
namespace cat {
// All other compilers use this
typedef uint8_t u8;
typedef int8_t s8;
typedef uint16_t u16;
typedef int16_t s16;
typedef uint32_t u32;
typedef int32_t s32;
typedef uint64_t u64;
typedef int64_t s64;
#endif
//// 128-bit types ////
#if defined(CAT_COMPILER_GCC) && defined(CAT_WORD_64)
# define CAT_HAS_U128
// GCC also adds 128-bit types :D
typedef __uint128_t u128;
typedef __int128_t s128;
#endif
//// Floating-point types ////
typedef float f32;
typedef double f64;
union Float32 {
float f;
u32 i;
Float32(float n) { f = n; }
Float32(u32 n) { i = n; }
};
//// String and buffer macros ////
} // namespace cat
#include <string.h>
namespace cat {
#if defined(CAT_OS_WINDOWS)
# define CAT_NEWLINE "\r\n"
#else
# define CAT_NEWLINE "\n"
#endif
#define CAT_FOREVER for (;;)
// Same as strncpy() in all ways except that the result is guaranteed to
// be a nul-terminated C string
#if defined(CAT_COMPILER_MSVC)
# define CAT_STRNCPY(dest, src, size) { strncpy_s(dest, size, src, size); (dest)[(size)-1] = '\0'; }
#else
# define CAT_STRNCPY(dest, src, size) { strncpy(dest, src, size); (dest)[(size)-1] = '\0'; }
#endif
// Because memory clearing is a frequent operation
#define CAT_CLR(dest, size) memset(dest, 0, size)
// Works for arrays, also
#define CAT_OBJCLR(obj) CAT_CLR((void*)&(obj), sizeof(obj))
// Stringize
#define CAT_STRINGIZE(X) DO_CAT_STRINGIZE(X)
#define DO_CAT_STRINGIZE(X) #X
// Fine and line string macro
#define CAT_FILE_LINE_STRING __FILE__ ":" CAT_STRINGIZE(__LINE__)
// Variable-length data trailing a struct
template<typename T> CAT_INLINE u8 *GetTrailingBytes(T *t) { return reinterpret_cast<u8*>( t ) + sizeof(T); }
// Bounds
template<typename T> CAT_INLINE T BoundMin(const T &minimum, const T &x)
{
if (x < minimum) return minimum;
return x;
}
template<typename T> CAT_INLINE T BoundMax(const T &maximum, const T &x)
{
if (x > maximum) return maximum;
return x;
}
template<typename T> CAT_INLINE T Bound(const T &minimum, const T &maximum, const T &x)
{
if (x < minimum) return minimum;
if (x > maximum) return maximum;
return x;
}
// Absolute Value
template<typename T> CAT_INLINE T AbsVal(const T &x)
{
return x < 0 ? -x : x;
}
//// Miscellaneous bitwise macros ////
#define CAT_BITCLRHI8(reg, count) ((u8)((u8)(reg) << (count)) >> (count)) /* sets to zero a number of high bits in a byte */
#define CAT_BITCLRLO8(reg, count) ((u8)((u8)(reg) >> (count)) << (count)) /* sets to zero a number of low bits in a byte */
#define CAT_BITCLRHI16(reg, count) ((u16)((u16)(reg) << (count)) >> (count)) /* sets to zero a number of high bits in a 16-bit word */
#define CAT_BITCLRLO16(reg, count) ((u16)((u16)(reg) >> (count)) << (count)) /* sets to zero a number of low bits in a 16-bit word */
#define CAT_BITCLRHI32(reg, count) ((u32)((u32)(reg) << (count)) >> (count)) /* sets to zero a number of high bits in a 32-bit word */
#define CAT_BITCLRLO32(reg, count) ((u32)((u32)(reg) >> (count)) << (count)) /* sets to zero a number of low bits in a 32-bit word */
//// Integer macros ////
#define CAT_AT_LEAST_2_BITS(n) ( (n) & ((n) - 1) )
#define CAT_LSB32(n) ( (n) & (u32)(-(s32)(n)) ) /* 0 -> 0 */
#define CAT_LSB64(n) ( (n) & (u64)(-(s64)(n)) ) /* 0 -> 0 */
#define CAT_IS_POWER_OF_2(n) ( n && !CAT_AT_LEAST_2_BITS(n) )
// Safely take the average of two numbers without possibility of overflow
#define CAT_SAFE_AVERAGE(A, B) (((A) & (B)) + (((A) ^ (B)) >> 1))
// Bump 'n' to the next unit of 'width'
// 0=CAT_CEIL_UNIT(0, 16), 1=CAT_CEIL_UNIT(1, 16), 1=CAT_CEIL_UNIT(16, 16), 2=CAT_CEIL_UNIT(17, 16)
#define CAT_CEIL_UNIT(n, width) ( ( (n) + (width) - 1 ) / (width) )
// 0=CAT_CEIL(0, 16), 16=CAT_CEIL(1, 16), 16=CAT_CEIL(16, 16), 32=CAT_CEIL(17, 16)
#define CAT_CEIL(n, width) ( CAT_CEIL_UNIT(n, width) * (width) )
//// Rotation macros ////
#define CAT_ROL8(n, r) ( (u8)((u8)(n) << (r)) | (u8)((u8)(n) >> ( 8 - (r))) ) /* only works for u8 */
#define CAT_ROR8(n, r) ( (u8)((u8)(n) >> (r)) | (u8)((u8)(n) << ( 8 - (r))) ) /* only works for u8 */
#define CAT_ROL16(n, r) ( (u16)((u16)(n) << (r)) | (u16)((u16)(n) >> (16 - (r))) ) /* only works for u16 */
#define CAT_ROR16(n, r) ( (u16)((u16)(n) >> (r)) | (u16)((u16)(n) << (16 - (r))) ) /* only works for u16 */
#define CAT_ROL32(n, r) ( (u32)((u32)(n) << (r)) | (u32)((u32)(n) >> (32 - (r))) ) /* only works for u32 */
#define CAT_ROR32(n, r) ( (u32)((u32)(n) >> (r)) | (u32)((u32)(n) << (32 - (r))) ) /* only works for u32 */
#define CAT_ROL64(n, r) ( (u64)((u64)(n) << (r)) | (u64)((u64)(n) >> (64 - (r))) ) /* only works for u64 */
#define CAT_ROR64(n, r) ( (u64)((u64)(n) >> (r)) | (u64)((u64)(n) << (64 - (r))) ) /* only works for u64 */
//// Byte-order swapping ////
#define CAT_BOSWAP16(n) CAT_ROL16(n, 8)
#define CAT_BOSWAP32(n) ( (CAT_ROL32(n, 8) & 0x00ff00ff) | (CAT_ROL32(n, 24) & 0xff00ff00) )
#define CAT_BOSWAP64(n) ( ((u64)CAT_BOSWAP32((u32)n) << 32) | CAT_BOSWAP32((u32)(n >> 32)) )
//// Intrinsics ////
#if defined(CAT_OS_WINDOWS_CE)
#pragma intrinsic(_lrotl)
#pragma intrinsic(_lrotr)
#undef CAT_ROL32
#undef CAT_ROR32
#define CAT_ROL32(n, r) _lrotl(n, r)
#define CAT_ROR32(n, r) _lrotr(n, r)
#elif defined(CAT_COMPILER_MSVC)
#pragma intrinsic(_rotl, _rotr, _rotl64, _rotr64)
#pragma intrinsic(_byteswap_ushort, _byteswap_ulong, _byteswap_uint64)
#pragma intrinsic(_BitScanForward, _BitScanReverse, _bittestandset)
#pragma intrinsic(__emulu)
#pragma intrinsic(_InterlockedExchange)
#pragma intrinsic(_interlockedbittestandset)
#pragma intrinsic(_interlockedbittestandreset)
#pragma intrinsic(_mm_sfence, _mm_lfence, _mm_mfence)
#if defined(CAT_WORD_64)
#pragma intrinsic(__rdtsc)
#pragma intrinsic(_umul128)
#pragma intrinsic(__shiftleft128, __shiftright128)
#pragma intrinsic(_BitScanForward64, _BitScanReverse64, _bittestandset64)
#pragma intrinsic(_InterlockedCompareExchange128)
#else
#pragma intrinsic(_InterlockedCompareExchange64)
#endif
#undef CAT_ROL32
#undef CAT_ROR32
#undef CAT_ROL64
#undef CAT_ROR64
#undef CAT_BOSWAP16
#undef CAT_BOSWAP32
#undef CAT_BOSWAP64
#define CAT_ROL32(n, r) _rotl(n, r)
#define CAT_ROR32(n, r) _rotr(n, r)
#define CAT_ROL64(n, r) _rotl64(n, r)
#define CAT_ROR64(n, r) _rotr64(n, r)
#define CAT_BOSWAP16(n) _byteswap_ushort(n)
#define CAT_BOSWAP32(n) _byteswap_ulong(n)
#define CAT_BOSWAP64(n) _byteswap_uint64(n)
#endif
//// No Copy ////
#define CAT_NO_COPY(T) \
private: \
CAT_INLINE T(const T&) {} \
CAT_INLINE T& operator=(const T&) { return *this; }
} // namespace cat
//// Memory Leaks ////
#define CAT_DEBUG_MEM_FLAGS()
#define CAT_DEBUG_LEAKS_DUMP()
#define CAT_DEBUG_CHECK_MEMORY()
#if defined(CAT_DEBUG)
#if defined(CAT_DEBUG_LEAKS)
# if defined(CAT_COMPILER_MSVC)
# define _CRTDBG_MAP_ALLOC
# include <stdlib.h>
# include <crtdbg.h>
# undef CAT_DEBUG_MEM_FLAGS
# undef CAT_DEBUG_LEAKS_DUMP
# undef CAT_DEBUG_CHECK_MEMORY
# define CAT_DEBUG_MEM_FLAGS() _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_EVERY_128_DF | _CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
# define CAT_DEBUG_LEAKS_DUMP() _CrtDumpMemoryLeaks();
# define CAT_DEBUG_CHECK_MEMORY() CAT_ENFORCE(_CrtCheckMemory() != 0) << "Memory check failed!";
# endif
#endif
#endif
//// Vector Extensions ////
#if !defined __has_extension
# define __has_extension(x) 0
#endif
#if defined(__GNUC__) && __GNUC_MINOR__ >= 7
# define CAT_HAS_VECTOR_EXTENSIONS
# define CAT_VECTOR_SIZE(T, elements) __attribute__((vector_size(sizeof(T) * elements)))
# define CAT_VECTOR_EXT_GCC
#elif defined(__clang__) && __has_extension(attribute_ext_vector_type)
# define CAT_HAS_VECTOR_EXTENSIONS
# define CAT_VECTOR_SIZE(T, elements) __attribute__((ext_vector_type(elements)))
# define CAT_VECTOR_EXT_CLANG
#endif
#endif // CAT_PLATFORM_HPP