From 779e13572e025d5b0b9db4ff66d7e608318f072f Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 24 Mar 2024 15:22:21 +0800 Subject: [PATCH] feat: compatible with c++17 standard --- CMakeLists.txt | 16 +++++++++++++++- src/impl/algorithm.inc | 6 +++--- src/impl/constexpr.inc | 7 +++---- src/impl/sine.inc | 8 ++++---- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f155250..a552ac2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,21 @@ project(md5sum LANGUAGES CXX) option(MD5_ENABLE_TESTING "Enable testing of the md5sum library." ON) option(MD5_ENABLE_BENCHMARK "Enable benchmark of the md5sum library." ON) -set(CMAKE_CXX_STANDARD 20) +set(CXX_STANDARDS) +foreach (CXX_FEATURE ${CMAKE_CXX_COMPILE_FEATURES}) + if (CXX_FEATURE MATCHES "^cxx_std_[0-9]+$") + string(REGEX MATCH "[0-9]+$" CXX_STANDARD ${CXX_FEATURE}) + list(APPEND CXX_STANDARDS ${CXX_STANDARD}) # Get supported c++ standards + endif() +endforeach() + +if (NOT 17 IN_LIST CXX_STANDARDS) + message(FATAL_ERROR "MD5 projects require at least C++17 support.") +endif() + +list(GET CXX_STANDARDS -1 MD5_CXX_STANDARD) # Select the latest C++ standard +message("MD5 CXX STANDARD: ${MD5_CXX_STANDARD}") +set(CMAKE_CXX_STANDARD ${MD5_CXX_STANDARD}) add_compile_options(-Wall -Wextra) add_library(md5sum STATIC src/impl/core.cc src/impl/wrapper.cc) diff --git a/src/impl/algorithm.inc b/src/impl/algorithm.inc index 6514903..6004ddc 100644 --- a/src/impl/algorithm.inc +++ b/src/impl/algorithm.inc @@ -58,14 +58,14 @@ struct md5_ctx { }; /// MD5 data block index, input between 0 and 63. -consteval int K(int i) { +constexpr int K(int i) { const int step[4] = {1, 5, 3, 7}; const int begin[4] = {0, 1, 5, 0}; return (begin[i >> 4] + step[i >> 4] * i) & 0b1111; } /// MD5 circular shift times, input between 0 and 63. -consteval int S(int i) { +constexpr int S(int i) { const int shift[4][4] = { {7, 12, 17, 22}, {5, 9, 14, 20}, @@ -76,7 +76,7 @@ consteval int S(int i) { } /// MD5 T-table constant, input between 0 and 63. -consteval uint32_t T(int i) { +constexpr uint32_t T(int i) { auto val = ::md5::math::sin(i + 1); return static_cast(::std::abs(val) * 0x100000000); } diff --git a/src/impl/constexpr.inc b/src/impl/constexpr.inc index 25d0b43..a04f04c 100644 --- a/src/impl/constexpr.inc +++ b/src/impl/constexpr.inc @@ -13,9 +13,8 @@ struct md5_ctx_ce { const char *data; uint64_t data_len, padded_len; - constexpr md5_ctx_ce(const char *ptr, uint64_t len) : data(ptr), data_len(len) { - padded_len = (data_len + 64 + 8) & ~0b111111ULL; - } + constexpr md5_ctx_ce(const char *data, uint64_t len) + : data(data), data_len(len), padded_len((len + 64 + 8) & ~0b111111ULL) {} }; using Block = std::array; // single md5 block with 64 bytes @@ -48,7 +47,7 @@ constexpr Block GetBlock(md5_ctx_ce *ctx, const uint64_t index) { /// Convert origin MD5 integers to hexadecimal character array. constexpr std::array DigestCE(std::array ctx) { std::array result {}; - for (uint32_t i = 0, val; i < 32; val >>= 8) { + for (uint32_t i = 0, val = 0; i < 32; val >>= 8) { if (!(i & 0b111)) val = ctx[i >> 3]; result[i++] = HexTable[(val >> 4) & 0b1111]; diff --git a/src/impl/sine.inc b/src/impl/sine.inc index 77e76cb..fbdaa62 100644 --- a/src/impl/sine.inc +++ b/src/impl/sine.inc @@ -6,7 +6,7 @@ namespace md5::math { constexpr double PI = 3.14159265358979323846264338327950; -consteval double pow(double x, int n) { +constexpr double pow(double x, int n) { double res = 1; for (int i = 0; i < n; ++i) { res *= x; @@ -14,7 +14,7 @@ consteval double pow(double x, int n) { return res; } -consteval double factorial(int n) { +constexpr double factorial(int n) { double res = 1; for (int i = 2 ; i <= n ; ++i) { res *= i; @@ -23,7 +23,7 @@ consteval double factorial(int n) { } /// Calculate sin(x) value with Maclaurin series. -consteval double sin_core(double x) { +constexpr double sin_core(double x) { double res = x; for (int i = 1; i < 80; ++i) { const int n = i * 2 + 1; @@ -34,7 +34,7 @@ consteval double sin_core(double x) { } /// Calculate the sin(x) value in radians. -consteval double sin(double x) { +constexpr double sin(double x) { x = std::fmod(x, 2 * PI); // -2PI < x < 2PI if (std::abs(x) > PI) {