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 # $results{$test}{"has_fails"} is set if exptected failures exist.
29 # In the following description $type and $float are:
30 # - $type is either "normal", "real" (for the real part of a complex number)
31 # or "imag" (for the imaginary part # of a complex number).
32 # - $float is either of float, ifloat, double, idouble, ldouble, ildouble;
33 # It represents the underlying floating point type (float, double or long
34 # double) and if inline functions (the leading i stands for inline)
36 # $results{$test}{$type}{"fail"}{$float} is defined and has a 1 if
37 # the test is expected to fail
38 # $results{$test}{$type}{"ulp"}{$float} is defined and has a delta as value
45 use vars qw
($input $output);
46 use vars qw
(%results);
47 use vars qw
(@tests @functions);
49 use vars qw
(%beautify @all_floats);
50 use vars qw
($output_dir $ulps_file);
52 # all_floats is sorted and contains all recognised float types
53 @all_floats = ('double', 'float', 'idouble',
54 'ifloat', 'ildouble', 'ldouble');
57 ( "minus_zero" => "-0",
59 "minus_infty" => "-inf",
60 "plus_infty" => "inf",
61 "qnan_value" => "qNaN",
65 "M_LOG10El", "log10(e)",
67 "M_PI_34l" => "3/4 pi",
71 "M_PI_34_LOG10El" => "3/4 pi*log10(e)",
72 "M_PI_LOG10El" => "pi*log10(e)",
73 "M_PI2_LOG10El" => "pi/2*log10(e)",
74 "M_PI4_LOG10El" => "pi/4*log10(e)",
75 "M_LOG_SQRT_PIl" => "log(sqrt(pi))",
76 "M_LOG_2_SQRT_PIl" => "log(2*sqrt(pi))",
77 "M_2_SQRT_PIl" => "2 sqrt (pi)",
78 "M_SQRT_PIl" => "sqrt (pi)",
87 # n: generate new ulps file
88 use vars
qw($opt_u $opt_h $opt_o $opt_n);
91 $ulps_file = 'libm-test-ulps';
95 print "Usage: gen-libm-test.pl [OPTIONS]\n";
96 print " -h print this help, then exit\n";
97 print " -o DIR directory where generated files will be placed\n";
98 print " -n only generate sorted file NewUlps from libm-test-ulps\n";
99 print " -u FILE input file with ulps\n";
103 $ulps_file = $opt_u if ($opt_u);
104 $output_dir = $opt_o if ($opt_o);
106 $input = "libm-test.inc";
107 $output = "${output_dir}libm-test.c";
111 &parse_ulps ($ulps_file);
112 &generate_testfile ($input, $output) unless ($opt_n);
113 &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
114 &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
116 # Return a nicer representation
121 if (exists $beautify{$arg}) {
122 return $beautify{$arg};
127 if (exists $beautify{$tmp}) {
128 return '-' . $beautify{$tmp};
131 if ($arg =~ /[0-9]L$/) {
137 # Return a nicer representation of a complex number
138 sub build_complex_beautify {
142 $str1 = &beautify ($r);
143 $str2 = &beautify ($i);
146 $str1 .= ' - ' . $str2;
148 $str1 .= ' + ' . $str2;
154 # Return name of a variable
158 return "x" if ($number == 1);
159 return "y" if ($number == 2);
160 return "z" if ($number == 3);
166 # Add a new test to internal data structures and fill in the
167 # ulps, failures and exception information for the C line.
169 my ($test, $exception) = @_;
173 if (exists $results{$test}{'has_ulps'}) {
174 $rest = ", DELTA$count";
178 if (exists $results{$test}{'has_fails'}) {
179 $rest .= ", FAIL$count";
183 if (defined $exception) {
184 $rest .= ", $exception";
189 # We must increment here to keep @tests and count in sync
195 # Treat some functions especially.
196 # Currently only sincos needs extra treatment.
197 sub special_functions {
198 my ($file, $args) = @_;
199 my (@args, $str, $test, $cline);
201 @args = split /,\s*/, $args;
203 unless ($args[0] =~ /sincos/) {
204 die ("Don't know how to handle $args[0] extra.");
207 print $file " FUNC (sincos) ($args[1], &sin_res, &cos_res);\n";
209 $str = 'sincos (' . &beautify ($args[1]) . ', &sin_res, &cos_res)';
211 $test = $str . ' puts ' . &beautify ($args[2]) . ' in sin_res';
213 $cline = " check_float (\"$test\", sin_res, $args[2]";
214 $cline .= &new_test ($test, $args[4]);
218 $test = $str . ' puts ' . &beautify ($args[3]) . ' in cos_res';
219 $cline = " check_float (\"$test\", cos_res, $args[3]";
220 # only tests once for exception
221 $cline .= &new_test ($test, undef);
226 # Parse the arguments to TEST_x_y
228 my ($file, $descr, $fct, $args) = @_;
229 my (@args, $str, $descr_args, $descr_res, @descr);
230 my ($current_arg, $cline, $i);
231 my ($pre, $post, @special);
232 my ($extra_var, $call, $c_call);
234 if ($descr eq 'extra') {
235 &special_functions ($file, $args);
238 ($descr_args, $descr_res) = split /_/,$descr, 2;
240 @args = split /,\s*/, $args;
244 # Generate first the string that's shown to the user
247 @descr = split //,$descr_args;
248 for ($i = 0; $i <= $#descr; $i++) {
252 # FLOAT, int, long int, long long int
253 if ($descr[$i] =~ /f|i|l|L/) {
254 $call .= &beautify ($args[$current_arg]);
258 # &FLOAT, &int - argument is added here
259 if ($descr[$i] =~ /F|I/) {
261 $call .= '&' . &get_variable ($extra_var);
265 if ($descr[$i] eq 'c') {
266 $call .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
271 die ("$descr[$i] is unknown");
277 @descr = split //,$descr_res;
279 if ($_ =~ /f|i|l|L/) {
280 $str .= &beautify ($args[$current_arg]);
282 } elsif ($_ eq 'c') {
283 $str .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
285 } elsif ($_ eq 'b') {
287 $str .= ($args[$current_arg] == 0) ? "false" : "true";
289 } elsif ($_ eq '1') {
292 die ("$_ is unknown");
296 if ($current_arg == $#args) {
297 die ("wrong number of arguments")
298 unless ($args[$current_arg] =~ /EXCEPTION|IGNORE_ZERO_INF_SIGN/);
299 } elsif ($current_arg < $#args) {
300 die ("wrong number of arguments");
301 } elsif ($current_arg > ($#args+1)) {
302 die ("wrong number of arguments");
306 # Put the C program line together
307 # Reset some variables to start again
310 if (substr($descr_res,0,1) eq 'f') {
311 $cline = 'check_float'
312 } elsif (substr($descr_res,0,1) eq 'b') {
313 $cline = 'check_bool';
314 } elsif (substr($descr_res,0,1) eq 'c') {
315 $cline = 'check_complex';
316 } elsif (substr($descr_res,0,1) eq 'i') {
317 $cline = 'check_int';
318 } elsif (substr($descr_res,0,1) eq 'l') {
319 $cline = 'check_long';
320 } elsif (substr($descr_res,0,1) eq 'L') {
321 $cline = 'check_longlong';
323 # Special handling for some macros:
324 $cline .= " (\"$str\", ";
325 if ($args[0] =~ /fpclassify|isnormal|isfinite|isinf|isnan|issignaling|signbit
326 |isgreater|isgreaterequal|isless|islessequal
327 |islessgreater|isunordered/x) {
328 $c_call = "$args[0] (";
330 $c_call = " FUNC($args[0]) (";
332 @descr = split //,$descr_args;
333 for ($i=0; $i <= $#descr; $i++) {
337 # FLOAT, int, long int, long long int
338 if ($descr[$i] =~ /f|i|l|L/) {
339 $c_call .= $args[$current_arg];
344 if ($descr[$i] =~ /F|I/) {
346 $c_call .= '&' . &get_variable ($extra_var);
350 if ($descr[$i] eq 'c') {
351 $c_call .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
357 $cline .= "$c_call, ";
359 @descr = split //,$descr_res;
361 if ($_ =~ /b|f|i|l|L/ ) {
362 $cline .= $args[$current_arg];
364 } elsif ($_ eq 'c') {
365 $cline .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
367 } elsif ($_ eq '1') {
368 push @special, $args[$current_arg];
373 $cline .= &new_test ($str, ($current_arg <= $#args) ? $args[$current_arg] : undef);
375 # special treatment for some functions
376 if ($args[0] eq 'frexp') {
377 if (defined $special[0] && $special[0] ne "IGNORE") {
378 my ($str) = "$call sets x to $special[0]";
379 $post = " check_int (\"$str\", x, $special[0]";
380 $post .= &new_test ($str, undef);
382 } elsif ($args[0] eq 'gamma' || $args[0] eq 'lgamma') {
383 $pre = " signgam = 0;\n";
384 if (defined $special[0] && $special[0] ne "IGNORE") {
385 my ($str) = "$call sets signgam to $special[0]";
386 $post = " check_int (\"$str\", signgam, $special[0]";
387 $post .= &new_test ($str, undef);
389 } elsif ($args[0] eq 'modf') {
390 if (defined $special[0] && $special[0] ne "IGNORE") {
391 my ($str) = "$call sets x to $special[0]";
392 $post = " check_float (\"$str\", x, $special[0]";
393 $post .= &new_test ($str, undef);
395 } elsif ($args[0] eq 'remquo') {
396 if (defined $special[0] && $special[0] ne "IGNORE") {
397 my ($str) = "$call sets x to $special[0]";
398 $post = " check_int (\"$str\", x, $special[0]";
399 $post .= &new_test ($str, undef);
403 if (defined $pre or defined $post) {
405 print $file " $pre" if (defined $pre);
406 print $file " $cline";
407 print $file " $post" if (defined $post);
410 print $file " $cline";
414 # Generate libm-test.c
415 sub generate_testfile {
416 my ($input, $output) = @_;
418 my (@args, $i, $str, $thisfct);
420 open INPUT, $input or die ("Can't open $input: $!");
421 open OUTPUT, ">$output" or die ("Can't open $output: $!");
423 # Replace the special macros
430 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
431 &parse_args (\*OUTPUT, $descr, $thisfct, $args);
436 ($thisfct) = ($_ =~ /START\s*\((.*)\)/);
437 print OUTPUT " init_max_error ();\n";
442 my ($fct, $line, $type);
449 ($fct) = ($_ =~ /END\s*\((.*)\)/);
450 if ($type eq 'complex') {
451 $line = " print_complex_max_error (\"$fct\", ";
453 $line = " print_max_error (\"$fct\", ";
455 if (exists $results{$fct}{'has_ulps'}) {
456 $line .= "DELTA$fct";
460 if (exists $results{$fct}{'has_fails'}) {
461 $line .= ", FAIL$fct";
467 push @functions, $fct;
481 my ($test, $type, $float, $eps, $kind);
483 # $type has the following values:
484 # "normal": No complex variable
485 # "real": Real part of complex result
486 # "imag": Imaginary part of complex result
487 open ULP, $file or die ("Can't open $file: $!");
490 # ignore comments and empty lines
494 if (/Real part of:/) {
497 } elsif (/Imaginary part of:/) {
498 s/Imaginary part of: //;
503 s/^.+\"(.*)\".*$/$1/;
509 if (/Real part of/) {
512 } elsif (/Imaginary part of/) {
513 s/Imaginary part of //;
518 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
522 if (/^i?(float|double|ldouble):/) {
523 ($float, $eps) = split /\s*:\s*/,$_,2;
525 if ($eps eq 'fail') {
526 $results{$test}{$type}{'fail'}{$float} = 1;
527 $results{$test}{'has_fails'} = 1;
528 } elsif ($eps eq "0") {
532 $results{$test}{$type}{'ulp'}{$float} = $eps;
533 $results{$test}{'has_ulps'} = 1;
535 if ($type =~ /^real|imag$/) {
536 $results{$test}{'type'} = 'complex';
537 } elsif ($type eq 'normal') {
538 $results{$test}{'type'} = 'normal';
540 $results{$test}{'kind'} = $kind;
543 print "Skipping unknown entry: `$_'\n";
549 # Clean up a floating point number
550 sub clean_up_number {
553 # Remove trailing zeros after the decimal point
554 if ($number =~ /\./) {
561 # Output a file which can be read in as ulps file.
562 sub print_ulps_file {
564 my ($test, $type, $float, $eps, $fct, $last_fct);
567 open NEWULP, ">$file" or die ("Can't open $file: $!");
568 print NEWULP "# Begin of automatic generation\n";
569 # first the function calls
570 foreach $test (sort keys %results) {
571 next if ($results{$test}{'kind'} ne 'test');
572 foreach $type ('real', 'imag', 'normal') {
573 if (exists $results{$test}{$type}) {
574 if (defined $results{$test}) {
575 ($fct) = ($test =~ /^(\w+)\s/);
576 if ($fct ne $last_fct) {
578 print NEWULP "\n# $fct\n";
581 if ($type eq 'normal') {
582 print NEWULP "Test \"$test\":\n";
583 } elsif ($type eq 'real') {
584 print NEWULP "Test \"Real part of: $test\":\n";
585 } elsif ($type eq 'imag') {
586 print NEWULP "Test \"Imaginary part of: $test\":\n";
588 foreach $float (@all_floats) {
589 if (exists $results{$test}{$type}{'ulp'}{$float}) {
590 print NEWULP "$float: ",
591 &clean_up_number ($results{$test}{$type}{'ulp'}{$float}),
594 if (exists $results{$test}{$type}{'fail'}{$float}) {
595 print NEWULP "$float: fail\n";
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}),
620 if (exists $results{$fct}{$type}{'fail'}{$float}) {
621 print NEWULP "$float: fail\n";
628 print NEWULP "# end of automatic generation\n";
633 my ($test, $type, $float) = @_;
635 if ($type eq 'complex') {
637 # Return 0 instead of BUILD_COMPLEX (0,0)
638 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
639 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
642 $res = 'BUILD_COMPLEX (';
643 $res .= (exists $results{$test}{'real'}{'ulp'}{$float}
644 ? $results{$test}{'real'}{'ulp'}{$float} : "0");
646 $res .= (exists $results{$test}{'imag'}{'ulp'}{$float}
647 ? $results{$test}{'imag'}{'ulp'}{$float} : "0");
651 return (exists $results{$test}{'normal'}{'ulp'}{$float}
652 ? $results{$test}{'normal'}{'ulp'}{$float} : "0");
656 my ($test, $type, $float) = @_;
657 if ($type eq 'complex') {
660 # Return 0 instead of BUILD_COMPLEX_INT (0,0)
661 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
662 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
665 $res = 'BUILD_COMPLEX_INT (';
666 $res .= (exists $results{$test}{'real'}{'fail'}{$float}
667 ? $results{$test}{'real'}{'fail'}{$float} : "0");
669 $res .= (exists $results{$test}{'imag'}{'fail'}{$float}
670 ? $results{$test}{'imag'}{'fail'}{$float} : "0");
674 return (exists $results{$test}{'normal'}{'fail'}{$float}
675 ? $results{$test}{'normal'}{'fail'}{$float} : "0");
679 # Output the defines for a single test
681 my ($file, $test, $name) = @_;
682 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
685 # Do we have ulps/failures?
686 if (!exists $results{$test}{'type'}) {
689 $type = $results{$test}{'type'};
690 if (exists $results{$test}{'has_ulps'}) {
691 # XXX use all_floats (change order!)
692 $ldouble = &get_ulps ($test, $type, "ldouble");
693 $double = &get_ulps ($test, $type, "double");
694 $float = &get_ulps ($test, $type, "float");
695 $ildouble = &get_ulps ($test, $type, "ildouble");
696 $idouble = &get_ulps ($test, $type, "idouble");
697 $ifloat = &get_ulps ($test, $type, "ifloat");
698 print $file "#define DELTA$name CHOOSE($ldouble, $double, $float, $ildouble, $idouble, $ifloat)\t/* $test */\n";
701 if (exists $results{$test}{'has_fails'}) {
702 $ldouble = &get_failure ($test, "ldouble");
703 $double = &get_failure ($test, "double");
704 $float = &get_failure ($test, "float");
705 $ildouble = &get_failure ($test, "ildouble");
706 $idouble = &get_failure ($test, "idouble");
707 $ifloat = &get_failure ($test, "ifloat");
708 print $file "#define FAIL$name CHOOSE($ldouble, $double, $float $ildouble, $idouble, $ifloat)\t/* $test */\n";
714 my ($file, $ulps_filename) = @_;
717 open ULP, ">$file" or die ("Can't open $file: $!");
719 print ULP "/* This file is automatically generated\n";
720 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
721 print ULP " Don't change it - change instead the master files. */\n\n";
723 print ULP "\n/* Maximal error of functions. */\n";
724 foreach $fct (@functions) {
725 output_test (\*ULP, $fct, $fct);
728 print ULP "\n/* Error of single function calls. */\n";
729 for ($i = 0; $i < $count; $i++) {
730 output_test (\*ULP, $tests[$i], $i);