On Thu, Mar 18, 2010 at 12:23 PM, Alain Ketterlin <alain.ketter...@gmail.com> wrote: > > I've reported here recently about gcc producing conditional branches > with static outcome. I've finally managed to produce a simple > self-contained example. Here it is: > > int f() > { > static int LSW=-1; > double x=0.987654321; > unsigned char ix = *((char *)&x); > if(ix==0xdd || ix==0x3f) > LSW = 1; > else > LSW = 0; > return 0; > } > > Compiling this with "gcc-4.5 -O3 -c f.c" (20100304 snapshot) on my > x86-64 system produces the following code: > > 0: b8 b8 ff ff ff mov $0xffffffb8,%eax > 5: 3c dd cmp $0xdd,%al > 7: 0f 94 c0 sete %al > ... > > Ok, not a branch, but still useless code. gcc-4.4 does the same, and > gcc-4.3 is slightly less clever (it moves the whole immediate 8 bytes > into %rax and does two cmp+je). > > I first thought the type-cast was responsible. But if you replace the > assignment to LSW with a simple return, gcc correctly optimizes the > whole function away. > > Can anybody confirm this? Is it worth a bug report?
I can confirm this and yes, it is worth a bugreport. If you have cases that do not involve floating-point constants accessed as integer values that would be even more interesting (because likely more common). Thanks, Richard. > -- Alain. >