]> git.sesse.net Git - stockfish/blob - src/tt.h
Small tweak to generate_castle_moves()
[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 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 kd) {
90
91       key32 = k;
92       data = (m & 0xFFFF) | (t << 21) | (g << 23);
93       value16     = (int16_t)v;
94       depth16     = (int16_t)d;
95       staticValue = (int16_t)statV;
96       staticValueMargin  = (int16_t)kd;
97   }
98   void set_generation(int g) { data = move() | (type() << 21) | (g << 23); }
99
100   uint32_t key() const { return key32; }
101   Depth depth() const { return Depth(depth16); }
102   Move move() const { return Move(data & 0xFFFF); }
103   Value value() const { return Value(value16); }
104   ValueType type() const { return ValueType((data >> 21) & 3); }
105   int generation() const { return data >> 23; }
106   Value static_value() const { return Value(staticValue); }
107   Value static_value_margin() const { return Value(staticValueMargin); }
108
109 private:
110   uint32_t key32;
111   uint32_t data;
112   int16_t value16;
113   int16_t depth16;
114   int16_t staticValue;
115   int16_t staticValueMargin;
116 };
117
118
119 /// This is the number of TTEntry slots for each cluster
120 const int ClusterSize = 4;
121
122 /// TTCluster consists of ClusterSize number of TTEntries.
123 /// Size of TTCluster must not be bigger than a cache line size.
124 /// In case it is less, it should be padded to guarantee always aligned accesses.
125
126 struct TTCluster {
127   TTEntry data[ClusterSize];
128 };
129
130
131 /// The transposition table class. This is basically just a huge array
132 /// containing TTCluster objects, and a few methods for writing new entries
133 /// and reading new ones.
134
135 class TranspositionTable {
136
137   TranspositionTable(const TranspositionTable&);
138   TranspositionTable& operator=(const TranspositionTable&);
139
140 public:
141   TranspositionTable();
142   ~TranspositionTable();
143   void set_size(size_t mbSize);
144   void clear();
145   void store(const Key posKey, Value v, ValueType type, Depth d, Move m, Value statV, Value kingD);
146   TTEntry* retrieve(const Key posKey) const;
147   void new_search();
148   TTEntry* first_entry(const Key posKey) const;
149   void refresh(const TTEntry* tte) const;
150
151 private:
152   size_t size;
153   TTCluster* entries;
154   uint8_t generation; // To properly compare, size must be smaller then TT stored value
155 };
156
157 extern TranspositionTable TT;
158
159
160 /// TranspositionTable::first_entry returns a pointer to the first
161 /// entry of a cluster given a position. The lowest order bits of the key
162 /// are used to get the index of the cluster.
163
164 inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
165
166   return entries[((uint32_t)posKey) & (size - 1)].data;
167 }
168
169
170 /// TranspositionTable::refresh updates the 'generation' value of the TTEntry
171 /// to avoid aging. Normally called after a TT hit, before to return.
172
173 inline void TranspositionTable::refresh(const TTEntry* tte) const {
174
175   const_cast<TTEntry*>(tte)->set_generation(generation);
176 }
177
178 #endif // !defined(TT_H_INCLUDED)