~macaptain/lkdo-chess

b9aa8f5ceb7f60ac26b8707531ee0282ac5412dc — Michael Captain 3 years ago fc3ebd8
Add subOccupancy function for generating submasks
1 files changed, 20 insertions(+), 0 deletions(-)

M src/attack.zig
M src/attack.zig => src/attack.zig +20 -0
@@ 282,3 282,23 @@ fn numBishopRelativeOccupancies() [64]u64 {
        return occupancies;
    }
}

// Given a blockMask, a mask of rays containing potentially blocking pieces for
// a sliding piece, return a bitboard where 1s are a subset of the 1s in the
// mask. There are many such subsets, they are indexed by 'i'.
fn subOccupancy(blockMask: u64, i: u64) u64 {
    var blockBitCount = @popCount(u64, blockMask);
    var occupancy: u64 = 0;
    var mask = blockMask;
    var count: u6 = 0;

    while (count < blockBitCount) : (count += 1) {
        var lsbIndex = @intCast(u6, @ctz(u64, mask));
        mask = resetBit(mask, @intToEnum(Square, lsbIndex));

        if (i & (@as(u64, 1) << count) != 0) {
            occupancy |= @as(u64, 1) << lsbIndex;
        }
    }
    return occupancy;
}