]> git.sesse.net Git - stockfish/blob - src/evaluate.cpp
Code style massage evaluate()
[stockfish] / src / evaluate.cpp
1 /*
2   Glaurung, a UCI chess playing engine.
3   Copyright (C) 2004-2008 Tord Romstad
4
5   Glaurung is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Glaurung is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 ////
21 //// Includes
22 ////
23
24 #include <cassert>
25 #include <cstring>
26
27 #include "evaluate.h"
28 #include "material.h"
29 #include "pawns.h"
30 #include "scale.h"
31 #include "thread.h"
32 #include "ucioption.h"
33
34
35 ////
36 //// Local definitions
37 ////
38
39 namespace {
40
41   const int Sign[2] = {1, -1};
42
43   // Evaluation grain size, must be a power of 2.
44   const int GrainSize = 4;
45
46   // Evaluation weights
47   int WeightMobilityMidgame      = 0x100;
48   int WeightMobilityEndgame      = 0x100;
49   int WeightPawnStructureMidgame = 0x100;
50   int WeightPawnStructureEndgame = 0x100;
51   int WeightPassedPawnsMidgame   = 0x100;
52   int WeightPassedPawnsEndgame   = 0x100;
53   int WeightKingSafety[2] = { 0x100, 0x100 };
54
55   // Internal evaluation weights.  These are applied on top of the evaluation
56   // weights read from UCI parameters.  The purpose is to be able to change
57   // the evaluation weights while keeping the default values of the UCI
58   // parameters at 100, which looks prettier.
59   const int WeightMobilityMidgameInternal      = 0x100;
60   const int WeightMobilityEndgameInternal      = 0x100;
61   const int WeightPawnStructureMidgameInternal = 0x100;
62   const int WeightPawnStructureEndgameInternal = 0x100;
63   const int WeightPassedPawnsMidgameInternal   = 0x100;
64   const int WeightPassedPawnsEndgameInternal   = 0x100;
65   const int WeightKingSafetyInternal           = 0x100;
66   const int WeightKingOppSafetyInternal        = 0x100;
67
68   // Visually better to define tables constants
69   typedef Value V;
70
71   // Knight mobility bonus in middle game and endgame, indexed by the number
72   // of attacked squares not occupied by friendly piecess.
73   const Value MidgameKnightMobilityBonus[] = {
74   //    0       1      2     3      4      5      6      7      8
75     V(-30), V(-20),V(-10), V(0), V(10), V(20), V(25), V(30), V(30)
76   };
77
78   const Value EndgameKnightMobilityBonus[] = {
79   //    0       1      2     3      4      5      6      7      8
80     V(-30), V(-20),V(-10), V(0), V(10), V(20), V(25), V(30), V(30)
81   };
82
83   // Bishop mobility bonus in middle game and endgame, indexed by the number
84   // of attacked squares not occupied by friendly pieces.  X-ray attacks through
85   // queens are also included.
86   const Value MidgameBishopMobilityBonus[] = {
87   //    0       1      2      3      4      5      6      7
88     V(-30), V(-15),  V(0), V(15), V(30), V(45), V(58), V(66),
89   //    8       9     10     11     12     13     14     15
90     V( 72), V( 76), V(78), V(80), V(81), V(82), V(83), V(83)
91   };
92
93   const Value EndgameBishopMobilityBonus[] = {
94   //    0       1      2      3      4      5      6      7
95     V(-30), V(-15),  V(0), V(15), V(30), V(45), V(58), V(66),
96   //    8       9     10     11     12     13     14     15
97     V( 72), V( 76), V(78), V(80), V(81), V(82), V(83), V(83)
98   };
99
100   // Rook mobility bonus in middle game and endgame, indexed by the number
101   // of attacked squares not occupied by friendly pieces.  X-ray attacks through
102   // queens and rooks are also included.
103   const Value MidgameRookMobilityBonus[] = {
104   //    0       1      2      3      4      5      6      7
105     V(-18), V(-12), V(-6),  V(0),  V(6), V(12), V(16), V(21),
106   //    8       9     10     11     12     13     14     15
107     V( 24), V( 27), V(28), V(29), V(30), V(31), V(32), V(33)
108   };
109
110   const Value EndgameRookMobilityBonus[] = {
111   //    0       1      2      3      4      5      6      7
112     V(-30), V(-18), V(-6),  V(6), V(18), V(30), V(42), V(54),
113   //    8       9     10     11     12     13     14     15
114     V( 66), V( 74), V(78), V(80), V(81), V(82), V(83), V(83)
115   };
116
117   // Queen mobility bonus in middle game and endgame, indexed by the number
118   // of attacked squares not occupied by friendly pieces.
119   const Value MidgameQueenMobilityBonus[] = {
120   //    0      1      2      3      4      5      6      7
121     V(-10), V(-8), V(-6), V(-4), V(-2), V( 0), V( 2), V( 4),
122   //    8      9     10     11     12     13     14     15
123     V(  6), V( 8), V(10), V(12), V(13), V(14), V(15), V(16),
124   //   16     17     18     19     20     21     22     23
125     V( 16), V(16), V(16), V(16), V(16), V(16), V(16), V(16),
126   //   24     25     26     27     28     29     30     31
127     V( 16), V(16), V(16), V(16), V(16), V(16), V(16), V(16)
128   };
129
130   const Value EndgameQueenMobilityBonus[] = {
131   //    0      1      2      3      4      5      6      7
132     V(-20),V(-15),V(-10), V(-5), V( 0), V( 5), V(10), V(15),
133   //    8      9     10     11     12     13     14     15
134     V( 19), V(23), V(27), V(29), V(30), V(30), V(30), V(30),
135   //   16     17     18     19     20     21     22     23
136     V( 30), V(30), V(30), V(30), V(30), V(30), V(30), V(30),
137   //   24     25     26     27     28     29     30     31
138     V( 30), V(30), V(30), V(30), V(30), V(30), V(30), V(30)
139   };
140
141   // Outpost bonuses for knights and bishops, indexed by square (from white's
142   // point of view).
143   const Value KnightOutpostBonus[64] = {
144   //  A     B     C     D     E     F     G     H
145     V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // 1
146     V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // 2
147     V(0), V(0), V(5),V(10),V(10), V(5), V(0), V(0), // 3
148     V(0), V(5),V(20),V(30),V(30),V(20), V(5), V(0), // 4
149     V(0),V(10),V(30),V(40),V(40),V(30),V(10), V(0), // 5
150     V(0), V(5),V(20),V(20),V(20),V(20), V(5), V(0), // 6
151     V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // 7
152     V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0)  // 8
153   };
154
155   const Value BishopOutpostBonus[64] = {
156   //  A     B     C     D     E     F     G     H
157     V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // 1
158     V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // 2
159     V(0), V(0), V(5), V(5), V(5), V(5), V(0), V(0), // 3
160     V(0), V(5),V(10),V(10),V(10),V(10), V(5), V(0), // 4
161     V(0),V(10),V(20),V(20),V(20),V(20),V(10), V(0), // 5
162     V(0), V(5), V(8), V(8), V(8), V(8), V(5), V(0), // 6
163     V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0), // 7
164     V(0), V(0), V(0), V(0), V(0), V(0), V(0), V(0)  // 8
165   };
166
167   // Bonus for unstoppable passed pawns:
168   const Value UnstoppablePawnValue = Value(0x500);
169
170   // Rooks and queens on the 7th rank:
171   const Value MidgameRookOn7thBonus  = Value(50);
172   const Value EndgameRookOn7thBonus  = Value(100);
173   const Value MidgameQueenOn7thBonus = Value(25);
174   const Value EndgameQueenOn7thBonus = Value(50);
175
176   // Rooks on open files:
177   const Value RookOpenFileBonus     = Value(40);
178   const Value RookHalfOpenFileBonus = Value(20);
179
180   // Penalty for rooks trapped inside a friendly king which has lost the
181   // right to castle:
182   const Value TrappedRookPenalty = Value(180);
183
184   // Penalty for a bishop on a7/h7 (a2/h2 for black) which is trapped by
185   // enemy pawns:
186   const Value TrappedBishopA7H7Penalty = Value(300);
187
188   // Bitboard masks for detecting trapped bishops on a7/h7 (a2/h2 for black):
189   const Bitboard MaskA7H7[2] = {
190     ((1ULL << SQ_A7) | (1ULL << SQ_H7)),
191     ((1ULL << SQ_A2) | (1ULL << SQ_H2))
192   };
193
194   // Penalty for a bishop on a1/h1 (a8/h8 for black) which is trapped by
195   // a friendly pawn on b2/g2 (b7/g7 for black).  This can obviously only
196   // happen in Chess960 games.
197   const Value TrappedBishopA1H1Penalty = Value(100);
198
199   // Bitboard masks for detecting trapped bishops on a1/h1 (a8/h8 for black):
200   const Bitboard MaskA1H1[2] = {
201     ((1ULL << SQ_A1) | (1ULL << SQ_H1)),
202     ((1ULL << SQ_A8) | (1ULL << SQ_H8))
203   };
204
205   /// King safety constants and variables.  The king safety scores are taken
206   /// from the array SafetyTable[].  Various little "meta-bonuses" measuring
207   /// the strength of the attack are added up into an integer, which is used
208   /// as an index to SafetyTable[].
209
210   // Attack weights for each piece type.
211   const int QueenAttackWeight  = 5;
212   const int RookAttackWeight   = 3;
213   const int BishopAttackWeight = 2;
214   const int KnightAttackWeight = 2;
215
216   // Bonuses for safe checks for each piece type.
217   int QueenContactCheckBonus = 4;
218   int RookContactCheckBonus  = 2;
219   int QueenCheckBonus        = 2;
220   int RookCheckBonus         = 1;
221   int BishopCheckBonus       = 1;
222   int KnightCheckBonus       = 1;
223   int DiscoveredCheckBonus   = 3;
224
225   // Scan for queen contact mates?
226   const bool QueenContactMates = true;
227
228   // Bonus for having a mate threat.
229   int MateThreatBonus = 3;
230
231   // InitKingDanger[] contains bonuses based on the position of the defending
232   // king.
233   const int InitKingDanger[64] = {
234      2,  0,  2,  5,  5,  2,  0,  2,
235      2,  2,  4,  8,  8,  4,  2,  2,
236      7, 10, 12, 12, 12, 12, 10,  7,
237     15, 15, 15, 15, 15, 15, 15, 15,
238     15, 15, 15, 15, 15, 15, 15, 15,
239     15, 15, 15, 15, 15, 15, 15, 15,
240     15, 15, 15, 15, 15, 15, 15, 15,
241     15, 15, 15, 15, 15, 15, 15, 15
242   };
243
244   // SafetyTable[] contains the actual king safety scores.  It is initialized
245   // in init_safety().
246   Value SafetyTable[100];
247
248   // Pawn and material hash tables, indexed by the current thread id:
249   PawnInfoTable *PawnTable[8] = {0, 0, 0, 0, 0, 0, 0, 0};
250   MaterialInfoTable *MaterialTable[8] = {0, 0, 0, 0, 0, 0, 0, 0};
251
252   // Sizes of pawn and material hash tables:
253   const int PawnTableSize = 16384;
254   const int MaterialTableSize = 1024;
255
256   // Array which gives the number of nonzero bits in an 8-bit integer:
257   uint8_t BitCount8Bit[256];
258
259   // Function prototypes:
260   void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei);
261   void evaluate_bishop(const Position &p, Square s, Color us, EvalInfo &ei);
262   void evaluate_rook(const Position &p, Square s, Color us, EvalInfo &ei);
263   void evaluate_queen(const Position &p, Square s, Color us, EvalInfo &ei);
264   void evaluate_king(const Position &p, Square s, Color us, EvalInfo &ei);
265
266   void evaluate_passed_pawns(const Position &pos, EvalInfo &ei);
267   void evaluate_trapped_bishop_a7h7(const Position &pos, Square s, Color us,
268                                     EvalInfo &ei);
269   void evaluate_trapped_bishop_a1h1(const Position &pos, Square s, Color us,
270                                     EvalInfo &ei);
271
272   inline Value apply_weight(Value v, int w);
273   Value scale_by_game_phase(Value mv, Value ev, Phase ph, ScaleFactor sf[]);
274
275   int count_1s_8bit(Bitboard b);
276
277   int compute_weight(int uciWeight, int internalWeight);
278   int weight_option(const std::string& opt, int weight);
279   void init_safety();
280
281 }
282
283
284 ////
285 //// Functions
286 ////
287
288 /// evaluate() is the main evaluation function.  It always computes two
289 /// values, an endgame score and a middle game score, and interpolates
290 /// between them based on the remaining material.
291
292 Value evaluate(const Position &pos, EvalInfo &ei, int threadID) {
293
294   assert(pos.is_ok());
295   assert(threadID >= 0 && threadID < THREAD_MAX);
296
297   memset(&ei, 0, sizeof(EvalInfo));
298
299   // Initialize by reading the incrementally updated scores included in the
300   // position object (material + piece square tables)
301   ei.mgValue = pos.mg_value();
302   ei.egValue = pos.eg_value();
303
304   // Probe the material hash table
305   ei.mi = MaterialTable[threadID]->get_material_info(pos);
306   ei.mgValue += ei.mi->mg_value();
307   ei.egValue += ei.mi->eg_value();
308
309   // If we have a specialized evaluation function for the current material
310   // configuration, call it and return
311   if (ei.mi->specialized_eval_exists())
312       return ei.mi->evaluate(pos);
313
314   // After get_material_info() call that modifies them
315   ScaleFactor factor[2];
316   factor[WHITE] = ei.mi->scale_factor(pos, WHITE);
317   factor[BLACK] = ei.mi->scale_factor(pos, BLACK);
318
319   // Probe the pawn hash table
320   ei.pi = PawnTable[threadID]->get_pawn_info(pos);
321   ei.mgValue += apply_weight(ei.pi->mg_value(), WeightPawnStructureMidgame);
322   ei.egValue += apply_weight(ei.pi->eg_value(), WeightPawnStructureEndgame);
323
324   // Initialize king attack bitboards and king attack zones for both sides
325   ei.attackedBy[WHITE][KING] = pos.king_attacks(pos.king_square(WHITE));
326   ei.attackedBy[BLACK][KING] = pos.king_attacks(pos.king_square(BLACK));
327   ei.kingZone[WHITE] = ei.attackedBy[BLACK][KING] | (ei.attackedBy[BLACK][KING] >> 8);
328   ei.kingZone[BLACK] = ei.attackedBy[WHITE][KING] | (ei.attackedBy[WHITE][KING] << 8);
329
330   // Initialize pawn attack bitboards for both sides
331   ei.attackedBy[WHITE][PAWN] = ((pos.pawns(WHITE) << 9) & ~FileABB) | ((pos.pawns(WHITE) << 7) & ~FileHBB);
332   ei.attackedBy[BLACK][PAWN] = ((pos.pawns(BLACK) >> 7) & ~FileABB) | ((pos.pawns(BLACK) >> 9) & ~FileHBB);
333   ei.kingAttackersCount[WHITE] = count_1s_max_15(ei.attackedBy[WHITE][PAWN] & ei.attackedBy[BLACK][KING])/2;
334   ei.kingAttackersCount[BLACK] = count_1s_max_15(ei.attackedBy[BLACK][PAWN] & ei.attackedBy[WHITE][KING])/2;
335
336   // Evaluate pieces
337   for (Color c = WHITE; c <= BLACK; c++)
338   {
339     // Knights
340     for (int i = 0; i < pos.knight_count(c); i++)
341         evaluate_knight(pos, pos.knight_list(c, i), c, ei);
342
343     // Bishops
344     for (int i = 0; i < pos.bishop_count(c); i++)
345         evaluate_bishop(pos, pos.bishop_list(c, i), c, ei);
346
347     // Rooks
348     for (int i = 0; i < pos.rook_count(c); i++)
349         evaluate_rook(pos, pos.rook_list(c, i), c, ei);
350
351     // Queens
352     for(int i = 0; i < pos.queen_count(c); i++)
353         evaluate_queen(pos, pos.queen_list(c, i), c, ei);
354
355     // Special pattern: trapped bishops on a7/h7/a2/h2
356     Bitboard b = pos.bishops(c) & MaskA7H7[c];
357     while (b)
358     {
359         Square s = pop_1st_bit(&b);
360         evaluate_trapped_bishop_a7h7(pos, s, c, ei);
361     }
362
363     // Special pattern: trapped bishops on a1/h1/a8/h8 in Chess960:
364     if (Chess960)
365     {
366         b = pos.bishops(c) & MaskA1H1[c];
367         while (b)
368         {
369             Square s = pop_1st_bit(&b);
370             evaluate_trapped_bishop_a1h1(pos, s, c, ei);
371         }
372     }
373
374     // Sum up all attacked squares
375     ei.attackedBy[c][0] =   ei.attackedBy[c][PAWN]   | ei.attackedBy[c][KNIGHT]
376                           | ei.attackedBy[c][BISHOP] | ei.attackedBy[c][ROOK]
377                           | ei.attackedBy[c][QUEEN]  | ei.attackedBy[c][KING];
378   }
379
380   // Kings.  Kings are evaluated after all other pieces for both sides,
381   // because we need complete attack information for all pieces when computing
382   // the king safety evaluation.
383   for (Color c = WHITE; c <= BLACK; c++)
384       evaluate_king(pos, pos.king_square(c), c, ei);
385
386   // Evaluate passed pawns.  We evaluate passed pawns for both sides at once,
387   // because we need to know which side promotes first in positions where
388   // both sides have an unstoppable passed pawn.
389   if (ei.pi->passed_pawns())
390       evaluate_passed_pawns(pos, ei);
391
392   Phase phase = pos.game_phase();
393
394   // Middle-game specific evaluation terms
395   if (phase > PHASE_ENDGAME)
396   {
397     // Pawn storms in positions with opposite castling.
398     if (   square_file(pos.king_square(WHITE)) >= FILE_E
399         && square_file(pos.king_square(BLACK)) <= FILE_D)
400
401         ei.mgValue += ei.pi->queenside_storm_value(WHITE)
402                     - ei.pi->kingside_storm_value(BLACK);
403
404     else if (   square_file(pos.king_square(WHITE)) <= FILE_D
405              && square_file(pos.king_square(BLACK)) >= FILE_E)
406
407         ei.mgValue += ei.pi->kingside_storm_value(WHITE)
408                     - ei.pi->queenside_storm_value(BLACK);
409   }
410
411   // Mobility
412   ei.mgValue += apply_weight(ei.mgMobility, WeightMobilityMidgame);
413   ei.egValue += apply_weight(ei.egMobility, WeightMobilityEndgame);
414
415   // If we don't already have an unusual scale factor, check for opposite
416   // colored bishop endgames, and use a lower scale for those:
417   if (   phase < PHASE_MIDGAME
418       && pos.opposite_colored_bishops()
419       && (   (factor[WHITE] == SCALE_FACTOR_NORMAL && ei.egValue > Value(0))
420           || (factor[BLACK] == SCALE_FACTOR_NORMAL && ei.egValue < Value(0))))
421   {
422     if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == 2*BishopValueMidgame)
423     {
424       // Only the two bishops
425       if (pos.pawn_count(WHITE) + pos.pawn_count(BLACK) == 1)
426       {
427         // KBP vs KB with only a single pawn; almost certainly a draw.
428         if (factor[WHITE] == SCALE_FACTOR_NORMAL)
429             factor[WHITE] = ScaleFactor(8);
430         if (factor[BLACK] == SCALE_FACTOR_NORMAL)
431             factor[BLACK] = ScaleFactor(8);
432       }
433       else
434       {
435         // At least two pawns
436         if (factor[WHITE] == SCALE_FACTOR_NORMAL)
437             factor[WHITE] = ScaleFactor(32);
438         if (factor[BLACK] == SCALE_FACTOR_NORMAL)
439             factor[BLACK] = ScaleFactor(32);
440       }
441     }
442     else
443     {
444       // Endgame with opposite-colored bishops, but also other pieces.
445       // Still a bit drawish, but not as drawish as with only the two
446       // bishops.
447       if (factor[WHITE] == SCALE_FACTOR_NORMAL)
448           factor[WHITE] = ScaleFactor(50);
449       if (factor[BLACK] == SCALE_FACTOR_NORMAL)
450           factor[BLACK] = ScaleFactor(50);
451     }
452   }
453
454   // Interpolate between the middle game and the endgame score, and
455   // return
456   Color stm = pos.side_to_move();
457
458   Value v = Sign[stm] * scale_by_game_phase(ei.mgValue, ei.egValue, phase, factor);
459
460   return (ei.mateThreat[stm] == MOVE_NONE ? v : 8 * QueenValueMidgame - v);
461 }
462
463
464 /// quick_evaluate() does a very approximate evaluation of the current position.
465 /// It currently considers only material and piece square table scores.  Perhaps
466 /// we should add scores from the pawn and material hash tables?
467
468 Value quick_evaluate(const Position &pos) {
469   Color stm;
470   Value mgValue, egValue;
471   ScaleFactor factor[2] = {SCALE_FACTOR_NORMAL, SCALE_FACTOR_NORMAL};
472   Phase phase;
473
474   assert(pos.is_ok());
475
476   stm = pos.side_to_move();
477
478   mgValue = pos.mg_value();
479   egValue = pos.eg_value();
480   phase = pos.game_phase();
481
482   Value value = scale_by_game_phase(mgValue, egValue, phase, factor);
483
484   return Sign[stm] * value;
485 }
486
487
488 /// init_eval() initializes various tables used by the evaluation function.
489
490 void init_eval(int threads) {
491
492   assert(threads <= THREAD_MAX);
493
494   for (int i = 0; i < THREAD_MAX; i++)
495   {
496     if (i >= threads)
497     {
498         delete PawnTable[i];
499         delete MaterialTable[i];
500         PawnTable[i] = NULL;
501         MaterialTable[i] = NULL;
502         continue;
503     }
504     if (!PawnTable[i])
505         PawnTable[i] = new PawnInfoTable(PawnTableSize);
506     if (!MaterialTable[i])
507         MaterialTable[i] = new MaterialInfoTable(MaterialTableSize);
508   }
509
510   for (Bitboard b = 0ULL; b < 256ULL; b++)
511       BitCount8Bit[b] = count_1s(b);
512 }
513
514
515 /// quit_eval() releases heap-allocated memory at program termination.
516
517 void quit_eval() {
518   for(int i = 0; i < THREAD_MAX; i++) {
519     delete PawnTable[i];
520     delete MaterialTable[i];
521   }
522 }
523
524
525 /// read_weights() reads evaluation weights from the corresponding UCI
526 /// parameters.
527
528 void read_weights(Color us) {
529
530   WeightMobilityMidgame      = weight_option("Mobility (Middle Game)", WeightMobilityMidgameInternal);
531   WeightMobilityEndgame      = weight_option("Mobility (Endgame)", WeightMobilityEndgameInternal);
532   WeightPawnStructureMidgame = weight_option("Pawn Structure (Middle Game)", WeightPawnStructureMidgameInternal);
533   WeightPawnStructureEndgame = weight_option("Pawn Structure (Endgame)", WeightPawnStructureEndgameInternal);
534   WeightPassedPawnsMidgame   = weight_option("Passed Pawns (Middle Game)", WeightPassedPawnsMidgameInternal);
535   WeightPassedPawnsEndgame   = weight_option("Passed Pawns (Endgame)", WeightPassedPawnsEndgameInternal);
536
537   Color them = opposite_color(us);
538
539   WeightKingSafety[us]   = weight_option("Cowardice", WeightKingSafetyInternal);
540   WeightKingSafety[them] = weight_option("Aggressiveness", WeightKingOppSafetyInternal);
541
542   init_safety();
543 }
544
545
546 namespace {
547
548   // evaluate_common() computes terms common to all pieces attack
549
550   int evaluate_common(const Position&p, const Bitboard& b, Color us, EvalInfo& ei,
551                        int AttackWeight, const Value* mgBonus, const Value* egBonus,
552                        Square s = SQ_NONE, const Value* OutpostBonus = NULL) {
553
554     Color them = opposite_color(us);
555
556     // King attack
557     if (b & ei.kingZone[us])
558     {
559       ei.kingAttackersCount[us]++;
560       ei.kingAttackersWeight[us] += AttackWeight;
561       Bitboard bb = (b & ei.attackedBy[them][KING]);
562       if (bb)
563           ei.kingZoneAttacksCount[us] += count_1s_max_15(bb);
564     }
565
566     // Mobility
567     int mob = count_1s_max_15(b & ~p.pieces_of_color(us));
568     ei.mgMobility += Sign[us] * mgBonus[mob];
569     ei.egMobility += Sign[us] * egBonus[mob];
570
571     // Bishop and Knight outposts
572     if (!OutpostBonus || !p.square_is_weak(s, them))
573         return mob;
574
575     // Initial bonus based on square
576     Value v, bonus;
577     v = bonus = OutpostBonus[relative_square(us, s)];
578
579     // Increase bonus if supported by pawn, especially if the opponent has
580     // no minor piece which can exchange the outpost piece
581     if (v && (p.pawn_attacks(them, s) & p.pawns(us)))
582     {
583         bonus += v / 2;
584         if (   p.knight_count(them) == 0
585             && (SquaresByColorBB[square_color(s)] & p.bishops(them)) == EmptyBoardBB)
586             bonus += v;
587     }
588     ei.mgValue += Sign[us] * bonus;
589     ei.egValue += Sign[us] * bonus;
590     return mob;
591   }
592
593
594   // evaluate_knight() assigns bonuses and penalties to a knight of a given
595   // color on a given square.
596
597   void evaluate_knight(const Position &p, Square s, Color us, EvalInfo &ei) {
598
599     Bitboard b = p.knight_attacks(s);
600     ei.attackedBy[us][KNIGHT] |= b;
601
602     // King attack, mobility and outposts
603     evaluate_common(p, b, us, ei, KnightAttackWeight, MidgameKnightMobilityBonus,
604                     EndgameKnightMobilityBonus, s, KnightOutpostBonus);
605   }
606
607
608   // evaluate_bishop() assigns bonuses and penalties to a bishop of a given
609   // color on a given square.
610
611   void evaluate_bishop(const Position &p, Square s, Color us, EvalInfo &ei) {
612
613     Bitboard b = bishop_attacks_bb(s, p.occupied_squares() & ~p.queens(us));
614     ei.attackedBy[us][BISHOP] |= b;
615
616     // King attack, mobility and outposts
617     evaluate_common(p, b, us, ei, BishopAttackWeight, MidgameBishopMobilityBonus,
618                     EndgameBishopMobilityBonus, s, BishopOutpostBonus);
619   }
620
621
622   // evaluate_rook() assigns bonuses and penalties to a rook of a given
623   // color on a given square.
624
625   void evaluate_rook(const Position &p, Square s, Color us, EvalInfo &ei) {
626
627     //Bitboard b = p.rook_attacks(s);
628     Bitboard b = rook_attacks_bb(s, p.occupied_squares() & ~p.rooks_and_queens(us));
629     ei.attackedBy[us][ROOK] |= b;
630
631     // King attack and mobility
632     int mob = evaluate_common(p, b, us, ei, RookAttackWeight, MidgameRookMobilityBonus,
633                               EndgameRookMobilityBonus);
634
635     // Rook on 7th rank
636     Color them = opposite_color(us);
637
638     if (   relative_rank(us, s) == RANK_7
639         && relative_rank(us, p.king_square(them)) == RANK_8)
640     {
641         ei.mgValue += Sign[us] * MidgameRookOn7thBonus;
642         ei.egValue += Sign[us] * EndgameRookOn7thBonus;
643     }
644
645     // Open and half-open files
646     File f = square_file(s);
647     if (ei.pi->file_is_half_open(us, f))
648     {
649         if (ei.pi->file_is_half_open(them, f))
650         {
651             ei.mgValue += Sign[us] * RookOpenFileBonus;
652             ei.egValue += Sign[us] * RookOpenFileBonus;
653         }
654         else
655         {
656             ei.mgValue += Sign[us] * RookHalfOpenFileBonus;
657             ei.egValue += Sign[us] * RookHalfOpenFileBonus;
658         }
659     }
660
661     // Penalize rooks which are trapped inside a king. Penalize more if
662     // king has lost right to castle
663     if (mob > 6 || ei.pi->file_is_half_open(us, f))
664         return;
665
666     Square ksq = p.king_square(us);
667
668     if (    square_file(ksq) >= FILE_E
669         &&  square_file(s) > square_file(ksq)
670         && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
671     {
672         // Is there a half-open file between the king and the edge of the board?
673         if (!ei.pi->has_open_file_to_right(us, square_file(ksq)))
674             ei.mgValue -= p.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
675                                           : Sign[us] *  (TrappedRookPenalty - mob * 16);
676     }
677     else if (    square_file(ksq) <= FILE_D
678              &&  square_file(s) < square_file(ksq)
679              && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
680     {
681         // Is there a half-open file between the king and the edge of the board?
682         if (!ei.pi->has_open_file_to_left(us, square_file(ksq)))
683             ei.mgValue -= p.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
684                                           : Sign[us] * (TrappedRookPenalty - mob * 16);
685     }
686   }
687
688
689   // evaluate_queen() assigns bonuses and penalties to a queen of a given
690   // color on a given square.
691
692   void evaluate_queen(const Position &p, Square s, Color us, EvalInfo &ei) {
693
694     Bitboard b = p.queen_attacks(s);
695     ei.attackedBy[us][QUEEN] |= b;
696
697     // King attack and mobility
698     evaluate_common(p, b, us, ei, QueenAttackWeight, MidgameQueenMobilityBonus,
699                     EndgameQueenMobilityBonus);
700
701     // Queen on 7th rank
702     Color them = opposite_color(us);
703
704     if (   relative_rank(us, s) == RANK_7
705         && relative_rank(us, p.king_square(them)) == RANK_8)
706     {
707         ei.mgValue += Sign[us] * MidgameQueenOn7thBonus;
708         ei.egValue += Sign[us] * EndgameQueenOn7thBonus;
709     }
710   }
711
712
713   // evaluate_king() assigns bonuses and penalties to a king of a given
714   // color on a given square.
715
716   void evaluate_king(const Position &p, Square s, Color us, EvalInfo &ei) {
717
718     int shelter = 0, sign = Sign[us];
719
720     // King shelter.
721     if(relative_rank(us, s) <= RANK_4) {
722       Bitboard pawns = p.pawns(us) & this_and_neighboring_files_bb(s);
723       Rank r = square_rank(s);
724       for(int i = 0; i < 3; i++)
725         shelter += count_1s_8bit(pawns >> ((r+(i+1)*sign) * 8)) * (64>>i);
726       ei.mgValue += sign * Value(shelter);
727     }
728
729     // King safety.  This is quite complicated, and is almost certainly far
730     // from optimally tuned.
731     Color them = opposite_color(us);
732     if(p.queen_count(them) >= 1 && ei.kingAttackersCount[them] >= 2
733        && p.non_pawn_material(them) >= QueenValueMidgame + RookValueMidgame
734        && ei.kingZoneAttacksCount[them]) {
735
736       // Is it the attackers turn to move?
737       bool sente = (them == p.side_to_move());
738
739       // Find the attacked squares around the king which has no defenders
740       // apart from the king itself:
741       Bitboard undefended =
742         ei.attacked_by(them) & ~ei.attacked_by(us, PAWN)
743         & ~ei.attacked_by(us, KNIGHT) & ~ei.attacked_by(us, BISHOP)
744         & ~ei.attacked_by(us, ROOK) & ~ei.attacked_by(us, QUEEN)
745         & ei.attacked_by(us, KING);
746       Bitboard occ = p.occupied_squares(), b, b2;
747
748       // Initialize the 'attackUnits' variable, which is used later on as an
749       // index to the SafetyTable[] array.  The initial is based on the number
750       // and types of the attacking pieces, the number of attacked and
751       // undefended squares around the king, the square of the king, and the
752       // quality of the pawn shelter.
753       int attackUnits =
754         Min((ei.kingAttackersCount[them] * ei.kingAttackersWeight[them]) / 2, 25)
755         + (ei.kingZoneAttacksCount[them] + count_1s_max_15(undefended)) * 3
756         + InitKingDanger[relative_square(us, s)] - shelter / 32;
757
758       // Analyse safe queen contact checks:
759       b = undefended & ei.attacked_by(them, QUEEN) & ~p.pieces_of_color(them);
760       if(b) {
761         Bitboard attackedByOthers =
762           ei.attacked_by(them, PAWN) | ei.attacked_by(them, KNIGHT)
763           | ei.attacked_by(them, BISHOP) | ei.attacked_by(them, ROOK);
764         b &= attackedByOthers;
765         if(b) {
766           // The bitboard b now contains the squares available for safe queen
767           // contact checks.
768           int count = count_1s_max_15(b);
769           attackUnits += QueenContactCheckBonus * count * (sente? 2 : 1);
770
771           // Is there a mate threat?
772           if(QueenContactMates && !p.is_check()) {
773             Bitboard escapeSquares =
774               p.king_attacks(s) & ~p.pieces_of_color(us) & ~attackedByOthers;
775             while(b) {
776               Square from, to = pop_1st_bit(&b);
777               if(!(escapeSquares
778                    & ~queen_attacks_bb(to, occ & clear_mask_bb(s)))) {
779                 // We have a mate, unless the queen is pinned or there
780                 // is an X-ray attack through the queen.
781                 for(int i = 0; i < p.queen_count(them); i++) {
782                   from = p.queen_list(them, i);
783                   if(bit_is_set(p.queen_attacks(from), to)
784                      && !bit_is_set(p.pinned_pieces(them), from)
785                      && !(rook_attacks_bb(to, occ & clear_mask_bb(from))
786                           & p.rooks_and_queens(us))
787                      && !(rook_attacks_bb(to, occ & clear_mask_bb(from))
788                           & p.rooks_and_queens(us)))
789                     ei.mateThreat[them] = make_move(from, to);
790                 }
791               }
792             }
793           }
794         }
795       }
796
797       // Analyse safe rook contact checks:
798       if(RookContactCheckBonus) {
799         b = undefended & ei.attacked_by(them, ROOK) & ~p.pieces_of_color(them);
800         if(b) {
801           Bitboard attackedByOthers =
802             ei.attacked_by(them, PAWN) | ei.attacked_by(them, KNIGHT)
803             | ei.attacked_by(them, BISHOP) | ei.attacked_by(them, QUEEN);
804           b &= attackedByOthers;
805           if(b) {
806             int count = count_1s_max_15(b);
807             attackUnits += (RookContactCheckBonus * count * (sente? 2 : 1));
808           }
809         }
810       }
811
812       // Analyse safe distance checks:
813       if(QueenCheckBonus > 0 || RookCheckBonus > 0) {
814         b = p.rook_attacks(s) & ~p.pieces_of_color(them) & ~ei.attacked_by(us);
815
816         // Queen checks
817         b2 = b & ei.attacked_by(them, QUEEN);
818         if(b2) attackUnits += QueenCheckBonus * count_1s_max_15(b2);
819
820         // Rook checks
821         b2 = b & ei.attacked_by(them, ROOK);
822         if(b2) attackUnits += RookCheckBonus * count_1s_max_15(b2);
823       }
824       if(QueenCheckBonus > 0 || BishopCheckBonus > 0) {
825         b = p.bishop_attacks(s) & ~p.pieces_of_color(them) & ~ei.attacked_by(us);
826         // Queen checks
827         b2 = b & ei.attacked_by(them, QUEEN);
828         if(b2) attackUnits += QueenCheckBonus * count_1s_max_15(b2);
829
830         // Bishop checks
831         b2 = b & ei.attacked_by(them, BISHOP);
832         if(b2) attackUnits += BishopCheckBonus * count_1s_max_15(b2);
833       }
834       if(KnightCheckBonus > 0) {
835         b = p.knight_attacks(s) & ~p.pieces_of_color(them) & ~ei.attacked_by(us);
836         // Knight checks
837         b2 = b & ei.attacked_by(them, KNIGHT);
838         if(b2) attackUnits += KnightCheckBonus * count_1s_max_15(b2);
839       }
840
841       // Analyse discovered checks (only for non-pawns right now, consider
842       // adding pawns later).
843       if(DiscoveredCheckBonus) {
844         b = p.discovered_check_candidates(them) & ~p.pawns();
845         if(b)
846           attackUnits +=
847             DiscoveredCheckBonus * count_1s_max_15(b) * (sente? 2 : 1);
848       }
849
850       // Has a mate threat been found?  We don't do anything here if the
851       // side with the mating move is the side to move, because in that
852       // case the mating side will get a huge bonus at the end of the main
853       // evaluation function instead.
854       if(ei.mateThreat[them] != MOVE_NONE)
855         attackUnits += MateThreatBonus;
856
857       // Ensure that attackUnits is between 0 and 99, in order to avoid array
858       // out of bounds errors:
859       if(attackUnits < 0) attackUnits = 0;
860       if(attackUnits >= 100) attackUnits = 99;
861
862       // Finally, extract the king safety score from the SafetyTable[] array.
863       // Add the score to the evaluation, and also to ei.futilityMargin.  The
864       // reason for adding the king safety score to the futility margin is
865       // that the king safety scores can sometimes be very big, and that
866       // capturing a single attacking piece can therefore result in a score
867       // change far bigger than the value of the captured piece.
868       Value v = apply_weight(SafetyTable[attackUnits], WeightKingSafety[us]);
869       ei.mgValue -= sign * v;
870       if(us == p.side_to_move())
871         ei.futilityMargin += v;
872     }
873   }
874
875
876   // evaluate_passed_pawns() evaluates the passed pawns for both sides.
877
878   void evaluate_passed_pawns(const Position &pos, EvalInfo &ei) {
879     bool hasUnstoppable[2] = {false, false};
880     int movesToGo[2] = {100, 100};
881
882     for(Color us = WHITE; us <= BLACK; us++) {
883       Color them = opposite_color(us);
884       Square ourKingSq = pos.king_square(us);
885       Square theirKingSq = pos.king_square(them);
886       Bitboard b = ei.pi->passed_pawns() & pos.pawns(us), b2, b3, b4;
887
888       while(b) {
889         Square s = pop_1st_bit(&b);
890         assert(pos.piece_on(s) == pawn_of_color(us));
891         assert(pos.pawn_is_passed(us, s));
892
893         int r = int(relative_rank(us, s) - RANK_2);
894         int tr = Max(0, r * (r-1));
895         Square blockSq = s + pawn_push(us);
896
897         // Base bonus based on rank:
898         Value mbonus = Value(20 * tr);
899         Value ebonus = Value(10 + r * r * 10);
900
901         // Adjust bonus based on king proximity:
902         ebonus -= Value(square_distance(ourKingSq, blockSq) * 3 * tr);
903         ebonus -=
904           Value(square_distance(ourKingSq, blockSq + pawn_push(us)) * 1 * tr);
905         ebonus += Value(square_distance(theirKingSq, blockSq) * 6 * tr);
906
907         // If the pawn is free to advance, increase bonus:
908         if(pos.square_is_empty(blockSq)) {
909
910           b2 = squares_in_front_of(us, s);
911           b3 = b2 & ei.attacked_by(them);
912           b4 = b2 & ei.attacked_by(us);
913           if((b2 & pos.pieces_of_color(them)) == EmptyBoardBB) {
914             // There are no enemy pieces in the pawn's path!  Are any of the
915             // squares in the pawn's path attacked by the enemy?
916             if(b3 == EmptyBoardBB)
917               // No enemy attacks, huge bonus!
918               ebonus += Value(tr * ((b2 == b4)? 17 : 15));
919             else
920               // OK, there are enemy attacks.  Are those squares which are
921               // attacked by the enemy also attacked by us?  If yes, big bonus
922               // (but smaller than when there are no enemy attacks), if no,
923               // somewhat smaller bonus.
924               ebonus += Value(tr * (((b3 & b4) == b3)? 13 : 8));
925           }
926           else {
927             // There are some enemy pieces in the pawn's path.  While this is
928             // sad, we still assign a moderate bonus if all squares in the path
929             // which are either occupied by or attacked by enemy pieces are
930             // also attacked by us.
931             if(((b3 | (b2 & pos.pieces_of_color(them))) & ~b4) == EmptyBoardBB)
932               ebonus += Value(tr * 6);
933           }
934           // At last, add a small bonus when there are no *friendly* pieces
935           // in the pawn's path:
936           if((b2 & pos.pieces_of_color(us)) == EmptyBoardBB)
937             ebonus += Value(tr);
938         }
939
940         // If the pawn is supported by a friendly pawn, increase bonus.
941         b2 = pos.pawns(us) & neighboring_files_bb(s);
942         if(b2 & rank_bb(s))
943           ebonus += Value(r * 20);
944         else if(pos.pawn_attacks(them, s) & b2)
945           ebonus += Value(r * 12);
946
947         // If the other side has only a king, check whether the pawn is
948         // unstoppable:
949         if(pos.non_pawn_material(them) == Value(0)) {
950           Square qsq;
951           int d;
952
953           qsq = relative_square(us, make_square(square_file(s), RANK_8));
954           d = square_distance(s, qsq) - square_distance(theirKingSq, qsq)
955             + ((us == pos.side_to_move())? 0 : 1);
956
957           if(d < 0) {
958             int mtg = RANK_8 - relative_rank(us, s);
959             int blockerCount =
960               count_1s_max_15(squares_in_front_of(us,s)&pos.occupied_squares());
961             mtg += blockerCount;
962             d += blockerCount;
963             if(d < 0) {
964               hasUnstoppable[us] = true;
965               movesToGo[us] = Min(movesToGo[us], mtg);
966             }
967           }
968         }
969         // Rook pawns are a special case:  They are sometimes worse, and
970         // sometimes better than other passed pawns.  It is difficult to find
971         // good rules for determining whether they are good or bad.  For now,
972         // we try the following:  Increase the value for rook pawns if the
973         // other side has no pieces apart from a knight, and decrease the
974         // value if the other side has a rook or queen.
975         if(square_file(s) == FILE_A || square_file(s) == FILE_H) {
976           if(pos.non_pawn_material(them) == KnightValueMidgame
977              && pos.knight_count(them) == 1)
978             ebonus += ebonus / 4;
979           else if(pos.rooks_and_queens(them))
980             ebonus -= ebonus / 4;
981         }
982
983         // Add the scores for this pawn to the middle game and endgame eval.
984         ei.mgValue += apply_weight(Sign[us] * mbonus, WeightPassedPawnsMidgame);
985         ei.egValue += apply_weight(Sign[us] * ebonus, WeightPassedPawnsEndgame);
986       }
987     }
988
989     // Does either side have an unstoppable passed pawn?
990     if(hasUnstoppable[WHITE] && !hasUnstoppable[BLACK])
991       ei.egValue += UnstoppablePawnValue - Value(0x40 * movesToGo[WHITE]);
992     else if(hasUnstoppable[BLACK] && !hasUnstoppable[WHITE])
993       ei.egValue -= UnstoppablePawnValue - Value(0x40 * movesToGo[BLACK]);
994     else if(hasUnstoppable[BLACK] && hasUnstoppable[WHITE]) {
995       // Both sides have unstoppable pawns!  Try to find out who queens
996       // first.  We begin by transforming 'movesToGo' to the number of
997       // plies until the pawn queens for both sides:
998       movesToGo[WHITE] *= 2;
999       movesToGo[BLACK] *= 2;
1000       movesToGo[pos.side_to_move()]--;
1001
1002       // If one side queens at least three plies before the other, that
1003       // side wins:
1004       if(movesToGo[WHITE] <= movesToGo[BLACK] - 3)
1005         ei.egValue += UnstoppablePawnValue - Value(0x40 * (movesToGo[WHITE]/2));
1006       else if(movesToGo[BLACK] <= movesToGo[WHITE] - 3)
1007         ei.egValue -= UnstoppablePawnValue - Value(0x40 * (movesToGo[BLACK]/2));
1008
1009       // We could also add some rules about the situation when one side
1010       // queens exactly one ply before the other:  Does the first queen
1011       // check the opponent's king, or attack the opponent's queening square?
1012       // This is slightly tricky to get right, because it is possible that
1013       // the opponent's king has moved somewhere before the first pawn queens.
1014     }
1015   }
1016
1017
1018   // evaluate_trapped_bishop_a7h7() determines whether a bishop on a7/h7
1019   // (a2/h2 for black) is trapped by enemy pawns, and assigns a penalty
1020   // if it is.
1021
1022   void evaluate_trapped_bishop_a7h7(const Position &pos, Square s, Color us,
1023                                     EvalInfo &ei) {
1024
1025     assert(square_is_ok(s));
1026     assert(pos.piece_on(s) == bishop_of_color(us));
1027
1028     Square b6 = relative_square(us, (square_file(s) == FILE_A) ? SQ_B6 : SQ_G6);
1029     Square b8 = relative_square(us, (square_file(s) == FILE_A) ? SQ_B8 : SQ_G8);
1030
1031     if (   pos.piece_on(b6) == pawn_of_color(opposite_color(us))
1032         && pos.see(s, b6) < 0
1033         && pos.see(s, b8) < 0)
1034     {
1035         ei.mgValue -= Sign[us] * TrappedBishopA7H7Penalty;
1036         ei.egValue -= Sign[us] * TrappedBishopA7H7Penalty;
1037     }
1038   }
1039
1040
1041   // evaluate_trapped_bishop_a1h1() determines whether a bishop on a1/h1
1042   // (a8/h8 for black) is trapped by a friendly pawn on b2/g2 (b7/g7 for
1043   // black), and assigns a penalty if it is.  This pattern can obviously
1044   // only occur in Chess960 games.
1045
1046   void evaluate_trapped_bishop_a1h1(const Position &pos, Square s, Color us,
1047                                     EvalInfo &ei) {
1048     Piece pawn = pawn_of_color(us);
1049     Square b2, b3, c3;
1050
1051     assert(Chess960);
1052     assert(square_is_ok(s));
1053     assert(pos.piece_on(s) == bishop_of_color(us));
1054
1055     if(square_file(s) == FILE_A) {
1056       b2 = relative_square(us, SQ_B2);
1057       b3 = relative_square(us, SQ_B3);
1058       c3 = relative_square(us, SQ_C3);
1059     }
1060     else {
1061       b2 = relative_square(us, SQ_G2);
1062       b3 = relative_square(us, SQ_G3);
1063       c3 = relative_square(us, SQ_F3);
1064     }
1065
1066     if(pos.piece_on(b2) == pawn) {
1067       Value penalty;
1068
1069       if(!pos.square_is_empty(b3))
1070         penalty = 2*TrappedBishopA1H1Penalty;
1071       else if(pos.piece_on(c3) == pawn)
1072         penalty = TrappedBishopA1H1Penalty;
1073       else
1074         penalty = TrappedBishopA1H1Penalty / 2;
1075
1076       ei.mgValue -= Sign[us] * penalty;
1077       ei.egValue -= Sign[us] * penalty;
1078     }
1079
1080   }
1081
1082
1083   // apply_weight applies an evaluation weight to a value.
1084
1085   inline Value apply_weight(Value v, int w) {
1086     return (v*w) / 0x100;
1087   }
1088
1089
1090   // scale_by_game_phase interpolates between a middle game and an endgame
1091   // score, based on game phase.  It also scales the return value by a
1092   // ScaleFactor array.
1093
1094   Value scale_by_game_phase(Value mv, Value ev, Phase ph, ScaleFactor sf[]) {
1095
1096     assert(mv > -VALUE_INFINITE && mv < VALUE_INFINITE);
1097     assert(ev > -VALUE_INFINITE && ev < VALUE_INFINITE);
1098     assert(ph >= PHASE_ENDGAME && ph <= PHASE_MIDGAME);
1099
1100     ev = apply_scale_factor(ev, sf[(ev > Value(0) ? WHITE : BLACK)]);
1101
1102     // Linearized sigmoid interpolator
1103     int sph = int(ph);
1104     sph -= (64 - sph) / 4;
1105     sph = Min(PHASE_MIDGAME, Max(PHASE_ENDGAME, sph));
1106
1107     Value result = Value(int((mv * sph + ev * (128 - sph)) / 128));
1108
1109     return Value(int(result) & ~(GrainSize - 1));
1110   }
1111
1112
1113   // count_1s_8bit() counts the number of nonzero bits in the 8 least
1114   // significant bits of a Bitboard. This function is used by the king
1115   // shield evaluation.
1116
1117   int count_1s_8bit(Bitboard b) {
1118     return int(BitCount8Bit[b & 0xFF]);
1119   }
1120
1121
1122   // compute_weight() computes the value of an evaluation weight, by combining
1123   // an UCI-configurable weight with an internal weight.
1124
1125   int compute_weight(int uciWeight, int internalWeight) {
1126     uciWeight = (uciWeight * 0x100) / 100;
1127     return (uciWeight * internalWeight) / 0x100;
1128   }
1129
1130
1131   // helper used in read_weights()
1132   int weight_option(const std::string& opt, int weight) {
1133
1134     return compute_weight(get_option_value_int(opt), weight);
1135   }
1136
1137
1138   // init_safety() initizes the king safety evaluation, based on UCI
1139   // parameters.  It is called from read_weights().
1140
1141   void init_safety() {
1142
1143     QueenContactCheckBonus = get_option_value_int("Queen Contact Check Bonus");
1144     RookContactCheckBonus  = get_option_value_int("Rook Contact Check Bonus");
1145     QueenCheckBonus        = get_option_value_int("Queen Check Bonus");
1146     RookCheckBonus         = get_option_value_int("Rook Check Bonus");
1147     BishopCheckBonus       = get_option_value_int("Bishop Check Bonus");
1148     KnightCheckBonus       = get_option_value_int("Knight Check Bonus");
1149     DiscoveredCheckBonus   = get_option_value_int("Discovered Check Bonus");
1150     MateThreatBonus        = get_option_value_int("Mate Threat Bonus");
1151
1152     int maxSlope = get_option_value_int("King Safety Max Slope");
1153     int peak     = get_option_value_int("King Safety Max Value") * 256 / 100;
1154     double a     = get_option_value_int("King Safety Coefficient") / 100.0;
1155     double b     = get_option_value_int("King Safety X Intercept");
1156     bool quad    = (get_option_value_string("King Safety Curve") == "Quadratic");
1157     bool linear  = (get_option_value_string("King Safety Curve") == "Linear");
1158
1159     for (int i = 0; i < 100; i++)
1160     {
1161         if (i < b)
1162             SafetyTable[i] = Value(0);
1163         else if(quad)
1164             SafetyTable[i] = Value((int)(a * (i - b) * (i - b)));
1165         else if(linear)
1166             SafetyTable[i] = Value((int)(100 * a * (i - b)));
1167     }
1168
1169     for (int i = 0; i < 100; i++)
1170     {
1171         if (SafetyTable[i+1] - SafetyTable[i] > maxSlope)
1172             for (int j = i + 1; j < 100; j++)
1173                 SafetyTable[j] = SafetyTable[j-1] + Value(maxSlope);
1174
1175         if (SafetyTable[i]  > Value(peak))
1176             SafetyTable[i] = Value(peak);
1177     }
1178   }
1179
1180 }