Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace getprime_mt with primesieve. Allows for primesieve_skipto with B1done #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 13 additions & 11 deletions pm1.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ along with the ECM Library; see the file COPYING.LIB. If not, see

#include <math.h>
#include <stdlib.h>
#include <primesieve.h>
#include "ecm-impl.h"
#include "getprime_r.h"

#define CASCADE_THRES 3
#define CASCADE_MAX 50000000.0
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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);

Expand Down
21 changes: 12 additions & 9 deletions pp1.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Lucas chains, Peter L. Montgomery, December 1983, revised January 1992. */

#include <math.h>
#include <stdlib.h>
#include <primesieve.h>
#include "ecm-impl.h"
#include "getprime_r.h"

#ifdef HAVE_LIMITS_H
# include <limits.h>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions test.pm1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down