Skip to content

Commit

Permalink
perf: first_100_primes moved to the static storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mpusz committed Nov 16, 2024
1 parent 4f8e959 commit 2c24c61
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/core/include/mp-units/ext/prime.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import std;
#include <numeric>
#include <optional>
#include <tuple>
#include <version>
#endif
#endif

Expand Down Expand Up @@ -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<std::size_t N>
struct first_n_primes_impl {
static constexpr auto value = first_n_primes<N>();
};
#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) {
Expand Down

0 comments on commit 2c24c61

Please sign in to comment.