]> git.sesse.net Git - stockfish/blob - src/tt.h
Implement a fallback system when aspiration search fails low and we are out of time.
[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 Marco Costalba
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 "position.h"
30 #include "value.h"
31
32
33 ////
34 //// Types
35 ////
36
37 /// The TTEntry class is the class of transposition table entries
38 ///
39 /// A TTEntry needs 128 bits to be stored
40 ///
41 /// bit    0-63: key
42 /// bit   64-95: data
43 /// bit  96-111: value
44 /// bit 112-127: depth
45 ///
46 /// the 32 bits of the data field are so defined
47 ///
48 /// bit  0-16: move
49 /// bit 17-19: not used
50 /// bit 20-22: value type
51 /// bit 23-31: generation
52
53 class TTEntry {
54
55 public:
56   TTEntry();
57   TTEntry(Key k, Value v, ValueType t, Depth d, Move m, int generation);
58   Key key() const { return key_; }
59   Depth depth() const { return Depth(depth_); }
60   Move move() const { return Move(data & 0x1FFFF); }
61   Value value() const { return Value(value_); }
62   ValueType type() const { return ValueType((data >> 20) & 7); }
63   int generation() const { return (data >> 23); }
64
65 private:
66   Key key_;
67   uint32_t data;
68   int16_t value_;
69   int16_t depth_;
70 };
71
72 /// The transposition table class.  This is basically just a huge array
73 /// containing TTEntry objects, and a few methods for writing new entries
74 /// and reading new ones.
75
76 class TranspositionTable {
77
78 public:
79   TranspositionTable(unsigned mbSize);
80   ~TranspositionTable();
81   void set_size(unsigned mbSize);
82   void clear();
83   void store(const Position &pos, Value v, Depth d, Move m, ValueType type);
84   TTEntry* retrieve(const Position &pos) const;
85   void new_search();
86   void insert_pv(const Position &pos, Move pv[]);
87   int full();
88
89 private:
90   inline TTEntry* first_entry(const Position &pos) const;
91
92   unsigned size;
93   int writes;
94   TTEntry* entries;
95   uint8_t generation;
96 };
97
98
99 ////
100 //// Constants and variables
101 ////
102
103 // Default transposition table size, in megabytes:
104 const int TTDefaultSize = 32;
105
106
107 #endif // !defined(TT_H_INCLUDED)