public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/115019] New: wraparound bug with with -O3 and int128
@ 2024-05-10  1:31 djnz00 at gmail dot com
  2024-05-10  1:37 ` [Bug c/115019] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: djnz00 at gmail dot com @ 2024-05-10  1:31 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115019
           Summary: wraparound bug with with -O3 and int128
           Product: gcc
           Version: 14.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: djnz00 at gmail dot com
  Target Milestone: ---

Created attachment 58157
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58157&action=edit
reduced testcase

wraparound and condition testing of 128bit values performs incorrectly under
-O3:

  __int128_t i = ((__uint128_t)1)<<127;
  do {
    puts(itoa128(buf, i)); /* do something with i */
    i += ((__uint128_t)1)<<125;
  } while (i);

in other code, this loop runs interminably under -O3, but completes as expected
without optimization; in this reduced testcase sample, it only executes one
repeat of the loop (clang does it correctly); either way, there is a divergence
of behavior with/without -O3

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

* [Bug c/115019] wraparound bug with with -O3 and int128
  2024-05-10  1:31 [Bug c/115019] New: wraparound bug with with -O3 and int128 djnz00 at gmail dot com
@ 2024-05-10  1:37 ` pinskia at gcc dot gnu.org
  2024-05-10  1:50 ` pinskia at gcc dot gnu.org
  2024-05-10  1:54 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-10  1:37 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
                URL|https://godbolt.org/z/be37n |
                   |7Mz3                        |
               Host|x86_64-pc-linux-gnu         |
              Build|x86_64-pc-linux-gnu         |
             Status|UNCONFIRMED                 |RESOLVED
             Target|x86_64-pc-linux-gnu         |

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is an undefined behavior with signed integer overflow. Did you try with
-fsanitize=undefined as requested by the bug reporting page?

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

* [Bug c/115019] wraparound bug with with -O3 and int128
  2024-05-10  1:31 [Bug c/115019] New: wraparound bug with with -O3 and int128 djnz00 at gmail dot com
  2024-05-10  1:37 ` [Bug c/115019] " pinskia at gcc dot gnu.org
@ 2024-05-10  1:50 ` pinskia at gcc dot gnu.org
  2024-05-10  1:54 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-10  1:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
/app/example.cpp:36:24: runtime error: negation of
0x80000000000000000000000000000000 cannot be represented in type '__int128';
cast to an unsigned type to negate this value to itself

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

* [Bug c/115019] wraparound bug with with -O3 and int128
  2024-05-10  1:31 [Bug c/115019] New: wraparound bug with with -O3 and int128 djnz00 at gmail dot com
  2024-05-10  1:37 ` [Bug c/115019] " pinskia at gcc dot gnu.org
  2024-05-10  1:50 ` pinskia at gcc dot gnu.org
@ 2024-05-10  1:54 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-10  1:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> /app/example.cpp:36:24: runtime error: negation of
> 0x80000000000000000000000000000000 cannot be represented in type '__int128';
> cast to an unsigned type to negate this value to itself

Once I fix that with:
```
static char *itoa128(char *buf, __int128_t v)
{
  if (v < 0) {
    buf = utoa128(buf, -(__uint128_t)v);
```

GCC behavior is what you expect.

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

end of thread, other threads:[~2024-05-10  1:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-10  1:31 [Bug c/115019] New: wraparound bug with with -O3 and int128 djnz00 at gmail dot com
2024-05-10  1:37 ` [Bug c/115019] " pinskia at gcc dot gnu.org
2024-05-10  1:50 ` pinskia at gcc dot gnu.org
2024-05-10  1:54 ` pinskia 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).