This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Fixing the wrapping issue
Joseph S. Myers wrote:
> On Mon, 15 Aug 2005, Daniel Berlin wrote:
>
> > > Could somebody recall me what is the result of the following conversion:
> > >
> > > u = (unsigned char) 255;
> > > s = (signed char) u;
> > >
> > > is the signed char conversion defined to be -1 or not defined in this case?
> >
> > 6.3.1.3 Signed and unsigned integers
> > 1 When a value with integer type is converted to another integer type
> > other than _Bool,
> > the value can be represented by the new type, it is unchanged.
> > 2 Otherwise, if the new type is unsigned, the value is converted by
> > repeatedly adding
> > subtracting one more than the maximum value that can be represented in
> > the new
> > until the value is in the range of the new type.49)
> > 3 Otherwise, the new type is signed and the value cannot be represented
> > in it; either
> > result is implementation-defined or an implementation-defined signal is
> > raised.
> >
> >
> >
> > The new type here is signed, so it falls into category 3.
>
> And in implement-c.texi we duly document the definition of such
> conversions for GNU C.
>
Thanks for the pointer. This says:
@cite{The result of, or the signal raised by, converting an integer to a
signed integer type when the value cannot be represented in an object of
that type (C90 6.2.1.2, C99 6.3.1.3).}
For conversion to a type of width @math{N}, the value is reduced
modulo @math{2^N} to be within range of the type; no signal is raised.