From 8a59ec9844987ca493106465de9029b6ec59b574 Mon Sep 17 00:00:00 2001 From: chocolatebakery Date: Wed, 20 Mar 2024 21:06:22 -0100 Subject: [PATCH] Fix problems in Atomic, and adapt to SFNNv7 possibly SFNNv8 in the future --- .vscode/settings.json | 75 +++++++++++++++++++ src/movegen.cpp | 42 +++++++++-- .../layers/affine_transform_sparse_input.h | 1 + src/position.cpp | 15 +++- 4 files changed, 124 insertions(+), 9 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000000..a559e21727d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,75 @@ +{ + "files.associations": { + "algorithm": "cpp", + "bitset": "cpp", + "iterator": "cpp", + "xhash": "cpp", + "xmemory": "cpp", + "xtree": "cpp", + "xutility": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "deque": "cpp", + "exception": "cpp", + "format": "cpp", + "forward_list": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "list": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "optional": "cpp", + "ostream": "cpp", + "ratio": "cpp", + "set": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "string": "cpp", + "system_error": "cpp", + "thread": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "utility": "cpp", + "vector": "cpp", + "xfacet": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xstring": "cpp", + "xtr1common": "cpp" + } +} \ No newline at end of file diff --git a/src/movegen.cpp b/src/movegen.cpp index 30817c9f1e4..ac7f1593706 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -20,6 +20,7 @@ #include "movegen.h" #include "position.h" +#include "uci.h" namespace Stockfish { @@ -224,10 +225,29 @@ namespace { #ifdef ATOMIC if constexpr (V == ATOMIC_VARIANT) { - b1 &= (Type == CAPTURES || Type == NON_EVASIONS) ? target : ~adjacent_squares_bb(pos.pieces(Us, KING)); - b2 &= (Type == CAPTURES || Type == NON_EVASIONS) ? target : ~adjacent_squares_bb(pos.pieces(Us, KING)); + //b1 &= (Type == CAPTURES || Type == NON_EVASIONS) ? target : ~adjacent_squares_bb(pos.pieces(Us, KING)); + //b2 &= (Type == CAPTURES || Type == NON_EVASIONS) ? target : ~adjacent_squares_bb(pos.pieces(Us, KING)); + if (Type == EVASIONS) { + b1 = shift(pawnsOn7) & ~adjacent_squares_bb(pos.pieces(Us, KING)) & target & pos.pieces(Them); + b2 = shift(pawnsOn7) & ~adjacent_squares_bb(pos.pieces(Us, KING)) & target & pos.pieces(Them); + } + else if (Type == CAPTURES) { + b1 = shift(pawnsOn7) & target; + b2 = shift(pawnsOn7) & target; + } + else if (Type == NON_EVASIONS) { + b1 = shift(pawnsOn7) & target & pos.pieces(Them); + b2 = shift(pawnsOn7) & target & pos.pieces(Them); + } } #endif + +#ifdef ATOMIC + // Promotes only if promotion wins or explodes checkers + if (V == ATOMIC_VARIANT && pos.checkers()) + b3 = shift(pawnsOn7) & emptySquares & target; +#endif + #ifdef ANTI if constexpr (V == ANTI_VARIANT) b3 &= target; @@ -257,10 +277,22 @@ namespace { #ifdef ATOMIC if constexpr (V == ATOMIC_VARIANT) { - b1 &= (Type == CAPTURES || Type == NON_EVASIONS) ? target : ~adjacent_squares_bb(pos.pieces(Us, KING)); - b2 &= (Type == CAPTURES || Type == NON_EVASIONS) ? target : ~adjacent_squares_bb(pos.pieces(Us, KING)); + //b1 &= (Type == CAPTURES || Type == NON_EVASIONS) ? target : ~adjacent_squares_bb(pos.pieces(Us, KING)); + //b2 &= (Type == CAPTURES || Type == NON_EVASIONS) ? target : ~adjacent_squares_bb(pos.pieces(Us, KING)); + if (Type == EVASIONS) { + b1 = shift(pawnsNotOn7) & ~adjacent_squares_bb(pos.pieces(Us, KING)) & target & pos.pieces(Them); + b2 = shift(pawnsNotOn7) & ~adjacent_squares_bb(pos.pieces(Us, KING)) & target & pos.pieces(Them); + } + else if (Type == CAPTURES) { + b1 = shift(pawnsNotOn7) & target; + b2 = shift(pawnsNotOn7) & target; + } + else if (Type == NON_EVASIONS) { + b1 = shift(pawnsNotOn7) & target & pos.pieces(Them); + b2 = shift(pawnsNotOn7) & target & pos.pieces(Them); + } } -#endif +#endif while (b1) { diff --git a/src/nnue/layers/affine_transform_sparse_input.h b/src/nnue/layers/affine_transform_sparse_input.h index 3c7defcc42c..0a43490246c 100644 --- a/src/nnue/layers/affine_transform_sparse_input.h +++ b/src/nnue/layers/affine_transform_sparse_input.h @@ -28,6 +28,7 @@ #include "../nnue_common.h" #include "affine_transform.h" #include "simd.h" +#include "../../bitboard.h" /* This file contains the definition for a fully connected layer (aka affine transform) with block sparse input. diff --git a/src/position.cpp b/src/position.cpp index 02f29faa5d9..4a58587a175 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -874,7 +874,7 @@ bool Position::legal(Move m) const { Color us = sideToMove; Square from = from_sq(m); Square to = to_sq(m); - + assert(color_of(moved_piece(m)) == us); #ifdef ANTI // If a player can capture, that player must capture @@ -1255,10 +1255,15 @@ bool Position::pseudo_legal(const Move m) const { return true; if (capture(m)) { + + //Square capsq = type_of(m) == EN_PASSANT ? make_square(file_of(to), rank_of(from)) : to; // Capturing the opposing king or all checking pieces suffices. Bitboard blast = attacks_bb(KING, to, 0) & (pieces() ^ pieces(PAWN)); + //Bitboard b = pieces() ^ ((blast | capsq) | from); + if ((blast & square(~us)) || ~(checkers() & blast)) return true; + } } #endif @@ -1419,6 +1424,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) { assert(is_ok(m)); assert(&newSt != st); + #ifdef ANTI assert(!is_anti() || !givesCheck); #endif @@ -1714,8 +1720,9 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) { { if (Eval::useNNUE) { - dp.from[0] = SQ_NONE; + dp.piece[0] = pc; dp.to[0] = SQ_NONE; + dp.from[0] = from; } remove_piece(from); // Update material (hash key already updated) @@ -2254,7 +2261,7 @@ bool Position::see_ge(Move m, Bitboard& occupied, Value threshold) const { if (threshold > VALUE_ZERO) return false; - Bitboard occupied = pieces() ^ from; + occupied = pieces() ^ from; Bitboard attackers = attackers_to(to, occupied) & occupied & pieces(~stm) & ~pieces(KING); // Loop over attacking pieces @@ -2282,7 +2289,7 @@ bool Position::see_ge(Move m, Bitboard& occupied, Value threshold) const { if (is_extinction() && ! more_than_one(pieces(color_of(piece_on(from)), type_of(piece_on(from))))) { // Toggles to square occupancy in case of stm != sideToMove - Bitboard occupied = pieces() ^ from ^ to; + occupied = pieces() ^ from ^ to; if (type_of(m) == EN_PASSANT) occupied ^= make_square(file_of(to), rank_of(from)); if (attackers_to(to, occupied) & occupied & pieces(color_of(piece_on(from))))