Answers.Body: <p>Notice, that the constructor for <code>BoardStatus</code> is <a href="https://en.cppreference.com/w/cpp/language/constexpr" rel="noreferrer"><code>constexpr</code></a> (btw, <a href="https://stackoverflow.com/q/31375381/2452869">this question</a> on SO is an interesting extra read on the topic). So, you should think of <code>BoardStatus</code> as a <a href="https://en.cppreference.com/w/cpp/named_req/LiteralType" rel="noreferrer"><strong>Literal Type</strong></a>, which allows many interesting things to be done with it during the compilation by the compiler (as opposed to runtime calculations), including the elimination of many <code>if</code> statements.</p>
  <p>The particular details would depend a lot on how this class is used, but, I would say, that <code>constexpr</code> is the key to the understanding of the quoted explanations.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-15T23:29:39.550Z
Answers.Text: Because constexpr is evaluated at compile-time, how would this work because you cannot be certain of the states of the variables in the class? Also, how would you get around checking `if (WhiteMove){ //do something} else { //do something}`?
Answers.UserDisplayName: BanjoMan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-16T01:52:46.933Z
Answers.Text: @BanjoMan 1) when you are able to write `if constexpr ()` - this is compile-time. I am certain a bunch of uses of this class is done this way. 2) when used as a template parameter 3) when it is used as synax sugar allowing to generate the necessary version of the code/code path eliminating actual branching.
Answers.UserDisplayName: Anton Menshov
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-16T03:29:44.290Z
Answers.Text: @BanjoMan Afaik it's basically like Anton describes, this generates code for all different cases. So you end up with a quite large executable and a very long compile time.
  Once compiled it then is super fast though. (whether this is useful when writing a chess engine is up for debate)
Answers.UserDisplayName: koedem
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-16T11:57:48.453Z
Answers.Text: I think your comment is the actual answer, the question does not contain the interesting parts of the code. The constexpr itself would make no difference, the optimiser will do its job without it, too.
Answers.UserDisplayName: Carsten S
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-15T21:08:53.740Z
Answers.LastActivityDate: 2022-07-15T21:08:53.740Z
Answers.UserDisplayName: Anton Menshov
Body: <p>From Gigantua, the fastest chess move generator, it says that</p>
  <blockquote>
  <p>The same holds true for the castling squares and most importantly the current moving color becomes a compiletime template and not an if statement. No single if statement should be wasted on non existing moves or which colors move it currently is.</p>
  <p>...</p>
  <p>We have 6 independent boolean board state flags - and none of these
  will ever need a single if during runtime. The cost of having if
  (Color == White) or Attack[Color] compiles away into nothingness. We
  only need a single switch statement as an entry point</p>
  </blockquote>
  <p>Here is the struct used for the current game state:</p>
  <pre><code>class BoardStatus {
  
  public:
      const bool WhiteMove;  const bool HasEPPawn;
      const bool WCastleL; const bool WCastleR;
      const bool BCastleL; const bool BCastleR;
  
      constexpr BoardStatus(bool white, bool ep, bool wcast_left, 
      bool wcast_right, bool bcast_left, bool bcast_right) :
          WhiteMove(white), HasEPPawn(ep), WCastleL(wcast_left), 
          WCastleR(wcast_right), BCastleL(bcast_left), BCastleR(bcast_right)
      {
  
      }
  }
  </code></pre>
  <p>Because of the constructor, this implies that a new <code>BoardStatus</code> structure will be created for every turn. How can that be efficient?</p>
  <p>For example, how does this reduce the number of <code>if</code> statements, because you still have to check the value of the <code>WhiteMove</code> boolean?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-07-16T10:25:17.103Z
Comments.Text: You do not show any of the template code that is mentioned in the text.
Comments.UserDisplayName: Carsten S
Tags: <engines><software><programming><computer-chess>
Title: How to implement programming with board states (turn, en passant squares, castling rights)
ThreadText: # How to implement programming with board states (turn, en passant squares, castling rights)
  
  (Asked by: username_0 on 2022-07-15)
  
  From Gigantua, the fastest chess move generator, it says that
  
  The same holds true for the castling squares and most importantly the current moving color becomes a compiletime template and not an if statement. No single if statement should be wasted on non existing moves or which colors move it currently is.
  ...
  We have 6 independent boolean board state flags - and none of these
  will ever need a single if during runtime. The cost of having if
  (Color == White) or Attack[Color] compiles away into nothingness. We
  only need a single switch statement as an entry point
  
  Here is the struct used for the current game state:
  class BoardStatus {
  
  public:
      const bool WhiteMove;  const bool HasEPPawn;
      const bool WCastleL; const bool WCastleR;
      const bool BCastleL; const bool BCastleR;
  
      constexpr BoardStatus(bool white, bool ep, bool wcast_left, 
      bool wcast_right, bool bcast_left, bool bcast_right) :
          WhiteMove(white), HasEPPawn(ep), WCastleL(wcast_left), 
          WCastleR(wcast_right), BCastleL(bcast_left), BCastleR(bcast_right)
      {
  
      }
  }
  
  Because of the constructor, this implies that a new BoardStatus structure will be created for every turn. How can that be efficient?
  For example, how does this reduce the number of if statements, because you still have to check the value of the WhiteMove boolean?
  
  **Tags:** engines, software, programming, computer-chess
  
  > **Comments:**
  > **username_1**: You do not show any of the template code that is mentioned in the text.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2022-07-15. Score: 6 | Accepted answer
  
  Notice, that the constructor for `BoardStatus` is [`constexpr`](https://en.cppreference.com/w/cpp/language/constexpr) (btw, [this question](https://stackoverflow.com/q/31375381/2452869) on SO is an interesting extra read on the topic). So, you should think of `BoardStatus` as a [**Literal Type**](https://en.cppreference.com/w/cpp/named_req/LiteralType), which allows many interesting things to be done with it during the compilation by the compiler (as opposed to runtime calculations), including the elimination of many `if` statements.
  
  The particular details would depend a lot on how this class is used, but, I would say, that `constexpr` is the key to the understanding of the quoted explanations.
  
  > **Comments:**
  > **username_0**: Because constexpr is evaluated at compile-time, how would this work because you cannot be certain of the states of the variables in the class? Also, how would you get around checking `if (WhiteMove){ //do something} else { //do something}`?  
  > **username_2**: @username_0 1) when you are able to write `if constexpr ()` - this is compile-time. I am certain a bunch of uses of this class is done this way. 2) when used as a template parameter 3) when it is used as synax sugar allowing to generate the necessary version of the code/code path eliminating actual branching.  
  > **username_3**: @username_0 Afaik it's basically like Anton describes, this generates code for all different cases. So you end up with a quite large executable and a very long compile time. Once compiled it then is super fast though. (whether this is useful when writing a chess engine is up for debate)  
  > **username_1**: I think your comment is the actual answer, the question does not contain the interesting parts of the code. The constexpr itself would make no difference, the optimiser will do its job without it, too.  
  
  ---
  
  