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, write to the Free
18 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # This file needs to be tidied up
22 # Note that functions and tests share the same namespace.
24 # Information about tests are stored in: %results
25 # $results{$test}{"kind"} is either "fct" or "test" and flags whether this
26 # is a maximal error of a function or a single test.
27 # $results{$test}{"type"} is the result type, e.g. normal or complex.
28 # $results{$test}{"has_ulps"} is set if deltas exist.
29 # $results{$test}{"has_fails"} is set if exptected failures exist.
30 # In the following description $type and $float are:
31 # - $type is either "normal", "real" (for the real part of a complex number)
32 # or "imag" (for the imaginary part # of a complex number).
33 # - $float is either of float, ifloat, double, idouble, ldouble, ildouble;
34 # It represents the underlying floating point type (float, double or long
35 # double) and if inline functions (the leading i stands for inline)
37 # $results{$test}{$type}{"fail"}{$float} is defined and has a 1 if
38 # the test is expected to fail
39 # $results{$test}{$type}{"ulp"}{$float} is defined and has a delta as value
46 use vars qw
($input $output);
47 use vars qw
(%results);
48 use vars qw
(@tests @functions);
50 use vars qw
(%beautify @all_floats);
51 use vars qw
($output_dir $ulps_file);
53 # all_floats is sorted and contains all recognised float types
54 @all_floats = ('double', 'float', 'idouble',
55 'ifloat', 'ildouble', 'ldouble');
58 ( "minus_zero" => "-0",
60 "minus_infty" => "-inf",
61 "plus_infty" => "inf",
66 "M_LOG10El", "log10(e)",
68 "M_PI_34l" => "3/4 pi",
72 "M_PI_34_LOG10El" => "3/4 pi*log10(e)",
73 "M_PI_LOG10El" => "pi*log10(e)",
74 "M_PI2_LOG10El" => "pi/2*log10(e)",
75 "M_PI4_LOG10El" => "pi/4*log10(e)",
76 "M_LOG_SQRT_PIl" => "log(sqrt(pi))",
77 "M_LOG_2_SQRT_PIl" => "log(2*sqrt(pi))",
78 "M_2_SQRT_PIl" => "2 sqrt (pi)",
79 "M_SQRT_PIl" => "sqrt (pi)",
80 "INVALID_EXCEPTION" => "invalid exception",
81 "DIVIDE_BY_ZERO_EXCEPTION" => "division by zero exception",
82 "INVALID_EXCEPTION_OK" => "invalid exception allowed",
83 "DIVIDE_BY_ZERO_EXCEPTION_OK" => "division by zero exception allowed",
84 "EXCEPTIONS_OK" => "exceptions allowed",
85 "IGNORE_ZERO_INF_SIGN" => "sign of zero/inf not specified",
86 "INVALID_EXCEPTION|IGNORE_ZERO_INF_SIGN" => "invalid exception and sign of zero/inf not specified"
95 # n: generate new ulps file
96 use vars
qw($opt_u $opt_h $opt_o $opt_n);
99 $ulps_file = 'libm-test-ulps';
103 print "Usage: gen-libm-test.pl [OPTIONS]\n";
104 print " -h print this help, then exit\n";
105 print " -o DIR directory where generated files will be placed\n";
106 print " -n only generate sorted file NewUlps from libm-test-ulps\n";
107 print " -u FILE input file with ulps\n";
111 $ulps_file = $opt_u if ($opt_u);
112 $output_dir = $opt_o if ($opt_o);
114 $input = "libm-test.inc";
115 $output = "${output_dir}libm-test.c";
119 &parse_ulps ($ulps_file);
120 &generate_testfile ($input, $output) unless ($opt_n);
121 &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
122 &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
124 # Return a nicer representation
129 if (exists $beautify{$arg}) {
130 return $beautify{$arg};
135 if (exists $beautify{$tmp}) {
136 return '-' . $beautify{$tmp};
139 if ($arg =~ /[0-9]L$/) {
145 # Return a nicer representation of a complex number
146 sub build_complex_beautify {
150 $str1 = &beautify ($r);
151 $str2 = &beautify ($i);
154 $str1 .= ' - ' . $str2;
156 $str1 .= ' + ' . $str2;
162 # Return name of a variable
166 return "x" if ($number == 1);
167 return "y" if ($number == 2);
168 return "z" if ($number == 3);
174 # Add a new test to internal data structures and fill in the
175 # ulps, failures and exception information for the C line.
177 my ($test, $exception) = @_;
181 if (exists $results{$test}{'has_ulps'}) {
182 $rest = ", DELTA$count";
186 if (exists $results{$test}{'has_fails'}) {
187 $rest .= ", FAIL$count";
191 if (defined $exception) {
192 $rest .= ", $exception";
197 # We must increment here to keep @tests and count in sync
203 # Treat some functions especially.
204 # Currently only sincos needs extra treatment.
205 sub special_functions {
206 my ($file, $args) = @_;
207 my (@args, $str, $test, $cline);
209 @args = split /,\s*/, $args;
211 unless ($args[0] =~ /sincos/) {
212 die ("Don't know how to handle $args[0] extra.");
214 print $file " FUNC (sincos) ($args[1], &sin_res, &cos_res);\n";
216 $str = 'sincos (' . &beautify ($args[1]) . ', &sin_res, &cos_res)';
218 $test = $str . ' puts ' . &beautify ($args[2]) . ' in sin_res';
220 $test .= " plus " . &beautify ($args[4]);
223 $cline = " check_float (\"$test\", sin_res, $args[2]";
224 $cline .= &new_test ($test, $args[4]);
228 $test = $str . ' puts ' . &beautify ($args[3]) . ' in cos_res';
229 $cline = " check_float (\"$test\", cos_res, $args[3]";
230 # only tests once for exception
231 $cline .= &new_test ($test, undef);
235 # Parse the arguments to TEST_x_y
237 my ($file, $descr, $fct, $args) = @_;
238 my (@args, $str, $descr_args, $descr_res, @descr);
239 my ($current_arg, $cline, $i);
240 my ($pre, $post, @special);
241 my ($extra_var, $call, $c_call);
243 if ($descr eq 'extra') {
244 &special_functions ($file, $args);
247 ($descr_args, $descr_res) = split /_/,$descr, 2;
249 @args = split /,\s*/, $args;
253 # Generate first the string that's shown to the user
256 @descr = split //,$descr_args;
257 for ($i = 0; $i <= $#descr; $i++) {
261 # FLOAT, int, long int, long long int
262 if ($descr[$i] =~ /f|i|l|L/) {
263 $call .= &beautify ($args[$current_arg]);
267 # &FLOAT, &int - argument is added here
268 if ($descr[$i] =~ /F|I/) {
270 $call .= '&' . &get_variable ($extra_var);
274 if ($descr[$i] eq 'c') {
275 $call .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
280 die ("$descr[$i] is unknown");
286 @descr = split //,$descr_res;
288 if ($_ =~ /f|i|l|L/) {
289 $str .= &beautify ($args[$current_arg]);
291 } elsif ($_ eq 'c') {
292 $str .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
294 } elsif ($_ eq 'b') {
296 $str .= ($args[$current_arg] == 0) ? "false" : "true";
298 } elsif ($_ eq '1') {
301 die ("$_ is unknown");
305 if ($current_arg == $#args) {
306 die ("wrong number of arguments")
307 unless ($args[$current_arg] =~ /EXCEPTION|IGNORE_ZERO_INF_SIGN/);
308 } elsif ($current_arg < $#args) {
309 die ("wrong number of arguments");
310 } elsif ($current_arg > ($#args+1)) {
311 die ("wrong number of arguments");
315 # check for exceptions
316 if ($current_arg <= $#args) {
317 $str .= " plus " . &beautify ($args[$current_arg]);
320 # Put the C program line together
321 # Reset some variables to start again
324 if (substr($descr_res,0,1) eq 'f') {
325 $cline = 'check_float'
326 } elsif (substr($descr_res,0,1) eq 'b') {
327 $cline = 'check_bool';
328 } elsif (substr($descr_res,0,1) eq 'c') {
329 $cline = 'check_complex';
330 } elsif (substr($descr_res,0,1) eq 'i') {
331 $cline = 'check_int';
332 } elsif (substr($descr_res,0,1) eq 'l') {
333 $cline = 'check_long';
334 } elsif (substr($descr_res,0,1) eq 'L') {
335 $cline = 'check_longlong';
337 # Special handling for some macros:
338 $cline .= " (\"$str\", ";
339 if ($args[0] =~ /fpclassify|isnormal|isfinite|signbit/) {
340 $c_call = "$args[0] (";
342 $c_call = " FUNC($args[0]) (";
344 @descr = split //,$descr_args;
345 for ($i=0; $i <= $#descr; $i++) {
349 # FLOAT, int, long int, long long int
350 if ($descr[$i] =~ /f|i|l|L/) {
351 $c_call .= $args[$current_arg];
356 if ($descr[$i] =~ /F|I/) {
358 $c_call .= '&' . &get_variable ($extra_var);
362 if ($descr[$i] eq 'c') {
363 $c_call .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
369 $cline .= "$c_call, ";
371 @descr = split //,$descr_res;
373 if ($_ =~ /b|f|i|l|L/ ) {
374 $cline .= $args[$current_arg];
376 } elsif ($_ eq 'c') {
377 $cline .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
379 } elsif ($_ eq '1') {
380 push @special, $args[$current_arg];
385 $cline .= &new_test ($str, ($current_arg <= $#args) ? $args[$current_arg] : undef);
387 # special treatment for some functions
388 if ($args[0] eq 'frexp') {
389 if (defined $special[0] && $special[0] ne "IGNORE") {
390 my ($str) = "$call sets x to $special[0]";
391 $post = " check_int (\"$str\", x, $special[0]";
392 $post .= &new_test ($str, undef);
394 } elsif ($args[0] eq 'gamma' || $args[0] eq 'lgamma') {
395 $pre = " signgam = 0;\n";
396 if (defined $special[0] && $special[0] ne "IGNORE") {
397 my ($str) = "$call sets signgam to $special[0]";
398 $post = " check_int (\"$str\", signgam, $special[0]";
399 $post .= &new_test ($str, undef);
401 } elsif ($args[0] eq 'modf') {
402 if (defined $special[0] && $special[0] ne "IGNORE") {
403 my ($str) = "$call sets x to $special[0]";
404 $post = " check_float (\"$str\", x, $special[0]";
405 $post .= &new_test ($str, undef);
407 } elsif ($args[0] eq 'remquo') {
408 if (defined $special[0] && $special[0] ne "IGNORE") {
409 my ($str) = "$call sets x to $special[0]";
410 $post = " check_int (\"$str\", x, $special[0]";
411 $post .= &new_test ($str, undef);
415 print $file $pre if (defined $pre);
417 print $file " $cline";
419 print $file $post if (defined $post);
422 # Generate libm-test.c
423 sub generate_testfile {
424 my ($input, $output) = @_;
426 my (@args, $i, $str, $thisfct);
428 open INPUT, $input or die ("Can't open $input: $!");
429 open OUTPUT, ">$output" or die ("Can't open $output: $!");
431 # Replace the special macros
438 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
439 &parse_args (\*OUTPUT, $descr, $thisfct, $args);
444 ($thisfct) = ($_ =~ /START\s*\((.*)\)/);
445 print OUTPUT " init_max_error ();\n";
450 my ($fct, $line, $type);
457 ($fct) = ($_ =~ /END\s*\((.*)\)/);
458 if ($type eq 'complex') {
459 $line = " print_complex_max_error (\"$fct\", ";
461 $line = " print_max_error (\"$fct\", ";
463 if (exists $results{$fct}{'has_ulps'}) {
464 $line .= "DELTA$fct";
468 if (exists $results{$fct}{'has_fails'}) {
469 $line .= ", FAIL$fct";
475 push @functions, $fct;
489 my ($test, $type, $float, $eps, $kind);
491 # $type has the following values:
492 # "normal": No complex variable
493 # "real": Real part of complex result
494 # "imag": Imaginary part of complex result
495 open ULP, $file or die ("Can't open $file: $!");
498 # ignore comments and empty lines
502 if (/Real part of:/) {
505 } elsif (/Imaginary part of:/) {
506 s/Imaginary part of: //;
511 s/^.+\"(.*)\".*$/$1/;
517 if (/Real part of/) {
520 } elsif (/Imaginary part of/) {
521 s/Imaginary part of //;
526 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
530 if (/^i?(float|double|ldouble):/) {
531 ($float, $eps) = split /\s*:\s*/,$_,2;
533 if ($eps eq 'fail') {
534 $results{$test}{$type}{'fail'}{$float} = 1;
535 $results{$test}{'has_fails'} = 1;
536 } elsif ($eps eq "0") {
540 $results{$test}{$type}{'ulp'}{$float} = $eps;
541 $results{$test}{'has_ulps'} = 1;
543 if ($type =~ /^real|imag$/) {
544 $results{$test}{'type'} = 'complex';
545 } elsif ($type eq 'normal') {
546 $results{$test}{'type'} = 'normal';
548 $results{$test}{'kind'} = $kind;
551 print "Skipping unknown entry: `$_'\n";
557 # Clean up a floating point number
558 sub clean_up_number {
561 # Remove trailing zeros after the decimal point
562 if ($number =~ /\./) {
569 # Output a file which can be read in as ulps file.
570 sub print_ulps_file {
572 my ($test, $type, $float, $eps, $fct, $last_fct);
575 open NEWULP, ">$file" or die ("Can't open $file: $!");
576 print NEWULP "# Begin of automatic generation\n";
577 # first the function calls
578 foreach $test (sort keys %results) {
579 next if ($results{$test}{'kind'} ne 'test');
580 foreach $type ('real', 'imag', 'normal') {
581 if (exists $results{$test}{$type}) {
582 if (defined $results{$test}) {
583 ($fct) = ($test =~ /^(\w+)\s/);
584 if ($fct ne $last_fct) {
586 print NEWULP "\n# $fct\n";
589 if ($type eq 'normal') {
590 print NEWULP "Test \"$test\":\n";
591 } elsif ($type eq 'real') {
592 print NEWULP "Test \"Real part of: $test\":\n";
593 } elsif ($type eq 'imag') {
594 print NEWULP "Test \"Imaginary part of: $test\":\n";
596 foreach $float (@all_floats) {
597 if (exists $results{$test}{$type}{'ulp'}{$float}) {
598 print NEWULP "$float: ",
599 &clean_up_number ($results{$test}{$type}{'ulp'}{$float}),
602 if (exists $results{$test}{$type}{'fail'}{$float}) {
603 print NEWULP "$float: fail\n";
609 print NEWULP "\n# Maximal error of functions:\n";
611 foreach $fct (sort keys %results) {
612 next if ($results{$fct}{'kind'} ne 'fct');
613 foreach $type ('real', 'imag', 'normal') {
614 if (exists $results{$fct}{$type}) {
615 if ($type eq 'normal') {
616 print NEWULP "Function: \"$fct\":\n";
617 } elsif ($type eq 'real') {
618 print NEWULP "Function: Real part of \"$fct\":\n";
619 } elsif ($type eq 'imag') {
620 print NEWULP "Function: Imaginary part of \"$fct\":\n";
622 foreach $float (@all_floats) {
623 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
624 print NEWULP "$float: ",
625 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
628 if (exists $results{$fct}{$type}{'fail'}{$float}) {
629 print NEWULP "$float: fail\n";
636 print NEWULP "# end of automatic generation\n";
641 my ($test, $type, $float) = @_;
643 if ($type eq 'complex') {
645 # Return 0 instead of BUILD_COMPLEX (0,0)
646 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
647 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
650 $res = 'BUILD_COMPLEX (';
651 $res .= (exists $results{$test}{'real'}{'ulp'}{$float}
652 ? $results{$test}{'real'}{'ulp'}{$float} : "0");
654 $res .= (exists $results{$test}{'imag'}{'ulp'}{$float}
655 ? $results{$test}{'imag'}{'ulp'}{$float} : "0");
659 return (exists $results{$test}{'normal'}{'ulp'}{$float}
660 ? $results{$test}{'normal'}{'ulp'}{$float} : "0");
664 my ($test, $type, $float) = @_;
665 if ($type eq 'complex') {
668 # Return 0 instead of BUILD_COMPLEX_INT (0,0)
669 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
670 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
673 $res = 'BUILD_COMPLEX_INT (';
674 $res .= (exists $results{$test}{'real'}{'fail'}{$float}
675 ? $results{$test}{'real'}{'fail'}{$float} : "0");
677 $res .= (exists $results{$test}{'imag'}{'fail'}{$float}
678 ? $results{$test}{'imag'}{'fail'}{$float} : "0");
682 return (exists $results{$test}{'normal'}{'fail'}{$float}
683 ? $results{$test}{'normal'}{'fail'}{$float} : "0");
687 # Output the defines for a single test
689 my ($file, $test, $name) = @_;
690 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
693 # Do we have ulps/failures?
694 if (!exists $results{$test}{'type'}) {
697 $type = $results{$test}{'type'};
698 if (exists $results{$test}{'has_ulps'}) {
699 # XXX use all_floats (change order!)
700 $ldouble = &get_ulps ($test, $type, "ldouble");
701 $double = &get_ulps ($test, $type, "double");
702 $float = &get_ulps ($test, $type, "float");
703 $ildouble = &get_ulps ($test, $type, "ildouble");
704 $idouble = &get_ulps ($test, $type, "idouble");
705 $ifloat = &get_ulps ($test, $type, "ifloat");
706 print $file "#define DELTA$name CHOOSE($ldouble, $double, $float, $ildouble, $idouble, $ifloat)\t/* $test */\n";
709 if (exists $results{$test}{'has_fails'}) {
710 $ldouble = &get_failure ($test, "ldouble");
711 $double = &get_failure ($test, "double");
712 $float = &get_failure ($test, "float");
713 $ildouble = &get_failure ($test, "ildouble");
714 $idouble = &get_failure ($test, "idouble");
715 $ifloat = &get_failure ($test, "ifloat");
716 print $file "#define FAIL$name CHOOSE($ldouble, $double, $float $ildouble, $idouble, $ifloat)\t/* $test */\n";
722 my ($file, $ulps_filename) = @_;
725 open ULP, ">$file" or die ("Can't open $file: $!");
727 print ULP "/* This file is automatically generated\n";
728 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
729 print ULP " Don't change it - change instead the master files. */\n\n";
731 print ULP "\n/* Maximal error of functions. */\n";
732 foreach $fct (@functions) {
733 output_test (\*ULP, $fct, $fct);
736 print ULP "\n/* Error of single function calls. */\n";
737 for ($i = 0; $i < $count; $i++) {
738 output_test (\*ULP, $tests[$i], $i);