xref: /netbsd-src/external/lgpl3/gmp/dist/mpz/nextprime.c (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* mpz_nextprime(p,t) - compute the next prime > t and store that in p.
2 
3 Copyright 1999-2001, 2008, 2009, 2012 Free Software Foundation, Inc.
4 
5 Contributed to the GNU project by Niels Möller and Torbjorn Granlund.
6 
7 This file is part of the GNU MP Library.
8 
9 The GNU MP Library is free software; you can redistribute it and/or modify
10 it under the terms of either:
11 
12   * the GNU Lesser General Public License as published by the Free
13     Software Foundation; either version 3 of the License, or (at your
14     option) any later version.
15 
16 or
17 
18   * the GNU General Public License as published by the Free Software
19     Foundation; either version 2 of the License, or (at your option) any
20     later version.
21 
22 or both in parallel, as here.
23 
24 The GNU MP Library is distributed in the hope that it will be useful, but
25 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
26 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27 for more details.
28 
29 You should have received copies of the GNU General Public License and the
30 GNU Lesser General Public License along with the GNU MP Library.  If not,
31 see https://www.gnu.org/licenses/.  */
32 
33 #include "gmp-impl.h"
34 #include "longlong.h"
35 
36 static const unsigned char primegap[] =
37 {
38   2,2,4,2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,2,4,14,4,6,
39   2,10,2,6,6,4,6,6,2,10,2,4,2,12,12,4,2,4,6,2,10,6,6,6,2,6,4,2,10,14,4,2,
40   4,14,6,10,2,4,6,8,6,6,4,6,8,4,8,10,2,10,2,6,4,6,8,4,2,4,12,8,4,8,4,6,
41   12,2,18,6,10,6,6,2,6,10,6,6,2,6,6,4,2,12,10,2,4,6,6,2,12,4,6,8,10,8,10,8,
42   6,6,4,8,6,4,8,4,14,10,12,2,10,2,4,2,10,14,4,2,4,14,4,2,4,20,4,8,10,8,4,6,
43   6,14,4,6,6,8,6,12
44 };
45 
46 #define NUMBER_OF_PRIMES 167
47 
48 void
mpz_nextprime(mpz_ptr p,mpz_srcptr n)49 mpz_nextprime (mpz_ptr p, mpz_srcptr n)
50 {
51   unsigned short *moduli;
52   unsigned long difference;
53   int i;
54   unsigned prime_limit;
55   unsigned long prime;
56   mp_size_t pn;
57   mp_bitcnt_t nbits;
58   unsigned incr;
59   TMP_SDECL;
60 
61   /* First handle tiny numbers */
62   if (mpz_cmp_ui (n, 2) < 0)
63     {
64       mpz_set_ui (p, 2);
65       return;
66     }
67   mpz_add_ui (p, n, 1);
68   mpz_setbit (p, 0);
69 
70   if (mpz_cmp_ui (p, 7) <= 0)
71     return;
72 
73   pn = SIZ(p);
74   MPN_SIZEINBASE_2EXP(nbits, PTR(p), pn, 1);
75   if (nbits / 2 >= NUMBER_OF_PRIMES)
76     prime_limit = NUMBER_OF_PRIMES - 1;
77   else
78     prime_limit = nbits / 2;
79 
80   TMP_SMARK;
81 
82   /* Compute residues modulo small odd primes */
83   moduli = TMP_SALLOC_TYPE (prime_limit, unsigned short);
84 
85   for (;;)
86     {
87       /* FIXME: Compute lazily? */
88       prime = 3;
89       for (i = 0; i < prime_limit; i++)
90 	{
91 	  moduli[i] = mpz_tdiv_ui (p, prime);
92 	  prime += primegap[i];
93 	}
94 
95 #define INCR_LIMIT 0x10000	/* deep science */
96 
97       for (difference = incr = 0; incr < INCR_LIMIT; difference += 2)
98 	{
99 	  /* First check residues */
100 	  prime = 3;
101 	  for (i = 0; i < prime_limit; i++)
102 	    {
103 	      unsigned r;
104 	      /* FIXME: Reduce moduli + incr and store back, to allow for
105 		 division-free reductions.  Alternatively, table primes[]'s
106 		 inverses (mod 2^16).  */
107 	      r = (moduli[i] + incr) % prime;
108 	      prime += primegap[i];
109 
110 	      if (r == 0)
111 		goto next;
112 	    }
113 
114 	  mpz_add_ui (p, p, difference);
115 	  difference = 0;
116 
117 	  /* Miller-Rabin test */
118 	  if (mpz_millerrabin (p, 25))
119 	    goto done;
120 	next:;
121 	  incr += 2;
122 	}
123       mpz_add_ui (p, p, difference);
124       difference = 0;
125     }
126  done:
127   TMP_SFREE;
128 }
129