Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/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
10.8k
{
62
10.8k
  unsigned long p, d, pi;
63
10.8k
  unsigned char *sp;
64
10.8k
  static unsigned char addtab[] =
65
10.8k
    { 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
10.8k
      2,4,6,2,6,4,2,4,2,10,2,10 };
67
10.8k
  unsigned char *addp = addtab;
68
10.8k
  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
10.8k
  d = ps->d;
73
10.8k
  sp = ps->s + d;
74
28.0k
  while (*sp != 0)
75
17.2k
    sp++;
76
10.8k
  if (sp != ps->s + SIEVESIZE)
77
10.2k
    {
78
10.2k
      d = sp - ps->s;
79
10.2k
      ps->d = d + 1;
80
10.2k
      return ps->s0 + 2 * d;
81
10.2k
    }
82
83
  /* Handle the number 2 separately.  */
84
591
  if (ps->s0 < 3)
85
298
    {
86
298
      ps->s0 = 3 - 2 * SIEVESIZE; /* Tricky */
87
298
      return 2;
88
298
    }
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
293
  memset (ps->s, 0, SIEVESIZE);
97
293
#endif
98
99
293
  ps->s0 += 2 * SIEVESIZE;
100
101
  /* Update sqrt_s0 as needed.  */
102
9.66k
  while ((ps->sqrt_s0 + 1) * (ps->sqrt_s0 + 1) <= ps->s0 + 2 * SIEVESIZE - 1)
103
9.37k
    ps->sqrt_s0++;
104
105
293
  pi = ((ps->s0 + 3) / 2) % 3;
106
293
  if (pi > 0)
107
0
    pi = 3 - pi;
108
293
  if (ps->s0 + 2 * pi <= 3)
109
293
    pi += 3;
110
293
  sp = ps->s + pi;
111
50.1k
  while (sp < ps->s + SIEVESIZE)
112
49.8k
    {
113
49.8k
      *sp = 1, sp += 3;
114
49.8k
    }
115
116
293
  pi = ((ps->s0 + 5) / 2) % 5;
117
293
  if (pi > 0)
118
293
    pi = 5 - pi;
119
293
  if (ps->s0 + 2 * pi <= 5)
120
293
    pi += 5;
121
293
  sp = ps->s + pi;
122
30.1k
  while (sp < ps->s + SIEVESIZE)
123
29.8k
    {
124
29.8k
      *sp = 1, sp += 5;
125
29.8k
    }
126
127
293
  pi = ((ps->s0 + 7) / 2) % 7;
128
293
  if (pi > 0)
129
293
    pi = 7 - pi;
130
293
  if (ps->s0 + 2 * pi <= 7)
131
293
    pi += 7;
132
293
  sp = ps->s + pi;
133
21.3k
  while (sp < ps->s + SIEVESIZE)
134
21.0k
    {
135
21.0k
      *sp = 1, sp += 7;
136
21.0k
    }
137
138
293
  p = 11;
139
293
  ai = 0;
140
2.34k
  while (p <= ps->sqrt_s0)
141
2.05k
    {
142
2.05k
      pi = ((ps->s0 + p) / 2) % p;
143
2.05k
      if (pi > 0)
144
2.05k
  pi = p - pi;
145
2.05k
      if (ps->s0 + 2 * pi <= p)
146
2.05k
    pi += p;
147
2.05k
      sp = ps->s + pi;
148
58.6k
      while (sp < ps->s + SIEVESIZE)
149
56.5k
  {
150
56.5k
    *sp = 1, sp += p;
151
56.5k
  }
152
2.05k
      p += addp[ai];
153
2.05k
      ai = (ai + 1) % 48;
154
2.05k
    }
155
293
  ps->d = 0;
156
293
  return gmp_nextprime (ps);
157
591
}
158
159
void
160
gmp_init_primesieve (gmp_primesieve_t *ps)
161
298
{
162
298
  ps->s0 = 0;
163
298
  ps->sqrt_s0 = 0;
164
298
  ps->d = SIEVESIZE;
165
298
  ps->s[SIEVESIZE] = 0;    /* sentinel */
166
298
}