]> git.sesse.net Git - stockfish/blob - src/material.h
Use intrinsic in pop_1st_bit() under MSVC 64 bits
[stockfish] / src / material.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(MATERIAL_H_INCLUDED)
21 #define MATERIAL_H_INCLUDED
22
23 #include "endgame.h"
24 #include "position.h"
25 #include "tt.h"
26
27 const int MaterialTableSize = 1024;
28
29 /// MaterialInfo is a class which contains various information about a
30 /// material configuration. It contains a material balance evaluation,
31 /// a function pointer to a special endgame evaluation function (which in
32 /// most cases is NULL, meaning that the standard evaluation function will
33 /// be used), and "scale factors" for black and white.
34 ///
35 /// The scale factors are used to scale the evaluation score up or down.
36 /// For instance, in KRB vs KR endgames, the score is scaled down by a factor
37 /// of 4, which will result in scores of absolute value less than one pawn.
38
39 class MaterialInfo {
40
41   friend class MaterialInfoTable;
42
43 public:
44   Score material_value() const;
45   ScaleFactor scale_factor(const Position& pos, Color c) const;
46   int space_weight() const;
47   Phase game_phase() const;
48   bool specialized_eval_exists() const;
49   Value evaluate(const Position& pos) const;
50
51 private:
52   Key key;
53   int16_t value;
54   uint8_t factor[2];
55   EndgameEvaluationFunctionBase* evaluationFunction;
56   EndgameScalingFunctionBase* scalingFunction[2];
57   int spaceWeight;
58   Phase gamePhase;
59 };
60
61 /// The MaterialInfoTable class represents a pawn hash table. The most important
62 /// method is get_material_info, which returns a pointer to a MaterialInfo object.
63 class EndgameFunctions;
64
65 class MaterialInfoTable : public SimpleHash<MaterialInfo, MaterialTableSize> {
66 public:
67   MaterialInfoTable();
68   ~MaterialInfoTable();
69   MaterialInfo* get_material_info(const Position& pos);
70   static Phase game_phase(const Position& pos);
71 private:
72   EndgameFunctions* funcs;
73 };
74
75
76 /// MaterialInfo::material_value simply returns the material balance
77 /// evaluation that is independent from game phase.
78
79 inline Score MaterialInfo::material_value() const {
80
81   return make_score(value, value);
82 }
83
84
85 /// MaterialInfo::scale_factor takes a position and a color as input, and
86 /// returns a scale factor for the given color. We have to provide the
87 /// position in addition to the color, because the scale factor need not
88 /// to be a constant: It can also be a function which should be applied to
89 /// the position. For instance, in KBP vs K endgames, a scaling function
90 /// which checks for draws with rook pawns and wrong-colored bishops.
91
92 inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) const {
93
94   if (scalingFunction[c] != NULL)
95   {
96       ScaleFactor sf = scalingFunction[c]->apply(pos);
97       if (sf != SCALE_FACTOR_NONE)
98           return sf;
99   }
100   return ScaleFactor(factor[c]);
101 }
102
103
104 /// MaterialInfo::space_weight() simply returns the weight for the space
105 /// evaluation for this material configuration.
106
107 inline int MaterialInfo::space_weight() const {
108
109   return spaceWeight;
110 }
111
112
113 /// MaterialInfo::game_phase() returns the game phase according
114 /// to this material configuration.
115
116 inline Phase MaterialInfo::game_phase() const {
117
118   return gamePhase;
119 }
120
121
122 /// MaterialInfo::specialized_eval_exists decides whether there is a
123 /// specialized evaluation function for the current material configuration,
124 /// or if the normal evaluation function should be used.
125
126 inline bool MaterialInfo::specialized_eval_exists() const {
127
128   return evaluationFunction != NULL;
129 }
130
131
132 /// MaterialInfo::evaluate applies a specialized evaluation function
133 /// to a given position object. It should only be called when
134 /// specialized_eval_exists() returns 'true'.
135
136 inline Value MaterialInfo::evaluate(const Position& pos) const {
137
138   return evaluationFunction->apply(pos);
139 }
140
141 #endif // !defined(MATERIAL_H_INCLUDED)