]>
git.ipfire.org Git - thirdparty/glibc.git/blob - math/gen-libm-test.pl
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
(@functions);
45 use vars qw
(%beautify @all_floats);
46 use vars qw
($output_dir $ulps_file);
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",
55 "minus_infty" => "-inf",
56 "plus_infty" => "inf",
57 "qnan_value" => "qNaN",
61 "M_LOG10El", "log10(e)",
63 "M_PI_34l" => "3/4 pi",
67 "M_PI_34_LOG10El" => "3/4 pi*log10(e)",
68 "M_PI_LOG10El" => "pi*log10(e)",
69 "M_PI2_LOG10El" => "pi/2*log10(e)",
70 "M_PI4_LOG10El" => "pi/4*log10(e)",
71 "M_LOG_SQRT_PIl" => "log(sqrt(pi))",
72 "M_LOG_2_SQRT_PIl" => "log(2*sqrt(pi))",
73 "M_2_SQRT_PIl" => "2 sqrt (pi)",
74 "M_SQRT_PIl" => "sqrt (pi)",
83 # n: generate new ulps file
84 use vars
qw($opt_u $opt_h $opt_o $opt_n);
87 $ulps_file = 'libm-test-ulps';
91 print "Usage: gen-libm-test.pl [OPTIONS]\n";
92 print " -h print this help, then exit\n";
93 print " -o DIR directory where generated files will be placed\n";
94 print " -n only generate sorted file NewUlps from libm-test-ulps\n";
95 print " -u FILE input file with ulps\n";
99 $ulps_file = $opt_u if ($opt_u);
100 $output_dir = $opt_o if ($opt_o);
102 $input = "libm-test.inc";
103 $output = "${output_dir}libm-test.c";
105 &parse_ulps ($ulps_file);
106 &generate_testfile ($input, $output) unless ($opt_n);
107 &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
108 &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
110 # Return a nicer representation
115 if (exists $beautify{$arg}) {
116 return $beautify{$arg};
121 if (exists $beautify{$tmp}) {
122 return '-' . $beautify{$tmp};
125 if ($arg =~ /[0-9]L$/) {
131 # Return a nicer representation of a complex number
132 sub build_complex_beautify {
136 $str1 = &beautify ($r);
137 $str2 = &beautify ($i);
140 $str1 .= ' - ' . $str2;
142 $str1 .= ' + ' . $str2;
148 # Return the text to put in an initializer for a test's exception
150 sub show_exceptions {
151 my ($exception) = @_;
152 if (defined $exception) {
153 return ", $exception";
159 # Parse the arguments to TEST_x_y
161 my ($file, $descr, $fct, $args) = @_;
162 my (@args, $str, $descr_args, $descr_res, @descr);
163 my ($current_arg, $cline, $i);
167 ($descr_args, $descr_res) = split /_/,$descr, 2;
169 @args = split /,\s*/, $args;
173 # Generate first the string that's shown to the user
175 @descr = split //,$descr_args;
176 for ($i = 0; $i <= $#descr; $i++) {
178 if ($current_arg > 1) {
181 # FLOAT, int, long int, long long int
182 if ($descr[$i] =~ /f|i|l|L/) {
183 $call .= $comma . &beautify ($args[$current_arg]);
187 # &FLOAT, &int - simplify call by not showing argument.
188 if ($descr[$i] =~ /F|I/) {
192 if ($descr[$i] eq 'c') {
193 $call .= $comma . &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
198 die ("$descr[$i] is unknown");
204 @descr = split //,$descr_res;
206 if ($_ =~ /f|i|l|L/) {
207 $str .= &beautify ($args[$current_arg]);
209 } elsif ($_ eq 'c') {
210 $str .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
212 } elsif ($_ eq 'b') {
214 $str .= ($args[$current_arg] == 0) ? "false" : "true";
216 } elsif ($_ eq '1') {
219 die ("$_ is unknown");
223 if ($current_arg == $#args) {
224 die ("wrong number of arguments")
225 unless ($args[$current_arg] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN/);
226 } elsif ($current_arg < $#args) {
227 die ("wrong number of arguments");
228 } elsif ($current_arg > ($#args+1)) {
229 die ("wrong number of arguments");
233 # Put the C program line together
234 # Reset some variables to start again
236 $cline = "{ \"$str\"";
237 @descr = split //,$descr_args;
238 for ($i=0; $i <= $#descr; $i++) {
239 # FLOAT, int, long int, long long int
240 if ($descr[$i] =~ /f|i|l|L/) {
241 $cline .= ", $args[$current_arg]";
246 if ($descr[$i] =~ /F|I/) {
250 if ($descr[$i] eq 'c') {
251 $cline .= ", $args[$current_arg], $args[$current_arg+1]";
257 @descr = split //,$descr_res;
259 if ($_ =~ /b|f|i|l|L/ ) {
260 $cline .= ", $args[$current_arg]";
262 } elsif ($_ eq 'c') {
263 $cline .= ", $args[$current_arg], $args[$current_arg+1]";
265 } elsif ($_ eq '1') {
266 push @special, $args[$current_arg];
271 $cline .= show_exceptions (($current_arg <= $#args)
272 ? $args[$current_arg]
275 # special treatment for some functions
279 my ($extra_expected) = $_;
280 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
281 my ($str) = "$call extra output $i";
284 $extra_expected = "0";
286 $cline .= ", \"$str\", $run_extra, $extra_expected";
288 print $file " $cline },\n";
291 # Generate libm-test.c
292 sub generate_testfile {
293 my ($input, $output) = @_;
295 my (@args, $i, $str, $thisfct);
297 open INPUT, $input or die ("Can't open $input: $!");
298 open OUTPUT, ">$output" or die ("Can't open $output: $!");
300 # Replace the special macros
307 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
308 &parse_args (\*OUTPUT, $descr, $thisfct, $args);
311 # START_DATA (function)
313 ($thisfct) = ($_ =~ /START_DATA\s*\((.*)\)/);
318 ($thisfct) = ($_ =~ /START\s*\((.*)\)/);
319 print OUTPUT " init_max_error ();\n";
322 # END_DATA (function)
328 my ($fct, $line, $type);
335 ($fct) = ($_ =~ /END\s*\((.*)\)/);
336 if ($type eq 'complex') {
337 $line = " print_complex_max_error (\"$fct\");\n";
339 $line = " print_max_error (\"$fct\");\n";
342 push @functions, $fct;
356 my ($test, $type, $float, $eps, $kind);
358 # $type has the following values:
359 # "normal": No complex variable
360 # "real": Real part of complex result
361 # "imag": Imaginary part of complex result
362 open ULP, $file or die ("Can't open $file: $!");
365 # ignore comments and empty lines
369 if (/Real part of:/) {
372 } elsif (/Imaginary part of:/) {
373 s/Imaginary part of: //;
378 s/^.+\"(.*)\".*$/$1/;
384 if (/Real part of/) {
387 } elsif (/Imaginary part of/) {
388 s/Imaginary part of //;
393 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
397 if (/^i?(float|double|ldouble):/) {
398 ($float, $eps) = split /\s*:\s*/,$_,2;
404 $results{$test}{$type}{'ulp'}{$float} = $eps;
405 $results{$test}{'has_ulps'} = 1;
407 if ($type =~ /^real|imag$/) {
408 $results{$test}{'type'} = 'complex';
409 } elsif ($type eq 'normal') {
410 $results{$test}{'type'} = 'normal';
412 $results{$test}{'kind'} = $kind;
415 print "Skipping unknown entry: `$_'\n";
421 # Clean up a floating point number
422 sub clean_up_number {
425 # Remove trailing zeros after the decimal point
426 if ($number =~ /\./) {
433 # Output a file which can be read in as ulps file.
434 sub print_ulps_file {
436 my ($test, $type, $float, $eps, $fct, $last_fct);
439 open NEWULP, ">$file" or die ("Can't open $file: $!");
440 print NEWULP "# Begin of automatic generation\n";
441 # first the function calls
442 foreach $test (sort keys %results) {
443 next if ($results{$test}{'kind'} ne 'test');
444 foreach $type ('real', 'imag', 'normal') {
445 if (exists $results{$test}{$type}) {
446 if (defined $results{$test}) {
447 ($fct) = ($test =~ /^(\w+)\s/);
448 if ($fct ne $last_fct) {
450 print NEWULP "\n# $fct\n";
453 if ($type eq 'normal') {
454 print NEWULP "Test \"$test\":\n";
455 } elsif ($type eq 'real') {
456 print NEWULP "Test \"Real part of: $test\":\n";
457 } elsif ($type eq 'imag') {
458 print NEWULP "Test \"Imaginary part of: $test\":\n";
460 foreach $float (@all_floats) {
461 if (exists $results{$test}{$type}{'ulp'}{$float}) {
462 print NEWULP "$float: ",
463 &clean_up_number ($results{$test}{$type}{'ulp'}{$float}),
470 print NEWULP "\n# Maximal error of functions:\n";
472 foreach $fct (sort keys %results) {
473 next if ($results{$fct}{'kind'} ne 'fct');
474 foreach $type ('real', 'imag', 'normal') {
475 if (exists $results{$fct}{$type}) {
476 if ($type eq 'normal') {
477 print NEWULP "Function: \"$fct\":\n";
478 } elsif ($type eq 'real') {
479 print NEWULP "Function: Real part of \"$fct\":\n";
480 } elsif ($type eq 'imag') {
481 print NEWULP "Function: Imaginary part of \"$fct\":\n";
483 foreach $float (@all_floats) {
484 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
485 print NEWULP "$float: ",
486 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
494 print NEWULP "# end of automatic generation\n";
499 my ($test, $type, $float) = @_;
501 return (exists $results{$test}{$type}{'ulp'}{$float}
502 ? $results{$test}{$type}{'ulp'}{$float} : "0");
505 # Return the ulps value for a single test.
506 sub get_all_ulps_for_test {
507 my ($test, $type) = @_;
508 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
510 if (exists $results{$test}{'has_ulps'}) {
511 # XXX use all_floats (change order!)
512 $ldouble = &get_ulps ($test, $type, "ldouble");
513 $double = &get_ulps ($test, $type, "double");
514 $float = &get_ulps ($test, $type, "float");
515 $ildouble = &get_ulps ($test, $type, "ildouble");
516 $idouble = &get_ulps ($test, $type, "idouble");
517 $ifloat = &get_ulps ($test, $type, "ifloat");
518 return "CHOOSE ($ldouble, $double, $float, $ildouble, $idouble, $ifloat)";
520 die "get_all_ulps_for_test called for \"$test\" with no ulps\n";
526 my ($file, $ulps_filename) = @_;
527 my ($i, $fct, $type, $ulp, $ulp_real, $ulp_imag);
528 my (%test_ulps, %func_ulps, %func_real_ulps, %func_imag_ulps);
530 open ULP, ">$file" or die ("Can't open $file: $!");
532 print ULP "/* This file is automatically generated\n";
533 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
534 print ULP " Don't change it - change instead the master files. */\n\n";
536 foreach $fct (keys %results) {
537 $type = $results{$fct}{'type'};
538 if ($type eq 'normal') {
539 $ulp = get_all_ulps_for_test ($fct, 'normal');
540 } elsif ($type eq 'complex') {
541 $ulp_real = get_all_ulps_for_test ($fct, 'real');
542 $ulp_imag = get_all_ulps_for_test ($fct, 'imag');
544 die "unknown results ($fct) type $type\n";
546 if ($results{$fct}{'kind'} eq 'fct') {
547 if ($type eq 'normal') {
548 $func_ulps{$fct} = $ulp;
550 $func_real_ulps{$fct} = $ulp_real;
551 $func_imag_ulps{$fct} = $ulp_imag;
553 } elsif ($results{$fct}{'kind'} eq 'test') {
554 if ($type eq 'normal') {
555 $test_ulps{$fct} = $ulp;
557 $test_ulps{"Real part of: $fct"} = $ulp_real;
558 $test_ulps{"Imaginary part of: $fct"} = $ulp_imag;
561 die "unknown results ($fct) kind $results{$fct}{'kind'}\n";
564 print ULP "\n/* Maximal error of functions. */\n";
565 print ULP "static const struct ulp_data func_ulps[] =\n {\n";
566 foreach $fct (sort keys %func_ulps) {
567 print ULP " { \"$fct\", $func_ulps{$fct} },\n";
570 print ULP "static const struct ulp_data func_real_ulps[] =\n {\n";
571 foreach $fct (sort keys %func_real_ulps) {
572 print ULP " { \"$fct\", $func_real_ulps{$fct} },\n";
575 print ULP "static const struct ulp_data func_imag_ulps[] =\n {\n";
576 foreach $fct (sort keys %func_imag_ulps) {
577 print ULP " { \"$fct\", $func_imag_ulps{$fct} },\n";
581 print ULP "\n/* Error of single function calls. */\n";
582 print ULP "static const struct ulp_data test_ulps[] =\n {\n";
583 foreach $fct (sort keys %test_ulps) {
584 print ULP " { \"$fct\", $test_ulps{$fct} },\n";