From f1c6f59ae02a9d9db50171a69032dc2409a0e6c6 Mon Sep 17 00:00:00 2001 From: Aargh Rai Date: Thu, 16 Jul 2026 15:30:08 +0530 Subject: magic bitboards via AI --- src/engine/moves/bishop.c | 49 ++++++----------------------------------------- 1 file changed, 6 insertions(+), 43 deletions(-) (limited to 'src/engine/moves/bishop.c') diff --git a/src/engine/moves/bishop.c b/src/engine/moves/bishop.c index 0bc47f8..a7b2812 100644 --- a/src/engine/moves/bishop.c +++ b/src/engine/moves/bishop.c @@ -7,52 +7,15 @@ void __forloop_bishop_moves_gen( bitboard_t friendly_pieces, bitboard_t enemy_pieces ) { + bitboard_t all_occupied = friendly_pieces | enemy_pieces; while (friendly_type) { int from = __builtin_ctzll(friendly_type); friendly_type &= friendly_type - 1; - - int og_rank = from / 8; // range: [0, 7] - int og_file = from % 8; // range: [0, 7] - int rank = og_rank + 1, file = og_file + 1, index = rank * 8 + file; - - bitboard_t occupied; - while ((rank < 8 && file < 8) && !occupied_by(friendly_pieces, index)) { - occupied = occupied_by(enemy_pieces, index); - add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0); - if (occupied) { - break; - } - index = ++rank * 8 + ++file; - } - - rank = og_rank - 1, file = og_file - 1, index = rank * 8 + file; - while ((rank >= 0 && file >= 0) && !occupied_by(friendly_pieces, index)) { - occupied = occupied_by(enemy_pieces, index); - add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0); - if (occupied) { - break; - } - index = --rank * 8 + --file; - } - - rank = og_rank - 1, file = og_file + 1, index = rank * 8 + file; - while ((rank >= 0 && file < 8) && !occupied_by(friendly_pieces, index)) { - occupied = occupied_by(enemy_pieces, index); - add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0); - if (occupied) { - break; - } - index = --rank * 8 + ++file; - } - - rank = og_rank + 1, file = og_file - 1, index = rank * 8 + file; - while ((rank < 8 && file >= 0) && !occupied_by(friendly_pieces, index)) { - occupied = occupied_by(enemy_pieces, index); - add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0); - if (occupied) { - break; - } - index = ++rank * 8 + --file; + bitboard_t attacks = get_bishop_attacks(from, all_occupied) & ~friendly_pieces; + while (attacks) { + int to = __builtin_ctzll(attacks); + attacks &= attacks - 1; + add_move(moves, from, to, .flags = ((enemy_pieces >> to) & 1) ? MOVE_CAPTURE : 0); } } } -- cgit v1.2.3