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 popcount from C++20 if available #551

Merged
merged 3 commits into from
Oct 13, 2020
Merged

Use popcount from C++20 if available #551

merged 3 commits into from
Oct 13, 2020

Conversation

axic
Copy link
Member

@axic axic commented Sep 24, 2020

Part of #543.

lib/fizzy/execute.cpp Outdated Show resolved Hide resolved
@axic axic force-pushed the popcount branch 2 times, most recently from 27d4d43 to 88582bd Compare September 30, 2020 13:00
@axic axic marked this pull request as ready for review September 30, 2020 13:00
@axic axic requested review from chfast and gumb0 and removed request for chfast and gumb0 September 30, 2020 13:01
template <class T>
constexpr int popcount(T x) noexcept
{
return std::popcount.as<T>(x);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this?

Copy link
Member Author

Choose a reason for hiding this comment

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

The using statement didn't seem to work for functions. We still want to "copy" it from std to fizzy.

Copy link
Collaborator

Choose a reason for hiding this comment

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

using does not work because functions are constant objects, not types. This is good approach. But the .as<T> is wrong. And because of we cannot test it now, maybe add static_assert() for now?

Copy link
Member Author

Choose a reason for hiding this comment

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

static_assert for what? The two types we support/test?

Copy link
Member Author

Choose a reason for hiding this comment

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

In that case it may be better not to use a template just the two specialisations.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I propose to this for now:

#if __cpp_lib_bitops
static_assert(false, "Add alias to std::popcount");
#endif 

As for function alias, only template function is generic function alias. There is also an option to use constexpr, but not for overloaded functions: https://stackoverflow.com/questions/9864125/c11-how-to-alias-a-function/49819511#49819511.

Copy link
Member Author

Choose a reason for hiding this comment

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

Why don't we add a test run for C++20? There is just no point adding these things otherwise.

Copy link
Collaborator

Choose a reason for hiding this comment

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

None of the C++ stdlib implementations have <bit> support.

lib/fizzy/cxx20/numeric.hpp Outdated Show resolved Hide resolved

#pragma once

#if __cplusplus > 201703L
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will not work, because you may be building in C++20, but without <bit> implemented.
Use __cpp_lib_bitops.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should use __cpp_lib_span for span too. And likewise #542 (comment) could be merged.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Correct, although the std::bit_cast is similarly untestable.

lib/fizzy/cxx20/numeric.hpp Outdated Show resolved Hide resolved
lib/fizzy/cxx20/numeric.hpp Outdated Show resolved Hide resolved
@gumb0
Copy link
Collaborator

gumb0 commented Sep 30, 2020

Looks good, needs some unit tests.

@axic
Copy link
Member Author

axic commented Sep 30, 2020

Looks good, needs some unit tests.

We do have tests for them via the opcodes, but you are right probably better to have unit tests for any of these wrappers.

@axic axic marked this pull request as draft September 30, 2020 20:23
@codecov
Copy link

codecov bot commented Sep 30, 2020

Codecov Report

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

@@           Coverage Diff           @@
##           master     #551   +/-   ##
=======================================
  Coverage   98.25%   98.26%           
=======================================
  Files          63       63           
  Lines        9243     9263   +20     
=======================================
+ Hits         9082     9102   +20     
  Misses        161      161           

return __builtin_popcount(x);
}

constexpr int popcount(uint64_t x) 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.

These should be inline, not consexpr as __builtin_popcount isn't constexpr.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Most builtins are constexpr. https://godbolt.org/z/Pnz4ob.

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool, the gcc documentation had no mention of either.

Copy link
Member Author

Choose a reason for hiding this comment

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

@axic axic force-pushed the popcount branch 2 times, most recently from a4c8353 to 45dc469 Compare October 8, 2020 17:41
@axic
Copy link
Member Author

axic commented Oct 8, 2020

@gumb0 added tests if you want to check

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 to me, needs squashing.

@axic
Copy link
Member Author

axic commented Oct 9, 2020

Will squash, but this is still blocked by clang11 for having a way to tests the C++20 implementation.

capi_test.cpp
constexpr_vector_test.cpp
cxx20_bit_test.cpp
cxx20_span_test.cpp
Copy link
Member Author

Choose a reason for hiding this comment

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

@gumb0 @chfast what do you think about this renaming?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's fine.

@axic axic marked this pull request as ready for review October 13, 2020 18:52
@axic axic requested review from chfast and gumb0 October 13, 2020 18:52
@@ -296,7 +296,7 @@ inline uint64_t ctz64(uint64_t value) noexcept

inline uint64_t popcnt64(uint64_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.

Should make these constexpr then since we established the intrinsics are.


#else

#include <cstdint>
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is also needed for C++20 variant.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right. Will fix. Though it seems all the places this is used at already includes cstdint. See the f commit which has a warning to see if the code path is compiled (and it is).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just include it on the top of the file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.


#else

#include <cstdint>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just include it on the top of the file.

@axic axic merged commit 28e8272 into master Oct 13, 2020
@axic axic deleted the popcount branch October 13, 2020 20:45
@axic axic mentioned this pull request Oct 13, 2020
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