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
|
/* Test file for l2b constants.
Copyright 2007-2020 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.
This file is part of the GNU MPFR Library.
The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of 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.
The GNU MPFR 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 Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
/* Execute this program with an argument to generate code that initializes
the l2b constants. */
#include "mpfr-test.h"
/* Must be a multiple of 4 */
static const int bits2use[] = {8, 16, 32, 64, 96, 128, 256};
#define size_of_bits2use (numberof (bits2use))
static __mpfr_struct l2b[BASE_MAX-1][2];
static void
print_mpfr (mpfr_srcptr x, const char *name)
{
unsigned char temp[16]; /* buffer for the base-256 string */
unsigned char *ptr; /* pointer to its first non-zero byte */
int size; /* size of the string */
int i; /* bits2use index */
int j; /* output limb index */
int k; /* byte index (in output limb) */
int r; /* digit index, relative to ptr */
char prefix[12]; /* "0x" or "UINT64_C(0x" */
char suffix[2]; /* "" or ")" */
if (printf ("#if 0\n") < 0)
{ fprintf (stderr, "Error in printf\n"); exit (1); }
for (i = 0; i < size_of_bits2use; i++)
{
if (bits2use[i] == 64)
{
strcpy (prefix, "UINT64_C(0x");
strcpy (suffix, ")");
}
else
{
strcpy (prefix, "0x");
strcpy (suffix, "");
}
if (printf ("#elif GMP_NUMB_BITS == %d\n"
"const mp_limb_t %s__tab[] = { %s", bits2use[i], name,
prefix) < 0)
{ fprintf (stderr, "Error in printf\n"); exit (1); }
size = mpn_get_str (temp, 256, MPFR_MANT (x), MPFR_LIMB_SIZE (x));
MPFR_ASSERTN (size <= 16);
ptr = temp;
/* Skip leading zeros. */
while (*ptr == 0)
{
ptr++;
size--;
MPFR_ASSERTN (size > 0);
}
MPFR_ASSERTN (*ptr >= 128);
for (j = (MPFR_PREC (x) - 1) / bits2use[i]; j >= 0; j--)
{
r = j * (bits2use[i] / 8);
for (k = 0; k < bits2use[i] / 8; k++)
if (printf ("%02x", r < size ? ptr[r++] : 0) < 0)
{ fprintf (stderr, "Error in printf\n"); exit (1); }
if (j == 0 && printf ("%s };\n", suffix) < 0)
{ fprintf (stderr, "Error in printf\n"); exit (1); }
else if (j > 0 && printf ("%s, %s", suffix, prefix) < 0)
{ fprintf (stderr, "Error in printf\n"); exit (1); }
}
}
if (printf ("#endif\n\n") < 0)
{ fprintf (stderr, "Error in printf\n"); exit (1); }
}
static void
compute_l2b (int output)
{
mpfr_ptr p;
mpfr_srcptr t;
int beta, i;
int error = 0;
char buffer[256]; /* larger than needed, for maintainability */
if (output)
printf ("#ifndef UINT64_C\n# define UINT64_C(c) c\n#endif\n\n");
for (beta = 2; beta <= BASE_MAX; beta++)
{
for (i = 0; i < 2; i++)
{
p = &l2b[beta-2][i];
/* Compute the value */
if (i == 0)
{
/* 23-bit upper approximation to log(b)/log(2) */
mpfr_init2 (p, 23);
mpfr_set_ui (p, beta, MPFR_RNDU);
mpfr_log2 (p, p, MPFR_RNDU);
}
else
{
/* 77-bit upper approximation to log(2)/log(b) */
mpfr_init2 (p, 77);
mpfr_set_ui (p, beta, MPFR_RNDD);
mpfr_log2 (p, p, MPFR_RNDD);
mpfr_ui_div (p, 1, p, MPFR_RNDU);
}
sprintf (buffer, "mpfr_l2b_%d_%d", beta, i);
if (output)
print_mpfr (p, buffer);
/* Check the value */
t = &__gmpfr_l2b[beta-2][i];
if (t == NULL || MPFR_PREC (t) == 0 || !mpfr_equal_p (p, t))
{
if (!output)
{
error = 1;
printf ("Error for constant %s\n", buffer);
}
}
if (!output)
mpfr_clear (p);
}
}
if (output)
{
if (printf ("const __mpfr_struct __gmpfr_l2b[BASE_MAX-1][2] = {\n")
< 0)
{ fprintf (stderr, "Error in printf\n"); exit (1); }
for (beta = 2; beta <= BASE_MAX; beta++)
{
for (i = 0; i < 2; i++)
{
p = &l2b[beta-2][i];
if (printf (" %c {%3d,%2d,%3ld, (mp_limb_t *) "
"mpfr_l2b_%d_%d__tab }%s\n", i == 0 ? '{' : ' ',
(int) MPFR_PREC (p), MPFR_SIGN (p),
(long) MPFR_GET_EXP (p), beta, i,
i == 0 ? "," : beta < BASE_MAX ? " }," : " } };")
< 0)
{ fprintf (stderr, "Error in printf\n"); exit (1); }
mpfr_clear (p);
}
}
}
/* If there was an error, the test fails. */
if (error)
exit (1);
}
int
main (int argc, char *argv[])
{
if (argc != 1)
{
/* Generate code that initializes the l2b constants. */
compute_l2b (1);
}
else
{
/* Check the l2b constants. */
tests_start_mpfr ();
compute_l2b (0);
tests_end_mpfr ();
}
return 0;
}
|