2 # Copyright (C) 1999-2013 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);
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';
80 print "Usage: gen-libm-test.pl [OPTIONS]\n";
81 print " -h print this help, then exit\n";
82 print " -o DIR directory where generated files will be placed\n";
83 print " -n only generate sorted file NewUlps from libm-test-ulps\n";
84 print " -u FILE input file with ulps\n";
88 $ulps_file = $opt_u if ($opt_u);
89 $output_dir = $opt_o if ($opt_o);
91 $input = "libm-test.inc";
92 $auto_input = "auto-libm-test-out";
93 $output = "${output_dir}libm-test.c";
95 &parse_ulps ($ulps_file);
96 &parse_auto_input ($auto_input);
97 &generate_testfile ($input, $output) unless ($opt_n);
98 &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
99 &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
101 # Return a nicer representation
106 if (exists $beautify{$arg}) {
107 return $beautify{$arg};
112 if (exists $beautify{$tmp}) {
113 return '-' . $beautify{$tmp};
116 if ($arg =~ /^-?0x[0-9a-f.]*p[-+][0-9]+f$/) {
119 if ($arg =~ /[0-9]L$/) {
125 # Return a nicer representation of a complex number
126 sub build_complex_beautify {
130 $str1 = &beautify ($r);
131 $str2 = &beautify ($i);
134 $str1 .= ' - ' . $str2;
136 $str1 .= ' + ' . $str2;
142 # Return the text to put in an initializer for a test's exception
144 sub show_exceptions {
145 my ($ignore_result, $exception) = @_;
146 $ignore_result = ($ignore_result ? "IGNORE_RESULT|" : "");
147 if (defined $exception) {
148 return ", ${ignore_result}$exception";
150 return ", ${ignore_result}0";
154 # Parse the arguments to TEST_x_y
156 my ($file, $descr, $args) = @_;
157 my (@args, $descr_args, $descr_res, @descr);
158 my ($current_arg, $cline, $i);
161 my ($ignore_result_any, $ignore_result_all);
163 ($descr_args, $descr_res) = split /_/,$descr, 2;
165 @args = split /,\s*/, $args;
169 # Generate first the string that's shown to the user
171 @descr = split //,$descr_args;
172 for ($i = 0; $i <= $#descr; $i++) {
174 if ($current_arg > 1) {
177 # FLOAT, int, long int, long long int
178 if ($descr[$i] =~ /f|i|l|L/) {
179 $call_args .= $comma . &beautify ($args[$current_arg]);
183 # &FLOAT, &int - simplify call by not showing argument.
184 if ($descr[$i] =~ /F|I/) {
188 if ($descr[$i] eq 'c') {
189 $call_args .= $comma . &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
194 die ("$descr[$i] is unknown");
198 @descr = split //,$descr_res;
200 if ($_ =~ /f|i|l|L/) {
202 } elsif ($_ eq 'c') {
204 } elsif ($_ eq 'b') {
207 } elsif ($_ eq '1') {
210 die ("$_ is unknown");
214 if ($current_arg == $#args) {
215 die ("wrong number of arguments")
216 unless ($args[$current_arg] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN|TEST_NAN_SIGN|NO_TEST_INLINE|XFAIL_TEST/);
217 } elsif ($current_arg < $#args) {
218 die ("wrong number of arguments");
219 } elsif ($current_arg > ($#args+1)) {
220 die ("wrong number of arguments");
224 # Put the C program line together
225 # Reset some variables to start again
227 $cline = "{ \"$call_args\"";
228 @descr = split //,$descr_args;
229 for ($i=0; $i <= $#descr; $i++) {
230 # FLOAT, int, long int, long long int
231 if ($descr[$i] =~ /f|i|l|L/) {
232 $cline .= ", $args[$current_arg]";
237 if ($descr[$i] =~ /F|I/) {
241 if ($descr[$i] eq 'c') {
242 $cline .= ", $args[$current_arg], $args[$current_arg+1]";
248 @descr = split //,$descr_res;
249 $ignore_result_any = 0;
250 $ignore_result_all = 1;
252 if ($_ =~ /b|f|i|l|L/ ) {
253 my ($result) = $args[$current_arg];
254 if ($result eq "IGNORE") {
255 $ignore_result_any = 1;
258 $ignore_result_all = 0;
260 $cline .= ", $result";
262 } elsif ($_ eq 'c') {
263 my ($result1) = $args[$current_arg];
264 if ($result1 eq "IGNORE") {
265 $ignore_result_any = 1;
268 $ignore_result_all = 0;
270 my ($result2) = $args[$current_arg + 1];
271 if ($result2 eq "IGNORE") {
272 $ignore_result_any = 1;
275 $ignore_result_all = 0;
277 $cline .= ", $result1, $result2";
279 } elsif ($_ eq '1') {
280 push @special, $args[$current_arg];
284 if ($ignore_result_any && !$ignore_result_all) {
285 die ("some but not all function results ignored\n");
288 $cline .= show_exceptions ($ignore_result_any,
289 ($current_arg <= $#args)
290 ? $args[$current_arg]
293 # special treatment for some functions
297 my ($extra_expected) = $_;
298 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
300 $extra_expected = "0";
302 $cline .= ", $run_extra, $extra_expected";
304 print $file " $cline },\n";
307 # Convert a condition from auto-libm-test-out to C form.
308 sub convert_condition {
311 @conds = split /:/, $cond;
316 $ret = join " && ", @conds;
320 # Return text to OR a value into an accumulated flags string.
330 # Return text to OR a conditional expression between two values into
331 # an accumulated flags string.
333 my ($cond, $if, $else) = @_;
335 return or_value ($if);
336 } elsif ($cond eq "0") {
337 return or_value ($else);
339 return or_value ("($cond ? $if : $else)");
343 # Generate libm-test.c
344 sub generate_testfile {
345 my ($input, $output) = @_;
347 open INPUT, $input or die ("Can't open $input: $!");
348 open OUTPUT, ">$output" or die ("Can't open $output: $!");
350 # Replace the special macros
352 # AUTO_TESTS (function, mode),
353 if (/^\s*AUTO_TESTS_/) {
354 my ($descr, $func, $mode, $auto_test, $num_auto_tests);
355 ($descr, $func, $mode) = ($_ =~ /AUTO_TESTS_(\w+)\s*\((\w+),\s*(\w+)\)/);
357 foreach $auto_test (sort keys %{$auto_tests{$func}{$mode}}) {
358 my ($finputs, $format, $inputs, $outputs, $flags);
359 my ($format_conv, $flags_conv, @flags, %flag_cond);
361 ($finputs, $outputs, $flags) = split / : */, $auto_test;
362 ($format, $inputs) = split / /, $finputs, 2;
364 $outputs =~ s/ /, /g;
365 $format_conv = convert_condition ($format);
366 print OUTPUT "#if $format_conv\n";
367 @flags = split / /, $flags;
369 if (/^([^:]*):(.*)$/) {
372 $cond = convert_condition ($2);
373 if (defined ($flag_cond{$flag})) {
374 if ($flag_cond{$flag} ne "1") {
375 $flag_cond{$flag} .= " || $cond";
378 $flag_cond{$flag} = $cond;
381 $flag_cond{$_} = "1";
385 if (defined ($flag_cond{"no-test-inline"})) {
386 $flags_conv .= or_cond_value ($flag_cond{"no-test-inline"},
387 "NO_TEST_INLINE", "0");
389 if (defined ($flag_cond{"xfail"})) {
390 $flags_conv .= or_cond_value ($flag_cond{"xfail"},
393 my (@exc_list) = qw(divbyzero inexact invalid overflow underflow);
395 foreach $exc (@exc_list) {
396 my ($exc_expected, $exc_ok, $no_exc);
397 $exc_expected = "\U$exc\E_EXCEPTION";
398 $exc_ok = "\U$exc\E_EXCEPTION_OK";
400 if ($exc eq "inexact") {
402 $no_exc = "NO_INEXACT_EXCEPTION";
404 if (defined ($flag_cond{$exc})) {
405 if ($flag_cond{$exc} ne "1") {
406 die ("unexpected condition for $exc\n");
408 if (defined ($flag_cond{"$exc-ok"})) {
409 $flags_conv .= or_cond_value
($flag_cond{"$exc-ok"},
410 $exc_ok, $exc_expected);
412 $flags_conv .= or_value
($exc_expected);
415 if (defined ($flag_cond{"$exc-ok"})) {
416 $flags_conv .= or_cond_value
($flag_cond{"$exc-ok"},
419 $flags_conv .= or_value
($no_exc);
423 my ($errno_expected, $errno_unknown_cond);
424 if (defined ($flag_cond{"errno-edom"})) {
425 if ($flag_cond{"errno-edom"} ne "1") {
426 die ("unexpected condition for errno-edom");
428 if (defined ($flag_cond{"errno-erange"})) {
429 die ("multiple errno values expected");
431 $errno_expected = "ERRNO_EDOM";
432 } elsif (defined ($flag_cond{"errno-erange"})) {
433 if ($flag_cond{"errno-erange"} ne "1") {
434 die ("unexpected condition for errno-erange");
436 $errno_expected = "ERRNO_ERANGE";
438 $errno_expected = "ERRNO_UNCHANGED";
440 if (defined ($flag_cond{"errno-edom-ok"})) {
441 if (defined ($flag_cond{"errno-erange-ok"})
442 && $flag_cond{"errno-erange-ok"} ne $flag_cond{"errno-edom-ok"}) {
443 $errno_unknown_cond = "($flag_cond{\"errno-edom-ok\"} || $flag_cond{\"errno-erange-ok\"})";
445 $errno_unknown_cond = $flag_cond{"errno-edom-ok"};
447 } elsif (defined ($flag_cond{"errno-erange-ok"})) {
448 $errno_unknown_cond = $flag_cond{"errno-erange-ok"};
450 $errno_unknown_cond = "0";
452 $flags_conv .= or_cond_value
($errno_unknown_cond,
453 "0", $errno_expected);
454 if ($flags_conv ne "") {
455 $flags_conv =~ s/^ \|/,/;
457 &parse_args
(\
*OUTPUT
, $descr,
458 "$func, $inputs, $outputs$flags_conv");
459 print OUTPUT
"#endif\n";
461 if ($num_auto_tests == 0) {
462 die ("no automatic tests for $func, $mode\n");
471 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
472 &parse_args
(\
*OUTPUT
, $descr, $args);
486 my ($test, $type, $float, $eps, $kind);
488 # $type has the following values:
489 # "normal": No complex variable
490 # "real": Real part of complex result
491 # "imag": Imaginary part of complex result
492 open ULP
, $file or die ("Can't open $file: $!");
495 # ignore comments and empty lines
499 if (/Real part of:/) {
502 } elsif (/Imaginary part of:/) {
503 s/Imaginary part of: //;
508 s/^.+\"(.*)\".*$/$1/;
514 if (/Real part of/) {
517 } elsif (/Imaginary part of/) {
518 s/Imaginary part of //;
523 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
527 if (/^i?(float|double|ldouble):/) {
528 ($float, $eps) = split /\s*:\s*/,$_,2;
534 $results{$test}{$type}{'ulp'}{$float} = $eps;
535 $results{$test}{'has_ulps'} = 1;
537 if ($type =~ /^real|imag$/) {
538 $results{$test}{'type'} = 'complex';
539 } elsif ($type eq 'normal') {
540 $results{$test}{'type'} = 'normal';
542 $results{$test}{'kind'} = $kind;
545 print "Skipping unknown entry: `$_'\n";
551 # Clean up a floating point number
552 sub clean_up_number
{
555 # Remove trailing zeros after the decimal point
556 if ($number =~ /\./) {
563 # Output a file which can be read in as ulps file.
564 sub print_ulps_file
{
566 my ($test, $type, $float, $eps, $fct, $last_fct);
569 open NEWULP
, ">$file" or die ("Can't open $file: $!");
570 print NEWULP
"# Begin of automatic generation\n";
571 # first the function calls
572 foreach $test (sort keys %results) {
573 next if ($results{$test}{'kind'} ne 'test');
574 foreach $type ('real', 'imag', 'normal') {
575 if (exists $results{$test}{$type}) {
576 if (defined $results{$test}) {
577 ($fct) = ($test =~ /^(\w+)\s/);
578 if ($fct ne $last_fct) {
580 print NEWULP
"\n# $fct\n";
583 if ($type eq 'normal') {
584 print NEWULP
"Test \"$test\":\n";
585 } elsif ($type eq 'real') {
586 print NEWULP
"Test \"Real part of: $test\":\n";
587 } elsif ($type eq 'imag') {
588 print NEWULP
"Test \"Imaginary part of: $test\":\n";
590 foreach $float (@all_floats) {
591 if (exists $results{$test}{$type}{'ulp'}{$float}) {
592 print NEWULP
"$float: ",
593 &clean_up_number
($results{$test}{$type}{'ulp'}{$float}),
600 print NEWULP
"\n# Maximal error of functions:\n";
602 foreach $fct (sort keys %results) {
603 next if ($results{$fct}{'kind'} ne 'fct');
604 foreach $type ('real', 'imag', 'normal') {
605 if (exists $results{$fct}{$type}) {
606 if ($type eq 'normal') {
607 print NEWULP
"Function: \"$fct\":\n";
608 } elsif ($type eq 'real') {
609 print NEWULP
"Function: Real part of \"$fct\":\n";
610 } elsif ($type eq 'imag') {
611 print NEWULP
"Function: Imaginary part of \"$fct\":\n";
613 foreach $float (@all_floats) {
614 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
615 print NEWULP
"$float: ",
616 &clean_up_number
($results{$fct}{$type}{'ulp'}{$float}),
624 print NEWULP
"# end of automatic generation\n";
629 my ($test, $type, $float) = @_;
631 return (exists $results{$test}{$type}{'ulp'}{$float}
632 ?
$results{$test}{$type}{'ulp'}{$float} : "0");
635 # Return the ulps value for a single test.
636 sub get_all_ulps_for_test
{
637 my ($test, $type) = @_;
638 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
640 if (exists $results{$test}{'has_ulps'}) {
641 # XXX use all_floats (change order!)
642 $ldouble = &get_ulps
($test, $type, "ldouble");
643 $double = &get_ulps
($test, $type, "double");
644 $float = &get_ulps
($test, $type, "float");
645 $ildouble = &get_ulps
($test, $type, "ildouble");
646 $idouble = &get_ulps
($test, $type, "idouble");
647 $ifloat = &get_ulps
($test, $type, "ifloat");
648 return "CHOOSE ($ldouble, $double, $float, $ildouble, $idouble, $ifloat)";
650 die "get_all_ulps_for_test called for \"$test\" with no ulps\n";
656 my ($file, $ulps_filename) = @_;
657 my ($i, $fct, $type, $ulp, $ulp_real, $ulp_imag);
658 my (%test_ulps, %func_ulps, %func_real_ulps, %func_imag_ulps);
660 open ULP
, ">$file" or die ("Can't open $file: $!");
662 print ULP
"/* This file is automatically generated\n";
663 print ULP
" from $ulps_filename with gen-libm-test.pl.\n";
664 print ULP
" Don't change it - change instead the master files. */\n\n";
666 foreach $fct (keys %results) {
667 $type = $results{$fct}{'type'};
668 if ($type eq 'normal') {
669 $ulp = get_all_ulps_for_test
($fct, 'normal');
670 } elsif ($type eq 'complex') {
671 $ulp_real = get_all_ulps_for_test
($fct, 'real');
672 $ulp_imag = get_all_ulps_for_test
($fct, 'imag');
674 die "unknown results ($fct) type $type\n";
676 if ($results{$fct}{'kind'} eq 'fct') {
677 if ($type eq 'normal') {
678 $func_ulps{$fct} = $ulp;
680 $func_real_ulps{$fct} = $ulp_real;
681 $func_imag_ulps{$fct} = $ulp_imag;
683 } elsif ($results{$fct}{'kind'} eq 'test') {
684 if ($type eq 'normal') {
685 $test_ulps{$fct} = $ulp;
687 $test_ulps{"Real part of: $fct"} = $ulp_real;
688 $test_ulps{"Imaginary part of: $fct"} = $ulp_imag;
691 die "unknown results ($fct) kind $results{$fct}{'kind'}\n";
694 print ULP
"\n/* Maximal error of functions. */\n";
695 print ULP
"static const struct ulp_data func_ulps[] =\n {\n";
696 foreach $fct (sort keys %func_ulps) {
697 print ULP
" { \"$fct\", $func_ulps{$fct} },\n";
700 print ULP
"static const struct ulp_data func_real_ulps[] =\n {\n";
701 foreach $fct (sort keys %func_real_ulps) {
702 print ULP
" { \"$fct\", $func_real_ulps{$fct} },\n";
705 print ULP
"static const struct ulp_data func_imag_ulps[] =\n {\n";
706 foreach $fct (sort keys %func_imag_ulps) {
707 print ULP
" { \"$fct\", $func_imag_ulps{$fct} },\n";
711 print ULP
"\n/* Error of single function calls. */\n";
712 print ULP
"static const struct ulp_data test_ulps[] =\n {\n";
713 foreach $fct (sort keys %test_ulps) {
714 print ULP
" { \"$fct\", $test_ulps{$fct} },\n";
720 # Parse auto-libm-test-out.
721 sub parse_auto_input
{
723 open AUTO
, $file or die ("Can't open $file: $!");
728 if (/^(\S+) (\S+) (.*)$/) {
729 $auto_tests{$1}{$2}{$3} = 1;
731 die ("bad automatic test line: $_\n");