Coverage Report

Created: 2024-06-28 06:19

/src/gmp-6.2.1/mpz/nextprime.c
Line
Count
Source (jump to first uncovered line)
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
0
#define NUMBER_OF_PRIMES 167
47
48
void
49
mpz_nextprime (mpz_ptr p, mpz_srcptr n)
50
0
{
51
0
  unsigned short *moduli;
52
0
  unsigned long difference;
53
0
  int i;
54
0
  unsigned prime_limit;
55
0
  unsigned long prime;
56
0
  mp_size_t pn;
57
0
  mp_bitcnt_t nbits;
58
0
  unsigned incr;
59
0
  TMP_SDECL;
60
61
  /* First handle tiny numbers */
62
0
  if (mpz_cmp_ui (n, 2) < 0)
63
0
    {
64
0
      mpz_set_ui (p, 2);
65
0
      return;
66
0
    }
67
0
  mpz_add_ui (p, n, 1);
68
0
  mpz_setbit (p, 0);
69
70
0
  if (mpz_cmp_ui (p, 7) <= 0)
71
0
    return;
72
73
0
  pn = SIZ(p);
74
0
  MPN_SIZEINBASE_2EXP(nbits, PTR(p), pn, 1);
75
0
  if (nbits / 2 >= NUMBER_OF_PRIMES)
76
0
    prime_limit = NUMBER_OF_PRIMES - 1;
77
0
  else
78
0
    prime_limit = nbits / 2;
79
80
0
  TMP_SMARK;
81
82
  /* Compute residues modulo small odd primes */
83
0
  moduli = TMP_SALLOC_TYPE (prime_limit, unsigned short);
84
85
0
  for (;;)
86
0
    {
87
      /* FIXME: Compute lazily? */
88
0
      prime = 3;
89
0
      for (i = 0; i < prime_limit; i++)
90
0
  {
91
0
    moduli[i] = mpz_tdiv_ui (p, prime);
92
0
    prime += primegap[i];
93
0
  }
94
95
0
#define INCR_LIMIT 0x10000  /* deep science */
96
97
0
      for (difference = incr = 0; incr < INCR_LIMIT; difference += 2)
98
0
  {
99
    /* First check residues */
100
0
    prime = 3;
101
0
    for (i = 0; i < prime_limit; i++)
102
0
      {
103
0
        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
0
        r = (moduli[i] + incr) % prime;
108
0
        prime += primegap[i];
109
110
0
        if (r == 0)
111
0
    goto next;
112
0
      }
113
114
0
    mpz_add_ui (p, p, difference);
115
0
    difference = 0;
116
117
    /* Miller-Rabin test */
118
0
    if (mpz_millerrabin (p, 25))
119
0
      goto done;
120
0
  next:;
121
0
    incr += 2;
122
0
  }
123
0
      mpz_add_ui (p, p, difference);
124
0
      difference = 0;
125
0
    }
126
0
 done:
127
0
  TMP_SFREE;
128
0
}