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",
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.");
206 print $file " FUNC (sincos) ($args[1], &sin_res, &cos_res);\n";
208 $str = 'sincos (' . &beautify ($args[1]) . ', &sin_res, &cos_res)';
210 $test = $str . ' puts ' . &beautify ($args[2]) . ' in sin_res';
212 $cline = " check_float (\"$test\", sin_res, $args[2]";
213 $cline .= &new_test ($test, $args[4]);
217 $test = $str . ' puts ' . &beautify ($args[3]) . ' in cos_res';
218 $cline = " check_float (\"$test\", cos_res, $args[3]";
219 # only tests once for exception
220 $cline .= &new_test ($test, undef);
224 # Parse the arguments to TEST_x_y
226 my ($file, $descr, $fct, $args) = @_;
227 my (@args, $str, $descr_args, $descr_res, @descr);
228 my ($current_arg, $cline, $i);
229 my ($pre, $post, @special);
230 my ($extra_var, $call, $c_call);
232 if ($descr eq 'extra') {
233 &special_functions ($file, $args);
236 ($descr_args, $descr_res) = split /_/,$descr, 2;
238 @args = split /,\s*/, $args;
242 # Generate first the string that's shown to the user
245 @descr = split //,$descr_args;
246 for ($i = 0; $i <= $#descr; $i++) {
250 # FLOAT, int, long int, long long int
251 if ($descr[$i] =~ /f|i|l|L/) {
252 $call .= &beautify ($args[$current_arg]);
256 # &FLOAT, &int - argument is added here
257 if ($descr[$i] =~ /F|I/) {
259 $call .= '&' . &get_variable ($extra_var);
263 if ($descr[$i] eq 'c') {
264 $call .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
269 die ("$descr[$i] is unknown");
275 @descr = split //,$descr_res;
277 if ($_ =~ /f|i|l|L/) {
278 $str .= &beautify ($args[$current_arg]);
280 } elsif ($_ eq 'c') {
281 $str .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
283 } elsif ($_ eq 'b') {
285 $str .= ($args[$current_arg] == 0) ? "false" : "true";
287 } elsif ($_ eq '1') {
290 die ("$_ is unknown");
294 if ($current_arg == $#args) {
295 die ("wrong number of arguments")
296 unless ($args[$current_arg] =~ /EXCEPTION|IGNORE_ZERO_INF_SIGN/);
297 } elsif ($current_arg < $#args) {
298 die ("wrong number of arguments");
299 } elsif ($current_arg > ($#args+1)) {
300 die ("wrong number of arguments");
304 # Put the C program line together
305 # Reset some variables to start again
308 if (substr($descr_res,0,1) eq 'f') {
309 $cline = 'check_float'
310 } elsif (substr($descr_res,0,1) eq 'b') {
311 $cline = 'check_bool';
312 } elsif (substr($descr_res,0,1) eq 'c') {
313 $cline = 'check_complex';
314 } elsif (substr($descr_res,0,1) eq 'i') {
315 $cline = 'check_int';
316 } elsif (substr($descr_res,0,1) eq 'l') {
317 $cline = 'check_long';
318 } elsif (substr($descr_res,0,1) eq 'L') {
319 $cline = 'check_longlong';
321 # Special handling for some macros:
322 $cline .= " (\"$str\", ";
323 if ($args[0] =~ /fpclassify|isnormal|isfinite|isinf|isnan|signbit
324 |isgreater|isgreaterequal|isless|islessequal
325 |islessgreater|isunordered/x) {
326 $c_call = "$args[0] (";
328 $c_call = " FUNC($args[0]) (";
330 @descr = split //,$descr_args;
331 for ($i=0; $i <= $#descr; $i++) {
335 # FLOAT, int, long int, long long int
336 if ($descr[$i] =~ /f|i|l|L/) {
337 $c_call .= $args[$current_arg];
342 if ($descr[$i] =~ /F|I/) {
344 $c_call .= '&' . &get_variable ($extra_var);
348 if ($descr[$i] eq 'c') {
349 $c_call .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
355 $cline .= "$c_call, ";
357 @descr = split //,$descr_res;
359 if ($_ =~ /b|f|i|l|L/ ) {
360 $cline .= $args[$current_arg];
362 } elsif ($_ eq 'c') {
363 $cline .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
365 } elsif ($_ eq '1') {
366 push @special, $args[$current_arg];
371 $cline .= &new_test ($str, ($current_arg <= $#args) ? $args[$current_arg] : undef);
373 # special treatment for some functions
374 if ($args[0] eq 'frexp') {
375 if (defined $special[0] && $special[0] ne "IGNORE") {
376 my ($str) = "$call sets x to $special[0]";
377 $post = " check_int (\"$str\", x, $special[0]";
378 $post .= &new_test ($str, undef);
380 } elsif ($args[0] eq 'gamma' || $args[0] eq 'lgamma') {
381 $pre = " signgam = 0;\n";
382 if (defined $special[0] && $special[0] ne "IGNORE") {
383 my ($str) = "$call sets signgam to $special[0]";
384 $post = " check_int (\"$str\", signgam, $special[0]";
385 $post .= &new_test ($str, undef);
387 } elsif ($args[0] eq 'modf') {
388 if (defined $special[0] && $special[0] ne "IGNORE") {
389 my ($str) = "$call sets x to $special[0]";
390 $post = " check_float (\"$str\", x, $special[0]";
391 $post .= &new_test ($str, undef);
393 } elsif ($args[0] eq 'remquo') {
394 if (defined $special[0] && $special[0] ne "IGNORE") {
395 my ($str) = "$call sets x to $special[0]";
396 $post = " check_int (\"$str\", x, $special[0]";
397 $post .= &new_test ($str, undef);
401 print $file $pre if (defined $pre);
403 print $file " $cline";
405 print $file $post if (defined $post);
408 # Generate libm-test.c
409 sub generate_testfile {
410 my ($input, $output) = @_;
412 my (@args, $i, $str, $thisfct);
414 open INPUT, $input or die ("Can't open $input: $!");
415 open OUTPUT, ">$output" or die ("Can't open $output: $!");
417 # Replace the special macros
424 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
425 &parse_args (\*OUTPUT, $descr, $thisfct, $args);
430 ($thisfct) = ($_ =~ /START\s*\((.*)\)/);
431 print OUTPUT " init_max_error ();\n";
436 my ($fct, $line, $type);
443 ($fct) = ($_ =~ /END\s*\((.*)\)/);
444 if ($type eq 'complex') {
445 $line = " print_complex_max_error (\"$fct\", ";
447 $line = " print_max_error (\"$fct\", ";
449 if (exists $results{$fct}{'has_ulps'}) {
450 $line .= "DELTA$fct";
454 if (exists $results{$fct}{'has_fails'}) {
455 $line .= ", FAIL$fct";
461 push @functions, $fct;
475 my ($test, $type, $float, $eps, $kind);
477 # $type has the following values:
478 # "normal": No complex variable
479 # "real": Real part of complex result
480 # "imag": Imaginary part of complex result
481 open ULP, $file or die ("Can't open $file: $!");
484 # ignore comments and empty lines
488 if (/Real part of:/) {
491 } elsif (/Imaginary part of:/) {
492 s/Imaginary part of: //;
497 s/^.+\"(.*)\".*$/$1/;
503 if (/Real part of/) {
506 } elsif (/Imaginary part of/) {
507 s/Imaginary part of //;
512 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
516 if (/^i?(float|double|ldouble):/) {
517 ($float, $eps) = split /\s*:\s*/,$_,2;
519 if ($eps eq 'fail') {
520 $results{$test}{$type}{'fail'}{$float} = 1;
521 $results{$test}{'has_fails'} = 1;
522 } elsif ($eps eq "0") {
526 $results{$test}{$type}{'ulp'}{$float} = $eps;
527 $results{$test}{'has_ulps'} = 1;
529 if ($type =~ /^real|imag$/) {
530 $results{$test}{'type'} = 'complex';
531 } elsif ($type eq 'normal') {
532 $results{$test}{'type'} = 'normal';
534 $results{$test}{'kind'} = $kind;
537 print "Skipping unknown entry: `$_'\n";
543 # Clean up a floating point number
544 sub clean_up_number {
547 # Remove trailing zeros after the decimal point
548 if ($number =~ /\./) {
555 # Output a file which can be read in as ulps file.
556 sub print_ulps_file {
558 my ($test, $type, $float, $eps, $fct, $last_fct);
561 open NEWULP, ">$file" or die ("Can't open $file: $!");
562 print NEWULP "# Begin of automatic generation\n";
563 # first the function calls
564 foreach $test (sort keys %results) {
565 next if ($results{$test}{'kind'} ne 'test');
566 foreach $type ('real', 'imag', 'normal') {
567 if (exists $results{$test}{$type}) {
568 if (defined $results{$test}) {
569 ($fct) = ($test =~ /^(\w+)\s/);
570 if ($fct ne $last_fct) {
572 print NEWULP "\n# $fct\n";
575 if ($type eq 'normal') {
576 print NEWULP "Test \"$test\":\n";
577 } elsif ($type eq 'real') {
578 print NEWULP "Test \"Real part of: $test\":\n";
579 } elsif ($type eq 'imag') {
580 print NEWULP "Test \"Imaginary part of: $test\":\n";
582 foreach $float (@all_floats) {
583 if (exists $results{$test}{$type}{'ulp'}{$float}) {
584 print NEWULP "$float: ",
585 &clean_up_number ($results{$test}{$type}{'ulp'}{$float}),
588 if (exists $results{$test}{$type}{'fail'}{$float}) {
589 print NEWULP "$float: fail\n";
595 print NEWULP "\n# Maximal error of functions:\n";
597 foreach $fct (sort keys %results) {
598 next if ($results{$fct}{'kind'} ne 'fct');
599 foreach $type ('real', 'imag', 'normal') {
600 if (exists $results{$fct}{$type}) {
601 if ($type eq 'normal') {
602 print NEWULP "Function: \"$fct\":\n";
603 } elsif ($type eq 'real') {
604 print NEWULP "Function: Real part of \"$fct\":\n";
605 } elsif ($type eq 'imag') {
606 print NEWULP "Function: Imaginary part of \"$fct\":\n";
608 foreach $float (@all_floats) {
609 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
610 print NEWULP "$float: ",
611 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
614 if (exists $results{$fct}{$type}{'fail'}{$float}) {
615 print NEWULP "$float: fail\n";
622 print NEWULP "# end of automatic generation\n";
627 my ($test, $type, $float) = @_;
629 if ($type eq 'complex') {
631 # Return 0 instead of BUILD_COMPLEX (0,0)
632 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
633 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
636 $res = 'BUILD_COMPLEX (';
637 $res .= (exists $results{$test}{'real'}{'ulp'}{$float}
638 ? $results{$test}{'real'}{'ulp'}{$float} : "0");
640 $res .= (exists $results{$test}{'imag'}{'ulp'}{$float}
641 ? $results{$test}{'imag'}{'ulp'}{$float} : "0");
645 return (exists $results{$test}{'normal'}{'ulp'}{$float}
646 ? $results{$test}{'normal'}{'ulp'}{$float} : "0");
650 my ($test, $type, $float) = @_;
651 if ($type eq 'complex') {
654 # Return 0 instead of BUILD_COMPLEX_INT (0,0)
655 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
656 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
659 $res = 'BUILD_COMPLEX_INT (';
660 $res .= (exists $results{$test}{'real'}{'fail'}{$float}
661 ? $results{$test}{'real'}{'fail'}{$float} : "0");
663 $res .= (exists $results{$test}{'imag'}{'fail'}{$float}
664 ? $results{$test}{'imag'}{'fail'}{$float} : "0");
668 return (exists $results{$test}{'normal'}{'fail'}{$float}
669 ? $results{$test}{'normal'}{'fail'}{$float} : "0");
673 # Output the defines for a single test
675 my ($file, $test, $name) = @_;
676 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
679 # Do we have ulps/failures?
680 if (!exists $results{$test}{'type'}) {
683 $type = $results{$test}{'type'};
684 if (exists $results{$test}{'has_ulps'}) {
685 # XXX use all_floats (change order!)
686 $ldouble = &get_ulps ($test, $type, "ldouble");
687 $double = &get_ulps ($test, $type, "double");
688 $float = &get_ulps ($test, $type, "float");
689 $ildouble = &get_ulps ($test, $type, "ildouble");
690 $idouble = &get_ulps ($test, $type, "idouble");
691 $ifloat = &get_ulps ($test, $type, "ifloat");
692 print $file "#define DELTA$name CHOOSE($ldouble, $double, $float, $ildouble, $idouble, $ifloat)\t/* $test */\n";
695 if (exists $results{$test}{'has_fails'}) {
696 $ldouble = &get_failure ($test, "ldouble");
697 $double = &get_failure ($test, "double");
698 $float = &get_failure ($test, "float");
699 $ildouble = &get_failure ($test, "ildouble");
700 $idouble = &get_failure ($test, "idouble");
701 $ifloat = &get_failure ($test, "ifloat");
702 print $file "#define FAIL$name CHOOSE($ldouble, $double, $float $ildouble, $idouble, $ifloat)\t/* $test */\n";
708 my ($file, $ulps_filename) = @_;
711 open ULP, ">$file" or die ("Can't open $file: $!");
713 print ULP "/* This file is automatically generated\n";
714 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
715 print ULP " Don't change it - change instead the master files. */\n\n";
717 print ULP "\n/* Maximal error of functions. */\n";
718 foreach $fct (@functions) {
719 output_test (\*ULP, $fct, $fct);
722 print ULP "\n/* Error of single function calls. */\n";
723 for ($i = 0; $i < $count; $i++) {
724 output_test (\*ULP, $tests[$i], $i);