]> git.sesse.net Git - stockfish/blob - src/types.h
Fixed some warnings when using -Weffc++ gcc option
[stockfish] / src / types.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4   Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
5
6   Stockfish is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   Stockfish is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21 #if !defined(TYPES_H_INCLUDED)
22 #define TYPES_H_INCLUDED
23
24 #if !defined(_MSC_VER)
25
26 #include <inttypes.h>
27
28 #else
29
30 typedef __int8 int8_t;
31 typedef unsigned __int8 uint8_t;
32 typedef __int16 int16;
33 typedef unsigned __int16 uint16_t;
34 typedef __int32 int32_t;
35 typedef unsigned __int32 uint32_t;
36 typedef __int64 int64_t;
37 typedef unsigned __int64 uint64_t;
38
39 typedef __int16 int16_t;
40 typedef __int64 int64_t;
41
42 #endif // !defined(_MSC_VER)
43
44 // Hash keys
45 typedef uint64_t Key;
46
47 // Bitboard type
48 typedef uint64_t Bitboard;
49
50
51 ////
52 //// Configuration
53 ////
54
55 //// For Linux and OSX configuration is done automatically using Makefile.
56 //// To get started type "make help".
57 ////
58 //// For windows part of the configuration is detected automatically, but
59 //// some switches need to be set manually:
60 ////
61 //// -DNDEBUG       | Disable debugging mode. Use always.
62 ////
63 //// -DNO_PREFETCH  | Disable use of prefetch asm-instruction. A must if you want the
64 ////                | executable to run on some very old machines.
65 ////
66 //// -DUSE_POPCNT   | Add runtime support for use of popcnt asm-instruction.
67 ////                | Works only in 64-bit mode. For compiling requires hardware
68 ////                | with popcnt support. Around 4% speed-up.
69
70 // Automatic detection for 64-bit under Windows
71 #if defined(_WIN64)
72 #define IS_64BIT
73 #endif
74
75 // Automatic detection for use of bsfq asm-instruction under Windows.
76 // Works only in 64-bit mode. Does not work with MSVC.
77 #if defined(_WIN64) && defined(__INTEL_COMPILER)
78 #define USE_BSFQ
79 #endif
80
81 // Cache line alignment specification
82 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
83 #define CACHE_LINE_ALIGNMENT __declspec(align(64))
84 #else
85 #define CACHE_LINE_ALIGNMENT  __attribute__ ((aligned(64)))
86 #endif
87
88 // Define a __cpuid() function for gcc compilers, for Intel and MSVC
89 // is already available as an intrinsic.
90 #if defined(_MSC_VER)
91 #include <intrin.h>
92 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
93 inline void __cpuid(int CPUInfo[4], int InfoType)
94 {
95   int* eax = CPUInfo + 0;
96   int* ebx = CPUInfo + 1;
97   int* ecx = CPUInfo + 2;
98   int* edx = CPUInfo + 3;
99
100   *eax = InfoType;
101   *ecx = 0;
102   __asm__("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
103                   : "0" (*eax), "2" (*ecx));
104 }
105 #else
106 inline void __cpuid(int CPUInfo[4], int)
107 {
108    CPUInfo[0] = CPUInfo[1] = CPUInfo[2] = CPUInfo[3] = 0;
109 }
110 #endif
111
112
113 // Templetized operators used by enum types like Depth, Piece, Square and so on.
114 // We don't want to write the same inline for each different enum. Note that we
115 // pass by value to silence scaring warnings when using volatiles.
116 // Because these templates override common operators and are included in all the
117 // files, there is a possibility that the compiler silently performs some unwanted
118 // overrides. To avoid possible very nasty bugs the templates are disabled by default
119 // and must be enabled for each type on a case by case base. The enabling trick
120 // uses template specialization, namely we just declare following struct.
121 template<typename T> struct TempletizedOperator;
122
123 // Then to enable the enum type we use following macro that defines a specialization
124 // of TempletizedOperator for the given enum T. Here is defined typedef Not_Enabled.
125 // Name of typedef is chosen to produce somewhat informative compile error messages.
126 #define ENABLE_OPERATORS_ON(T)  \
127         template<> struct TempletizedOperator<T> { typedef T Not_Enabled; }
128
129 // Finally we use macro OK(T) to check if type T is enabled. The macro simply
130 // tries to use Not_Enabled, if was not previously defined a compile error occurs.
131 // The check is done fully at compile time and there is zero overhead at runtime.
132 #define OK(T) typedef typename TempletizedOperator<T>::Not_Enabled Type
133
134 template<typename T>
135 inline T operator+ (const T d1, const T d2) { OK(T); return T(int(d1) + int(d2)); }
136
137 template<typename T>
138 inline T operator- (const T d1, const T d2) { OK(T); return T(int(d1) - int(d2)); }
139
140 template<typename T>
141 inline T operator* (int i, const T d) { OK(T); return T(i * int(d)); }
142
143 template<typename T>
144 inline T operator* (const T d, int i) { OK(T); return T(int(d) * i); }
145
146 template<typename T>
147 inline T operator/ (const T d, int i) { OK(T); return T(int(d) / i); }
148
149 template<typename T>
150 inline T operator- (const T d) { OK(T); return T(-int(d)); }
151
152 template<typename T>
153 inline T operator++ (T& d, int) { OK(T); d = T(int(d) + 1); return d; }
154
155 template<typename T>
156 inline T operator-- (T& d, int) { OK(T); d = T(int(d) - 1); return d; }
157
158 template<typename T>
159 inline void operator+= (T& d1, const T d2) { OK(T); d1 = d1 + d2; }
160
161 template<typename T>
162 inline void operator-= (T& d1, const T d2) { OK(T); d1 = d1 - d2; }
163
164 template<typename T>
165 inline void operator*= (T& d, int i) { OK(T); d = T(int(d) * i); }
166
167 template<typename T>
168 inline void operator/= (T& d, int i) { OK(T); d = T(int(d) / i); }
169
170 #undef OK
171
172 #endif // !defined(TYPES_H_INCLUDED)