1
/* Generate mp_bases data.
3
Copyright 1991, 1993, 1994, 1996, 2000, 2002, 2004 Free Software Foundation,
6
This file is part of the GNU MP Library.
8
The GNU MP Library is free software; you can redistribute it and/or modify
9
it under the terms of the GNU Lesser General Public License as published by
10
the Free Software Foundation; either version 2.1 of the License, or (at your
11
option) any later version.
13
The GNU MP Library is distributed in the hope that it will be useful, but
14
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16
License for more details.
18
You should have received a copy of the GNU Lesser General Public License
19
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21
MA 02110-1301, USA. */
29
double chars_per_bit_exactly;
31
int normalization_steps;
32
mpz_t big_base_inverted;
36
#define POW2_P(n) (((n) & ((n) - 1)) == 0)
39
ulog2 (unsigned int x)
42
for (i = 0; x != 0; i++)
48
generate (int limb_bits, int nail_bits, int base)
50
int numb_bits = limb_bits - nail_bits;
53
mpz_mul_2exp (t, t, numb_bits);
54
mpz_set_ui (big_base, 1L);
58
mpz_mul_ui (big_base, big_base, (long) base);
59
if (mpz_cmp (big_base, t) > 0)
64
chars_per_bit_exactly = 0.69314718055994530942 / log ((double) base);
66
mpz_ui_pow_ui (big_base, (long) base, (long) chars_per_limb);
68
normalization_steps = limb_bits - mpz_sizeinbase (big_base, 2);
71
mpz_mul_2exp (t, t, 2*limb_bits - normalization_steps);
72
mpz_tdiv_q (big_base_inverted, t, big_base);
74
mpz_mul_2exp (t, t, limb_bits);
75
mpz_sub (big_base_inverted, big_base_inverted, t);
79
header (int limb_bits, int nail_bits)
81
int numb_bits = limb_bits - nail_bits;
83
generate (limb_bits, nail_bits, 10);
85
printf ("/* This file generated by gen-bases.c - DO NOT EDIT. */\n");
87
printf ("#if GMP_NUMB_BITS != %d\n", numb_bits);
88
printf ("Error, error, this data is for %d bits\n", numb_bits);
91
printf ("/* mp_bases[10] data, as literal values */\n");
92
printf ("#define MP_BASES_CHARS_PER_LIMB_10 %d\n", chars_per_limb);
93
printf ("#define MP_BASES_BIG_BASE_10 CNST_LIMB(0x");
94
mpz_out_str (stdout, 16, big_base);
96
printf ("#define MP_BASES_BIG_BASE_INVERTED_10 CNST_LIMB(0x");
97
mpz_out_str (stdout, 16, big_base_inverted);
99
printf ("#define MP_BASES_NORMALIZATION_STEPS_10 %d\n", normalization_steps);
103
table (int limb_bits, int nail_bits)
105
int numb_bits = limb_bits - nail_bits;
108
printf ("/* This file generated by gen-bases.c - DO NOT EDIT. */\n");
110
printf ("#include \"gmp.h\"\n");
111
printf ("#include \"gmp-impl.h\"\n");
113
printf ("#if GMP_NUMB_BITS != %d\n", numb_bits);
114
printf ("Error, error, this data is for %d bits\n", numb_bits);
117
puts ("const struct bases mp_bases[257] =\n{");
118
puts (" /* 0 */ { 0, 0.0, 0 },");
119
puts (" /* 1 */ { 0, 1e37, 0 },");
120
for (base = 2; base <= 256; base++)
122
generate (limb_bits, nail_bits, base);
124
printf (" /* %3u */ { ", base);
127
printf ("%u, %.16f, 0x%x },\n",
128
chars_per_limb, chars_per_bit_exactly, ulog2 (base) - 1);
132
printf ("%u, %.16f, CNST_LIMB(0x",
133
chars_per_limb, chars_per_bit_exactly);
134
mpz_out_str (stdout, 16, big_base);
135
printf ("), CNST_LIMB(0x");
136
mpz_out_str (stdout, 16, big_base_inverted);
145
main (int argc, char **argv)
147
int limb_bits, nail_bits;
150
mpz_init (big_base_inverted);
155
fprintf (stderr, "Usage: gen-bases <header|table> <limbbits> <nailbits>\n");
159
limb_bits = atoi (argv[2]);
160
nail_bits = atoi (argv[3]);
164
|| nail_bits >= limb_bits)
166
fprintf (stderr, "Invalid limb/nail bits: %d %d\n",
167
limb_bits, nail_bits);
171
if (strcmp (argv[1], "header") == 0)
172
header (limb_bits, nail_bits);
173
else if (strcmp (argv[1], "table") == 0)
174
table (limb_bits, nail_bits);
177
fprintf (stderr, "Invalid header/table choice: %s\n", argv[1]);