Coverage Report

Created: 2024-11-21 07:03

/src/libgmp/nextprime.c
Line
Count
Source (jump to first uncovered line)
1
/* gmp_nextprime -- generate small primes reasonably efficiently for internal
2
   GMP needs.
3
4
   Contributed to the GNU project by Torbjorn Granlund.  Miscellaneous
5
   improvements by Martin Boij.
6
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.
10
11
Copyright 2009 Free Software Foundation, Inc.
12
13
This file is part of the GNU MP Library.
14
15
The GNU MP Library is free software; you can redistribute it and/or modify
16
it under the terms of either:
17
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.
21
22
or
23
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
26
    later version.
27
28
or both in parallel, as here.
29
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
33
for more details.
34
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/.  */
38
39
/*
40
  Optimisation ideas:
41
42
  1. Unroll the sieving loops.  Should reach 1 write/cycle.  That would be a 2x
43
     improvement.
44
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.
47
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
52
     division.
53
*/
54
55
#include "gmp-impl.h"
56
#include <string.h>   /* for memset */
57
58
59
unsigned long int
60
gmp_nextprime (gmp_primesieve_t *ps)
61
1.79k
{
62
1.79k
  unsigned long p, d, pi;
63
1.79k
  unsigned char *sp;
64
1.79k
  static unsigned char addtab[] =
65
1.79k
    { 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,
66
1.79k
      2,4,6,2,6,4,2,4,2,10,2,10 };
67
1.79k
  unsigned char *addp = addtab;
68
1.79k
  unsigned long ai;
69
70
  /* Look for already sieved primes.  A sentinel at the end of the sieving
71
     area allows us to use a very simple loop here.  */
72
1.79k
  d = ps->d;
73
1.79k
  sp = ps->s + d;
74
3.57k
  while (*sp != 0)
75
1.77k
    sp++;
76
1.79k
  if (sp != ps->s + SIEVESIZE)
77
1.55k
    {
78
1.55k
      d = sp - ps->s;
79
1.55k
      ps->d = d + 1;
80
1.55k
      return ps->s0 + 2 * d;
81
1.55k
    }
82
83
  /* Handle the number 2 separately.  */
84
237
  if (ps->s0 < 3)
85
119
    {
86
119
      ps->s0 = 3 - 2 * SIEVESIZE; /* Tricky */
87
119
      return 2;
88
119
    }
89
90
  /* Exhausted computed primes.  Resieve, then call ourselves recursively.  */
91
92
#if 0
93
  for (sp = ps->s; sp < ps->s + SIEVESIZE; sp++)
94
    *sp = 0;
95
#else
96
118
  memset (ps->s, 0, SIEVESIZE);
97
118
#endif
98
99
118
  ps->s0 += 2 * SIEVESIZE;
100
101
  /* Update sqrt_s0 as needed.  */
102
3.89k
  while ((ps->sqrt_s0 + 1) * (ps->sqrt_s0 + 1) <= ps->s0 + 2 * SIEVESIZE - 1)
103
3.77k
    ps->sqrt_s0++;
104
105
118
  pi = ((ps->s0 + 3) / 2) % 3;
106
118
  if (pi > 0)
107
0
    pi = 3 - pi;
108
118
  if (ps->s0 + 2 * pi <= 3)
109
118
    pi += 3;
110
118
  sp = ps->s + pi;
111
20.1k
  while (sp < ps->s + SIEVESIZE)
112
20.0k
    {
113
20.0k
      *sp = 1, sp += 3;
114
20.0k
    }
115
116
118
  pi = ((ps->s0 + 5) / 2) % 5;
117
118
  if (pi > 0)
118
118
    pi = 5 - pi;
119
118
  if (ps->s0 + 2 * pi <= 5)
120
118
    pi += 5;
121
118
  sp = ps->s + pi;
122
12.1k
  while (sp < ps->s + SIEVESIZE)
123
12.0k
    {
124
12.0k
      *sp = 1, sp += 5;
125
12.0k
    }
126
127
118
  pi = ((ps->s0 + 7) / 2) % 7;
128
118
  if (pi > 0)
129
118
    pi = 7 - pi;
130
118
  if (ps->s0 + 2 * pi <= 7)
131
118
    pi += 7;
132
118
  sp = ps->s + pi;
133
8.61k
  while (sp < ps->s + SIEVESIZE)
134
8.49k
    {
135
8.49k
      *sp = 1, sp += 7;
136
8.49k
    }
137
138
118
  p = 11;
139
118
  ai = 0;
140
944
  while (p <= ps->sqrt_s0)
141
826
    {
142
826
      pi = ((ps->s0 + p) / 2) % p;
143
826
      if (pi > 0)
144
826
  pi = p - pi;
145
826
      if (ps->s0 + 2 * pi <= p)
146
826
    pi += p;
147
826
      sp = ps->s + pi;
148
23.6k
      while (sp < ps->s + SIEVESIZE)
149
22.7k
  {
150
22.7k
    *sp = 1, sp += p;
151
22.7k
  }
152
826
      p += addp[ai];
153
826
      ai = (ai + 1) % 48;
154
826
    }
155
118
  ps->d = 0;
156
118
  return gmp_nextprime (ps);
157
237
}
158
159
void
160
gmp_init_primesieve (gmp_primesieve_t *ps)
161
119
{
162
119
  ps->s0 = 0;
163
119
  ps->sqrt_s0 = 0;
164
119
  ps->d = SIEVESIZE;
165
119
  ps->s[SIEVESIZE] = 0;    /* sentinel */
166
119
}