On 2006-12-31 08:25:23 -0500, Richard Kenner wrote: > > > > I suppose there is > > > > > > > > *hv = (HOST_WIDE_INT) -(unsigned HOST_WIDE_INT) h1; > > > > > > > > to make it safe. > > > > > > Can't that conversion overflow? > > > > Not on a two's complement machine, > > Then I'm confused about C's arithmetic rules. Suppose h1 is 1. It's > cast to unsigned, so stays as 1. Now we do unary minus in unsigned. > That's ~0 because it's done mod 2**32, but interpreted as a POSITIVE > number. Now, that positive number overflows when converted to signed > (the intent is for that overflow make it negative). Am I missing > something (quite likely!)?
No, this is not portable. On the same kind of code, even the wrap may hide a bug, e.g. when one has: *hv = -(unsigned HOST_WIDE_INT) h1; where *hv is signed, but not the same type as the original one. For instance, such a code will work on a 32-bit machine, but not necessarily on a 64-bit machine. Now, will GCC do anything to make such code work on such machines? I doubt that. -- Vincent Lefèvre <[EMAIL PROTECTED]> - Web: <http://www.vinc17.org/> 100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/> Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)