xref: /netbsd-src/external/lgpl3/mpfr/dist/tests/tl2b.c (revision ba125506a622fe649968631a56eba5d42ff57863)
1 /* Test file for l2b constants.
2 
3 Copyright 2007-2023 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5 
6 This file is part of the GNU MPFR Library.
7 
8 The GNU MPFR 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 3 of the License, or (at your
11 option) any later version.
12 
13 The GNU MPFR 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.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 /* Execute this program with an argument to generate code that initializes
24    the l2b constants. */
25 
26 #include "mpfr-test.h"
27 
28 /* Must be a multiple of 4 */
29 static const int bits2use[] = {8, 16, 32, 64, 96, 128, 256};
30 #define size_of_bits2use (numberof (bits2use))
31 
32 static __mpfr_struct l2b[BASE_MAX-1][2];
33 
34 static void
print_mpfr(mpfr_srcptr x,const char * name)35 print_mpfr (mpfr_srcptr x, const char *name)
36 {
37   unsigned char temp[16]; /* buffer for the base-256 string */
38   unsigned char *ptr;     /* pointer to its first non-zero byte */
39   int size;               /* size of the string */
40   int i;                  /* bits2use index */
41   int j;                  /* output limb index */
42   int k;                  /* byte index (in output limb) */
43   int r;                  /* digit index, relative to ptr */
44   char prefix[12];        /* "0x" or "UINT64_C(0x" */
45   char suffix[2];         /* "" or ")" */
46 
47   if (printf ("#if 0\n") < 0)
48     { fprintf (stderr, "Error in printf\n"); exit (1); }
49   for (i = 0; i < size_of_bits2use; i++)
50     {
51       if (bits2use[i] == 64)
52         {
53           strcpy (prefix, "UINT64_C(0x");
54           strcpy (suffix, ")");
55         }
56       else
57         {
58           strcpy (prefix, "0x");
59           strcpy (suffix, "");
60         }
61       if (printf ("#elif GMP_NUMB_BITS == %d\n"
62                   "const mp_limb_t %s__tab[] = { %s", bits2use[i], name,
63                   prefix) < 0)
64         { fprintf (stderr, "Error in printf\n"); exit (1); }
65       size = mpn_get_str (temp, 256, MPFR_MANT (x), MPFR_LIMB_SIZE (x));
66       MPFR_ASSERTN (size <= 16);
67       ptr = temp;
68       /* Skip leading zeros. */
69       while (*ptr == 0)
70         {
71           ptr++;
72           size--;
73           MPFR_ASSERTN (size > 0);
74         }
75       MPFR_ASSERTN (*ptr >= 128);
76       for (j = (MPFR_PREC (x) - 1) / bits2use[i]; j >= 0; j--)
77         {
78           r = j * (bits2use[i] / 8);
79           for (k = 0; k < bits2use[i] / 8; k++)
80             if (printf ("%02x", r < size ? ptr[r++] : 0) < 0)
81               { fprintf (stderr, "Error in printf\n"); exit (1); }
82           if (j == 0 && printf ("%s };\n", suffix) < 0)
83             { fprintf (stderr, "Error in printf\n"); exit (1); }
84           else if (j > 0 && printf ("%s, %s", suffix, prefix) < 0)
85             { fprintf (stderr, "Error in printf\n"); exit (1); }
86         }
87     }
88   if (printf ("#endif\n\n") < 0)
89     { fprintf (stderr, "Error in printf\n"); exit (1); }
90 }
91 
92 static void
compute_l2b(int output)93 compute_l2b (int output)
94 {
95   mpfr_ptr p;
96   mpfr_srcptr t;
97   int beta, i;
98   int error = 0;
99   char buffer[256];  /* larger than needed, for maintainability */
100 
101   if (output)
102     printf ("#ifndef UINT64_C\n# define UINT64_C(c) c\n#endif\n\n");
103 
104   for (beta = 2; beta <= BASE_MAX; beta++)
105     {
106       for (i = 0; i < 2; i++)
107         {
108           p = &l2b[beta-2][i];
109 
110           /* Compute the value */
111           if (i == 0)
112             {
113               /* 23-bit upper approximation to log(b)/log(2) */
114               mpfr_init2 (p, 23);
115               mpfr_set_ui (p, beta, MPFR_RNDU);
116               mpfr_log2 (p, p, MPFR_RNDU);
117             }
118           else
119             {
120               /* 77-bit upper approximation to log(2)/log(b) */
121               mpfr_init2 (p, 77);
122               mpfr_set_ui (p, beta, MPFR_RNDD);
123               mpfr_log2 (p, p, MPFR_RNDD);
124               mpfr_ui_div (p, 1, p, MPFR_RNDU);
125             }
126 
127           sprintf (buffer, "mpfr_l2b_%d_%d", beta, i);
128           if (output)
129             print_mpfr (p, buffer);
130 
131           /* Check the value */
132           t = &__gmpfr_l2b[beta-2][i];
133           if (t == NULL || MPFR_PREC (t) == 0 || !mpfr_equal_p (p, t))
134             {
135               if (!output)
136                 {
137                   error = 1;
138                   printf ("Error for constant %s\n", buffer);
139                 }
140             }
141 
142           if (!output)
143             mpfr_clear (p);
144         }
145     }
146 
147   if (output)
148     {
149       if (printf ("const __mpfr_struct __gmpfr_l2b[BASE_MAX-1][2] = {\n")
150           < 0)
151         { fprintf (stderr, "Error in printf\n"); exit (1); }
152       for (beta = 2; beta <= BASE_MAX; beta++)
153         {
154           for (i = 0; i < 2; i++)
155             {
156               p = &l2b[beta-2][i];
157               if (printf ("  %c {%3d,%2d,%3ld, (mp_limb_t *) "
158                           "mpfr_l2b_%d_%d__tab }%s\n", i == 0 ? '{' : ' ',
159                           (int) MPFR_PREC (p), MPFR_SIGN (p),
160                           (long) MPFR_GET_EXP (p), beta, i,
161                           i == 0 ? "," : beta < BASE_MAX ? " }," : " } };")
162                   < 0)
163                 { fprintf (stderr, "Error in printf\n"); exit (1); }
164               mpfr_clear (p);
165             }
166         }
167     }
168 
169   /* If there was an error, the test fails. */
170   if (error)
171     exit (1);
172 }
173 
174 int
main(int argc,char * argv[])175 main (int argc, char *argv[])
176 {
177   if (argc != 1)
178     {
179       /* Generate code that initializes the l2b constants. */
180       compute_l2b (1);
181     }
182   else
183     {
184       /* Check the l2b constants. */
185       tests_start_mpfr ();
186       compute_l2b (0);
187       tests_end_mpfr ();
188     }
189   return 0;
190 }
191