summaryrefslogtreecommitdiff
path: root/gen-trialdivtab.c
blob: 218c3220e4ab0333feeecd2174909e707da87a34 (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
/* gen-trialdivtab.c

   Contributed to the GNU project by Torbjorn Granlund.

Copyright 2009, 2012, 2013, 2016, 2018 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

The GNU MP Library 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 copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library.  If not,
see https://www.gnu.org/licenses/.  */

/*
  Generate tables for fast, division-free trial division for GMP.

  There is one main table, ptab.  It contains primes, multiplied together, and
  several types of pre-computed inverses.  It refers to tables of the type
  dtab, via the last two indices.  That table contains the individual primes in
  the range, except that the primes are not actually included in the table (see
  the P macro; it sneakingly excludes the primes themselves).  Instead, the
  dtab tables contains tuples for each prime (modular-inverse, limit) used for
  divisibility checks.

  This interface is not intended for division of very many primes, since then
  other algorithms apply.
*/

#include <stdlib.h>
#include <stdio.h>
#include "bootstrap.c"

int sumspills (mpz_t, mpz_t *, int);
void mpn_mod_1s_4p_cps (mpz_t [7], mpz_t);

int limb_bits;

mpz_t B;

int
main (int argc, char *argv[])
{
  int t, p;
  mpz_t ppp, acc, inv, gmp_numb_max, tmp, Bhalf;
  mpz_t pre[7];
  int i;
  int start_p, end_p, interval_start, interval_end, omitted_p;
  const char *endtok;
  int stop;
  int np, start_idx;

  if (argc < 2)
    {
      fprintf (stderr, "usage: %s bits endprime\n", argv[0]);
      exit (1);
    }

  limb_bits = atoi (argv[1]);

  end_p = 1290;			/* default end prime */
  if (argc == 3)
    end_p = atoi (argv[2]);

  printf ("#if GMP_LIMB_BITS != %d\n", limb_bits);
  printf ("#error This table is for GMP_LIMB_BITS = %d\n", limb_bits);
  printf ("#endif\n\n");

  printf ("#if GMP_NAIL_BITS != 0\n");
  printf ("#error This table does not support nails\n");
  printf ("#endif\n\n");

  for (i = 0; i < 7; i++)
    mpz_init (pre[i]);

  mpz_init (B);
  mpz_setbit (B, limb_bits);
  mpz_init_set (gmp_numb_max, B);
  mpz_sub_ui (gmp_numb_max, gmp_numb_max, 1);

  mpz_init (tmp);
  mpz_init (inv);

  mpz_init (Bhalf);
  mpz_setbit (Bhalf, limb_bits - 1);

  start_p = 3;

  mpz_init_set_ui (ppp, 1);
  mpz_init (acc);
  interval_start = start_p;
  omitted_p = 3;
  interval_end = 0;

/*  printf ("static struct gmp_primes_dtab gmp_primes_dtab[] = {\n"); */

  printf ("#ifdef WANT_dtab\n");

  for (t = start_p; t <= end_p; t += 2)
    {
      if (! isprime (t))
	continue;

      mpz_mul_ui (acc, ppp, t);
      stop = mpz_cmp (acc, Bhalf) >= 0;
      if (!stop)
	{
	  mpn_mod_1s_4p_cps (pre, acc);
	  stop = sumspills (acc, pre + 2, 5);
	}

      if (stop)
	{
	  for (p = interval_start; p <= interval_end; p += 2)
	    {
	      if (! isprime (p))
		continue;

	      printf ("  P(%d,", (int) p);
	      mpz_invert_ui_2exp (inv, p, limb_bits);
	      printf ("CNST_LIMB(0x");  mpz_out_str (stdout, 16, inv);  printf ("),");

	      mpz_tdiv_q_ui (tmp, gmp_numb_max, p);
	      printf ("CNST_LIMB(0x");  mpz_out_str (stdout, 16, tmp);
	      printf (")),\n");
	    }
	  mpz_set_ui (ppp, t);
	  interval_start = t;
	  omitted_p = t;
	}
      else
	{
	  mpz_set (ppp, acc);
	}
      interval_end = t;
    }
  printf ("#define SMALLEST_OMITTED_PRIME %d\n", (int) omitted_p);
  printf ("#endif\n");

  printf ("#ifdef WANT_ptab\n");

/*  printf ("static struct gmp_primes_ptab gmp_primes_ptab[] = {\n"); */

  endtok = "";

  mpz_set_ui (ppp, 1);
  interval_start = start_p;
  interval_end = 0;
  np = 0;
  start_idx = 0;
  for (t = start_p; t <= end_p; t += 2)
    {
      if (! isprime (t))
	continue;

      mpz_mul_ui (acc, ppp, t);

      stop = mpz_cmp (acc, Bhalf) >= 0;
      if (!stop)
	{
	  mpn_mod_1s_4p_cps (pre, acc);
	  stop = sumspills (acc, pre + 2, 5);
	}

      if (stop)
	{
	  mpn_mod_1s_4p_cps (pre, ppp);
	  printf ("%s", endtok);
	  printf ("  {CNST_LIMB(0x");  mpz_out_str (stdout, 16, ppp);
	  printf ("),{CNST_LIMB(0x");  mpz_out_str (stdout, 16, pre[0]);
	  printf ("),%d", (int) PTR(pre[1])[0]);
	  for (i = 0; i < 5; i++)
	    {
	      printf (",");
	      printf ("CNST_LIMB(0x");  mpz_out_str (stdout, 16, pre[2 + i]);
	      printf (")");
	    }
	  printf ("},");
	  printf ("%d,", start_idx);
	  printf ("%d}", np - start_idx);

	  endtok = ",\n";
	  mpz_set_ui (ppp, t);
	  interval_start = t;
	  start_idx = np;
	}
      else
	{
	  mpz_set (ppp, acc);
	}
      interval_end = t;
      np++;
    }

  printf ("\n");
  printf ("#endif\n");

  return 0;
}

unsigned long
mpz_log2 (mpz_t x)
{
  return mpz_sgn (x) ? mpz_sizeinbase (x, 2) : 0;
}

void
mpn_mod_1s_4p_cps (mpz_t cps[7], mpz_t bparm)
{
  mpz_t b, bi;
  mpz_t B1modb, B2modb, B3modb, B4modb, B5modb;
  mpz_t t;
  int cnt;

  mpz_init_set (b, bparm);

  cnt = limb_bits - mpz_log2 (b);

  mpz_init (bi);
  mpz_init (t);
  mpz_init (B1modb);
  mpz_init (B2modb);
  mpz_init (B3modb);
  mpz_init (B4modb);
  mpz_init (B5modb);

  mpz_set_ui (t, 1);
  mpz_mul_2exp (t, t, limb_bits - cnt);
  mpz_sub (t, t, b);
  mpz_mul_2exp (t, t, limb_bits);
  mpz_tdiv_q (bi, t, b);		/* bi = B^2/b, except msb */

  mpz_set_ui (t, 1);
  mpz_mul_2exp (t, t, limb_bits);	/* t = B */
  mpz_tdiv_r (B1modb, t, b);

  mpz_mul_2exp (t, B1modb, limb_bits);
  mpz_tdiv_r (B2modb, t, b);

  mpz_mul_2exp (t, B2modb, limb_bits);
  mpz_tdiv_r (B3modb, t, b);

  mpz_mul_2exp (t, B3modb, limb_bits);
  mpz_tdiv_r (B4modb, t, b);

  mpz_mul_2exp (t, B4modb, limb_bits);
  mpz_tdiv_r (B5modb, t, b);

  mpz_set (cps[0], bi);
  mpz_set_ui (cps[1], cnt);
  mpz_tdiv_q_2exp (cps[2], B1modb, 0);
  mpz_tdiv_q_2exp (cps[3], B2modb, 0);
  mpz_tdiv_q_2exp (cps[4], B3modb, 0);
  mpz_tdiv_q_2exp (cps[5], B4modb, 0);
  mpz_tdiv_q_2exp (cps[6], B5modb, 0);

  mpz_clear (b);
  mpz_clear (bi);
  mpz_clear (t);
  mpz_clear (B1modb);
  mpz_clear (B2modb);
  mpz_clear (B3modb);
  mpz_clear (B4modb);
  mpz_clear (B5modb);
}

int
sumspills (mpz_t ppp, mpz_t *a, int n)
{
  mpz_t s;
  int i, ret;

  mpz_init_set (s, a[0]);

  for (i = 1; i < n; i++)
    {
      mpz_add (s, s, a[i]);
    }
  ret = mpz_cmp (s, B) >= 0;
  mpz_clear (s);

  return ret;
}