]> git.sesse.net Git - stockfish/blob - src/pawns.h
Last touches in history.h
[stockfish] / src / pawns.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
21 #if !defined(PAWNS_H_INCLUDED)
22 #define PAWNS_H_INCLUDED
23
24 ////
25 //// Includes
26 ////
27
28 #include "bitboard.h"
29 #include "position.h"
30 #include "tt.h"
31 #include "value.h"
32
33
34 ////
35 //// Types
36 ////
37
38 const int PawnTableSize = 16384;
39
40 /// PawnInfo is a class which contains various information about a pawn
41 /// structure. Currently, it only includes a middle game and an end game
42 /// pawn structure evaluation, and a bitboard of passed pawns. We may want
43 /// to add further information in the future. A lookup to the pawn hash table
44 /// (performed by calling the get_pawn_info method in a PawnInfoTable object)
45 /// returns a pointer to a PawnInfo object.
46 class PawnInfo {
47
48   friend class PawnInfoTable;
49
50 public:
51   Score pawns_value() const;
52   Bitboard pawn_attacks(Color c) const;
53   Bitboard passed_pawns(Color c) const;
54   int file_is_half_open(Color c, File f) const;
55   int has_open_file_to_left(Color c, File f) const;
56   int has_open_file_to_right(Color c, File f) const;
57
58   template<Color Us>
59   Score king_shelter(const Position& pos, Square ksq);
60
61 private:
62   template<Color Us>
63   Score updateShelter(const Position& pos, Square ksq);
64
65   Key key;
66   Bitboard passedPawns[2];
67   Bitboard pawnAttacks[2];
68   Square kingSquares[2];
69   Score value;
70   int halfOpenFiles[2];
71   Score kingShelters[2];
72 };
73
74
75 /// The PawnInfoTable class represents a pawn hash table. The most important
76 /// method is get_pawn_info, which returns a pointer to a PawnInfo object.
77
78 class PawnInfoTable : public SimpleHash<PawnInfo, PawnTableSize> {
79
80   enum SideType { KingSide, QueenSide };
81
82 public:
83   PawnInfo* get_pawn_info(const Position& pos) const;
84   void prefetch(Key key) const;
85
86 private:
87   template<Color Us>
88   Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi) const;
89 };
90
91
92 ////
93 //// Inline functions
94 ////
95
96 inline void PawnInfoTable::prefetch(Key key) const {
97
98     unsigned index = unsigned(key & (PawnTableSize - 1));
99     PawnInfo* pi = entries + index;
100     ::prefetch((char*) pi);
101 }
102
103 inline Score PawnInfo::pawns_value() const {
104   return value;
105 }
106
107 inline Bitboard PawnInfo::pawn_attacks(Color c) const {
108   return pawnAttacks[c];
109 }
110
111 inline Bitboard PawnInfo::passed_pawns(Color c) const {
112   return passedPawns[c];
113 }
114
115 inline int PawnInfo::file_is_half_open(Color c, File f) const {
116   return (halfOpenFiles[c] & (1 << int(f)));
117 }
118
119 inline int PawnInfo::has_open_file_to_left(Color c, File f) const {
120   return halfOpenFiles[c] & ((1 << int(f)) - 1);
121 }
122
123 inline int PawnInfo::has_open_file_to_right(Color c, File f) const {
124   return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
125 }
126
127 /// PawnInfo::updateShelter() calculates and caches king shelter. It is called
128 /// only when king square changes, about 20% of total king_shelter() calls.
129 template<Color Us>
130 Score PawnInfo::updateShelter(const Position& pos, Square ksq) {
131
132   const int Shift = (Us == WHITE ? 8 : -8);
133
134   Bitboard pawns;
135   int r, shelter = 0;
136
137   if (relative_rank(Us, ksq) <= RANK_4)
138   {
139       pawns = pos.pieces(PAWN, Us) & this_and_neighboring_files_bb(ksq);
140       r = square_rank(ksq) * 8;
141       for (int i = 1; i < 4; i++)
142       {
143           r += Shift;
144           shelter += BitCount8Bit[(pawns >> r) & 0xFF] * (128 >> i);
145       }
146   }
147   kingSquares[Us] = ksq;
148   kingShelters[Us] = make_score(shelter, 0);
149   return kingShelters[Us];
150 }
151
152 template<Color Us>
153 inline Score PawnInfo::king_shelter(const Position& pos, Square ksq) {
154   return kingSquares[Us] == ksq ? kingShelters[Us] : updateShelter<Us>(pos, ksq);
155 }
156
157 #endif // !defined(PAWNS_H_INCLUDED)