Answers.Body: <p>Given there is a maximum of 16 pieces on the chessboard you could loop on the pieces instead of the 64 squares. In this way, the less pieces on the board the faster.
  Each piece contains the index of the square, you then only have to pick the value of the corresponding weight to have your score. As each piece could have their own array of weight you can even pre compute the values on the weight adding the piece value and the position.
  If the performances really matter, use a profiler to know where is the bottleneck. You might end up using integers instead of floats, pointers instead of indexes, masks for operations and other smart operators to run operations in the same time.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-27T20:49:06.420Z
Answers.Text: But after doing a bin scan on the pieces bitboard, how do I check which piece is actually on that square? By doing if(white_pawns & pieces) etc for all piece bitboards?
Answers.UserDisplayName: spinosarus123
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-27T20:23:59.163Z
Answers.LastActivityDate: 2021-10-27T20:23:59.163Z
Answers.UserDisplayName: Flavien Volken
Body: <p>How do you go about writing an evaluation function for a bitboard bases chess engine? I want to start of simple and only evaluate based on piece value, and where the pieces stand. For example a knight on c3 should give more points than a knight on a3.</p>
  <p>I can start with making an array of size 64 for each piece with weights. For example I can store the value 0.1 in the square c3 and -0.1 in the square a3 for the knights. Then I could loop over the 64 squares and check if any of the 12 piece types are in this square and add (or subtract) it from the total evaluation. However it seems like this is very slow.</p>
  <p>If I were only counting the piece values then I could use a pop count on each bitboard to get the value, but I don't see how I can implement the square weights this way.</p>
  <p>Is there a smarter way to think about this?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-29T09:09:51.520Z
Comments.Text: You should simultaneously store a "mailbox" chess board, alongside your bitboards. After all, how can you quickly query the piece on a particular square (even during move generation) without this?
Comments.UserDisplayName: Nihar Karve
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-29T09:53:40.060Z
Comments.Text: @NiharKarve If I understand it correctly a "mailbox" would be an array of 64 say integers that represent which pieces are on the 64 different squares? So kind of like a non-bitboard board representation?
Comments.UserDisplayName: spinosarus123
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-29T10:31:26.233Z
Comments.Text: Yes, exactly. Stockfish does this too IIRC
Comments.UserDisplayName: Nihar Karve
Tags: <engines><programming><bitboard>
Title: Evaluation function in a bitboard based chess engine
ThreadText: # Evaluation function in a bitboard based chess engine
  
  (Asked by: username_0 on 2021-10-27)
  
  How do you go about writing an evaluation function for a bitboard bases chess engine? I want to start of simple and only evaluate based on piece value, and where the pieces stand. For example a knight on c3 should give more points than a knight on a3.
  
  I can start with making an array of size 64 for each piece with weights. For example I can store the value 0.1 in the square c3 and -0.1 in the square a3 for the knights. Then I could loop over the 64 squares and check if any of the 12 piece types are in this square and add (or subtract) it from the total evaluation. However it seems like this is very slow.
  
  If I were only counting the piece values then I could use a pop count on each bitboard to get the value, but I don't see how I can implement the square weights this way.
  
  Is there a smarter way to think about this?
  
  **Tags:** engines, programming, bitboard
  
  > **Comments:**
  > **username_1**: You should simultaneously store a "mailbox" chess board, alongside your bitboards. After all, how can you quickly query the piece on a particular square (even during move generation) without this?  
  > **username_0**: @username_1 If I understand it correctly a "mailbox" would be an array of 64 say integers that represent which pieces are on the 64 different squares? So kind of like a non-bitboard board representation?  
  > **username_1**: Yes, exactly. Stockfish does this too IIRC  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2021-10-27. Score: 1 | Accepted answer
  
  Given there is a maximum of 16 pieces on the chessboard you could loop on the pieces instead of the 64 squares. In this way, the less pieces on the board the faster.
  Each piece contains the index of the square, you then only have to pick the value of the corresponding weight to have your score. As each piece could have their own array of weight you can even pre compute the values on the weight adding the piece value and the position.
  If the performances really matter, use a profiler to know where is the bottleneck. You might end up using integers instead of floats, pointers instead of indexes, masks for operations and other smart operators to run operations in the same time.
  
  > **Comments:**
  > **username_0**: But after doing a bin scan on the pieces bitboard, how do I check which piece is actually on that square? By doing if(white_pawns & pieces) etc for all piece bitboards?  
  
  ---
  
  