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",
67 "M_LOG10El", "log10(e)",
69 "M_PI_34l" => "3/4 pi",
73 "M_PI_34_LOG10El" => "3/4 pi*log10(e)",
74 "M_PI_LOG10El" => "pi*log10(e)",
75 "M_PI2_LOG10El" => "pi/2*log10(e)",
76 "M_PI4_LOG10El" => "pi/4*log10(e)",
77 "M_LOG_SQRT_PIl" => "log(sqrt(pi))",
78 "M_LOG_2_SQRT_PIl" => "log(2*sqrt(pi))",
79 "M_2_SQRT_PIl" => "2 sqrt (pi)",
80 "M_SQRT_PIl" => "sqrt (pi)",
89 # n: generate new ulps file
90 use vars
qw($opt_u $opt_h $opt_o $opt_n);
93 $ulps_file = 'libm-test-ulps';
97 print "Usage: gen-libm-test.pl [OPTIONS]\n";
98 print " -h print this help, then exit\n";
99 print " -o DIR directory where generated files will be placed\n";
100 print " -n only generate sorted file NewUlps from libm-test-ulps\n";
101 print " -u FILE input file with ulps\n";
105 $ulps_file = $opt_u if ($opt_u);
106 $output_dir = $opt_o if ($opt_o);
108 $input = "libm-test.inc";
109 $auto_input = "auto-libm-test-out";
110 $output = "${output_dir}libm-test.c";
112 &parse_ulps ($ulps_file);
113 &parse_auto_input ($auto_input);
114 &generate_testfile ($input, $output) unless ($opt_n);
115 &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
116 &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
118 # Return a nicer representation
123 if (exists $beautify{$arg}) {
124 return $beautify{$arg};
129 if (exists $beautify{$tmp}) {
130 return '-' . $beautify{$tmp};
133 if ($arg =~ /^-?0x[0-9a-f.]*p[-+][0-9]+f$/) {
136 if ($arg =~ /[0-9]L$/) {
142 # Return a nicer representation of a complex number
143 sub build_complex_beautify {
147 $str1 = &beautify ($r);
148 $str2 = &beautify ($i);
151 $str1 .= ' - ' . $str2;
153 $str1 .= ' + ' . $str2;
159 # Return the text to put in an initializer for a test's exception
161 sub show_exceptions {
162 my ($ignore_result, $exception) = @_;
163 $ignore_result = ($ignore_result ? "IGNORE_RESULT|" : "");
164 if (defined $exception) {
165 return ", ${ignore_result}$exception";
167 return ", ${ignore_result}0";
171 # Parse the arguments to TEST_x_y
173 my ($file, $descr, $args) = @_;
174 my (@args, $descr_args, $descr_res, @descr);
175 my ($current_arg, $cline, $i);
178 my ($ignore_result_any, $ignore_result_all);
180 ($descr_args, $descr_res) = split /_/,$descr, 2;
182 @args = split /,\s*/, $args;
186 # Generate first the string that's shown to the user
188 @descr = split //,$descr_args;
189 for ($i = 0; $i <= $#descr; $i++) {
191 if ($current_arg > 1) {
194 # FLOAT, int, long int, long long int
195 if ($descr[$i] =~ /f|i|l|L/) {
196 $call_args .= $comma . &beautify ($args[$current_arg]);
200 # &FLOAT, &int - simplify call by not showing argument.
201 if ($descr[$i] =~ /F|I/) {
205 if ($descr[$i] eq 'c') {
206 $call_args .= $comma . &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
211 die ("$descr[$i] is unknown");
215 @descr = split //,$descr_res;
217 if ($_ =~ /f|i|l|L/) {
219 } elsif ($_ eq 'c') {
221 } elsif ($_ eq 'b') {
224 } elsif ($_ eq '1') {
227 die ("$_ is unknown");
231 if ($current_arg == $#args) {
232 die ("wrong number of arguments")
233 unless ($args[$current_arg] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN|TEST_NAN_SIGN|NO_TEST_INLINE|XFAIL_TEST/);
234 } elsif ($current_arg < $#args) {
235 die ("wrong number of arguments");
236 } elsif ($current_arg > ($#args+1)) {
237 die ("wrong number of arguments");
241 # Put the C program line together
242 # Reset some variables to start again
244 $cline = "{ \"$call_args\"";
245 @descr = split //,$descr_args;
246 for ($i=0; $i <= $#descr; $i++) {
247 # FLOAT, int, long int, long long int
248 if ($descr[$i] =~ /f|i|l|L/) {
249 $cline .= ", $args[$current_arg]";
254 if ($descr[$i] =~ /F|I/) {
258 if ($descr[$i] eq 'c') {
259 $cline .= ", $args[$current_arg], $args[$current_arg+1]";
265 @descr = split //,$descr_res;
266 $ignore_result_any = 0;
267 $ignore_result_all = 1;
269 if ($_ =~ /b|f|i|l|L/ ) {
270 my ($result) = $args[$current_arg];
271 if ($result eq "IGNORE") {
272 $ignore_result_any = 1;
275 $ignore_result_all = 0;
277 $cline .= ", $result";
279 } elsif ($_ eq 'c') {
280 my ($result1) = $args[$current_arg];
281 if ($result1 eq "IGNORE") {
282 $ignore_result_any = 1;
285 $ignore_result_all = 0;
287 my ($result2) = $args[$current_arg + 1];
288 if ($result2 eq "IGNORE") {
289 $ignore_result_any = 1;
292 $ignore_result_all = 0;
294 $cline .= ", $result1, $result2";
296 } elsif ($_ eq '1') {
297 push @special, $args[$current_arg];
301 if ($ignore_result_any && !$ignore_result_all) {
302 die ("some but not all function results ignored\n");
305 $cline .= show_exceptions ($ignore_result_any,
306 ($current_arg <= $#args)
307 ? $args[$current_arg]
310 # special treatment for some functions
314 my ($extra_expected) = $_;
315 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
317 $extra_expected = "0";
319 $cline .= ", $run_extra, $extra_expected";
321 print $file " $cline },\n";
324 # Convert a condition from auto-libm-test-out to C form.
325 sub convert_condition {
328 @conds = split /:/, $cond;
333 $ret = join " && ", @conds;
337 # Return text to OR a value into an accumulated flags string.
347 # Return text to OR a conditional expression between two values into
348 # an accumulated flags string.
350 my ($cond, $if, $else) = @_;
352 return or_value ($if);
353 } elsif ($cond eq "0") {
354 return or_value ($else);
356 return or_value ("($cond ? $if : $else)");
360 # Generate libm-test.c
361 sub generate_testfile {
362 my ($input, $output) = @_;
364 open INPUT, $input or die ("Can't open $input: $!");
365 open OUTPUT, ">$output" or die ("Can't open $output: $!");
367 # Replace the special macros
369 # AUTO_TESTS (function, mode),
370 if (/^\s*AUTO_TESTS_/) {
371 my ($descr, $func, $mode, $auto_test, $num_auto_tests);
372 ($descr, $func, $mode) = ($_ =~ /AUTO_TESTS_(\w+)\s*\((\w+),\s*(\w+)\)/);
374 foreach $auto_test (sort keys %{$auto_tests{$func}{$mode}}) {
375 my ($finputs, $format, $inputs, $outputs, $flags);
376 my ($format_conv, $flags_conv, @flags, %flag_cond);
378 ($finputs, $outputs, $flags) = split / : */, $auto_test;
379 ($format, $inputs) = split / /, $finputs, 2;
381 $outputs =~ s/ /, /g;
382 $format_conv = convert_condition ($format);
383 print OUTPUT "#if $format_conv\n";
384 @flags = split / /, $flags;
386 if (/^([^:]*):(.*)$/) {
389 $cond = convert_condition ($2);
390 if (defined ($flag_cond{$flag})) {
391 if ($flag_cond{$flag} ne "1") {
392 $flag_cond{$flag} .= " || $cond";
395 $flag_cond{$flag} = $cond;
398 $flag_cond{$_} = "1";
402 if (defined ($flag_cond{"no-test-inline"})) {
403 $flags_conv .= or_cond_value ($flag_cond{"no-test-inline"},
404 "NO_TEST_INLINE", "0");
406 if (defined ($flag_cond{"xfail"})) {
407 $flags_conv .= or_cond_value ($flag_cond{"xfail"},
410 my (@exc_list) = qw(divbyzero inexact invalid overflow underflow);
412 foreach $exc (@exc_list) {
413 my ($exc_expected, $exc_ok, $no_exc);
414 $exc_expected = "\U$exc\E_EXCEPTION";
415 $exc_ok = "\U$exc\E_EXCEPTION_OK";
417 if ($exc eq "inexact") {
419 $no_exc = "NO_INEXACT_EXCEPTION";
421 if (defined ($flag_cond{$exc})) {
422 if ($flag_cond{$exc} ne "1") {
423 die ("unexpected condition for $exc\n");
425 if (defined ($flag_cond{"$exc-ok"})) {
426 $flags_conv .= or_cond_value
($flag_cond{"$exc-ok"},
427 $exc_ok, $exc_expected);
429 $flags_conv .= or_value
($exc_expected);
432 if (defined ($flag_cond{"$exc-ok"})) {
433 $flags_conv .= or_cond_value
($flag_cond{"$exc-ok"},
436 $flags_conv .= or_value
($no_exc);
440 my ($errno_expected, $errno_unknown_cond);
441 if (defined ($flag_cond{"errno-edom"})) {
442 if ($flag_cond{"errno-edom"} ne "1") {
443 die ("unexpected condition for errno-edom");
445 if (defined ($flag_cond{"errno-erange"})) {
446 die ("multiple errno values expected");
448 $errno_expected = "ERRNO_EDOM";
449 } elsif (defined ($flag_cond{"errno-erange"})) {
450 if ($flag_cond{"errno-erange"} ne "1") {
451 die ("unexpected condition for errno-erange");
453 $errno_expected = "ERRNO_ERANGE";
455 $errno_expected = "ERRNO_UNCHANGED";
457 if (defined ($flag_cond{"errno-edom-ok"})) {
458 if (defined ($flag_cond{"errno-erange-ok"})
459 && $flag_cond{"errno-erange-ok"} ne $flag_cond{"errno-edom-ok"}) {
460 $errno_unknown_cond = "($flag_cond{\"errno-edom-ok\"} || $flag_cond{\"errno-erange-ok\"})";
462 $errno_unknown_cond = $flag_cond{"errno-edom-ok"};
464 } elsif (defined ($flag_cond{"errno-erange-ok"})) {
465 $errno_unknown_cond = $flag_cond{"errno-erange-ok"};
467 $errno_unknown_cond = "0";
469 $flags_conv .= or_cond_value
($errno_unknown_cond,
470 "0", $errno_expected);
471 if ($flags_conv ne "") {
472 $flags_conv =~ s/^ \|/,/;
474 &parse_args
(\
*OUTPUT
, $descr,
475 "$func, $inputs, $outputs$flags_conv");
476 print OUTPUT
"#endif\n";
478 if ($num_auto_tests == 0) {
479 die ("no automatic tests for $func, $mode\n");
488 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
489 &parse_args
(\
*OUTPUT
, $descr, $args);
503 my ($test, $type, $float, $eps, $kind);
505 # $type has the following values:
506 # "normal": No complex variable
507 # "real": Real part of complex result
508 # "imag": Imaginary part of complex result
509 open ULP
, $file or die ("Can't open $file: $!");
512 # ignore comments and empty lines
516 if (/Real part of:/) {
519 } elsif (/Imaginary part of:/) {
520 s/Imaginary part of: //;
525 s/^.+\"(.*)\".*$/$1/;
531 if (/Real part of/) {
534 } elsif (/Imaginary part of/) {
535 s/Imaginary part of //;
540 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
544 if (/^i?(float|double|ldouble):/) {
545 ($float, $eps) = split /\s*:\s*/,$_,2;
551 $results{$test}{$type}{'ulp'}{$float} = $eps;
552 $results{$test}{'has_ulps'} = 1;
554 if ($type =~ /^real|imag$/) {
555 $results{$test}{'type'} = 'complex';
556 } elsif ($type eq 'normal') {
557 $results{$test}{'type'} = 'normal';
559 $results{$test}{'kind'} = $kind;
562 print "Skipping unknown entry: `$_'\n";
568 # Clean up a floating point number
569 sub clean_up_number
{
572 # Remove trailing zeros after the decimal point
573 if ($number =~ /\./) {
580 # Output a file which can be read in as ulps file.
581 sub print_ulps_file
{
583 my ($test, $type, $float, $eps, $fct, $last_fct);
586 open NEWULP
, ">$file" or die ("Can't open $file: $!");
587 print NEWULP
"# Begin of automatic generation\n";
588 # first the function calls
589 foreach $test (sort keys %results) {
590 next if ($results{$test}{'kind'} ne 'test');
591 foreach $type ('real', 'imag', 'normal') {
592 if (exists $results{$test}{$type}) {
593 if (defined $results{$test}) {
594 ($fct) = ($test =~ /^(\w+)\s/);
595 if ($fct ne $last_fct) {
597 print NEWULP
"\n# $fct\n";
600 if ($type eq 'normal') {
601 print NEWULP
"Test \"$test\":\n";
602 } elsif ($type eq 'real') {
603 print NEWULP
"Test \"Real part of: $test\":\n";
604 } elsif ($type eq 'imag') {
605 print NEWULP
"Test \"Imaginary part of: $test\":\n";
607 foreach $float (@all_floats) {
608 if (exists $results{$test}{$type}{'ulp'}{$float}) {
609 print NEWULP
"$float: ",
610 &clean_up_number
($results{$test}{$type}{'ulp'}{$float}),
617 print NEWULP
"\n# Maximal error of functions:\n";
619 foreach $fct (sort keys %results) {
620 next if ($results{$fct}{'kind'} ne 'fct');
621 foreach $type ('real', 'imag', 'normal') {
622 if (exists $results{$fct}{$type}) {
623 if ($type eq 'normal') {
624 print NEWULP
"Function: \"$fct\":\n";
625 } elsif ($type eq 'real') {
626 print NEWULP
"Function: Real part of \"$fct\":\n";
627 } elsif ($type eq 'imag') {
628 print NEWULP
"Function: Imaginary part of \"$fct\":\n";
630 foreach $float (@all_floats) {
631 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
632 print NEWULP
"$float: ",
633 &clean_up_number
($results{$fct}{$type}{'ulp'}{$float}),
641 print NEWULP
"# end of automatic generation\n";
646 my ($test, $type, $float) = @_;
648 return (exists $results{$test}{$type}{'ulp'}{$float}
649 ?
$results{$test}{$type}{'ulp'}{$float} : "0");
652 # Return the ulps value for a single test.
653 sub get_all_ulps_for_test
{
654 my ($test, $type) = @_;
655 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
657 if (exists $results{$test}{'has_ulps'}) {
658 # XXX use all_floats (change order!)
659 $ldouble = &get_ulps
($test, $type, "ldouble");
660 $double = &get_ulps
($test, $type, "double");
661 $float = &get_ulps
($test, $type, "float");
662 $ildouble = &get_ulps
($test, $type, "ildouble");
663 $idouble = &get_ulps
($test, $type, "idouble");
664 $ifloat = &get_ulps
($test, $type, "ifloat");
665 return "CHOOSE ($ldouble, $double, $float, $ildouble, $idouble, $ifloat)";
667 die "get_all_ulps_for_test called for \"$test\" with no ulps\n";
673 my ($file, $ulps_filename) = @_;
674 my ($i, $fct, $type, $ulp, $ulp_real, $ulp_imag);
675 my (%test_ulps, %func_ulps, %func_real_ulps, %func_imag_ulps);
677 open ULP
, ">$file" or die ("Can't open $file: $!");
679 print ULP
"/* This file is automatically generated\n";
680 print ULP
" from $ulps_filename with gen-libm-test.pl.\n";
681 print ULP
" Don't change it - change instead the master files. */\n\n";
683 foreach $fct (keys %results) {
684 $type = $results{$fct}{'type'};
685 if ($type eq 'normal') {
686 $ulp = get_all_ulps_for_test
($fct, 'normal');
687 } elsif ($type eq 'complex') {
688 $ulp_real = get_all_ulps_for_test
($fct, 'real');
689 $ulp_imag = get_all_ulps_for_test
($fct, 'imag');
691 die "unknown results ($fct) type $type\n";
693 if ($results{$fct}{'kind'} eq 'fct') {
694 if ($type eq 'normal') {
695 $func_ulps{$fct} = $ulp;
697 $func_real_ulps{$fct} = $ulp_real;
698 $func_imag_ulps{$fct} = $ulp_imag;
700 } elsif ($results{$fct}{'kind'} eq 'test') {
701 if ($type eq 'normal') {
702 $test_ulps{$fct} = $ulp;
704 $test_ulps{"Real part of: $fct"} = $ulp_real;
705 $test_ulps{"Imaginary part of: $fct"} = $ulp_imag;
708 die "unknown results ($fct) kind $results{$fct}{'kind'}\n";
711 print ULP
"\n/* Maximal error of functions. */\n";
712 print ULP
"static const struct ulp_data func_ulps[] =\n {\n";
713 foreach $fct (sort keys %func_ulps) {
714 print ULP
" { \"$fct\", $func_ulps{$fct} },\n";
717 print ULP
"static const struct ulp_data func_real_ulps[] =\n {\n";
718 foreach $fct (sort keys %func_real_ulps) {
719 print ULP
" { \"$fct\", $func_real_ulps{$fct} },\n";
722 print ULP
"static const struct ulp_data func_imag_ulps[] =\n {\n";
723 foreach $fct (sort keys %func_imag_ulps) {
724 print ULP
" { \"$fct\", $func_imag_ulps{$fct} },\n";
728 print ULP
"\n/* Error of single function calls. */\n";
729 print ULP
"static const struct ulp_data test_ulps[] =\n {\n";
730 foreach $fct (sort keys %test_ulps) {
731 print ULP
" { \"$fct\", $test_ulps{$fct} },\n";
737 # Parse auto-libm-test-out.
738 sub parse_auto_input
{
740 open AUTO
, $file or die ("Can't open $file: $!");
745 if (/^(\S+) (\S+) (.*)$/) {
746 $auto_tests{$1}{$2}{$3} = 1;
748 die ("bad automatic test line: $_\n");