From 4ccd368ccbb6a5d86eae238a7965f12061d55bff Mon Sep 17 00:00:00 2001 From: Seth Troisi Date: Mon, 28 Mar 2022 20:27:27 -0700 Subject: [PATCH] Replace getprime_mt with primesieve. Allows for primesieve_skipto with B1done --- Makefile.am | 2 +- pm1.c | 24 +++++++++++++----------- pp1.c | 21 ++++++++++++--------- test.pm1 | 3 +++ 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/Makefile.am b/Makefile.am index 17e2551a..b94cdddc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -51,7 +51,7 @@ libecm_la_CFLAGS = $(OPENMP_CFLAGS) -g # If any interfaces have been added, removed, or changed since the last # update, increment current, and set revision to 0. libecm_la_LDFLAGS = $(LIBECM_LDFLAGS) -version-info 1:0:0 -g -libecm_la_LIBADD = $(MULREDCLIBRARY) +libecm_la_LIBADD = $(MULREDCLIBRARY) -lprimesieve if WANT_GPU libecm_la_SOURCES += cudakernel.cu cudacommon.cu if WANT_CGBN diff --git a/pm1.c b/pm1.c index 49f478a6..01aa2dfd 100644 --- a/pm1.c +++ b/pm1.c @@ -22,8 +22,8 @@ along with the ECM Library; see the file COPYING.LIB. If not, see #include #include +#include #include "ecm-impl.h" -#include "getprime_r.h" #define CASCADE_THRES 3 #define CASCADE_MAX 50000000.0 @@ -157,7 +157,7 @@ pm1_stage1 (mpz_t f, mpres_t a, mpmod_t n, double B1, double *B1done, mul_casc *cascade; long last_chkpnt_time; const double B0 = sqrt (B1); - prime_info_t prime_info; + primesieve_iterator it; mpz_init (g); mpz_init (d); @@ -210,8 +210,8 @@ pm1_stage1 (mpz_t f, mpres_t a, mpmod_t n, double B1, double *B1done, primes */ /* Add small primes <= MIN(sqrt(B1), cascade_limit) in the appropriate power to the cascade */ - prime_info_init (prime_info); - for (p = 2.; p <= MIN(B0, cascade_limit); p = (double) getprime_mt (prime_info)) + primesieve_init(&it); + for (p = 2.; p <= MIN(B0, cascade_limit); p = (double) primesieve_next_prime (&it)) { for (q = 1., r = p; r <= B1; r *= p) if (r > *B1done) q *= p; @@ -220,7 +220,7 @@ pm1_stage1 (mpz_t f, mpres_t a, mpmod_t n, double B1, double *B1done, /* If B0 < cascade_limit, we can add some primes > sqrt(B1) with exponent 1 to the cascade */ - for ( ; p <= cascade_limit; p = (double) getprime_mt (prime_info)) + for ( ; p <= cascade_limit; p = (double) primesieve_next_prime (&it)) if (p > *B1done) mulcascade_mul_d (cascade, p, d); @@ -243,7 +243,7 @@ pm1_stage1 (mpz_t f, mpres_t a, mpmod_t n, double B1, double *B1done, /* If B0 > cascade_limit, we need to process the primes cascade_limit < p < B0 in the appropriate exponent yet */ - for ( ; p <= B0; p = (double) getprime_mt (prime_info)) + for ( ; p <= B0; p = (double) primesieve_next_prime (&it)) { for (q = 1, r = p; r <= B1; r *= p) if (r > *B1done) q *= p; @@ -265,13 +265,15 @@ pm1_stage1 (mpz_t f, mpres_t a, mpmod_t n, double B1, double *B1done, /* All primes sqrt(B1) < p <= B1 appear with exponent 1. All primes <= B1done are already included with exponent at least 1, so it's safe to skip ahead to B1done+1. */ - - while (p <= *B1done) - p = (double) getprime_mt (prime_info); + if (p <= *B1done) { + primesieve_skipto(&it, *B1done, primesieve_get_max_stop()); + p = primesieve_next_prime (&it); + ASSERT( p > *B1done ); + } /* then remaining primes > max(sqrt(B1), cascade_limit) and taken with exponent 1 */ - for (; p <= B1; p = (double) getprime_mt (prime_info)) + for (; p <= B1; p = (double) primesieve_next_prime (&it)) { mpz_mul_d (g, g, p, d); if (mpz_sizeinbase (g, 2) >= max_size) @@ -314,7 +316,7 @@ pm1_stage1 (mpz_t f, mpres_t a, mpmod_t n, double B1, double *B1done, clear_pm1_stage1: if (chkfilename != NULL) writechkfile (chkfilename, ECM_PM1, *B1done, n, NULL, a, NULL, NULL); - prime_info_clear (prime_info); /* free the prime table */ + primesieve_free_iterator(&it); /* free the prime iterator */ mpz_clear (d); mpz_clear (g); diff --git a/pp1.c b/pp1.c index 699d9c30..9c9cdd43 100644 --- a/pp1.c +++ b/pp1.c @@ -30,8 +30,8 @@ Lucas chains, Peter L. Montgomery, December 1983, revised January 1992. */ #include #include +#include #include "ecm-impl.h" -#include "getprime_r.h" #ifdef HAVE_LIMITS_H # include @@ -129,7 +129,7 @@ pp1_stage1 (mpz_t f, mpres_t P0, mpmod_t n, double B1, double *B1done, int youpi = ECM_NO_FACTOR_FOUND; unsigned int max_size, size_n; long last_chkpnt_time; - prime_info_t prime_info; + primesieve_iterator it; mpz_init (g); mpres_init (P, n); @@ -168,8 +168,8 @@ pp1_stage1 (mpz_t f, mpres_t P0, mpmod_t n, double B1, double *B1done, last_chkpnt_p = 2.; last_chkpnt_time = cputime (); /* first loop through small primes <= sqrt(B1) */ - prime_info_init (prime_info); - for (p = 2.0; p <= B0; p = (double) getprime_mt (prime_info)) + primesieve_init (&it); + for (p = 2.0; p <= B0; p = (double) primesieve_next_prime (&it)) { for (q = 1, r = p; r <= B1; r *= p) if (r > *B1done) q *= p; @@ -194,12 +194,15 @@ pp1_stage1 (mpz_t f, mpres_t P0, mpmod_t n, double B1, double *B1done, /* All primes sqrt(B1) < p <= B1 appear with exponent 1. All primes <= B1done are already included with exponent at least 1, so it's safe to skip ahead to B1done+1. */ - - while (p <= *B1done) - p = (double) getprime_mt (prime_info); + + if (p <= *B1done) { + primesieve_skipto (&it, *B1done, primesieve_get_max_stop()); + p = primesieve_next_prime (&it); + ASSERT( p > *B1done ); + } /* then all primes > sqrt(B1) and taken with exponent 1 */ - for (; p <= B1; p = (double) getprime_mt (prime_info)) + for (; p <= B1; p = (double) primesieve_next_prime (&it)) { pp1_mul_prac (P0, (ecm_uint) p, n, P, Q, R, S, T); @@ -229,7 +232,7 @@ pp1_stage1 (mpz_t f, mpres_t P0, mpmod_t n, double B1, double *B1done, clear_and_exit: if (chkfilename != NULL) writechkfile (chkfilename, ECM_PP1, p, n, NULL, P0, NULL, NULL); - prime_info_clear (prime_info); /* free the prime table */ + primesieve_free_iterator (&it); /* free the prime iterator */ mpres_clear (Q, n); mpres_clear (R, n); mpres_clear (S, n); diff --git a/test.pm1 b/test.pm1 index 5ec4c0ea..a2477951 100755 --- a/test.pm1 +++ b/test.pm1 @@ -52,6 +52,9 @@ echo 335203548019575991076297 | $PM1 -x0 2 23 31; checkcode $? 8 ### stage 2 of length 1 ### echo 335203548019575991076297 | $PM1 -x0 3 31 58766400424189339249-58766400424189339249; checkcode $? 8 +### Stage 1 of length 10 ### +echo "200000004934000028207" | $PM1 -x0 2 1000000000-1000000010 100000000000-100000002000; checkcode $? 8 + # try primes < d in stage 2 echo 2050449353925555290706354283 | $PM1 -k 1 20 0-1e6; checkcode $? 14