public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/101938] New: [12 Regression] Wrong code with -fwrapv since r12-2591-g2e96b5f14e402569
@ 2021-08-16 16:07 marxin at gcc dot gnu.org
  2021-08-16 16:07 ` [Bug tree-optimization/101938] " marxin at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: marxin at gcc dot gnu.org @ 2021-08-16 16:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101938

            Bug ID: 101938
           Summary: [12 Regression] Wrong code with -fwrapv since
                    r12-2591-g2e96b5f14e402569
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marxin at gcc dot gnu.org
                CC: aldyh at gcc dot gnu.org, amacleod at redhat dot com
  Target Milestone: ---

It's a test-case reduced from postgresql package:

$ cat postgresql.c 
#include <stdio.h>

typedef long long int int64;

#define INT64CONST(x)  (x##LL)
#define PG_INT64_MIN    (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)

static int64
__attribute__((noipa))
int8gcd_internal(int64 arg1, int64 arg2)
{
        int64           swap;
        int64           a1,
                                a2;

        /*
         * Put the greater absolute value in arg1.
         *
         * This would happen automatically in the loop below, but avoids an
         * expensive modulo operation, and simplifies the special-case handling
         * for INT64_MIN below.
         *
         * We do this in negative space in order to handle INT64_MIN.
         */
        a1 = (arg1 < 0) ? arg1 : -arg1;
        a2 = (arg2 < 0) ? arg2 : -arg2;
        if (a1 > a2)
        {
                swap = arg1;
                arg1 = arg2;
                arg2 = swap;
        }

        /* Special care needs to be taken with INT64_MIN.  See comments above.
*/
        if (arg1 == PG_INT64_MIN)
        {
                if (arg2 == 0 || arg2 == PG_INT64_MIN)
    {
      __builtin_printf ("ret=out of range\n");
    }

                /*
                 * Some machines throw a floating-point exception for INT64_MIN
% -1,
                 * which is a bit silly since the correct answer is perfectly
                 * well-defined, namely zero.  Guard against this and just
return the
                 * result, gcd(INT64_MIN, -1) = 1.
                 */
                if (arg2 == -1) {
      __builtin_printf ("ret=%d\n", 1);
                        return 1;
    }
        }

        /* Use the Euclidean algorithm to find the GCD */
        while (arg2 != 0)
        {
                swap = arg2;
                arg2 = arg1 % arg2;
                arg1 = swap;
        }

        /*
         * Make sure the result is positive. (We know we don't have INT64_MIN
         * anymore).
         */
        if (arg1 < 0)
                arg1 = -arg1;

  __builtin_printf ("ret=%lld\n", arg1);
        return arg1;
}

int main()
{
  int8gcd_internal (-1, -9223372036854775808ULL);
  return 0;
}

$ gcc postgresql.c -fwrapv && ./a.out 
ret=1
$ gcc postgresql.c -O2 -fwrapv && ./a.out 
Floating point exception (core dumped)

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

end of thread, other threads:[~2021-08-17  9:43 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-16 16:07 [Bug tree-optimization/101938] New: [12 Regression] Wrong code with -fwrapv since r12-2591-g2e96b5f14e402569 marxin at gcc dot gnu.org
2021-08-16 16:07 ` [Bug tree-optimization/101938] " marxin at gcc dot gnu.org
2021-08-16 16:19 ` marxin at gcc dot gnu.org
2021-08-17  7:43 ` rguenth at gcc dot gnu.org
2021-08-17  8:58 ` aldyh at gcc dot gnu.org
2021-08-17  8:59 ` aldyh at gcc dot gnu.org
2021-08-17  9:39 ` cvs-commit at gcc dot gnu.org
2021-08-17  9:40 ` aldyh at gcc dot gnu.org
2021-08-17  9:42 ` marxin at gcc dot gnu.org
2021-08-17  9:43 ` aldyh at gcc dot gnu.org

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