Skip to content

Commit c2c355f

Browse files
committed
Fix VLB move encoding and bitboard ops
1 parent 025a2e4 commit c2c355f

File tree

5 files changed

+159
-16
lines changed

5 files changed

+159
-16
lines changed

src/position.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,
268268
*/
269269

270270
unsigned char col, row, token;
271-
size_t idx;
272271
std::istringstream ss(fenStr);
273272

274273
std::memset(this, 0, sizeof(Position));

src/position.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,12 +1294,9 @@ inline Square Position::gate_square(Move m) const {
12941294
if (type_of(m) != CASTLING)
12951295
return from;
12961296
Square to = to_sq(m);
1297-
const unsigned mask = (1u << PIECE_TYPE_BITS) - 1u;
1298-
const unsigned gate_bits = static_cast<unsigned>(gating_square(m)) & mask;
1299-
if ((static_cast<unsigned>(from) & mask) == gate_bits)
1300-
return from;
1301-
if ((static_cast<unsigned>(to) & mask) == gate_bits)
1302-
return to;
1297+
Square gate = gating_square(m);
1298+
if (gate == from || gate == to)
1299+
return gate;
13031300
return from;
13041301
}
13051302
return gating_square(m);

src/tt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void TTEntry::save(Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev)
3737

3838
// Preserve any existing move for the same position
3939
if (m || (uint16_t)k != key16)
40-
move32 = (uint32_t)m;
40+
move32 = static_cast<TTMove>(m);
4141

4242
// Overwrite less valuable entries (cheapest checks first)
4343
if (b == BOUND_EXACT

src/tt.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424

2525
namespace Stockfish {
2626

27+
#if defined(VERY_LARGE_BOARDS)
28+
using TTMove = uint64_t;
29+
#else
30+
using TTMove = uint32_t;
31+
#endif
32+
2733
/// TTEntry struct is the 12 bytes transposition table entry, defined as below:
2834
///
2935
/// key 16 bit
@@ -37,7 +43,7 @@ namespace Stockfish {
3743

3844
struct TTEntry {
3945

40-
Move move() const { return (Move )move32; }
46+
Move move() const { return Move(move32); }
4147
Value value() const { return (Value)value16; }
4248
Value eval() const { return (Value)eval16; }
4349
Depth depth() const { return (Depth)depth8 + DEPTH_OFFSET; }
@@ -51,7 +57,7 @@ struct TTEntry {
5157
uint16_t key16;
5258
uint8_t depth8;
5359
uint8_t genBound8;
54-
uint32_t move32;
60+
TTMove move32;
5561
int16_t value16;
5662
int16_t eval16;
5763
};
@@ -65,11 +71,17 @@ struct TTEntry {
6571

6672
class TranspositionTable {
6773

74+
#if defined(VERY_LARGE_BOARDS)
75+
static constexpr int ClusterSize = 4;
76+
#else
6877
static constexpr int ClusterSize = 5;
78+
#endif
6979

7080
struct Cluster {
7181
TTEntry entry[ClusterSize];
82+
#if !defined(VERY_LARGE_BOARDS)
7283
char padding[4]; // Pad to 64 bytes
84+
#endif
7385
};
7486

7587
static_assert(sizeof(Cluster) == 64, "Unexpected Cluster size");

src/types.h

Lines changed: 141 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ struct Bitboard {
204204
}
205205

206206
constexpr bool operator == (const Bitboard y) const {
207-
return (b64[0] == y.b64[0]) && (b64[1] == y.b64[1]);
207+
return (b64[0] == y.b64[0]) && (b64[1] == y.b64[1]) && (b64[2] == y.b64[2]) && (b64[3] == y.b64[3]);
208208
}
209209

210210
constexpr bool operator != (const Bitboard y) const {
@@ -314,6 +314,123 @@ struct Bitboard {
314314
constexpr Bitboard() : b64 {0, 0} {}
315315
constexpr Bitboard(uint64_t i) : b64 {0, i} {}
316316
constexpr Bitboard(uint64_t hi, uint64_t lo) : b64 {hi, lo} {};
317+
318+
constexpr operator bool() const {
319+
return b64[0] || b64[1];
320+
}
321+
322+
constexpr operator long long unsigned () const {
323+
return b64[1];
324+
}
325+
326+
constexpr operator unsigned() const {
327+
return b64[1];
328+
}
329+
330+
constexpr Bitboard operator << (const unsigned int bits) const {
331+
if (bits == 0)
332+
return *this;
333+
if (bits >= 128)
334+
return Bitboard();
335+
if (bits >= 64)
336+
return Bitboard(b64[1] << (bits - 64), 0);
337+
return Bitboard((b64[0] << bits) | (b64[1] >> (64 - bits)), b64[1] << bits);
338+
}
339+
340+
constexpr Bitboard operator >> (const unsigned int bits) const {
341+
if (bits == 0)
342+
return *this;
343+
if (bits >= 128)
344+
return Bitboard();
345+
if (bits >= 64)
346+
return Bitboard(0, b64[0] >> (bits - 64));
347+
return Bitboard(b64[0] >> bits, (b64[0] << (64 - bits)) | (b64[1] >> bits));
348+
}
349+
350+
constexpr Bitboard operator << (const int bits) const {
351+
return *this << unsigned(bits);
352+
}
353+
354+
constexpr Bitboard operator >> (const int bits) const {
355+
return *this >> unsigned(bits);
356+
}
357+
358+
constexpr bool operator == (const Bitboard y) const {
359+
return (b64[0] == y.b64[0]) && (b64[1] == y.b64[1]);
360+
}
361+
362+
constexpr bool operator != (const Bitboard y) const {
363+
return !(*this == y);
364+
}
365+
366+
inline Bitboard& operator |=(const Bitboard x) {
367+
b64[0] |= x.b64[0];
368+
b64[1] |= x.b64[1];
369+
return *this;
370+
}
371+
inline Bitboard& operator &=(const Bitboard x) {
372+
b64[0] &= x.b64[0];
373+
b64[1] &= x.b64[1];
374+
return *this;
375+
}
376+
inline Bitboard& operator ^=(const Bitboard x) {
377+
b64[0] ^= x.b64[0];
378+
b64[1] ^= x.b64[1];
379+
return *this;
380+
}
381+
382+
constexpr Bitboard operator ~ () const {
383+
return Bitboard(~b64[0], ~b64[1]);
384+
}
385+
386+
constexpr Bitboard operator - () const {
387+
uint64_t n1 = 0ULL - b64[1];
388+
uint64_t borrow = b64[1] != 0;
389+
uint64_t n0 = 0ULL - b64[0] - borrow;
390+
return Bitboard(n0, n1);
391+
}
392+
393+
constexpr Bitboard operator | (const Bitboard x) const {
394+
return Bitboard(b64[0] | x.b64[0], b64[1] | x.b64[1]);
395+
}
396+
397+
constexpr Bitboard operator & (const Bitboard x) const {
398+
return Bitboard(b64[0] & x.b64[0], b64[1] & x.b64[1]);
399+
}
400+
401+
constexpr Bitboard operator ^ (const Bitboard x) const {
402+
return Bitboard(b64[0] ^ x.b64[0], b64[1] ^ x.b64[1]);
403+
}
404+
405+
constexpr Bitboard operator - (const Bitboard x) const {
406+
uint64_t r1 = b64[1] - x.b64[1];
407+
uint64_t borrow = b64[1] < x.b64[1];
408+
uint64_t r0 = b64[0] - x.b64[0] - borrow;
409+
return Bitboard(r0, r1);
410+
}
411+
412+
constexpr Bitboard operator - (const int x) const {
413+
return *this - Bitboard(x);
414+
}
415+
416+
inline Bitboard operator * (const Bitboard x) const {
417+
#if defined(__GNUC__) || defined(__clang__)
418+
unsigned __int128 lo = (unsigned __int128)b64[1] * x.b64[1];
419+
unsigned __int128 cross1 = (unsigned __int128)b64[1] * x.b64[0];
420+
unsigned __int128 cross2 = (unsigned __int128)b64[0] * x.b64[1];
421+
uint64_t r1 = uint64_t(lo);
422+
uint64_t r0 = uint64_t(lo >> 64);
423+
r0 += uint64_t(cross1);
424+
r0 += uint64_t(cross2);
425+
return Bitboard(r0, r1);
426+
#else
427+
Bitboard result;
428+
for (int i = 0; i < 128; ++i)
429+
if (((*this >> i) & Bitboard(1)))
430+
result |= x << i;
431+
return result;
432+
#endif
433+
}
317434
};
318435
#endif
319436
constexpr int SQUARE_BITS = 7;
@@ -351,10 +468,17 @@ constexpr int MAX_PLY = 246;
351468
/// any normal move destination square is always different from origin square
352469
/// while MOVE_NONE and MOVE_NULL have the same origin and destination square.
353470

471+
#if defined(VERY_LARGE_BOARDS)
472+
enum Move : uint64_t {
473+
MOVE_NONE,
474+
MOVE_NULL = 1 + (1ULL << SQUARE_BITS)
475+
};
476+
#else
354477
enum Move : int {
355478
MOVE_NONE,
356479
MOVE_NULL = 1 + (1 << SQUARE_BITS)
357480
};
481+
#endif
358482

359483
enum MoveType : int {
360484
NORMAL,
@@ -935,7 +1059,8 @@ inline PieceType gating_type(Move m) {
9351059
}
9361060

9371061
inline Square gating_square(Move m) {
938-
return Square((static_cast<uint32_t>(m) >> (2 * SQUARE_BITS + MOVE_TYPE_BITS + PIECE_TYPE_BITS)) & SQUARE_BIT_MASK);
1062+
const uint64_t raw = static_cast<uint64_t>(m);
1063+
return Square((raw >> (2 * SQUARE_BITS + MOVE_TYPE_BITS + PIECE_TYPE_BITS)) & SQUARE_BIT_MASK);
9391064
}
9401065

9411066
inline bool is_gating(Move m) {
@@ -947,16 +1072,22 @@ inline bool is_pass(Move m) {
9471072
}
9481073

9491074
constexpr Move make_move(Square from, Square to) {
950-
return Move((from << SQUARE_BITS) + to);
1075+
return Move((static_cast<uint64_t>(from) << SQUARE_BITS) + static_cast<uint64_t>(to));
9511076
}
9521077

9531078
template<MoveType T>
9541079
inline Move make(Square from, Square to, PieceType pt = NO_PIECE_TYPE) {
955-
return Move((pt << (2 * SQUARE_BITS + MOVE_TYPE_BITS)) + T + (from << SQUARE_BITS) + to);
1080+
return Move((static_cast<uint64_t>(pt) << (2 * SQUARE_BITS + MOVE_TYPE_BITS))
1081+
+ static_cast<uint64_t>(T)
1082+
+ (static_cast<uint64_t>(from) << SQUARE_BITS)
1083+
+ static_cast<uint64_t>(to));
9561084
}
9571085

9581086
constexpr Move make_drop(Square to, PieceType pt_in_hand, PieceType pt_dropped) {
959-
return Move((pt_in_hand << (2 * SQUARE_BITS + MOVE_TYPE_BITS + PIECE_TYPE_BITS)) + (pt_dropped << (2 * SQUARE_BITS + MOVE_TYPE_BITS)) + DROP + to);
1087+
return Move((static_cast<uint64_t>(pt_in_hand) << (2 * SQUARE_BITS + MOVE_TYPE_BITS + PIECE_TYPE_BITS))
1088+
+ (static_cast<uint64_t>(pt_dropped) << (2 * SQUARE_BITS + MOVE_TYPE_BITS))
1089+
+ static_cast<uint64_t>(DROP)
1090+
+ static_cast<uint64_t>(to));
9601091
}
9611092

9621093
constexpr Move reverse_move(Move m) {
@@ -965,7 +1096,11 @@ constexpr Move reverse_move(Move m) {
9651096

9661097
template<MoveType T>
9671098
constexpr Move make_gating(Square from, Square to, PieceType pt, Square gate) {
968-
return Move((gate << (2 * SQUARE_BITS + MOVE_TYPE_BITS + PIECE_TYPE_BITS)) + (pt << (2 * SQUARE_BITS + MOVE_TYPE_BITS)) + T + (from << SQUARE_BITS) + to);
1099+
return Move((static_cast<uint64_t>(gate) << (2 * SQUARE_BITS + MOVE_TYPE_BITS + PIECE_TYPE_BITS))
1100+
+ (static_cast<uint64_t>(pt) << (2 * SQUARE_BITS + MOVE_TYPE_BITS))
1101+
+ static_cast<uint64_t>(T)
1102+
+ (static_cast<uint64_t>(from) << SQUARE_BITS)
1103+
+ static_cast<uint64_t>(to));
9691104
}
9701105

9711106
constexpr PieceType dropped_piece_type(Move m) {

0 commit comments

Comments
 (0)