-
Notifications
You must be signed in to change notification settings - Fork 34
[2.0.0-dev] add xxhash #1868
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
[2.0.0-dev] add xxhash #1868
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #pragma once | ||
|
|
||
| #include <fc/fwd.hpp> | ||
| #include <fc/crypto/packhash.hpp> | ||
| #include <fc/io/raw_fwd.hpp> | ||
|
|
||
| #include <xxHash/xxhash.h> | ||
|
|
||
| namespace fc { | ||
|
|
||
| namespace detail { | ||
| struct alignas(64) alignmeto64 {}; | ||
|
|
||
| template <typename T> | ||
| concept ContiguousCharSource = requires(const T& obj) { | ||
| { obj.data() } -> std::convertible_to<const char*>; | ||
| { obj.size() } -> std::unsigned_integral; | ||
| }; | ||
| } | ||
|
|
||
| struct xxh3 : public add_packhash_to_hash<xxh3> { | ||
| xxh3() = default; | ||
| explicit xxh3(const uint64_t h) : _hash(h) {} | ||
|
|
||
| ///no data() since would that be confusing LE/BE? but that breaks common interface across hash types | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like how the hash interface is a little different for |
||
|
|
||
| static xxh3 hash(const char* d, uint32_t dlen); | ||
| static xxh3 hash(const std::string&); | ||
|
|
||
| template<typename T> | ||
| static xxh3 hash( const T& t ) { | ||
| return packhash(t); | ||
| } | ||
|
|
||
| static xxh3 hash_raw(const detail::ContiguousCharSource auto& r) { | ||
| return xxh3{XXH3_64bits(r.data(), r.size())}; | ||
| } | ||
|
|
||
| class encoder { | ||
| public: | ||
| encoder(); | ||
|
|
||
| void write( const char* d, uint32_t dlen ); | ||
| void put( char c ) { write( &c, 1 ); } | ||
| void reset(); | ||
| xxh3 result(); | ||
|
|
||
| private: | ||
| struct impl; | ||
| //XXH3_state_t documents requirement of 64 byte alignment. | ||
| fc::fwd<impl,576,detail::alignmeto64> my; | ||
| }; | ||
|
|
||
| friend auto operator<=>(const xxh3&, const xxh3&) = default; | ||
|
|
||
| uint64_t _hash = 0; | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| FC_REFLECT(xxh3, (_hash)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| add_library(xxhash INTERFACE) | ||
| target_include_directories(xxhash INTERFACE .) | ||
| target_compile_definitions(xxhash INTERFACE -DXXH_INLINE_ALL=1) | ||
|
|
||
| install(FILES xxHash/xxhash.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/springxxhash/xxHash" COMPONENT dev EXCLUDE_FROM_ALL) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The library is used header only now; fwiw in an earlier revision I was compiling it as a library and this allows me to access the "x86dispatch" optimizations, |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #include <fc/crypto/xxh3.hpp> | ||
| #include <fc/fwd_impl.hpp> | ||
|
|
||
| namespace fc { | ||
|
|
||
| struct xxh3::encoder::impl { | ||
| XXH3_state_t ctx; | ||
| }; | ||
|
|
||
| xxh3::encoder::encoder() { | ||
| reset(); | ||
| } | ||
| void xxh3::encoder::reset() { | ||
| XXH3_64bits_reset(&my->ctx); | ||
| } | ||
| void xxh3::encoder::write(const char* d, uint32_t dlen) { | ||
| XXH3_64bits_update(&my->ctx, d, dlen); | ||
| } | ||
| xxh3 xxh3::encoder::result() { | ||
| xxh3 h; | ||
| h._hash = XXH3_64bits_digest(&my->ctx); | ||
| return h; | ||
| } | ||
|
|
||
| xxh3 xxh3::hash(const char* d, uint32_t dlen) { | ||
| encoder e; | ||
| e.write(d,dlen); | ||
| return e.result(); | ||
| } | ||
| xxh3 xxh3::hash(const std::string& s) { | ||
| return hash(s.data(), s.size()); | ||
| } | ||
|
|
||
| } |
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.
We really need a better place for stuff like this -- even just putting in
detailhere doesn't protect against against ODR oopsies since it's just infc::detailstill.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.
Put in a separate file with a different name space?