My castling code

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
leanchess
Posts: 176
Joined: Sun Dec 08, 2019 8:16 pm
Full name: Dmitry Shechtman

Re: My castling code

Post by leanchess »

As it turns out, splitting attack_bits across rook/bishop makes it marginally slower, whereas duplicating it makes it more than twice as slow. Back to the compression idea...
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: My castling code

Post by Michael Sherwin »

It is really sad to have to unsubscribe from ones own topic because it has degenerated into a toral circle jerk!
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
User avatar
mvanthoor
Posts: 1784
Joined: Wed Jul 03, 2019 4:42 pm
Location: Netherlands
Full name: Marcel Vanthoor

Re: My castling code

Post by mvanthoor »

My suggestion would be to contact one of the moderators, and ask to have the topic split after your reaction to my post. After that, it has indeed changed to talking about code readability and leanchess has also introduced his own castling code. Maybe the topic should actually be split into three:

- yours.
- one about code readability.
- one with leanchess' castling code.

Sorry for (unintentionally) derailing your topic.
Author of Rustic, an engine written in Rust.
Releases | Code | Docs | Progress | CCRL
User avatar
leanchess
Posts: 176
Joined: Sun Dec 08, 2019 8:16 pm
Full name: Dmitry Shechtman

Re: My castling code

Post by leanchess »

I suppose I'm the one that should apologise. I agree with mvanthoor's suggestion.

Dear moderators, please split (at least) from this post.
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: My castling code

Post by AlvaroBegue »

mar wrote: Mon Apr 20, 2020 6:40 am You should take your own advice then, RuyDos is full of single-character variables/function arguments and even function names.
I have no problem with short names as I understand that o is occupancy, N stands for north etc.,
but if you assume a certain position and do the exact opposite yourself, well...
Do as I say, not as I do. :)
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: My castling code

Post by Michael Sherwin »

I think I have made progress in my understanding. Just considering the white king if the wcancast (white_can_castle) variable is less than the current ply then nothing else needs to be done as castling is disabled. Else wcancast is set to INFINITY if the WC (white_king_that_can_castle) is on E1 else wcancast is set to ply. Then only when ply dips below and comes back up is a complete check made again as castling may have or may not have been enabled.

Note: I have gone back to using a thread pointer instead of an index and a structure instead of arrays. I have determined that it is nested structures that are slow. Arrays in structures (SOA) are fine for speed. The following code looks single threaded but it is not. The preprocessor is used to "hide" the thread pointer.
#define board t->board
Then piece = board[square]; can be written instead of piece = t->board[square];--it is just much cleaner.
Also note that Attacked squares are no longer being looked for. Instead three WK (white_king_that_cannot_castle) are placed on the board and a function pointer is updated in MakeMove() to handle the removal of two kings and the placement of the rook.

Code: Select all

      case WC:
        bb = kingMoves[fs] & notme;
        if (wcancast >= ply) {
          if (board[E1] == WC) {
            wcancast = INFINITY;
            if (wcancask >= ply) {
              if (board[H1] == WR) {
                wcancask = INFINITY;
                if (!(apieces & (F1BIT | G1BIT))) {
                  bb |= G1BIT;
                }
              }
              else {
                wcancask = ply;
              }
            }
          }
          if (wcancasq >= ply) {
            if (board[A1] == WR) {
              wcancasq = INFINITY;
              if (!(apieces & (D1BIT | C1BIT))) {
                bb |=  C1BIT;
              }
            }
            else {
              wcancasq = ply;
             }
          }
        }
        else {
          wcancast = ply;
        }
        break;
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through