tech-toolchain archive

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

Re: gcc unsigned vs signed compare



> [...] a signed vs unsigned compare issue.

>       (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 ?

mips64el, I think you said.  Am I correct in thinking that long is 64
bits and int is 32?  I'll assume so.

As I read the C99 draft I have a copy of, exp is converted to unsigned
long, an unsigned long subtraction is performed on the LHS, and
similarly on the RHS the ints are converted to unsigned long and
unsigned long arithmetic is done.  The quote is:

            [...if floating-point is involved...]

            Otherwise, the integer promotions are performed on both
            operands.   Then the following rules are applied to the
            promoted operands:

                 If both operands  have  the  same  type,  then  no
                 further conversion is needed.

                 Otherwise,  if  both  operands have signed integer
                 types or both have  unsigned  integer  types, [...]

                 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.

("rank" is defined earlier; unsigned long has higher rank than int.)

Each side of the comparison then has type unsigned long, so there
should be no warning even with -Wsign-compare, and the comparison
should be an unsigned 64-bit comparison.

/~\ The ASCII                             Mouse
\ / Ribbon Campaign
 X  Against HTML                mouse%rodents-montreal.org@localhost
/ \ Email!           7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index