Thanks for committing this code! I'm back with a new mpz_prevprime patch
I also added large negative tests for mpz_nextprime and we can enable test_largegaps now that the default gap is smaller On Tue, Mar 24, 2020 at 12:45 PM Seth Troisi <brain...@gmail.com> wrote: > Code looks great, I don't have any further comments > > On Tue, Mar 24, 2020 at 12:21 PM Marco Bodrato <bodr...@mail.dm.unipi.it> > wrote: > >> Ciao, >> >> Il 2020-03-24 18:54 Seth Troisi ha scritto: >> > On Tue, Mar 24, 2020 at 9:56 AM Marco Bodrato >> > <bodr...@mail.dm.unipi.it> wrote: >> >> I propose a variation of your patch, you find it attached, on my >> >> computer it's faster. >> > >> > Couple of small notes but otherwise this looks good >> > >> > +/* NP_SMALL_LIMIT = prevprime (LAST_PRIME ^ 2) */ >> > >> > In the light of the day this can be bumped slightly to >> > prevprime(nextprime(LAST_PRIME)^2) - 1 = 316960 >> >> I'm not sure. >> I mean: with that list of primes we can certify primality up to that >> limit, I agree. >> But what about the exit condition? > > The current code consider that a number is prime if(n/prime<prime). >> To support n > LAST_PRIME^2, we should add one more branch in the loop. >> > You are correct I was remembering my code. > > >> > + /* Technically this should be prev_prime(LAST_PRIME ^ 2) */ >> > I'd remove this, It's covered by the comment above > > >> Right. >> >> > + if (q < prime) >> > + return t; >> > + if (r == 0) >> > + break; >> > >> > Can this be rearranged to >> > >> > + if (r == 0) >> > + break; >> > + if (q <= prime) >> > + return t; >> >> I fear it can't. The case t=3, prime=3 would consider 3 a composite. >> Moreover I believe that, on some architectures, q can be ready before r >> is, so the order Torbjorn used should reduce latency. But I may be wrong >> on that point. >> > >> The case t=3 can be healed with an if(t<9) return t; just before the >> loop. Then we should measure speed on different platforms. >> With the order you propose, we may use >> NP_SMALL_LIMIT = prevprime (LAST_PRIME * (LAST_PRIME + 1)) >> right? >> > > I thought my proposed code would allow us to exit potentially one prime > earlier > but if we have to add an additional check before the loop it doesn't help. > > >> >> > + ASSERT (i < NUMBER_OF_PRIMES); >> > Should this be placed at the start of the loop or be? >> > + ASSERT (i+1 < NUMBER_OF_PRIMES); >> >> The ASSERT on i make sense just before i is used by the instruction >> prime+=primegap[i], i.e. at the end of the loop. Isn't it? >> > For some reason I was thinking ++i not i++ > >> >> >> If writing the code is not too complex, it may be interesting to >> >> test if it is worth. >> > >> > I'll try it out tonight >> >> Great! >> >> >> Did you try tweaking with the -p parameter? It can be useful when a >> >> measure is "noisy". >> > >> > Nope I had not, using -p3 seems to work just as well. >> >> I would have used something like -p100000 or -p1000000, but if -p3 is >> good for you, I'm happy :-) >> >> Ĝis, >> m >> >
diff -r 805304ca965a doc/gmp.texi --- a/doc/gmp.texi Tue Mar 24 23:13:28 2020 +0100 +++ b/doc/gmp.texi Tue Mar 24 18:24:38 2020 -0700 @@ -3563,8 +3563,19 @@ @deftypefun void mpz_nextprime (mpz_t @var{rop}, const mpz_t @var{op}) @cindex Next prime function Set @var{rop} to the next prime greater than @var{op}. - -This function uses a probabilistic algorithm to identify primes. For +@end deftypefun + +@deftypefun int mpz_prevprime (mpz_t @var{rop}, const mpz_t @var{op}) +@cindex Previous prime function +Set @var{rop} to the greatest prime less than @var{op}. + +If previous prime doesn't exist (e.g. @var{op} < 3), rop is unchanged and +0 is returned. + +Return 1 if @var{rop} is a probably prime, and 2 if @var{rop} is definitely +prime. + +These functions use a probabilistic algorithm to identify primes. For practical purposes it's adequate, the chance of a composite passing will be extremely small. @end deftypefun diff -r 805304ca965a gmp-h.in --- a/gmp-h.in Tue Mar 24 23:13:28 2020 +0100 +++ b/gmp-h.in Tue Mar 24 18:24:38 2020 -0700 @@ -947,6 +947,9 @@ #define mpz_nextprime __gmpz_nextprime __GMP_DECLSPEC void mpz_nextprime (mpz_ptr, mpz_srcptr); +#define mpz_prevprime __gmpz_prevprime +__GMP_DECLSPEC int mpz_prevprime (mpz_ptr, mpz_srcptr); + #define mpz_out_raw __gmpz_out_raw #ifdef _GMP_H_HAVE_FILE __GMP_DECLSPEC size_t mpz_out_raw (FILE *, mpz_srcptr); diff -r 805304ca965a mpz/nextprime.c --- a/mpz/nextprime.c Tue Mar 24 23:13:28 2020 +0100 +++ b/mpz/nextprime.c Tue Mar 24 18:24:38 2020 -0700 @@ -124,14 +124,16 @@ } static unsigned -mpz_nextprime_small (unsigned t) +findnext_small (unsigned t, short diff) { - ASSERT (t > 0); /* Expect t=1 if the operand was smaller.*/ + /* Expect t=1 if the operand was smaller.*/ + ASSERT (t > 0 || (diff < 0 && t > 2)); ASSERT (t < NP_SMALL_LIMIT); /* Start from next candidate (2 or odd) */ - t = (t + 1) | (t > 1); - for (; ; t += 2) + t = diff > 0 ? ((t + 1) | (t > 1)) : + ((t == 3) ? 2 : ((t - 2) | 1)); + for (; ; t += diff) { unsigned prime = 3; for (int i = 0; ; prime += primegap_small[i++]) @@ -148,8 +150,10 @@ } } -void -mpz_nextprime (mpz_ptr p, mpz_srcptr n) +int +findnext (mpz_ptr p, + unsigned long(*nextmod_func)(const mpz_t, unsigned long), + void(*nextseq_func)(mpz_t, const mpz_t, unsigned long)) { char *composite; const unsigned char *primegap; @@ -160,19 +164,12 @@ unsigned odds_in_composite_sieve; TMP_DECL; - /* First handle small numbers */ - if (mpz_cmp_ui (n, NP_SMALL_LIMIT) < 0) - { - ASSERT (NP_SMALL_LIMIT < UINT_MAX); - mpz_set_ui (p, mpz_nextprime_small (SIZ (n) > 0 ? mpz_get_ui (n) : 1)); - return; - } - mpz_add_ui (p, n, 1); - mpz_setbit (p, 0); - TMP_MARK; pn = SIZ(p); MPN_SIZEINBASE_2EXP(nbits, PTR(p), pn, 1); + /* smaller numbers handled earlier*/ + ASSERT (nbits >= 15); + if (nbits / 2 <= NUMBER_OF_PRIMES) { primegap = primegap_small; @@ -229,12 +226,13 @@ { unsigned long difference; unsigned long incr, prime; + int primetest; memset (composite, 0, odds_in_composite_sieve); prime = 3; for (i = 0; i < prime_limit; i++) { - m = mpz_cdiv_ui(p, prime); + m = nextmod_func(p, prime); /* Only care about odd multiplies of prime. */ if (m & 1) m += prime; @@ -252,20 +250,59 @@ if (composite[incr]) continue; - mpz_add_ui (p, p, difference); + nextseq_func(p, p, difference); difference = 0; /* Miller-Rabin test */ - if (mpz_millerrabin (p, 25)) - { - TMP_FREE; - return; - } + primetest = mpz_millerrabin (p, 25); + if (primetest) + { + TMP_FREE; + return primetest; + } } /* Sieve next segment, very rare */ - mpz_add_ui (p, p, difference); + nextseq_func(p, p, difference); + } +} + +void +mpz_nextprime (mpz_ptr p, mpz_srcptr n) +{ + /* First handle small numbers */ + if (mpz_cmp_ui (n, NP_SMALL_LIMIT) < 0) + { + ASSERT (NP_SMALL_LIMIT < UINT_MAX); + mpz_set_ui (p, findnext_small (SIZ (n) > 0 ? mpz_get_ui (n) : 1, +2)); + return; } + + mpz_add_ui (p, n, 1); + mpz_setbit (p, 0); + + findnext(p, mpz_cdiv_ui, mpz_add_ui); +} + +int +mpz_prevprime (mpz_ptr p, mpz_srcptr n) +{ + /* First handle tiny numbers */ + if (mpz_cmp_ui (n, 2) <= 0) + return 0; + + if (mpz_cmp_ui (n, NP_SMALL_LIMIT) < 0) + { + ASSERT (NP_SMALL_LIMIT < UINT_MAX); + mpz_set_ui (p, findnext_small (SIZ (n) > 0 ? mpz_get_ui (n) : 1, -2)); + return 2; + } + + /* First odd less than n */ + mpz_sub_ui (p, n, 2); + mpz_setbit (p, 0); + + return findnext(p, mpz_fdiv_ui, mpz_sub_ui); } #undef LOOP_ON_SIEVE_END diff -r 805304ca965a tests/mpz/t-nextprime.c --- a/tests/mpz/t-nextprime.c Tue Mar 24 23:13:28 2020 +0100 +++ b/tests/mpz/t-nextprime.c Tue Mar 24 18:24:38 2020 -0700 @@ -33,10 +33,26 @@ } void +refmpz_prevprime (mpz_ptr p, mpz_srcptr t) +{ + if (mpz_cmp_ui(t, 2) <= 0) + return; + + if (mpz_cmp_ui(t, 3) <= 0) + { + mpz_set_ui (p, 2); + return; + } + + mpz_sub_ui (p, t, 1L); + while (! mpz_probab_prime_p (p, 10)) + mpz_sub_ui (p, p, 1L); +} + +void test_largegap (mpz_t low, const int gap) { mpz_t t, nxt; - mpz_init (t); mpz_init (nxt); @@ -45,45 +61,55 @@ if (mpz_cmp_ui(t, gap) != 0) { - gmp_printf ("prime gap %Zd != %d\n", t, gap); + gmp_printf ("nextprime gap %Zd => %Zd != %d\n", low, nxt, gap); abort (); } + mpz_prevprime(t, nxt); + if (mpz_cmp(t, low) != 0) + { + gmp_printf ("prevprime gap %Zd => %Zd != %d\n", nxt, t, gap); + abort (); + } + + mpz_clear (nxt); mpz_clear (t); - mpz_clear (nxt); } void test_largegaps () { - mpz_t x; + mpz_t n; + + mpz_init (n); - mpz_init (x); + // largest gap with start < 2^32. + mpz_set_ui (n, 3842610773); + test_largegap (n, 336); - // This takes ~3 seconds on a fast computer. - // Gap 33008 from P454 = 55261931 * 1063#/210 - 13116 - mpz_primorial_ui (x, 1063); - mpz_mul_ui (x, x, 55261931); - mpz_divexact_ui (x, x, 210); - mpz_sub_ui (x, x, 13116); + // largest gap with start < 2^64. + mpz_set_ui (n, 18361375334787046697UL); + test_largegap (n, 1550); - test_largegap(x, 33008); + // test high merit primegap in the P30 digit range. + mpz_set_str (n, "3001549619028223830552751967", 10); + test_largegap (n, 2184); - mpz_clear (x); - + // test high merit primegap in the P100 range. + mpz_primorial_ui (n, 257); + mpz_mul_ui (n, n, 4280516017); + mpz_divexact_ui (n, n, 5610); + mpz_sub_ui (n, n, 2560); + test_largegap (n, 9006); - /* - // This takes ~30 seconds, it test the deep science magic constant in - // nextprime.c but takes too long to be always enabled. - // Gap 66520 from P816 = 1931 * 1933# / 7230 - 30244 - mpz_primorial_ui (x, 1933); - mpz_mul_ui (x, x, 1931); - mpz_divexact_ui (x, x, 7230); - mpz_sub_ui (x, x, 30244); + // test high merit primegap in the P200 range. + mpz_primorial_ui (n, 409); + mpz_mul_ui (n, n, 3483347771); + mpz_divexact_ui (n, n, 30); + mpz_sub_ui (n, n, 7016); + test_largegap (n, 15900); - test_largegap(x, 66520); - */ - + mpz_clear (n); } void @@ -112,8 +138,8 @@ if (mpz_cmp (x, y) != 0) { - gmp_printf ("got %Zx\n", x); - gmp_printf ("want %Zx\n", y); + gmp_printf ("got %Zd\n", x); + gmp_printf ("want %Zd\n", y); abort (); } @@ -121,6 +147,45 @@ mpz_clear (x); } +void +run_p (const char *start, int reps, const char *end, short diffs[]) +{ + mpz_t x, y; + int i; + + mpz_init_set_str (x, end, 0); + mpz_init (y); + + // Last rep doesn't share same data with nextprime + for (i = 0; i < reps - 1; i++) + { + mpz_prevprime (y, x); + mpz_sub (x, x, y); + if (diffs != NULL && + (! mpz_fits_sshort_p (x) || diffs[reps - i - 1] != (short) mpz_get_ui (x))) + { + gmp_printf ("diff list discrepancy %Zd, %d vs %d\n", + y, diffs[i], mpz_get_ui (x)); + abort (); + } + mpz_swap (x, y); + } + + // starts aren't always prime, so check that result is less than or equal + mpz_prevprime(x, x); + + mpz_set_str(y, start, 0); + if (mpz_cmp (x, y) > 0) + { + gmp_printf ("got %Zd\n", x); + gmp_printf ("want %Zd\n", y); + } + + mpz_clear (y); + mpz_clear (x); +} + + extern short diff1[]; extern short diff3[]; extern short diff4[]; @@ -128,15 +193,18 @@ extern short diff6[]; void -test_ref(gmp_randstate_ptr rands, int reps) { +test_ref (gmp_randstate_ptr rands, int reps, + void (*func)(mpz_t, const mpz_t), + void(*ref_func)(mpz_t, const mpz_t)) +{ int i; - mpz_t bs, x, next_p, ref_next_p; + mpz_t bs, x, test_p, ref_p; unsigned long size_range; mpz_init (bs); mpz_init (x); - mpz_init (next_p); - mpz_init (ref_next_p); + mpz_init (test_p); + mpz_init (ref_p); for (i = 0; i < reps; i++) { @@ -146,35 +214,59 @@ mpz_urandomb (bs, rands, size_range); mpz_rrandomb (x, rands, mpz_get_ui (bs)); -/* gmp_printf ("%ld: %Zd\n", mpz_sizeinbase (x, 2), x); */ - - mpz_nextprime (next_p, x); - refmpz_nextprime (ref_next_p, x); - if (mpz_cmp (next_p, ref_next_p) != 0) + func (test_p, x); + ref_func (ref_p, x); + if (mpz_cmp (test_p, ref_p) != 0) { - gmp_printf ("Ref mismatch %Zd => %Zd vs %Zd\n", x, ref_next_p, next_p); + gmp_printf ("start %Zd\n", x); + gmp_printf ("got %Zd\n", test_p); + gmp_printf ("want %Zd\n", ref_p); abort (); } } mpz_clear (bs); mpz_clear (x); - mpz_clear (next_p); - mpz_clear (ref_next_p); + mpz_clear (test_p); + mpz_clear (ref_p); } -int -main (int argc, char **argv) +void +test_nextprime(gmp_randstate_ptr rands, int reps) { - gmp_randstate_ptr rands; - int reps = 20; + /* Test mpz_nextprime(n <= 1) returns 2. */ + { + long i; + mpz_t n, nxtp; + mpz_init (n); + mpz_init (nxtp); - tests_start(); + for (i = -10; i <= 1; i++) + { + mpz_set_si(n, i); + mpz_nextprime (nxtp, n); + if ( mpz_cmp_ui (nxtp, 2) != 0 ) + { + gmp_printf ("mpz_nxtprime(%Zd) return %Zd\n", n, nxtp); + abort (); + } + } - rands = RANDS; - TESTS_REPS (reps, argv, argc); + for (i = 0; i <= 1000; i++) + { + mpz_ui_pow_ui(n, 2, i); + mpz_neg(n, n); + mpz_nextprime (nxtp, n); + if ( mpz_cmp_ui (nxtp, 2) != 0 ) + { + gmp_printf ("mpz_prevprime(%Zd) return %Zd\n", n, nxtp); + abort (); + } + } - test_ref(rands, reps); + mpz_clear (n); + mpz_clear (nxtp); + } run ("2", 1000, "0x1ef7", diff1); @@ -192,8 +284,106 @@ run ("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF80", 50, /* 2^128 - 128 */ "0x10000000000000000000000000000155B", diff6); - // Too slow to include in normal testing. - //test_largegaps (); + test_ref( + rands, reps, + (void (*)(mpz_t, const mpz_t)) mpz_nextprime, + refmpz_nextprime); +} + +void +test_prevprime (gmp_randstate_ptr rands, int reps) +{ + long i; + int retval; + mpz_t n, prvp; + + mpz_init (n); + mpz_init (prvp); + + /* Test mpz_prevprime(n <= 2) returns 0, leaves rop unchanged. */ + { + mpz_set_ui (prvp, 123); + for (i = -10; i <= 2; i++) + { + mpz_set_si(n, i); + retval = mpz_prevprime (prvp, n); + if ( retval != 0 || mpz_cmp_ui (prvp, 123) != 0 ) + { + gmp_printf ("mpz_prevprime(%Zd) return (%d) rop (%Zd)\n", n, retval, prvp); + abort (); + } + } + } + + /* Test mpz_prevprime(3 <= n < 2^45) returns 2. */ + { + for (i = 10; i < 0x200000000000L; i += i/10) + { + mpz_set_si(n, i); + retval = mpz_prevprime (prvp, n); + if ( retval != 2 ) + { + gmp_printf ("mpz_prevprime(%Zd) return (%d) rop (%Zd)\n", n, retval, prvp); + abort (); + } + } + } + + /* Test mpz_prevprime(n > 2^70) returns 1. */ + { + for (i = 70; i < 100; i++) + { + mpz_ui_pow_ui(n, 2, i); + retval = mpz_prevprime (prvp, n); + if ( retval != 1 ) + { + gmp_printf ("mpz_prevprime(%Zd) return (%d) rop (%Zd)\n", n, retval, prvp); + abort (); + } + } + } + + mpz_clear (n); + mpz_clear (prvp); + + run_p ("2", 1000, "0x1ef7", diff1); + + run_p ("3", 1000 - 1, "0x1ef7", NULL); + + run_p ("0x8a43866f5776ccd5b02186e90d28946aeb0ed914", 50, + "0x8a43866f5776ccd5b02186e90d28946aeb0eeec5", diff3); + + run_p ("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6C", 50, /* 2^148 - 148 */ + "0x100000000000000000000000000000000010ab", diff4); + + run_p ("0x1c2c26be55317530311facb648ea06b359b969715db83292ab8cf898d8b1b", 50, + "0x1c2c26be55317530311facb648ea06b359b969715db83292ab8cf898da957", diff5); + + run_p ("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF80", 50, /* 2^128 - 128 */ + "0x10000000000000000000000000000155B", diff6); + + // Cast away int return from mpz_prevprime for test ref. + test_ref( + rands, reps, + (void (*)(mpz_t, const mpz_t)) mpz_prevprime, + refmpz_prevprime); +} + +int +main (int argc, char **argv) +{ + gmp_randstate_ptr rands; + int reps = 20; + + tests_start(); + + rands = RANDS; + TESTS_REPS (reps, argv, argc); + + test_nextprime(rands, reps); + test_prevprime(rands, reps); + + test_largegaps (); tests_end (); return 0; diff -r 805304ca965a tune/common.c --- a/tune/common.c Tue Mar 24 23:13:28 2020 +0100 +++ b/tune/common.c Tue Mar 24 18:24:38 2020 -0700 @@ -1776,6 +1776,18 @@ } double +speed_mpz_prevprime (struct speed_params *s) +{ + SPEED_ROUTINE_MPZ_NEXTPRIME (mpz_prevprime); +} + +double +speed_mpz_prevprime_1 (struct speed_params *s) +{ + SPEED_ROUTINE_MPZ_UNARY_1 (mpz_prevprime); +} + +double speed_mpz_jacobi (struct speed_params *s) { SPEED_ROUTINE_MPZ_JACOBI (mpz_jacobi); diff -r 805304ca965a tune/speed.c --- a/tune/speed.c Tue Mar 24 23:13:28 2020 +0100 +++ b/tune/speed.c Tue Mar 24 18:24:38 2020 -0700 @@ -318,6 +318,8 @@ { "mpz_nextprime", speed_mpz_nextprime }, { "mpz_nextprime_1", speed_mpz_nextprime_1, FLAG_R_OPTIONAL }, + { "mpz_prevprime", speed_mpz_prevprime }, + { "mpz_prevprime_1", speed_mpz_prevprime_1, FLAG_R_OPTIONAL }, { "mpz_jacobi", speed_mpz_jacobi }, { "mpn_jacobi_base", speed_mpn_jacobi_base }, diff -r 805304ca965a tune/speed.h --- a/tune/speed.h Tue Mar 24 23:13:28 2020 +0100 +++ b/tune/speed.h Tue Mar 24 18:24:38 2020 -0700 @@ -409,6 +409,8 @@ double speed_mpz_init_realloc_clear (struct speed_params *); double speed_mpz_nextprime (struct speed_params *); double speed_mpz_nextprime_1 (struct speed_params *); +double speed_mpz_prevprime (struct speed_params *); +double speed_mpz_prevprime_1 (struct speed_params *); double speed_mpz_jacobi (struct speed_params *); double speed_mpz_lucnum_ui (struct speed_params *); double speed_mpz_lucnum2_ui (struct speed_params *);
_______________________________________________ gmp-devel mailing list gmp-devel@gmplib.org https://gmplib.org/mailman/listinfo/gmp-devel