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

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

syzygy
Posts: 5557
Joined: Tue Feb 28, 2012 11:56 pm

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

Post by syzygy »

I would not recommend the for() solution, though ;-)
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

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

Post by Michael Sherwin »

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
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
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

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

Post by Ras »

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: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

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

Post by Michael Sherwin »

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
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
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

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

Post by Michael Sherwin »

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;
  
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
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

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

Post by Ras »

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: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

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

Post by Michael Sherwin »

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
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
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

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

Post by Ras »

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: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

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

Post by Michael Sherwin »

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;)
:?:
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
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

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

Post by Ras »

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.