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