Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
05/26/12 11:17
Read: 697 times

#187541 - Technically, they are not the same
Responding to: Maarten Brock's previous message
That is why the hex file is different.

The while(1) is known to always compare and it even makes sense in the parameters passed in to the for statement as there is nothing to compare.

I'd suspect that a compare for a while(1) would be generated if the compiler did not handle this for the user, but the hex file would be seen as different. Again, the logic is technically different. The middle of the logic performs the same exact way, but the end is going to be different, regardless.

if statement 1 said:
if ( a && !b && !c)

is different from
if statment 2 said:
if (a)
if b break;
if c break;

The logic performed seems to be the same, but again, technically, that's incorrect. The middle is definitely the same but the end logic is different.

The first if statement will guarantee that all conditions are tested before moving on to some other task. Although, if the first parameter fails, one can move on. So, if you want speed, the next if statement will prioritize those compares for you and if one fails a compare jumps out of the comparison. In order to jump out of all those compares extra processes are added so that cpu time is not dedicated to comparing statements that will inherently not make a difference. Which means that you are definitely not guaranteed to test all of the parameters and hence it is not the same.

So, the logic looks to be the same, but unless the logic is followed through they will be different as an example below shows.

An analogy might be more helpful. If you have two fractions say, 1/4 and 25/100. Technically, one can reduce the 25/100 to 1/4, but information is then lost especially if it is part of some statistical study that had a population that was not 4. They look to be the same, but they are effectively different numbers.

List of 33 messages in thread
Where can one learn Intermediate C techniques for 8051      David Good      05/22/12 17:42      
   on the right track        Maarten Brock      05/23/12 06:11      
      one more thing      Erik Malund      05/23/12 07:15      
         Not uncommon bid bad coding standards to comply with      Per Westermark      05/23/12 08:14      
         Not afraid of globals, but...      David Good      05/23/12 10:07      
   More keil optimizer interesting tidbits      David Good      05/24/12 16:24      
      optimization      Maarten Brock      05/25/12 05:35      
      nothing gained, nothing lost      Erik Malund      05/25/12 07:53      
         I don't      Per Westermark      05/25/12 07:58      
            you can do both      Erik Malund      05/25/12 09:44      
               That is not helping the compiler      Justin Fontes      05/25/12 16:31      
                  exact same      Maarten Brock      05/26/12 03:36      
                     Technically, they are not the same      Justin Fontes      05/26/12 11:17      
                        Hmmm...      Andy Neil      05/26/12 23:46      
                        C don't do full evaluation of logical expressions      Per Westermark      05/28/12 06:33      
                           In discrete mathematics proving one is not a proof      Justin Fontes      05/28/12 14:02      
                              Lazy evaluation demanded        Per Westermark      05/28/12 14:51      
                                 I have learned something new because of this      Justin Fontes      05/28/12 17:54      
                                    me too      Andy Peters      06/01/12 11:43      
                                       Very Important      Michael Karas      06/01/12 14:30      
                              Breaks.      Michael Karas      05/28/12 17:57      
                                 Compile the code      Justin Fontes      05/28/12 18:28      
                                    To be more exact      Justin Fontes      05/28/12 19:11      
                                       Stop It!!        Michael Karas      05/28/12 22:01      
                                          That is exactly what I intended      Justin Fontes      05/29/12 10:16      
                                             Switch Break.      Michael Karas      05/29/12 22:04      
               obfusciating code to help the compiler is a VERY bad idea      Andy Neil      05/27/12 07:13      
                  the source of this      Erik Malund      05/27/12 07:17      
   Where can one learn Intermediate C techniques for 8051      Andy Neil      05/26/12 23:56      
   Getting the least out of your compiler        Andy Neil      05/27/12 07:25      
      Maybe IAR should follow their own advice?      Christoph Franck      05/30/12 02:05      
         provided the case ...      Maarten Brock      05/30/12 06:29      
            Compilers not knowing the target chip.      Christoph Franck      05/31/12 03:19      

Back to Subject List