1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-07-13 12:39:16 +00:00

Cleanup probe_wdl_table()

Memory barrier is totally unused inside a locked scope.
This commit is contained in:
Marco Costalba 2016-04-21 14:47:33 +02:00
parent 1073ffb26a
commit c6e96881ca

View file

@ -658,7 +658,7 @@ void init_tb(const std::vector<PieceType>& pieces)
TBHash.insert(entry, key1); TBHash.insert(entry, key1);
if (key2 != key1) if (key2 != key1) // Asymmetric distribution
TBHash.insert(entry, key2); TBHash.insert(entry, key2);
} }
@ -1486,111 +1486,91 @@ std::string prt_str(Position& pos, bool mirror)
return s; return s;
} }
int probe_wdl_table(Position& pos, int *success) int probe_wdl_table(Position& pos, int* success)
{ {
uint64_t idx;
int i, res;
int p[TBPIECES];
Key key = pos.material_key(); Key key = pos.material_key();
if (pos.count<ALL_PIECES>(WHITE) + pos.count<ALL_PIECES>(BLACK) == 2) if (pos.count<ALL_PIECES>(WHITE) + pos.count<ALL_PIECES>(BLACK) == 2)
return 0; // KvK return 0; // KvK
TBEntry* ptr = TBHash[key]; TBEntry* ptr = TBHash[key];
if (!ptr) { if (!ptr) {
*success = 0; *success = 0;
return 0; return 0;
} }
// Init table at first access attempt
if (!ptr->ready) { if (!ptr->ready) {
TB_mutex.lock(); std::unique_lock<Mutex> lk(TB_mutex);
if (!ptr->ready) { if (!ptr->ready) {
std::string s = prt_str(pos, ptr->key != key); if (!init_table_wdl(ptr, prt_str(pos, ptr->key != key))) {
if (!init_table_wdl(ptr, s)) {
// Was ptr2->key = 0ULL; Just leave !ptr->ready condition // Was ptr2->key = 0ULL; Just leave !ptr->ready condition
*success = 0; *success = 0;
TB_mutex.unlock();
return 0; return 0;
} }
// Memory barrier to ensure ptr->ready = 1 is not reordered.
#ifdef _MSC_VER
_ReadWriteBarrier();
#else
__asm__ __volatile__ ("" ::: "memory");
#endif
ptr->ready = 1; ptr->ready = 1;
} }
TB_mutex.unlock();
} }
int bside, mirror, cmirror; int squares[TBPIECES];
int bside, smirror, cmirror;
if (!ptr->symmetric) { assert(key == ptr->key || !ptr->symmetric);
if (key != ptr->key) {
cmirror = 8; // Entries are stored from point of view of white, so in case of a symmetric
mirror = 070; // material distribution, we just need to lookup the relative TB entry in
bside = (pos.side_to_move() == WHITE); // case we are black. Instead in case of asymmetric distribution, because
} else { // stored entry is the same for both keys, we have first to verify if the
cmirror = mirror = 0; // entry is stored according to our key, otherwise we have to lookup
bside = !(pos.side_to_move() == WHITE); // the relative entry.
} if (ptr->symmetric) {
cmirror = pos.side_to_move() * 8;
smirror = pos.side_to_move() * 070;
bside = WHITE;
} else { } else {
cmirror = pos.side_to_move() == WHITE ? 0 : 8; cmirror = (key != ptr->key) * 8; // Switch color
mirror = pos.side_to_move() == WHITE ? 0 : 070; smirror = (key != ptr->key) * 070; // Vertical flip SQ_A1 -> SQ_A8
bside = 0; bside = (key != ptr->key) ^ pos.side_to_move();
} }
// p[i] is to contain the square 0-63 (A1-H8) for a piece of type // squares[i] is to contain the square 0-63 (A1-H8) for a piece of type
// pc[i] ^ cmirror, where 1 = white pawn, ..., 14 = black king. // pc[i] ^ cmirror, where 1 = white pawn, ..., 14 = black king.
// Pieces of the same type are guaranteed to be consecutive. // Pieces of the same type are guaranteed to be consecutive.
if (!ptr->has_pawns) { if (!ptr->has_pawns) {
TBEntry_piece *entry = (TBEntry_piece *)ptr; TBEntry_piece* entry = (TBEntry_piece*)ptr;
uint8_t *pc = entry->pieces[bside];
for (i = 0; i < entry->num;) { for (int i = 0; i < entry->num; ) {
Bitboard bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3), Piece pc = Piece(entry->pieces[bside][i] ^ cmirror);
(PieceType)(pc[i] & 7)); Bitboard b = pos.pieces(color_of(pc), type_of(pc));
do
do { squares[i++] = pop_lsb(&b);
p[i++] = pop_lsb(&bb); while (b);
} while (bb);
} }
idx = encode_piece(entry, entry->norm[bside], p, entry->factor[bside]); uint64_t idx = encode_piece(entry, entry->norm[bside], squares, entry->factor[bside]);
res = decompress_pairs(entry->precomp[bside], idx); return decompress_pairs(entry->precomp[bside], idx) - 2;
} else { } else {
TBEntry_pawn *entry = (TBEntry_pawn *)ptr; TBEntry_pawn* entry = (TBEntry_pawn*)ptr;
int k = entry->file[0].pieces[0][0] ^ cmirror; Piece pc = Piece(entry->file[0].pieces[0][0] ^ cmirror);
Bitboard bb = pos.pieces((Color)(k >> 3), (PieceType)(k & 7)); Bitboard b = pos.pieces(color_of(pc), type_of(pc));
i = 0; int i = 0;
do
squares[i++] = pop_lsb(&b) ^ smirror;
while (b);
do { int f = pawn_file(entry, squares);
p[i++] = pop_lsb(&bb) ^ mirror;
} while (bb);
int f = pawn_file(entry, p); for ( ; i < entry->num; ) {
uint8_t *pc = entry->file[f].pieces[bside]; pc = Piece(entry->file[f].pieces[bside][i] ^ cmirror);
b = pos.pieces(color_of(pc), type_of(pc));
for (; i < entry->num;) { do
bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3), squares[i++] = pop_lsb(&b) ^ smirror;
(PieceType)(pc[i] & 7)); while (b);
do {
p[i++] = pop_lsb(&bb) ^ mirror;
} while (bb);
} }
idx = encode_pawn(entry, entry->file[f].norm[bside], p, entry->file[f].factor[bside]); uint64_t idx = encode_pawn(entry, entry->file[f].norm[bside], squares, entry->file[f].factor[bside]);
res = decompress_pairs(entry->file[f].precomp[bside], idx); return decompress_pairs(entry->file[f].precomp[bside], idx) - 2;
} }
return (int)res - 2;
} }
int probe_dtz_table(Position& pos, int wdl, int *success) int probe_dtz_table(Position& pos, int wdl, int *success)