summaryrefslogtreecommitdiff
path: root/tests/devel/primes.c
blob: e14fcc74875ed9da60823bf4c58525b13b710b9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
Copyright 2018-2019 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.

The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

/* Usage:

   ./primes [p|c] [n0] <nMax>

     Checks mpz_probab_prime_p(n, r) exhaustively, starting from n=n0
     up to nMax.
     If n0 * n0 > nMax, the intervall is sieved piecewise, else the
     full intervall [0..nMax] is sieved at once.
     With the parameter "p" (or nothing), tests all numbers. With "c"
     only composites are tested.

   ./primes n [n0] <nMax>

     Checks mpz_nextprime() exhaustively, starting from n=n0 up to
     nMax.

     WARNING: The full intervall [0..nMax] is sieved at once, even if
     only a piece is needed. This may require a lot of memory!

 */

#include <stdlib.h>
#include <stdio.h>
#include "gmp-impl.h"
#include "longlong.h"
#include "tests.h"
#define STOP(x) return (x)
/* #define STOP(x) x */
#define REPS 10
/* #define TRACE(x,n) if ((n)>1) {x;} */
#define TRACE(x,n)

/* The full primesieve.c is included, just for block_resieve, that
   is not exported ... */
#undef gmp_primesieve
#include "../../primesieve.c"

#ifndef BLOCK_SIZE
#define BLOCK_SIZE 2048
#endif

/*********************************************************/
/* Section sieve: sieving functions and tools for primes */
/*********************************************************/

static mp_size_t
primesieve_size (mp_limb_t n) { return n_to_bit(n) / GMP_LIMB_BITS + 1; }

/*************************************************************/
/* Section macros: common macros, for swing/fac/bin (&sieve) */
/*************************************************************/

#define LOOP_ON_SIEVE_CONTINUE(prime,end)			\
    __max_i = (end);						\
								\
    do {							\
      ++__i;							\
      if ((*__sieve & __mask) == 0)				\
	{							\
	  mp_limb_t prime;					\
	  prime = id_to_n(__i)

#define LOOP_ON_SIEVE_BEGIN(prime,start,end,off,sieve)		\
  do {								\
    mp_limb_t __mask, *__sieve, __max_i, __i;			\
								\
    __i = (start)-(off);					\
    __sieve = (sieve) + __i / GMP_LIMB_BITS;			\
    __mask = CNST_LIMB(1) << (__i % GMP_LIMB_BITS);		\
    __i += (off);						\
								\
    LOOP_ON_SIEVE_CONTINUE(prime,end)

#define LOOP_ON_SIEVE_STOP					\
	}							\
      __mask = __mask << 1 | __mask >> (GMP_LIMB_BITS-1);	\
      __sieve += __mask & 1;					\
    }  while (__i <= __max_i)

#define LOOP_ON_SIEVE_END					\
    LOOP_ON_SIEVE_STOP;						\
  } while (0)

mpz_t g;

int something_wrong (mpz_t er, int exp)
{
  fprintf (stderr, "value = %lu , expected = %i\n", mpz_get_ui (er), exp);
  return -1;
}

int
check_pprime (unsigned long begin, unsigned long end, int composites)
{
  begin = (begin / 6U) * 6U;
  for (;(begin < 2) & (begin <= end); ++begin)
    {
      *(g->_mp_d) = begin;
      TRACE(printf ("-%li ", begin),1);
      if (mpz_probab_prime_p (g, REPS))
	STOP (something_wrong (g, 0));
    }
  for (;(begin < 4) & (begin <= end); ++begin)
    {
      *(g->_mp_d) = begin;
      TRACE(printf ("+%li ", begin),2);
      if (!composites && !mpz_probab_prime_p (g, REPS))
	STOP (something_wrong (g, 1));
    }
  if (end > 4) {
    if ((end > 10000) && (begin > end / begin))
      {
	mp_limb_t *sieve, *primes;
	mp_size_t size_s, size_p, off;
	unsigned long start;

	mpz_set_ui (g, end);
	mpz_sqrt (g, g);
	start = mpz_get_ui (g) + GMP_LIMB_BITS;
	size_p = primesieve_size (start);

	primes = __GMP_ALLOCATE_FUNC_LIMBS (size_p);
	gmp_primesieve (primes, start);

	size_s = BLOCK_SIZE * 2;
	sieve = __GMP_ALLOCATE_FUNC_LIMBS (size_s);
	off = n_to_bit(begin) + (begin % 3 == 0);

	do {
	  TRACE (printf ("off =%li\n", off),3);
	  block_resieve (sieve, BLOCK_SIZE, off, primes);
	  TRACE (printf ("LOOP =%li - %li\n", id_to_n (off+1), id_to_n (off + BLOCK_SIZE * GMP_LIMB_BITS)),3);
	  LOOP_ON_SIEVE_BEGIN (prime, off, off + BLOCK_SIZE * GMP_LIMB_BITS - 1,
			       off, sieve);

	  do {
	    *(g->_mp_d) = begin;
	    TRACE(printf ("-%li ", begin),1);
	    if (mpz_probab_prime_p (g, REPS))
	      STOP (something_wrong (g, 0));
	    if ((begin & 0xff) == 0)
	      {
		spinner();
		if ((begin & 0xfffffff) == 0)
		  printf ("%li (0x%lx)\n", begin, begin);
	      }
	  } while (++begin < prime);

	  *(g->_mp_d) = begin;
	  TRACE(printf ("+%li ", begin),2);
	  if (!composites && ! mpz_probab_prime_p (g, REPS))
	    STOP (something_wrong (g, 1));
	  ++begin;

	  LOOP_ON_SIEVE_END;
	  off += BLOCK_SIZE * GMP_LIMB_BITS;
	} while (begin < end);

	__GMP_FREE_FUNC_LIMBS (sieve, size_s);
	__GMP_FREE_FUNC_LIMBS (primes, size_p);
      }
    else
      {
	mp_limb_t *sieve;
	mp_size_t size;
	unsigned long start;

	size = primesieve_size (end);

	sieve = __GMP_ALLOCATE_FUNC_LIMBS (size);
	gmp_primesieve (sieve, end);
	start = MAX (begin, 5) | 1;
	LOOP_ON_SIEVE_BEGIN (prime, n_to_bit(start) + (start % 3 == 0),
			     n_to_bit (end), 0, sieve);

	do {
	  *(g->_mp_d) = begin;
	  TRACE(printf ("-%li ", begin),1);
	  if (mpz_probab_prime_p (g, REPS))
	    STOP (something_wrong (g, 0));
	  if ((begin & 0xff) == 0)
	    {
	      spinner();
	      if ((begin & 0xfffffff) == 0)
		printf ("%li (0x%lx)\n", begin, begin);
	    }
	} while (++begin < prime);

	*(g->_mp_d) = begin;
	TRACE(printf ("+%li ", begin),2);
	if (!composites && ! mpz_probab_prime_p (g, REPS))
	  STOP (something_wrong (g, 1));
	++begin;

	LOOP_ON_SIEVE_END;

	__GMP_FREE_FUNC_LIMBS (sieve, size);
      }
  }

  for (;begin < end; ++begin)
    {
      *(g->_mp_d) = begin;
      TRACE(printf ("-%li ", begin),1);
      if (mpz_probab_prime_p (g, REPS))
	STOP (something_wrong (g, 0));
    }

  gmp_printf ("%Zd\n", g);
  return 0;
}

int
check_nprime (unsigned long begin, unsigned long end)
{
  if (begin < 2)
    {
      *(g->_mp_d) = begin;
      TRACE(printf ("%li ", begin),1);
      mpz_nextprime (g, g);
      if (mpz_cmp_ui (g, 2) != 0)
	STOP (something_wrong (g, -1));
      begin = mpz_get_ui (g);
    }
  if (begin < 3)
    {
      *(g->_mp_d) = begin;
      TRACE(printf ("%li ", begin),1);
      mpz_nextprime (g, g);
      if (mpz_cmp_ui (g, 3) != 0)
	STOP (something_wrong (g, -1));
      begin = mpz_get_ui (g);
    }
  if (end > 4)
      {
	mp_limb_t *sieve;
	mp_size_t size;
	unsigned long start;

	size = primesieve_size (end);

	sieve = __GMP_ALLOCATE_FUNC_LIMBS (size);
	gmp_primesieve (sieve, end);
	start = MAX (begin, 5) | 1;
	*(g->_mp_d) = begin;
	LOOP_ON_SIEVE_BEGIN (prime, n_to_bit(start) + (start % 3 == 0),
			     n_to_bit (end), 0, sieve);

	mpz_nextprime (g, g);
	if (mpz_cmp_ui (g, prime) != 0)
	  STOP (something_wrong (g, -1));

	if (prime - start > 200)
	  {
	    start = prime;
	    spinner();
	    if (prime - begin > 0xfffffff)
	      {
		begin = prime;
		printf ("%li (0x%lx)\n", begin, begin);
	      }
	  }

	LOOP_ON_SIEVE_END;

	__GMP_FREE_FUNC_LIMBS (sieve, size);
      }

  if (mpz_cmp_ui (g, end) < 0)
    {
      mpz_nextprime (g, g);
      if (mpz_cmp_ui (g, end) <= 0)
	STOP (something_wrong (g, -1));
    }

  gmp_printf ("%Zd\n", g);
  return 0;
}

int
main (int argc, char **argv)
{
  int ret, mode = 0;
  unsigned long begin = 0, end = 0;

  for (;argc > 1;--argc,++argv)
    switch (*argv[1]) {
    case 'p':
      mode = 0;
      break;
    case 'c':
      mode = 2;
      break;
    case 'n':
      mode = 1;
      break;
    default:
      begin = end;
      end = atol (argv[1]);
    }

  if (begin >= end)
    {
      fprintf (stderr, "usage: primes [n|p|c] [n0] <nMax>\n");
      exit (1);
    }

  mpz_init_set_ui (g, ULONG_MAX);

  switch (mode) {
  case 1:
    ret = check_nprime (begin, end);
    break;
  default:
    ret = check_pprime (begin, end, mode);
  }

  mpz_clear (g);

  if (ret == 0)
    printf ("Prime tests checked in [%lu - %lu] [0x%lx - 0x%lx].\n", begin, end, begin, end);
  return ret;
}