From 4289293082eb753d4daf984e129269bec01a6f76 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 21 Sep 2024 10:39:13 +0800 Subject: [PATCH] perf: several improvements --- src/impl/{constexpr.inl => hash_ce.inl} | 6 +++--- src/impl/value.inl | 5 +++-- src/md5.h | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) rename src/impl/{constexpr.inl => hash_ce.inl} (94%) diff --git a/src/impl/constexpr.inl b/src/impl/hash_ce.inl similarity index 94% rename from src/impl/constexpr.inl rename to src/impl/hash_ce.inl index e6d1d8a..8899c7a 100644 --- a/src/impl/constexpr.inl +++ b/src/impl/hash_ce.inl @@ -84,8 +84,8 @@ constexpr std::array DigestCE(const std::array &ctx) { /// MD5 hash implement based on constexpr. constexpr std::array Hash(const char *data, const uint64_t len) { md5_ctx ctx; - const md5_data md5(data, len); - for (uint32_t index = 0; index < md5.padded_len; index += 64) { + const md5_data md5 {data, len}; + for (uint64_t index = 0; index < md5.padded_len; index += 64) { const auto [A, B, C, D] = Round(GetBlock(md5, index), ctx); ctx.A += A; ctx.B += B; @@ -95,6 +95,6 @@ constexpr std::array Hash(const char *data, const uint64_t len) { return DigestCE({ctx.A, ctx.B, ctx.C, ctx.D}); } -static_assert(Hash("", 0)[0] == 'd'); +static_assert(Hash("", 0)[0] == 'd'); // d41d8cd98f00b204e9800998ecf8427e } // namespace md5::ce diff --git a/src/impl/value.inl b/src/impl/value.inl index 4ebc661..504d6be 100644 --- a/src/impl/value.inl +++ b/src/impl/value.inl @@ -48,12 +48,13 @@ static_assert(S(0) != S(63)); MD5_T(30) MD5_T(31) MD5_T(32) MD5_T(33) MD5_T(34) MD5_T(35) MD5_T(36) MD5_T(37) \ MD5_T(38) MD5_T(39) MD5_T(3a) MD5_T(3b) MD5_T(3c) MD5_T(3d) MD5_T(3e) MD5_T(3f) -#define MD5_T(x) constexpr auto kT_##x = static_cast(math::abs(math::sin(0x##x + 1)) * 0x100000000); +#define MD5_T(x) constexpr auto kT_##x = \ + static_cast(math::abs(math::sin(0x##x + 1)) * 0x1'0000'0000); MD5_TT #undef MD5_T #define MD5_T(x) kT_##x, -constexpr std::array kT = {MD5_TT}; +constexpr std::array kT {MD5_TT}; #undef MD5_T #undef MD5_TT diff --git a/src/md5.h b/src/md5.h index 21939e7..dbc319c 100644 --- a/src/md5.h +++ b/src/md5.h @@ -23,7 +23,7 @@ static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__, #endif #include "impl/value.inl" -#include "impl/constexpr.inl" +#include "impl/hash_ce.inl" namespace md5 {