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);
43 use vars qw
(%results);
44 use vars qw
(@tests @functions);
46 use vars qw
(%beautify @all_floats);
47 use vars qw
($output_dir $ulps_file);
49 # all_floats is sorted and contains all recognised float types
50 @all_floats = ('double', 'float', 'idouble',
51 'ifloat', 'ildouble', 'ldouble');
54 ( "minus_zero" => "-0",
56 "minus_infty" => "-inf",
57 "plus_infty" => "inf",
58 "qnan_value" => "qNaN",
62 "M_LOG10El", "log10(e)",
64 "M_PI_34l" => "3/4 pi",
68 "M_PI_34_LOG10El" => "3/4 pi*log10(e)",
69 "M_PI_LOG10El" => "pi*log10(e)",
70 "M_PI2_LOG10El" => "pi/2*log10(e)",
71 "M_PI4_LOG10El" => "pi/4*log10(e)",
72 "M_LOG_SQRT_PIl" => "log(sqrt(pi))",
73 "M_LOG_2_SQRT_PIl" => "log(2*sqrt(pi))",
74 "M_2_SQRT_PIl" => "2 sqrt (pi)",
75 "M_SQRT_PIl" => "sqrt (pi)",
84 # n: generate new ulps file
85 use vars
qw($opt_u $opt_h $opt_o $opt_n);
88 $ulps_file = 'libm-test-ulps';
92 print "Usage: gen-libm-test.pl [OPTIONS]\n";
93 print " -h print this help, then exit\n";
94 print " -o DIR directory where generated files will be placed\n";
95 print " -n only generate sorted file NewUlps from libm-test-ulps\n";
96 print " -u FILE input file with ulps\n";
100 $ulps_file = $opt_u if ($opt_u);
101 $output_dir = $opt_o if ($opt_o);
103 $input = "libm-test.inc";
104 $output = "${output_dir}libm-test.c";
108 &parse_ulps ($ulps_file);
109 &generate_testfile ($input, $output) unless ($opt_n);
110 &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
111 &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
113 # Return a nicer representation
118 if (exists $beautify{$arg}) {
119 return $beautify{$arg};
124 if (exists $beautify{$tmp}) {
125 return '-' . $beautify{$tmp};
128 if ($arg =~ /[0-9]L$/) {
134 # Return a nicer representation of a complex number
135 sub build_complex_beautify {
139 $str1 = &beautify ($r);
140 $str2 = &beautify ($i);
143 $str1 .= ' - ' . $str2;
145 $str1 .= ' + ' . $str2;
151 # Return name of a variable
155 return "x" if ($number == 1);
156 return "y" if ($number == 2);
157 return "z" if ($number == 3);
163 # Add a new test to internal data structures and fill in the
164 # ulps and exception information for the C line.
166 my ($test, $exception, $show_exception) = @_;
170 if (exists $results{$test}{'has_ulps'}) {
171 $rest = ", DELTA$count";
175 if ($show_exception) {
176 if (defined $exception) {
177 $rest .= ", $exception";
182 # We must increment here to keep @tests and count in sync
188 # Adjust an argument or expected value for use in a constant
191 my ($arg, $in_func) = @_;
193 $arg =~ s/(plus_zero|minus_zero|plus_infty|minus_infty|qnan_value
194 |max_value|min_value|min_subnorm_value)/\U$1\E_INIT/xg;
199 # Treat some functions especially.
200 # Currently only sincos needs extra treatment.
201 sub special_functions {
202 my ($file, $in_func, $args) = @_;
203 my (@args, $str, $test, $cline);
205 @args = split /,\s*/, $args;
207 unless ($args[0] =~ /sincos/) {
208 die ("Don't know how to handle $args[0] extra.");
211 $cline = " RUN_TEST_sincos (" . adjust_arg ($args[1], $in_func);
213 $cline = " { " . adjust_arg ($args[1], $in_func);
216 $str = 'sincos (' . &beautify ($args[1]) . ', &sin_res, &cos_res)';
218 $test = $str . ' puts ' . &beautify ($args[2]) . ' in sin_res';
220 my ($sin_res_var) = ($in_func ? ", sin_res" : "");
221 $cline .= ", \"$test\"$sin_res_var, " . adjust_arg ($args[2], $in_func);
222 $cline .= &new_test ($test, $args[4], 0);
225 $test = $str . ' puts ' . &beautify ($args[3]) . ' in cos_res';
226 my ($cos_res_var) = ($in_func ? ", cos_res" : "");
227 $cline .= ", \"$test\"$cos_res_var, " . adjust_arg ($args[3], $in_func);
228 $cline .= &new_test ($test, $args[4], 1);
237 # Parse the arguments to TEST_x_y
239 my ($file, $descr, $fct, $in_func, $args) = @_;
240 my (@args, $str, $descr_args, $descr_res, @descr);
241 my ($current_arg, $cline, $i);
243 my ($extra_var, $call);
245 if ($descr eq 'extra') {
246 &special_functions ($file, $in_func, $args);
249 ($descr_args, $descr_res) = split /_/,$descr, 2;
251 @args = split /,\s*/, $args;
255 # Generate first the string that's shown to the user
258 @descr = split //,$descr_args;
259 for ($i = 0; $i <= $#descr; $i++) {
263 # FLOAT, int, long int, long long int
264 if ($descr[$i] =~ /f|i|l|L/) {
265 $call .= &beautify ($args[$current_arg]);
269 # &FLOAT, &int - argument is added here
270 if ($descr[$i] =~ /F|I/) {
272 $call .= '&' . &get_variable ($extra_var);
276 if ($descr[$i] eq 'c') {
277 $call .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
282 die ("$descr[$i] is unknown");
288 @descr = split //,$descr_res;
290 if ($_ =~ /f|i|l|L/) {
291 $str .= &beautify ($args[$current_arg]);
293 } elsif ($_ eq 'c') {
294 $str .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
296 } elsif ($_ eq 'b') {
298 $str .= ($args[$current_arg] == 0) ? "false" : "true";
300 } elsif ($_ eq '1') {
303 die ("$_ is unknown");
307 if ($current_arg == $#args) {
308 die ("wrong number of arguments")
309 unless ($args[$current_arg] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN/);
310 } elsif ($current_arg < $#args) {
311 die ("wrong number of arguments");
312 } elsif ($current_arg > ($#args+1)) {
313 die ("wrong number of arguments");
317 # Put the C program line together
318 # Reset some variables to start again
322 $cline = "RUN_TEST_$descr";
326 # Special handling for some macros:
327 if ($in_func && $args[0] =~ /fpclassify|isnormal|isfinite|isinf|isnan
328 |issignaling|signbit|isgreater|isgreaterequal|isless|islessequal
329 |islessgreater|isunordered/x) {
330 $cline = "${cline}_tg";
335 $cline .= "\"$str\"";
337 $cline .= ", $args[0]";
339 @descr = split //,$descr_args;
340 for ($i=0; $i <= $#descr; $i++) {
341 # FLOAT, int, long int, long long int
342 if ($descr[$i] =~ /f|i|l|L/) {
343 $cline .= ", " . adjust_arg ($args[$current_arg], $in_func);
348 if ($descr[$i] =~ /F|I/) {
352 if ($descr[$i] eq 'c') {
353 $cline .= ", " . adjust_arg ($args[$current_arg], $in_func);
354 $cline .= ", " . adjust_arg ($args[$current_arg+1], $in_func);
361 @descr = split //,$descr_res;
363 if ($_ =~ /b|f|i|l|L/ ) {
364 $cline .= adjust_arg ($args[$current_arg], $in_func);
366 } elsif ($_ eq 'c') {
367 $cline .= adjust_arg ($args[$current_arg], $in_func);
368 $cline .= ", " . adjust_arg ($args[$current_arg+1], $in_func);
370 } elsif ($_ eq '1') {
371 push @special, $args[$current_arg];
376 $cline .= &new_test ($str, ($current_arg <= $#args) ? $args[$current_arg] : undef, 1);
378 # special treatment for some functions
379 if ($args[0] eq 'frexp') {
380 if (defined $special[0]) {
381 my ($extra_expected) = $special[0];
382 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
383 my ($str) = "$call sets x to $extra_expected";
384 my ($out_var) = ($in_func ? ", x" : "");
387 $extra_expected = "0";
389 $cline .= ", \"$str\"$out_var, 123456789, $run_extra, $extra_expected";
391 $cline .= &new_test ($str, undef, 0);
396 } elsif ($args[0] eq 'gamma' || $args[0] eq 'lgamma') {
397 if (defined $special[0]) {
398 my ($extra_expected) = $special[0];
399 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
400 my ($str) = "$call sets signgam to $extra_expected";
401 my ($out_var) = ($in_func ? ", signgam" : "");
404 $extra_expected = "0";
406 $cline .= ", \"$str\"$out_var, 0, $run_extra, $extra_expected";
408 $cline .= &new_test ($str, undef, 0);
413 } elsif ($args[0] eq 'modf') {
414 if (defined $special[0]) {
415 my ($extra_expected) = $special[0];
416 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
417 my ($str) = "$call sets x to $extra_expected";
418 my ($out_var) = ($in_func ? ", x" : "");
421 $extra_expected = "0";
423 $extra_expected = adjust_arg ($extra_expected, $in_func);
424 $cline .= ", \"$str\"$out_var, 123.456789, $run_extra, $extra_expected";
426 $cline .= &new_test ($str, undef, 0);
431 } elsif ($args[0] eq 'remquo') {
432 if (defined $special[0]) {
433 my ($extra_expected) = $special[0];
434 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
435 my ($str) = "$call sets x to $extra_expected";
436 my ($out_var) = ($in_func ? ", x" : "");
439 $extra_expected = "0";
441 $cline .= ", \"$str\"$out_var, 123456789, $run_extra, $extra_expected";
443 $cline .= &new_test ($str, undef, 0);
450 print $file " $cline);\n";
452 print $file " $cline },\n";
456 # Generate libm-test.c
457 sub generate_testfile {
458 my ($input, $output) = @_;
460 my (@args, $i, $str, $thisfct, $in_func);
462 open INPUT, $input or die ("Can't open $input: $!");
463 open OUTPUT, ">$output" or die ("Can't open $output: $!");
465 # Replace the special macros
473 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
474 &parse_args (\*OUTPUT, $descr, $thisfct, $in_func, $args);
477 # START_DATA (function)
479 ($thisfct) = ($_ =~ /START_DATA\s*\((.*)\)/);
485 ($thisfct) = ($_ =~ /START\s*\((.*)\)/);
487 print OUTPUT " init_max_error ();\n";
490 # END_DATA (function)
496 my ($fct, $line, $type);
504 ($fct) = ($_ =~ /END\s*\((.*)\)/);
505 if ($type eq 'complex') {
506 $line = " print_complex_max_error (\"$fct\", ";
508 $line = " print_max_error (\"$fct\", ";
510 if (exists $results{$fct}{'has_ulps'}) {
511 $line .= "DELTA$fct";
517 push @functions, $fct;
531 my ($test, $type, $float, $eps, $kind);
533 # $type has the following values:
534 # "normal": No complex variable
535 # "real": Real part of complex result
536 # "imag": Imaginary part of complex result
537 open ULP, $file or die ("Can't open $file: $!");
540 # ignore comments and empty lines
544 if (/Real part of:/) {
547 } elsif (/Imaginary part of:/) {
548 s/Imaginary part of: //;
553 s/^.+\"(.*)\".*$/$1/;
559 if (/Real part of/) {
562 } elsif (/Imaginary part of/) {
563 s/Imaginary part of //;
568 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
572 if (/^i?(float|double|ldouble):/) {
573 ($float, $eps) = split /\s*:\s*/,$_,2;
579 $results{$test}{$type}{'ulp'}{$float} = $eps;
580 $results{$test}{'has_ulps'} = 1;
582 if ($type =~ /^real|imag$/) {
583 $results{$test}{'type'} = 'complex';
584 } elsif ($type eq 'normal') {
585 $results{$test}{'type'} = 'normal';
587 $results{$test}{'kind'} = $kind;
590 print "Skipping unknown entry: `$_'\n";
596 # Clean up a floating point number
597 sub clean_up_number {
600 # Remove trailing zeros after the decimal point
601 if ($number =~ /\./) {
608 # Output a file which can be read in as ulps file.
609 sub print_ulps_file {
611 my ($test, $type, $float, $eps, $fct, $last_fct);
614 open NEWULP, ">$file" or die ("Can't open $file: $!");
615 print NEWULP "# Begin of automatic generation\n";
616 # first the function calls
617 foreach $test (sort keys %results) {
618 next if ($results{$test}{'kind'} ne 'test');
619 foreach $type ('real', 'imag', 'normal') {
620 if (exists $results{$test}{$type}) {
621 if (defined $results{$test}) {
622 ($fct) = ($test =~ /^(\w+)\s/);
623 if ($fct ne $last_fct) {
625 print NEWULP "\n# $fct\n";
628 if ($type eq 'normal') {
629 print NEWULP "Test \"$test\":\n";
630 } elsif ($type eq 'real') {
631 print NEWULP "Test \"Real part of: $test\":\n";
632 } elsif ($type eq 'imag') {
633 print NEWULP "Test \"Imaginary part of: $test\":\n";
635 foreach $float (@all_floats) {
636 if (exists $results{$test}{$type}{'ulp'}{$float}) {
637 print NEWULP "$float: ",
638 &clean_up_number ($results{$test}{$type}{'ulp'}{$float}),
645 print NEWULP "\n# Maximal error of functions:\n";
647 foreach $fct (sort keys %results) {
648 next if ($results{$fct}{'kind'} ne 'fct');
649 foreach $type ('real', 'imag', 'normal') {
650 if (exists $results{$fct}{$type}) {
651 if ($type eq 'normal') {
652 print NEWULP "Function: \"$fct\":\n";
653 } elsif ($type eq 'real') {
654 print NEWULP "Function: Real part of \"$fct\":\n";
655 } elsif ($type eq 'imag') {
656 print NEWULP "Function: Imaginary part of \"$fct\":\n";
658 foreach $float (@all_floats) {
659 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
660 print NEWULP "$float: ",
661 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
669 print NEWULP "# end of automatic generation\n";
674 my ($test, $type, $float) = @_;
676 if ($type eq 'complex') {
678 # Return 0 instead of BUILD_COMPLEX_ULP (0,0)
679 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
680 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
683 $res = 'BUILD_COMPLEX_ULP (';
684 $res .= (exists $results{$test}{'real'}{'ulp'}{$float}
685 ? $results{$test}{'real'}{'ulp'}{$float} : "0");
687 $res .= (exists $results{$test}{'imag'}{'ulp'}{$float}
688 ? $results{$test}{'imag'}{'ulp'}{$float} : "0");
692 return (exists $results{$test}{'normal'}{'ulp'}{$float}
693 ? $results{$test}{'normal'}{'ulp'}{$float} : "0");
696 # Output the defines for a single test
698 my ($file, $test, $name) = @_;
699 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
703 if (!exists $results{$test}{'type'}) {
706 $type = $results{$test}{'type'};
707 if (exists $results{$test}{'has_ulps'}) {
708 # XXX use all_floats (change order!)
709 $ldouble = &get_ulps ($test, $type, "ldouble");
710 $double = &get_ulps ($test, $type, "double");
711 $float = &get_ulps ($test, $type, "float");
712 $ildouble = &get_ulps ($test, $type, "ildouble");
713 $idouble = &get_ulps ($test, $type, "idouble");
714 $ifloat = &get_ulps ($test, $type, "ifloat");
715 print $file "#define DELTA$name CHOOSE($ldouble, $double, $float, $ildouble, $idouble, $ifloat)\t/* $test */\n";
721 my ($file, $ulps_filename) = @_;
724 open ULP, ">$file" or die ("Can't open $file: $!");
726 print ULP "/* This file is automatically generated\n";
727 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
728 print ULP " Don't change it - change instead the master files. */\n\n";
730 print ULP "\n/* Maximal error of functions. */\n";
731 foreach $fct (@functions) {
732 output_test (\*ULP, $fct, $fct);
735 print ULP "\n/* Error of single function calls. */\n";
736 for ($i = 0; $i < $count; $i++) {
737 output_test (\*ULP, $tests[$i], $i);