tinycc-devel
[Top][All Lists]
Advanced

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

RE: [Tinycc-devel] wrong preprocessor shift operation


From: Christian Jullien
Subject: RE: [Tinycc-devel] wrong preprocessor shift operation
Date: Wed, 31 Dec 2008 08:12:37 +0100

Thanks for explanation.
My real code using unsigned was in fact something like:

printf("%08x\n", (~0U) >> 1);

That had broken result before my investigation and grischka fix and now
produces expected value on all compilers with my extreeemly portable
OpenLisp ISLISP implementation (see www.eligis.com).

I initially tried to produce tests to exhibit bug and was too quick (wrong)
with just ~0 >> 1.

Now, with fix, OpenLisp uses the same code as with other compilers (more
that 60 !!) and my own non-regression tests suite that tests all aspects of
ISLISP language (based of course on C compiler and libraries) runs without
any errors.

Thanks again and happy new year.

-----Original Message-----
From: address@hidden
[mailto:address@hidden On Behalf Of Dave
Dodge
Sent: Wednesday, December 31, 2008 0:08
To: address@hidden
Subject: Re: [Tinycc-devel] wrong preprocessor shift operation


On Tue, Dec 30, 2008 at 05:32:57PM +0100, grischka wrote:
> Christian Jullien wrote:
>>      printf("fails %08x\n", (~0) >> 1);

> fails ffffffff

> Maybe gcc is not correct but then I need an exact explanation why ;)

0 has type int.  On x86 the representation is 32 value bits set to
zero, with no padding bits.

~0 flips all of the bits of the representation, producing an int with
value -1.

(~0) >> 1 is a right-shift of a signed integer with negative value,
which is an implementation-defined operation.  On x86, this is usually
implemented with the SAR instruction, which divides by 2 with rounding
toward -infinity.  -1/2 rounded toward -infinity is -1, so the value
is unchanged.

This then passes int -1 to a printf specifier that expects unsigned
int.  Since the value is negative and cannot be represented by
unsigned int, you get undefined behavior and any result is allowed.

I believe in this case, the representation of int -1 is reinterpreted
as an unsigned int, producing the printed value ffffffff.  But really,
the test itself is fundamentally broken.

                                                  -Dave Dodge


_______________________________________________
Tinycc-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/tinycc-devel
----------------------------------------------------------------------------
-----------
Orange vous informe que cet  e-mail a ete controle par l'anti-virus mail. 
Aucun virus connu a ce jour par nos services n'a ete detecte.









reply via email to

[Prev in Thread] Current Thread [Next in Thread]