From 2c24c61da56cb530102c4b5c4e1dca057badf02d Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sat, 16 Nov 2024 09:11:52 +0100 Subject: [PATCH] perf: `first_100_primes` moved to the static storage --- src/core/include/mp-units/ext/prime.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/core/include/mp-units/ext/prime.h b/src/core/include/mp-units/ext/prime.h index 1206dc7e7..a5a8f1068 100644 --- a/src/core/include/mp-units/ext/prime.h +++ b/src/core/include/mp-units/ext/prime.h @@ -38,6 +38,7 @@ import std; #include #include #include +#include #endif #endif @@ -441,9 +442,20 @@ constexpr auto get_first_of(const Rng& rng, UnaryFunction f) return get_first_of(begin(rng), end(rng), f); } +#if __cpp_constexpr < 202211L +template +struct first_n_primes_impl { + static constexpr auto value = first_n_primes(); +}; +#endif + [[nodiscard]] consteval std::uintmax_t find_first_factor(std::uintmax_t n) { - constexpr auto first_100_primes = first_n_primes<100>(); +#if __cpp_constexpr >= 202211L + static constexpr auto first_100_primes = first_n_primes<100>(); +#else + constexpr auto first_100_primes = first_n_primes_impl<100>::value; +#endif for (const auto& p : first_100_primes) { if (n % p == 0u) {