2 # Copyright (C) 1999 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; see the file COPYING.LIB. If
18 # not, see <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)",
79 "INVALID_EXCEPTION" => "invalid exception",
80 "DIVIDE_BY_ZERO_EXCEPTION" => "division by zero exception",
81 "INVALID_EXCEPTION_OK" => "invalid exception allowed",
82 "DIVIDE_BY_ZERO_EXCEPTION_OK" => "division by zero exception allowed",
83 "EXCEPTIONS_OK" => "exceptions allowed",
84 "IGNORE_ZERO_INF_SIGN" => "sign of zero/inf not specified",
85 "INVALID_EXCEPTION|IGNORE_ZERO_INF_SIGN" => "invalid exception and sign of zero/inf not specified"
94 # n: generate new ulps file
95 use vars
qw($opt_u $opt_h $opt_o $opt_n);
98 $ulps_file = 'libm-test-ulps';
102 print "Usage: gen-libm-test.pl [OPTIONS]\n";
103 print " -h print this help, then exit\n";
104 print " -o DIR directory where generated files will be placed\n";
105 print " -n only generate sorted file NewUlps from libm-test-ulps\n";
106 print " -u FILE input file with ulps\n";
110 $ulps_file = $opt_u if ($opt_u);
111 $output_dir = $opt_o if ($opt_o);
113 $input = "libm-test.inc";
114 $output = "${output_dir}libm-test.c";
118 &parse_ulps ($ulps_file);
119 &generate_testfile ($input, $output) unless ($opt_n);
120 &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
121 &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
123 # Return a nicer representation
128 if (exists $beautify{$arg}) {
129 return $beautify{$arg};
134 if (exists $beautify{$tmp}) {
135 return '-' . $beautify{$tmp};
138 if ($arg =~ /[0-9]L$/) {
144 # Return a nicer representation of a complex number
145 sub build_complex_beautify {
149 $str1 = &beautify ($r);
150 $str2 = &beautify ($i);
153 $str1 .= ' - ' . $str2;
155 $str1 .= ' + ' . $str2;
161 # Return name of a variable
165 return "x" if ($number == 1);
166 return "y" if ($number == 2);
167 return "z" if ($number == 3);
173 # Add a new test to internal data structures and fill in the
174 # ulps, failures and exception information for the C line.
176 my ($test, $exception) = @_;
180 if (exists $results{$test}{'has_ulps'}) {
181 $rest = ", DELTA$count";
185 if (exists $results{$test}{'has_fails'}) {
186 $rest .= ", FAIL$count";
190 if (defined $exception) {
191 $rest .= ", $exception";
196 # We must increment here to keep @tests and count in sync
202 # Treat some functions especially.
203 # Currently only sincos needs extra treatment.
204 sub special_functions {
205 my ($file, $args) = @_;
206 my (@args, $str, $test, $cline);
208 @args = split /,\s*/, $args;
210 unless ($args[0] =~ /sincos/) {
211 die ("Don't know how to handle $args[0] extra.");
213 print $file " FUNC (sincos) ($args[1], &sin_res, &cos_res);\n";
215 $str = 'sincos (' . &beautify ($args[1]) . ', &sin_res, &cos_res)';
217 $test = $str . ' puts ' . &beautify ($args[2]) . ' in sin_res';
219 $test .= " plus " . &beautify ($args[4]);
222 $cline = " check_float (\"$test\", sin_res, $args[2]";
223 $cline .= &new_test ($test, $args[4]);
227 $test = $str . ' puts ' . &beautify ($args[3]) . ' in cos_res';
228 $cline = " check_float (\"$test\", cos_res, $args[3]";
229 # only tests once for exception
230 $cline .= &new_test ($test, undef);
234 # Parse the arguments to TEST_x_y
236 my ($file, $descr, $args) = @_;
237 my (@args, $str, $descr_args, $descr_res, @descr);
238 my ($current_arg, $cline, $i);
239 my ($pre, $post, @special);
240 my ($extra_var, $call, $c_call);
242 if ($descr eq 'extra') {
243 &special_functions ($file, $args);
246 ($descr_args, $descr_res) = split /_/,$descr, 2;
248 @args = split /,\s*/, $args;
250 $call = "$args[0] (";
252 # Generate first the string that's shown to the user
255 @descr = split //,$descr_args;
256 for ($i = 0; $i <= $#descr; $i++) {
260 # FLOAT, int, long int, long long int
261 if ($descr[$i] =~ /f|i|l|L/) {
262 $call .= &beautify ($args[$current_arg]);
266 # &FLOAT, &int - argument is added here
267 if ($descr[$i] =~ /F|I/) {
269 $call .= '&' . &get_variable ($extra_var);
273 if ($descr[$i] eq 'c') {
274 $call .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
279 die ("$descr[$i] is unknown");
285 @descr = split //,$descr_res;
287 if ($_ =~ /f|i|l|L/) {
288 $str .= &beautify ($args[$current_arg]);
290 } elsif ($_ eq 'c') {
291 $str .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
293 } elsif ($_ eq 'b') {
295 $str .= ($args[$current_arg] == 0) ? "false" : "true";
297 } elsif ($_ eq '1') {
300 die ("$_ is unknown");
304 if ($current_arg == $#args) {
305 die ("wrong number of arguments")
306 unless ($args[$current_arg] =~ /EXCEPTION|IGNORE_ZERO_INF_SIGN/);
307 } elsif ($current_arg < $#args) {
308 die ("wrong number of arguments");
309 } elsif ($current_arg > ($#args+1)) {
310 die ("wrong number of arguments");
314 # check for exceptions
315 if ($current_arg <= $#args) {
316 $str .= " plus " . &beautify ($args[$current_arg]);
319 # Put the C program line together
320 # Reset some variables to start again
323 if (substr($descr_res,0,1) eq 'f') {
324 $cline = 'check_float'
325 } elsif (substr($descr_res,0,1) eq 'b') {
326 $cline = 'check_bool';
327 } elsif (substr($descr_res,0,1) eq 'c') {
328 $cline = 'check_complex';
329 } elsif (substr($descr_res,0,1) eq 'i') {
330 $cline = 'check_int';
331 } elsif (substr($descr_res,0,1) eq 'l') {
332 $cline = 'check_long';
333 } elsif (substr($descr_res,0,1) eq 'L') {
334 $cline = 'check_longlong';
336 # Special handling for some macros:
337 $cline .= " (\"$str\", ";
338 if ($args[0] =~ /fpclassify|isnormal|isfinite|signbit/) {
339 $c_call = "$args[0] (";
341 $c_call = " FUNC($args[0]) (";
343 @descr = split //,$descr_args;
344 for ($i=0; $i <= $#descr; $i++) {
348 # FLOAT, int, long int, long long int
349 if ($descr[$i] =~ /f|i|l|L/) {
350 $c_call .= $args[$current_arg];
355 if ($descr[$i] =~ /F|I/) {
357 $c_call .= '&' . &get_variable ($extra_var);
361 if ($descr[$i] eq 'c') {
362 $c_call .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
368 $cline .= "$c_call, ";
370 @descr = split //,$descr_res;
372 if ($_ =~ /b|f|i|l|L/ ) {
373 $cline .= $args[$current_arg];
375 } elsif ($_ eq 'c') {
376 $cline .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
378 } elsif ($_ eq '1') {
379 push @special, $args[$current_arg];
384 $cline .= &new_test ($str, ($current_arg <= $#args) ? $args[$current_arg] : undef);
386 # special treatment for some functions
387 if ($args[0] eq 'frexp') {
388 if (defined $special[0] && $special[0] ne "IGNORE") {
389 my ($str) = "$call sets x to $special[0]";
390 $post = " check_int (\"$str\", x, $special[0]";
391 $post .= &new_test ($str, undef);
393 } elsif ($args[0] eq 'gamma' || $args[0] eq 'lgamma') {
394 $pre = " signgam = 0;\n";
395 if (defined $special[0] && $special[0] ne "IGNORE") {
396 my ($str) = "$call sets signgam to $special[0]";
397 $post = " check_int (\"$str\", signgam, $special[0]";
398 $post .= &new_test ($str, undef);
400 } elsif ($args[0] eq 'modf') {
401 if (defined $special[0] && $special[0] ne "IGNORE") {
402 my ($str) = "$call sets x to $special[0]";
403 $post = " check_float (\"$str\", x, $special[0]";
404 $post .= &new_test ($str, undef);
406 } elsif ($args[0] eq 'remquo') {
407 if (defined $special[0] && $special[0] ne "IGNORE") {
408 my ($str) = "$call sets x to $special[0]";
409 $post = " check_int (\"$str\", x, $special[0]";
410 $post .= &new_test ($str, undef);
414 print $file $pre if (defined $pre);
416 print $file " $cline";
418 print $file $post if (defined $post);
421 # Generate libm-test.c
422 sub generate_testfile {
423 my ($input, $output) = @_;
425 my (@args, $i, $str);
427 open INPUT, $input or die ("Can't open $input: $!");
428 open OUTPUT, ">$output" or die ("Can't open $output: $!");
430 # Replace the special macros
437 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
438 &parse_args (\*OUTPUT, $descr, $args);
443 print OUTPUT " init_max_error ();\n";
448 my ($fct, $line, $type);
455 ($fct) = ($_ =~ /END\s*\((.*)\)/);
456 if ($type eq 'complex') {
457 $line = " print_complex_max_error (\"$fct\", ";
459 $line = " print_max_error (\"$fct\", ";
461 if (exists $results{$fct}{'has_ulps'}) {
462 $line .= "DELTA$fct";
466 if (exists $results{$fct}{'has_fails'}) {
467 $line .= ", FAIL$fct";
473 push @functions, $fct;
487 my ($test, $type, $float, $eps, $kind);
489 # $type has the following values:
490 # "normal": No complex variable
491 # "real": Real part of complex result
492 # "imag": Imaginary part of complex result
493 open ULP, $file or die ("Can't open $file: $!");
496 # ignore comments and empty lines
500 if (/Real part of:/) {
503 } elsif (/Imaginary part of:/) {
504 s/Imaginary part of: //;
509 s/^.+\"(.*)\".*$/$1/;
515 if (/Real part of/) {
518 } elsif (/Imaginary part of/) {
519 s/Imaginary part of //;
524 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
528 if (/^i?(float|double|ldouble):/) {
529 ($float, $eps) = split /\s*:\s*/,$_,2;
531 if ($eps eq 'fail') {
532 $results{$test}{$type}{'fail'}{$float} = 1;
533 $results{$test}{'has_fails'} = 1;
534 } elsif ($eps eq "0") {
538 $results{$test}{$type}{'ulp'}{$float} = $eps;
539 $results{$test}{'has_ulps'} = 1;
541 if ($type =~ /^real|imag$/) {
542 $results{$test}{'type'} = 'complex';
543 } elsif ($type eq 'normal') {
544 $results{$test}{'type'} = 'normal';
546 $results{$test}{'kind'} = $kind;
549 print "Skipping unknown entry: `$_'\n";
555 # Clean up a floating point number
556 sub clean_up_number {
559 # Remove trailing zeros
565 # Output a file which can be read in as ulps file.
566 sub print_ulps_file {
568 my ($test, $type, $float, $eps, $fct, $last_fct);
571 open NEWULP, ">$file" or die ("Can't open $file: $!");
572 print NEWULP "# Begin of automatic generation\n";
573 # first the function calls
574 foreach $test (sort keys %results) {
575 next if ($results{$test}{'kind'} ne 'test');
576 foreach $type ('real', 'imag', 'normal') {
577 if (exists $results{$test}{$type}) {
578 if (defined $results{$test}) {
579 ($fct) = ($test =~ /^(\w+)\s/);
580 if ($fct ne $last_fct) {
582 print NEWULP "\n# $fct\n";
585 if ($type eq 'normal') {
586 print NEWULP "Test \"$test\":\n";
587 } elsif ($type eq 'real') {
588 print NEWULP "Test \"Real part of: $test\":\n";
589 } elsif ($type eq 'imag') {
590 print NEWULP "Test \"Imaginary part of: $test\":\n";
592 foreach $float (@all_floats) {
593 if (exists $results{$test}{$type}{'ulp'}{$float}) {
594 print NEWULP "$float: ",
595 &clean_up_number ($results{$test}{$type}{'ulp'}{$float}),
598 if (exists $results{$test}{$type}{'fail'}{$float}) {
599 print NEWULP "$float: fail\n";
605 print NEWULP "\n# Maximal error of functions:\n";
607 foreach $fct (sort keys %results) {
608 next if ($results{$fct}{'kind'} ne 'fct');
609 foreach $type ('real', 'imag', 'normal') {
610 if (exists $results{$fct}{$type}) {
611 if ($type eq 'normal') {
612 print NEWULP "Function: \"$fct\":\n";
613 } elsif ($type eq 'real') {
614 print NEWULP "Function: Real part of \"$fct\":\n";
615 } elsif ($type eq 'imag') {
616 print NEWULP "Function: Imaginary part of \"$fct\":\n";
618 foreach $float (@all_floats) {
619 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
620 print NEWULP "$float: ",
621 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
624 if (exists $results{$fct}{$type}{'fail'}{$float}) {
625 print NEWULP "$float: fail\n";
632 print NEWULP "# end of automatic generation\n";
637 my ($test, $type, $float) = @_;
639 if ($type eq 'complex') {
641 # Return 0 instead of BUILD_COMPLEX (0,0)
642 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
643 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
646 $res = 'BUILD_COMPLEX (';
647 $res .= (exists $results{$test}{'real'}{'ulp'}{$float}
648 ? $results{$test}{'real'}{'ulp'}{$float} : "0");
650 $res .= (exists $results{$test}{'imag'}{'ulp'}{$float}
651 ? $results{$test}{'imag'}{'ulp'}{$float} : "0");
655 return (exists $results{$test}{'normal'}{'ulp'}{$float}
656 ? $results{$test}{'normal'}{'ulp'}{$float} : "0");
660 my ($test, $type, $float) = @_;
661 if ($type eq 'complex') {
664 # Return 0 instead of BUILD_COMPLEX_INT (0,0)
665 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
666 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
669 $res = 'BUILD_COMPLEX_INT (';
670 $res .= (exists $results{$test}{'real'}{'fail'}{$float}
671 ? $results{$test}{'real'}{'fail'}{$float} : "0");
673 $res .= (exists $results{$test}{'imag'}{'fail'}{$float}
674 ? $results{$test}{'imag'}{'fail'}{$float} : "0");
678 return (exists $results{$test}{'normal'}{'fail'}{$float}
679 ? $results{$test}{'normal'}{'fail'}{$float} : "0");
683 # Output the defines for a single test
685 my ($file, $test, $name) = @_;
686 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
689 # Do we have ulps/failures?
690 if (!exists $results{$test}{'type'}) {
693 $type = $results{$test}{'type'};
694 if (exists $results{$test}{'has_ulps'}) {
695 # XXX use all_floats (change order!)
696 $ldouble = &get_ulps ($test, $type, "ldouble");
697 $double = &get_ulps ($test, $type, "double");
698 $float = &get_ulps ($test, $type, "float");
699 $ildouble = &get_ulps ($test, $type, "ildouble");
700 $idouble = &get_ulps ($test, $type, "idouble");
701 $ifloat = &get_ulps ($test, $type, "ifloat");
702 print $file "#define DELTA$name CHOOSE($ldouble, $double, $float, $ildouble, $idouble, $ifloat)\t/* $test */\n";
705 if (exists $results{$test}{'has_fails'}) {
706 $ldouble = &get_failure ($test, "ldouble");
707 $double = &get_failure ($test, "double");
708 $float = &get_failure ($test, "float");
709 $ildouble = &get_failure ($test, "ildouble");
710 $idouble = &get_failure ($test, "idouble");
711 $ifloat = &get_failure ($test, "ifloat");
712 print $file "#define FAIL$name CHOOSE($ldouble, $double, $float $ildouble, $idouble, $ifloat)\t/* $test */\n";
718 my ($file, $ulps_filename) = @_;
721 open ULP, ">$file" or die ("Can't open $file: $!");
723 print ULP "/* This file is automatically generated\n";
724 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
725 print ULP " Don't change it - change instead the master files. */\n\n";
727 print ULP "\n/* Maximal error of functions. */\n";
728 foreach $fct (@functions) {
729 output_test (\*ULP, $fct, $fct);
732 print ULP "\n/* Error of single function calls. */\n";
733 for ($i = 0; $i < $count; $i++) {
734 output_test (\*ULP, $tests[$i], $i);