Topic: signed overflow is UB, what should numeric_limits<int>::is_modulo


Author: Alberto Barbati <AlbertoBarbati@libero.it>
Date: Thu, 26 Feb 2004 15:33:46 CST
Raw View
Gabriel Dos Reis wrote:
> I think LIA-1 words should apply and numeric_limits<int>::is_modulo
> should be
>
>   (1) false, by default; and
>   (2) true only, when an implementation does chose to define the
>       "undefined behaviour" to be a modulo arithmetic -- in which
>       case, it cannot do arbitrary things.

I am no LIA-1 expert, but I just found this
http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n750.htm
and, if I interpret it well, it contradicts you. About modulo (H.2.2.1),
it sais that:

"The parameter modulo is always true for the unsigned types [...].  The
parameter modulo is true when INT_OUT_OF_BOUNDS is 1 (wrap) or false
when INT_OUT_OF_BOUNDS is 2 (notify) and covers all LIA-1 conformant
signed types.  The implementation picks the value of modulo.  It is
implementation defined if the user can change the value of modulo."

That is (for signed types):
(1) false: notify about overflow via exception
(2) true: wrap modulo INT_MAX-INT_MIN+1

"Undefined behaviour" is not allowed.

It's interesting to notice that LIA-1 allows implementation to change
the value of modulo, however C++ defines is_modulo to be a const bool
and not a function, so the current definition is inadeguate to express
the LIA-1 requirements.

Alberto Barbati

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Alberto Barbati <AlbertoBarbati@libero.it>
Date: Sun, 29 Feb 2004 06:09:17 CST
Raw View
Gabriel Dos Reis wrote:

> Alberto Barbati <AlbertoBarbati@libero.it> writes:
>
> | Gabriel Dos Reis wrote:
> | > I think LIA-1 words should apply and numeric_limits<int>::is_modulo
> | > should be
> | >   (1) false, by default; and
> | >   (2) true only, when an implementation does chose to define the
> | >       "undefined behaviour" to be a modulo arithmetic -- in which
> | >       case, it cannot do arbitrary things.
> |
> | I am no LIA-1 expert, but I just found this
> | http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n750.htm
> | and, if I interpret it well, it contradicts you. About modulo
> | (H.2.2.1), it sais that:
> |
> | "The parameter modulo is always true for the unsigned types [...].
> | The parameter modulo is true when INT_OUT_OF_BOUNDS is 1 (wrap) or
> | false when INT_OUT_OF_BOUNDS is 2 (notify) and covers all LIA-1
> | conformant signed types.  The implementation picks the value of
> | modulo.  It is implementation defined if the user can change the value
> | of modulo."
> |
> | That is (for signed types):
> | (1) false: notify about overflow via exception
> | (2) true: wrap modulo INT_MAX-INT_MIN+1
> |
> | "Undefined behaviour" is not allowed.
> |
> | It's interesting to notice that LIA-1 allows implementation to change
> | the value of modulo, however C++ defines is_modulo to be a const bool
> | and not a function, so the current definition is inadeguate to express
> | the LIA-1 requirements.
>
> No, you're confused.
>
> [...]
>
> In that regard, the C++ definition for numeric_limits<T>::is_modulo
> fulfills the spirit and the letter of the LIA specification.
>

Thanks Gabriel, you have been very informative. Is there a copy of ISO
10967-1 publicly available on the net? I looked here:
http://std.dkuug.dk/JTC1/SC22/WG11/ but it seems that access to the full
document is restricted.

Your remarks are only about my last statement, however. As you look to
be very competent in this area and it seems that you have access to
restricted documents, would you mind discussing my other statement, in
particular the one about the meaning of is_modulo==false which would
mean "notify" rather than "UB"?

Alberto Barbati


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: scott douglass <sdouglass@arm.com>
Date: Tue, 24 Feb 2004 22:34:25 CST
Raw View
Hi,

In the C++ standard signed int overflow is undefined behavior (clause 5 para 5) [and the same is true in C].  Suppose a compiler takes advantage of this to optimize, for example:
    bool f(int i) { return i + 1 < i; }
into this
    bool f(int) { return false; }

Now, how should numeric_limits<int>::is_modulo (18.2.1.2 para 56) be set?  The definition in the C++ standard seems pretty weak:

>>
A type is modulo if it is possible to add two positive numbers and have a
result that wraps around to a third number that is less.
<<

The footnote there says "Required by LIA-1".  LIA-1 gives a stronger definition of modulo in 5.1.2:
  modulo is true if certain particular operations "wrap" and
  modulo is false if overflows cause a notification

But C++ allows more than these two possibilities.  For the implementatin I'm considering some cases will wrap and some will silently give results different from the wrapped results.

In this implementation, what should numeric_limits<int>::is_modulo be?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: James Dennett <jdennett@acm.org>
Date: Wed, 25 Feb 2004 00:38:37 CST
Raw View
scott douglass wrote:

> Hi,
>
> In the C++ standard signed int overflow is undefined behavior (clause 5
> para 5) [and the same is true in C].  Suppose a compiler takes advantage
> of this to optimize, for example:
>    bool f(int i) { return i + 1 < i; }
> into this
>    bool f(int) { return false; }
>
> Now, how should numeric_limits<int>::is_modulo (18.2.1.2 para 56) be
> set?  The definition in the C++ standard seems pretty weak:
>
>>>
> A type is modulo if it is possible to add two positive numbers and have a
> result that wraps around to a third number that is less.
> <<
>
> The footnote there says "Required by LIA-1".  LIA-1 gives a stronger
> definition of modulo in 5.1.2:
>  modulo is true if certain particular operations "wrap" and
>  modulo is false if overflows cause a notification
>
> But C++ allows more than these two possibilities.  For the implementatin
> I'm considering some cases will wrap and some will silently give results
> different from the wrapped results.
>
> In this implementation, what should numeric_limits<int>::is_modulo be?

It would seem, just looking at what you've quoted above, that
the only options for an implementation that conforms to LIA-1
are either modular arithmetic or notification on overflow.

-- James.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]