I included a patch with the rename but not including Marco's tdiv I measured tune/speed and it seems very stable (this requires setting cpupower -g powersave to avoid weird turbo behavior) [image: time_nextprime.png]
Il Ven, 16 Ottobre 2020 7:13 am, Marco Bodrato ha scritto: > m = mpz_tdiv_ui(p, prime); > m = (diff > 0) ? prime - m : m; I remember that, in this context, if p = 0 (mod prime), the result m = prime is as good as the result m = 0. Because the next two lines are: if (m & 1) m += prime; won't this cause m = 2*prime, instead of the original result, m = 0? I used m = (diff > 0 && m) ? prime - m : m; to make sure that p can be marked as composite. On Fri, Oct 16, 2020 at 12:13 AM Niels Möller <ni...@lysator.liu.se> wrote: > Seth Troisi <brain...@gmail.com> writes: > > > I just figured out how to make it generic (but please let's do this in a > > 2nd change). > > Cool! But I agree that should be a followup change. If nothing else, it > has a new failure mode, since it must fail if gcd(start, diff) > 1. > > > I'd suggest "negative_mod_ui" or "distance_ui" to replace "nextmod_func" > > and "increment_ui" to replace "nextseq_fun" > > Also potentially adding the comment > > > > - m = nextmod_fuct(p, prime) > > + /* Distance to next multiple of prime */ > > + m = negative_mod_ui(p, prime); > > Sounds good to me. > > Regards, > /Niels > > -- > Niels Möller. PGP-encrypted email is preferred. Keyid 368C6677. > Internet email is subject to wholesale government surveillance. >
diff -r 838698ee40e1 -r 5154aecbd394 doc/gmp.texi --- a/doc/gmp.texi Tue Sep 22 15:12:20 2020 +0200 +++ b/doc/gmp.texi Thu Oct 15 01:13:43 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 a previous prime doesn't exist (i.e. @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 838698ee40e1 -r 5154aecbd394 gmp-h.in --- a/gmp-h.in Tue Sep 22 15:12:20 2020 +0200 +++ b/gmp-h.in Thu Oct 15 01:13:43 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 838698ee40e1 -r 5154aecbd394 mpz/nextprime.c --- a/mpz/nextprime.c Tue Sep 22 15:12:20 2020 +0200 +++ b/mpz/nextprime.c Thu Oct 15 01:13:43 2020 -0700 @@ -124,14 +124,20 @@ } static unsigned -mpz_nextprime_small (unsigned t) +findnext_small (unsigned t, short diff) { - ASSERT (t > 0); /* Expect t=1 if the operand was smaller.*/ + /* For diff= 2, expect t = 1 if operand was negative. + * For diff=-2, expect t >= 3 + */ + ASSERT (t >= 3 || (diff > 0 && t >= 1)); 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 - 2) | 1) + (t == 3); + + for (; ; t += diff) { unsigned prime = 3; for (int i = 0; ; prime += primegap_small[i++]) @@ -148,8 +154,10 @@ } } -void -mpz_nextprime (mpz_ptr p, mpz_srcptr n) +static int +findnext (mpz_ptr p, + unsigned long(*negative_mod_ui)(const mpz_t, unsigned long), + void(*increment_ui)(mpz_t, const mpz_t, unsigned long)) { char *composite; const unsigned char *primegap; @@ -160,19 +168,14 @@ 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 >= 3); + /* p odd */ + ASSERT ((PTR(p)[0] & 1) == 1); + if (nbits / 2 <= NUMBER_OF_PRIMES) { primegap = primegap_small; @@ -229,12 +232,14 @@ { 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); + /* Distance to next multiple of prime */ + m = negative_mod_ui(p, prime); /* Only care about odd multiplies of prime. */ if (m & 1) m += prime; @@ -252,20 +257,60 @@ if (composite[incr]) continue; - mpz_add_ui (p, p, difference); + increment_ui(p, p, difference); difference = 0; /* Miller-Rabin test */ - if (mpz_millerrabin (p, 25)) + primetest = mpz_millerrabin (p, 25); + if (primetest) { TMP_FREE; - return; + return primetest; } } /* Sieve next segment, very rare */ - mpz_add_ui (p, p, difference); + increment_ui(p, p, difference); + } +} + +void +mpz_nextprime (mpz_ptr p, mpz_srcptr n) +{ + /* Handle negative and 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; } + + /* First odd greater than n */ + 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) +{ + /* Handle negative and small 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 (mpz_get_ui (n), -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 838698ee40e1 -r 5154aecbd394 tests/mpz/t-nextprime.c --- a/tests/mpz/t-nextprime.c Tue Sep 22 15:12:20 2020 +0200 +++ b/tests/mpz/t-nextprime.c Thu Oct 15 01:13:43 2020 -0700 @@ -33,10 +33,20 @@ } void +refmpz_prevprime (mpz_ptr p, mpz_srcptr t) +{ + if (mpz_cmp_ui(t, 2) <= 0) + 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 +55,91 @@ 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); + + // largest gap with start < 2^32. + mpz_set_str (n, "3842610773", 10); + test_largegap (n, 336); - mpz_init (x); + // largest gap with start < 2^64. + mpz_set_str (n, "18361375334787046697", 10); + test_largegap (n, 1550); + + // test high merit primegap in the P30 digit range. + mpz_set_str (n, "3001549619028223830552751967", 10); + test_largegap (n, 2184); - // 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); + // test high merit primegap in the P100 range. + mpz_primorial_ui (n, 257); + mpz_mul_ui (n, n, 4280516017UL); + mpz_divexact_ui (n, n, 5610); + mpz_sub_ui (n, n, 2560); + test_largegap (n, 9006); - test_largegap(x, 33008); + // test high merit primegap in the P200 range. + mpz_primorial_ui (n, 409); + mpz_mul_ui (n, n, 3483347771UL); + mpz_divexact_ui (n, n, 30); + mpz_sub_ui (n, n, 7016); + test_largegap (n, 15900); + + mpz_clear (n); +} - mpz_clear (x); +void +test_bitboundaries () +{ + mpz_t n; + mpz_init (n); + mpz_set_str (n, "0xfff1", 0); + test_largegap (n, 16); + + mpz_set_str (n, "0xfffffffb", 0); + test_largegap (n, 20); + + mpz_set_str (n, "0xffffffffffc5", 0); + test_largegap (n, 80); - /* - // 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); + mpz_set_str (n, "0xffffffffffffffc5", 0); + test_largegap (n, 72); + + mpz_set_str (n, "0xffffffffffffffffffbf", 0); + test_largegap (n, 78); + + mpz_set_str (n, "0xffffffffffffffffffffffef", 0); + test_largegap (n, 78); - test_largegap(x, 66520); - */ + mpz_set_str (n, "0xffffffffffffffffffffffffffb5", 0); + test_largegap (n, 100); + mpz_set_str (n, "0xffffffffffffffffffffffffffffff61", 0); + test_largegap (n, 210); + + mpz_set_str (n, "0xffffffffffffffffffffffffffffffffffffffffffffff13", 0); + test_largegap (n, 370); + + mpz_clear (n); } void @@ -112,8 +168,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 +177,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 +223,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 +244,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 +314,108 @@ 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. */ + { + int temp = 123; + mpz_set_ui (prvp, temp); + for (i = -10; i <= 2; i++) + { + mpz_set_si(n, i); + retval = mpz_prevprime (prvp, n); + if ( retval != 0 || mpz_cmp_ui (prvp, temp) != 0 ) + { + gmp_printf ("mpz_prevprime(%Zd) return (%d) rop (%Zd)\n", n, retval, prvp); + abort (); + } + } + } + + /* Test mpz_prevprime(3 <= n < 2^30) expect definitely prime (return 2). */ + { + for (i = 10; i < 0x40000000L; i += i/10) + { + mpz_set_ui(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). */ + { + for (i = 70; i < 100; i++) + { + mpz_ui_pow_ui(n, 2, i); + retval = mpz_prevprime (prvp, n); + if ( retval == 0 ) + { + 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 (); + test_bitboundaries (); tests_end (); return 0; diff -r 838698ee40e1 -r 5154aecbd394 tests/mpz/t-pprime_p.c --- a/tests/mpz/t-pprime_p.c Tue Sep 22 15:12:20 2020 +0200 +++ b/tests/mpz/t-pprime_p.c Thu Oct 15 01:13:43 2020 -0700 @@ -94,7 +94,7 @@ for (i = 0; i < 300; i++) { mpz_set_si (n, i); - check_pn (n, isprime (i)); + check_pn (n, 2 * isprime (i)); } mpz_clear (n); diff -r 838698ee40e1 -r 5154aecbd394 tune/common.c --- a/tune/common.c Tue Sep 22 15:12:20 2020 +0200 +++ b/tune/common.c Thu Oct 15 01:13:43 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 838698ee40e1 -r 5154aecbd394 tune/speed.c --- a/tune/speed.c Tue Sep 22 15:12:20 2020 +0200 +++ b/tune/speed.c Thu Oct 15 01:13:43 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 838698ee40e1 -r 5154aecbd394 tune/speed.h --- a/tune/speed.h Tue Sep 22 15:12:20 2020 +0200 +++ b/tune/speed.h Thu Oct 15 01:13:43 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