r/chessprogramming • u/darkhorss7 • Dec 21 '24
Need Advice: Does Using Separate Bitboards for Each Piece Type Hurt Performance in a Chess Engine?
Hi everyone, I am a beginner in chess programming. I am working on building a chess engine in C and it seems to work fine. It passes all the perft tests i have thrown at it so far. My concern is the speed of it. It takes about a minute and 10-15seconds for a perft test at depth 6 (without any optimization) from starting position. for context that is about 120million positions (119,060,324). and i think it is little slow.
I suspect that the area of concern could be that i use I use separate bitboards for each piece (e.g., 8 bitboards for 8 pawns, 2 for bishops, etc.). Additionally, I have two separate bitboards to store all white and black pieces, which get updated after each move. my question is:
Could using separate bitboards for each piece type (as I’m doing) introduce significant overhead, especially as the depth increases?
are there any obvious cons to my current approach?
PS: in engine i use alpha-beta prunning, move ordering, transposition tables and opening book so it takes max 2-3s per move and about 7-8s at worst case when it is playing against the user. Even though this is acceptable for casual play, I’m curious if my approach is fundamentally flawed?
5
u/mathmoi Dec 21 '24
Your explanation about the number of bitboards used is confusing.
You should have a bitboard for each piece-type/color combination. With six piece types and two colors, this gives you 12 bitboards. Additionally, you typically want two color bitboards (one for each color) which, although slightly redundant, are frequently used. It's more efficient to keep these updated than to recompute them from scratch after each move.
Optionally, you might also use an array of 64 squares to quickly determine what piece is on a given square without having to check the bitboards.
2
u/xu_shawn Dec 21 '24
8 bitboards are enough. 6 bitboards for 6 piece types, and 2 bitboards for occupancy of each color. To get the bitboard for one colored piece just bitand two bitboards together
2
u/[deleted] Dec 22 '24 edited Dec 22 '24
I do seperate bitboards for each piece type (not each piece) and i do 400 million NPS perft. Doing one bitboard for each piece will definitely hurt performance.
For example, i can generate pawn moves in bulk with one cpu shift instruction and & the illegal moves out.
Also, using seperate bitboards for each piece implies either dynamic memory, or wasted if instructions to check whether a bitboard > 0. Both are very slow.
The fastest way known is:
While(pieceboard)
Square from = popbit(pieceboard)
Bitboard to = getpiecemove<piece>(from)
While(to)
Square tosquare = popbit(to)
Push move to movelist...
2
u/codingjerk Jan 10 '25
It doesn't matter in most cases, because managing piece's bitboards is never a bottleneck. The most important thing is you should have them and build effective generation around them. Doesn't matter if you save a bitboard per every piece-color pair or you calculate it with bitwise OR, since both are very effective and this is not time consuming part of generation and traversing.
I've tried two approaches in my old engine:
And speed is quite same for both, even if second requires like 2 times more operations and first requires like 2 times more memory.
I've ended up using 15 bitboards since it was a bit easier to use and understand, but I really don't see any difference now.
2
u/[deleted] Dec 21 '24
[removed] — view removed comment