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

refactor: remove all c style casts in minisketch subtree #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

PastaPastaPasta
Copy link

@sipa
Copy link
Owner

sipa commented Dec 18, 2021

I don't really see the point of using C++ style casts for integer types, as C-style casts are equivalent for those anyway.

@PastaPastaPasta
Copy link
Author

I don't really see the point of using C++ style casts for integer types, as C-style casts are equivalent for those anyway.

Yeah, if we could use C style casts for only integer types I'd be totally happy. The problem comes when using C style casts with more complex (and potentially dangerous) types.

The main reason for this PR, is that it allows us to use tooling (see -Wold-style-cast) to prevent, in Bitcoin Core or other libraries that use minisketch, the dangerous usage of C style casts. If you look at the bitcoin PR, I replaced a LOT of C style casts that were acting as a reinterpret_cast as opposed to a normal static_cast.

I speak more about the benefits of this change / enforcement of no C style casts in the bitcoin PR

@maflcko
Copy link

maflcko commented Dec 18, 2021

I know nothing about the build system, but I think it is possible to disable warnings for subtrees?

@PastaPastaPasta
Copy link
Author

Please see latest @sipa

I have replaced non-narrowing static_casts (I introduced) with c++11 braced init instead

src/test.cpp Outdated
@@ -289,7 +289,7 @@ int main(int argc, char** argv) {
#else
const char* mode = "";
#endif
printf("Running libminisketch tests%s with complexity=%llu\n", mode, (unsigned long long)test_complexity);
printf("Running libminisketch tests%s with complexity=%lu\n", mode, uint64_t{test_complexity});
Copy link
Author

Choose a reason for hiding this comment

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

I think if we want this to be unsigned long long and not uint64, then I'll need to use a static_cast (since it's a multiple word identifier). Up to you

Copy link
Owner

Choose a reason for hiding this comment

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

This isn't correct. uint64_t isn't the same as unsigned long on all platforms.

Copy link
Author

Choose a reason for hiding this comment

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

Okay. reverted

@PastaPastaPasta
Copy link
Author

@sipa can you please review the current state of this PR?

Thanks

@@ -33,7 +33,7 @@ template<typename I, int BITS, I MOD> NO_SANITIZE_MEMORY I MulWithClMulReduce(I
static constexpr I MASK = Mask<BITS, I>();

const __m128i MOD128 = _mm_cvtsi64_si128(MOD);
__m128i product = _mm_clmulepi64_si128(_mm_cvtsi64_si128((uint64_t)a), _mm_cvtsi64_si128((uint64_t)b), 0x00);
__m128i product = _mm_clmulepi64_si128(_mm_cvtsi64_si128(uint64_t(a)), _mm_cvtsi64_si128(uint64_t(b)), 0x00);
Copy link
Owner

Choose a reason for hiding this comment

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

I think all of these can be uint64_t{a} etc.

@@ -143,7 +143,7 @@ template<typename I, int B, I MOD, I (*MUL)(I, I), typename F, const F* SQR, con
Elem FromSeed(uint64_t seed) const {
uint64_t k0 = 0x434c4d554c466c64ull; // "CLMULFld"
uint64_t k1 = seed;
uint64_t count = ((uint64_t)B) << 32;
uint64_t count = (uint64_t(B)) << 32;
Copy link
Owner

Choose a reason for hiding this comment

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

Can be uint64_t{B}, and then doesn't need parenthesis around it anymore.

src/test.cpp Outdated
@@ -289,7 +289,7 @@ int main(int argc, char** argv) {
#else
const char* mode = "";
#endif
printf("Running libminisketch tests%s with complexity=%llu\n", mode, (unsigned long long)test_complexity);
printf("Running libminisketch tests%s with complexity=%llu\n", mode, static_cast<unsigned long long>(test_complexity));
Copy link
Owner

Choose a reason for hiding this comment

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

You can use (unsigned long long){test_complexity} here to avoid a C-style case (and prevent narrowing at the same time!).

@PastaPastaPasta
Copy link
Author

All done, will squash if requested

@PastaPastaPasta
Copy link
Author

@sipa how does that look?

src/test.cpp Outdated Show resolved Hide resolved
@PastaPastaPasta
Copy link
Author

Hi going through my old open PRs. Any chance we can get this merged?

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