[m-dev.] proposal: specifying integer overflow behaviour, representations etc

Julien Fischer jfischer at opturion.com
Sat Apr 14 22:56:48 AEST 2018


Hi all,

Below are four proposed changes to the language and one to the implementation.
I would like a yes or no on these before I do any further work adding them
to the reference manual / implement the change.

The proposed language changes are:

    1. Mercury's builtin signed integer types must use two's complement
       representation.

    2. The minimum width of the builtin types int and uint is 32 bits.

    3. On overflow, operations on Mercury's builtin unsigned integer types will
       produce a value that is congruent modulo 2^n to the true mathematical
       result of the operation (i.e. it will wrap around).  n being the width
       of the integer.

    4. On overflow, operations on Mercury's builtin signed integer types will produce
       a result that is the low-order bits of the true two's complement result (i.e
       it will wrap around).

For 3 and 4, the behaviour specified above is the behaviour to be required when
the implementation does *not* check for overflow.

The only thing that needs to change in the implementation is that we do not
technically have (4) for the C backends.  In order to get it, we either need to
compile with GCC's -fwrapv option enabled (or the equivalent for  other C
compilers), or change the code generator so that the following Mercury

    X : int + Y : int

gets compiled into the following C:

    (MR_Integer)((MR_Unsigned)X + (MR_Unsigned)Y)

Similarly, for subtraction and multiplication.  The latter is my preferred
option as I'm not sure if all of the C compilers have an equivalent to the
-fwrapv option and it is more user-proof in any case.

Julien.


More information about the developers mailing list