]> git.sesse.net Git - stockfish/blob - src/tt.h
Fix a (bestValue == -VALUE_INFINITE) assert
[stockfish] / src / tt.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(TT_H_INCLUDED)
22 #define TT_H_INCLUDED
23
24 #include <iostream>
25
26 #include "depth.h"
27 #include "move.h"
28 #include "value.h"
29
30
31 ////
32 //// Types
33 ////
34
35
36 /// A simple fixed size hash table used to store pawns and material
37 /// configurations. It is basically just an array of Entry objects.
38 /// Without cluster concept or overwrite policy.
39
40 template<class Entry, int HashSize>
41 class SimpleHash {
42
43   SimpleHash(const SimpleHash&);
44   SimpleHash& operator=(const SimpleHash&);
45
46 public:
47   SimpleHash() {
48
49     entries = new (std::nothrow) Entry[HashSize];
50     if (!entries)
51     {
52         std::cerr << "Failed to allocate " << HashSize * sizeof(Entry)
53                   << " bytes for material hash table." << std::endl;
54         exit(EXIT_FAILURE);
55     }
56     memset(entries, 0, HashSize * sizeof(Entry));
57   }
58
59   ~SimpleHash() { delete [] entries; }
60
61   Entry* find(Key key) const { return entries + ((uint32_t)key & (HashSize - 1)); }
62
63 protected:
64   Entry* entries;
65 };
66
67
68 /// The TTEntry class is the class of transposition table entries
69 ///
70 /// A TTEntry needs 128 bits to be stored
71 ///
72 /// bit  0-31: key
73 /// bit 32-63: data
74 /// bit 64-79: value
75 /// bit 80-95: depth
76 /// bit 96-111: static value
77 /// bit 112-127: margin of static value
78 ///
79 /// the 32 bits of the data field are so defined
80 ///
81 /// bit  0-15: move
82 /// bit 16-20: not used
83 /// bit 21-22: value type
84 /// bit 23-31: generation
85
86 class TTEntry {
87
88 public:
89   void save(uint32_t k, Value v, ValueType t, Depth d, Move m, int g, Value statV, Value statM) {
90
91     key32        = (uint32_t)k;
92     move16       = (uint16_t)m;
93     valueType    = (uint8_t)t;
94     generation8  = (uint8_t)g;
95     value16      = (int16_t)v;
96     depth16      = (int16_t)d;
97     staticValue  = (int16_t)statV;
98     staticMargin = (int16_t)statM;
99   }
100   void set_generation(int g) { generation8 = (uint8_t)g; }
101
102   uint32_t key() const { return key32; }
103   Depth depth() const { return Depth(depth16); }
104   Move move() const { return Move(move16); }
105   Value value() const { return Value(value16); }
106   ValueType type() const { return ValueType(valueType); }
107   int generation() const { return generation8; }
108   Value static_value() const { return Value(staticValue); }
109   Value static_value_margin() const { return Value(staticMargin); }
110
111 private:
112   uint32_t key32;
113   uint16_t move16;
114   uint8_t valueType;
115   uint8_t generation8;
116   int16_t value16;
117   int16_t depth16;
118   int16_t staticValue;
119   int16_t staticMargin;
120 };
121
122
123 /// This is the number of TTEntry slots for each cluster
124 const int ClusterSize = 4;
125
126 /// TTCluster consists of ClusterSize number of TTEntries.
127 /// Size of TTCluster must not be bigger than a cache line size.
128 /// In case it is less, it should be padded to guarantee always aligned accesses.
129
130 struct TTCluster {
131   TTEntry data[ClusterSize];
132 };
133
134
135 /// The transposition table class. This is basically just a huge array
136 /// containing TTCluster objects, and a few methods for writing new entries
137 /// and reading new ones.
138
139 class TranspositionTable {
140
141   TranspositionTable(const TranspositionTable&);
142   TranspositionTable& operator=(const TranspositionTable&);
143
144 public:
145   TranspositionTable();
146   ~TranspositionTable();
147   void set_size(size_t mbSize);
148   void clear();
149   void store(const Key posKey, Value v, ValueType type, Depth d, Move m, Value statV, Value kingD);
150   TTEntry* retrieve(const Key posKey) const;
151   void new_search();
152   TTEntry* first_entry(const Key posKey) const;
153   void refresh(const TTEntry* tte) const;
154
155 private:
156   size_t size;
157   TTCluster* entries;
158   uint8_t generation; // To properly compare, size must be smaller then TT stored value
159 };
160
161 extern TranspositionTable TT;
162
163
164 /// TranspositionTable::first_entry returns a pointer to the first
165 /// entry of a cluster given a position. The lowest order bits of the key
166 /// are used to get the index of the cluster.
167
168 inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
169
170   return entries[((uint32_t)posKey) & (size - 1)].data;
171 }
172
173
174 /// TranspositionTable::refresh updates the 'generation' value of the TTEntry
175 /// to avoid aging. Normally called after a TT hit, before to return.
176
177 inline void TranspositionTable::refresh(const TTEntry* tte) const {
178
179   const_cast<TTEntry*>(tte)->set_generation(generation);
180 }
181
182 #endif // !defined(TT_H_INCLUDED)