2 # Copyright (C) 1999, 2006 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)",
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, $fct, $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;
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, $thisfct);
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, $thisfct, $args);
443 ($thisfct) = ($_ =~ /START\s*\((.*)\)/);
444 print OUTPUT " init_max_error ();\n";
449 my ($fct, $line, $type);
456 ($fct) = ($_ =~ /END\s*\((.*)\)/);
457 if ($type eq 'complex') {
458 $line = " print_complex_max_error (\"$fct\", ";
460 $line = " print_max_error (\"$fct\", ";
462 if (exists $results{$fct}{'has_ulps'}) {
463 $line .= "DELTA$fct";
467 if (exists $results{$fct}{'has_fails'}) {
468 $line .= ", FAIL$fct";
474 push @functions, $fct;
488 my ($test, $type, $float, $eps, $kind);
490 # $type has the following values:
491 # "normal": No complex variable
492 # "real": Real part of complex result
493 # "imag": Imaginary part of complex result
494 open ULP, $file or die ("Can't open $file: $!");
497 # ignore comments and empty lines
501 if (/Real part of:/) {
504 } elsif (/Imaginary part of:/) {
505 s/Imaginary part of: //;
510 s/^.+\"(.*)\".*$/$1/;
516 if (/Real part of/) {
519 } elsif (/Imaginary part of/) {
520 s/Imaginary part of //;
525 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
529 if (/^i?(float|double|ldouble):/) {
530 ($float, $eps) = split /\s*:\s*/,$_,2;
532 if ($eps eq 'fail') {
533 $results{$test}{$type}{'fail'}{$float} = 1;
534 $results{$test}{'has_fails'} = 1;
535 } elsif ($eps eq "0") {
539 $results{$test}{$type}{'ulp'}{$float} = $eps;
540 $results{$test}{'has_ulps'} = 1;
542 if ($type =~ /^real|imag$/) {
543 $results{$test}{'type'} = 'complex';
544 } elsif ($type eq 'normal') {
545 $results{$test}{'type'} = 'normal';
547 $results{$test}{'kind'} = $kind;
550 print "Skipping unknown entry: `$_'\n";
556 # Clean up a floating point number
557 sub clean_up_number {
560 # Remove trailing zeros after the decimal point
561 if ($number =~ /\./) {
568 # Output a file which can be read in as ulps file.
569 sub print_ulps_file {
571 my ($test, $type, $float, $eps, $fct, $last_fct);
574 open NEWULP, ">$file" or die ("Can't open $file: $!");
575 print NEWULP "# Begin of automatic generation\n";
576 # first the function calls
577 foreach $test (sort keys %results) {
578 next if ($results{$test}{'kind'} ne 'test');
579 foreach $type ('real', 'imag', 'normal') {
580 if (exists $results{$test}{$type}) {
581 if (defined $results{$test}) {
582 ($fct) = ($test =~ /^(\w+)\s/);
583 if ($fct ne $last_fct) {
585 print NEWULP "\n# $fct\n";
588 if ($type eq 'normal') {
589 print NEWULP "Test \"$test\":\n";
590 } elsif ($type eq 'real') {
591 print NEWULP "Test \"Real part of: $test\":\n";
592 } elsif ($type eq 'imag') {
593 print NEWULP "Test \"Imaginary part of: $test\":\n";
595 foreach $float (@all_floats) {
596 if (exists $results{$test}{$type}{'ulp'}{$float}) {
597 print NEWULP "$float: ",
598 &clean_up_number ($results{$test}{$type}{'ulp'}{$float}),
601 if (exists $results{$test}{$type}{'fail'}{$float}) {
602 print NEWULP "$float: fail\n";
608 print NEWULP "\n# Maximal error of functions:\n";
610 foreach $fct (sort keys %results) {
611 next if ($results{$fct}{'kind'} ne 'fct');
612 foreach $type ('real', 'imag', 'normal') {
613 if (exists $results{$fct}{$type}) {
614 if ($type eq 'normal') {
615 print NEWULP "Function: \"$fct\":\n";
616 } elsif ($type eq 'real') {
617 print NEWULP "Function: Real part of \"$fct\":\n";
618 } elsif ($type eq 'imag') {
619 print NEWULP "Function: Imaginary part of \"$fct\":\n";
621 foreach $float (@all_floats) {
622 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
623 print NEWULP "$float: ",
624 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
627 if (exists $results{$fct}{$type}{'fail'}{$float}) {
628 print NEWULP "$float: fail\n";
635 print NEWULP "# end of automatic generation\n";
640 my ($test, $type, $float) = @_;
642 if ($type eq 'complex') {
644 # Return 0 instead of BUILD_COMPLEX (0,0)
645 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
646 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
649 $res = 'BUILD_COMPLEX (';
650 $res .= (exists $results{$test}{'real'}{'ulp'}{$float}
651 ? $results{$test}{'real'}{'ulp'}{$float} : "0");
653 $res .= (exists $results{$test}{'imag'}{'ulp'}{$float}
654 ? $results{$test}{'imag'}{'ulp'}{$float} : "0");
658 return (exists $results{$test}{'normal'}{'ulp'}{$float}
659 ? $results{$test}{'normal'}{'ulp'}{$float} : "0");
663 my ($test, $type, $float) = @_;
664 if ($type eq 'complex') {
667 # Return 0 instead of BUILD_COMPLEX_INT (0,0)
668 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
669 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
672 $res = 'BUILD_COMPLEX_INT (';
673 $res .= (exists $results{$test}{'real'}{'fail'}{$float}
674 ? $results{$test}{'real'}{'fail'}{$float} : "0");
676 $res .= (exists $results{$test}{'imag'}{'fail'}{$float}
677 ? $results{$test}{'imag'}{'fail'}{$float} : "0");
681 return (exists $results{$test}{'normal'}{'fail'}{$float}
682 ? $results{$test}{'normal'}{'fail'}{$float} : "0");
686 # Output the defines for a single test
688 my ($file, $test, $name) = @_;
689 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
692 # Do we have ulps/failures?
693 if (!exists $results{$test}{'type'}) {
696 $type = $results{$test}{'type'};
697 if (exists $results{$test}{'has_ulps'}) {
698 # XXX use all_floats (change order!)
699 $ldouble = &get_ulps ($test, $type, "ldouble");
700 $double = &get_ulps ($test, $type, "double");
701 $float = &get_ulps ($test, $type, "float");
702 $ildouble = &get_ulps ($test, $type, "ildouble");
703 $idouble = &get_ulps ($test, $type, "idouble");
704 $ifloat = &get_ulps ($test, $type, "ifloat");
705 print $file "#define DELTA$name CHOOSE($ldouble, $double, $float, $ildouble, $idouble, $ifloat)\t/* $test */\n";
708 if (exists $results{$test}{'has_fails'}) {
709 $ldouble = &get_failure ($test, "ldouble");
710 $double = &get_failure ($test, "double");
711 $float = &get_failure ($test, "float");
712 $ildouble = &get_failure ($test, "ildouble");
713 $idouble = &get_failure ($test, "idouble");
714 $ifloat = &get_failure ($test, "ifloat");
715 print $file "#define FAIL$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);