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

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

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

Post by Michael Sherwin »

Code: Select all

#define FALSE  0
#define TRUE   1

#define QUIT   0
#define THINK  1
#define GETCMD 2

#define s32 signed   __int32
#define u64 unsigned __int64

void Think(void);
void Initialize(void);
void GetCmd(void);
s32 main(void);

u64 dir7p[64];
u64 dir9p[64];
u64 dir7m[64];
u64 dir9m[64];
u64 dir1p[64];
u64 dir8p[64];
u64 dir1m[64];
u64 dir8m[64];
u64 dirKn[64];
u64 dirKi[64];

s32 mode = GETCMD;

u64 *dirPtr[8] = { dir7p, dir9p, dir7m, dir9m, dir1p, dir8p, dir1m, dir8m };

void Think() {

}

void GetCmd() {
  mode = QUIT;
}

void Initialize() {
  s32 i, j, k, x, y, sq, ts;
  s32 dir[8] = { 9, 11, -9, -11, 1, 10, -1, -10 };
  s32 din[8] = { 8, 12, 18, 22, -8, -12, -18, -22 };
  s32 initBoard[120] = {
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

  for &#40;i = 21; i < 99; i++) &#123;
    if &#40;initBoard&#91;i&#93;) &#123;
      y = i / 10; x = i - y * 10 - 1; y -= 2;
      sq = y * 8 + x;
      dirKi&#91;sq&#93; = 0;
      dirKn&#91;sq&#93; = 0;
      for &#40;j = 0; j < 8; j++) &#123;
        k = i + din&#91;j&#93;;
        if &#40;initBoard&#91;k&#93;) &#123;
          y = k / 10; x = k - y * 10 - 1; y -= 2;
          ts = y * 8 + x;
          dirKn&#91;sq&#93; |= &#40;u64&#41;1 << ts;
        &#125;
        dirPtr&#91;j&#93;&#91;sq&#93; = 0;
        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;
          do &#123;
            dirPtr&#91;j&#93;&#91;sq&#93; |= (&#40;u64&#41;1 << ts&#41;;
            k += dir&#91;j&#93;;
            y = k / 10; x = k - y * 10 - 1; y -= 2;
            ts = y * 8 + x;
          &#125; while &#40;initBoard&#91;k&#93;);
        &#125;
      &#125;
    &#125;
  &#125;
&#125;

s32 main&#40;)
&#123;
  Initialize&#40;);

  while &#40;mode&#41; &#123;
    if &#40;mode == THINK&#41; Think&#40;);
    GetCmd&#40;);
  &#125;
  return 0;
&#125;
In the do while loop I have to execute instructions that at some point will not need to be executed. It is not really a problem in this simple initiation example. But still it seems like a poor design. It would be perfect if the do while worked like this.

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;
          do &#123;
            dirPtr&#91;j&#93;&#91;sq&#93; |= (&#40;u64&#41;1 << ts&#41;;
            k += dir&#91;j&#93;;
          &#125; while &#40;initBoard&#91;k&#93;) &#123;
            y = k / 10; x = k - y * 10 - 1; y -= 2;
            ts = y * 8 + x;
          &#125;
        &#125;
 
If it would execute the code in the braces before looping back to the do statement. Maybe there is away around this minor dilemma. I just do not know what it is?
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
elcabesa
Posts: 855
Joined: Sun May 23, 2010 1:32 pm

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

Post by elcabesa »

maybe you can try using a while(){} instead of do{}while.

you are initializzing y,x,sq before the do and inside the do so it seems that while loop fits better your situation. but you have to try :)
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 »

elcabesa wrote:maybe you can try using a while(){} instead of do{}while.

you are initializzing y,x,sq before the do and inside the do so it seems that while loop fits better your situation. but you have to try :)
Thank you for the reply. The entry test for the proposed while statement was already done in a preceding if statement. Doing the test twice is a worse evil in my opinion. I could break the king, knight and sliders out separate from each other so that a while statement would then make sense. But that really makes no sense either.
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
phhnguyen
Posts: 1431
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: 2486
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: 1431
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: 5555
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&#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;
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