How to efficiently generate individual moves from bitmasks of pieces?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

PinkPandaPresident
Posts: 5
Joined: Thu Jan 14, 2021 8:25 pm
Full name: Aditya Gupta

How to efficiently generate individual moves from bitmasks of pieces?

Post by PinkPandaPresident »

I have been trying to build a chess engine for a variant of chess called Monster Chess, in which white starts with just a king and 4 pawns but has two moves for every move black has - I posted about that here, if you are interested (link).

In any case I have been trying to implement simple bitboards to get some efficiency on my project. I have been following this tutorial: (link), which is sadly incomplete. As such I have one major question. Having generated a bit mask for, say, all the places any knight could jump to, how do we generate individual moves for each knight independently? I managed to follow this code - (link) - to do so for the king, but I don't see how we would extend the logic for when we have multiple pieces that can move, especially with pawns.

Any help much appreciated!
User avatar
hgm
Posts: 27808
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: How to efficiently generate individual moves from bitmasks of pieces?

Post by hgm »

You have to do that for every Knight separately.
PinkPandaPresident
Posts: 5
Joined: Thu Jan 14, 2021 8:25 pm
Full name: Aditya Gupta

Re: How to efficiently generate individual moves from bitmasks of pieces?

Post by PinkPandaPresident »

From the tutorial I was following - "However, it will be shown later how it is possible to use these algorithms for all the pieces of the same kind and color on the board at the same time."

Does such a method exist or is it just a phantom?
User avatar
hgm
Posts: 27808
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: How to efficiently generate individual moves from bitmasks of pieces?

Post by hgm »

Well, I have never used bitboards, but I don't see how doing all Knights at once could ever be better. For a single Knight the set of target squares can be a simple lookup in a 64-entry table indexed by square number. I already don't see how you could get the set of all squares attacked by a Knight more efficiently than doing just that, and OR them together. For getting all squares attacked by Pawns that could still make sense, because each Pawn has few moves, and you can have many Pawns. So you can attack left-forward and right-forward attacks by simple shifts and masking away of the edge wrap-arounds, and the OR together, and you would have done 8 Pawns. But Knights have 8 moves, and you have only 2 Knights. So it is much faster to work per piece than per move direction.

When you start already with bitboards for individual pieces, it seems silly to OR them together, because then you have to figure out later which target square is attacked by which Knight.
PinkPandaPresident
Posts: 5
Joined: Thu Jan 14, 2021 8:25 pm
Full name: Aditya Gupta

Re: How to efficiently generate individual moves from bitmasks of pieces?

Post by PinkPandaPresident »

Right, that makes a lot more sense. Thanks!
Gerd Isenberg
Posts: 2250
Joined: Wed Mar 08, 2006 8:47 pm
Location: Hattingen, Germany

Re: How to efficiently generate individual moves from bitmasks of pieces?

Post by Gerd Isenberg »

PinkPandaPresident wrote: Fri Jan 15, 2021 3:18 pm I have been trying to build a chess engine for a variant of chess called Monster Chess, in which white starts with just a king and 4 pawns but has two moves for every move black has - I posted about that here, if you are interested (link).

In any case I have been trying to implement simple bitboards to get some efficiency on my project. I have been following this tutorial: (link), which is sadly incomplete. As such I have one major question. Having generated a bit mask for, say, all the places any knight could jump to, how do we generate individual moves for each knight independently? I managed to follow this code - (link) - to do so for the king, but I don't see how we would extend the logic for when we have multiple pieces that can move, especially with pawns.

Any help much appreciated!
Inspired by hardware approaches, and to have a little bit more fun with bitboards, I proposed DirGolem, a branch-less pure calculation legal move target generator for all 16 directions suited for SIMD architectures like AVX2/512, intended to hide the latency of a prefetched TT probe. I admit that the 8 knight directions hurt a bit - that's the price for consistency. Of course HGM's proposal is simpler to implement and the hybrid approach with direction-wise pawn pushes and left/right attacks, and otherwise piece-wise lookups is the most common used.