mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 16:53:09 +00:00
Code style in tt.cpp
No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
a860576493
commit
c13b53a514
2 changed files with 73 additions and 92 deletions
50
src/tt.cpp
50
src/tt.cpp
|
@ -17,29 +17,18 @@
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
////
|
|
||||||
//// Includes
|
|
||||||
////
|
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
|
||||||
// The main transposition table
|
TranspositionTable TT; // Our global transposition table
|
||||||
TranspositionTable TT;
|
|
||||||
|
|
||||||
////
|
|
||||||
//// Functions
|
|
||||||
////
|
|
||||||
|
|
||||||
TranspositionTable::TranspositionTable() {
|
TranspositionTable::TranspositionTable() {
|
||||||
|
|
||||||
size = 0;
|
size = generation = 0;
|
||||||
entries = 0;
|
entries = NULL;
|
||||||
generation = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TranspositionTable::~TranspositionTable() {
|
TranspositionTable::~TranspositionTable() {
|
||||||
|
@ -48,7 +37,7 @@ TranspositionTable::~TranspositionTable() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::set_size sets the size of the transposition table,
|
/// TranspositionTable::set_size() sets the size of the transposition table,
|
||||||
/// measured in megabytes.
|
/// measured in megabytes.
|
||||||
|
|
||||||
void TranspositionTable::set_size(size_t mbSize) {
|
void TranspositionTable::set_size(size_t mbSize) {
|
||||||
|
@ -78,10 +67,9 @@ void TranspositionTable::set_size(size_t mbSize) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::clear overwrites the entire transposition table
|
/// TranspositionTable::clear() overwrites the entire transposition table
|
||||||
/// with zeroes. It is called whenever the table is resized, or when the
|
/// with zeroes. It is called whenever the table is resized, or when the
|
||||||
/// user asks the program to clear the table (from the UCI interface).
|
/// user asks the program to clear the table (from the UCI interface).
|
||||||
/// Perhaps we should also clear it when the "ucinewgame" command is received?
|
|
||||||
|
|
||||||
void TranspositionTable::clear() {
|
void TranspositionTable::clear() {
|
||||||
|
|
||||||
|
@ -89,23 +77,22 @@ void TranspositionTable::clear() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::store writes a new entry containing position key and
|
/// TranspositionTable::store() writes a new entry containing position key and
|
||||||
/// valuable information of current position.
|
/// valuable information of current position. The lowest order bits of position
|
||||||
/// The Lowest order bits of position key are used to decide on which cluster
|
/// key are used to decide on which cluster the position will be placed.
|
||||||
/// the position will be placed.
|
|
||||||
/// When a new entry is written and there are no empty entries available in cluster,
|
/// When a new entry is written and there are no empty entries available in cluster,
|
||||||
/// it replaces the least valuable of entries.
|
/// it replaces the least valuable of entries. A TTEntry t1 is considered to be
|
||||||
/// A TTEntry t1 is considered to be more valuable than a TTEntry t2 if t1 is from the
|
/// more valuable than a TTEntry t2 if t1 is from the current search and t2 is from
|
||||||
/// current search and t2 is from a previous search, or if the depth of t1
|
/// a previous search, or if the depth of t1 is bigger than the depth of t2.
|
||||||
/// is bigger than the depth of t2.
|
|
||||||
|
|
||||||
void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m, Value statV, Value kingD) {
|
void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m, Value statV, Value kingD) {
|
||||||
|
|
||||||
int c1, c2, c3;
|
int c1, c2, c3;
|
||||||
TTEntry *tte, *replace;
|
TTEntry *tte, *replace;
|
||||||
uint32_t posKey32 = posKey >> 32; // Use the high 32 bits as key
|
uint32_t posKey32 = posKey >> 32; // Use the high 32 bits as key inside the cluster
|
||||||
|
|
||||||
tte = replace = first_entry(posKey);
|
tte = replace = first_entry(posKey);
|
||||||
|
|
||||||
for (int i = 0; i < ClusterSize; i++, tte++)
|
for (int i = 0; i < ClusterSize; i++, tte++)
|
||||||
{
|
{
|
||||||
if (!tte->key() || tte->key() == posKey32) // Empty or overwrite old
|
if (!tte->key() || tte->key() == posKey32) // Empty or overwrite old
|
||||||
|
@ -118,10 +105,7 @@ void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replacing first entry is default and already set before entering for-loop
|
// Implement replace strategy
|
||||||
if (i == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
c1 = (replace->generation() == generation ? 2 : 0);
|
c1 = (replace->generation() == generation ? 2 : 0);
|
||||||
c2 = (tte->generation() == generation ? -2 : 0);
|
c2 = (tte->generation() == generation ? -2 : 0);
|
||||||
c3 = (tte->depth() < replace->depth() ? 1 : 0);
|
c3 = (tte->depth() < replace->depth() ? 1 : 0);
|
||||||
|
@ -133,9 +117,9 @@ void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::retrieve looks up the current position in the
|
/// TranspositionTable::retrieve() looks up the current position in the
|
||||||
/// transposition table. Returns a pointer to the TTEntry or NULL
|
/// transposition table. Returns a pointer to the TTEntry or NULL if
|
||||||
/// if position is not found.
|
/// position is not found.
|
||||||
|
|
||||||
TTEntry* TranspositionTable::retrieve(const Key posKey) const {
|
TTEntry* TranspositionTable::retrieve(const Key posKey) const {
|
||||||
|
|
||||||
|
|
113
src/tt.h
113
src/tt.h
|
@ -25,40 +25,8 @@
|
||||||
#include "move.h"
|
#include "move.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
/// A simple fixed size hash table used to store pawns and material
|
|
||||||
/// configurations. It is basically just an array of Entry objects.
|
|
||||||
/// Without cluster concept or overwrite policy.
|
|
||||||
|
|
||||||
template<class Entry, int HashSize>
|
/// The TTEntry is the class of transposition table entries
|
||||||
class SimpleHash {
|
|
||||||
|
|
||||||
SimpleHash(const SimpleHash&);
|
|
||||||
SimpleHash& operator=(const SimpleHash&);
|
|
||||||
|
|
||||||
public:
|
|
||||||
SimpleHash() {
|
|
||||||
|
|
||||||
entries = new (std::nothrow) Entry[HashSize];
|
|
||||||
if (!entries)
|
|
||||||
{
|
|
||||||
std::cerr << "Failed to allocate " << HashSize * sizeof(Entry)
|
|
||||||
<< " bytes for material hash table." << std::endl;
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
memset(entries, 0, HashSize * sizeof(Entry));
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~SimpleHash() { delete [] entries; }
|
|
||||||
|
|
||||||
Entry* find(Key key) const { return entries + ((uint32_t)key & (HashSize - 1)); }
|
|
||||||
void prefetch(Key key) const { ::prefetch((char*)find(key)); }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Entry* entries;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/// The TTEntry class is the class of transposition table entries
|
|
||||||
///
|
///
|
||||||
/// A TTEntry needs 128 bits to be stored
|
/// A TTEntry needs 128 bits to be stored
|
||||||
///
|
///
|
||||||
|
@ -93,41 +61,37 @@ public:
|
||||||
void set_generation(int g) { generation8 = (uint8_t)g; }
|
void set_generation(int g) { generation8 = (uint8_t)g; }
|
||||||
|
|
||||||
uint32_t key() const { return key32; }
|
uint32_t key() const { return key32; }
|
||||||
Depth depth() const { return Depth(depth16); }
|
Depth depth() const { return (Depth)depth16; }
|
||||||
Move move() const { return Move(move16); }
|
Move move() const { return (Move)move16; }
|
||||||
Value value() const { return Value(value16); }
|
Value value() const { return (Value)value16; }
|
||||||
ValueType type() const { return ValueType(valueType); }
|
ValueType type() const { return (ValueType)valueType; }
|
||||||
int generation() const { return generation8; }
|
int generation() const { return (int)generation8; }
|
||||||
Value static_value() const { return Value(staticValue); }
|
Value static_value() const { return (Value)staticValue; }
|
||||||
Value static_value_margin() const { return Value(staticMargin); }
|
Value static_value_margin() const { return (Value)staticMargin; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t key32;
|
uint32_t key32;
|
||||||
uint16_t move16;
|
uint16_t move16;
|
||||||
uint8_t valueType;
|
uint8_t valueType, generation8;
|
||||||
uint8_t generation8;
|
int16_t value16, depth16, staticValue, staticMargin;
|
||||||
int16_t value16;
|
|
||||||
int16_t depth16;
|
|
||||||
int16_t staticValue;
|
|
||||||
int16_t staticMargin;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// This is the number of TTEntry slots for each cluster
|
/// This is the number of TTEntry slots for each cluster
|
||||||
const int ClusterSize = 4;
|
const int ClusterSize = 4;
|
||||||
|
|
||||||
/// TTCluster consists of ClusterSize number of TTEntries.
|
|
||||||
/// Size of TTCluster must not be bigger than a cache line size.
|
/// TTCluster consists of ClusterSize number of TTEntries. Size of TTCluster
|
||||||
/// In case it is less, it should be padded to guarantee always aligned accesses.
|
/// must not be bigger than a cache line size. In case it is less, it should
|
||||||
|
/// be padded to guarantee always aligned accesses.
|
||||||
|
|
||||||
struct TTCluster {
|
struct TTCluster {
|
||||||
TTEntry data[ClusterSize];
|
TTEntry data[ClusterSize];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// The transposition table class. This is basically just a huge array
|
/// The transposition table class. This is basically just a huge array containing
|
||||||
/// containing TTCluster objects, and a few methods for writing new entries
|
/// TTCluster objects, and a few methods for writing and reading entries.
|
||||||
/// and reading new ones.
|
|
||||||
|
|
||||||
class TranspositionTable {
|
class TranspositionTable {
|
||||||
|
|
||||||
|
@ -148,15 +112,15 @@ public:
|
||||||
private:
|
private:
|
||||||
size_t size;
|
size_t size;
|
||||||
TTCluster* entries;
|
TTCluster* entries;
|
||||||
uint8_t generation; // To properly compare, size must be smaller then TT stored value
|
uint8_t generation; // Size must be not bigger then TTEntry::generation8
|
||||||
};
|
};
|
||||||
|
|
||||||
extern TranspositionTable TT;
|
extern TranspositionTable TT;
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::first_entry returns a pointer to the first
|
/// TranspositionTable::first_entry() returns a pointer to the first entry of
|
||||||
/// entry of a cluster given a position. The lowest order bits of the key
|
/// a cluster given a position. The lowest order bits of the key are used to
|
||||||
/// are used to get the index of the cluster.
|
/// get the index of the cluster.
|
||||||
|
|
||||||
inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
|
inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
|
||||||
|
|
||||||
|
@ -164,12 +128,45 @@ inline TTEntry* TranspositionTable::first_entry(const Key posKey) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::refresh updates the 'generation' value of the TTEntry
|
/// TranspositionTable::refresh() updates the 'generation' value of the TTEntry
|
||||||
/// to avoid aging. Normally called after a TT hit, before to return.
|
/// to avoid aging. Normally called after a TT hit.
|
||||||
|
|
||||||
inline void TranspositionTable::refresh(const TTEntry* tte) const {
|
inline void TranspositionTable::refresh(const TTEntry* tte) const {
|
||||||
|
|
||||||
const_cast<TTEntry*>(tte)->set_generation(generation);
|
const_cast<TTEntry*>(tte)->set_generation(generation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// A simple fixed size hash table used to store pawns and material
|
||||||
|
/// configurations. It is basically just an array of Entry objects.
|
||||||
|
/// Without cluster concept or overwrite policy.
|
||||||
|
|
||||||
|
template<class Entry, int HashSize>
|
||||||
|
class SimpleHash {
|
||||||
|
|
||||||
|
SimpleHash(const SimpleHash&);
|
||||||
|
SimpleHash& operator=(const SimpleHash&);
|
||||||
|
|
||||||
|
public:
|
||||||
|
SimpleHash() {
|
||||||
|
|
||||||
|
entries = new (std::nothrow) Entry[HashSize];
|
||||||
|
if (!entries)
|
||||||
|
{
|
||||||
|
std::cerr << "Failed to allocate " << HashSize * sizeof(Entry)
|
||||||
|
<< " bytes for material hash table." << std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
memset(entries, 0, HashSize * sizeof(Entry));
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~SimpleHash() { delete [] entries; }
|
||||||
|
|
||||||
|
Entry* find(Key key) const { return entries + ((uint32_t)key & (HashSize - 1)); }
|
||||||
|
void prefetch(Key key) const { ::prefetch((char*)find(key)); }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Entry* entries;
|
||||||
|
};
|
||||||
|
|
||||||
#endif // !defined(TT_H_INCLUDED)
|
#endif // !defined(TT_H_INCLUDED)
|
||||||
|
|
Loading…
Add table
Reference in a new issue