This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: 20040309-1.c vs overflow being undefined


Andreas Schwab wrote:-

> Andrew Pinski <pinskia@physics.uc.edu> writes:
> 
> > sorry wrong number, I had meant 32769.
> >   if (foo (32769) != 1)
> >     abort ();
> 
> I think with 16 bit ints you should get 0 here, since (int)32769 ==
> -32767, which is less than 32767.

int foo(unsigned short x)
{
  unsigned short y;
  y = x > 32767 ? x - 32768 : 0;
  return y;
}

With 16-bit ints unsigned short promotes to unsigned int, since int
does not hold all the values.  Similarly 32768 has type unsigned int
and not int from inception.  So everything is unsigned and it should be:

  y = 32769U > 32767U ? 32769U - 32768U: 0;

which is 1 to my understanding.

Neil.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]