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/knight.c | 6 +++--- src/engine/moves/pawn.c | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) (limited to 'src/engine/moves') 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