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

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
phhnguyen
Posts: 1434
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

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

Post by phhnguyen »

When asking, the best if you could provide us some basic information about the code and what you are doing (such as author, program's name, you want to learn or improve...) If the author of the code here he definitely be happy to reply you directly.

AFAIK, using do-while instead of for/while is an old trick to speed up the app. It helps by omitting the first checking condition (when you are sure the first checking is always true). Suppose your code repeats 10 times, using do-while you need to check conditions 9 times only instead of 10.

Theoretically it is "clever" and faster. However in practice it is very hard to measure out the improvement. Thus, you may forget that trick and write your code in your own styles.
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 »

phhnguyen wrote:When asking, the best if you could provide us some basic information about the code and what you are doing (such as author, program's name, you want to learn or improve...) If the author of the code here he definitely be happy to reply you directly.

AFAIK, using do-while instead of for/while is an old trick to speed up the app. It helps by omitting the first checking condition (when you are sure the first checking is always true). Suppose your code repeats 10 times, using do-while you need to check conditions 9 times only instead of 10.

Theoretically it is "clever" and faster. However in practice it is very hard to measure out the improvement. Thus, you may forget that trick and write your code in your own styles.
also thank you for the reply. I am the author. This is my own code. :D

And I am sure that the condition is true the first time. That is why I chose the do while loop. The first condition is true and finds a bit for the king then it enters the do loop to process bits for a slider. The code is logically sound but it does reveal a deficiency in C/C++ IMHO.
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 »

If you really wanted to optimise the last bit out of here, you could even drop the whole initboard variable and instead use 8 loops after each other, one for each row. You might use a parametrised macro for that in C.
User avatar
phhnguyen
Posts: 1434
Joined: Wed Apr 21, 2010 4:58 am
Location: Australia
Full name: Nguyen Hong Pham

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

Post by phhnguyen »

lol, thought it was from other people!

Why you are not happy with your own code? If it could run correctly - it is done, at least for a lap of developing. Even you spend much more time, ask more people you are hardly to improve it significantly.

Just focus to other tasks. Come back to improve it when you have completed all other tasks and more important, when you have completely forgot that code.
syzygy
Posts: 5563
Joined: Tue Feb 28, 2012 11:56 pm

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

Post by syzygy »

If you want to break in the middle of the loop, then use break:

Code: Select all

        k = i + dir[j];
        if (initBoard[k])  {
          y = k / 10; x = k - y * 10 - 1; y -= 2;
          ts = y * 8 + x;
          dirKi&#91;sq&#93; |= &#40;u64&#41;1 << ts;
          while &#40;1&#41; &#123;
            dirPtr&#91;j&#93;&#91;sq&#93; |= (&#40;u64&#41;1 << ts&#41;;
            k += dir&#91;j&#93;;
            if (!initBoard&#91;k&#93;)
              break;
            y = k / 10; x = k - y * 10 - 1; y -= 2;
            ts = y * 8 + x;
          &#125;
        &#125;
Or use a for statement:

Code: Select all

        k = i + dir&#91;j&#93;;
        if &#40;initBoard&#91;k&#93;)  &#123;
          y = k / 10; x = k - y * 10 - 1; y -= 2;
          ts = y * 8 + x;
          dirKi&#91;sq&#93; |= &#40;u64&#41;1 << ts;
          for (;
            dirPtr&#91;j&#93;&#91;sq&#93; |= (&#40;u64&#41;1 << ts&#41;,
            k += dir&#91;j&#93;,
            initBoard&#91;k&#93;;
            y = k / 10, x = k - y * 10 - 1, y -= 2,
            ts = y * 8 + x
          );
        &#125;
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:If you really wanted to optimise the last bit out of here, you could even drop the whole initboard variable and instead use 8 loops after each other, one for each row. You might use a parametrised macro for that in C.
I'm not following. The initBoard variable is absolutely needed to test for on board vs off board as I chose to generate moves on a 10 x 12 board and then convert the on board squares to an 8 x 8 board index.
Last edited by Michael Sherwin on Sun Feb 18, 2018 11:13 am, edited 1 time in total.
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 »

syzygy wrote:If you want to break in the middle of the loop, then use break:

Code: Select all

        k = i + dir&#91;j&#93;;
        if &#40;initBoard&#91;k&#93;)  &#123;
          y = k / 10; x = k - y * 10 - 1; y -= 2;
          ts = y * 8 + x;
          dirKi&#91;sq&#93; |= &#40;u64&#41;1 << ts;
          while &#40;1&#41; &#123;
            dirPtr&#91;j&#93;&#91;sq&#93; |= (&#40;u64&#41;1 << ts&#41;;
            k += dir&#91;j&#93;;
            if (!initBoard&#91;k&#93;)
              break;
            y = k / 10; x = k - y * 10 - 1; y -= 2;
            ts = y * 8 + x;
          &#125;
        &#125;
Or use a for statement:

Code: Select all

        k = i + dir&#91;j&#93;;
        if &#40;initBoard&#91;k&#93;)  &#123;
          y = k / 10; x = k - y * 10 - 1; y -= 2;
          ts = y * 8 + x;
          dirKi&#91;sq&#93; |= &#40;u64&#41;1 << ts;
          for (;
            dirPtr&#91;j&#93;&#91;sq&#93; |= (&#40;u64&#41;1 << ts&#41;,
            k += dir&#91;j&#93;,
            initBoard&#91;k&#93;;
            y = k / 10, x = k - y * 10 - 1, y -= 2,
            ts = y * 8 + x
          );
        &#125;
I do follow. And each solution is perfect. Thanks for reminding me of those solutions. I knew them at one time but forgot about them. :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
syzygy
Posts: 5563
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.