From 80a6ad9d56e57fa178dcf3d5de26cb80fa20ba2b Mon Sep 17 00:00:00 2001 From: Kim Walisch Date: Mon, 1 Jul 2024 14:43:07 +0200 Subject: [PATCH] Move generate_phi.hpp code phi_vector.cpp (#74) --- CMakeLists.txt | 1 + ChangeLog | 3 +- include/phi_vector.hpp | 41 ++++++++++++++++ src/deleglise-rivat/S2_hard.cpp | 6 +-- src/gourdon/D.cpp | 6 +-- src/lmo/pi_lmo_parallel.cpp | 10 ++-- .../generate_phi.hpp => src/phi_vector.cpp | 48 +++++++++++++++---- test/{generate_phi.cpp => phi_vector.cpp} | 13 ++--- 8 files changed, 100 insertions(+), 28 deletions(-) create mode 100644 include/phi_vector.hpp rename include/generate_phi.hpp => src/phi_vector.cpp (89%) rename test/{generate_phi.cpp => phi_vector.cpp} (79%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0626b0d5b..aba179fd1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,6 +121,7 @@ set(LIB_SRC src/api.cpp src/generate.cpp src/nth_prime.cpp src/phi.cpp + src/phi_vector.cpp src/pi_legendre.cpp src/pi_lehmer.cpp src/pi_meissel.cpp diff --git a/ChangeLog b/ChangeLog index 72eef6aec..b88bc96c2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ -Changes in primecount-7.14, 2024-06-22 +Changes in primecount-7.14, 2024-07-01 * Move x86 cpuid code from cpuid.hpp to src/x86/cpuid.cpp. +* Move generate_phi.hpp code to src/phi_vector.cpp. * int128_t.hpp: Rename namespace port to pstd (portable std namespace). * Sieve.hpp: Tune AVX512 code. * cpu_supports_popcnt.hpp: Simplify, move preprocessor checks to new multiarch_x86_popcnt.cmake. diff --git a/include/phi_vector.hpp b/include/phi_vector.hpp new file mode 100644 index 000000000..66ac041eb --- /dev/null +++ b/include/phi_vector.hpp @@ -0,0 +1,41 @@ +/// +/// @file phi_vector.hpp +/// +/// Copyright (C) 2024 Kim Walisch, +/// +/// This file is distributed under the BSD License. See the COPYING +/// file in the top level directory. +/// + +#ifndef PHI_VECTOR_HPP +#define PHI_VECTOR_HPP + +#include +#include +#include + +namespace primecount { + +/// Returns a vector with phi(x, i - 1) values such that +/// phi[i] = phi(x, i - 1) for 1 <= i <= a. +/// phi(x, a) counts the numbers <= x that are not +/// divisible by any of the first a primes. +/// +Vector phi_vector(int64_t x, + int64_t a, + const Vector& primes, + const PiTable& pi); + +/// Returns a vector with phi(x, i - 1) values such that +/// phi[i] = phi(x, i - 1) for 1 <= i <= a. +/// phi(x, a) counts the numbers <= x that are not +/// divisible by any of the first a primes. +/// +Vector phi_vector(int64_t x, + int64_t a, + const Vector& primes, + const PiTable& pi); + +} // namespace + +#endif diff --git a/src/deleglise-rivat/S2_hard.cpp b/src/deleglise-rivat/S2_hard.cpp index 9740768bd..393a55c2c 100644 --- a/src/deleglise-rivat/S2_hard.cpp +++ b/src/deleglise-rivat/S2_hard.cpp @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include #include @@ -83,7 +83,7 @@ T S2_hard_thread(T x, if (min_b > max_b) return 0; - auto phi = generate_phi(low, max_b, primes, pi); + Vector phi = phi_vector(low, max_b, primes, pi); Sieve sieve(low, segment_size, max_b); thread.init_finished(); @@ -254,7 +254,7 @@ int64_t S2_hard(int64_t x, FactorTable factor(y, threads); int64_t max_prime = min(y, z / isqrt(y)); - auto primes = generate_primes(max_prime); + auto primes = generate_primes(max_prime); int64_t sum = S2_hard_OpenMP(x, y, z, c, s2_hard_approx, primes, factor, threads, is_print); if (is_print) diff --git a/src/gourdon/D.cpp b/src/gourdon/D.cpp index bb6052e6b..ebfa93af4 100644 --- a/src/gourdon/D.cpp +++ b/src/gourdon/D.cpp @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include @@ -72,7 +72,7 @@ T D_thread(T x, if (min_b > max_b) return 0; - auto phi = generate_phi(low, max_b, primes, pi); + Vector phi = phi_vector(low, max_b, primes, pi); Sieve sieve(low, segment_size, max_b); thread.init_finished(); @@ -247,7 +247,7 @@ int64_t D(int64_t x, } FactorTableD factor(y, z, threads); - auto primes = generate_primes(y); + auto primes = generate_primes(y); int64_t sum = D_OpenMP(x, y, z, k, d_approx, primes, factor, threads, is_print); if (is_print) diff --git a/src/lmo/pi_lmo_parallel.cpp b/src/lmo/pi_lmo_parallel.cpp index cddf445f2..4cd72dfb9 100644 --- a/src/lmo/pi_lmo_parallel.cpp +++ b/src/lmo/pi_lmo_parallel.cpp @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include @@ -51,7 +51,7 @@ int64_t S2_thread(int64_t x, int64_t z, int64_t c, const PiTable& pi, - const Vector& primes, + const Vector& primes, const Vector& lpf, const Vector& mu, ThreadData& thread) @@ -70,7 +70,7 @@ int64_t S2_thread(int64_t x, if (min_b > max_b) return 0; - auto phi = generate_phi(low, max_b, primes, pi); + Vector phi = phi_vector(low, max_b, primes, pi); Sieve sieve(low, segment_size, max_b); thread.init_finished(); @@ -168,7 +168,7 @@ int64_t S2(int64_t x, int64_t z, int64_t c, int64_t s2_approx, - const Vector& primes, + const Vector& primes, const Vector& lpf, const Vector& mu, int threads, @@ -242,7 +242,7 @@ int64_t pi_lmo_parallel(int64_t x, print(x, y, z, c, threads); } - auto primes = generate_primes(y); + auto primes = generate_primes(y); auto lpf = generate_lpf(y); auto mu = generate_moebius(y); diff --git a/include/generate_phi.hpp b/src/phi_vector.cpp similarity index 89% rename from include/generate_phi.hpp rename to src/phi_vector.cpp index b23cbf21b..beb077cd2 100644 --- a/include/generate_phi.hpp +++ b/src/phi_vector.cpp @@ -1,5 +1,5 @@ /// -/// @file generate_phi.hpp +/// @file phi_vector.cpp /// @brief The PhiCache class calculates the partial sieve function /// (a.k.a. Legendre-sum) using the recursive formula: /// phi(x, a) = phi(x, a - 1) - phi(x / primes[a], a - 1). @@ -22,9 +22,7 @@ /// file in the top level directory. /// -#ifndef GENERATE_PHI_HPP -#define GENERATE_PHI_HPP - +#include #include #include #include @@ -40,7 +38,9 @@ #include #include -namespace primecount { +namespace { + +using namespace primecount; template class PhiCache : public BitSieve240 @@ -300,10 +300,10 @@ class PhiCache : public BitSieve240 /// divisible by any of the first a primes. /// template -NOINLINE Vector generate_phi(int64_t x, - int64_t a, - const Primes& primes, - const PiTable& pi) +Vector phi_vector(int64_t x, + int64_t a, + const Primes& primes, + const PiTable& pi) { int64_t size = a + 1; Vector phi(size); @@ -337,4 +337,32 @@ NOINLINE Vector generate_phi(int64_t x, } // namespace -#endif +namespace primecount { + +/// Returns a vector with phi(x, i - 1) values such that +/// phi[i] = phi(x, i - 1) for 1 <= i <= a. +/// phi(x, a) counts the numbers <= x that are not +/// divisible by any of the first a primes. +/// +Vector phi_vector(int64_t x, + int64_t a, + const Vector& primes, + const PiTable& pi) +{ + return ::phi_vector(x, a, primes, pi); +} + +/// Returns a vector with phi(x, i - 1) values such that +/// phi[i] = phi(x, i - 1) for 1 <= i <= a. +/// phi(x, a) counts the numbers <= x that are not +/// divisible by any of the first a primes. +/// +Vector phi_vector(int64_t x, + int64_t a, + const Vector& primes, + const PiTable& pi) +{ + return ::phi_vector(x, a, primes, pi); +} + +} // namespace diff --git a/test/generate_phi.cpp b/test/phi_vector.cpp similarity index 79% rename from test/generate_phi.cpp rename to test/phi_vector.cpp index 14bc75d24..3e3db0669 100644 --- a/test/generate_phi.cpp +++ b/test/phi_vector.cpp @@ -1,9 +1,9 @@ /// -/// @file generate_phi.cpp -/// @brief Test that generate_phi(x, a) and phi(x, a) +/// @file phi_vector.cpp +/// @brief Test that phi_vector(x, a) and phi(x, a) /// results are identical /// -/// Copyright (C) 2017 Kim Walisch, +/// Copyright (C) 2024 Kim Walisch, /// /// This file is distributed under the BSD License. See the COPYING /// file in the top level directory. @@ -11,7 +11,8 @@ #include #include -#include +#include +#include #include #include @@ -38,7 +39,7 @@ int main() int64_t a = pi[y]; auto primes = generate_primes(y); - auto phi_vect = generate_phi(x, a, primes, pi); + auto phi_vect = phi_vector(x, a, primes, pi); for (size_t i = 1; i < phi_vect.size(); i++) { @@ -47,7 +48,7 @@ int main() if (phi1 != phi2) { - std::cerr << "Error: generate_phi(x, i - 1) = " << phi1 << std::endl; + std::cerr << "Error: phi_vector(x, i - 1) = " << phi1 << std::endl; std::cerr << "Correct: phi(x, i - 1) = " << phi2 << std::endl; std::cerr << "x = " << x << std::endl; std::cerr << "i - 1 = " << i - 1 << std::endl;