]> git.sesse.net Git - stockfish/blob - src/tt.h
Merge branch 'master' into clusterMergeMaster11
[stockfish] / src / tt.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2020 The Stockfish developers (see AUTHORS file)
4
5   Stockfish is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Stockfish is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef TT_H_INCLUDED
20 #define TT_H_INCLUDED
21
22 #include "misc.h"
23 #include "types.h"
24
25 namespace Cluster {
26   void init();
27 }
28 //void Cluster::init();
29
30 /// TTEntry struct is the 10 bytes transposition table entry, defined as below:
31 ///
32 /// key        16 bit
33 /// depth       8 bit
34 /// generation  5 bit
35 /// pv node     1 bit
36 /// bound type  2 bit
37 /// move       16 bit
38 /// value      16 bit
39 /// eval value 16 bit
40
41 struct TTEntry {
42
43   Move  move()  const { return (Move )move16; }
44   Value value() const { return (Value)value16; }
45   Value eval()  const { return (Value)eval16; }
46   Depth depth() const { return (Depth)depth8 + DEPTH_OFFSET; }
47   bool is_pv()  const { return (bool)(genBound8 & 0x4); }
48   Bound bound() const { return (Bound)(genBound8 & 0x3); }
49   void save(Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev);
50
51 private:
52   friend class TranspositionTable;
53   friend void Cluster::init();
54
55   uint16_t key16;
56   uint8_t  depth8;
57   uint8_t  genBound8;
58   uint16_t move16;
59   int16_t  value16;
60   int16_t  eval16;
61 };
62
63
64 /// A TranspositionTable is an array of Cluster, of size clusterCount. Each
65 /// cluster consists of ClusterSize number of TTEntry. Each non-empty TTEntry
66 /// contains information on exactly one position. The size of a Cluster should
67 /// divide the size of a cache line for best performance, as the cacheline is
68 /// prefetched when possible.
69
70 class TranspositionTable {
71
72   friend void Cluster::init();
73
74   static constexpr int ClusterSize = 3;
75
76   struct Cluster {
77     TTEntry entry[ClusterSize];
78     char padding[2]; // Pad to 32 bytes
79   };
80
81   static_assert(sizeof(Cluster) == 32, "Unexpected Cluster size");
82
83 public:
84  ~TranspositionTable() { aligned_ttmem_free(mem); }
85   void new_search() { generation8 += 8; } // Lower 3 bits are used by PV flag and Bound
86   TTEntry* probe(const Key key, bool& found) const;
87   int hashfull() const;
88   void resize(size_t mbSize);
89   void clear();
90
91   TTEntry* first_entry(const Key key) const {
92     return &table[mul_hi64(key, clusterCount)].entry[0];
93   }
94
95 private:
96   friend struct TTEntry;
97
98   size_t clusterCount;
99   Cluster* table;
100   void* mem;
101   uint8_t generation8; // Size must be not bigger than TTEntry::genBound8
102 };
103
104 extern TranspositionTable TT;
105
106 #endif // #ifndef TT_H_INCLUDED