2 # Copyright (C) 1999-2014 Free Software Foundation, Inc.
3 # This file is part of the GNU C Library.
4 # Contributed by Andreas Jaeger <aj@suse.de>, 1999.
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 # This file needs to be tidied up
21 # Note that functions and tests share the same namespace.
23 # Information about tests are stored in: %results
24 # $results{$test}{"kind"} is either "fct" or "test" and flags whether this
25 # is a maximal error of a function or a single test.
26 # $results{$test}{"type"} is the result type, e.g. normal or complex.
27 # $results{$test}{"has_ulps"} is set if deltas exist.
28 # In the following description $type and $float are:
29 # - $type is either "normal", "real" (for the real part of a complex number)
30 # or "imag" (for the imaginary part # of a complex number).
31 # - $float is either of float, ifloat, double, idouble, ldouble, ildouble;
32 # It represents the underlying floating point type (float, double or long
33 # double) and if inline functions (the leading i stands for inline)
35 # $results{$test}{$type}{"ulp"}{$float} is defined and has a delta as value
42 use vars qw
($input $output $auto_input);
43 use vars qw
(%results);
44 use vars qw
(%beautify @all_floats);
45 use vars qw
($output_dir $ulps_file $srcdir);
46 use vars qw
(%auto_tests);
48 # all_floats is sorted and contains all recognised float types
49 @all_floats = ('double', 'float', 'idouble',
50 'ifloat', 'ildouble', 'ldouble');
53 ( "minus_zero" => "-0",
61 "minus_infty" => "-inf",
62 "plus_infty" => "inf",
63 "qnan_value" => "qNaN",
72 # n: generate new ulps file
73 use vars
qw($opt_u $opt_h $opt_o $opt_n);
76 $ulps_file = 'libm-test-ulps';
78 ($srcdir = $0) =~ s{[^/]*$}{};
81 print "Usage: gen-libm-test.pl [OPTIONS]\n";
82 print " -h print this help, then exit\n";
83 print " -o DIR directory where generated files will be placed\n";
84 print " -n only generate sorted file NewUlps from libm-test-ulps\n";
85 print " -u FILE input file with ulps\n";
89 $ulps_file = $opt_u if ($opt_u);
90 $output_dir = $opt_o if ($opt_o);
92 $input = "libm-test.inc";
93 $auto_input = "${srcdir}auto-libm-test-out";
94 $output = "${output_dir}libm-test.c";
96 &parse_ulps ($ulps_file);
97 &parse_auto_input ($auto_input);
98 &generate_testfile ($input, $output) unless ($opt_n);
99 &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
100 &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
102 # Return a nicer representation
107 if (exists $beautify{$arg}) {
108 return $beautify{$arg};
113 if (exists $beautify{$tmp}) {
114 return '-' . $beautify{$tmp};
117 if ($arg =~ /^-?0x[0-9a-f.]*p[-+][0-9]+f$/) {
120 if ($arg =~ /[0-9]L$/) {
126 # Return a nicer representation of a complex number
127 sub build_complex_beautify {
131 $str1 = &beautify ($r);
132 $str2 = &beautify ($i);
135 $str1 .= ' - ' . $str2;
137 $str1 .= ' + ' . $str2;
143 # Return the text to put in an initializer for a test's exception
145 sub show_exceptions {
146 my ($ignore_result, $exception) = @_;
147 $ignore_result = ($ignore_result ? "IGNORE_RESULT|" : "");
148 if (defined $exception) {
149 return ", ${ignore_result}$exception";
151 return ", ${ignore_result}0";
155 # Parse the arguments to TEST_x_y
157 my ($file, $descr, $args) = @_;
158 my (@args, $descr_args, $descr_res, @descr);
159 my ($current_arg, $cline, $i);
162 my ($ignore_result_any, $ignore_result_all);
164 ($descr_args, $descr_res) = split /_/,$descr, 2;
166 @args = split /,\s*/, $args;
170 # Generate first the string that's shown to the user
172 @descr = split //,$descr_args;
173 for ($i = 0; $i <= $#descr; $i++) {
175 if ($current_arg > 1) {
178 # FLOAT, int, long int, long long int
179 if ($descr[$i] =~ /f|i|l|L/) {
180 $call_args .= $comma . &beautify ($args[$current_arg]);
184 # &FLOAT, &int - simplify call by not showing argument.
185 if ($descr[$i] =~ /F|I/) {
189 if ($descr[$i] eq 'c') {
190 $call_args .= $comma . &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
195 die ("$descr[$i] is unknown");
199 @descr = split //,$descr_res;
201 if ($_ =~ /f|i|l|L/) {
203 } elsif ($_ eq 'c') {
205 } elsif ($_ eq 'b') {
208 } elsif ($_ eq '1') {
211 die ("$_ is unknown");
215 if ($current_arg == $#args) {
216 die ("wrong number of arguments")
217 unless ($args[$current_arg] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN|TEST_NAN_SIGN|NO_TEST_INLINE|XFAIL_TEST/);
218 } elsif ($current_arg < $#args) {
219 die ("wrong number of arguments");
220 } elsif ($current_arg > ($#args+1)) {
221 die ("wrong number of arguments");
225 # Put the C program line together
226 # Reset some variables to start again
228 $cline = "{ \"$call_args\"";
229 @descr = split //,$descr_args;
230 for ($i=0; $i <= $#descr; $i++) {
231 # FLOAT, int, long int, long long int
232 if ($descr[$i] =~ /f|i|l|L/) {
233 $cline .= ", $args[$current_arg]";
238 if ($descr[$i] =~ /F|I/) {
242 if ($descr[$i] eq 'c') {
243 $cline .= ", $args[$current_arg], $args[$current_arg+1]";
249 @descr = split //,$descr_res;
250 $ignore_result_any = 0;
251 $ignore_result_all = 1;
253 if ($_ =~ /b|f|i|l|L/ ) {
254 my ($result) = $args[$current_arg];
255 if ($result eq "IGNORE") {
256 $ignore_result_any = 1;
259 $ignore_result_all = 0;
261 $cline .= ", $result";
263 } elsif ($_ eq 'c') {
264 my ($result1) = $args[$current_arg];
265 if ($result1 eq "IGNORE") {
266 $ignore_result_any = 1;
269 $ignore_result_all = 0;
271 my ($result2) = $args[$current_arg + 1];
272 if ($result2 eq "IGNORE") {
273 $ignore_result_any = 1;
276 $ignore_result_all = 0;
278 $cline .= ", $result1, $result2";
280 } elsif ($_ eq '1') {
281 push @special, $args[$current_arg];
285 if ($ignore_result_any && !$ignore_result_all) {
286 die ("some but not all function results ignored\n");
289 $cline .= show_exceptions ($ignore_result_any,
290 ($current_arg <= $#args)
291 ? $args[$current_arg]
294 # special treatment for some functions
298 my ($extra_expected) = $_;
299 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
301 $extra_expected = "0";
303 $cline .= ", $run_extra, $extra_expected";
305 print $file " $cline },\n";
308 # Convert a condition from auto-libm-test-out to C form.
309 sub convert_condition {
312 @conds = split /:/, $cond;
317 $ret = join " && ", @conds;
321 # Return text to OR a value into an accumulated flags string.
331 # Return text to OR a conditional expression between two values into
332 # an accumulated flags string.
334 my ($cond, $if, $else) = @_;
336 return or_value ($if);
337 } elsif ($cond eq "0") {
338 return or_value ($else);
340 return or_value ("($cond ? $if : $else)");
344 # Generate libm-test.c
345 sub generate_testfile {
346 my ($input, $output) = @_;
348 open INPUT, $input or die ("Can't open $input: $!");
349 open OUTPUT, ">$output" or die ("Can't open $output: $!");
351 # Replace the special macros
353 # AUTO_TESTS (function, mode),
354 if (/^\s*AUTO_TESTS_/) {
355 my ($descr, $func, $mode, $auto_test, $num_auto_tests);
356 ($descr, $func, $mode) = ($_ =~ /AUTO_TESTS_(\w+)\s*\((\w+),\s*(\w+)\)/);
358 foreach $auto_test (sort keys %{$auto_tests{$func}{$mode}}) {
359 my ($finputs, $format, $inputs, $outputs, $flags);
360 my ($format_conv, $flags_conv, @flags, %flag_cond);
362 ($finputs, $outputs, $flags) = split / : */, $auto_test;
363 ($format, $inputs) = split / /, $finputs, 2;
365 $outputs =~ s/ /, /g;
366 $format_conv = convert_condition ($format);
367 print OUTPUT "#if $format_conv\n";
368 @flags = split / /, $flags;
370 if (/^([^:]*):(.*)$/) {
373 $cond = convert_condition ($2);
374 if (defined ($flag_cond{$flag})) {
375 if ($flag_cond{$flag} ne "1") {
376 $flag_cond{$flag} .= " || $cond";
379 $flag_cond{$flag} = $cond;
382 $flag_cond{$_} = "1";
386 if (defined ($flag_cond{"no-test-inline"})) {
387 $flags_conv .= or_cond_value ($flag_cond{"no-test-inline"},
388 "NO_TEST_INLINE", "0");
390 if (defined ($flag_cond{"xfail"})) {
391 $flags_conv .= or_cond_value ($flag_cond{"xfail"},
394 my (@exc_list) = qw(divbyzero inexact invalid overflow underflow);
396 foreach $exc (@exc_list) {
397 my ($exc_expected, $exc_ok, $no_exc);
398 $exc_expected = "\U$exc\E_EXCEPTION";
399 $exc_ok = "\U$exc\E_EXCEPTION_OK";
401 if ($exc eq "inexact") {
403 $no_exc = "NO_INEXACT_EXCEPTION";
405 if (defined ($flag_cond{$exc})) {
406 if ($flag_cond{$exc} ne "1") {
407 die ("unexpected condition for $exc\n");
409 if (defined ($flag_cond{"$exc-ok"})) {
410 $flags_conv .= or_cond_value
($flag_cond{"$exc-ok"},
411 $exc_ok, $exc_expected);
413 $flags_conv .= or_value
($exc_expected);
416 if (defined ($flag_cond{"$exc-ok"})) {
417 $flags_conv .= or_cond_value
($flag_cond{"$exc-ok"},
420 $flags_conv .= or_value
($no_exc);
424 my ($errno_expected, $errno_unknown_cond);
425 if (defined ($flag_cond{"errno-edom"})) {
426 if ($flag_cond{"errno-edom"} ne "1") {
427 die ("unexpected condition for errno-edom");
429 if (defined ($flag_cond{"errno-erange"})) {
430 die ("multiple errno values expected");
432 $errno_expected = "ERRNO_EDOM";
433 } elsif (defined ($flag_cond{"errno-erange"})) {
434 if ($flag_cond{"errno-erange"} ne "1") {
435 die ("unexpected condition for errno-erange");
437 $errno_expected = "ERRNO_ERANGE";
439 $errno_expected = "ERRNO_UNCHANGED";
441 if (defined ($flag_cond{"errno-edom-ok"})) {
442 if (defined ($flag_cond{"errno-erange-ok"})
443 && $flag_cond{"errno-erange-ok"} ne $flag_cond{"errno-edom-ok"}) {
444 $errno_unknown_cond = "($flag_cond{\"errno-edom-ok\"} || $flag_cond{\"errno-erange-ok\"})";
446 $errno_unknown_cond = $flag_cond{"errno-edom-ok"};
448 } elsif (defined ($flag_cond{"errno-erange-ok"})) {
449 $errno_unknown_cond = $flag_cond{"errno-erange-ok"};
451 $errno_unknown_cond = "0";
453 $flags_conv .= or_cond_value
($errno_unknown_cond,
454 "0", $errno_expected);
455 if ($flags_conv ne "") {
456 $flags_conv =~ s/^ \|/,/;
458 &parse_args
(\
*OUTPUT
, $descr,
459 "$func, $inputs, $outputs$flags_conv");
460 print OUTPUT
"#endif\n";
462 if ($num_auto_tests == 0) {
463 die ("no automatic tests for $func, $mode\n");
472 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
473 &parse_args
(\
*OUTPUT
, $descr, $args);
487 my ($test, $type, $float, $eps, $kind);
489 # $type has the following values:
490 # "normal": No complex variable
491 # "real": Real part of complex result
492 # "imag": Imaginary part of complex result
493 open ULP
, $file or die ("Can't open $file: $!");
496 # ignore comments and empty lines
500 if (/Real part of:/) {
503 } elsif (/Imaginary part of:/) {
504 s/Imaginary part of: //;
509 s/^.+\"(.*)\".*$/$1/;
515 if (/Real part of/) {
518 } elsif (/Imaginary part of/) {
519 s/Imaginary part of //;
524 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
528 if (/^i?(float|double|ldouble):/) {
529 ($float, $eps) = split /\s*:\s*/,$_,2;
535 $results{$test}{$type}{'ulp'}{$float} = $eps;
536 $results{$test}{'has_ulps'} = 1;
538 if ($type =~ /^real|imag$/) {
539 $results{$test}{'type'} = 'complex';
540 } elsif ($type eq 'normal') {
541 $results{$test}{'type'} = 'normal';
543 $results{$test}{'kind'} = $kind;
546 print "Skipping unknown entry: `$_'\n";
552 # Clean up a floating point number
553 sub clean_up_number
{
556 # Remove trailing zeros after the decimal point
557 if ($number =~ /\./) {
564 # Output a file which can be read in as ulps file.
565 sub print_ulps_file
{
567 my ($test, $type, $float, $eps, $fct, $last_fct);
570 open NEWULP
, ">$file" or die ("Can't open $file: $!");
571 print NEWULP
"# Begin of automatic generation\n";
572 # first the function calls
573 foreach $test (sort keys %results) {
574 next if ($results{$test}{'kind'} ne 'test');
575 foreach $type ('real', 'imag', 'normal') {
576 if (exists $results{$test}{$type}) {
577 if (defined $results{$test}) {
578 ($fct) = ($test =~ /^(\w+)\s/);
579 if ($fct ne $last_fct) {
581 print NEWULP
"\n# $fct\n";
584 if ($type eq 'normal') {
585 print NEWULP
"Test \"$test\":\n";
586 } elsif ($type eq 'real') {
587 print NEWULP
"Test \"Real part of: $test\":\n";
588 } elsif ($type eq 'imag') {
589 print NEWULP
"Test \"Imaginary part of: $test\":\n";
591 foreach $float (@all_floats) {
592 if (exists $results{$test}{$type}{'ulp'}{$float}) {
593 print NEWULP
"$float: ",
594 &clean_up_number
($results{$test}{$type}{'ulp'}{$float}),
601 print NEWULP
"\n# Maximal error of functions:\n";
603 foreach $fct (sort keys %results) {
604 next if ($results{$fct}{'kind'} ne 'fct');
605 foreach $type ('real', 'imag', 'normal') {
606 if (exists $results{$fct}{$type}) {
607 if ($type eq 'normal') {
608 print NEWULP
"Function: \"$fct\":\n";
609 } elsif ($type eq 'real') {
610 print NEWULP
"Function: Real part of \"$fct\":\n";
611 } elsif ($type eq 'imag') {
612 print NEWULP
"Function: Imaginary part of \"$fct\":\n";
614 foreach $float (@all_floats) {
615 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
616 print NEWULP
"$float: ",
617 &clean_up_number
($results{$fct}{$type}{'ulp'}{$float}),
625 print NEWULP
"# end of automatic generation\n";
630 my ($test, $type, $float) = @_;
632 return (exists $results{$test}{$type}{'ulp'}{$float}
633 ?
$results{$test}{$type}{'ulp'}{$float} : "0");
636 # Return the ulps value for a single test.
637 sub get_all_ulps_for_test
{
638 my ($test, $type) = @_;
639 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
641 if (exists $results{$test}{'has_ulps'}) {
642 # XXX use all_floats (change order!)
643 $ldouble = &get_ulps
($test, $type, "ldouble");
644 $double = &get_ulps
($test, $type, "double");
645 $float = &get_ulps
($test, $type, "float");
646 $ildouble = &get_ulps
($test, $type, "ildouble");
647 $idouble = &get_ulps
($test, $type, "idouble");
648 $ifloat = &get_ulps
($test, $type, "ifloat");
649 return "CHOOSE ($ldouble, $double, $float, $ildouble, $idouble, $ifloat)";
651 die "get_all_ulps_for_test called for \"$test\" with no ulps\n";
657 my ($file, $ulps_filename) = @_;
658 my ($i, $fct, $type, $ulp, $ulp_real, $ulp_imag);
659 my (%test_ulps, %func_ulps, %func_real_ulps, %func_imag_ulps);
661 open ULP
, ">$file" or die ("Can't open $file: $!");
663 print ULP
"/* This file is automatically generated\n";
664 print ULP
" from $ulps_filename with gen-libm-test.pl.\n";
665 print ULP
" Don't change it - change instead the master files. */\n\n";
667 foreach $fct (keys %results) {
668 $type = $results{$fct}{'type'};
669 if ($type eq 'normal') {
670 $ulp = get_all_ulps_for_test
($fct, 'normal');
671 } elsif ($type eq 'complex') {
672 $ulp_real = get_all_ulps_for_test
($fct, 'real');
673 $ulp_imag = get_all_ulps_for_test
($fct, 'imag');
675 die "unknown results ($fct) type $type\n";
677 if ($results{$fct}{'kind'} eq 'fct') {
678 if ($type eq 'normal') {
679 $func_ulps{$fct} = $ulp;
681 $func_real_ulps{$fct} = $ulp_real;
682 $func_imag_ulps{$fct} = $ulp_imag;
684 } elsif ($results{$fct}{'kind'} eq 'test') {
685 if ($type eq 'normal') {
686 $test_ulps{$fct} = $ulp;
688 $test_ulps{"Real part of: $fct"} = $ulp_real;
689 $test_ulps{"Imaginary part of: $fct"} = $ulp_imag;
692 die "unknown results ($fct) kind $results{$fct}{'kind'}\n";
695 print ULP
"\n/* Maximal error of functions. */\n";
696 print ULP
"static const struct ulp_data func_ulps[] =\n {\n";
697 foreach $fct (sort keys %func_ulps) {
698 print ULP
" { \"$fct\", $func_ulps{$fct} },\n";
701 print ULP
"static const struct ulp_data func_real_ulps[] =\n {\n";
702 foreach $fct (sort keys %func_real_ulps) {
703 print ULP
" { \"$fct\", $func_real_ulps{$fct} },\n";
706 print ULP
"static const struct ulp_data func_imag_ulps[] =\n {\n";
707 foreach $fct (sort keys %func_imag_ulps) {
708 print ULP
" { \"$fct\", $func_imag_ulps{$fct} },\n";
712 print ULP
"\n/* Error of single function calls. */\n";
713 print ULP
"static const struct ulp_data test_ulps[] =\n {\n";
714 foreach $fct (sort keys %test_ulps) {
715 print ULP
" { \"$fct\", $test_ulps{$fct} },\n";
721 # Parse auto-libm-test-out.
722 sub parse_auto_input
{
724 open AUTO
, $file or die ("Can't open $file: $!");
729 if (/^(\S+) (\S+) (.*)$/) {
730 $auto_tests{$1}{$2}{$3} = 1;
732 die ("bad automatic test line: $_\n");