diff --git a/math/Makefile b/math/Makefile
index 90b3b68..d78f40f 100644
--- a/math/Makefile
+++ b/math/Makefile
@@ -252,7 +252,7 @@ tests += test-math-isinff test-math-iszero test-math-issignaling \
 	 test-math-iscanonical test-math-cxx11 test-math-iseqsig
 endif
 
-ifneq (no,$(PERL))
+ifdef PYTHON
 libm-vec-tests = $(addprefix test-,$(libmvec-tests))
 libm-test-support = $(foreach t,$(test-types),libm-test-support-$(t))
 test-extras += $(libm-test-support)
@@ -263,9 +263,9 @@ extra-test-objs += $(addsuffix .o, $(libm-vec-test-wrappers))
 
 ulps-file = $(firstword $(wildcard $(sysdirs:%=%/libm-test-ulps)))
 
-$(objpfx)libm-test-ulps.h: $(ulps-file) gen-libm-test.pl
+$(objpfx)libm-test-ulps.h: $(ulps-file) gen-libm-test.py
 	$(make-target-directory)
-	$(PERL) gen-libm-test.pl -u $< -H $@
+	$(PYTHON) gen-libm-test.py -u $< -H $@
 
 libm-test-funcs-auto = acos acosh asin asinh atan atan2 atanh cabs cacos \
 		       cacosh carg casin casinh catan catanh cbrt ccos ccosh \
@@ -333,21 +333,21 @@ libm-test-c-noauto-obj = $(addprefix $(objpfx),$(libm-test-c-noauto))
 libm-test-c-narrow-obj = $(addprefix $(objpfx),$(libm-test-c-narrow))
 
 $(libm-test-c-noauto-obj): $(objpfx)libm-test%.c: libm-test%.inc \
-						  gen-libm-test.pl
+						  gen-libm-test.py
 	$(make-target-directory)
-	$(PERL) gen-libm-test.pl -c $< -a /dev/null -C $@
+	$(PYTHON) gen-libm-test.py -c $< -a /dev/null -C $@
 
 $(libm-test-c-auto-obj): $(objpfx)libm-test%.c: libm-test%.inc \
-						gen-libm-test.pl \
+						gen-libm-test.py \
 						auto-libm-test-out%
 	$(make-target-directory)
-	$(PERL) gen-libm-test.pl -c $< -a auto-libm-test-out$* -C $@
+	$(PYTHON) gen-libm-test.py -c $< -a auto-libm-test-out$* -C $@
 
 $(libm-test-c-narrow-obj): $(objpfx)libm-test%.c: libm-test%.inc \
-						  gen-libm-test.pl \
+						  gen-libm-test.py \
 						  auto-libm-test-out%
 	$(make-target-directory)
-	$(PERL) gen-libm-test.pl -c $< -a auto-libm-test-out$* -C $@
+	$(PYTHON) gen-libm-test.py -c $< -a auto-libm-test-out$* -C $@
 endif
 
 ifdef PYTHON
@@ -473,7 +473,7 @@ $(objpfx)gen-libm-templates.stmp: Makefile
 $(foreach t, $(call type-foreach, $(gen-all-calls)), \
              $(objpfx)$(t).c): $(objpfx)gen-libm-templates.stmp
 
-ifneq (no,$(PERL))
+ifdef PYTHON
 # This must come after the inclusion of sysdeps Makefiles via Rules.
 
 $(foreach t,$(libm-tests-normal),$(objpfx)$(t).c): $(objpfx)test-%.c:
@@ -628,15 +628,15 @@ regen-ulps: $(addprefix $(objpfx),$(libm-tests))
 	  cat $(objpfx)ULPs >> $(objpfx)libm-test-ulps; \
 	  rm $(objpfx)ULPs; \
 	done; \
-	$(PERL) gen-libm-test.pl -n $(objpfx)NewUlps \
-				 -u $(objpfx)libm-test-ulps; \
+	$(PYTHON) gen-libm-test.py -n $(objpfx)NewUlps \
+				   -u $(objpfx)libm-test-ulps; \
 	echo "Automatic regeneration of ULPs complete."; \
 	echo "Difference between the current baseline and the new baseline is:";\
 	diff -urN $(ulps-file) $(objpfx)NewUlps; \
 	echo "Copy $(objpfx)NewUlps to $(ulps-file) (relative to source)."
 else
 regen-ulps:
-	@echo "Automatic regeneration of ULPs requires perl."; \
+	@echo "Automatic regeneration of ULPs requires python."; \
 	exit 1;
 endif
 
diff --git a/math/README.libm-test b/math/README.libm-test
index 41702c6..53172bb 100644
--- a/math/README.libm-test
+++ b/math/README.libm-test
@@ -3,10 +3,10 @@ README for libm-test math test suite
 
 The libm-test math test suite tests a number of function points of
 math functions in the GNU C library.  The following sections contain a
-brief overview.  Please note that the test drivers and the Perl script
-"gen-libm-test.pl" have some options.  A full list of options is
-available with --help (for the test drivers) and -h for
-"gen-libm-test.pl".
+brief overview.  Please note that the test drivers and the Python
+script "gen-libm-test.py" have some options.  A full list of options
+is available with --help (for the test drivers) and -h for
+"gen-libm-test.py".
 
 
 What is tested?
@@ -25,8 +25,8 @@ The main files are "libm-test-<func>.inc".  They are independent of
 the target platform and the specific real floating type and format and
 contain placeholder test "templates" for math functions defined in
 libm.  These files, along with generated files named
-"auto-libm-test-out-<func>", are preprocessed by the Perl script
-"gen-libm-test.pl" to expand the templates and produce a set of test
+"auto-libm-test-out-<func>", are preprocessed by the Python script
+"gen-libm-test.py" to expand the templates and produce a set of test
 cases for each math function that are specific to the target platform
 but still independent of the real floating type.  The results of the
 processing are "libm-test-<func>.c" and a file "libm-test-ulps.h" with
@@ -48,7 +48,7 @@ defines or undefines the __NO_MATH_INLINES macro just before including
 respectively.  Each driver is compiled into a single executable test
 program with the corresponding name.
 
-As mentioned above, the "gen-libm-test.pl" script looks for a file
+As mentioned above, the "gen-libm-test.py" script looks for a file
 named "libm-test-ulps" in the platform specific sysdep directory (or
 its fpu or nofpu subdirectory) and for each variant (real floating
 type and rounding mode) of every tested function reads from it the
@@ -80,17 +80,17 @@ This generates a file "ULPs" with all double ULPs in it, ignoring any
 previously calculated ULPs, and running with the newly built dynamic
 loader and math library (assumes you didn't install your build).  Now
 generate the ULPs for all other formats, the tests will be appending the
-data to the "ULPs" file.  As final step run "gen-libm-test.pl" with the
+data to the "ULPs" file.  As final step run "gen-libm-test.py" with the
 file as input and ask to generate a pretty printed output in the file
 "NewUlps":
-  gen-libm-test.pl -u ULPs -n NewUlps
+  gen-libm-test.py -u ULPs -n NewUlps
 Copy "NewUlps" to "libm-test-ulps" in the appropriate machine sysdep
 directory.
 
 Note that the test drivers have an option "-u" to output an unsorted
 list of all epsilons that the functions have.  The output can be read
 in directly but it's better to pretty print it first.
-"gen-libm-test.pl" has an option to generate a pretty-printed and
+"gen-libm-test.py" has an option to generate a pretty-printed and
 sorted new ULPs file from the output of the test drivers.
 
 Contents of libm-test-ulps
diff --git a/math/gen-libm-test.pl b/math/gen-libm-test.pl
deleted file mode 100755
index 664fba5..0000000
--- a/math/gen-libm-test.pl
+++ /dev/null
@@ -1,846 +0,0 @@
-#!/usr/bin/perl -w
-# Copyright (C) 1999-2018 Free Software Foundation, Inc.
-# This file is part of the GNU C Library.
-# Contributed by Andreas Jaeger <aj@suse.de>, 1999.
-
-# The GNU C 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 2.1 of the License, or (at your option) any later version.
-
-# The GNU C 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 C Library; if not, see
-# <http://www.gnu.org/licenses/>.
-
-# This file needs to be tidied up
-# Note that functions and tests share the same namespace.
-
-# Information about tests are stored in: %results
-# $results{$test}{"type"} is the result type, e.g. normal or complex.
-# $results{$test}{"has_ulps"} is set if deltas exist.
-# In the following description $type and $float are:
-# - $type is either "normal", "real" (for the real part of a complex number)
-#   or "imag" (for the imaginary part # of a complex number).
-# - $float is either of float, ifloat, double, idouble, ldouble, ildouble;
-#   It represents the underlying floating point type (float, double or long
-#   double) and if inline functions (the leading i stands for inline)
-#   are used.
-# $results{$test}{$type}{"ulp"}{$float} is defined and has a delta as value
-
-
-use Getopt::Std;
-
-use strict;
-
-use vars qw ($input $output $auto_input);
-use vars qw (%results);
-use vars qw (%beautify @all_floats %all_floats_pfx);
-use vars qw ($ulps_file);
-use vars qw (%auto_tests);
-
-# all_floats is sorted and contains all recognised float types
-@all_floats = ('double', 'float', 'float128', 'idouble',
-	       'ifloat', 'ifloat128', 'ildouble', 'ldouble');
-
-# all_floats_pfx maps C types to their C like prefix for macros.
-%all_floats_pfx =
-  ( "double" => "DBL",
-    "ldouble" => "LDBL",
-    "float" => "FLT",
-    "float128" => "FLT128",
-  );
-
-%beautify =
-  ( "minus_zero" => "-0",
-    "plus_zero" => "+0",
-    "-0x0p+0f" => "-0",
-    "-0x0p+0" => "-0",
-    "-0x0p+0L" => "-0",
-    "0x0p+0f" => "+0",
-    "0x0p+0" => "+0",
-    "0x0p+0L" => "+0",
-    "minus_infty" => "-inf",
-    "plus_infty" => "inf",
-    "qnan_value" => "qNaN",
-    "snan_value" => "sNaN",
-    "snan_value_ld" => "sNaN",
-  );
-
-
-# get Options
-# Options:
-# a: auto-libm-test-out input file
-# c: .inc input file
-# u: ulps-file
-# n: new ulps file
-# C: libm-test.c output file
-# H: libm-test-ulps.h output file
-# h: help
-use vars qw($opt_a $opt_c $opt_u $opt_n $opt_C $opt_H $opt_h);
-getopts('a:c:u:n:C:H:h');
-
-$ulps_file = 'libm-test-ulps';
-
-if ($opt_h) {
-  print "Usage: gen-libm-test.pl [OPTIONS]\n";
-  print " -h         print this help, then exit\n";
-  print " -a FILE    input file with automatically generated tests\n";
-  print " -c FILE    input file .inc file with tests\n";
-  print " -u FILE    input file with ulps\n";
-  print " -n FILE    generate sorted file FILE from libm-test-ulps\n";
-  print " -C FILE    generate output C file FILE from libm-test.inc\n";
-  print " -H FILE    generate output ulps header FILE from libm-test-ulps\n";
-  exit 0;
-}
-
-$ulps_file = $opt_u if ($opt_u);
-
-$input = $opt_c if ($opt_c);
-$auto_input = $opt_a if ($opt_a);
-$output = $opt_C if ($opt_C);
-
-&parse_ulps ($ulps_file) if ($opt_H || $opt_n);
-&parse_auto_input ($auto_input) if ($opt_C);
-&generate_testfile ($input, $output) if ($opt_C);
-&output_ulps ($opt_H, $ulps_file) if ($opt_H);
-&print_ulps_file ($opt_n) if ($opt_n);
-
-# Return a nicer representation
-sub beautify {
-  my ($arg) = @_;
-  my ($tmp);
-
-  if (exists $beautify{$arg}) {
-    return $beautify{$arg};
-  }
-  if ($arg =~ /^-/) {
-    $tmp = $arg;
-    $tmp =~ s/^-//;
-    if (exists $beautify{$tmp}) {
-      return '-' . $beautify{$tmp};
-    }
-  }
-  if ($arg =~ /^-?0x[0-9a-f.]*p[-+][0-9]+f$/) {
-    $arg =~ s/f$//;
-  }
-  if ($arg =~ /[0-9]L$/) {
-    $arg =~ s/L$//;
-  }
-  return $arg;
-}
-
-# Return a nicer representation of a complex number
-sub build_complex_beautify {
-  my ($r, $i) = @_;
-  my ($str1, $str2);
-
-  $str1 = &beautify ($r);
-  $str2 = &beautify ($i);
-  if ($str2 =~ /^-/) {
-    $str2 =~ s/^-//;
-    $str1 .= ' - ' . $str2;
-  } else {
-    $str1 .= ' + ' . $str2;
-  }
-  $str1 .= ' i';
-  return $str1;
-}
-
-# Return the text to put in an initializer for a test's exception
-# information.
-sub show_exceptions {
-  my ($ignore_result, $non_finite, $test_snan, $exception) = @_;
-  $ignore_result = ($ignore_result ? "IGNORE_RESULT|" : "");
-  $non_finite = ($non_finite ? "NON_FINITE|" : "");
-  $test_snan = ($test_snan ? "TEST_SNAN|" : "");
-  if (defined $exception) {
-    return ", ${ignore_result}${non_finite}${test_snan}$exception";
-  } else {
-    return ", ${ignore_result}${non_finite}${test_snan}0";
-  }
-}
-
-# Apply the LIT(x) or ARG_LIT(x) macro to a literal floating point constant
-# and strip any existing suffix.
-sub _apply_lit {
-  my ($macro, $lit) = @_;
-  my $exp_re = "([+-])?[[:digit:]]+";
-  # Don't wrap something that does not look like a:
-  #  * Hexadecimal FP value
-  #  * Decimal FP value without a decimal point
-  #  * Decimal value with a fraction
-  return $lit if $lit !~ /([+-])?0x[[:xdigit:]\.]+[pP]$exp_re/
-		 and $lit !~ /[[:digit:]]+[eE]$exp_re/
-		 and $lit !~ /[[:digit:]]*\.[[:digit:]]*([eE]$exp_re)?/;
-
-  # Strip any existing literal suffix.
-  $lit =~ s/[lLfF]$//;
-
-  return "$macro (${lit})";
-}
-
-# Apply LIT macro to individual tokens within an expression.
-#
-# This function assumes the C expression follows GNU coding
-# standards.  Specifically, a space separates each lexical
-# token.  Otherwise, this post-processing may apply LIT
-# incorrectly, or around an entire expression.
-sub apply_lit {
-  my ($lit) = @_;
-  my @toks = split (/ /, $lit);
-  foreach (@toks) {
-    $_ = _apply_lit ("LIT", $_);
-  }
-  return join (' ', @toks);
-}
-
-# Likewise, but apply ARG_LIT for arguments to narrowing functions.
-sub apply_arglit {
-  my ($lit) = @_;
-  my @toks = split (/ /, $lit);
-  foreach (@toks) {
-    $_ = _apply_lit ("ARG_LIT", $_);
-  }
-  return join (' ', @toks);
-}
-
-# Parse the arguments to TEST_x_y
-sub parse_args {
-  my ($file, $descr, $args) = @_;
-  my (@args, $descr_args, $descr_res, @descr);
-  my ($current_arg, $cline, $cline_res, $i);
-  my (@special);
-  my ($call_args);
-  my ($ignore_result_any, $ignore_result_all);
-  my ($num_res, @args_res, @start_rm, $rm);
-  my (@plus_oflow, @minus_oflow, @plus_uflow, @minus_uflow);
-  my (@errno_plus_oflow, @errno_minus_oflow);
-  my (@errno_plus_uflow, @errno_minus_uflow);
-  my (@xfail_rounding_ibm128_libgcc);
-  my ($non_finite, $test_snan);
-
-  ($descr_args, $descr_res) = split /_/,$descr, 2;
-
-  @args = split /,\s*/, $args;
-
-  $call_args = "";
-
-  # Generate first the string that's shown to the user
-  $current_arg = 1;
-  @descr = split //,$descr_args;
-  for ($i = 0; $i <= $#descr; $i++) {
-    my $comma = "";
-    if ($current_arg > 1) {
-      $comma = ', ';
-    }
-    # FLOAT, ARG_FLOAT, long double, int, unsigned int, long int, long long int
-    if ($descr[$i] =~ /f|a|j|i|u|l|L/) {
-      $call_args .= $comma . &beautify ($args[$current_arg]);
-      ++$current_arg;
-      next;
-    }
-    # Argument passed via pointer.
-    if ($descr[$i] =~ /p/) {
-      next;
-    }
-    # &FLOAT, &int - simplify call by not showing argument.
-    if ($descr[$i] =~ /F|I/) {
-      next;
-    }
-    # complex
-    if ($descr[$i] eq 'c') {
-      $call_args .= $comma . &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
-      $current_arg += 2;
-      next;
-    }
-
-    die ("$descr[$i] is unknown");
-  }
-
-  # Result
-  @args_res = @args[$current_arg .. $#args];
-  $num_res = 0;
-  @descr = split //,$descr_res;
-  foreach (@descr) {
-    if ($_ =~ /f|i|l|L|M|U/) {
-      ++$num_res;
-    } elsif ($_ eq 'c') {
-      $num_res += 2;
-    } elsif ($_ eq 'b') {
-      # boolean
-      ++$num_res;
-    } elsif ($_ eq '1') {
-      ++$num_res;
-    } else {
-      die ("$_ is unknown");
-    }
-  }
-  # consistency check
-  if ($#args_res == $num_res - 1) {
-    # One set of results for all rounding modes, no flags.
-    @start_rm = ( 0, 0, 0, 0 );
-  } elsif ($#args_res == $num_res) {
-    # One set of results for all rounding modes, with flags.
-    die ("wrong number of arguments")
-      unless ($args_res[$#args_res] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN|TEST_NAN_SIGN|NO_TEST_INLINE|XFAIL/);
-    @start_rm = ( 0, 0, 0, 0 );
-  } elsif ($#args_res == 4 * $num_res + 3) {
-    # One set of results per rounding mode, with flags.
-    @start_rm = ( 0, $num_res + 1, 2 * $num_res + 2, 3 * $num_res + 3 );
-  } else {
-    die ("wrong number of arguments");
-  }
-
-  # Put the C program line together
-  # Reset some variables to start again
-  $current_arg = 1;
-  $call_args =~ s/\"/\\\"/g;
-  $cline = "{ \"$call_args\"";
-  @descr = split //,$descr_args;
-  for ($i=0; $i <= $#descr; $i++) {
-    # FLOAT, ARG_FLOAT, long double, int, unsigned int, long int, long long int
-    if ($descr[$i] =~ /f|a|j|i|u|l|L/) {
-      if ($descr[$i] eq "f") {
-        $cline .= ", " . &apply_lit ($args[$current_arg]);
-      } elsif ($descr[$i] eq "a") {
-        $cline .= ", " . &apply_arglit ($args[$current_arg]);
-      } else {
-        $cline .= ", $args[$current_arg]";
-      }
-      $current_arg++;
-      next;
-    }
-    # &FLOAT, &int, argument passed via pointer
-    if ($descr[$i] =~ /F|I|p/) {
-      next;
-    }
-    # complex
-    if ($descr[$i] eq 'c') {
-      $cline .= ", " . &apply_lit ($args[$current_arg]);
-      $cline .= ", " . &apply_lit ($args[$current_arg+1]);
-      $current_arg += 2;
-      next;
-    }
-  }
-
-  @descr = split //,$descr_res;
-  @plus_oflow = qw(max_value plus_infty max_value plus_infty);
-  @minus_oflow = qw(minus_infty minus_infty -max_value -max_value);
-  @plus_uflow = qw(plus_zero plus_zero plus_zero min_subnorm_value);
-  @minus_uflow = qw(-min_subnorm_value minus_zero minus_zero minus_zero);
-  @errno_plus_oflow = qw(0 ERRNO_ERANGE 0 ERRNO_ERANGE);
-  @errno_minus_oflow = qw(ERRNO_ERANGE ERRNO_ERANGE 0 0);
-  @errno_plus_uflow = qw(ERRNO_ERANGE ERRNO_ERANGE ERRNO_ERANGE 0);
-  @errno_minus_uflow = qw(0 ERRNO_ERANGE ERRNO_ERANGE ERRNO_ERANGE);
-  @xfail_rounding_ibm128_libgcc = qw(XFAIL_IBM128_LIBGCC 0
-				     XFAIL_IBM128_LIBGCC XFAIL_IBM128_LIBGCC);
-  for ($rm = 0; $rm <= 3; $rm++) {
-    $current_arg = $start_rm[$rm];
-    $ignore_result_any = 0;
-    $ignore_result_all = 1;
-    $cline_res = "";
-    @special = ();
-    foreach (@descr) {
-      if ($_ =~ /b|f|j|i|l|L|M|U/ ) {
-	my ($result) = $args_res[$current_arg];
-	if ($result eq "IGNORE") {
-	  $ignore_result_any = 1;
-	  $result = "0";
-	} else {
-	  $ignore_result_all = 0;
-	}
-	if ($_ eq "f") {
-	  $result = apply_lit ($result);
-	}
-	$cline_res .= ", $result";
-	$current_arg++;
-      } elsif ($_ eq 'c') {
-	my ($result1) = $args_res[$current_arg];
-	if ($result1 eq "IGNORE") {
-	  $ignore_result_any = 1;
-	  $result1 = "0";
-	} else {
-	  $ignore_result_all = 0;
-	}
-	my ($result2) = $args_res[$current_arg + 1];
-	if ($result2 eq "IGNORE") {
-	  $ignore_result_any = 1;
-	  $result2 = "0";
-	} else {
-	  $ignore_result_all = 0;
-	}
-	$result1 = apply_lit ($result1);
-	$result2 = apply_lit ($result2);
-	$cline_res .= ", $result1, $result2";
-	$current_arg += 2;
-      } elsif ($_ eq '1') {
-	push @special, $args_res[$current_arg];
-	++$current_arg;
-      }
-    }
-    if ($ignore_result_any && !$ignore_result_all) {
-      die ("some but not all function results ignored\n");
-    }
-    # Determine whether any arguments or results, for any rounding
-    # mode, are non-finite.
-    $non_finite = ($args =~ /qnan_value|snan_value|plus_infty|minus_infty/);
-    $test_snan = ($args =~ /snan_value/);
-    # Add exceptions.
-    $cline_res .= show_exceptions ($ignore_result_any,
-				   $non_finite,
-				   $test_snan,
-				   ($current_arg <= $#args_res)
-				   ? $args_res[$current_arg]
-				   : undef);
-
-    # special treatment for some functions
-    $i = 0;
-    foreach (@special) {
-      ++$i;
-      my ($extra_expected) = $_;
-      my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
-      if (!$run_extra) {
-	$extra_expected = "0";
-      } else {
-	$extra_expected = apply_lit ($extra_expected);
-      }
-      $cline_res .= ", $run_extra, $extra_expected";
-    }
-    $cline_res =~ s/^, //;
-    $cline_res =~ s/plus_oflow/$plus_oflow[$rm]/g;
-    $cline_res =~ s/minus_oflow/$minus_oflow[$rm]/g;
-    $cline_res =~ s/plus_uflow/$plus_uflow[$rm]/g;
-    $cline_res =~ s/minus_uflow/$minus_uflow[$rm]/g;
-    $cline_res =~ s/ERRNO_PLUS_OFLOW/$errno_plus_oflow[$rm]/g;
-    $cline_res =~ s/ERRNO_MINUS_OFLOW/$errno_minus_oflow[$rm]/g;
-    $cline_res =~ s/ERRNO_PLUS_UFLOW/$errno_plus_uflow[$rm]/g;
-    $cline_res =~ s/ERRNO_MINUS_UFLOW/$errno_minus_uflow[$rm]/g;
-    $cline_res =~ s/XFAIL_ROUNDING_IBM128_LIBGCC/$xfail_rounding_ibm128_libgcc[$rm]/g;
-    $cline .= ", { $cline_res }";
-  }
-  print $file "    $cline },\n";
-}
-
-# Convert a condition from auto-libm-test-out to C form.
-sub convert_condition {
-  my ($cond) = @_;
-  my (@conds, $ret);
-  @conds = split /:/, $cond;
-  foreach (@conds) {
-    if ($_ !~ /^arg_fmt\(/) {
-      s/-/_/g;
-    }
-    s/^/TEST_COND_/;
-  }
-  $ret = join " && ", @conds;
-  return "($ret)";
-}
-
-# Return text to OR a value into an accumulated flags string.
-sub or_value {
-  my ($cond) = @_;
-  if ($cond eq "0") {
-    return "";
-  } else {
-    return " | $cond";
-  }
-}
-
-# Return a conditional expression between two values.
-sub cond_value {
-  my ($cond, $if, $else) = @_;
-  if ($cond eq "1") {
-    return $if;
-  } elsif ($cond eq "0") {
-    return $else;
-  } else {
-    return "($cond ? $if : $else)";
-  }
-}
-
-# Return text to OR a conditional expression between two values into
-# an accumulated flags string.
-sub or_cond_value {
-  my ($cond, $if, $else) = @_;
-  return or_value (cond_value ($cond, $if, $else));
-}
-
-# Generate libm-test.c
-sub generate_testfile {
-  my ($input, $output) = @_;
-
-  open INPUT, $input or die ("Can't open $input: $!");
-  open OUTPUT, ">$output" or die ("Can't open $output: $!");
-
-  # Replace the special macros
-  while (<INPUT>) {
-    # AUTO_TESTS (function),
-    if (/^\s*AUTO_TESTS_/) {
-      my ($descr, $func, @modes, $auto_test, $num_auto_tests);
-      my (@rm_tests, $rm, $i);
-      @modes = qw(downward tonearest towardzero upward);
-      ($descr, $func) = ($_ =~ /AUTO_TESTS_(\w+)\s*\((\w+)\)/);
-      for ($rm = 0; $rm <= 3; $rm++) {
-	$rm_tests[$rm] = [sort keys %{$auto_tests{$func}{$modes[$rm]}}];
-      }
-      $num_auto_tests = scalar @{$rm_tests[0]};
-      for ($rm = 1; $rm <= 3; $rm++) {
-	if ($num_auto_tests != scalar @{$rm_tests[$rm]}) {
-	  die ("inconsistent numbers of tests for $func\n");
-	}
-	for ($i = 0; $i < $num_auto_tests; $i++) {
-	  if ($rm_tests[0][$i] ne $rm_tests[$rm][$i]) {
-	    die ("inconsistent list of tests of $func\n");
-	  }
-	}
-      }
-      if ($num_auto_tests == 0) {
-	die ("no automatic tests for $func\n");
-      }
-      foreach $auto_test (@{$rm_tests[0]}) {
-	my ($format, $inputs, $format_conv, $args_str);
-	($format, $inputs) = split / /, $auto_test, 2;
-	$inputs =~ s/ /, /g;
-	$format_conv = convert_condition ($format);
-	print OUTPUT "#if $format_conv\n";
-	$args_str = "$func, $inputs";
-	for ($rm = 0; $rm <= 3; $rm++) {
-	  my ($auto_test_out, $outputs, $flags);
-	  my ($flags_conv, @flags, %flag_cond);
-	  $auto_test_out = $auto_tests{$func}{$modes[$rm]}{$auto_test};
-	  ($outputs, $flags) = split / : */, $auto_test_out;
-	  $outputs =~ s/ /, /g;
-	  @flags = split / /, $flags;
-	  foreach (@flags) {
-	    if (/^([^:]*):(.*)$/) {
-	      my ($flag, $cond);
-	      $flag = $1;
-	      $cond = convert_condition ($2);
-	      if (defined ($flag_cond{$flag})) {
-		if ($flag_cond{$flag} ne "1") {
-		  $flag_cond{$flag} .= " || $cond";
-		}
-	      } else {
-		$flag_cond{$flag} = $cond;
-	      }
-	    } else {
-	      $flag_cond{$_} = "1";
-	    }
-	  }
-	  $flags_conv = "";
-	  if (defined ($flag_cond{"ignore-zero-inf-sign"})) {
-	    $flags_conv .= or_cond_value ($flag_cond{"ignore-zero-inf-sign"},
-					  "IGNORE_ZERO_INF_SIGN", "0");
-	  }
-	  if (defined ($flag_cond{"no-test-inline"})) {
-	    $flags_conv .= or_cond_value ($flag_cond{"no-test-inline"},
-					  "NO_TEST_INLINE", "0");
-	  }
-	  if (defined ($flag_cond{"xfail"})) {
-	    $flags_conv .= or_cond_value ($flag_cond{"xfail"},
-					  "XFAIL_TEST", "0");
-	  }
-	  my (@exc_list) = qw(divbyzero inexact invalid overflow underflow);
-	  my ($exc);
-	  foreach $exc (@exc_list) {
-	    my ($exc_expected, $exc_ok, $no_exc, $exc_cond, $exc_ok_cond);
-	    $exc_expected = "\U$exc\E_EXCEPTION";
-	    $exc_ok = "\U$exc\E_EXCEPTION_OK";
-	    $no_exc = "0";
-	    if ($exc eq "inexact") {
-	      $exc_ok = "0";
-	      $no_exc = "NO_INEXACT_EXCEPTION";
-	    }
-	    if (defined ($flag_cond{$exc})) {
-	      $exc_cond = $flag_cond{$exc};
-	    } else {
-	      $exc_cond = "0";
-	    }
-	    if (defined ($flag_cond{"$exc-ok"})) {
-	      $exc_ok_cond = $flag_cond{"$exc-ok"};
-	    } else {
-	      $exc_ok_cond = "0";
-	    }
-	    $flags_conv .= or_cond_value ($exc_cond,
-					  cond_value ($exc_ok_cond,
-						      $exc_ok, $exc_expected),
-					  cond_value ($exc_ok_cond,
-						      $exc_ok, $no_exc));
-	  }
-	  my ($errno_expected, $errno_unknown_cond);
-	  if (defined ($flag_cond{"errno-edom"})) {
-	    if ($flag_cond{"errno-edom"} ne "1") {
-	      die ("unexpected condition for errno-edom");
-	    }
-	    if (defined ($flag_cond{"errno-erange"})) {
-	      die ("multiple errno values expected");
-	    }
-	    $errno_expected = "ERRNO_EDOM";
-	  } elsif (defined ($flag_cond{"errno-erange"})) {
-	    if ($flag_cond{"errno-erange"} ne "1") {
-	      die ("unexpected condition for errno-erange");
-	    }
-	    $errno_expected = "ERRNO_ERANGE";
-	  } else {
-	    $errno_expected = "ERRNO_UNCHANGED";
-	  }
-	  if (defined ($flag_cond{"errno-edom-ok"})) {
-	    if (defined ($flag_cond{"errno-erange-ok"})
-		&& ($flag_cond{"errno-erange-ok"}
-		    ne $flag_cond{"errno-edom-ok"})) {
-	      $errno_unknown_cond = "($flag_cond{\"errno-edom-ok\"} || $flag_cond{\"errno-erange-ok\"})";
-	    } else {
-	      $errno_unknown_cond = $flag_cond{"errno-edom-ok"};
-	    }
-	  } elsif (defined ($flag_cond{"errno-erange-ok"})) {
-	    $errno_unknown_cond = $flag_cond{"errno-erange-ok"};
-	  } else {
-	    $errno_unknown_cond = "0";
-	  }
-	  $flags_conv .= or_cond_value ($errno_unknown_cond,
-					"0", $errno_expected);
-	  if ($flags_conv eq "") {
-	    $flags_conv = ", NO_EXCEPTION";
-	  } else {
-	    $flags_conv =~ s/^ \|/,/;
-	  }
-	  $args_str .= ", $outputs$flags_conv";
-	}
-	&parse_args (\*OUTPUT, $descr, $args_str);
-	print OUTPUT "#endif\n";
-      }
-      next;
-    }
-
-    # TEST_...
-    if (/^\s*TEST_/) {
-      my ($descr, $args);
-      chop;
-      ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
-      &parse_args (\*OUTPUT, $descr, $args);
-      next;
-    }
-    print OUTPUT;
-  }
-  close INPUT;
-  close OUTPUT;
-}
-
-
-
-# Parse ulps file
-sub parse_ulps {
-  my ($file) = @_;
-  my ($test, $type, $float, $eps, $float_regex);
-
-  # Build a basic regex to match type entries in the
-  # generated ULPS file.
-  foreach my $ftype (@all_floats) {
-    $float_regex .= "|" . $ftype;
-  }
-  $float_regex = "^" . substr ($float_regex, 1) . ":";
-
-  # $type has the following values:
-  # "normal": No complex variable
-  # "real": Real part of complex result
-  # "imag": Imaginary part of complex result
-  open ULP, $file  or die ("Can't open $file: $!");
-  while (<ULP>) {
-    chop;
-    # ignore comments and empty lines
-    next if /^#/;
-    next if /^\s*$/;
-    if (/^Function: /) {
-      if (/Real part of/) {
-	s/Real part of //;
-	$type = 'real';
-      } elsif (/Imaginary part of/) {
-	s/Imaginary part of //;
-	$type = 'imag';
-      } else {
-	$type = 'normal';
-      }
-      ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
-      next;
-    }
-    if (/$float_regex/) {
-      ($float, $eps) = split /\s*:\s*/,$_,2;
-
-      if ($eps eq "0") {
-	# ignore
-	next;
-      } else {
-	if (!defined ($results{$test}{$type}{'ulp'}{$float})
-	    || $results{$test}{$type}{'ulp'}{$float} < $eps) {
-	  $results{$test}{$type}{'ulp'}{$float} = $eps;
-	  $results{$test}{'has_ulps'} = 1;
-	}
-      }
-      if ($type =~ /^real|imag$/) {
-	$results{$test}{'type'} = 'complex';
-      } elsif ($type eq 'normal') {
-	$results{$test}{'type'} = 'normal';
-      }
-      next;
-    }
-    print "Skipping unknown entry: `$_'\n";
-  }
-  close ULP;
-}
-
-
-# Clean up a floating point number
-sub clean_up_number {
-  my ($number) = @_;
-
-  # Remove trailing zeros after the decimal point
-  if ($number =~ /\./) {
-    $number =~ s/0+$//;
-    $number =~ s/\.$//;
-  }
-  return $number;
-}
-
-# Output a file which can be read in as ulps file.
-sub print_ulps_file {
-  my ($file) = @_;
-  my ($test, $type, $float, $eps, $fct, $last_fct);
-
-  $last_fct = '';
-  open NEWULP, ">$file" or die ("Can't open $file: $!");
-  print NEWULP "# Begin of automatic generation\n";
-  print NEWULP "\n# Maximal error of functions:\n";
-
-  foreach $fct (sort keys %results) {
-    foreach $type ('real', 'imag', 'normal') {
-      if (exists $results{$fct}{$type}) {
-	if ($type eq 'normal') {
-	  print NEWULP "Function: \"$fct\":\n";
-	} elsif ($type eq 'real') {
-	  print NEWULP "Function: Real part of \"$fct\":\n";
-	} elsif ($type eq 'imag') {
-	  print NEWULP "Function: Imaginary part of \"$fct\":\n";
-	}
-	foreach $float (@all_floats) {
-	  if (exists $results{$fct}{$type}{'ulp'}{$float}) {
-	    print NEWULP "$float: ",
-	    &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
-	    "\n";
-	  }
-	}
-	print NEWULP "\n";
-      }
-    }
-  }
-  print NEWULP "# end of automatic generation\n";
-  close NEWULP;
-}
-
-sub get_ulps {
-  my ($test, $type, $float) = @_;
-
-  return (exists $results{$test}{$type}{'ulp'}{$float}
-	  ? $results{$test}{$type}{'ulp'}{$float} : "0");
-}
-
-# Return the ulps value for a single test.
-sub get_all_ulps_for_test {
-  my ($test, $type) = @_;
-  my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
-  my ($ulps_str);
-
-  if (exists $results{$test}{'has_ulps'}) {
-    foreach $float (@all_floats) {
-      $ulps_str .= &get_ulps ($test, $type, $float) . ", ";
-    }
-    return "{" . substr ($ulps_str, 0, -2) . "}";
-  } else {
-    die "get_all_ulps_for_test called for \"$test\" with no ulps\n";
-  }
-}
-
-# Print include file
-sub output_ulps {
-  my ($file, $ulps_filename) = @_;
-  my ($i, $fct, $type, $ulp, $ulp_real, $ulp_imag);
-  my (%func_ulps, %func_real_ulps, %func_imag_ulps);
-
-  open ULP, ">$file" or die ("Can't open $file: $!");
-
-  print ULP "/* This file is automatically generated\n";
-  print ULP "   from $ulps_filename with gen-libm-test.pl.\n";
-  print ULP "   Don't change it - change instead the master files.  */\n\n";
-
-  print ULP "struct ulp_data\n";
-  print ULP "{\n";
-  print ULP "  const char *name;\n";
-  print ULP "  FLOAT max_ulp[" . @all_floats . "];\n";
-  print ULP "};\n\n";
-
-  for ($i = 0; $i <= $#all_floats; $i++) {
-    $type = $all_floats[$i];
-    print ULP "#define ULP_";
-    if ($type =~ /^i/) {
-      print ULP "I_";
-      $type = substr $type, 1;
-    }
-    print ULP "$all_floats_pfx{$type} $i\n";
-  }
-
-  foreach $fct (keys %results) {
-    $type = $results{$fct}{'type'};
-    if ($type eq 'normal') {
-      $ulp = get_all_ulps_for_test ($fct, 'normal');
-    } elsif ($type eq 'complex') {
-      $ulp_real = get_all_ulps_for_test ($fct, 'real');
-      $ulp_imag = get_all_ulps_for_test ($fct, 'imag');
-    } else {
-      die "unknown results ($fct) type $type\n";
-    }
-    if ($type eq 'normal') {
-      $func_ulps{$fct} = $ulp;
-    } else {
-      $func_real_ulps{$fct} = $ulp_real;
-      $func_imag_ulps{$fct} = $ulp_imag;
-    }
-  }
-  print ULP "\n/* Maximal error of functions.  */\n";
-  print ULP "static const struct ulp_data func_ulps[] =\n  {\n";
-  foreach $fct (sort keys %func_ulps) {
-    print ULP "    { \"$fct\", $func_ulps{$fct} },\n";
-  }
-  print ULP "  };\n";
-  print ULP "static const struct ulp_data func_real_ulps[] =\n  {\n";
-  foreach $fct (sort keys %func_real_ulps) {
-    print ULP "    { \"$fct\", $func_real_ulps{$fct} },\n";
-  }
-  print ULP "  };\n";
-  print ULP "static const struct ulp_data func_imag_ulps[] =\n  {\n";
-  foreach $fct (sort keys %func_imag_ulps) {
-    print ULP "    { \"$fct\", $func_imag_ulps{$fct} },\n";
-  }
-  print ULP "  };\n";
-  close ULP;
-}
-
-# Parse auto-libm-test-out.
-sub parse_auto_input {
-  my ($file) = @_;
-  open AUTO, $file or die ("Can't open $file: $!");
-  while (<AUTO>) {
-    chop;
-    next if !/^= /;
-    s/^= //;
-    if (/^(\S+) (\S+) ([^: ][^ ]* [^:]*) : (.*)$/) {
-      $auto_tests{$1}{$2}{$3} = $4;
-    } else {
-      die ("bad automatic test line: $_\n");
-    }
-  }
-  close AUTO;
-}
diff --git a/math/gen-libm-test.py b/math/gen-libm-test.py
new file mode 100755
index 0000000..8d156ea
--- /dev/null
+++ b/math/gen-libm-test.py
@@ -0,0 +1,603 @@
+#!/usr/bin/python
+# Generate tests for libm functions.
+# Copyright (C) 2018 Free Software Foundation, Inc.
+# This file is part of the GNU C Library.
+#
+# The GNU C 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 2.1 of the License, or (at your option) any later version.
+#
+# The GNU C 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 C Library; if not, see
+# <http://www.gnu.org/licenses/>.
+
+import argparse
+from collections import defaultdict
+import re
+
+
+# Sorted list of all float types in ulps files.
+ALL_FLOATS = ('double', 'float', 'float128', 'idouble',
+              'ifloat', 'ifloat128', 'ildouble', 'ldouble')
+
+# Map float types in ulps files to C-like prefix for macros.
+ALL_FLOATS_PFX = {'double': 'DBL',
+                  'ldouble': 'LDBL',
+                  'float': 'FLT',
+                  'float128': 'FLT128'}
+
+# Number of arguments in structure (as opposed to arguments that are
+# pointers to return values) for an argument descriptor.
+DESCR_NUM_ARGS = {'f': 1, 'a': 1, 'j': 1, 'i': 1, 'u': 1, 'l': 1, 'L': 1,
+                  'p': 0, 'F': 0, 'I': 0,
+                  'c': 2}
+
+# Number of results in structure for a result descriptor.
+DESCR_NUM_RES = {'f': 1, 'i': 1, 'l': 1, 'L': 1, 'M': 1, 'U': 1, 'b': 1,
+                 '1': 1,
+                 'c': 2}
+
+# Rounding modes, in the form in which they appear in
+# auto-libm-test-out-* and the order in which expected results appear
+# in structures and TEST_* calls.
+ROUNDING_MODES = ('downward', 'tonearest', 'towardzero', 'upward')
+
+# Map from special text in TEST_* calls for rounding-mode-specific
+# results and flags, to those results for each mode.
+ROUNDING_MAP = {
+    'plus_oflow': ('max_value', 'plus_infty', 'max_value', 'plus_infty'),
+    'minus_oflow': ('minus_infty', 'minus_infty', '-max_value', '-max_value'),
+    'plus_uflow': ('plus_zero', 'plus_zero', 'plus_zero', 'min_subnorm_value'),
+    'minus_uflow': ('-min_subnorm_value', 'minus_zero', 'minus_zero',
+                    'minus_zero'),
+    'ERRNO_PLUS_OFLOW': ('0', 'ERRNO_ERANGE', '0', 'ERRNO_ERANGE'),
+    'ERRNO_MINUS_OFLOW': ('ERRNO_ERANGE', 'ERRNO_ERANGE', '0', '0'),
+    'ERRNO_PLUS_UFLOW': ('ERRNO_ERANGE', 'ERRNO_ERANGE', 'ERRNO_ERANGE', '0'),
+    'ERRNO_MINUS_UFLOW': ('0', 'ERRNO_ERANGE', 'ERRNO_ERANGE', 'ERRNO_ERANGE'),
+    'XFAIL_ROUNDING_IBM128_LIBGCC': ('XFAIL_IBM128_LIBGCC', '0',
+                                     'XFAIL_IBM128_LIBGCC',
+                                     'XFAIL_IBM128_LIBGCC')
+    }
+
+# Map from raw test arguments to a nicer form to use when displaying
+# test results.
+BEAUTIFY_MAP = {'minus_zero': '-0',
+                'plus_zero': '+0',
+                '-0x0p+0f': '-0',
+                '-0x0p+0': '-0',
+                '-0x0p+0L': '-0',
+                '0x0p+0f': '+0',
+                '0x0p+0': '+0',
+                '0x0p+0L': '+0',
+                'minus_infty': '-inf',
+                'plus_infty': 'inf',
+                'qnan_value': 'qNaN',
+                'snan_value': 'sNaN',
+                'snan_value_ld': 'sNaN'}
+
+# Flags in auto-libm-test-out that map directly to C flags.
+FLAGS_SIMPLE = {'ignore-zero-inf-sign': 'IGNORE_ZERO_INF_SIGN',
+                'no-test-inline': 'NO_TEST_INLINE',
+                'xfail': 'XFAIL_TEST'}
+
+# Exceptions in auto-libm-test-out, and their corresponding C flags
+# for being required, OK or required to be absent.
+EXC_EXPECTED = {'divbyzero': 'DIVBYZERO_EXCEPTION',
+                'inexact': 'INEXACT_EXCEPTION',
+                'invalid': 'INVALID_EXCEPTION',
+                'overflow': 'OVERFLOW_EXCEPTION',
+                'underflow': 'UNDERFLOW_EXCEPTION'}
+EXC_OK = {'divbyzero': 'DIVBYZERO_EXCEPTION_OK',
+          'inexact': '0',
+          'invalid': 'INVALID_EXCEPTION_OK',
+          'overflow': 'OVERFLOW_EXCEPTION_OK',
+          'underflow': 'UNDERFLOW_EXCEPTION_OK'}
+EXC_NO = {'divbyzero': '0',
+          'inexact': 'NO_INEXACT_EXCEPTION',
+          'invalid': '0',
+          'overflow': '0',
+          'underflow': '0'}
+
+
+class Ulps(object):
+    """Maximum expected errors of libm functions."""
+
+    def __init__(self):
+        """Initialize an Ulps object."""
+        # normal[function][float_type] is the ulps value, and likewise
+        # for real and imag.
+        self.normal = defaultdict(lambda: defaultdict(lambda: 0))
+        self.real = defaultdict(lambda: defaultdict(lambda: 0))
+        self.imag = defaultdict(lambda: defaultdict(lambda: 0))
+        # List of ulps kinds, in the order in which they appear in
+        # sorted ulps files.
+        self.ulps_kinds = (('Real part of ', self.real),
+                           ('Imaginary part of ', self.imag),
+                           ('', self.normal))
+        self
+
+    def read(self, ulps_file):
+        """Read ulps from a file into an Ulps object."""
+        self.ulps_file = ulps_file
+        with open(ulps_file, 'r') as f:
+            ulps_dict = None
+            ulps_fn = None
+            for line in f:
+                # Ignore comments.
+                if line.startswith('#'):
+                    continue
+                line = line.rstrip()
+                # Ignore empty lines.
+                if line == '':
+                    continue
+                m = re.match(r'([^:]*): (.*)\Z', line)
+                if not m:
+                    raise ValueError('bad ulps line: %s' % line)
+                line_first = m.group(1)
+                line_second = m.group(2)
+                if line_first == 'Function':
+                    fn = None
+                    ulps_dict = None
+                    for k_prefix, k_dict in self.ulps_kinds:
+                        if line_second.startswith(k_prefix):
+                            ulps_dict = k_dict
+                            fn = line_second[len(k_prefix):]
+                            break
+                    if not fn.startswith('"') or not fn.endswith('":'):
+                        raise ValueError('bad ulps line: %s' % line)
+                    ulps_fn = fn[1:-2]
+                else:
+                    if line_first not in ALL_FLOATS:
+                        raise ValueError('bad ulps line: %s' % line)
+                    ulps_val = int(line_second)
+                    if ulps_val > 0:
+                        ulps_dict[ulps_fn][line_first] = max(
+                            ulps_dict[ulps_fn][line_first],
+                            ulps_val)
+
+    def write(self, ulps_file):
+        """Write ulps back out as a sorted ulps file."""
+        # Output is sorted first by function name, then by (real,
+        # imag, normal), then by float type.
+        out_data = {}
+        for order, (prefix, d) in enumerate(self.ulps_kinds):
+            for fn in d.keys():
+                fn_data = ['%s: %d' % (f, d[fn][f])
+                           for f in sorted(d[fn].keys())]
+                fn_text = 'Function: %s"%s":\n%s' % (prefix, fn,
+                                                     '\n'.join(fn_data))
+                out_data[(fn, order)] = fn_text
+        out_list = [out_data[fn_order] for fn_order in sorted(out_data.keys())]
+        out_text = ('# Begin of automatic generation\n\n'
+                    '# Maximal error of functions:\n'
+                    '%s\n\n'
+                    '# end of automatic generation\n'
+                    % '\n\n'.join(out_list))
+        with open(ulps_file, 'w') as f:
+            f.write(out_text)
+
+    @staticmethod
+    def ulps_table(name, ulps_dict):
+        """Return text of a C table of ulps."""
+        ulps_list = []
+        for fn in sorted(ulps_dict.keys()):
+            fn_ulps = [str(ulps_dict[fn][f]) for f in ALL_FLOATS]
+            ulps_list.append('    { "%s", {%s} },' % (fn, ', '.join(fn_ulps)))
+        ulps_text = ('static const struct ulp_data %s[] =\n'
+                     '  {\n'
+                     '%s\n'
+                     '  };'
+                     % (name, '\n'.join(ulps_list)))
+        return ulps_text
+
+    def write_header(self, ulps_header):
+        """Write header file with ulps data."""
+        header_text_1 = ('/* This file is automatically generated\n'
+                         '   from %s with gen-libm-test.py.\n'
+                         '   Don\'t change it - change instead the master '
+                         'files.  */\n\n'
+                         'struct ulp_data\n'
+                         '{\n'
+                         '  const char *name;\n'
+                         '  FLOAT max_ulp[%d];\n'
+                         '};'
+                         % (self.ulps_file, len(ALL_FLOATS)))
+        macro_list = []
+        for i, f in enumerate(ALL_FLOATS):
+            if f.startswith('i'):
+                itxt = 'I_'
+                f = f[1:]
+            else:
+                itxt = ''
+            macro_list.append('#define ULP_%s%s %d'
+                              % (itxt, ALL_FLOATS_PFX[f], i))
+        header_text = ('%s\n\n'
+                       '%s\n\n'
+                       '/* Maximal error of functions.  */\n'
+                       '%s\n'
+                       '%s\n'
+                       '%s\n'
+                       % (header_text_1, '\n'.join(macro_list),
+                          self.ulps_table('func_ulps', self.normal),
+                          self.ulps_table('func_real_ulps', self.real),
+                          self.ulps_table('func_imag_ulps', self.imag)))
+        with open(ulps_header, 'w') as f:
+            f.write(header_text)
+
+
+def read_auto_tests(test_file):
+    """Read tests from auto-libm-test-out-<function> (possibly None)."""
+    auto_tests = defaultdict(lambda: defaultdict(dict))
+    if test_file is None:
+        return auto_tests
+    with open(test_file, 'r') as f:
+        for line in f:
+            if not line.startswith('= '):
+                continue
+            line = line[len('= '):].rstrip()
+            # Function, rounding mode, condition and inputs, outputs
+            # and flags.
+            m = re.match(r'([^ ]+) ([^ ]+) ([^: ][^ ]* [^:]*) : (.*)\Z', line)
+            if not m:
+                raise ValueError('bad automatic test line: %s' % line)
+            auto_tests[m.group(1)][m.group(2)][m.group(3)] = m.group(4)
+    return auto_tests
+
+
+def beautify(arg):
+    """Return a nicer representation of a test argument."""
+    if arg in BEAUTIFY_MAP:
+        return BEAUTIFY_MAP[arg]
+    if arg.startswith('-') and arg[1:] in BEAUTIFY_MAP:
+        return '-' + BEAUTIFY_MAP[arg[1:]]
+    if re.match(r'-?0x[0-9a-f.]*p[-+][0-9]+f\Z', arg):
+        return arg[:-1]
+    if re.search(r'[0-9]L\Z', arg):
+        return arg[:-1]
+    return arg
+
+
+def complex_beautify(arg_real, arg_imag):
+    """Return a nicer representation of a complex test argument."""
+    res_real = beautify(arg_real)
+    res_imag = beautify(arg_imag)
+    if res_imag.startswith('-'):
+        return '%s - %s i' % (res_real, res_imag[1:])
+    else:
+        return '%s + %s i' % (res_real, res_imag)
+
+
+def apply_lit_token(arg, macro):
+    """Apply the LIT or ARG_LIT macro to a single token."""
+    # The macro must only be applied to a floating-point constant, not
+    # to an integer constant or lit_* value.
+    sign_re = r'[+-]?'
+    exp_re = r'([+-])?[0-9]+'
+    suffix_re = r'[lLfF]?'
+    dec_exp_re = r'[eE]' + exp_re
+    hex_exp_re = r'[pP]' + exp_re
+    dec_frac_re = r'(?:[0-9]*\.[0-9]+|[0-9]+\.)'
+    hex_frac_re = r'(?:[0-9a-fA-F]*\.[0-9a-fA-F]+|[0-9a-fA-F]+\.)'
+    dec_int_re = r'[0-9]+'
+    hex_int_re = r'[0-9a-fA-F]+'
+    dec_cst_re = r'(?:%s(?:%s)?|%s%s)' % (dec_frac_re, dec_exp_re,
+                                          dec_int_re, dec_exp_re)
+    hex_cst_re = r'0[xX](?:%s|%s)%s' % (hex_frac_re, hex_int_re, hex_exp_re)
+    fp_cst_re = r'(%s(?:%s|%s))%s\Z' % (sign_re, dec_cst_re, hex_cst_re,
+                                        suffix_re)
+    m = re.match(fp_cst_re, arg)
+    if m:
+        return '%s (%s)' % (macro, m.group(1))
+    else:
+        return arg
+
+
+def apply_lit(arg, macro):
+    """Apply the LIT or ARG_LIT macro to constants within an expression."""
+    # Assume expressions follow the GNU Coding Standards, with tokens
+    # separated by spaces.
+    return ' '.join([apply_lit_token(t, macro) for t in arg.split()])
+
+
+def gen_test_args_res(descr_args, descr_res, args, res_rm):
+    """Generate a test given the arguments and per-rounding-mode results."""
+    # Determine whether any arguments or results, for any rounding
+    # mode, are non-finite.  (For consistency with the old perl
+    # script, this does not handle infinities resulting from
+    # ROUNDING_MAP.)
+    non_finite = False
+    test_snan = False
+    all_args_res = list(args)
+    for r in res_rm:
+        all_args_res.extend(r[:len(r)-1])
+    for a in all_args_res:
+        if 'snan_value' in a:
+            test_snan = True
+            non_finite = True
+        elif 'qnan_value' in a or 'plus_infty' in a or 'minus_infty' in a:
+            non_finite = True
+    # Process the arguments.
+    args_disp = []
+    args_c = []
+    arg_pos = 0
+    for d in descr_args:
+        if DESCR_NUM_ARGS[d] == 0:
+            continue
+        if d == 'c':
+            args_disp.append(complex_beautify(args[arg_pos],
+                                              args[arg_pos + 1]))
+            args_c.append(apply_lit(args[arg_pos], 'LIT'))
+            args_c.append(apply_lit(args[arg_pos + 1], 'LIT'))
+        else:
+            args_disp.append(beautify(args[arg_pos]))
+            if d == 'f':
+                args_c.append(apply_lit(args[arg_pos], 'LIT'))
+            elif d == 'a':
+                args_c.append(apply_lit(args[arg_pos], 'ARG_LIT'))
+            else:
+                args_c.append(args[arg_pos])
+        arg_pos += DESCR_NUM_ARGS[d]
+    args_disp_text = ', '.join(args_disp).replace('"', '\\"')
+    # Process the results.
+    for rm in range(len(ROUNDING_MODES)):
+        res = res_rm[rm]
+        res_pos = 0
+        rm_args = []
+        ignore_result_any = False
+        ignore_result_all = True
+        special = []
+        for d in descr_res:
+            if d == '1':
+                special.append(res[res_pos])
+            elif DESCR_NUM_RES[d] == 1:
+                result = res[res_pos]
+                if result == 'IGNORE':
+                    ignore_result_any = True
+                    result = '0'
+                else:
+                    ignore_result_all = False
+                    if d == 'f':
+                        result = apply_lit(result, 'LIT')
+                rm_args.append(result)
+            else:
+                # Complex result.
+                result1 = res[res_pos]
+                if result1 == 'IGNORE':
+                    ignore_result_any = True
+                    result1 = '0'
+                else:
+                    ignore_result_all = False
+                    result1 = apply_lit(result1, 'LIT')
+                rm_args.append(result1)
+                result2 = res[res_pos + 1]
+                if result2 == 'IGNORE':
+                    ignore_result_any = True
+                    result2 = '0'
+                else:
+                    ignore_result_all = False
+                    result2 = apply_lit(result2, 'LIT')
+                rm_args.append(result2)
+            res_pos += DESCR_NUM_RES[d]
+        if ignore_result_any and not ignore_result_all:
+            raise ValueError('some but not all function results ignored')
+        flags = []
+        if ignore_result_any:
+            flags.append('IGNORE_RESULT')
+        if non_finite:
+            flags.append('NON_FINITE')
+        if test_snan:
+            flags.append('TEST_SNAN')
+        flags.append(res[res_pos])
+        rm_args.append('|'.join(flags))
+        for sp in special:
+            if sp == 'IGNORE':
+                rm_args.extend(['0', '0'])
+            else:
+                rm_args.extend(['1', apply_lit(sp, 'LIT')])
+        for k in sorted(ROUNDING_MAP.keys()):
+            rm_args = [arg.replace(k, ROUNDING_MAP[k][rm]) for arg in rm_args]
+        args_c.append('{ %s }' % ', '.join(rm_args))
+    return '    { "%s", %s },\n' % (args_disp_text, ', '.join(args_c))
+
+
+def convert_condition(cond):
+    """Convert a condition from auto-libm-test-out to C form."""
+    conds = cond.split(':')
+    conds_c = []
+    for c in conds:
+        if not c.startswith('arg_fmt('):
+            c = c.replace('-', '_')
+        conds_c.append('TEST_COND_' + c)
+    return '(%s)' % ' && '.join(conds_c)
+
+
+def cond_value(cond, if_val, else_val):
+    """Return a C conditional expression between two values."""
+    if cond == '1':
+        return if_val
+    elif cond == '0':
+        return else_val
+    else:
+        return '(%s ? %s : %s)' % (cond, if_val, else_val)
+
+
+def gen_auto_tests(auto_tests, descr_args, descr_res, fn):
+    """Generate C code for the auto-libm-test-out-* tests for a function."""
+    for rm_idx, rm_name in enumerate(ROUNDING_MODES):
+        this_tests = sorted(auto_tests[fn][rm_name].keys())
+        if rm_idx == 0:
+            rm_tests = this_tests
+            if not rm_tests:
+                raise ValueError('no automatic tests for %s' % fn)
+        else:
+            if rm_tests != this_tests:
+                raise ValueError('inconsistent lists of tests of %s' % fn)
+    test_list = []
+    for test in rm_tests:
+        fmt_args = test.split()
+        fmt = fmt_args[0]
+        args = fmt_args[1:]
+        test_list.append('#if %s\n' % convert_condition(fmt))
+        res_rm = []
+        for rm in ROUNDING_MODES:
+            test_out = auto_tests[fn][rm][test]
+            out_str, flags_str = test_out.split(':', 1)
+            this_res = out_str.split()
+            flags = flags_str.split()
+            flag_cond = {}
+            for flag in flags:
+                m = re.match(r'([^:]*):(.*)\Z', flag)
+                if m:
+                    f_name = m.group(1)
+                    cond = convert_condition(m.group(2))
+                    if f_name in flag_cond:
+                        if flag_cond[f_name] != '1':
+                            flag_cond[f_name] = ('%s || %s'
+                                                 % (flag_cond[f_name], cond))
+                    else:
+                        flag_cond[f_name] = cond
+                else:
+                    flag_cond[flag] = '1'
+            flags_c = []
+            for flag in sorted(FLAGS_SIMPLE.keys()):
+                if flag in flag_cond:
+                    flags_c.append(cond_value(flag_cond[flag],
+                                              FLAGS_SIMPLE[flag], '0'))
+            for exc in sorted(EXC_EXPECTED.keys()):
+                exc_expected = EXC_EXPECTED[exc]
+                exc_ok = EXC_OK[exc]
+                no_exc = EXC_NO[exc]
+                exc_cond = flag_cond.get(exc, '0')
+                exc_ok_cond = flag_cond.get(exc + '-ok', '0')
+                flags_c.append(cond_value(exc_cond,
+                                          cond_value(exc_ok_cond, exc_ok,
+                                                     exc_expected),
+                                          cond_value(exc_ok_cond, exc_ok,
+                                                     no_exc)))
+            if 'errno-edom' in flag_cond and 'errno-erange' in flag_cond:
+                raise ValueError('multiple errno values expected')
+            if 'errno-edom' in flag_cond:
+                if flag_cond['errno-edom'] != '1':
+                    raise ValueError('unexpected condition for errno-edom')
+                errno_expected = 'ERRNO_EDOM'
+            elif 'errno-erange' in flag_cond:
+                if flag_cond['errno-erange'] != '1':
+                    raise ValueError('unexpected condition for errno-erange')
+                errno_expected = 'ERRNO_ERANGE'
+            else:
+                errno_expected = 'ERRNO_UNCHANGED'
+            if 'errno-edom-ok' in flag_cond:
+                if ('errno-erange-ok' in flag_cond
+                    and (flag_cond['errno-erange-ok']
+                         != flag_cond['errno-edom-ok'])):
+                    errno_unknown_cond = ('%s || %s'
+                                          % (flag_cond['errno-edom-ok'],
+                                             flag_cond['errno-erange-ok']))
+                else:
+                    errno_unknown_cond = flag_cond['errno-edom-ok']
+            else:
+                errno_unknown_cond = flag_cond.get('errno-erange-ok', '0')
+            flags_c.append(cond_value(errno_unknown_cond, '0', errno_expected))
+            flags_c = [flag for flag in flags_c if flag != '0']
+            if not flags_c:
+                flags_c = ['NO_EXCEPTION']
+            this_res.append(' | '.join(flags_c))
+            res_rm.append(this_res)
+        test_list.append(gen_test_args_res(descr_args, descr_res, args,
+                                           res_rm))
+        test_list.append('#endif\n')
+    return ''.join(test_list)
+
+
+def gen_test_line(descr_args, descr_res, args_str):
+    """Generate C code for the tests for a single TEST_* line."""
+    test_args = args_str.split(',')
+    test_args = test_args[1:]
+    test_args = [a.strip() for a in test_args]
+    num_args = sum([DESCR_NUM_ARGS[c] for c in descr_args])
+    num_res = sum([DESCR_NUM_RES[c] for c in descr_res])
+    args = test_args[:num_args]
+    res = test_args[num_args:]
+    if len(res) == num_res:
+        # One set of results for all rounding modes, no flags.
+        res.append('0')
+        res_rm = [res, res, res, res]
+    elif len(res) == num_res + 1:
+        # One set of results for all rounding modes, with flags.
+        if not ('EXCEPTION' in res[-1]
+                or 'ERRNO' in res[-1]
+                or 'IGNORE_ZERO_INF_SIGN' in res[-1]
+                or 'TEST_NAN_SIGN' in res[-1]
+                or 'NO_TEST_INLINE' in res[-1]
+                or 'XFAIL' in res[-1]):
+            raise ValueError('wrong number of arguments: %s' % args_str)
+        res_rm = [res, res, res, res]
+    elif len(res) == (num_res + 1) * 4:
+        # One set of results per rounding mode, with flags.
+        nr_plus = num_res + 1
+        res_rm = [res[:nr_plus], res[nr_plus:2*nr_plus],
+                  res[2*nr_plus:3*nr_plus], res[3*nr_plus:]]
+    return gen_test_args_res(descr_args, descr_res, args, res_rm)
+
+
+def generate_testfile(inc_input, auto_tests, c_output):
+    """Generate test .c file from .inc input."""
+    test_list = []
+    with open(inc_input, 'r') as f:
+        for line in f:
+            line_strip = line.strip()
+            if line_strip.startswith('AUTO_TESTS_'):
+                m = re.match(r'AUTO_TESTS_([^_]*)_([^_ ]*) *\(([^)]*)\),\Z',
+                             line_strip)
+                if not m:
+                    raise ValueError('bad AUTO_TESTS line: %s' % line)
+                test_list.append(gen_auto_tests(auto_tests, m.group(1),
+                                                m.group(2), m.group(3)))
+            elif line_strip.startswith('TEST_'):
+                m = re.match(r'TEST_([^_]*)_([^_ ]*) *\((.*)\),\Z', line_strip)
+                if not m:
+                    raise ValueError('bad TEST line: %s' % line)
+                test_list.append(gen_test_line(m.group(1), m.group(2),
+                                               m.group(3)))
+            else:
+                test_list.append(line)
+    with open(c_output, 'w') as f:
+        f.write(''.join(test_list))
+
+
+def main():
+    """The main entry point."""
+    parser = argparse.ArgumentParser(description='Generate libm tests.')
+    parser.add_argument('-a', dest='auto_input', metavar='FILE',
+                        help='input file with automatically generated tests')
+    parser.add_argument('-c', dest='inc_input', metavar='FILE',
+                        help='input file .inc file with tests')
+    parser.add_argument('-u', dest='ulps_file', metavar='FILE',
+                        help='input file with ulps')
+    parser.add_argument('-n', dest='ulps_output', metavar='FILE',
+                        help='generate sorted ulps file FILE')
+    parser.add_argument('-C', dest='c_output', metavar='FILE',
+                        help='generate output C file FILE from .inc file')
+    parser.add_argument('-H', dest='ulps_header', metavar='FILE',
+                        help='generate output ulps header FILE')
+    args = parser.parse_args()
+    ulps = Ulps()
+    if args.ulps_file is not None:
+        ulps.read(args.ulps_file)
+    auto_tests = read_auto_tests(args.auto_input)
+    if args.ulps_output is not None:
+        ulps.write(args.ulps_output)
+    if args.ulps_header is not None:
+        ulps.write_header(args.ulps_header)
+    if args.c_output is not None:
+        generate_testfile(args.inc_input, auto_tests, args.c_output)
+
+
+if __name__ == '__main__':
+    main()
diff --git a/math/libm-test-driver.c b/math/libm-test-driver.c
index 30646da..2bbfdef 100644
--- a/math/libm-test-driver.c
+++ b/math/libm-test-driver.c
@@ -154,7 +154,7 @@ struct test_ff_f_data
     int exceptions;
   } rd, rn, rz, ru;
 };
-/* Strictly speaking, a j type argument is one gen-libm-test.pl will not
+/* Strictly speaking, a j type argument is one gen-libm-test.py will not
    attempt to muck with.  For now, it is only used to prevent it from
    mucking up an explicitly long double argument.  */
 struct test_fj_f_data
diff --git a/math/libm-test-nexttoward.inc b/math/libm-test-nexttoward.inc
index edd69a2..d7e5f58 100644
--- a/math/libm-test-nexttoward.inc
+++ b/math/libm-test-nexttoward.inc
@@ -20,7 +20,7 @@
 
 /* Note, the second argument is always typed as long double.  The j type
    argument to TEST_ indicates the literal should not be post-processed by
-   gen-libm-test.pl.  */
+   gen-libm-test.py.  */
 
 static const struct test_fj_f_data nexttoward_test_data[] =
   {
diff --git a/math/libm-test-support.c b/math/libm-test-support.c
index 1a12ed6..cb78daa 100644
--- a/math/libm-test-support.c
+++ b/math/libm-test-support.c
@@ -25,7 +25,7 @@
    different types and for variants such as testing inline functions.
 
    The tests of individual functions are in .inc files processed by
-   gen-libm-test.pl, with the resulting files included together with
+   gen-libm-test.py, with the resulting files included together with
    libm-test-driver.c.
 
    The per-type headers included both before libm-test-support.c and
diff --git a/math/libm-test-support.h b/math/libm-test-support.h
index 4263774..194da49 100644
--- a/math/libm-test-support.h
+++ b/math/libm-test-support.h
@@ -67,7 +67,7 @@ extern const char doc[];
 #define ERRNO_UNCHANGED			0x8000
 #define ERRNO_EDOM			0x10000
 #define ERRNO_ERANGE			0x20000
-/* Flags generated by gen-libm-test.pl, not entered here manually.  */
+/* Flags generated by gen-libm-test.py, not entered here manually.  */
 #define IGNORE_RESULT			0x40000
 #define NON_FINITE			0x80000
 #define TEST_SNAN			0x100000
@@ -125,7 +125,7 @@ extern const char doc[];
 #endif
 
 /* Mark a test as expected to fail for ibm128-libgcc.  This is used
-   via XFAIL_ROUNDING_IBM128_LIBGCC, which gen-libm-test.pl transforms
+   via XFAIL_ROUNDING_IBM128_LIBGCC, which gen-libm-test.py transforms
    appropriately for each rounding mode.  */
 #define XFAIL_IBM128_LIBGCC (TEST_COND_ibm128_libgcc ? XFAIL_TEST : 0)
 
diff --git a/sysdeps/generic/libm-test-ulps b/sysdeps/generic/libm-test-ulps
index 7cfa1f8..c96d364 100644
--- a/sysdeps/generic/libm-test-ulps
+++ b/sysdeps/generic/libm-test-ulps
@@ -2,4 +2,4 @@
 # This file is the fallback and contains
 # no data
 # You can create a new file with e.g. `test-double -u'
-# followed by `gen-libm-test.pl -u ULPs -n'.
+# followed by `gen-libm-test.py -u ULPs -n'.
