1 /* Generate data for fpioconst.c.
2    Copyright (C) 2012-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <gmp.h>
24 #include <stdint.h>
25 
26 int
main(void)27 main (void)
28 {
29   FILE *out32 = fopen ("fpioconst-32", "w");
30   if (out32 == NULL)
31     abort ();
32   FILE *out64 = fopen ("fpioconst-64", "w");
33   if (out64 == NULL)
34     abort ();
35   FILE *outtable = fopen ("fpioconst-table", "w");
36   if (outtable == NULL)
37     abort ();
38   mpz_t p;
39   mpz_init (p);
40   for (int i = 0; i <= 14; i++)
41     {
42       int j = 1 << i;
43       mpz_ui_pow_ui (p, 10, j - 1);
44       int exp_m = mpz_sizeinbase (p, 2);
45       mpz_ui_pow_ui (p, 10, j);
46       int exp_p = mpz_sizeinbase (p, 2);
47       int size32 = 2 + (exp_p + 31) / 32;
48       int size64 = 1 + (exp_p + 63) / 64;
49       uint32_t data32[size32];
50       uint64_t data64[size64];
51       memset (data32, 0, sizeof data32);
52       memset (data64, 0, sizeof data64);
53       mpz_export (data32 + 2, NULL, -1, 4, 0, 0, p);
54       mpz_export (data64 + 1, NULL, -1, 8, 0, 0, p);
55       if (i == 0)
56 	{
57 	  fprintf (out32, "#define TENS_P%d_IDX\t0\n", i);
58 	  fprintf (out64, "#define TENS_P%d_IDX\t0\n", i);
59 	}
60       else
61 	{
62 	  fprintf (out32, "#define TENS_P%d_IDX\t"
63 		   "(TENS_P%d_IDX + TENS_P%d_SIZE)\n",
64 		   i, i - 1, i - 1);
65 	  fprintf (out64, "#define TENS_P%d_IDX\t"
66 		   "(TENS_P%d_IDX + TENS_P%d_SIZE)\n",
67 		   i, i - 1, i - 1);
68 	}
69       fprintf (out32, "#define TENS_P%d_SIZE\t%d\n", i, size32);
70       fprintf (out64, "#define TENS_P%d_SIZE\t%d\n", i, size64);
71       for (int k = 0; k < size32; k++)
72 	{
73 	  if (k == 0)
74 	    fprintf (out32, "  [TENS_P%d_IDX] = ", i);
75 	  else if (k % 6 == 5)
76 	    fprintf (out32, "\n  ");
77 	  else
78 	    fprintf (out32, " ");
79 	  fprintf (out32, "0x%08"PRIx32",", data32[k]);
80 	}
81       for (int k = 0; k < size64; k++)
82 	{
83 	  if (k == 0)
84 	    fprintf (out64, "  [TENS_P%d_IDX] = ", i);
85 	  else if (k % 3 == 2)
86 	    fprintf (out64, "\n  ");
87 	  else
88 	    fprintf (out64, " ");
89 	  fprintf (out64, "0x%016"PRIx64"ull,", data64[k]);
90 	}
91       fprintf (out32, "\n\n");
92       fprintf (out64, "\n\n");
93       const char *t = (i >= 10 ? "\t" : "\t\t");
94       if (i == 0)
95 	fprintf (outtable, "  { TENS_P%d_IDX, TENS_P%d_SIZE,%s%d,\t      },\n",
96 		 i, i, t, exp_p);
97       else
98 	fprintf (outtable, "  { TENS_P%d_IDX, TENS_P%d_SIZE,%s%d,\t%5d },\n",
99 		 i, i, t, exp_p, exp_m);
100     }
101   if (fclose (out32) != 0)
102     abort ();
103   if (fclose (out64) != 0)
104     abort ();
105   if (fclose (outtable) != 0)
106     abort ();
107   return 0;
108 }
109