-
Notifications
You must be signed in to change notification settings - Fork 34
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
Conversation
27d4d43
to
88582bd
Compare
lib/fizzy/cxx20/numeric.hpp
Outdated
template <class T> | ||
constexpr int popcount(T x) noexcept | ||
{ | ||
return std::popcount.as<T>(x); |
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 is this?
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.
The using
statement didn't seem to work for functions. We still want to "copy" it from std
to fizzy
.
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.
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?
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.
static_assert
for what? The two types we support/test?
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.
In that case it may be better not to use a template just the two specialisations.
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 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.
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 don't we add a test run for C++20? There is just no point adding these things otherwise.
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.
None of the C++ stdlib implementations have <bit>
support.
lib/fizzy/cxx20/numeric.hpp
Outdated
|
||
#pragma once | ||
|
||
#if __cplusplus > 201703L |
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.
This will not work, because you may be building in C++20, but without <bit>
implemented.
Use __cpp_lib_bitops
.
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.
Should use __cpp_lib_span
for span too. And likewise #542 (comment) could be merged.
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.
Correct, although the std::bit_cast
is similarly untestable.
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. |
Codecov Report
@@ 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 |
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.
These should be inline, not consexpr as __builtin_popcount
isn't constexpr.
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.
Most builtins are constexpr. https://godbolt.org/z/Pnz4ob.
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.
Cool, the gcc documentation had no mention of either.
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.
Clang actually marks them constexpr: https://clang.llvm.org/docs/LanguageExtensions.html#intrinsics-support-within-constant-expressions
But GCC does not clarify it: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
a4c8353
to
45dc469
Compare
@gumb0 added tests if you want to check |
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.
Looks good to me, needs squashing.
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 |
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.
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 think it's fine.
lib/fizzy/execute.cpp
Outdated
@@ -296,7 +296,7 @@ inline uint64_t ctz64(uint64_t value) noexcept | |||
|
|||
inline uint64_t popcnt64(uint64_t value) noexcept |
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.
Should make these constexpr
then since we established the intrinsics are.
lib/fizzy/cxx20/bit.hpp
Outdated
|
||
#else | ||
|
||
#include <cstdint> |
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.
This is also needed for C++20 variant.
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.
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).
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.
Just include it on the top of the file.
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.
Done.
lib/fizzy/cxx20/bit.hpp
Outdated
|
||
#else | ||
|
||
#include <cstdint> |
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.
Just include it on the top of the file.
Part of #543.