Skip to content

Commit

Permalink
Move generate_phi.hpp code phi_vector.cpp (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch authored Jul 1, 2024
1 parent eb3f1f5 commit 80a6ad9
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 28 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
41 changes: 41 additions & 0 deletions include/phi_vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
///
/// @file phi_vector.hpp
///
/// Copyright (C) 2024 Kim Walisch, <kim.walisch@gmail.com>
///
/// 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 <PiTable.hpp>
#include <Vector.hpp>
#include <stdint.h>

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<int64_t> phi_vector(int64_t x,
int64_t a,
const Vector<uint32_t>& 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<int64_t> phi_vector(int64_t x,
int64_t a,
const Vector<int64_t>& primes,
const PiTable& pi);

} // namespace

#endif
6 changes: 3 additions & 3 deletions src/deleglise-rivat/S2_hard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <Sieve.hpp>
#include <fast_div.hpp>
#include <generate.hpp>
#include <generate_phi.hpp>
#include <phi_vector.hpp>
#include <imath.hpp>
#include <int128_t.hpp>
#include <LoadBalancerS2.hpp>
Expand Down Expand Up @@ -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<int64_t> phi = phi_vector(low, max_b, primes, pi);
Sieve sieve(low, segment_size, max_b);
thread.init_finished();

Expand Down Expand Up @@ -254,7 +254,7 @@ int64_t S2_hard(int64_t x,

FactorTable<uint16_t> factor(y, threads);
int64_t max_prime = min(y, z / isqrt(y));
auto primes = generate_primes<int32_t>(max_prime);
auto primes = generate_primes<uint32_t>(max_prime);
int64_t sum = S2_hard_OpenMP(x, y, z, c, s2_hard_approx, primes, factor, threads, is_print);

if (is_print)
Expand Down
6 changes: 3 additions & 3 deletions src/gourdon/D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <LoadBalancerS2.hpp>
#include <fast_div.hpp>
#include <generate.hpp>
#include <generate_phi.hpp>
#include <phi_vector.hpp>
#include <gourdon.hpp>
#include <imath.hpp>
#include <int128_t.hpp>
Expand Down Expand Up @@ -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<int64_t> phi = phi_vector(low, max_b, primes, pi);
Sieve sieve(low, segment_size, max_b);
thread.init_finished();

Expand Down Expand Up @@ -247,7 +247,7 @@ int64_t D(int64_t x,
}

FactorTableD<uint16_t> factor(y, z, threads);
auto primes = generate_primes<int32_t>(y);
auto primes = generate_primes<uint32_t>(y);
int64_t sum = D_OpenMP(x, y, z, k, d_approx, primes, factor, threads, is_print);

if (is_print)
Expand Down
10 changes: 5 additions & 5 deletions src/lmo/pi_lmo_parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <primecount-internal.hpp>
#include <Sieve.hpp>
#include <generate.hpp>
#include <generate_phi.hpp>
#include <phi_vector.hpp>
#include <LoadBalancerS2.hpp>
#include <min.hpp>
#include <imath.hpp>
Expand All @@ -51,7 +51,7 @@ int64_t S2_thread(int64_t x,
int64_t z,
int64_t c,
const PiTable& pi,
const Vector<int32_t>& primes,
const Vector<uint32_t>& primes,
const Vector<int32_t>& lpf,
const Vector<int32_t>& mu,
ThreadData& thread)
Expand All @@ -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<int64_t> phi = phi_vector(low, max_b, primes, pi);
Sieve sieve(low, segment_size, max_b);
thread.init_finished();

Expand Down Expand Up @@ -168,7 +168,7 @@ int64_t S2(int64_t x,
int64_t z,
int64_t c,
int64_t s2_approx,
const Vector<int32_t>& primes,
const Vector<uint32_t>& primes,
const Vector<int32_t>& lpf,
const Vector<int32_t>& mu,
int threads,
Expand Down Expand Up @@ -242,7 +242,7 @@ int64_t pi_lmo_parallel(int64_t x,
print(x, y, z, c, threads);
}

auto primes = generate_primes<int32_t>(y);
auto primes = generate_primes<uint32_t>(y);
auto lpf = generate_lpf(y);
auto mu = generate_moebius(y);

Expand Down
48 changes: 38 additions & 10 deletions include/generate_phi.hpp → src/phi_vector.cpp
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -22,9 +22,7 @@
/// file in the top level directory.
///

#ifndef GENERATE_PHI_HPP
#define GENERATE_PHI_HPP

#include <phi_vector.hpp>
#include <primecount-internal.hpp>
#include <BitSieve240.hpp>
#include <fast_div.hpp>
Expand All @@ -40,7 +38,9 @@
#include <algorithm>
#include <utility>

namespace primecount {
namespace {

using namespace primecount;

template <typename Primes>
class PhiCache : public BitSieve240
Expand Down Expand Up @@ -300,10 +300,10 @@ class PhiCache : public BitSieve240
/// divisible by any of the first a primes.
///
template <typename Primes>
NOINLINE Vector<int64_t> generate_phi(int64_t x,
int64_t a,
const Primes& primes,
const PiTable& pi)
Vector<int64_t> phi_vector(int64_t x,
int64_t a,
const Primes& primes,
const PiTable& pi)
{
int64_t size = a + 1;
Vector<int64_t> phi(size);
Expand Down Expand Up @@ -337,4 +337,32 @@ NOINLINE Vector<int64_t> 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<int64_t> phi_vector(int64_t x,
int64_t a,
const Vector<uint32_t>& 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<int64_t> phi_vector(int64_t x,
int64_t a,
const Vector<int64_t>& primes,
const PiTable& pi)
{
return ::phi_vector(x, a, primes, pi);
}

} // namespace
13 changes: 7 additions & 6 deletions test/generate_phi.cpp → test/phi_vector.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
///
/// @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, <kim.walisch@gmail.com>
/// Copyright (C) 2024 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primecount.hpp>
#include <generate.hpp>
#include <generate_phi.hpp>
#include <imath.hpp>
#include <phi_vector.hpp>
#include <PiTable.hpp>

#include <stdint.h>
Expand All @@ -38,7 +39,7 @@ int main()
int64_t a = pi[y];

auto primes = generate_primes<int64_t>(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++)
{
Expand All @@ -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;
Expand Down

0 comments on commit 80a6ad9

Please sign in to comment.