Skip to content

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

Merged
chfast merged 2 commits intomasterfrom
cxx20-bit
Dec 27, 2021
Merged

Use countl_zero/countr_zero from C++20 if available#689
chfast merged 2 commits intomasterfrom
cxx20-bit

Conversation

@axic
Copy link
Copy Markdown
Member

@axic axic commented Jan 8, 2021

Closes #543.

Comment thread test/unittests/execute_numeric_test.cpp Outdated
@codecov
Copy link
Copy Markdown

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%> (ø)

Comment thread lib/fizzy/execute.cpp Outdated
}

inline uint32_t clz32(uint32_t value) noexcept
constexpr uint32_t clz32(uint32_t value) noexcept
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread lib/fizzy/cxx20/bit.hpp Outdated
Comment thread lib/fizzy/cxx20/bit.hpp Outdated
Comment thread lib/fizzy/cxx20/bit.hpp Outdated
Comment thread lib/fizzy/cxx20/bit.hpp Outdated
Comment thread lib/fizzy/cxx20/bit.hpp Outdated
Comment thread lib/fizzy/execute.cpp Outdated
Comment thread lib/fizzy/execute.cpp Outdated
}

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

Comment thread lib/fizzy/execute.cpp Outdated
Comment thread lib/fizzy/execute.cpp Outdated
Comment thread test/unittests/bitcount_test_cases.hpp Outdated
namespace fizzy::test
{
constexpr std::pair<uint32_t, uint32_t> popcount32_test_cases[]{
constexpr std::pair<uint32_t, int> popcount32_test_cases[]{
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread CMakeLists.txt Outdated
Comment thread lib/fizzy/execute.cpp
Copy link
Copy Markdown
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