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 # In the following description $type and $float are:
29 # - $type is either "normal", "real" (for the real part of a complex number)
30 # or "imag" (for the imaginary part # of a complex number).
31 # - $float is either of float, ifloat, double, idouble, ldouble, ildouble;
32 # It represents the underlying floating point type (float, double or long
33 # double) and if inline functions (the leading i stands for inline)
35 # $results{$test}{$type}{"ulp"}{$float} is defined and has a delta as value
42 use vars qw
($input $output);
43 use vars qw
(%results);
44 use vars qw
(@tests @functions);
46 use vars qw
(%beautify @all_floats);
47 use vars qw
($output_dir $ulps_file);
49 # all_floats is sorted and contains all recognised float types
50 @all_floats = ('double', 'float', 'idouble',
51 'ifloat', 'ildouble', 'ldouble');
54 ( "minus_zero" => "-0",
56 "minus_infty" => "-inf",
57 "plus_infty" => "inf",
58 "qnan_value" => "qNaN",
62 "M_LOG10El", "log10(e)",
64 "M_PI_34l" => "3/4 pi",
68 "M_PI_34_LOG10El" => "3/4 pi*log10(e)",
69 "M_PI_LOG10El" => "pi*log10(e)",
70 "M_PI2_LOG10El" => "pi/2*log10(e)",
71 "M_PI4_LOG10El" => "pi/4*log10(e)",
72 "M_LOG_SQRT_PIl" => "log(sqrt(pi))",
73 "M_LOG_2_SQRT_PIl" => "log(2*sqrt(pi))",
74 "M_2_SQRT_PIl" => "2 sqrt (pi)",
75 "M_SQRT_PIl" => "sqrt (pi)",
84 # n: generate new ulps file
85 use vars
qw($opt_u $opt_h $opt_o $opt_n);
88 $ulps_file = 'libm-test-ulps';
92 print "Usage: gen-libm-test.pl [OPTIONS]\n";
93 print " -h print this help, then exit\n";
94 print " -o DIR directory where generated files will be placed\n";
95 print " -n only generate sorted file NewUlps from libm-test-ulps\n";
96 print " -u FILE input file with ulps\n";
100 $ulps_file = $opt_u if ($opt_u);
101 $output_dir = $opt_o if ($opt_o);
103 $input = "libm-test.inc";
104 $output = "${output_dir}libm-test.c";
108 &parse_ulps ($ulps_file);
109 &generate_testfile ($input, $output) unless ($opt_n);
110 &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
111 &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
113 # Return a nicer representation
118 if (exists $beautify{$arg}) {
119 return $beautify{$arg};
124 if (exists $beautify{$tmp}) {
125 return '-' . $beautify{$tmp};
128 if ($arg =~ /[0-9]L$/) {
134 # Return a nicer representation of a complex number
135 sub build_complex_beautify {
139 $str1 = &beautify ($r);
140 $str2 = &beautify ($i);
143 $str1 .= ' - ' . $str2;
145 $str1 .= ' + ' . $str2;
151 # Return name of a variable
155 return "x" if ($number == 1);
156 return "y" if ($number == 2);
157 return "z" if ($number == 3);
163 # Add a new test to internal data structures and fill in the
164 # ulps and exception information for the C line.
166 my ($test, $exception) = @_;
170 if (exists $results{$test}{'has_ulps'}) {
171 $rest = ", DELTA$count";
175 if (defined $exception) {
176 $rest .= ", $exception";
181 # We must increment here to keep @tests and count in sync
187 # Treat some functions especially.
188 # Currently only sincos needs extra treatment.
189 sub special_functions {
190 my ($file, $args) = @_;
191 my (@args, $str, $test, $cline);
193 @args = split /,\s*/, $args;
195 unless ($args[0] =~ /sincos/) {
196 die ("Don't know how to handle $args[0] extra.");
199 print $file " FUNC (sincos) ($args[1], &sin_res, &cos_res);\n";
201 $str = 'sincos (' . &beautify ($args[1]) . ', &sin_res, &cos_res)';
203 $test = $str . ' puts ' . &beautify ($args[2]) . ' in sin_res';
205 $cline = " check_float (\"$test\", sin_res, $args[2]";
206 $cline .= &new_test ($test, $args[4]);
210 $test = $str . ' puts ' . &beautify ($args[3]) . ' in cos_res';
211 $cline = " check_float (\"$test\", cos_res, $args[3]";
212 # only tests once for exception
213 $cline .= &new_test ($test, undef);
218 # Parse the arguments to TEST_x_y
220 my ($file, $descr, $fct, $args) = @_;
221 my (@args, $str, $descr_args, $descr_res, @descr);
222 my ($current_arg, $cline, $i);
223 my ($pre, $post, @special);
224 my ($extra_var, $call, $c_call);
226 if ($descr eq 'extra') {
227 &special_functions ($file, $args);
230 ($descr_args, $descr_res) = split /_/,$descr, 2;
232 @args = split /,\s*/, $args;
236 # Generate first the string that's shown to the user
239 @descr = split //,$descr_args;
240 for ($i = 0; $i <= $#descr; $i++) {
244 # FLOAT, int, long int, long long int
245 if ($descr[$i] =~ /f|i|l|L/) {
246 $call .= &beautify ($args[$current_arg]);
250 # &FLOAT, &int - argument is added here
251 if ($descr[$i] =~ /F|I/) {
253 $call .= '&' . &get_variable ($extra_var);
257 if ($descr[$i] eq 'c') {
258 $call .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
263 die ("$descr[$i] is unknown");
269 @descr = split //,$descr_res;
271 if ($_ =~ /f|i|l|L/) {
272 $str .= &beautify ($args[$current_arg]);
274 } elsif ($_ eq 'c') {
275 $str .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
277 } elsif ($_ eq 'b') {
279 $str .= ($args[$current_arg] == 0) ? "false" : "true";
281 } elsif ($_ eq '1') {
284 die ("$_ is unknown");
288 if ($current_arg == $#args) {
289 die ("wrong number of arguments")
290 unless ($args[$current_arg] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN/);
291 } elsif ($current_arg < $#args) {
292 die ("wrong number of arguments");
293 } elsif ($current_arg > ($#args+1)) {
294 die ("wrong number of arguments");
298 # Put the C program line together
299 # Reset some variables to start again
302 if (substr($descr_res,0,1) eq 'f') {
303 $cline = 'check_float'
304 } elsif (substr($descr_res,0,1) eq 'b') {
305 $cline = 'check_bool';
306 } elsif (substr($descr_res,0,1) eq 'c') {
307 $cline = 'check_complex';
308 } elsif (substr($descr_res,0,1) eq 'i') {
309 $cline = 'check_int';
310 } elsif (substr($descr_res,0,1) eq 'l') {
311 $cline = 'check_long';
312 } elsif (substr($descr_res,0,1) eq 'L') {
313 $cline = 'check_longlong';
315 # Special handling for some macros:
316 $cline .= " (\"$str\", ";
317 if ($args[0] =~ /fpclassify|isnormal|isfinite|isinf|isnan|issignaling|signbit
318 |isgreater|isgreaterequal|isless|islessequal
319 |islessgreater|isunordered/x) {
320 $c_call = "$args[0] (";
322 $c_call = " FUNC($args[0]) (";
324 @descr = split //,$descr_args;
325 for ($i=0; $i <= $#descr; $i++) {
329 # FLOAT, int, long int, long long int
330 if ($descr[$i] =~ /f|i|l|L/) {
331 $c_call .= $args[$current_arg];
336 if ($descr[$i] =~ /F|I/) {
338 $c_call .= '&' . &get_variable ($extra_var);
342 if ($descr[$i] eq 'c') {
343 $c_call .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
349 $cline .= "$c_call, ";
351 @descr = split //,$descr_res;
353 if ($_ =~ /b|f|i|l|L/ ) {
354 $cline .= $args[$current_arg];
356 } elsif ($_ eq 'c') {
357 $cline .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
359 } elsif ($_ eq '1') {
360 push @special, $args[$current_arg];
365 $cline .= &new_test ($str, ($current_arg <= $#args) ? $args[$current_arg] : undef);
367 # special treatment for some functions
368 if ($args[0] eq 'frexp') {
369 $pre = " x = 123456789;\n";
370 if (defined $special[0] && $special[0] ne "IGNORE") {
371 my ($str) = "$call sets x to $special[0]";
372 $post = " check_int (\"$str\", x, $special[0]";
373 $post .= &new_test ($str, undef);
375 } elsif ($args[0] eq 'gamma' || $args[0] eq 'lgamma') {
376 $pre = " signgam = 0;\n";
377 if (defined $special[0] && $special[0] ne "IGNORE") {
378 my ($str) = "$call sets signgam to $special[0]";
379 $post = " check_int (\"$str\", signgam, $special[0]";
380 $post .= &new_test ($str, undef);
382 } elsif ($args[0] eq 'modf') {
383 $pre = " x = 123.456789;\n";
384 if (defined $special[0] && $special[0] ne "IGNORE") {
385 my ($str) = "$call sets x to $special[0]";
386 $post = " check_float (\"$str\", x, $special[0]";
387 $post .= &new_test ($str, undef);
389 } elsif ($args[0] eq 'remquo') {
390 $pre = " x = 123456789;\n";
391 if (defined $special[0] && $special[0] ne "IGNORE") {
392 my ($str) = "$call sets x to $special[0]";
393 $post = " check_int (\"$str\", x, $special[0]";
394 $post .= &new_test ($str, undef);
398 if (defined $pre or defined $post) {
400 print $file " $pre" if (defined $pre);
401 print $file " $cline";
402 print $file " $post" if (defined $post);
405 print $file " $cline";
409 # Generate libm-test.c
410 sub generate_testfile {
411 my ($input, $output) = @_;
413 my (@args, $i, $str, $thisfct);
415 open INPUT, $input or die ("Can't open $input: $!");
416 open OUTPUT, ">$output" or die ("Can't open $output: $!");
418 # Replace the special macros
425 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
426 &parse_args (\*OUTPUT, $descr, $thisfct, $args);
431 ($thisfct) = ($_ =~ /START\s*\((.*)\)/);
432 print OUTPUT " init_max_error ();\n";
437 my ($fct, $line, $type);
444 ($fct) = ($_ =~ /END\s*\((.*)\)/);
445 if ($type eq 'complex') {
446 $line = " print_complex_max_error (\"$fct\", ";
448 $line = " print_max_error (\"$fct\", ";
450 if (exists $results{$fct}{'has_ulps'}) {
451 $line .= "DELTA$fct";
457 push @functions, $fct;
471 my ($test, $type, $float, $eps, $kind);
473 # $type has the following values:
474 # "normal": No complex variable
475 # "real": Real part of complex result
476 # "imag": Imaginary part of complex result
477 open ULP, $file or die ("Can't open $file: $!");
480 # ignore comments and empty lines
484 if (/Real part of:/) {
487 } elsif (/Imaginary part of:/) {
488 s/Imaginary part of: //;
493 s/^.+\"(.*)\".*$/$1/;
499 if (/Real part of/) {
502 } elsif (/Imaginary part of/) {
503 s/Imaginary part of //;
508 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
512 if (/^i?(float|double|ldouble):/) {
513 ($float, $eps) = split /\s*:\s*/,$_,2;
519 $results{$test}{$type}{'ulp'}{$float} = $eps;
520 $results{$test}{'has_ulps'} = 1;
522 if ($type =~ /^real|imag$/) {
523 $results{$test}{'type'} = 'complex';
524 } elsif ($type eq 'normal') {
525 $results{$test}{'type'} = 'normal';
527 $results{$test}{'kind'} = $kind;
530 print "Skipping unknown entry: `$_'\n";
536 # Clean up a floating point number
537 sub clean_up_number {
540 # Remove trailing zeros after the decimal point
541 if ($number =~ /\./) {
548 # Output a file which can be read in as ulps file.
549 sub print_ulps_file {
551 my ($test, $type, $float, $eps, $fct, $last_fct);
554 open NEWULP, ">$file" or die ("Can't open $file: $!");
555 print NEWULP "# Begin of automatic generation\n";
556 # first the function calls
557 foreach $test (sort keys %results) {
558 next if ($results{$test}{'kind'} ne 'test');
559 foreach $type ('real', 'imag', 'normal') {
560 if (exists $results{$test}{$type}) {
561 if (defined $results{$test}) {
562 ($fct) = ($test =~ /^(\w+)\s/);
563 if ($fct ne $last_fct) {
565 print NEWULP "\n# $fct\n";
568 if ($type eq 'normal') {
569 print NEWULP "Test \"$test\":\n";
570 } elsif ($type eq 'real') {
571 print NEWULP "Test \"Real part of: $test\":\n";
572 } elsif ($type eq 'imag') {
573 print NEWULP "Test \"Imaginary part of: $test\":\n";
575 foreach $float (@all_floats) {
576 if (exists $results{$test}{$type}{'ulp'}{$float}) {
577 print NEWULP "$float: ",
578 &clean_up_number ($results{$test}{$type}{'ulp'}{$float}),
585 print NEWULP "\n# Maximal error of functions:\n";
587 foreach $fct (sort keys %results) {
588 next if ($results{$fct}{'kind'} ne 'fct');
589 foreach $type ('real', 'imag', 'normal') {
590 if (exists $results{$fct}{$type}) {
591 if ($type eq 'normal') {
592 print NEWULP "Function: \"$fct\":\n";
593 } elsif ($type eq 'real') {
594 print NEWULP "Function: Real part of \"$fct\":\n";
595 } elsif ($type eq 'imag') {
596 print NEWULP "Function: Imaginary part of \"$fct\":\n";
598 foreach $float (@all_floats) {
599 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
600 print NEWULP "$float: ",
601 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
609 print NEWULP "# end of automatic generation\n";
614 my ($test, $type, $float) = @_;
616 if ($type eq 'complex') {
618 # Return 0 instead of BUILD_COMPLEX (0,0)
619 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
620 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
623 $res = 'BUILD_COMPLEX (';
624 $res .= (exists $results{$test}{'real'}{'ulp'}{$float}
625 ? $results{$test}{'real'}{'ulp'}{$float} : "0");
627 $res .= (exists $results{$test}{'imag'}{'ulp'}{$float}
628 ? $results{$test}{'imag'}{'ulp'}{$float} : "0");
632 return (exists $results{$test}{'normal'}{'ulp'}{$float}
633 ? $results{$test}{'normal'}{'ulp'}{$float} : "0");
636 # Output the defines for a single test
638 my ($file, $test, $name) = @_;
639 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
643 if (!exists $results{$test}{'type'}) {
646 $type = $results{$test}{'type'};
647 if (exists $results{$test}{'has_ulps'}) {
648 # XXX use all_floats (change order!)
649 $ldouble = &get_ulps ($test, $type, "ldouble");
650 $double = &get_ulps ($test, $type, "double");
651 $float = &get_ulps ($test, $type, "float");
652 $ildouble = &get_ulps ($test, $type, "ildouble");
653 $idouble = &get_ulps ($test, $type, "idouble");
654 $ifloat = &get_ulps ($test, $type, "ifloat");
655 print $file "#define DELTA$name CHOOSE($ldouble, $double, $float, $ildouble, $idouble, $ifloat)\t/* $test */\n";
661 my ($file, $ulps_filename) = @_;
664 open ULP, ">$file" or die ("Can't open $file: $!");
666 print ULP "/* This file is automatically generated\n";
667 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
668 print ULP " Don't change it - change instead the master files. */\n\n";
670 print ULP "\n/* Maximal error of functions. */\n";
671 foreach $fct (@functions) {
672 output_test (\*ULP, $fct, $fct);
675 print ULP "\n/* Error of single function calls. */\n";
676 for ($i = 0; $i < $count; $i++) {
677 output_test (\*ULP, $tests[$i], $i);