Re: Usage of unlikely in RCU code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Manish,

On Mon, Aug 1, 2011 at 4:45 PM, Manish Katiyar <mkatiyar@xxxxxxxxx> wrote:
>> C absolutely guarantees that the second will NEVER be evaluated before
>> the first. Otherwise, things like this:
>>
>> if ( ptr && ptr->field )
>>
>> could fail if ptr was NULL.
>
> In your example yes. But is there a problem if the two conditions are
> independent ? for eg..

The compiler doesn't care if the two conditions are independent or dependant.

> foo() {
> ....
>  a = 0;
>  b = my_complex_function_call();
>  if (b && a) {
>      /* Do some non-trivial stuff */
>  } else {
>     printf("I'm an intelligent compiler\n");
>  }
> .....
> }
>
> Does C guarantee that even in this case it won't be reordered and not
> optimized ?

C guarantees that within the if ( b && a ) statement, a won't be
evaluated if b evaluates to zero.

If you coded if ( 0 && func() ) then func will NEVER be called.
if you coded if ( func() && 0 ) then I think that func will always be
called since it won't evaluate the 0 until after the left hand side of
the && has been evaluated.

I tried the following very simple example (compiled with -O3)

#include <stdio.h>
static inline int func() { printf( "func called\n" ); return 1; }
int main( int argc, char **argv ) { if ( func() && 0 ) printf(
"path1\n" ); else printf( "path2\n" ); return 0; }

The compiler was smart enough to optimize out the printf( "path1\n" )
(i.e. running strings on the executable only showed path2), but it
still calls func().

-- 
Dave Hylands
Shuswap, BC, Canada
http://www.davehylands.com

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux