This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Division by INT64_MIN
- From: Joseph Myers <joseph at codesourcery dot com>
- To: Matthew Fernandez <matthew dot fernandez at gmail dot com>
- Cc: <gcc at gcc dot gnu dot org>
- Date: Mon, 5 Oct 2015 14:09:35 +0000
- Subject: Re: Division by INT64_MIN
- Authentication-results: sourceware.org; auth=none
- References: <5611DC31 dot 3030301 at gmail dot com>
On Mon, 5 Oct 2015, Matthew Fernandez wrote:
> tl;dr: Is dividing by INT64_MIN in C undefined behaviour?
No, only dividing by 0 or dividing the minimum value of the type by -1
(and the latter should be defined for -fwrapv, see bug 30484).
> on x86 is emitted as a call to __divdi3. If the numerator or denominator are
> negative, __divdi3 negates them. If either of these values is INT64_MIN, I
> believe this negation is undefined. Is this correct? If this is the case, then
> it seems code like "INT64_MIN / INT64_MIN" which should be perfectly legal
> accidentally causes undefined behaviour via libgcc. In practice, everything
> seems to work as expected, but it seems to me that the C code of __divdi3
> should not be relying on these negations working consistently.
It sounds like some casts to UDWtype should be inserted so the negation
takes place on an unsigned type.
--
Joseph S. Myers
joseph@codesourcery.com