I'm not very happy with the do {} while() statement in C

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

syzygy
Posts: 4221
Joined: Tue Feb 28, 2012 10:56 pm

Re: I'm not very happy with the do {} while() statement in C

Post by syzygy » Sun Feb 18, 2018 10:21 am

I would not recommend the for() solution, though ;-)

Michael Sherwin
Posts: 2799
Joined: Fri May 26, 2006 1:00 am
Location: OH, USA

Re: I'm not very happy with the do {} while() statement in C

Post by Michael Sherwin » Sun Feb 18, 2018 10:23 am

syzygy wrote:I would not recommend the for() solution, though ;-)


Agreed! I already changed it to using while and break and checked it in debug. :D
Regards,
Mike

Ras
Posts: 935
Joined: Tue Aug 30, 2016 6:19 pm
Contact:

Re: I'm not very happy with the do {} while() statement in C

Post by Ras » Sun Feb 18, 2018 10:27 am

Michael Sherwin wrote:The initBoard variable is absolutely needed
It isn't. You could eliminate it because the whole initialisation only takes place for the 64 centre squares of the board, which is checked via the initboard.

So why not using eight dedicated loops? The first runs from 21 to 28, the second from 31 to 38, the third from 41 to 48 and so on. You would even eliminate the superfluous initboard checks when i is 29 or 30, or 39 or 40.

If you put each of these row-wise loops in a parametrised macro, you'd eliminate the code duplication that directly rolling out these 8 loops would bring.

Michael Sherwin
Posts: 2799
Joined: Fri May 26, 2006 1:00 am
Location: OH, USA

Re: I'm not very happy with the do {} while() statement in C

Post by Michael Sherwin » Sun Feb 18, 2018 10:34 am

Ras wrote:
Michael Sherwin wrote:The initBoard variable is absolutely needed
It isn't. You could eliminate it because the whole initialisation only takes place for the 64 centre squares of the board, which is checked via the initboard.

So why not using eight dedicated loops? The first runs from 21 to 28, the second from 31 to 38, the third from 41 to 48 and so on. You would even eliminate the superfluous initboard checks when i is 29 or 30, or 39 or 40.

If you put each of these row-wise loops in a parametrised macro, you'd eliminate the code duplication that directly rolling out these 8 loops would bring.


Okay, now I follow! :D
Regards,
Mike

Michael Sherwin
Posts: 2799
Joined: Fri May 26, 2006 1:00 am
Location: OH, USA

Re: I'm not very happy with the do {} while() statement in C

Post by Michael Sherwin » Sun Feb 18, 2018 10:54 am

Michael Sherwin wrote:
Ras wrote:
Michael Sherwin wrote:The initBoard variable is absolutely needed
It isn't. You could eliminate it because the whole initialisation only takes place for the 64 centre squares of the board, which is checked via the initboard.

So why not using eight dedicated loops? The first runs from 21 to 28, the second from 31 to 38, the third from 41 to 48 and so on. You would even eliminate the superfluous initboard checks when i is 29 or 30, or 39 or 40.

If you put each of these row-wise loops in a parametrised macro, you'd eliminate the code duplication that directly rolling out these 8 loops would bring.


Okay, now I follow! :D


Or better just changing the direction!

Code: Select all

for&#40;sq = 0, sq < 64, sq++) &#123;
  y = sq / 8; x = sq - y * 8;
  k = y * 10 + x + 1;
  ...
&#125;
  
Regards,
Mike

Ras
Posts: 935
Joined: Tue Aug 30, 2016 6:19 pm
Contact:

Re: I'm not very happy with the do {} while() statement in C

Post by Ras » Sun Feb 18, 2018 11:00 am

Michael Sherwin wrote:Or better just changing the direction!
That's even nicer because it's clearer. And you could make the loop count down instead of counting up. And
y = sq / 8; x = sq - y * 8;
could be replaced by
y = sq >> 3; x = sq & 0x07;
though the compiler will likely do the first replacement automatically.

Or, and that would be even more readable:

Code: Select all

#define RANK64&#40;x&#41; &#40;x >> 3&#41;
#define FILE64&#40;x&#41; &#40;x & 0x07&#41;
...
y = RANK64&#40;sq&#41;;
x = FILE64&#40;sq&#41;;

Michael Sherwin
Posts: 2799
Joined: Fri May 26, 2006 1:00 am
Location: OH, USA

Re: I'm not very happy with the do {} while() statement in C

Post by Michael Sherwin » Sun Feb 18, 2018 11:09 am

Ras wrote:
Michael Sherwin wrote:Or better just changing the direction!
That's even nicer because it's clearer. And you could make the loop count down instead of counting up. And
y = sq / 8; x = sq - y * 8;
could be replaced by
y = sq >> 3; x = sq & 0x07;
though the compiler will likely do the first replacement automatically.

Or, and that would be even more readable:

Code: Select all

#define RANK64&#40;x&#41; &#40;x >> 3&#41;
#define FILE64&#40;x&#41; &#40;x & 0x07&#41;
...
y = RANK64&#40;sq&#41;;
x = FILE64&#40;sq&#41;;
Consider it done! :D All this stuff I used to know but forgot because I have not programmed anything new for a long time. Thank you
Regards,
Mike

Ras
Posts: 935
Joined: Tue Aug 30, 2016 6:19 pm
Contact:

Re: I'm not very happy with the do {} while() statement in C

Post by Ras » Sun Feb 18, 2018 11:20 am

Ras wrote:

Code: Select all

#define RANK64&#40;x&#41; &#40;x >> 3&#41;
#define FILE64&#40;x&#41; &#40;x & 0x07&#41;
Subtle oppurtunities for bugs upon a second glance. If the x that is handed over isn't a variable, but e.g. a sum, then this will go wrong. Should be:

Code: Select all

#define RANK64&#40;x&#41; (&#40;x&#41; >> 3&#41;
#define FILE64&#40;x&#41; (&#40;x&#41; & 0x07&#41;

Michael Sherwin
Posts: 2799
Joined: Fri May 26, 2006 1:00 am
Location: OH, USA

Re: I'm not very happy with the do {} while() statement in C

Post by Michael Sherwin » Sun Feb 18, 2018 11:48 am

Ras wrote:
Ras wrote:

Code: Select all

#define RANK64&#40;x&#41; &#40;x >> 3&#41;
#define FILE64&#40;x&#41; &#40;x & 0x07&#41;
Subtle oppurtunities for bugs upon a second glance. If the x that is handed over isn't a variable, but e.g. a sum, then this will go wrong. Should be:

Code: Select all

#define RANK64&#40;x&#41; (&#40;x&#41; >> 3&#41;
#define FILE64&#40;x&#41; (&#40;x&#41; & 0x07&#41;

Code: Select all

#define SQUARE120&#40;x&#41; ((&#40;x&#41; >> 3 * 10&#41; + ((&#40;x&#41; & 7&#41; + 1&#41;)
:?:
Regards,
Mike

Ras
Posts: 935
Joined: Tue Aug 30, 2016 6:19 pm
Contact:

Re: I'm not very happy with the do {} while() statement in C

Post by Ras » Sun Feb 18, 2018 12:40 pm

Michael Sherwin wrote:

Code: Select all

#define SQUARE120&#40;x&#41; ((&#40;x&#41; >> 3 * 10&#41; + ((&#40;x&#41; & 7&#41; + 1&#41;)
:?:
Multiplication has operator precedence over shifting, so the shift would be evaluated to ">> 30" at compile time.

(Source: http://en.cppreference.com/w/c/language ... precedence )

Code: Select all

#define SQUARE120&#40;x&#41; (((&#40;x&#41; >> 3&#41; * 10&#41; + (&#40;x&#41; & 7&#41; + 1&#41;
For the init code, this is OK. If that were used during search, though, I'd use a global lookup table of const int8_t, together with an enum for the squares.

Post Reply