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

Use countl_zero/countr_zero from C++20 if available #689

Merged
merged 2 commits into from
Dec 27, 2021
Merged

Conversation

axic
Copy link
Member

@axic axic commented Jan 8, 2021

Closes #543.

@codecov
Copy link

codecov bot commented Jan 8, 2021

Codecov Report

Merging #689 (dc8a9d5) into master (a125996) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #689   +/-   ##
=======================================
  Coverage   99.02%   99.02%           
=======================================
  Files          80       80           
  Lines       12788    12812   +24     
=======================================
+ Hits        12663    12687   +24     
  Misses        125      125           
Flag Coverage Δ
rust 99.90% <ø> (ø)
spectests 90.09% <100.00%> (+<0.01%) ⬆️
unittests 98.94% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
lib/fizzy/cxx20/bit.hpp 100.00% <100.00%> (ø)
lib/fizzy/execute.cpp 99.29% <100.00%> (-0.02%) ⬇️
test/unittests/cxx20_bit_test.cpp 100.00% <100.00%> (ø)
test/unittests/execute_numeric_test.cpp 100.00% <100.00%> (ø)

@@ -254,41 +254,29 @@ inline constexpr T rotr(T lhs, T rhs) noexcept
return (lhs >> k) | (lhs << (num_bits - k));
}

inline uint32_t clz32(uint32_t value) noexcept
constexpr uint32_t clz32(uint32_t value) noexcept
Copy link
Member Author

Choose a reason for hiding this comment

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

We can replace these with a template and use them as clz<uint32_t> below. Would cat this code duplication into half.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we dont even need this wrapper layer, the only thing it does is explicit casting of the return value.

lib/fizzy/cxx20/bit.hpp Outdated Show resolved Hide resolved
lib/fizzy/cxx20/bit.hpp Outdated Show resolved Hide resolved
lib/fizzy/cxx20/bit.hpp Outdated Show resolved Hide resolved
lib/fizzy/cxx20/bit.hpp Outdated Show resolved Hide resolved
lib/fizzy/cxx20/bit.hpp Outdated Show resolved Hide resolved
lib/fizzy/execute.cpp Outdated Show resolved Hide resolved
}

inline uint32_t ctz32(uint32_t value) noexcept
constexpr uint32_t ctz32(uint32_t value) noexcept
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
constexpr uint32_t ctz32(uint32_t value) noexcept
inline constexpr uint32_t ctz32(uint32_t value) noexcept

Copy link
Member Author

Choose a reason for hiding this comment

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

#689 (comment)

Actually just removing these wrappers seems to be okay?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Dropping the wrappers means all the results are going to be converted to i32. This is wrong for i64.c[lt]z. This mean you now need to construct a unit test what will reproduce this bug.

Copy link
Member Author

Choose a reason for hiding this comment

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

So how about the template proposed in #689 (comment) ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Template is fine.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This mean you now need to construct a unit test what will reproduce this bug.

This is still missing. It requires a special test where you pollute the stack first and then assign result of i64.clz. If the result is int some garbage should stay in the value. One case should be enough. See TEST(execute_numeric, i64_extend_i32_u).

I can also handle this later.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks it is not easy to omit the wrapper so I will skip creating tests for this.

lib/fizzy/execute.cpp Outdated Show resolved Hide resolved
lib/fizzy/execute.cpp Outdated Show resolved Hide resolved
@@ -9,7 +9,7 @@

namespace fizzy::test
{
constexpr std::pair<uint32_t, uint32_t> popcount32_test_cases[]{
constexpr std::pair<uint32_t, int> popcount32_test_cases[]{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Duplicating inputs does not make sense. Create a struct with results for all instructions.

template <T>
struct SomeGoodName
{
   T input;
   int popcount;
   int countl_zero;
   int countr_zero;
};

constexpr SomeGoodName<uint32_t> bit_counting32_test_cases[]{
    {0, 0, 32, 32},
    {0x80, 1, 24, 7},
    ...
};

Copy link
Member Author

Choose a reason for hiding this comment

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

I will keep this a separate commit even for merging, as not sure it is more readable alltogether.

CMakeLists.txt Outdated Show resolved Hide resolved
Copy link
Collaborator

@gumb0 gumb0 left a comment

Choose a reason for hiding this comment

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

Looks good, don't forget to remove the change in CMakeLists.txt

@chfast chfast merged commit 95d5087 into master Dec 27, 2021
@chfast chfast deleted the cxx20-bit branch December 27, 2021 10:11
@axic axic mentioned this pull request May 14, 2022
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.

Make use of C++20 bit manipulation features
4 participants