Browse Source

perf: several improvements

master
Dnomd343 2 months ago
parent
commit
4289293082
  1. 6
      src/impl/hash_ce.inl
  2. 5
      src/impl/value.inl
  3. 2
      src/md5.h

6
src/impl/constexpr.inl → src/impl/hash_ce.inl

@ -84,8 +84,8 @@ constexpr std::array<char, 32> DigestCE(const std::array<uint32_t, 4> &ctx) {
/// MD5 hash implement based on constexpr.
constexpr std::array<char, 32> 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<char, 32> 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

5
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<uint32_t>(math::abs(math::sin(0x##x + 1)) * 0x100000000);
#define MD5_T(x) constexpr auto kT_##x = \
static_cast<uint32_t>(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

2
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 {

Loading…
Cancel
Save