public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/31075]  New: 2's complement arithmetic (LLONG_MIN-1) works differently with and without optimization
@ 2007-03-07 19:50 sdirkse at gams dot com
  2007-03-07 20:23 ` [Bug c/31075] " pinskia at gcc dot gnu dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: sdirkse at gams dot com @ 2007-03-07 19:50 UTC (permalink / raw)
  To: gcc-bugs

testing if LLONG_MIN-1 == LLONG_MAX gives different results with -O3 and
without.  Here's a short example and the output I get on my machine.

mamie:/home/distrib/lang/pas$cat mini64.c
#include <stdio.h>
typedef signed long long int int64;
#define MAXINT64  9223372036854775807LL
#define MININT64 ((-9223372036854775807LL)-1LL)

int main (int argc, char **argv)
{
  int64 minint64;

  printf ("mini64: test 2's-complement arithmetic\n");
  minint64 = MININT64;
  printf ("minint64-1 = %lld\n", minint64-1);
  if (minint64 - 1 != MAXINT64) {
    printf ("minint64 - 1 <> MAXINT64\n");
  }
  else {
    printf ("minint64 - 1 == MAXINT64\n");
  }
  return 0;
} /* main */
mamie:/home/distrib/lang/pas$gcc -o mini64 mini64.c && ./mini64
mini64: test 2's-complement arithmetic
minint64-1 = 9223372036854775807
minint64 - 1 == MAXINT64
mamie:/home/distrib/lang/pas$gcc -O3 -o mini64 mini64.c && ./mini64
mini64: test 2's-complement arithmetic
minint64-1 = 9223372036854775807
minint64 - 1 <> MAXINT64
mamie:/home/distrib/lang/pas$gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../configure --enable-languages=c,c++,fortran
Thread model: posix
gcc version 4.3.0 20070305 (experimental)


-- 
           Summary: 2's complement arithmetic (LLONG_MIN-1) works
                    differently with and without optimization
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sdirkse at gams dot com
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31075


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

* [Bug c/31075] 2's complement arithmetic (LLONG_MIN-1) works differently with and without optimization
  2007-03-07 19:50 [Bug c/31075] New: 2's complement arithmetic (LLONG_MIN-1) works differently with and without optimization sdirkse at gams dot com
@ 2007-03-07 20:23 ` pinskia at gcc dot gnu dot org
  2007-03-07 22:52 ` sdirkse at gams dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-03-07 20:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2007-03-07 20:23 -------
Signed integer overflow is undefined.  Either use unsigned or -fwrapv.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31075


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

* [Bug c/31075] 2's complement arithmetic (LLONG_MIN-1) works differently with and without optimization
  2007-03-07 19:50 [Bug c/31075] New: 2's complement arithmetic (LLONG_MIN-1) works differently with and without optimization sdirkse at gams dot com
  2007-03-07 20:23 ` [Bug c/31075] " pinskia at gcc dot gnu dot org
@ 2007-03-07 22:52 ` sdirkse at gams dot com
  2007-03-07 23:00 ` pinskia at gcc dot gnu dot org
  2007-03-07 23:10 ` sdirkse at gams dot com
  3 siblings, 0 replies; 5+ messages in thread
From: sdirkse at gams dot com @ 2007-03-07 22:52 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from sdirkse at gams dot com  2007-03-07 22:52 -------
Thanks for telling me about the -fwrapv flag, that's good to know - I should
have double-checked my K&R 2nd Edition before sending the bug.  But I tried
running the above example with the addition of the -fwrapv flag, and it didn't
change the behavior one bit.

Also, I tried a similar example with 32- instead of 64-bit types.  It behaved
similarly.

So, can we call this a bug now?


-- 

sdirkse at gams dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31075


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

* [Bug c/31075] 2's complement arithmetic (LLONG_MIN-1) works differently with and without optimization
  2007-03-07 19:50 [Bug c/31075] New: 2's complement arithmetic (LLONG_MIN-1) works differently with and without optimization sdirkse at gams dot com
  2007-03-07 20:23 ` [Bug c/31075] " pinskia at gcc dot gnu dot org
  2007-03-07 22:52 ` sdirkse at gams dot com
@ 2007-03-07 23:00 ` pinskia at gcc dot gnu dot org
  2007-03-07 23:10 ` sdirkse at gams dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-03-07 23:00 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2007-03-07 23:00 -------
[apinski@dhcp-10-98-10-216 ~]$ ~/gcc-mainline/bin/gcc t.c -O2 -fwrapv
[apinski@dhcp-10-98-10-216 ~]$ !./
./a.out
mini64: test 2's-complement arithmetic
minint64-1 = 9223372036854775807
minint64 - 1 == MAXINT64
[apinski@dhcp-10-98-10-216 ~]$ ~/gcc-mainline/bin/gcc t.c -O2
[apinski@dhcp-10-98-10-216 ~]$ !./
./a.out
mini64: test 2's-complement arithmetic
minint64-1 = 9223372036854775807
minint64 - 1 <> MAXINT64


[apinski@dhcp-10-98-10-216 ~]$ ~/gcc-mainline/bin/gcc -v
Using built-in specs.
Target: powerpc64-unknown-linux-gnu
Configured with: /home/apinski/src/local/gcc/configure
--prefix=/home/apinski/gcc-mainline --with-mpfr=/usr/local --with-cpu=default32
Thread model: posix
gcc version 4.3.0 20070303 (experimental)


Unless something changed in the last two days.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31075


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

* [Bug c/31075] 2's complement arithmetic (LLONG_MIN-1) works differently with and without optimization
  2007-03-07 19:50 [Bug c/31075] New: 2's complement arithmetic (LLONG_MIN-1) works differently with and without optimization sdirkse at gams dot com
                   ` (2 preceding siblings ...)
  2007-03-07 23:00 ` pinskia at gcc dot gnu dot org
@ 2007-03-07 23:10 ` sdirkse at gams dot com
  3 siblings, 0 replies; 5+ messages in thread
From: sdirkse at gams dot com @ 2007-03-07 23:10 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from sdirkse at gams dot com  2007-03-07 23:10 -------
DOH!  I wasn't running the executable compiled with -fwrapv.  Using the -fwrapv
flag does indeed make things work as I hoped and as documented.


-- 

sdirkse at gams dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31075


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

end of thread, other threads:[~2007-03-07 23:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-07 19:50 [Bug c/31075] New: 2's complement arithmetic (LLONG_MIN-1) works differently with and without optimization sdirkse at gams dot com
2007-03-07 20:23 ` [Bug c/31075] " pinskia at gcc dot gnu dot org
2007-03-07 22:52 ` sdirkse at gams dot com
2007-03-07 23:00 ` pinskia at gcc dot gnu dot org
2007-03-07 23:10 ` sdirkse at gams dot com

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).