Skip to content

Commit

Permalink
#EDITS: adding SSE assembly kernel for Sieve of Eratosthenes and othe…
Browse files Browse the repository at this point in the history
…r small fixes
  • Loading branch information
akielaries committed May 12, 2024
1 parent be315ba commit 4d199fe
Show file tree
Hide file tree
Showing 8 changed files with 591 additions and 51 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ if(NOT BUILD_TINYGPMP AND NOT BUILD_PYGPMP OR BUILD_OPENGPMP)
get_target_property(project_link_libraries ${PROJECT_NAME} INTERFACE_LINK_LIBRARIES)
endif()

include(GNUInstallDirs)

# include directory for openGPMP
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/openGPMP>
Expand Down Expand Up @@ -209,8 +211,6 @@ if(NOT BUILD_TINYGPMP AND NOT BUILD_PYGPMP OR BUILD_OPENGPMP)
endif()


include(GNUInstallDirs)

endif()

# TINYGPMP INSTALLATION
Expand Down
9 changes: 6 additions & 3 deletions include/openGPMP/nt/prime_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
* You may opt to use, copy, modify, merge, publish, distribute
* and/or sell copies of the Software, and permit persons to whom
* the Software is furnished to do so, under the terms of the
* LICENSE file. As this is an Open Source effort, all implementations
* must be of the same methodology.
* LICENSE file.
*
*
*
Expand All @@ -33,13 +32,17 @@

#ifndef PRIME_GEN_HPP
#define PRIME_GEN_HPP

#include <cstdint>
#include <vector>

namespace gpmp {

class PrimalityGen {
public:
void sieve_of_eratosthenes(uint64_t n);

static std::vector<int> sieve_of_eratosthenes(int n);

};

} // namespace gpmp
Expand Down
24 changes: 12 additions & 12 deletions modules/calculus/differential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ gpmp::Differential gpmp::Differential::power_rule() const {
gpmp::Differential result;
for (const auto &term : terms) {
if (term.exponent > 0) {
double newCoefficient = term.coefficient * term.exponent;
int newExponent = term.exponent - 1;
result.add_term(newCoefficient, newExponent);
double new_coefficient = term.coefficient * term.exponent;
int new_exponent = term.exponent - 1;
result.add_term(new_coefficient, new_exponent);
}
}
return result;
Expand All @@ -80,21 +80,21 @@ gpmp::Differential
gpmp::Differential::chain_rule(const gpmp::Differential &inner) const {
gpmp::Differential result;

for (const auto &outerTerm : terms) {
for (const auto &outer_term : terms) {
// Apply the chain rule to each term of the outer function
gpmp::Differential innerDerivative = inner.power_rule();
gpmp::Differential inner_derivative = inner.power_rule();

// Multiply each term of innerDerivative by the coefficient of the outer
// Multiply each term of inner_derivative by the coefficient of the outer
// term
for (auto &innerTerm : innerDerivative.terms) {
innerTerm.coefficient *= outerTerm.coefficient;
for (auto &inner_term : inner_derivative.terms) {
inner_term.coefficient *= outer_term.coefficient;
}

// Multiply the inner derivative by the derivative of the outer term
for (const auto &innerTerm : innerDerivative.terms) {
double newCoefficient = innerTerm.coefficient;
int newExponent = outerTerm.exponent + innerTerm.exponent;
result.add_term(newCoefficient, newExponent);
for (const auto &inner_term : inner_derivative.terms) {
double new_coefficient = inner_term.coefficient;
int new_exponent = outer_term.exponent + inner_term.exponent;
result.add_term(new_coefficient, new_exponent);
}
}

Expand Down
6 changes: 6 additions & 0 deletions modules/nt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ set(SOURCE_FILES
random.cpp
)

if (SIMD_ISA STREQUAL "AVX2" OR SIMD_ISA STREQUAL "SSE")
list(APPEND SOURCE_FILES
sse_sieve_erat.S
)

endif()

add_library(nt OBJECT ${SOURCE_FILES})

Expand Down
40 changes: 8 additions & 32 deletions modules/nt/prime_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
* You may opt to use, copy, modify, merge, publish, distribute
* and/or sell copies of the Software, and permit persons to whom
* the Software is furnished to do so, under the terms of the
* LICENSE file. As this is an Open Source effort, all implementations
* must be of the same methodology.
* LICENSE file.
*
*
*
Expand All @@ -42,35 +41,12 @@
#include <stdio.h>
#include <string>

/*
* since this module deals with a great deal of number theory and
* various logical arithmetic operations, the greek algorithm sieve of
* Eratosthenes is able to capture all prime numbers to any given
* limit
*/
void gpmp::PrimalityGen::sieve_of_eratosthenes(uint64_t n) {
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[n + 1];
memset(prime, true, sizeof(prime));

for (uint64_t p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p] == true) {
// Update all multiples of p greater than or
// equal to the square of it numbers which are
// multiple of p and are less than p^2 are
// already been marked.
for (uint64_t i = p * p; i <= n; i += p)
prime[i] = false;
}
}
extern "C" {
// sieve of eratosthenes assembly kernel
std::vector<int> _Z19sieveOfEratosthenesi(int n);
}

// Print all prime numbers
for (uint64_t p = 2; p <= n; p++) {
if (prime[p]) {
std::cout << p << " " << std::endl;
}
}
std::vector<int> gpmp::PrimalityGen::sieve_of_eratosthenes(int n) {
return _Z19sieveOfEratosthenesi(n);
}

3 changes: 1 addition & 2 deletions modules/nt/prime_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
* You may opt to use, copy, modify, merge, publish, distribute
* and/or sell copies of the Software, and permit persons to whom
* the Software is furnished to do so, under the terms of the
* LICENSE file. As this is an Open Source effort, all implementations
* must be of the same methodology.
* LICENSE file.
*
*
*
Expand Down
Loading

0 comments on commit 4d199fe

Please sign in to comment.