ChessUSA.com TalkChess.com
Hosted by Your Move Chess & Games
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

I'm not very happy with the do {} while() statement in C
Post new topic    TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions Flat
View previous topic :: View next topic  
Author Message
Sven Schüle



Joined: 15 May 2008
Posts: 3528
Location: Berlin, Germany

PostPost subject: Re: I'm not very happy with the do {} while() statement in C    Posted: Tue Feb 20, 2018 10:37 pm Reply to topic Reply with quote

Michael Sherwin wrote:
This is what I ended up with. I hope that I do not hurt Jumbo's feelings because I did not build up the final macros using simple macros but I did use the modulo operator as it has a counterpart in the other macro's '&'. Also there was a peanut shell with a bad peanut in it, Sad. The modified do loop reintroduced the problem of double execution of SQUARE64(ts120) before entering and on entering the do loop. If that is the case then a while loop should just be used. That is unless the compiler is smart enough to jump over the second conversion in the do loop because SQUARE64(ts120) was called with the same parameter. But, I do not know if that is the case. Sad But all in all I am happy with what I ended up with. Very Happy

No feelings were hurt Wink Double execution of SQUARE64(ts120) with identical parameter value would fall under the typical common subexpression optimization of even the most ancient compilers. In the given case, though, the parameter ts120 is modified within the loop (but not between its use before entering and on entering the loop). So I think compilers probably create code that almost corresponds to the "while (1) ... if (...) break" form - but why should we really care about that?

So the code snippet

Code:
ts64 = SQUARE64(ts120);
dirKi[fs64] |= BIT64(ts64);
while(1) {
  dirPtr[i][fs64] |= BIT64(ts64);
  ts120 += dir[i];
  if (!isOnBoard[ts120]) break;
  ts64 = SQUARE64(ts120);
}

is semantically identical to

Code:
dirKi[fs64] |= BIT64(SQUARE64(ts120));
while(1) {
  dirPtr[i][fs64] |= BIT64(SQUARE64(ts120));
  ts120 += dir[i];
  if (!isOnBoard[ts120]) break;
}

after realizing that ts64 is only a temporary variable. And that version can also be written with do-while, making the whole sequence really trivial and obvious. That's exactly what you want: register the current bit exactly once for the king, and register it once per step for a sliding piece until the incremented target square is off the board. Using a temporary ts64 variable complicates everything in my opinion since the conversion macro that is necessary to update ts64 requires ts120 to be a valid square, a condition that is also the loop condition.

Code:
dirKi[fs64] |= BIT64(SQUARE64(ts120));
do {
  dirPtr[i][fs64] |= BIT64(SQUARE64(ts120));
  ts120 += dir[i];
} while (isOnBoard[ts120]);

I would be surprised if anyone would prove this to be slower than the "while ... break" version.
Back to top
View user's profile Send private message
Display posts from previous:   
Subject Author Date/Time
I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 8:06 am
      Re: I'm not very happy with the do {} while() statement in C Marco Belli Sun Feb 18, 2018 8:47 am
            Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 9:03 am
      Re: I'm not very happy with the do {} while() statement in C Nguyen Pham Sun Feb 18, 2018 9:11 am
            Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 9:21 am
                  Re: I'm not very happy with the do {} while() statement in C Nguyen Pham Sun Feb 18, 2018 9:42 am
                  Re: I'm not very happy with the do {} while() statement in C Ronald de Man Sun Feb 18, 2018 9:51 am
                        Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 10:07 am
                              Re: I'm not very happy with the do {} while() statement in C Ronald de Man Sun Feb 18, 2018 10:21 am
                                    Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 10:23 am
      Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sun Feb 18, 2018 9:42 am
            Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 10:05 am
                  Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sun Feb 18, 2018 10:27 am
                        Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 10:34 am
                              Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 10:54 am
                                    Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sun Feb 18, 2018 11:00 am
                                          Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 11:09 am
                                          Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sun Feb 18, 2018 11:20 am
                                                Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 11:48 am
                                                      Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sun Feb 18, 2018 12:40 pm
      Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 1:56 pm
            Re: I'm not very happy with the do {} while() statement in C Ronald de Man Sun Feb 18, 2018 2:34 pm
            Re: I'm not very happy with the do {} while() statement in C Sven Schüle Tue Feb 20, 2018 12:23 am
                  Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Tue Feb 20, 2018 2:25 am
                        Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Tue Feb 20, 2018 5:39 am
                              Re: I'm not very happy with the do {} while() statement in C Sven Schüle Tue Feb 20, 2018 10:37 pm
                                    Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Tue Feb 20, 2018 11:59 pm
                              Re: I'm not very happy with the do {} while() statement in C Fulvio Benini Wed Feb 21, 2018 10:08 am
                                    Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Mon Mar 05, 2018 12:03 am
                                          Re: I'm not very happy with the do {} while() statement in C Álvaro Begué Mon Mar 05, 2018 1:03 am
                                                Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Mon Mar 05, 2018 1:11 am
                                                      Re: I'm not very happy with the do {} while() statement in C Álvaro Begué Mon Mar 05, 2018 1:27 am
                                                            Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Mon Mar 05, 2018 2:09 am
                                                                  Re: I'm not very happy with the do {} while() statement in C Lucas Braesch Wed Mar 07, 2018 4:06 am
                                                                        Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Wed Mar 07, 2018 12:54 pm
                                                                              Re: I'm not very happy with the do {} while() statement in C Lucas Braesch Wed Mar 07, 2018 11:29 pm
                                                                              Re: I'm not very happy with the do {} while() statement in C Marco Costalba Fri Mar 09, 2018 8:40 am
                                                                                    Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Fri Mar 09, 2018 3:35 pm
                                                                        Re: I'm not very happy with the do {} while() statement in C Alcides Schulz Wed Mar 07, 2018 11:32 pm
                                                                  Re: I'm not very happy with the do {} while() statement in C Ed Schroder Fri Mar 09, 2018 2:59 pm
                                                                        Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Fri Mar 09, 2018 3:25 pm
                                                                        Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Fri Mar 09, 2018 3:47 pm
                                                                              Re: I'm not very happy with the do {} while() statement in C Richard Delorme Fri Mar 09, 2018 7:07 pm
                                                                                    Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Fri Mar 09, 2018 7:43 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Fri Mar 09, 2018 8:11 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sat Mar 10, 2018 9:58 am
                                                                                    Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Fri Mar 09, 2018 8:14 pm
                                                                        Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Sat Mar 10, 2018 4:20 am
                                                                              Re: I'm not very happy with the do {} while() statement in C Martin Sedlak Sat Mar 10, 2018 9:21 am
                                                                                    Re: I'm not very happy with the do {} while() statement in C Martin Sedlak Sat Mar 10, 2018 9:29 am
                                                                                    Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Sat Mar 10, 2018 6:08 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Martin Sedlak Sat Mar 10, 2018 6:37 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Sat Mar 10, 2018 10:13 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Martin Sedlak Sat Mar 10, 2018 10:45 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Wed Mar 14, 2018 2:06 am
                                                                                          Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Fri Mar 23, 2018 3:33 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Martin Sedlak Fri Mar 23, 2018 7:19 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Álvaro Begué Fri Mar 23, 2018 7:29 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Ronald de Man Fri Mar 23, 2018 8:51 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Ronald de Man Fri Mar 23, 2018 8:55 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Álvaro Begué Sat Mar 24, 2018 12:00 am
                                                                                          Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Fri Mar 23, 2018 4:17 am
                                                                                          Re: I'm not very happy with the do {} while() statement in C Martin Sedlak Fri Mar 23, 2018 7:33 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sat Mar 10, 2018 7:16 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Matthew Hull Sat Mar 10, 2018 9:17 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Sat Mar 10, 2018 10:18 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sat Mar 10, 2018 10:40 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sat Mar 10, 2018 10:54 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Matthew Hull Sun Mar 11, 2018 11:14 am
                                                                                          Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sun Mar 11, 2018 1:51 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Ronald de Man Sun Mar 11, 2018 12:32 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sun Mar 11, 2018 1:42 pm
                                                                                          Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Wed Mar 14, 2018 2:20 am
                                                                                          Re: I'm not very happy with the do {} while() statement in C laurie tunnicliffe Sun Mar 11, 2018 9:16 am
                                                                                          Re: I'm not very happy with the do {} while() statement in C Lucas Braesch Sun Mar 11, 2018 11:11 am
                                                                              Re: I'm not very happy with the do {} while() statement in C Ed Schroder Sat Mar 10, 2018 9:59 am
                                                            Re: I'm not very happy with the do {} while() statement in C P. Kumar Mon Mar 05, 2018 4:42 am
      Re: I'm not very happy with the do {} while() statement in C Martin Sedlak Sun Feb 18, 2018 5:39 pm
            Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 5:59 pm
                  Re: I'm not very happy with the do {} while() statement in C Martin Sedlak Sun Feb 18, 2018 6:40 pm
                        Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Sun Feb 18, 2018 7:16 pm
                        Re: I'm not very happy with the do {} while() statement in C Ronald de Man Sun Feb 18, 2018 8:39 pm
                              Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sun Feb 18, 2018 10:06 pm
                                    Re: I'm not very happy with the do {} while() statement in C Martin Sedlak Sun Feb 18, 2018 10:32 pm
                                    Re: I'm not very happy with the do {} while() statement in C Ronald de Man Sun Feb 18, 2018 11:53 pm
                                          Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Mon Feb 19, 2018 5:58 pm
                        Re: I'm not very happy with the do {} while() statement in C Martin Sedlak Sun Feb 18, 2018 9:33 pm
                  Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sun Feb 18, 2018 7:04 pm
                        Re: I'm not very happy with the do {} while() statement in C Ronald de Man Sun Feb 18, 2018 9:12 pm
                              Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Sun Feb 18, 2018 10:05 pm
                              Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Tue Feb 20, 2018 5:22 am
                                    Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Tue Feb 20, 2018 6:14 am
                                          Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Tue Feb 20, 2018 6:33 am
                                                Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Tue Feb 20, 2018 10:23 pm
                                                      Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Tue Feb 20, 2018 11:52 pm
                                          Re: I'm not very happy with the do {} while() statement in C Álvaro Begué Tue Feb 20, 2018 11:45 pm
                                                Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Wed Feb 21, 2018 12:15 am
                                                      Re: I'm not very happy with the do {} while() statement in C H.G.Muller Wed Feb 21, 2018 8:34 am
                                                            Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Thu Feb 22, 2018 2:21 am
                                                                  Re: I'm not very happy with the do {} while() statement in C Ronald de Man Thu Feb 22, 2018 12:51 pm
                                                                        Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Fri Feb 23, 2018 4:12 am
                                                                  Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Thu Feb 22, 2018 5:58 pm
                                                                        Re: I'm not very happy with the do {} while() statement in C Eugene Kotlov Thu Feb 22, 2018 6:16 pm
                                                            Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Thu Feb 22, 2018 5:54 pm
                                                                  Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Fri Feb 23, 2018 4:22 am
                  Re: I'm not very happy with the do {} while() statement in C Lucas Braesch Mon Feb 19, 2018 11:45 am
                        Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Mon Feb 19, 2018 2:13 pm
                  Re: I'm not very happy with the do {} while() statement in C Robert Hyatt Tue Feb 20, 2018 5:13 am
      Re: I'm not very happy with the do {} while() statement in C laurie tunnicliffe Mon Mar 05, 2018 12:22 am
            Re: I'm not very happy with the do {} while() statement in C Michael Sherwin Mon Mar 05, 2018 1:04 am
            Re: I'm not very happy with the do {} while() statement in C Álvaro Begué Mon Mar 05, 2018 1:06 am
            Re: I'm not very happy with the do {} while() statement in C Rasmus Althoff Wed Mar 07, 2018 4:58 pm
Post new topic    TalkChess.com Forum Index -> Computer Chess Club: Programming and Technical Discussions

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Powered by phpBB © 2001, 2005 phpBB Group
Enhanced with Moby Threads