]> git.sesse.net Git - stockfish/blob - src/types.h
Unify best move update logic
[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 #include <cstdlib>
24
25 #if defined(_MSC_VER)
26
27 // Disable some silly and noisy warning from MSVC compiler
28 #pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
29 #pragma warning(disable: 4127) // Conditional expression is constant
30 #pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
31
32 // MSVC does not support <inttypes.h>
33 typedef   signed __int8    int8_t;
34 typedef unsigned __int8   uint8_t;
35 typedef   signed __int16  int16_t;
36 typedef unsigned __int16 uint16_t;
37 typedef   signed __int32  int32_t;
38 typedef unsigned __int32 uint32_t;
39 typedef   signed __int64  int64_t;
40 typedef unsigned __int64 uint64_t;
41
42 #else
43
44 #include <inttypes.h>
45
46 #endif
47
48 // Our hash key and bitboard types
49 typedef uint64_t Key;
50 typedef uint64_t Bitboard;
51
52 #define Min(x, y) (((x) < (y)) ? (x) : (y))
53 #define Max(x, y) (((x) < (y)) ? (y) : (x))
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 // Intel header for _mm_popcnt_u64() intrinsic
90 #if defined(USE_POPCNT) && defined(_MSC_VER) && defined(__INTEL_COMPILER)
91 #include <nmmintrin.h>
92 #endif
93
94 // Cache line alignment specification
95 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
96 #define CACHE_LINE_ALIGNMENT __declspec(align(64))
97 #else
98 #define CACHE_LINE_ALIGNMENT  __attribute__ ((aligned(64)))
99 #endif
100
101 // Define a __cpuid() function for gcc compilers, for Intel and MSVC
102 // is already available as an intrinsic.
103 #if defined(_MSC_VER)
104 #include <intrin.h>
105 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
106 inline void __cpuid(int CPUInfo[4], int InfoType)
107 {
108   int* eax = CPUInfo + 0;
109   int* ebx = CPUInfo + 1;
110   int* ecx = CPUInfo + 2;
111   int* edx = CPUInfo + 3;
112
113   *eax = InfoType;
114   *ecx = 0;
115   __asm__("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
116                   : "0" (*eax), "2" (*ecx));
117 }
118 #else
119 inline void __cpuid(int CPUInfo[4], int)
120 {
121    CPUInfo[0] = CPUInfo[1] = CPUInfo[2] = CPUInfo[3] = 0;
122 }
123 #endif
124
125 // Define FORCE_INLINE macro to force inlining overriding compiler choice
126 #if defined(_MSC_VER)
127 #define FORCE_INLINE  __forceinline
128 #elif defined(__GNUC__)
129 #define FORCE_INLINE  inline __attribute__((always_inline))
130 #else
131 #define FORCE_INLINE  inline
132 #endif
133
134 // Operators used by enum types like Depth, Piece, Square and so on.
135
136 #define ENABLE_OPERATORS_ON(T) \
137 inline T operator+ (const T d1, const T d2) { return T(int(d1) + int(d2)); } \
138 inline T operator- (const T d1, const T d2) { return T(int(d1) - int(d2)); } \
139 inline T operator* (int i, const T d) {  return T(i * int(d)); } \
140 inline T operator* (const T d, int i) {  return T(int(d) * i); } \
141 inline T operator/ (const T d, int i) { return T(int(d) / i); } \
142 inline T operator- (const T d) { return T(-int(d)); } \
143 inline T operator++ (T& d, int) {d = T(int(d) + 1); return d; } \
144 inline T operator-- (T& d, int) { d = T(int(d) - 1); return d; } \
145 inline void operator+= (T& d1, const T d2) { d1 = d1 + d2; } \
146 inline void operator-= (T& d1, const T d2) { d1 = d1 - d2; } \
147 inline void operator*= (T& d, int i) { d = T(int(d) * i); } \
148 inline void operator/= (T& d, int i) { d = T(int(d) / i); }
149
150
151 /// cpu_has_popcnt() detects support for popcnt instruction at runtime
152 inline bool cpu_has_popcnt() {
153
154   int CPUInfo[4] = {-1};
155   __cpuid(CPUInfo, 0x00000001);
156   return (CPUInfo[2] >> 23) & 1;
157 }
158
159 /// CpuHasPOPCNT is a global constant initialized at startup that
160 /// is set to true if CPU on which application runs supports popcnt
161 /// hardware instruction. Unless USE_POPCNT is not defined.
162 #if defined(USE_POPCNT)
163 const bool CpuHasPOPCNT = cpu_has_popcnt();
164 #else
165 const bool CpuHasPOPCNT = false;
166 #endif
167
168
169 /// CpuIs64Bit is a global constant initialized at compile time that
170 /// is set to true if CPU on which application runs is a 64 bits.
171 #if defined(IS_64BIT)
172 const bool CpuIs64Bit = true;
173 #else
174 const bool CpuIs64Bit = false;
175 #endif
176
177 #endif // !defined(TYPES_H_INCLUDED)