public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106542] New: -O2 sign-extended uint32 to uint64
@ 2022-08-06  2:46 wuz73 at hotmail dot com
  2022-08-06  3:32 ` [Bug c++/106542] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: wuz73 at hotmail dot com @ 2022-08-06  2:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106542
           Summary: -O2 sign-extended uint32 to uint64
           Product: gcc
           Version: 9.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wuz73 at hotmail dot com
  Target Milestone: ---

#include <iostream>

unsigned Msb(uint64_t x) { return 63 - __builtin_clzl(x); }

template<typename T>
void f(T r, T p)
{
    if (p > r)
    {
        auto d = p - r;
        unsigned k = Msb(sizeof(T) == 4 ? unsigned(d) : d);
        std::cout << sizeof(T) << " k=" << k << "\n";
    }
}

int main()
{
  int data[] = {-1610499096,2086724600};
  f(data[0],data[1]);
}

When compiled with "g++ -O2" the output is "4 k=63". Apparently before calling
Msb(), g++ sign-extended unsigned(d). static_cast<unsigend>(d) yields the same
result. However, "g++ -O0" is correct (k=31), so are earlier versions of g++
such as 4.8.5 and 5.3.1.

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

* [Bug c++/106542] -O2 sign-extended uint32 to uint64
  2022-08-06  2:46 [Bug c++/106542] New: -O2 sign-extended uint32 to uint64 wuz73 at hotmail dot com
@ 2022-08-06  3:32 ` pinskia at gcc dot gnu.org
  2022-08-06  3:51 ` wuz73 at hotmail dot com
  2022-08-06  3:58 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-08-06  3:32 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The code is undefined and can be detected with -fsanitize=undefined:

/app/example.cpp:15:14: runtime error: signed integer overflow: 2086724600 -
-1610499096 cannot be represented in type 'int'

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

* [Bug c++/106542] -O2 sign-extended uint32 to uint64
  2022-08-06  2:46 [Bug c++/106542] New: -O2 sign-extended uint32 to uint64 wuz73 at hotmail dot com
  2022-08-06  3:32 ` [Bug c++/106542] " pinskia at gcc dot gnu.org
@ 2022-08-06  3:51 ` wuz73 at hotmail dot com
  2022-08-06  3:58 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: wuz73 at hotmail dot com @ 2022-08-06  3:51 UTC (permalink / raw)
  To: gcc-bugs

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

wuz73 at hotmail dot com changed:

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

--- Comment #2 from wuz73 at hotmail dot com ---
(In reply to Andrew Pinski from comment #1)
> The code is undefined and can be detected with -fsanitize=undefined:
> 
> /app/example.cpp:15:14: runtime error: signed integer overflow: 2086724600 -
> -1610499096 cannot be represented in type 'int'

I'm only interested in the lower 32 bits, hence the unsigned(d).

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

* [Bug c++/106542] -O2 sign-extended uint32 to uint64
  2022-08-06  2:46 [Bug c++/106542] New: -O2 sign-extended uint32 to uint64 wuz73 at hotmail dot com
  2022-08-06  3:32 ` [Bug c++/106542] " pinskia at gcc dot gnu.org
  2022-08-06  3:51 ` wuz73 at hotmail dot com
@ 2022-08-06  3:58 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-08-06  3:58 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The code is undefined as the subtraction is done in the int type.
You either need to use -fwrapv or cast to an unsigned type before doing the
subtraction.

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

end of thread, other threads:[~2022-08-06  3:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-06  2:46 [Bug c++/106542] New: -O2 sign-extended uint32 to uint64 wuz73 at hotmail dot com
2022-08-06  3:32 ` [Bug c++/106542] " pinskia at gcc dot gnu.org
2022-08-06  3:51 ` wuz73 at hotmail dot com
2022-08-06  3:58 ` 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).