bug-gnulib
[Top][All Lists]
Advanced

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

Re: changing "configure" to default to "gcc -g -O2 -fwrapv ..."


From: Paul Eggert
Subject: Re: changing "configure" to default to "gcc -g -O2 -fwrapv ..."
Date: Sat, 30 Dec 2006 23:30:07 -0800
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

Joe Buck <address@hidden> writes:

>> >       *hv = (HOST_WIDE_INT) -(unsigned HOST_WIDE_INT) h1;
>> 
>> Can't that conversion overflow?
>
> Not on a two's complement machine,

Sure it can.  Suppose we have a 64-bit two's complement
machine with no padding, and h1 is - 2**63.  Then (unsigned
HOST_WIDE_INT) h1 is 2**63, -(unsigned HOST_WIDE_INT) h1 is
also 2**63, and converting 2**63 to HOST_WIDE_INT overflows.

In this case C99 says the overflow results in an
implementation-defined value, or an implementation-defined
signal.  If the former, the most plausible value is - 2**63
but C99 allows other values.  If the latter, GCC will crash.
Either way, the code is "broken" -- unless you assume wrapv
semantics of course.

Even an expert like you can easily get this wrong, and this
suggests how tricky this area of Standard C really is.  Only
a tiny fraction of C programmers know how to write this sort
of code reliably and in conformance to minimal standard C.
It's little wonder that most C programmers assume wrapv
semantics in cases like this.




reply via email to

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