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