Skip to content
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

Merged
merged 2 commits into from
Aug 14, 2020
Merged

Implement reinterpret instructions #459

merged 2 commits into from
Aug 14, 2020

Conversation

chfast
Copy link
Collaborator

@chfast chfast commented Aug 4, 2020

image

@chfast chfast force-pushed the fp_demote branch 2 times, most recently from f8a6675 to 20cdd1b Compare August 6, 2020 10:34
@chfast chfast force-pushed the fp_demote branch 4 times, most recently from 0fa4519 to 7d8ad08 Compare August 12, 2020 16:18
@chfast chfast force-pushed the fp_demote branch 2 times, most recently from b5cef53 to 15dba1e Compare August 12, 2020 16:53
@chfast chfast force-pushed the fp_reinterpret branch 2 times, most recently from 8582a80 to 4f0e3a4 Compare August 12, 2020 17:43
@chfast chfast marked this pull request as ready for review August 12, 2020 17:43
@chfast chfast requested review from axic and gumb0 August 12, 2020 17:43
@chfast chfast force-pushed the fp_demote branch 2 times, most recently from a94bc35 to 111b100 Compare August 12, 2020 21:02
lib/fizzy/execute.cpp Outdated Show resolved Hide resolved
@@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What zero-extension?

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I improved the explanation.

@chfast chfast force-pushed the fp_demote branch 9 times, most recently from 28be58f to d99c375 Compare August 14, 2020 13:32
@chfast chfast force-pushed the fp_reinterpret branch 2 times, most recently from 2d3a07a to 0d05d5a Compare August 14, 2020 14:06
Base automatically changed from fp_demote to master August 14, 2020 14:43
/// 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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.

@@ -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.
Copy link
Collaborator

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?

Copy link
Collaborator Author

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
Copy link

codecov bot commented Aug 14, 2020

Codecov Report

Merging #459 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           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));
Copy link
Member

@axic axic Aug 14, 2020

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. You cannot do reinterpret_cast<uint32_t>(f32).
  2. Using pointers would work: *reinterpreter_cast<uint32_t*>(&f32), but in theory that is not valid use of reinterpret_cast and undefined behavior. Using memcpy is the safest way of doing type pruning. Although looks ugly.
  3. The C++20 there will be std::bit_cast - exactly what we need here.

@chfast chfast merged commit fa0ec8d into master Aug 14, 2020
@chfast chfast deleted the fp_reinterpret branch August 14, 2020 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants