PATCH: Bad signed int to long long conversion

Vincent Lefevre vincent at vinc17.net
Fri Feb 10 11:30:05 CET 2012


On 2012-02-09 18:17:17 +0100, Torbjorn Granlund wrote:
> Vincent Lefevre <vincent at vinc17.net> writes:
> 
>   On 2012-02-09 17:01:45 +0100, Torbjorn Granlund wrote:
>   > Strictly speaking, I don't think things like (unsigned long)(MIN_LONG)
>   > is well-defined C.
>   
>   It is well-defined:
>   
>         6.3.1.3 Signed and unsigned integers
>   
>     1   When a value with integer type is converted to another integer
>         type other than _Bool, if 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 or subtracting one more than the maximum
>         value that can be represented in the new type until the value
>         is in the range of the new type.60)
>   
> Rule 2 surely covers this.  Is this from C99?

This is from (the new) C11, but C99 had the same rule.

> IIRC, this was not well-defined in C89. Am I wrong?

The rationale (for both C89 and C99) says:

  6.3.1.3        Signed and unsigned integers

  Precise rules are now provided for converting to and from unsigned
  integers. On a two's-complement machine, the operation is still
  virtual (no change of representation is required), but the rules
  are now stated independent of representation.

So, there was a change either before or after C89, but if I understand
correctly, machines with a two's-complement are not affected. So, the
number of affected still-used implementations should be close to 0.

-- 
Vincent Lefèvre <vincent at vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


More information about the gmp-bugs mailing list