Skip to content

Commit 09e9d3e

Browse files
Fix cursed TB position output in DTZ (#1034)
Bench: 2482674
1 parent f27c57c commit 09e9d3e

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

deps/Fathom/tbprobe.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ SOFTWARE.
4040
#include "tbprobe.h"
4141

4242
#define TB_PIECES 7
43-
#define MAX_DTZ 100000
4443
#define TB_HASHBITS (TB_PIECES < 7 ? 11 : 12)
4544
#define TB_MAX_PIECE (TB_PIECES < 7 ? 254 : 650)
4645
#define TB_MAX_PAWN (TB_PIECES < 7 ? 256 : 861)
@@ -2266,7 +2265,7 @@ static int root_probe_dtz(const Pos *pos, bool hasRepeated, bool useRule50, stru
22662265

22672266
// The border between draw and win lies at rank 1 or rank 900, depending
22682267
// on whether the 50-move rule is used.
2269-
int bound = useRule50 ? (MAX_DTZ - 100) : 1;
2268+
int bound = useRule50 ? 900 : 1;
22702269

22712270
// Probe, rank and score each move.
22722271
TbMove rootMoves[TB_MAX_MOVES];
@@ -2302,18 +2301,13 @@ static int root_probe_dtz(const Pos *pos, bool hasRepeated, bool useRule50, stru
23022301
// Note that moves ranked 900 have dtz + cnt50 == 100, which in rare
23032302
// cases may be insufficient to win as dtz may be one off (see the
23042303
// comments before TB_probe_dtz()).
2305-
int r = v > 0 ? (v + cnt50 <= 99 && !hasRepeated ? MAX_DTZ : MAX_DTZ - (v + cnt50))
2306-
: v < 0 ? (-v * 2 + cnt50 < 100 ? -MAX_DTZ : -MAX_DTZ + (-v + cnt50))
2304+
int r = v > 0 ? (v + cnt50 <= 99 && !hasRepeated ? 1000 : 1000 - (v + cnt50))
2305+
: v < 0 ? (-v * 2 + cnt50 < 100 ? -1000 : -1000 + (-v + cnt50))
23072306
: 0;
23082307
m->tbRank = r;
23092308

2310-
// Determine the score to be displayed for this move. Assign at least
2311-
// 1 cp to cursed wins and let it grow to 49 cp as the position gets
2312-
// closer to a real win.
23132309
m->tbScore = r >= bound ? TB_VALUE_MATE - TB_MAX_MATE_PLY - 1
2314-
: r > 0 ? max( 3, r - (MAX_DTZ - 200)) * TB_VALUE_PAWN / 200
2315-
: r == 0 ? TB_VALUE_DRAW
2316-
: r > -bound ? min(-3, r + (MAX_DTZ - 200)) * TB_VALUE_PAWN / 200
2310+
: r > -bound ? TB_VALUE_DRAW
23172311
: -TB_VALUE_MATE + TB_MAX_MATE_PLY + 1;
23182312
}
23192313
return 1;
@@ -2324,7 +2318,7 @@ static int root_probe_dtz(const Pos *pos, bool hasRepeated, bool useRule50, stru
23242318
// A return value of 0 means that not all probes were successful.
23252319
int root_probe_wdl(const Pos *pos, bool useRule50, struct TbRootMoves *rm)
23262320
{
2327-
static int WdlToRank[] = { -MAX_DTZ, -MAX_DTZ + 101, 0, MAX_DTZ - 101, MAX_DTZ };
2321+
static int WdlToRank[] = { -1000, -899, 0, 899, 1000 };
23282322
static Value WdlToValue[] = {
23292323
-TB_VALUE_MATE + TB_MAX_MATE_PLY + 1,
23302324
TB_VALUE_DRAW - 2,

src/thread.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,20 @@ impl ThreadData {
231231
let mut upperbound = root_move.upperbound;
232232
let mut lowerbound = root_move.lowerbound;
233233

234-
if self.shared.root_in_tb.load(Ordering::Relaxed) && score.abs() <= Score::TB_WIN {
235-
score = root_move.tb_score;
236-
upperbound = false;
237-
lowerbound = false;
234+
if self.shared.root_in_tb.load(Ordering::Relaxed) {
235+
// Cursed win/loss due to 50-move rule
236+
let is_cursed = root_move.tb_rank > 0 && root_move.tb_rank < 900
237+
|| root_move.tb_rank < 0 && root_move.tb_rank > -900;
238+
239+
if is_cursed {
240+
upperbound = false;
241+
lowerbound = false;
242+
score = 0;
243+
} else if score.abs() <= Score::TB_WIN {
244+
upperbound = false;
245+
lowerbound = false;
246+
score = root_move.tb_score;
247+
}
238248
}
239249

240250
let mut formatted_score = match score.abs() {

0 commit comments

Comments
 (0)