@@ -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
319436constexpr 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
354477enum Move : int {
355478 MOVE_NONE,
356479 MOVE_NULL = 1 + (1 << SQUARE_BITS)
357480};
481+ #endif
358482
359483enum MoveType : int {
360484 NORMAL,
@@ -935,7 +1059,8 @@ inline PieceType gating_type(Move m) {
9351059}
9361060
9371061inline 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
9411066inline bool is_gating (Move m) {
@@ -947,16 +1072,22 @@ inline bool is_pass(Move m) {
9471072}
9481073
9491074constexpr 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
9531078template <MoveType T>
9541079inline 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
9581086constexpr 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
9621093constexpr Move reverse_move (Move m) {
@@ -965,7 +1096,11 @@ constexpr Move reverse_move(Move m) {
9651096
9661097template <MoveType T>
9671098constexpr 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
9711106constexpr PieceType dropped_piece_type (Move m) {
0 commit comments