tech-toolchain archive

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

Re: gcc unsigned vs signed compare



On Wed, 31 Aug 2011, Manuel Bouyer wrote:
In lib/libc/gen/fixunsgen_ieee754.c there is a compare:
        (exp - mant_dig > sizeof(UINTXX_T)*8-1)
exp is int, mant_dig is size_t (unsigned long) and sizeof returns size_t. gcc generates an unsigned compare for this, which doesn't gives the expected result when exp < mant_dig. Is gcc right when generating an unsigned compare here ?

I think gcc is correct. C99 section 6.3.1.8 "Usual arithmetic conversions" says: "Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type." Here, mant_dig is the operand of the "-" opetator that has unsigned type, and exp is the operand with signed type. "Rank" is defined in section 6.3.1.1, and the rank of "unsigned long" is greater than the rank of "int". So, the int operand is converted to unsigned long before the subtraction, and the result of the subtraction is unsigned long. sizeof returns an unsigned result, so both operands of the ">" operator are unsigned.

--apb (Alan Barrett)


Home | Main Index | Thread Index | Old Index