public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* INT_MIN % -1
@ 2004-11-29 23:38 Morten Welinder
  2004-11-30  0:24 ` David Daney
  2004-11-30  0:45 ` Andreas Schwab
  0 siblings, 2 replies; 9+ messages in thread
From: Morten Welinder @ 2004-11-29 23:38 UTC (permalink / raw)
  To: gcc


What is the value of  INT_MIN % -1  supposed to be, assuming 32-bit
ints and two-complement representation.

C99 seems to be telling us two things about '%':

(1) It computes remainder for integer division.
(2) "If the quotient  a/b  is representable, the expression
    (a/b)*b + a%b  shall equal  a."

(2) is not useful for the case since the quotient is not representable.  I'd
say that (1) means that the result should be zero, but that would be rather
expensive to implement as you would have to test for -1 at runtime on i386,
for example.

I would claim that there is no overflow in the calculation: both arguments
and the result are perfectly representable as 32-bit ints.  The fact that
the quotient of the same values, if performed, overflows is no more relevant
that the fact that you can't take the square root of the two sides.

gcc 3.4 on solaris/sparc seems to get zero; gcc 3.3.1 on linux gives me a
crash at runtime.  (Because the signed integer division instruction traps
as documented.)

Comments?

Morten



-----------------------------------------------------------------------------

> uname -a
SunOS troll 5.8 Generic_117350-11 sun4u sparc
> ./a.out 
-2147483648 % -1 (const) = 0
-2147483648 % -1 (non-const) = 0

-----------------------------------------------------------------------------

> uname -a
Linux darter 2.4.21-243-default #1 Thu Aug 12 15:22:14 UTC 2004 i686 i686 i386 GNU/Linux
> ./a.out 
-2147483648 % -1 (const) = 0
Floating point exception

-----------------------------------------------------------------------------

#include <stdio.h>
#include <limits.h>

#define A INT_MIN
#define B -1

static int a = A;
static int b = B;

int
main (int argc, char **argv)
{
  printf ("%d %% %d (const) = %d\n", A, B, A % B);
  printf ("%d %% %d (non-const) = %d\n", a, b, a % b);
  return 0;
}

^ permalink raw reply	[flat|nested] 9+ messages in thread
* Re: INT_MIN % -1
@ 2004-11-30  4:25 Paul Schlie
  2004-11-30  7:02 ` Robert Dewar
  0 siblings, 1 reply; 9+ messages in thread
From: Paul Schlie @ 2004-11-30  4:25 UTC (permalink / raw)
  To: gcc

> Andreas wrote:
>> terra@gnome.org (Morten Welinder) writes:
>>
>> gcc 3.4 on solaris/sparc seems to get zero; gcc 3.3.1 on linux gives me a
>> crash at runtime.  (Because the signed integer division instruction traps
> as documented.)
>
> FWIW, on m68k you'll get -1 (because the divs insn does not modify the
> operands on overflow and doesn't trap either).

Then would guess divs shouldn't be used with operands which may yield
needlessly incorrect results, and would guess it should likely be considered
a code-generator bug if it is; as a compiler which produces code which
may generate needlessly incorrect results (even if misguidedly allowed
by the standard), isn't truly as useful as one which reliably produces
reasonably correct results.

(where I'm assuming: (-INT_MIN / -1) => -1 isn't reasonably correct or
necessary, nor is a run-time exception/trap)

Although possibly naive, I'd like to think that the C standard won't be used
as a crutch, but that GCC may rise above many of the unspecified behaviors,
and establish instead, well defined logically consistent useful ones, which
others may aspire to emulate.


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2004-12-01 11:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-29 23:38 INT_MIN % -1 Morten Welinder
2004-11-30  0:24 ` David Daney
2004-11-30  0:45 ` Andreas Schwab
2004-11-30  4:25 Paul Schlie
2004-11-30  7:02 ` Robert Dewar
2004-11-30 22:47   ` Paul Schlie
2004-12-01  0:39     ` Robert Dewar
2004-12-01  3:22       ` Paul Schlie
2004-12-01 11:40         ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).