]> git.sesse.net Git - stockfish/blob - src/types.h
Readd SRWLOCK and Condition Variables under Windows
[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 #if !defined(TYPES_H_INCLUDED)
21 #define TYPES_H_INCLUDED
22
23 #if !defined(_MSC_VER)
24
25 #include <inttypes.h>
26
27 #else
28
29 // Disable some silly and noisy warning from MSVC compiler
30 #pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
31 #pragma warning(disable: 4127) // Conditional expression is constant
32
33 typedef __int8 int8_t;
34 typedef unsigned __int8 uint8_t;
35 typedef __int16 int16;
36 typedef unsigned __int16 uint16_t;
37 typedef __int32 int32_t;
38 typedef unsigned __int32 uint32_t;
39 typedef __int64 int64_t;
40 typedef unsigned __int64 uint64_t;
41
42 typedef __int16 int16_t;
43 typedef __int64 int64_t;
44
45 #endif // !defined(_MSC_VER)
46
47 // Hash keys
48 typedef uint64_t Key;
49
50 // Bitboard type
51 typedef uint64_t Bitboard;
52
53 #include <cstdlib>
54
55 ////
56 //// Configuration
57 ////
58
59 //// For Linux and OSX configuration is done automatically using Makefile.
60 //// To get started type "make help".
61 ////
62 //// For windows part of the configuration is detected automatically, but
63 //// some switches need to be set manually:
64 ////
65 //// -DNDEBUG       | Disable debugging mode. Use always.
66 ////
67 //// -DNO_PREFETCH  | Disable use of prefetch asm-instruction. A must if you want the
68 ////                | executable to run on some very old machines.
69 ////
70 //// -DUSE_POPCNT   | Add runtime support for use of popcnt asm-instruction.
71 ////                | Works only in 64-bit mode. For compiling requires hardware
72 ////                | with popcnt support. Around 4% speed-up.
73 ////
74 //// -DOLD_LOCKS    | By default under Windows are used the fast Slim Reader/Writer (SRW)
75 ////                | Locks and Condition Variables: these are not supported by Windows XP
76 ////                | and older, to compile for those platforms you should enable OLD_LOCKS.
77
78 // Automatic detection for 64-bit under Windows
79 #if defined(_WIN64)
80 #define IS_64BIT
81 #endif
82
83 // Automatic detection for use of bsfq asm-instruction under Windows.
84 // Works only in 64-bit mode. Does not work with MSVC.
85 #if defined(_WIN64) && defined(__INTEL_COMPILER)
86 #define USE_BSFQ
87 #endif
88
89 // Cache line alignment specification
90 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
91 #define CACHE_LINE_ALIGNMENT __declspec(align(64))
92 #else
93 #define CACHE_LINE_ALIGNMENT  __attribute__ ((aligned(64)))
94 #endif
95
96 // Define a __cpuid() function for gcc compilers, for Intel and MSVC
97 // is already available as an intrinsic.
98 #if defined(_MSC_VER)
99 #include <intrin.h>
100 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
101 inline void __cpuid(int CPUInfo[4], int InfoType)
102 {
103   int* eax = CPUInfo + 0;
104   int* ebx = CPUInfo + 1;
105   int* ecx = CPUInfo + 2;
106   int* edx = CPUInfo + 3;
107
108   *eax = InfoType;
109   *ecx = 0;
110   __asm__("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
111                   : "0" (*eax), "2" (*ecx));
112 }
113 #else
114 inline void __cpuid(int CPUInfo[4], int)
115 {
116    CPUInfo[0] = CPUInfo[1] = CPUInfo[2] = CPUInfo[3] = 0;
117 }
118 #endif
119
120
121 // Templetized operators used by enum types like Depth, Piece, Square and so on.
122 // We don't want to write the same inline for each different enum. Note that we
123 // pass by value to silence scaring warnings when using volatiles.
124 // Because these templates override common operators and are included in all the
125 // files, there is a possibility that the compiler silently performs some unwanted
126 // overrides. To avoid possible very nasty bugs the templates are disabled by default
127 // and must be enabled for each type on a case by case base. The enabling trick
128 // uses template specialization, namely we just declare following struct.
129 template<typename T> struct TempletizedOperator;
130
131 // Then to enable the enum type we use following macro that defines a specialization
132 // of TempletizedOperator for the given enum T. Here is defined typedef Not_Enabled.
133 // Name of typedef is chosen to produce somewhat informative compile error messages.
134 #define ENABLE_OPERATORS_ON(T)  \
135         template<> struct TempletizedOperator<T> { typedef T Not_Enabled; }
136
137 // Finally we use macro OK(T) to check if type T is enabled. The macro simply
138 // tries to use Not_Enabled, if was not previously defined a compile error occurs.
139 // The check is done fully at compile time and there is zero overhead at runtime.
140 #define OK(T) typedef typename TempletizedOperator<T>::Not_Enabled Type
141
142 template<typename T>
143 inline T operator+ (const T d1, const T d2) { OK(T); return T(int(d1) + int(d2)); }
144
145 template<typename T>
146 inline T operator- (const T d1, const T d2) { OK(T); return T(int(d1) - int(d2)); }
147
148 template<typename T>
149 inline T operator* (int i, const T d) { OK(T); return T(i * int(d)); }
150
151 template<typename T>
152 inline T operator* (const T d, int i) { OK(T); return T(int(d) * i); }
153
154 template<typename T>
155 inline T operator/ (const T d, int i) { OK(T); return T(int(d) / i); }
156
157 template<typename T>
158 inline T operator- (const T d) { OK(T); return T(-int(d)); }
159
160 template<typename T>
161 inline T operator++ (T& d, int) { OK(T); d = T(int(d) + 1); return d; }
162
163 template<typename T>
164 inline T operator-- (T& d, int) { OK(T); d = T(int(d) - 1); return d; }
165
166 template<typename T>
167 inline void operator+= (T& d1, const T d2) { OK(T); d1 = d1 + d2; }
168
169 template<typename T>
170 inline void operator-= (T& d1, const T d2) { OK(T); d1 = d1 - d2; }
171
172 template<typename T>
173 inline void operator*= (T& d, int i) { OK(T); d = T(int(d) * i); }
174
175 template<typename T>
176 inline void operator/= (T& d, int i) { OK(T); d = T(int(d) / i); }
177
178 #undef OK
179
180 #endif // !defined(TYPES_H_INCLUDED)