Skip to content

Commit

Permalink
Rename PrintPrimes to CountPrintPrimes
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch committed May 18, 2022
1 parent 8f4e88f commit 9725ba8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ set(BIN_SRC src/app/cmdoptions.cpp

set(LIB_SRC src/api-c.cpp
src/api.cpp
src/CountPrintPrimes.cpp
src/CpuInfo.cpp
src/Erat.cpp
src/EratSmall.cpp
Expand All @@ -61,7 +62,6 @@ set(LIB_SRC src/api-c.cpp
src/ParallelSieve.cpp
src/popcount.cpp
src/PreSieve.cpp
src/PrintPrimes.cpp
src/PrimeSieve.cpp
src/SievingPrimes.cpp)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
///
/// @file PrintPrimes.hpp
/// @file CountPrintPrimes.hpp
///
/// Copyright (C) 2022 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#ifndef PRINTPRIMES_HPP
#define PRINTPRIMES_HPP
#ifndef COUNTPRINTPRIMES_HPP
#define COUNTPRINTPRIMES_HPP

#include "Erat.hpp"
#include "MemoryPool.hpp"
Expand All @@ -22,14 +22,14 @@ namespace primesieve {

class Store;

/// After a segment has been sieved PrintPrimes is
/// After a segment has been sieved CountPrintPrimes is
/// used to reconstruct primes and prime k-tuplets from
/// 1 bits of the sieve array
/// 1 bits of the sieve array.
///
class PrintPrimes : public Erat
class CountPrintPrimes : public Erat
{
public:
PrintPrimes(PrimeSieve&);
CountPrintPrimes(PrimeSieve&);
NOINLINE void sieve();
private:
uint64_t low_ = 0;
Expand All @@ -40,7 +40,6 @@ class PrintPrimes : public Erat
PrimeSieve& ps_;
MemoryPool memoryPool_;
void initCounts();
void print();
void countPrimes();
void countkTuplets();
void printPrimes() const;
Expand Down
54 changes: 25 additions & 29 deletions src/PrintPrimes.cpp → src/CountPrintPrimes.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
///
/// @file PrintPrimes.cpp
/// @brief PrintPrimes is used for printing primes to stdout and for
/// counting primes. After a segment has been sieved (using
/// Erat) PrintPrimes is used to reconstruct primes and prime
/// k-tuplets from 1 bits of the sieve array.
/// @file CountPrintPrimes.cpp
/// @brief CountPrintPrimes is used for counting primes and for
/// printing primes to stdout. After a segment has been sieved
/// (using the parent Erat class) CountPrintPrimes is used
/// to reconstruct primes and prime k-tuplets from 1 bits of
/// the sieve array.
///
/// Copyright (C) 2022 Kim Walisch, <kim.walisch@gmail.com>
///
/// This file is distributed under the BSD License. See the COPYING
/// file in the top level directory.
///

#include <primesieve/PrintPrimes.hpp>
#include <primesieve/CountPrintPrimes.hpp>
#include <primesieve/Erat.hpp>
#include <primesieve/forward.hpp>
#include <primesieve/littleendian_cast.hpp>
Expand Down Expand Up @@ -41,7 +42,7 @@ const uint64_t bitmasks[6][5] =

namespace primesieve {

PrintPrimes::PrintPrimes(PrimeSieve& ps) :
CountPrintPrimes::CountPrintPrimes(PrimeSieve& ps) :
counts_(ps.getCounts()),
ps_(ps)
{
Expand All @@ -60,7 +61,7 @@ PrintPrimes::PrintPrimes(PrimeSieve& ps) :
/// Initialize the lookup tables to count the number
/// of twins, triplets, ... per byte
///
void PrintPrimes::initCounts()
void CountPrintPrimes::initCounts()
{
for (unsigned i = 1; i < counts_.size(); i++)
{
Expand All @@ -82,7 +83,7 @@ void PrintPrimes::initCounts()
}
}

void PrintPrimes::sieve()
void CountPrintPrimes::sieve()
{
uint64_t sieveSize = ps_.getSieveSize();
SievingPrimes sievingPrimes(this, sieveSize, ps_.getPreSieve(), memoryPool_);
Expand All @@ -97,32 +98,27 @@ void PrintPrimes::sieve()
addSievingPrime(prime);

sieveSegment();
print();
}
}

/// Executed after each sieved segment
void PrintPrimes::print()
{
if (ps_.isCountPrimes())
countPrimes();
if (ps_.isCountkTuplets())
countkTuplets();
if (ps_.isPrintPrimes())
printPrimes();
if (ps_.isPrintkTuplets())
printkTuplets();
if (ps_.isStatus())
ps_.updateStatus(sieveSize_ * 30);
if (ps_.isCountPrimes())
countPrimes();
if (ps_.isCountkTuplets())
countkTuplets();
if (ps_.isPrintPrimes())
printPrimes();
if (ps_.isPrintkTuplets())
printkTuplets();
if (ps_.isStatus())
ps_.updateStatus(sieveSize_ * 30);
}
}

void PrintPrimes::countPrimes()
void CountPrintPrimes::countPrimes()
{
uint64_t size = ceilDiv(sieveSize_, 8);
counts_[0] += popcount((const uint64_t*) sieve_, size);
}

void PrintPrimes::countkTuplets()
void CountPrintPrimes::countkTuplets()
{
// i = 1 twins, i = 2 triplets, ...
for (unsigned i = 1; i < counts_.size(); i++)
Expand All @@ -145,7 +141,7 @@ void PrintPrimes::countkTuplets()
}

/// Print primes to stdout
void PrintPrimes::printPrimes() const
void CountPrintPrimes::printPrimes() const
{
uint64_t i = 0;
uint64_t low = low_;
Expand All @@ -170,7 +166,7 @@ void PrintPrimes::printPrimes() const
}

/// Print prime k-tuplets to stdout
void PrintPrimes::printkTuplets() const
void CountPrintPrimes::printkTuplets() const
{
// i = 1 twins, i = 2 triplets, ...
unsigned i = 1;
Expand Down
6 changes: 3 additions & 3 deletions src/PrimeSieve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include <primesieve/forward.hpp>
#include <primesieve/PrimeSieve.hpp>
#include <primesieve/ParallelSieve.hpp>
#include <primesieve/CountPrintPrimes.hpp>
#include <primesieve/pmath.hpp>
#include <primesieve/PrintPrimes.hpp>
#include <primesieve/PreSieve.hpp>

#include <stdint.h>
Expand Down Expand Up @@ -292,8 +292,8 @@ void PrimeSieve::sieve()

if (stop_ >= 7)
{
PrintPrimes printPrimes(*this);
printPrimes.sieve();
CountPrintPrimes countPrintPrimes(*this);
countPrintPrimes.sieve();
}

auto t2 = std::chrono::system_clock::now();
Expand Down
11 changes: 6 additions & 5 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@
speedup of up to 30% when sieving the primes < 10^10.

* **SievingPrimes** is used to generate the sieving primes ≤ sqrt(stop).
SievingPrimes is used by the PrintPrimes and PrimeGenerator classes.
SievingPrimes is used by the CountPrintPrimes and PrimeGenerator classes.

* **PrintPrimes** is used for printing primes to stdout and for counting
primes. After a segment has been sieved (using Erat) PrintPrimes is used
to reconstruct primes and prime k-tuplets from 1 bits of the sieve array.
This class is mainly used by the primesieve command-line app.
* **CountPrintPrimes** is used for counting primes and for printing
primes to stdout. After a segment has been sieved (using Erat)
CountPrintPrimes is used to reconstruct primes and prime k-tuplets
from 1 bits of the sieve array. This class is mainly used by the
primesieve command-line app.

* **PrimeGenerator** is derived from Erat. ```primesieve::iterator``` uses
PrimeGenerator under the hood: PrimeGenerator generates a few primes
Expand Down

0 comments on commit 9725ba8

Please sign in to comment.