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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/28/12 12:33
Read: times


 
#187558 - C don't do full evaluation of logical expressions
Responding to: ???'s previous message
Justin Fontes said:
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.

No - a while loop is not known to always compare. It is only known to always require an expression. But it's up to the compiler to decide what code to generate if that expression can be computed at compile time.

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.


No - the C compiler likes early out, so you can write code like:
if ((num_elements != 0) && ((total_bytes / num_elements) > 5)) {
    ...
}


Let's look at some code generated by gcc:
int test(int a,int b,int c) {
    if (a && !b && !c) {
        return 1;
    }
    return 0;
}

Produces the following code for a x86 processor:
.globl test
        .type   test, @function
test:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $4, %esp
        cmpl    $0, 8(%ebp)
        je      .L2                <= instant out if a is zero
        cmpl    $0, 12(%ebp)
        jne     .L2                <= instant out if b is non-zero
        cmpl    $0, 16(%ebp)
        jne     .L2                <= instant out if c is non-zero
        movl    $1, -4(%ebp)       <= Return value if "if" statement did match
        jmp     .L3
.L2:
        movl    $0, -4(%ebp)       <= Return value if "if" statement didn't match
.L3:
        movl    -4(%ebp), %eax
        leave
        ret
        .size   test, .-test


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.

And above example shows you wrong. I'll leave it up to you to take a closer took at the C standard and figure out the relevant paragraphs.

List of 33 messages in thread
TopicAuthorDate
Where can one learn Intermediate C techniques for 8051            01/01/70 00:00      
   on the right track            01/01/70 00:00      
      one more thing            01/01/70 00:00      
         Not uncommon bid bad coding standards to comply with            01/01/70 00:00      
         Not afraid of globals, but...            01/01/70 00:00      
   More keil optimizer interesting tidbits            01/01/70 00:00      
      optimization            01/01/70 00:00      
      nothing gained, nothing lost            01/01/70 00:00      
         I don't            01/01/70 00:00      
            you can do both            01/01/70 00:00      
               That is not helping the compiler            01/01/70 00:00      
                  exact same            01/01/70 00:00      
                     Technically, they are not the same            01/01/70 00:00      
                        Hmmm...            01/01/70 00:00      
                        C don't do full evaluation of logical expressions            01/01/70 00:00      
                           In discrete mathematics proving one is not a proof            01/01/70 00:00      
                              Lazy evaluation demanded            01/01/70 00:00      
                                 I have learned something new because of this            01/01/70 00:00      
                                    me too            01/01/70 00:00      
                                       Very Important            01/01/70 00:00      
                              Breaks.            01/01/70 00:00      
                                 Compile the code            01/01/70 00:00      
                                    To be more exact            01/01/70 00:00      
                                       Stop It!!            01/01/70 00:00      
                                          That is exactly what I intended            01/01/70 00:00      
                                             Switch Break.            01/01/70 00:00      
               obfusciating code to help the compiler is a VERY bad idea            01/01/70 00:00      
                  the source of this            01/01/70 00:00      
   Where can one learn Intermediate C techniques for 8051            01/01/70 00:00      
   Getting the least out of your compiler            01/01/70 00:00      
      Maybe IAR should follow their own advice?            01/01/70 00:00      
         provided the case ...            01/01/70 00:00      
            Compilers not knowing the target chip.            01/01/70 00:00      

Back to Subject List