Refactor tst-strtod-round.c
[glibc.git] / stdlib / gen-tst-strtod-round.c
blobfa5562ee002d3b1facf6d82138a77fcc3af1e901
1 /* Generate table of tests in tst-strtod-round.c from
2 tst-strtod-round-data.
3 Copyright (C) 2012-2016 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 /* Compile this program as:
22 gcc -std=gnu11 -O2 -Wall -Wextra gen-tst-strtod-round.c -lmpfr
23 -o gen-tst-strtod-round
25 (use of current MPFR version recommended) and run it as:
27 gen-tst-strtod-round tst-strtod-round-data tst-strtod-round-data.h
29 The output file will be generated as tst-strtod-round-data.h
33 #define _GNU_SOURCE
34 #include <assert.h>
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <mpfr.h>
41 /* Work around incorrect ternary value from mpfr_strtofr
42 <https://sympa.inria.fr/sympa/arc/mpfr/2012-08/msg00005.html>. */
43 #define WORKAROUND
45 static int
46 string_to_fp (mpfr_t f, const char *s, mpfr_rnd_t rnd)
48 #ifdef WORKAROUND
49 mpfr_t f2;
50 mpfr_init2 (f2, 100000);
51 int r0 = mpfr_strtofr (f2, s, NULL, 0, rnd);
52 int r = mpfr_set (f, f2, rnd);
53 mpfr_subnormalize (f, r, rnd);
54 mpfr_clear (f2);
55 return r0 | r;
56 #else
57 int r = mpfr_strtofr (f, s, NULL, 0, rnd);
58 mpfr_subnormalize (f, r, rnd);
59 return r;
60 #endif
63 static void
64 print_fp (FILE *fout, mpfr_t f, const char *suffix, const char *suffix2)
66 if (mpfr_inf_p (f))
67 mpfr_fprintf (fout, "\t%sINFINITY%s", mpfr_signbit (f) ? "-" : "",
68 suffix2);
69 else
70 mpfr_fprintf (fout, "\t%Ra%s%s", f, suffix, suffix2);
73 static void
74 round_str (FILE *fout, const char *s, const char *suffix,
75 int prec, int emin, int emax, bool ibm_ld)
77 mpfr_t f;
78 mpfr_set_default_prec (prec);
79 mpfr_set_emin (emin);
80 mpfr_set_emax (emax);
81 mpfr_init (f);
82 int r = string_to_fp (f, s, MPFR_RNDD);
83 if (ibm_ld)
85 assert (prec == 106 && emin == -1073 && emax == 1024);
86 /* The maximum value in IBM long double has discontiguous
87 mantissa bits. */
88 mpfr_t max_value;
89 mpfr_init2 (max_value, 107);
90 mpfr_set_str (max_value, "0x1.fffffffffffff7ffffffffffffcp+1023", 0,
91 MPFR_RNDN);
92 if (mpfr_cmpabs (f, max_value) > 0)
93 r = 1;
94 mpfr_clear (max_value);
96 mpfr_fprintf (fout, "\t%s,\n", r ? "false" : "true");
97 print_fp (fout, f, suffix, ",\n");
98 string_to_fp (f, s, MPFR_RNDN);
99 print_fp (fout, f, suffix, ",\n");
100 string_to_fp (f, s, MPFR_RNDZ);
101 print_fp (fout, f, suffix, ",\n");
102 string_to_fp (f, s, MPFR_RNDU);
103 print_fp (fout, f, suffix, "");
104 mpfr_clear (f);
107 static void
108 round_for_all (FILE *fout, const char *s)
110 static const struct fmt {
111 const char *suffix;
112 int prec;
113 int emin;
114 int emax;
115 bool ibm_ld;
116 } formats[7] = {
117 { "f", 24, -148, 128, false },
118 { "", 53, -1073, 1024, false },
119 { "L", 53, -1073, 1024, false },
120 /* This is the Intel extended float format. */
121 { "L", 64, -16444, 16384, false },
122 /* This is the Motorola extended float format. */
123 { "L", 64, -16445, 16384, false },
124 { "L", 106, -1073, 1024, true },
125 { "L", 113, -16493, 16384, false },
127 mpfr_fprintf (fout, " TEST (\"");
128 const char *p;
129 for (p = s; *p; p++)
131 fputc (*p, fout);
132 if ((p - s) % 60 == 59 && p[1])
133 mpfr_fprintf (fout, "\"\n\t\"");
135 mpfr_fprintf (fout, "\",\n");
136 int i;
137 for (i = 0; i < 7; i++)
139 round_str (fout, s, formats[i].suffix, formats[i].prec,
140 formats[i].emin, formats[i].emax, formats[i].ibm_ld);
141 if (i < 6)
142 mpfr_fprintf (fout, ",\n");
144 mpfr_fprintf (fout, "),\n");
148 main (int argc, char **argv)
150 char *p = NULL;
151 size_t len;
152 ssize_t nbytes;
153 FILE *fin, *fout;
154 char *fin_name, *fout_name;
156 if (argc < 3)
158 fprintf (stderr, "Usage: %s <input> <output>\n", basename (argv[0]));
159 return EXIT_FAILURE;
162 fin_name = argv[1];
163 fout_name = argv[2];
165 fin = fopen (fin_name, "r");
166 if (fin == NULL)
168 perror ("Could not open input for reading");
169 return EXIT_FAILURE;
172 fout = fopen (fout_name, "w");
173 if (fout == NULL)
175 perror ("Could not open output for writing");
176 return EXIT_FAILURE;
179 fprintf (fout, "/* This file was generated by %s from %s. */\n",
180 __FILE__, fin_name);
181 fputs ("static const struct test tests[] = {\n", fout);
182 while ((nbytes = getline (&p, &len, fin)) != -1)
184 if (p[nbytes - 1] == '\n')
185 p[nbytes - 1] = 0;
186 round_for_all (fout, p);
187 free (p);
188 p = NULL;
190 fputs ("};\n", fout);
192 return EXIT_SUCCESS;