Skip to content

Commit 9e4c78e

Browse files
committed
Separate and simplify antichess tablebase encoding and lookup official-stockfish#511
No functional change
1 parent d6ecfaa commit 9e4c78e

File tree

1 file changed

+53
-42
lines changed

1 file changed

+53
-42
lines changed

src/syzygy/tbprobe.cpp

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ const char* WdlSuffixes[SUBVARIANT_NB] = {
112112
#endif
113113
};
114114

115+
#ifdef ANTI
115116
const char* PawnlessWdlSuffixes[SUBVARIANT_NB] = {
116117
nullptr,
117118
#ifdef ANTI
@@ -166,7 +167,7 @@ const char* PawnlessWdlSuffixes[SUBVARIANT_NB] = {
166167
nullptr,
167168
#endif
168169
};
169-
170+
#endif
170171

171172
const char* DtzSuffixes[SUBVARIANT_NB] = {
172173
".rtbz",
@@ -223,6 +224,7 @@ const char* DtzSuffixes[SUBVARIANT_NB] = {
223224
#endif
224225
};
225226

227+
#ifdef ANTI
226228
const char* PawnlessDtzSuffixes[SUBVARIANT_NB] = {
227229
nullptr,
228230
#ifdef ANTI
@@ -277,6 +279,7 @@ const char* PawnlessDtzSuffixes[SUBVARIANT_NB] = {
277279
nullptr,
278280
#endif
279281
};
282+
#endif
280283

281284
// Each table has a set of flags: all of them refer to DTZ tables, the last one to WDL tables
282285
enum TBFlag { STM = 1, Mapped = 2, WinPlies = 4, LossPlies = 8, SingleValue = 128 };
@@ -397,7 +400,7 @@ TBEntry<WDL>::TBEntry(const std::string& code, Variant v) : TBEntry() {
397400
if (count == 1)
398401
numUniquePieces++;
399402
#ifdef ANTI
400-
else if (variant == ANTI_VARIANT && (count < minModePieces || !minModePieces))
403+
else if (main_variant(variant) == ANTI_VARIANT && (count < minModePieces || !minModePieces))
401404
minModePieces = count;
402405
#endif
403406
}
@@ -784,14 +787,15 @@ void HashTable::insert(const std::vector<PieceType>& w, const std::vector<PieceT
784787

785788
if (file.is_open()) // Only WDL file is checked
786789
file.close();
787-
else if (variant != CHESS_VARIANT && code.find("P") == std::string::npos &&
788-
PawnlessWdlSuffixes[variant])
790+
#ifdef ANTI
791+
else if (main_variant(variant) == ANTI_VARIANT && code.find("P") == std::string::npos)
789792
{
790793
TBFile pawnlessFile(code + PawnlessWdlSuffixes[variant]);
791794
if (!pawnlessFile.is_open()) // Only WDL file is checked
792795
return;
793796
pawnlessFile.close();
794797
}
798+
#endif
795799
else
796800
return;
797801

@@ -1120,8 +1124,8 @@ T do_probe_table(const Position& pos, TBEntry<Type>* entry, WDLScore wdl, ProbeS
11201124
//
11211125
// In case we have at least 3 unique pieces (inlcuded kings) we encode them
11221126
// together.
1123-
if (entry->numUniquePieces >= 3) {
1124-
1127+
if (entry->numUniquePieces >= 3)
1128+
{
11251129
int adjust1 = squares[1] > squares[0];
11261130
int adjust2 = (squares[2] > squares[0]) + (squares[2] > squares[1]);
11271131

@@ -1154,39 +1158,8 @@ T do_probe_table(const Position& pos, TBEntry<Type>* entry, WDLScore wdl, ProbeS
11541158
+ rank_of(squares[0]) * 7 * 6
11551159
+ (rank_of(squares[1]) - adjust1) * 6
11561160
+ (rank_of(squares[2]) - adjust2);
1157-
} else if (entry->numUniquePieces == 2) {
1158-
1159-
bool connectedKings = false;
1160-
#ifdef ATOMIC
1161-
connectedKings = connectedKings || entry->variant == ATOMIC_VARIANT;
1162-
#endif
1163-
#ifdef ANTI
1164-
connectedKings = connectedKings || main_variant(entry->variant) == ANTI_VARIANT;
1165-
#endif
1166-
1167-
if (connectedKings) {
1168-
int adjust = squares[1] > squares[0];
1169-
1170-
if (off_A1H8(squares[0]))
1171-
idx = MapA1D1D4[squares[0]] * 63
1172-
+ (squares[1] - adjust);
1173-
1174-
else if (off_A1H8(squares[1]))
1175-
idx = 6 * 63
1176-
+ rank_of(squares[0]) * 28
1177-
+ MapB1H1H7[squares[1]];
1178-
1179-
else
1180-
idx = 6 * 63 + 4 * 28
1181-
+ rank_of(squares[0]) * 7
1182-
+ (rank_of(squares[1]) - adjust);
1183-
} else
1184-
// We don't have at least 3 unique pieces, like in KRRvKBB, just map
1185-
// the kings.
1186-
idx = MapKK[MapA1D1D4[squares[0]]][squares[1]];
1187-
1188-
#ifdef ANTI
1189-
} else if (entry->variant == ANTI_VARIANT && entry->minModePieces == 2) {
1161+
#if defined(ATOMIC) || defined(ANTI)
1162+
} else if (entry->variant != CHESS_VARIANT && entry->numUniquePieces != 2 && entry->minModePieces == 2) {
11901163
if (Triangle[squares[0]] > Triangle[squares[1]])
11911164
std::swap(squares[0], squares[1]);
11921165

@@ -1209,8 +1182,7 @@ T do_probe_table(const Position& pos, TBEntry<Type>* entry, WDLScore wdl, ProbeS
12091182
}
12101183

12111184
idx = MapPP[Triangle[squares[0]]][squares[1]];
1212-
#endif
1213-
} else {
1185+
} else if (entry->variant != CHESS_VARIANT && entry->numUniquePieces != 2) {
12141186
for (int i = 1; i < d->groupLen[0]; ++i)
12151187
if (Triangle[squares[0]] > Triangle[squares[i]])
12161188
std::swap(squares[0], squares[i]);
@@ -1236,6 +1208,39 @@ T do_probe_table(const Position& pos, TBEntry<Type>* entry, WDLScore wdl, ProbeS
12361208

12371209
for (int i = 1; i < d->groupLen[0]; ++i)
12381210
idx += Binomial[i][MultTwist[squares[i]]];
1211+
#endif
1212+
} else {
1213+
bool connectedKings = false;
1214+
#ifdef ATOMIC
1215+
connectedKings = connectedKings || entry->variant == ATOMIC_VARIANT;
1216+
#endif
1217+
#ifdef ANTI
1218+
connectedKings = connectedKings || main_variant(entry->variant) == ANTI_VARIANT;
1219+
#endif
1220+
1221+
if (connectedKings)
1222+
{
1223+
int adjust = squares[1] > squares[0];
1224+
1225+
if (off_A1H8(squares[0]))
1226+
idx = MapA1D1D4[squares[0]] * 63
1227+
+ (squares[1] - adjust);
1228+
1229+
else if (off_A1H8(squares[1]))
1230+
idx = 6 * 63
1231+
+ rank_of(squares[0]) * 28
1232+
+ MapB1H1H7[squares[1]];
1233+
1234+
else
1235+
idx = 6 * 63 + 4 * 28
1236+
+ rank_of(squares[0]) * 7
1237+
+ (rank_of(squares[1]) - adjust);
1238+
}
1239+
else
1240+
// We don't have at least 3 unique pieces, like in KRRvKBB, just map
1241+
// the kings.
1242+
idx = MapKK[MapA1D1D4[squares[0]]][squares[1]];
1243+
12391244
}
12401245

12411246
encode_remaining:
@@ -1323,7 +1328,7 @@ void set_groups(TBEntry<Type>& e, PairsData* d, int order[], File f) {
13231328
// Standard or Atomic/Giveaway
13241329
idx *= (e.variant == CHESS_VARIANT) ? 462 : 518;
13251330
#ifdef ANTI
1326-
else if (e.variant == ANTI_VARIANT)
1331+
else if (main_variant(e.variant) == ANTI_VARIANT)
13271332
idx *= (e.minModePieces == 2) ? 278 : MultFactor[e.minModePieces - 1];
13281333
#endif
13291334
}
@@ -1648,6 +1653,7 @@ void* init(TBEntry<Type>& e, const Position& pos) {
16481653
#endif
16491654
};
16501655

1656+
#ifdef ANTI
16511657
constexpr uint8_t PAWNLESS_TB_MAGIC[SUBVARIANT_NB][2][4] = {
16521658
{
16531659
{ 0xD7, 0x66, 0x0C, 0xA5 },
@@ -1756,20 +1762,25 @@ void* init(TBEntry<Type>& e, const Position& pos) {
17561762
},
17571763
#endif
17581764
};
1765+
#endif
17591766

17601767
fname = e.key == pos.material_key() ? w + 'v' + b : b + 'v' + w;
17611768

17621769
const char** Suffixes = Type == WDL ? WdlSuffixes : DtzSuffixes;
1770+
#ifdef ANTI
17631771
const char** PawnlessSuffixes = Type == WDL ? PawnlessWdlSuffixes : PawnlessDtzSuffixes;
1772+
#endif
17641773
uint8_t* data = nullptr;
17651774
TBFile file(fname + Suffixes[e.variant]);
17661775

17671776
if (file.is_open())
17681777
data = file.map(&e.baseAddress, &e.mapping, TB_MAGIC[e.variant][Type == WDL]);
1778+
#ifdef ANTI
17691779
else if (fname.find("P") == std::string::npos && PawnlessSuffixes[e.variant]) {
17701780
TBFile pawnlessFile(fname + PawnlessSuffixes[e.variant]);
17711781
data = pawnlessFile.map(&e.baseAddress, &e.mapping, PAWNLESS_TB_MAGIC[e.variant][Type == WDL]);
17721782
}
1783+
#endif
17731784

17741785
if (data)
17751786
{

0 commit comments

Comments
 (0)