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