1 /* gmp_nextprime -- generate small primes reasonably efficiently for internal
4 Contributed to the GNU project by Torbjorn Granlund. Miscellaneous
5 improvements by Martin Boij.
7 THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
8 SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
9 GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
11 Copyright 2009 Free Software Foundation, Inc.
13 This file is part of the GNU MP Library.
15 The GNU MP Library is free software; you can redistribute it and/or modify
16 it under the terms of either:
18 * the GNU Lesser General Public License as published by the Free
19 Software Foundation; either version 3 of the License, or (at your
20 option) any later version.
24 * the GNU General Public License as published by the Free Software
25 Foundation; either version 2 of the License, or (at your option) any
28 or both in parallel, as here.
30 The GNU MP Library is distributed in the hope that it will be useful, but
31 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
32 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
35 You should have received copies of the GNU General Public License and the
36 GNU Lesser General Public License along with the GNU MP Library. If not,
37 see https://www.gnu.org/licenses/. */
42 1. Unroll the sieving loops. Should reach 1 write/cycle. That would be a 2x
45 2. Separate sieving with primes p < SIEVESIZE and p >= SIEVESIZE. The latter
46 will need at most one write, and thus not need any inner loop.
48 3. For primes p >= SIEVESIZE, i.e., typically the majority of primes, we
49 perform more than one division per sieving write. That might dominate the
50 entire run time for the nextprime function. A incrementally initialised
51 remainder table of Pi(65536) = 6542 16-bit entries could replace that
57 #include <string.h> /* for memset */
61 gmp_nextprime (gmp_primesieve_t
*ps
)
63 unsigned long p
, d
, pi
;
65 static unsigned char addtab
[] =
66 { 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,8,6,4,6,2,4,6,2,6,6,4,
67 2,4,6,2,6,4,2,4,2,10,2,10 };
68 unsigned char *addp
= addtab
;
71 /* Look for already sieved primes. A sentinel at the end of the sieving
72 area allows us to use a very simple loop here. */
77 if (sp
!= ps
->s
+ SIEVESIZE
)
81 return ps
->s0
+ 2 * d
;
84 /* Handle the number 2 separately. */
87 ps
->s0
= 3 - 2 * SIEVESIZE
; /* Tricky */
91 /* Exhausted computed primes. Resieve, then call ourselves recursively. */
94 for (sp
= ps
->s
; sp
< ps
->s
+ SIEVESIZE
; sp
++)
97 memset (ps
->s
, 0, SIEVESIZE
);
100 ps
->s0
+= 2 * SIEVESIZE
;
102 /* Update sqrt_s0 as needed. */
103 while ((ps
->sqrt_s0
+ 1) * (ps
->sqrt_s0
+ 1) <= ps
->s0
+ 2 * SIEVESIZE
- 1)
106 pi
= ((ps
->s0
+ 3) / 2) % 3;
109 if (ps
->s0
+ 2 * pi
<= 3)
112 while (sp
< ps
->s
+ SIEVESIZE
)
117 pi
= ((ps
->s0
+ 5) / 2) % 5;
120 if (ps
->s0
+ 2 * pi
<= 5)
123 while (sp
< ps
->s
+ SIEVESIZE
)
128 pi
= ((ps
->s0
+ 7) / 2) % 7;
131 if (ps
->s0
+ 2 * pi
<= 7)
134 while (sp
< ps
->s
+ SIEVESIZE
)
141 while (p
<= ps
->sqrt_s0
)
143 pi
= ((ps
->s0
+ p
) / 2) % p
;
146 if (ps
->s0
+ 2 * pi
<= p
)
149 while (sp
< ps
->s
+ SIEVESIZE
)
157 return gmp_nextprime (ps
);
161 gmp_init_primesieve (gmp_primesieve_t
*ps
)
166 ps
->s
[SIEVESIZE
] = 0; /* sentinel */