Re: GCC 3.3.1, new warnings with <limits>

From: Erik Trulsson <ertr1013_at_student.uu.se>
Date: Sun, 13 Jul 2003 21:07:20 +0200
On Sun, Jul 13, 2003 at 01:37:32PM -0500, David Leimbach wrote:
> 
> On Sunday, July 13, 2003, at 1:23PM, M. Warner Losh wrote:
> 
> >: >     134 #define __glibcpp_signed(T)     ((T)(-1) < 0)
> >: #define __glibcpp_signed(T)     (!((T)(-1) > 0))
> >
> >Why not the simpler:
> >
> >#define __glibcpp_signed(T)     ((T)(-1) <= 0)
> >
> >that way we have an overlap on the range of the two types, so we won't
> >get a warning.  We know for a fact that -1 != 0 for all known machine
> >types (all machines are two's complement, or are required to behave as
> >if they are two's complement, per the standard).
> >
> 
> You keep saying this... where is this "must behave as two's compliment 
> stated?"
> 
> 
> >(unsigned int) -1 == 0xffffffff	  (assuming 32-bit int).
> 
> or with a valid one's compliment C99 compliant system
> (unsigned int) -1 = 0xfffffffe;
Only if UINT_MAX happens to be0xfffffffe, which it probablky won't be.
For all  C99 compliant systems  you have that:
(unsigned int) -1 == UINT_MAX

> 
> >
> >even on a one's complement's machine, without the standard conversion,
> >the 'type punning' conversion of -1 would yield 0xfffffffe, which is
> >still > 0.
> >
> Correct :).  I still don't think C enforces two's compliment.

C doesn't require two's compliment, but  it encourages it.

If you take a signed value and convert it to the corresponding
unsigned type , the result must be equal modulo 2^N to the original
value (where N is the number of bits in the unsigned type. (Ignoring
any padding bits.)) (Actually it is modulo a value one greater than the
largest value representable by the unsigned  type, but this amounts to
the same thing.)
This means that -1 converted to an unsigned type will always be the
largest number representable by that unsigned type.
This is true regardless of how negative numbers are represented.
For two's complement there is no need to change the representation when
converting signed to unsigned values, while this can be needed when
using sign-magnitude or one's-complement.

And to answer the original question:
It is valid to assume that -1 converted to an unsigned integer type
will never be equal to 0.


-- 
<Insert your favourite quote here.>
Erik Trulsson
ertr1013_at_student.uu.se
Received on Sun Jul 13 2003 - 10:07:25 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:15 UTC