-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement reinterpret instructions #459
Conversation
chfast
commented
Aug 4, 2020
•
edited
Loading
edited
f8a6675
to
20cdd1b
Compare
0fa4519
to
7d8ad08
Compare
b5cef53
to
15dba1e
Compare
8582a80
to
4f0e3a4
Compare
a94bc35
to
111b100
Compare
lib/fizzy/execute.cpp
Outdated
@@ -357,6 +357,20 @@ inline void convert(OperandStack& stack) noexcept | |||
stack.top() = static_cast<DstT>(stack.top().as<SrcT>()); | |||
} | |||
|
|||
/// Performs a bitcast from SrcT type to DstT type. | |||
/// | |||
/// This is a formality (except for f32 -> i32 where zero-extension is needed) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What zero-extension?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we push a i32
value to the stack we actually zero-extend it to i64
in Value
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I improved the explanation.
28be58f
to
d99c375
Compare
2d3a07a
to
0d05d5a
Compare
lib/fizzy/execute.cpp
Outdated
/// Performs a bit_cast from SrcT type to DstT type. | ||
/// | ||
/// This is a formality and should be optimized to empty function in assembly. | ||
/// Except for f32 -> i32 where pushing the result i32 value to the stack requires zero-extension. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Except for f32 -> i32 where pushing the result i32 value to the stack requires zero-extension. | |
/// Except for f32 -> i32 where pushing the result i32 value to the stack requires zero-extension to 64-bit. |
lib/fizzy/execute.cpp
Outdated
@@ -357,6 +357,20 @@ inline void convert(OperandStack& stack) noexcept | |||
stack.top() = static_cast<DstT>(stack.top().as<SrcT>()); | |||
} | |||
|
|||
/// Performs a bit_cast from SrcT type to DstT type. | |||
/// | |||
/// This is a formality and should be optimized to empty function in assembly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I wouldn't call it a "formality". Compiler can optimize it away, but from C++ point of view it's required to avoid UB, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what I call formality.
Codecov Report
@@ Coverage Diff @@
## master #459 +/- ##
=======================================
Coverage 99.55% 99.55%
=======================================
Files 54 54
Lines 16685 16729 +44
=======================================
+ Hits 16611 16655 +44
Misses 74 74 |
static_assert(sizeof(SrcT) == sizeof(DstT)); | ||
const auto src = stack.top().as<SrcT>(); | ||
DstT dst; | ||
__builtin_memcpy(&dst, &src, sizeof(dst)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why couldn't reinterpret_cast
work here instead? Or the memcpy option somehow magically does zero extension?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- You cannot do
reinterpret_cast<uint32_t>(f32)
. - Using pointers would work:
*reinterpreter_cast<uint32_t*>(&f32)
, but in theory that is not valid use ofreinterpret_cast
and undefined behavior. Usingmemcpy
is the safest way of doing type pruning. Although looks ugly. - The C++20 there will be
std::bit_cast
- exactly what we need here.