Subversion Repositories Games.Chess Giants

Rev

Rev 96 | Rev 169 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 96 Rev 154
Line 47... Line 47...
47
#if defined(_MSC_VER)
47
#if defined(_MSC_VER)
48
// Disable some silly and noisy warning from MSVC compiler
48
// Disable some silly and noisy warning from MSVC compiler
49
#pragma warning(disable: 4127) // Conditional expression is constant
49
#pragma warning(disable: 4127) // Conditional expression is constant
50
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
50
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
51
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
51
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
52
#include <intrin.h> // Pierre-Marie Baty -- for popcnt()
-
 
53
#endif
52
#endif
54
 
53
 
55
/// Predefined macros hell:
54
/// Predefined macros hell:
56
///
55
///
57
/// __GNUC__           Compiler is gcc, Clang or Intel on Linux
56
/// __GNUC__           Compiler is gcc, Clang or Intel on Linux
Line 59... Line 58...
59
/// _MSC_VER           Compiler is MSVC or Intel on Windows
58
/// _MSC_VER           Compiler is MSVC or Intel on Windows
60
/// _WIN32             Building on Windows (any)
59
/// _WIN32             Building on Windows (any)
61
/// _WIN64             Building on Windows 64 bit
60
/// _WIN64             Building on Windows 64 bit
62
 
61
 
63
#if defined(_WIN64) && defined(_MSC_VER) // No Makefile used
62
#if defined(_WIN64) && defined(_MSC_VER) // No Makefile used
64
#  include <intrin.h> // MSVC popcnt and bsfq instrinsics
63
#  include <intrin.h> // Microsoft header for _BitScanForward64()
65
#  define IS_64BIT
64
#  define IS_64BIT
66
#  define USE_BSFQ
-
 
67
#endif
65
#endif
68
 
66
 
69
#if defined(USE_POPCNT) && defined(__INTEL_COMPILER) && defined(_MSC_VER)
67
#if defined(USE_POPCNT) && (defined(__INTEL_COMPILER) || defined(_MSC_VER))
70
#  include <nmmintrin.h> // Intel header for _mm_popcnt_u64() intrinsic
68
#  include <nmmintrin.h> // Intel and Microsoft header for _mm_popcnt_u64()
71
#endif
69
#endif
72
 
70
 
73
#if !defined(NO_PREFETCH) && (defined(__INTEL_COMPILER) || defined(_MSC_VER))
71
#if !defined(NO_PREFETCH) && (defined(__INTEL_COMPILER) || defined(_MSC_VER))
74
#  include <xmmintrin.h> // Intel and Microsoft header for _mm_prefetch()
72
#  include <xmmintrin.h> // Intel and Microsoft header for _mm_prefetch()
75
#endif
73
#endif
Line 115... Line 113...
115
///
113
///
116
/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in because in
114
/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in because in
117
/// any normal move destination square is always different from origin square
115
/// any normal move destination square is always different from origin square
118
/// while MOVE_NONE and MOVE_NULL have the same origin and destination square.
116
/// while MOVE_NONE and MOVE_NULL have the same origin and destination square.
119
 
117
 
120
enum Move {
118
enum Move : int {
121
  MOVE_NONE,
119
  MOVE_NONE,
122
  MOVE_NULL = 65
120
  MOVE_NULL = 65
123
};
121
};
124
 
122
 
125
enum MoveType {
123
enum MoveType {
Line 183... Line 181...
183
  VALUE_NONE      = 32002,
181
  VALUE_NONE      = 32002,
184
 
182
 
185
  VALUE_MATE_IN_MAX_PLY  =  VALUE_MATE - 2 * MAX_PLY,
183
  VALUE_MATE_IN_MAX_PLY  =  VALUE_MATE - 2 * MAX_PLY,
186
  VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + 2 * MAX_PLY,
184
  VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + 2 * MAX_PLY,
187
 
185
 
188
  PawnValueMg   = 198,   PawnValueEg   = 258,
186
  PawnValueMg   = 188,   PawnValueEg   = 248,
189
  KnightValueMg = 817,   KnightValueEg = 846,
187
  KnightValueMg = 753,   KnightValueEg = 832,
190
  BishopValueMg = 836,   BishopValueEg = 857,
188
  BishopValueMg = 826,   BishopValueEg = 897,
191
  RookValueMg   = 1270,  RookValueEg   = 1281,
189
  RookValueMg   = 1285,  RookValueEg   = 1371,
192
  QueenValueMg  = 2521,  QueenValueEg  = 2558,
190
  QueenValueMg  = 2513,  QueenValueEg  = 2650,
193
 
191
 
194
  MidgameLimit  = 15581, EndgameLimit  = 3998
192
  MidgameLimit  = 15258, EndgameLimit  = 3915
195
};
193
};
196
 
194
 
197
enum PieceType {
195
enum PieceType {
198
  NO_PIECE_TYPE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING,
196
  NO_PIECE_TYPE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING,
199
  ALL_PIECES = 0,
197
  ALL_PIECES = 0,
Line 204... Line 202...
204
  NO_PIECE,
202
  NO_PIECE,
205
  W_PAWN = 1, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
203
  W_PAWN = 1, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
206
  B_PAWN = 9, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING,
204
  B_PAWN = 9, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING,
207
  PIECE_NB = 16
205
  PIECE_NB = 16
208
};
206
};
-
 
207
 
-
 
208
const Piece Pieces[] = { W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
-
 
209
                         B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING };
-
 
210
extern Value PieceValue[PHASE_NB][PIECE_NB];
209
 
211
 
210
enum Depth {
212
enum Depth {
211
 
213
 
212
  ONE_PLY = 1,
214
  ONE_PLY = 1,
213
 
215
 
214
  DEPTH_ZERO          =  0,
216
  DEPTH_ZERO          =  0 * ONE_PLY,
215
  DEPTH_QS_CHECKS     =  0,
217
  DEPTH_QS_CHECKS     =  0 * ONE_PLY,
216
  DEPTH_QS_NO_CHECKS  = -1,
218
  DEPTH_QS_NO_CHECKS  = -1 * ONE_PLY,
217
  DEPTH_QS_RECAPTURES = -5,
219
  DEPTH_QS_RECAPTURES = -5 * ONE_PLY,
218
 
220
 
219
  DEPTH_NONE = -6,
221
  DEPTH_NONE = -6 * ONE_PLY,
220
  DEPTH_MAX  = MAX_PLY
222
  DEPTH_MAX  = MAX_PLY * ONE_PLY
221
};
223
};
-
 
224
 
-
 
225
static_assert(!(ONE_PLY & (ONE_PLY - 1)), "ONE_PLY is not a power of 2");
222
 
226
 
223
enum Square {
227
enum Square {
224
  SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
228
  SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
225
  SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
229
  SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
226
  SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
230
  SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
Line 231... Line 235...
231
  SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
235
  SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
232
  SQ_NONE,
236
  SQ_NONE,
233
 
237
 
234
  SQUARE_NB = 64,
238
  SQUARE_NB = 64,
235
 
239
 
236
  DELTA_N =  8,
240
  NORTH =  8,
237
  DELTA_E =  1,
241
  EAST  =  1,
238
  DELTA_S = -8,
242
  SOUTH = -8,
239
  DELTA_W = -1,
243
  WEST  = -1,
240
 
244
 
241
  DELTA_NN = DELTA_N + DELTA_N,
-
 
242
  DELTA_NE = DELTA_N + DELTA_E,
-
 
243
  DELTA_SE = DELTA_S + DELTA_E,
245
  NORTH_EAST = NORTH + EAST,
244
  DELTA_SS = DELTA_S + DELTA_S,
246
  SOUTH_EAST = SOUTH + EAST,
245
  DELTA_SW = DELTA_S + DELTA_W,
247
  SOUTH_WEST = SOUTH + WEST,
246
  DELTA_NW = DELTA_N + DELTA_W
248
  NORTH_WEST = NORTH + WEST
247
};
249
};
248
 
250
 
249
enum File {
251
enum File : int {
250
  FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NB
252
  FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NB
251
};
253
};
252
 
254
 
253
enum Rank {
255
enum Rank : int {
254
  RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB
256
  RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB
255
};
257
};
256
 
258
 
257
 
259
 
258
/// Score enum stores a middlegame and an endgame value in a single integer
260
/// Score enum stores a middlegame and an endgame value in a single integer
259
/// (enum). The least significant 16 bits are used to store the endgame value
261
/// (enum). The least significant 16 bits are used to store the endgame value
260
/// and the upper 16 bits are used to store the middlegame value.
262
/// and the upper 16 bits are used to store the middlegame value. Take some
-
 
263
/// care to avoid left-shifting a signed int to avoid undefined behavior.
261
enum Score : int { SCORE_ZERO };
264
enum Score : int { SCORE_ZERO };
262
 
265
 
263
inline Score make_score(int mg, int eg) {
266
inline Score make_score(int mg, int eg) {
264
  return Score((mg << 16) + eg);
267
  return Score((int)((unsigned int)eg << 16) + mg);
265
}
268
}
266
 
269
 
267
/// Extracting the signed lower and upper 16 bits is not so trivial because
270
/// Extracting the signed lower and upper 16 bits is not so trivial because
268
/// according to the standard a simple cast to short is implementation defined
271
/// according to the standard a simple cast to short is implementation defined
269
/// and so is a right shift of a signed integer.
272
/// and so is a right shift of a signed integer.
270
inline Value mg_value(Score s) {
273
inline Value eg_value(Score s) {
271
 
274
 
272
  union { uint16_t u; int16_t s; } mg = { uint16_t(unsigned(s + 0x8000) >> 16) };
275
  union { uint16_t u; int16_t s; } eg = { uint16_t(unsigned(s + 0x8000) >> 16) };
273
  return Value(mg.s);
276
  return Value(eg.s);
274
}
277
}
275
 
278
 
276
inline Value eg_value(Score s) {
279
inline Value mg_value(Score s) {
277
 
280
 
278
  union { uint16_t u; int16_t s; } eg = { uint16_t(unsigned(s)) };
281
  union { uint16_t u; int16_t s; } mg = { uint16_t(unsigned(s)) };
279
  return Value(eg.s);
282
  return Value(mg.s);
280
}
283
}
281
 
284
 
282
#define ENABLE_BASE_OPERATORS_ON(T)                             \
285
#define ENABLE_BASE_OPERATORS_ON(T)                             \
283
inline T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \
286
inline T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \
284
inline T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \
287
inline T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \
Line 323... Line 326...
323
 
326
 
324
/// Division of a Score must be handled separately for each term
327
/// Division of a Score must be handled separately for each term
325
inline Score operator/(Score s, int i) {
328
inline Score operator/(Score s, int i) {
326
  return make_score(mg_value(s) / i, eg_value(s) / i);
329
  return make_score(mg_value(s) / i, eg_value(s) / i);
327
}
330
}
328
 
-
 
329
extern Value PieceValue[PHASE_NB][PIECE_NB];
-
 
330
 
331
 
331
inline Color operator~(Color c) {
332
inline Color operator~(Color c) {
332
  return Color(c ^ BLACK);
333
  return Color(c ^ BLACK); // Toggle color
333
}
334
}
334
 
335
 
335
inline Square operator~(Square s) {
336
inline Square operator~(Square s) {
336
  return Square(s ^ SQ_A8); // Vertical flip SQ_A1 -> SQ_A8
337
  return Square(s ^ SQ_A8); // Vertical flip SQ_A1 -> SQ_A8
-
 
338
}
-
 
339
 
-
 
340
inline Piece operator~(Piece pc) {
-
 
341
  return Piece(pc ^ 8); // Swap color of piece B_KNIGHT -> W_KNIGHT
337
}
342
}
338
 
343
 
339
inline CastlingRight operator|(Color c, CastlingSide s) {
344
inline CastlingRight operator|(Color c, CastlingSide s) {
340
  return CastlingRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
345
  return CastlingRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
341
}
346
}
Line 347... Line 352...
347
inline Value mated_in(int ply) {
352
inline Value mated_in(int ply) {
348
  return -VALUE_MATE + ply;
353
  return -VALUE_MATE + ply;
349
}
354
}
350
 
355
 
351
inline Square make_square(File f, Rank r) {
356
inline Square make_square(File f, Rank r) {
352
  return Square((r << 3) | f);
357
  return Square((r << 3) + f);
353
}
358
}
354
 
359
 
355
inline Piece make_piece(Color c, PieceType pt) {
360
inline Piece make_piece(Color c, PieceType pt) {
356
  return Piece((c << 3) | pt);
361
  return Piece((c << 3) + pt);
357
}
362
}
358
 
363
 
359
inline PieceType type_of(Piece pc)  {
364
inline PieceType type_of(Piece pc) {
360
  return PieceType(pc & 7);
365
  return PieceType(pc & 7);
361
}
366
}
362
 
367
 
363
inline Color color_of(Piece pc) {
368
inline Color color_of(Piece pc) {
364
  assert(pc != NO_PIECE);
369
  assert(pc != NO_PIECE);
Line 393... Line 398...
393
  int s = int(s1) ^ int(s2);
398
  int s = int(s1) ^ int(s2);
394
  return ((s >> 3) ^ s) & 1;
399
  return ((s >> 3) ^ s) & 1;
395
}
400
}
396
 
401
 
397
inline Square pawn_push(Color c) {
402
inline Square pawn_push(Color c) {
398
  return c == WHITE ? DELTA_N : DELTA_S;
403
  return c == WHITE ? NORTH : SOUTH;
399
}
404
}
400
 
405
 
401
inline Square from_sq(Move m) {
406
inline Square from_sq(Move m) {
402
  return Square((m >> 6) & 0x3F);
407
  return Square((m >> 6) & 0x3F);
403
}
408
}
Line 413... Line 418...
413
inline PieceType promotion_type(Move m) {
418
inline PieceType promotion_type(Move m) {
414
  return PieceType(((m >> 12) & 3) + KNIGHT);
419
  return PieceType(((m >> 12) & 3) + KNIGHT);
415
}
420
}
416
 
421
 
417
inline Move make_move(Square from, Square to) {
422
inline Move make_move(Square from, Square to) {
418
  return Move(to | (from << 6));
423
  return Move((from << 6) + to);
419
}
424
}
420
 
425
 
421
template<MoveType T>
426
template<MoveType T>
422
inline Move make(Square from, Square to, PieceType pt = KNIGHT) {
427
inline Move make(Square from, Square to, PieceType pt = KNIGHT) {
423
  return Move(to | (from << 6) | T | ((pt - KNIGHT) << 12));
428
  return Move(T + ((pt - KNIGHT) << 12) + (from << 6) + to);
424
}
429
}
425
 
430
 
426
inline bool is_ok(Move m) {
431
inline bool is_ok(Move m) {
427
  return from_sq(m) != to_sq(m); // Catch MOVE_NULL and MOVE_NONE
432
  return from_sq(m) != to_sq(m); // Catch MOVE_NULL and MOVE_NONE
428
}
433
}