]> git.sesse.net Git - stockfish/blob - src/tt.h
Better code for hash table generation
[stockfish] / src / tt.h
1 /*
2   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3   Copyright (C) 2004-2021 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 /// TTEntry struct is the 10 bytes transposition table entry, defined as below:
26 ///
27 /// key        16 bit
28 /// depth       8 bit
29 /// generation  5 bit
30 /// pv node     1 bit
31 /// bound type  2 bit
32 /// move       16 bit
33 /// value      16 bit
34 /// eval value 16 bit
35
36 struct TTEntry {
37
38   Move  move()  const { return (Move )move16; }
39   Value value() const { return (Value)value16; }
40   Value eval()  const { return (Value)eval16; }
41   Depth depth() const { return (Depth)depth8 + DEPTH_OFFSET; }
42   bool is_pv()  const { return (bool)(genBound8 & 0x4); }
43   Bound bound() const { return (Bound)(genBound8 & 0x3); }
44   void save(Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev);
45
46 private:
47   friend class TranspositionTable;
48
49   uint16_t key16;
50   uint8_t  depth8;
51   uint8_t  genBound8;
52   uint16_t move16;
53   int16_t  value16;
54   int16_t  eval16;
55 };
56
57
58 /// A TranspositionTable is an array of Cluster, of size clusterCount. Each
59 /// cluster consists of ClusterSize number of TTEntry. Each non-empty TTEntry
60 /// contains information on exactly one position. The size of a Cluster should
61 /// divide the size of a cache line for best performance, as the cacheline is
62 /// prefetched when possible.
63
64 class TranspositionTable {
65
66   static constexpr int ClusterSize = 3;
67
68   struct Cluster {
69     TTEntry entry[ClusterSize];
70     char padding[2]; // Pad to 32 bytes
71   };
72
73   static_assert(sizeof(Cluster) == 32, "Unexpected Cluster size");
74
75   // Constants used to refresh the hash table periodically
76   static constexpr unsigned GENERATION_BITS  = 3;                                // nb of bits reserved for other things
77   static constexpr int      GENERATION_DELTA = (1 << GENERATION_BITS);           // increment for generation field
78   static constexpr int      GENERATION_CYCLE = 255 + (1 << GENERATION_BITS);     // cycle length
79   static constexpr int      GENERATION_MASK  = (0xFF << GENERATION_BITS) & 0xFF; // mask to pull out generation number
80
81 public:
82  ~TranspositionTable() { aligned_large_pages_free(table); }
83   void new_search() { generation8 += GENERATION_DELTA; } // Lower bits are used for other things
84   TTEntry* probe(const Key key, bool& found) const;
85   int hashfull() const;
86   void resize(size_t mbSize);
87   void clear();
88
89   TTEntry* first_entry(const Key key) const {
90     return &table[mul_hi64(key, clusterCount)].entry[0];
91   }
92
93 private:
94   friend struct TTEntry;
95
96   size_t clusterCount;
97   Cluster* table;
98   uint8_t generation8; // Size must be not bigger than TTEntry::genBound8
99 };
100
101 extern TranspositionTable TT;
102
103 #endif // #ifndef TT_H_INCLUDED