Blame stdlib/gen-tst-strtod-round.c

Packit 6c4009
/* Generate table of tests in tst-strtod-round.c from
Packit 6c4009
   tst-strtod-round-data.
Packit 6c4009
   Copyright (C) 2012-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* Compile this program as:
Packit 6c4009
Packit 6c4009
   gcc -std=gnu11 -O2 -Wall -Wextra gen-tst-strtod-round.c -lmpfr \
Packit 6c4009
     -o gen-tst-strtod-round
Packit 6c4009
Packit 6c4009
   (use of current MPFR version recommended) and run it as:
Packit 6c4009
Packit 6c4009
   gen-tst-strtod-round tst-strtod-round-data tst-strtod-round-data.h
Packit 6c4009
Packit 6c4009
   The output file will be generated as tst-strtod-round-data.h
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define _GNU_SOURCE
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <mpfr.h>
Packit 6c4009
Packit 6c4009
/* Work around incorrect ternary value from mpfr_strtofr
Packit 6c4009
   <https://sympa.inria.fr/sympa/arc/mpfr/2012-08/msg00005.html>.  */
Packit 6c4009
#define WORKAROUND
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
string_to_fp (mpfr_t f, const char *s, mpfr_rnd_t rnd)
Packit 6c4009
{
Packit 6c4009
  mpfr_clear_overflow ();
Packit 6c4009
#ifdef WORKAROUND
Packit 6c4009
  mpfr_t f2;
Packit 6c4009
  mpfr_init2 (f2, 100000);
Packit 6c4009
  int r0 = mpfr_strtofr (f2, s, NULL, 0, rnd);
Packit 6c4009
  int r = mpfr_set (f, f2, rnd);
Packit 6c4009
  r |= mpfr_subnormalize (f, r, rnd);
Packit 6c4009
  mpfr_clear (f2);
Packit 6c4009
  return r0 | r;
Packit 6c4009
#else
Packit 6c4009
  int r = mpfr_strtofr (f, s, NULL, 0, rnd);
Packit 6c4009
  r |= mpfr_subnormalize (f, r, rnd);
Packit 6c4009
  return r;
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
print_fp (FILE *fout, mpfr_t f, const char *suffix)
Packit 6c4009
{
Packit 6c4009
  if (mpfr_inf_p (f))
Packit 6c4009
    mpfr_fprintf (fout, "\t%sINF%s", mpfr_signbit (f) ? "-" : "", suffix);
Packit 6c4009
  else
Packit 6c4009
    mpfr_fprintf (fout, "\t%Ra%s", f, suffix);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
round_str (FILE *fout, const char *s, int prec, int emin, int emax,
Packit 6c4009
	   bool ibm_ld)
Packit 6c4009
{
Packit 6c4009
  mpfr_t max_value;
Packit 6c4009
  mpfr_t f;
Packit 6c4009
  mpfr_set_default_prec (prec);
Packit 6c4009
  mpfr_set_emin (emin);
Packit 6c4009
  mpfr_set_emax (emax);
Packit 6c4009
  mpfr_init (f);
Packit 6c4009
  int r = string_to_fp (f, s, MPFR_RNDD);
Packit 6c4009
  bool overflow = mpfr_overflow_p () != 0;
Packit 6c4009
  if (ibm_ld)
Packit 6c4009
    {
Packit 6c4009
      assert (prec == 106 && emin == -1073 && emax == 1024);
Packit 6c4009
      /* The maximum value in IBM long double has discontiguous
Packit 6c4009
	 mantissa bits.  */
Packit 6c4009
      mpfr_init2 (max_value, 107);
Packit 6c4009
      mpfr_set_str (max_value, "0x1.fffffffffffff7ffffffffffffcp+1023", 0,
Packit 6c4009
		    MPFR_RNDN);
Packit 6c4009
      if (mpfr_cmpabs (f, max_value) > 0)
Packit 6c4009
	{
Packit 6c4009
	  r = 1;
Packit 6c4009
	  overflow = true;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  mpfr_fprintf (fout, "\t%s,\n", r ? "false" : "true");
Packit 6c4009
  print_fp (fout, f, overflow ? ", true,\n" : ", false,\n");
Packit 6c4009
  string_to_fp (f, s, MPFR_RNDN);
Packit 6c4009
  overflow = (mpfr_overflow_p () != 0
Packit 6c4009
	      || (ibm_ld && mpfr_cmpabs (f, max_value) > 0));
Packit 6c4009
  print_fp (fout, f, overflow ? ", true,\n" : ", false,\n");
Packit 6c4009
  string_to_fp (f, s, MPFR_RNDZ);
Packit 6c4009
  overflow = (mpfr_overflow_p () != 0
Packit 6c4009
	      || (ibm_ld && mpfr_cmpabs (f, max_value) > 0));
Packit 6c4009
  print_fp (fout, f, overflow ? ", true,\n" : ", false,\n");
Packit 6c4009
  string_to_fp (f, s, MPFR_RNDU);
Packit 6c4009
  overflow = (mpfr_overflow_p () != 0
Packit 6c4009
	      || (ibm_ld && mpfr_cmpabs (f, max_value) > 0));
Packit 6c4009
  print_fp (fout, f, overflow ? ", true" : ", false");
Packit 6c4009
  mpfr_clear (f);
Packit 6c4009
  if (ibm_ld)
Packit 6c4009
    mpfr_clear (max_value);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
round_for_all (FILE *fout, const char *s)
Packit 6c4009
{
Packit 6c4009
  static const struct fmt {
Packit 6c4009
    int prec;
Packit 6c4009
    int emin;
Packit 6c4009
    int emax;
Packit 6c4009
    bool ibm_ld;
Packit 6c4009
  } formats[] = {
Packit 6c4009
    { 24, -148, 128, false },
Packit 6c4009
    { 53, -1073, 1024, false },
Packit 6c4009
    /* This is the Intel extended float format.  */
Packit 6c4009
    { 64, -16444, 16384, false },
Packit 6c4009
    /* This is the Motorola extended float format.  */
Packit 6c4009
    { 64, -16445, 16384, false },
Packit 6c4009
    { 106, -1073, 1024, true },
Packit 6c4009
    { 113, -16493, 16384, false },
Packit 6c4009
  };
Packit 6c4009
  mpfr_fprintf (fout, "  TEST (\"");
Packit 6c4009
  const char *p;
Packit 6c4009
  for (p = s; *p; p++)
Packit 6c4009
    {
Packit 6c4009
      fputc (*p, fout);
Packit 6c4009
      if ((p - s) % 60 == 59 && p[1])
Packit 6c4009
	mpfr_fprintf (fout, "\"\n\t\"");
Packit 6c4009
    }
Packit 6c4009
  mpfr_fprintf (fout, "\",\n");
Packit 6c4009
  int i;
Packit 6c4009
  int n_formats = sizeof (formats) / sizeof (formats[0]);
Packit 6c4009
  for (i = 0; i < n_formats; i++)
Packit 6c4009
    {
Packit 6c4009
      round_str (fout, s, formats[i].prec, formats[i].emin,
Packit 6c4009
		 formats[i].emax, formats[i].ibm_ld);
Packit 6c4009
      if (i < n_formats - 1)
Packit 6c4009
	mpfr_fprintf (fout, ",\n");
Packit 6c4009
    }
Packit 6c4009
  mpfr_fprintf (fout, "),\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  char *p = NULL;
Packit 6c4009
  size_t len;
Packit 6c4009
  ssize_t nbytes;
Packit 6c4009
  FILE *fin, *fout;
Packit 6c4009
  char *fin_name, *fout_name;
Packit 6c4009
Packit 6c4009
  if (argc < 3)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "Usage: %s <input> <output>\n", basename (argv[0]));
Packit 6c4009
      return EXIT_FAILURE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fin_name = argv[1];
Packit 6c4009
  fout_name = argv[2];
Packit 6c4009
Packit 6c4009
  fin = fopen (fin_name, "r");
Packit 6c4009
  if (fin == NULL)
Packit 6c4009
    {
Packit 6c4009
      perror ("Could not open input for reading");
Packit 6c4009
      return EXIT_FAILURE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fout = fopen (fout_name, "w");
Packit 6c4009
  if (fout == NULL)
Packit 6c4009
    {
Packit 6c4009
      perror ("Could not open output for writing");
Packit 6c4009
      return EXIT_FAILURE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fprintf (fout, "/* This file was generated by %s from %s.  */\n",
Packit 6c4009
	  __FILE__, fin_name);
Packit 6c4009
  fputs ("static const struct test tests[] = {\n", fout);
Packit 6c4009
  while ((nbytes = getline (&p, &len, fin)) != -1)
Packit 6c4009
    {
Packit 6c4009
      if (p[nbytes - 1] == '\n')
Packit 6c4009
	p[nbytes - 1] = 0;
Packit 6c4009
      round_for_all (fout, p);
Packit 6c4009
      free (p);
Packit 6c4009
      p = NULL;
Packit 6c4009
    }
Packit 6c4009
  fputs ("};\n", fout);
Packit 6c4009
Packit 6c4009
  return EXIT_SUCCESS;
Packit 6c4009
}