Gcc with optimization flags creates incorrect machine code.
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
gcc-defaults (Ubuntu) |
Invalid
|
Undecided
|
Unassigned |
Bug Description
Binary package hint: gcc
Gcc version - 4.0.3 (Ubuntu 4.0.3-1ubuntu5)
Kubuntu version dapper drake - 6.06
code was compiled with the following options....
-O3 -g3 -ggdb -Wall -mtune=pentium4 -mmmx -msse -msse2 -ffast-math -funsafe-
The following C code snipit was compiled incorrectly...
#define LARGEST_
#define MAX_CAHCEBLOCK_SIZE 4096
<.....>
const unsigned int numBits = sizeof( size_t ) * 8;
unsigned int largestBit;
unsigned int shiftOffset;
size_t floorOfSize;
size_t ceilOfSize;
LARGEST_BIT( size, largestBit );
floorOfSize = 1 << largestBit;
shiftOffset = ( ( floorOfSize - size ) >> ( numBits - 1 ) );
ceilOfSize = 1 << ( shiftOffset + largestBit) ;
return ( size < MAX_CAHCEBLOCK_SIZE ) ? ceilOfSize : size;
<.....>
and the resultant asm code was...
bsr %ecx,%ebx
mov $0x1, %edx
mov %edx,%eax
shl %cl,%eax
sub %ebx,%eax
shr $0x1f,%eax
add %eax,%ecx
shl %cl,%edx
cmp $0xfff,%ebx
cmvbe %edx,%ebx
If the C variable size equals 12 the C code will return 16, the asm code from gcc will return 4096.
description: | updated |
description: | updated |
Nevermind, Upon further investigation, gcc inlined the code, confusing the debugger, causing it to jump to incorrect sections of the code.
Sorry.