Gcc with optimization flags creates incorrect machine code.

Bug #64288 reported by Ardavon Falls
4
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-math-optimizations -ffinite-math-only -fno-trapping-math -fsingle-precision-constant -fprefetch-loop-arrays -fomit-frame-pointer -mno-push-args -DNDEBUG -D_GNU_SOURCE -D_REENTRANT

The following C code snipit was compiled incorrectly...

#define LARGEST_BIT(x,retval) __asm("bsrl %0, %1;" : "=r"(retval) : "r"(x))
#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
Revision history for this message
Ardavon Falls (ardyfalls) wrote :

Nevermind, Upon further investigation, gcc inlined the code, confusing the debugger, causing it to jump to incorrect sections of the code.
Sorry.

Revision history for this message
Tim Butler (timbutler) wrote :

As per your second comming, I'm rejecting this as a bug. Glad to see you got it working though!

Changed in gcc-defaults:
status: Unconfirmed → Rejected
Revision history for this message
Tim Butler (timbutler) wrote :

Shocking typing, buy "comming" I meant to write comment :)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.