1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 16:53:09 +00:00

Assorted small stuff

This commit is contained in:
Marco Costalba 2016-05-09 12:52:43 +02:00
parent 56d1bef249
commit 2681419141
3 changed files with 35 additions and 49 deletions

View file

@ -268,7 +268,7 @@ void MainThread::search() {
TB::Cardinality = Options["SyzygyProbeLimit"]; TB::Cardinality = Options["SyzygyProbeLimit"];
// Skip TB probing when no TB found: !TBLargest -> !TB::Cardinality // Skip TB probing when no TB found: !TBLargest -> !TB::Cardinality
if (TB::Cardinality > TB::MaxCardinality) if (TB::Cardinality > int(TB::MaxCardinality))
{ {
TB::Cardinality = TB::MaxCardinality; TB::Cardinality = TB::MaxCardinality;
TB::ProbeDepth = DEPTH_ZERO; TB::ProbeDepth = DEPTH_ZERO;

View file

@ -12,9 +12,9 @@
#include <cstdint> #include <cstdint>
#include <cstring> // For std::memset #include <cstring> // For std::memset
#include <deque> #include <deque>
#include <list>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <list>
#include <sstream> #include <sstream>
#include <type_traits> #include <type_traits>
@ -42,7 +42,7 @@
using namespace Tablebases; using namespace Tablebases;
int Tablebases::MaxCardinality = 0; size_t Tablebases::MaxCardinality;
namespace { namespace {
@ -80,7 +80,7 @@ struct Atomic {
std::atomic_bool ready; std::atomic_bool ready;
}; };
struct WDLEntry : Atomic { struct WDLEntry : public Atomic {
WDLEntry(const Position& pos, Key keys[]); WDLEntry(const Position& pos, Key keys[]);
~WDLEntry(); ~WDLEntry();
bool init(const std::string& fname); bool init(const std::string& fname);
@ -215,10 +215,7 @@ template<typename T, int LE> T number(void* addr) {
class HashTable { class HashTable {
struct Entry { typedef std::pair<Key, WDLEntry*> Entry;
Key key;
WDLEntry* ptr;
};
static const int TBHASHBITS = 10; static const int TBHASHBITS = 10;
static const int HSHMAX = 5; static const int HSHMAX = 5;
@ -229,9 +226,8 @@ class HashTable {
Entry* entry = table[key >> (64 - TBHASHBITS)]; Entry* entry = table[key >> (64 - TBHASHBITS)];
for (int i = 0; i < HSHMAX; ++i, ++entry) for (int i = 0; i < HSHMAX; ++i, ++entry)
if (!entry->ptr || entry->key == key) { if (!entry->second || entry->first == key) {
entry->key = key; *entry = std::make_pair(key, ptr);
entry->ptr = ptr;
return; return;
} }
@ -244,8 +240,8 @@ public:
Entry* entry = table[key >> (64 - TBHASHBITS)]; Entry* entry = table[key >> (64 - TBHASHBITS)];
for (int i = 0; i < HSHMAX; ++i, ++entry) for (int i = 0; i < HSHMAX; ++i, ++entry)
if (entry->key == key) if (entry->first == key)
return entry->ptr; return entry->second;
return nullptr; return nullptr;
} }
@ -262,10 +258,9 @@ class TBFile : public std::ifstream {
std::string fname; std::string fname;
public: public:
// Open the file with the given name found among the TBPaths. TBPaths stores // Open the file with the given name found among the TBPaths directories
// the paths to directories where the .rtbw and .rtbz files can be found. // where the .rtbw and .rtbz files can be found. Multiple directories are
// Multiple directories are separated by ";" on Windows and by ":" on // separated by ";" on Windows and by ":" on Unix-based operating systems.
// Unix-based operating systems.
// //
// Example: // Example:
// C:\tb\wdl345;C:\tb\wdl6;D:\tb\dtz345;D:\tb\dtz6 // C:\tb\wdl345;C:\tb\wdl6;D:\tb\dtz345;D:\tb\dtz6
@ -282,7 +277,6 @@ public:
while (std::getline(ss, path, SepChar)) { while (std::getline(ss, path, SepChar)) {
fname = path + "/" + f; fname = path + "/" + f;
std::ifstream::open(fname); std::ifstream::open(fname);
if (is_open()) if (is_open())
return; return;
} }
@ -389,15 +383,12 @@ WDLEntry::~WDLEntry()
if (baseAddress) if (baseAddress)
TBFile::unmap(baseAddress, mapping); TBFile::unmap(baseAddress, mapping);
if (hasPawns) for (int i = 0; i < 2; ++i)
for (File f = FILE_A; f <= FILE_D; ++f) { if (hasPawns)
delete pawn.file[0][f].precomp; for (File f = FILE_A; f <= FILE_D; ++f)
delete pawn.file[1][f].precomp; delete pawn.file[i][f].precomp;
} else
else { delete piece[i].precomp;
delete piece[0].precomp;
delete piece[1].precomp;
}
} }
DTZEntry::DTZEntry(const WDLEntry& wdl, Key wdlKeys[]) DTZEntry::DTZEntry(const WDLEntry& wdl, Key wdlKeys[])
@ -432,10 +423,9 @@ DTZEntry::~DTZEntry()
delete piece.precomp; delete piece.precomp;
} }
// Given a position with 6 or fewer pieces, produce a text string // Given a position return a string of the form KQPvKRP, where KQP represents
// of the form KQPvKRP, where "KQP" represents the white pieces if // the white pieces if mirror == false and the black pieces if mirror == true.
// mirror == false and the black pieces if mirror == true. std::string str_code(const Position& pos, bool mirror = false)
std::string file_name(const Position& pos, bool mirror)
{ {
std::string w, b; std::string w, b;
@ -456,16 +446,14 @@ void HashTable::insert(const std::vector<PieceType>& pieces)
for (PieceType pt : pieces) for (PieceType pt : pieces)
code += PieceToChar[pt]; code += PieceToChar[pt];
int bk = code.find('K', 1); // Black king TBFile file(str_code(pos.set(code, WHITE, &st)) + ".rtbw");
TBFile f(code.substr(0, bk) + 'v' + code.substr(bk) + ".rtbw");
if (!f.is_open()) if (!file.is_open())
return; return;
f.close(); file.close();
if (int(pieces.size()) > Tablebases::MaxCardinality) MaxCardinality = std::max(pieces.size(), MaxCardinality);
Tablebases::MaxCardinality = pieces.size();
Key keys[] = { pos.set(code, WHITE, &st).material_key(), Key keys[] = { pos.set(code, WHITE, &st).material_key(),
pos.set(code, BLACK, &st).material_key() }; pos.set(code, BLACK, &st).material_key() };
@ -1127,7 +1115,7 @@ WDLScore probe_wdl_table(Position& pos, int* success)
if (!entry->ready.load(std::memory_order_acquire)) { if (!entry->ready.load(std::memory_order_acquire)) {
std::unique_lock<Mutex> lk(TB_mutex); std::unique_lock<Mutex> lk(TB_mutex);
if (!entry->ready.load(std::memory_order_relaxed)) { if (!entry->ready.load(std::memory_order_relaxed)) {
std::string fname = file_name(pos, entry->key != key) + ".rtbw"; std::string fname = str_code(pos, entry->key != key) + ".rtbw";
if (!entry->init(fname)) { if (!entry->init(fname)) {
// Was ptr2->key = 0ULL; Just leave !ptr->ready condition // Was ptr2->key = 0ULL; Just leave !ptr->ready condition
*success = 0; *success = 0;
@ -1167,7 +1155,7 @@ int probe_dtz_table(const Position& pos, WDLScore wdl, int *success)
StateInfo st; StateInfo st;
Position p; Position p;
std::string wdlCode = file_name(pos, wdlEntry->key != key); std::string wdlCode = str_code(pos, wdlEntry->key != key);
std::string fname = wdlCode + ".rtbz"; std::string fname = wdlCode + ".rtbz";
wdlCode.erase(wdlCode.find('v'), 1); wdlCode.erase(wdlCode.find('v'), 1);
@ -1533,8 +1521,8 @@ void Tablebases::init(const std::string& paths)
// Compute MapA1D1D4[] that encodes a square on the a1-d1-d4 triangle to 0..9 // Compute MapA1D1D4[] that encodes a square on the a1-d1-d4 triangle to 0..9
std::vector<Square> diagonal; std::vector<Square> diagonal;
code = 0; code = 0;
for (Square s = SQ_A1; s <= SQ_H8; ++s) for (Square s = SQ_A1; s <= SQ_D4; ++s)
if (off_A1H8(s) < 0 && file_of(s) <= FILE_D && rank_of(s) <= RANK_4) if (off_A1H8(s) < 0 && file_of(s) <= FILE_D)
MapA1D1D4[s] = code++; MapA1D1D4[s] = code++;
else if (!off_A1H8(s) && file_of(s) <= FILE_D) else if (!off_A1H8(s) && file_of(s) <= FILE_D)
@ -1544,16 +1532,15 @@ void Tablebases::init(const std::string& paths)
for (auto s : diagonal) for (auto s : diagonal)
MapA1D1D4[s] = code++; MapA1D1D4[s] = code++;
// Compute KK_idx[] that encodes all the 461 possible legal positions of a couple of // Compute MapKK[] that encodes all the 461 possible legal positions of a
// kings where first king is on a1-d1-d4 triangle. When first king is on the a1-d4 // couple of kings where the first is on a1-d1-d4 triangle. If first king is
// diagonal, second king is assumed not to be above the a1-h8 diagonal. // on the a1-d4 diagonal, the other is assumed not to be above the a1-h8 diagonal.
std::vector<std::pair<int, Square>> bothOnDiagonal; std::vector<std::pair<int, Square>> bothOnDiagonal;
code = 0; code = 0;
for (int idx = 0; idx < 10; idx++) for (int idx = 0; idx < 10; idx++)
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1) for (Square s1 = SQ_A1; s1 <= SQ_D4; ++s1)
if (idx == MapA1D1D4[s1] && (idx || s1 == SQ_B1)) // SQ_B1 is mapped to 0 if (idx == MapA1D1D4[s1] && (idx || s1 == SQ_B1)) // SQ_B1 is mapped to 0
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2) for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
{
if ((StepAttacksBB[KING][s1] | s1) & s2) // Illegal position if ((StepAttacksBB[KING][s1] | s1) & s2) // Illegal position
MapKK[idx][s2] = -1; MapKK[idx][s2] = -1;
@ -1565,7 +1552,6 @@ void Tablebases::init(const std::string& paths)
else else
MapKK[idx][s2] = code++; MapKK[idx][s2] = code++;
}
// Legal positions with both kings on diagonal are encoded as last ones // Legal positions with both kings on diagonal are encoded as last ones
for (auto p : bothOnDiagonal) for (auto p : bothOnDiagonal)

View file

@ -13,7 +13,7 @@ enum WDLScore {
WDLWin = 2, // Win WDLWin = 2, // Win
}; };
extern int MaxCardinality; extern size_t MaxCardinality;
void init(const std::string& paths); void init(const std::string& paths);
WDLScore probe_wdl(Position& pos, int* success); WDLScore probe_wdl(Position& pos, int* success);