From c34b4c1b036058b2906556850cc4ded6ececcf89 Mon Sep 17 00:00:00 2001 From: Aargh Rai Date: Thu, 16 Jul 2026 23:03:28 +0530 Subject: fixed pawn gen, onto the next perf --- src/engine/moves.c | 75 +++++++++++++++++++++++------------------------ src/engine/moves/knight.c | 6 ++-- src/engine/moves/pawn.c | 16 +++++++--- 3 files changed, 51 insertions(+), 46 deletions(-) (limited to 'src/engine') diff --git a/src/engine/moves.c b/src/engine/moves.c index 3aff11e..0c923a1 100644 --- a/src/engine/moves.c +++ b/src/engine/moves.c @@ -221,14 +221,17 @@ void perft_divide(position_t position, int depth) { moves_t moves; moves_init(&moves); get_legal_moves(&moves, position); + int nodes = 0; for (int i = 0; i < moves.length; i++) { position_t copy = position; position_make_move(©, moves.moves[i]); int c = count_positions(copy, depth - 1); + nodes += c; printf(" %c%d%c%d: %d\n", 'a' + (moves.moves[i].from % 8), 1 + (moves.moves[i].from / 8), 'a' + (moves.moves[i].to % 8), 1 + (moves.moves[i].to / 8), c); } + printf("\nNodes: %d\n", nodes); moves_deinit(moves); } @@ -238,19 +241,17 @@ bool test_perft_starting_position() { "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" ).position; int d; - d = count_positions(position, 0); printf("d0: %d\n", d); if (d != 1) return false; - d = count_positions(position, 1); printf("d1: %d\n", d); if (d != 20) return false; - d = count_positions(position, 2); printf("d2: %d\n", d); if (d != 400) return false; - d = count_positions(position, 3); printf("d3: %d\n", d); if (d != 8902) return false; - d = count_positions(position, 4); printf("d4: %d\n", d); if (d != 197281) return false; + d = count_positions(position, 0); if (d != 1) return false; + d = count_positions(position, 1); if (d != 20) return false; + d = count_positions(position, 2); if (d != 400) return false; + d = count_positions(position, 3); if (d != 8902) return false; + d = count_positions(position, 4); if (d != 197281) return false; position_t castle_pos = load_fen( "r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1" ).position; - int cp = count_positions(castle_pos, 1); - printf("R3K2R d1: %d (expected 26)\n", cp); - - d = count_positions(position, 5); printf("d5: %d\n", d); if (d != 4865609) return false; + d = count_positions(castle_pos, 1); if (d != 26) return false; + d = count_positions(position, 5); if (d != 4865609) return false; return true; } @@ -259,37 +260,33 @@ bool test_perft_kiwipete_position() { "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1" ).position; int d; - d = count_positions(position, 1); printf("d1: %d (expected 48)\n", d); if (d != 48) return false; - printf("--- Kiwipete d2 divide ---\n"); perft_divide(position, 2); - d = count_positions(position, 2); printf("d2: %d (expected 2039)\n", d); if (d != 2039) return false; + d = count_positions(position, 1); if (d != 48) return false; + d = count_positions(position, 2); if (d != 2039) return false; + d = count_positions(position, 3); if (d != 97862) return false; + d = count_positions(position, 4); if (d != 4085603) return false; - printf("\n--- Debug: investigate e5g6 ---\n"); - { - moves_t moves; - moves_init(&moves); - get_legal_moves(&moves, position); - for (int i = 0; i < moves.length; i++) { - if (moves.moves[i].from == 36 && moves.moves[i].to == 46) { - position_t copy = position; - position_make_move(©, moves.moves[i]); - printf("After Nxe5g6, turn=%d, is e8 attacked by white? %d\n", - copy.turn, square_attacked(copy, 60, WHITE_TURN)); - moves_t black_moves; - moves_init(&black_moves); - get_legal_moves(&black_moves, copy); - printf("Black has %d legal moves:\n", black_moves.length); - for (int j = 0; j < black_moves.length; j++) { - printf(" %c%d%c%d flags=%d\n", - 'a' + (black_moves.moves[j].from % 8), 1 + (black_moves.moves[j].from / 8), - 'a' + (black_moves.moves[j].to % 8), 1 + (black_moves.moves[j].to / 8), - black_moves.moves[j].flags); - } - moves_deinit(black_moves); - break; - } - } - moves_deinit(moves); - } + // my move gen too slow to verify this + // d = count_positions(position, 5); if (d != 193690690) return false; + + return true; +} + +bool test_perft_position3() { + position_t position = load_fen( + "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1" + ).position; + int d; + d = count_positions(position, 1); if (d != 14) return false; + d = count_positions(position, 2); if (d != 191) return false; + d = count_positions(position, 3); if (d != 2812) return false; + d = count_positions(position, 4); if (d != 43238) return false; + d = count_positions(position, 5); if (d != 674624) return false; + perft_divide(position, 6); + d = count_positions(position, 6); + printf("%d\n", d); + if (d != 11030083) return false; + d = count_positions(position, 7); if (d != 178633661) return false; + d = count_positions(position, 8); if (d != 3009794393) return false; return true; } diff --git a/src/engine/moves/knight.c b/src/engine/moves/knight.c index 64e44ed..4aca3ac 100644 --- a/src/engine/moves/knight.c +++ b/src/engine/moves/knight.c @@ -68,9 +68,9 @@ bitboard_t knight_moves[64] = { 1128098930098176ULL, 2257297371824128ULL, 4796069720358912ULL, - 4796069720358912ULL >> 1, - 4796069720358912ULL >> 2, - 4796069720358912ULL >> 3, + 4796069720358912ULL << 1, + 4796069720358912ULL << 2, + 4796069720358912ULL << 3, 4679521487814656ULL, 9077567998918656ULL, }; diff --git a/src/engine/moves/pawn.c b/src/engine/moves/pawn.c index 66b1d06..bc97fb7 100644 --- a/src/engine/moves/pawn.c +++ b/src/engine/moves/pawn.c @@ -34,6 +34,12 @@ bitboard_t get_black_pawn_forwardmoves(moves_t* moves, position_t position) { return x; } +bool pawn_in_range(int to, int next_from, int ub_escape, int turn) { + if (next_from == ub_escape) return false; + if (turn == WHITE_TURN) return to > next_from && to - next_from <= 16; + return to < next_from && next_from - to <= 16; +} + void get_pawn_moves(moves_t* moves, position_t position) { assert_valid_position(position); @@ -65,20 +71,23 @@ void get_pawn_moves(moves_t* moves, position_t position) { bitboard_t item = file_masks[i] & forward_moves; bitboard_t pawns = file_masks[i] & friendly_pawns; + int UB_ESCAPE = position.turn == WHITE_TURN ? 64 : -1; + int from = __builtin_ctzll(pawns); pawns &= (pawns - 1); int next_from = __builtin_ctzll(pawns); - if (pawns == 0) next_from = 64; // to work around UB + if (pawns == 0) next_from = UB_ESCAPE; while (item) { int to = __builtin_ctzll(item); - if (to > next_from) { + if (pawn_in_range(to, next_from, UB_ESCAPE, position.turn)) { from = next_from; pawns &= (pawns - 1); next_from = __builtin_ctzll(pawns); - if (pawns == 0) next_from = 64; + if (pawns == 0) next_from = UB_ESCAPE; } item &= (item - 1); + if (to >= 56 || to <= 7) { add_promote_moves(0); } else add_move(moves, from, to); @@ -128,7 +137,6 @@ void get_pawn_moves(moves_t* moves, position_t position) { } } - bitboard_t ATTACK_MASK; int shift; if (position.turn == WHITE_TURN) { -- cgit v1.2.3