public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111436] New: Wrong code when bit-casting struct through pointer
@ 2023-09-16 21:37 josopait at goopax dot com
2023-09-16 22:17 ` [Bug c++/111436] " pinskia at gcc dot gnu.org
2023-09-17 20:59 ` redi at gcc dot gnu.org
0 siblings, 2 replies; 3+ messages in thread
From: josopait at goopax dot com @ 2023-09-16 21:37 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111436
Bug ID: 111436
Summary: Wrong code when bit-casting struct through pointer
Product: gcc
Version: 12.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: josopait at goopax dot com
Target Milestone: ---
Does gcc produce wrong code here? The program below is part of a bitwise
conversion mechanism, similar to std::bit_cast, but for more general types.
The output should be:
from=17,23
to=17,23
But with -O2 or higher the output is:
from=17,23
to=0,0
I tried with gcc 12, 13, and 14 on x86_64 linux.
#include <iostream>
#include <cstdint>
#include <array>
using namespace std;
template<typename TO, typename FROM>
inline void reinterpret_switch3(TO& to, const FROM& from)
{
to = *reinterpret_cast<const TO*>(&from);
}
template<typename TO, typename FROM>
inline void reinterpret_switch2(TO& to, const FROM& from)
{
reinterpret_switch3(to, array<FROM, 1>({ { from } }));
}
template<typename TO, typename FROM>
inline void reinterpret_switch(TO& to, const FROM& from)
{
array<TO, 1> tmp;
reinterpret_switch2(tmp, from);
to = tmp[0];
}
int main(int argc, char** argv)
{
pair<uint32_t, uint32_t> from = {17, 23};
pair<int32_t, int32_t> to;
reinterpret_switch(to, from);
cout << "from=" << from.first << "," << from.second << endl;
cout << "to=" << to.first << "," << to.second << endl;
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug c++/111436] Wrong code when bit-casting struct through pointer
2023-09-16 21:37 [Bug c++/111436] New: Wrong code when bit-casting struct through pointer josopait at goopax dot com
@ 2023-09-16 22:17 ` pinskia at gcc dot gnu.org
2023-09-17 20:59 ` redi at gcc dot gnu.org
1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-16 22:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111436
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |INVALID
Status|UNCONFIRMED |RESOLVED
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
You are type punning and that will almost definitely cause undefined code due
to alias violations.
You could use memcpy instead of the which will (almost; standard layout
structures [or POD for pre C++11]) always work.
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug c++/111436] Wrong code when bit-casting struct through pointer
2023-09-16 21:37 [Bug c++/111436] New: Wrong code when bit-casting struct through pointer josopait at goopax dot com
2023-09-16 22:17 ` [Bug c++/111436] " pinskia at gcc dot gnu.org
@ 2023-09-17 20:59 ` redi at gcc dot gnu.org
1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2023-09-17 20:59 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111436
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Please re-read the notice that was shown before you created this bug:
"Before reporting a bug, please read the bug writing guidelines, please look at
the list of most frequently reported bugs, and please search for the bug."
"Before reporting that GCC compiles your code incorrectly, compile it with gcc
-Wall -Wextra and see whether this shows anything wrong with your code.
Similarly, if compiling with -fno-strict-aliasing -fwrapv makes a difference,
your code probably is not correct."
Your code produces the expected results with -fno-strict-aliasing and as it
says, that means your code is probably not correct. In this case, it's
definitely not correct.
The reason std::bit_cast doesn't allow more general types is because C++
doesn't support doing it. You can't just reinterpret_cast and expect it to
work.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-09-17 20:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-16 21:37 [Bug c++/111436] New: Wrong code when bit-casting struct through pointer josopait at goopax dot com
2023-09-16 22:17 ` [Bug c++/111436] " pinskia at gcc dot gnu.org
2023-09-17 20:59 ` redi 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).