Page 2 of 12

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

Posted: Sun Feb 18, 2018 11:21 am
by syzygy
I would not recommend the for() solution, though ;-)

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

Posted: Sun Feb 18, 2018 11:23 am
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

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

Posted: Sun Feb 18, 2018 11:27 am
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.

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

Posted: Sun Feb 18, 2018 11:34 am
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

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

Posted: Sun Feb 18, 2018 11:54 am
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;
  

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

Posted: Sun Feb 18, 2018 12:00 pm
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;;

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

Posted: Sun Feb 18, 2018 12:09 pm
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

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

Posted: Sun Feb 18, 2018 12:20 pm
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;

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

Posted: Sun Feb 18, 2018 12:48 pm
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;)
:?:

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

Posted: Sun Feb 18, 2018 1:40 pm
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.