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