-
Notifications
You must be signed in to change notification settings - Fork 5
/
gen-bases.c
265 lines (212 loc) · 6.36 KB
/
gen-bases.c
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
/* Generate mp_bases data.
Copyright 1991, 1993, 1994, 1996, 2000, 2002, 2004, 2011, 2012,
2015-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/. */
#include "bootstrap.c"
int chars_per_limb;
int big_base_ctz;
mpz_t big_base;
int normalization_steps;
mpz_t big_base_inverted;
mpz_t big_base_binverted;
mpz_t t;
#define POW2_P(n) (((n) & ((n) - 1)) == 0)
unsigned int
ulog2 (unsigned int x)
{
unsigned int i;
for (i = 0; x != 0; i++)
x >>= 1;
return i;
}
void
binvert (int numb_bits)
{
mpz_t bbo;
mpz_init_set (bbo, big_base);
big_base_ctz = mpz_make_odd (bbo);
mpz_invert_2exp (big_base_binverted, bbo, numb_bits);
}
void
generate (int limb_bits, int nail_bits, int base)
{
int numb_bits = limb_bits - nail_bits;
mpz_set_ui (t, 1L);
mpz_mul_2exp (t, t, numb_bits);
mpz_set_ui (big_base, (long) base);
chars_per_limb = 0;
while (mpz_cmp (big_base, t) <= 0)
{
mpz_mul_ui (big_base, big_base, (long) base);
chars_per_limb++;
}
mpz_ui_pow_ui (big_base, (long) base, (long) chars_per_limb);
normalization_steps = limb_bits - mpz_sizeinbase (big_base, 2);
mpz_set_ui (t, 1L);
mpz_mul_2exp (t, t, 2*limb_bits - normalization_steps);
mpz_tdiv_q (big_base_inverted, t, big_base);
mpz_clrbit (big_base_inverted, limb_bits);
binvert (numb_bits);
}
void
header (int limb_bits, int nail_bits)
{
int numb_bits = limb_bits - nail_bits;
generate (limb_bits, nail_bits, 10);
printf ("/* This file generated by gen-bases.c - DO NOT EDIT. */\n");
printf ("\n");
printf ("#if GMP_NUMB_BITS != %d\n", numb_bits);
printf ("Error, error, this data is for %d bits\n", numb_bits);
printf ("#endif\n");
printf ("\n");
printf ("/* mp_bases[10] data, as literal values */\n");
printf ("#define MP_BASES_CHARS_PER_LIMB_10 %d\n", chars_per_limb);
printf ("#define MP_BASES_BIG_BASE_CTZ_10 %d\n", big_base_ctz);
printf ("#define MP_BASES_BIG_BASE_10 CNST_LIMB(0x");
mpz_out_str (stdout, 16, big_base);
printf (")\n");
printf ("#define MP_BASES_BIG_BASE_INVERTED_10 CNST_LIMB(0x");
mpz_out_str (stdout, 16, big_base_inverted);
printf (")\n");
printf ("#define MP_BASES_BIG_BASE_BINVERTED_10 CNST_LIMB(0x");
mpz_out_str (stdout, 16, big_base_binverted);
printf (")\n");
printf ("#define MP_BASES_NORMALIZATION_STEPS_10 %d\n", normalization_steps);
}
#define EXTRA 16
/* Compute log(2)/log(b) as a fixnum. */
void
mp_2logb (mpz_t r, int bi, int prec)
{
mpz_t t, t2, two, b;
int i;
mpz_init (t);
mpz_setbit (t, prec + EXTRA);
mpz_init (t2);
mpz_init (two);
mpz_setbit (two, prec + EXTRA + 1);
mpz_set_ui (r, 0);
mpz_init_set_ui (b, bi);
mpz_mul_2exp (b, b, prec+EXTRA);
for (i = prec-1; i >= 0; i--)
{
mpz_mul_2exp (b, b, prec+EXTRA);
mpz_sqrt (b, b);
mpz_mul (t2, t, b);
mpz_tdiv_q_2exp (t2, t2, prec+EXTRA);
if (mpz_cmp (t2, two) < 0) /* not too large? */
{
mpz_setbit (r, i); /* set next less significant bit */
mpz_swap (t, t2); /* new value acceptable */
}
}
mpz_clear (t);
mpz_clear (t2);
mpz_clear (two);
mpz_clear (b);
}
void
table (int limb_bits, int nail_bits)
{
int numb_bits = limb_bits - nail_bits;
int base;
mpz_t r, t, logb2, log2b;
mpz_init (r);
mpz_init (t);
mpz_init (logb2);
mpz_init (log2b);
printf ("/* This file generated by gen-bases.c - DO NOT EDIT. */\n");
printf ("\n");
printf ("#include \"gmp-impl.h\"\n");
printf ("\n");
printf ("#if GMP_NUMB_BITS != %d\n", numb_bits);
printf ("Error, error, this data is for %d bits\n", numb_bits);
printf ("#endif\n");
printf ("\n");
puts ("const struct bases mp_bases[257] =\n{");
puts (" /* 0 */ { 0, 0, 0, 0, 0 },");
puts (" /* 1 */ { 0, 0, 0, 0, 0 },");
for (base = 2; base <= 256; base++)
{
generate (limb_bits, nail_bits, base);
mp_2logb (r, base, limb_bits + 8);
mpz_tdiv_q_2exp (logb2, r, 8);
mpz_set_ui (t, 1);
mpz_mul_2exp (t, t, 2*limb_bits + 5);
mpz_sub_ui (t, t, 1);
mpz_add_ui (r, r, 1);
mpz_tdiv_q (log2b, t, r);
printf (" /* %3u */ { ", base);
if (POW2_P (base))
{
mpz_set_ui (big_base, ulog2 (base) - 1);
mpz_set_ui (big_base_inverted, 0);
}
printf ("%u,", chars_per_limb);
printf (" CNST_LIMB(0x");
mpz_out_str (stdout, 16, logb2);
printf ("), CNST_LIMB(0x");
mpz_out_str (stdout, 16, log2b);
printf ("), CNST_LIMB(0x");
mpz_out_str (stdout, 16, big_base);
printf ("), CNST_LIMB(0x");
mpz_out_str (stdout, 16, big_base_inverted);
printf (") },\n");
}
puts ("};");
mpz_clear (r);
mpz_clear (t);
mpz_clear (logb2);
mpz_clear (log2b);
}
int
main (int argc, char **argv)
{
int limb_bits, nail_bits;
mpz_init (big_base);
mpz_init (big_base_inverted);
mpz_init (big_base_binverted);
mpz_init (t);
if (argc != 4)
{
fprintf (stderr, "Usage: gen-bases <header|table> <limbbits> <nailbits>\n");
exit (1);
}
limb_bits = atoi (argv[2]);
nail_bits = atoi (argv[3]);
if (limb_bits <= 0
|| nail_bits < 0
|| nail_bits >= limb_bits)
{
fprintf (stderr, "Invalid limb/nail bits: %d %d\n",
limb_bits, nail_bits);
exit (1);
}
if (strcmp (argv[1], "header") == 0)
header (limb_bits, nail_bits);
else if (strcmp (argv[1], "table") == 0)
table (limb_bits, nail_bits);
else
{
fprintf (stderr, "Invalid header/table choice: %s\n", argv[1]);
exit (1);
}
return 0;
}