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
(%beautify @all_floats);
45 use vars qw
($output_dir $ulps_file);
47 # all_floats is sorted and contains all recognised float types
48 @all_floats = ('double', 'float', 'idouble',
49 'ifloat', 'ildouble', 'ldouble');
52 ( "minus_zero" => "-0",
54 "minus_infty" => "-inf",
55 "plus_infty" => "inf",
56 "qnan_value" => "qNaN",
60 "M_LOG10El", "log10(e)",
62 "M_PI_34l" => "3/4 pi",
66 "M_PI_34_LOG10El" => "3/4 pi*log10(e)",
67 "M_PI_LOG10El" => "pi*log10(e)",
68 "M_PI2_LOG10El" => "pi/2*log10(e)",
69 "M_PI4_LOG10El" => "pi/4*log10(e)",
70 "M_LOG_SQRT_PIl" => "log(sqrt(pi))",
71 "M_LOG_2_SQRT_PIl" => "log(2*sqrt(pi))",
72 "M_2_SQRT_PIl" => "2 sqrt (pi)",
73 "M_SQRT_PIl" => "sqrt (pi)",
82 # n: generate new ulps file
83 use vars
qw($opt_u $opt_h $opt_o $opt_n);
86 $ulps_file = 'libm-test-ulps';
90 print "Usage: gen-libm-test.pl [OPTIONS]\n";
91 print " -h print this help, then exit\n";
92 print " -o DIR directory where generated files will be placed\n";
93 print " -n only generate sorted file NewUlps from libm-test-ulps\n";
94 print " -u FILE input file with ulps\n";
98 $ulps_file = $opt_u if ($opt_u);
99 $output_dir = $opt_o if ($opt_o);
101 $input = "libm-test.inc";
102 $output = "${output_dir}libm-test.c";
104 &parse_ulps ($ulps_file);
105 &generate_testfile ($input, $output) unless ($opt_n);
106 &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
107 &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
109 # Return a nicer representation
114 if (exists $beautify{$arg}) {
115 return $beautify{$arg};
120 if (exists $beautify{$tmp}) {
121 return '-' . $beautify{$tmp};
124 if ($arg =~ /[0-9]L$/) {
130 # Return a nicer representation of a complex number
131 sub build_complex_beautify {
135 $str1 = &beautify ($r);
136 $str2 = &beautify ($i);
139 $str1 .= ' - ' . $str2;
141 $str1 .= ' + ' . $str2;
147 # Return the text to put in an initializer for a test's exception
149 sub show_exceptions {
150 my ($exception) = @_;
151 if (defined $exception) {
152 return ", $exception";
158 # Parse the arguments to TEST_x_y
160 my ($file, $descr, $args) = @_;
161 my (@args, $descr_args, $descr_res, @descr);
162 my ($current_arg, $cline, $i);
166 ($descr_args, $descr_res) = split /_/,$descr, 2;
168 @args = split /,\s*/, $args;
172 # Generate first the string that's shown to the user
174 @descr = split //,$descr_args;
175 for ($i = 0; $i <= $#descr; $i++) {
177 if ($current_arg > 1) {
180 # FLOAT, int, long int, long long int
181 if ($descr[$i] =~ /f|i|l|L/) {
182 $call_args .= $comma . &beautify ($args[$current_arg]);
186 # &FLOAT, &int - simplify call by not showing argument.
187 if ($descr[$i] =~ /F|I/) {
191 if ($descr[$i] eq 'c') {
192 $call_args .= $comma . &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
197 die ("$descr[$i] is unknown");
201 @descr = split //,$descr_res;
203 if ($_ =~ /f|i|l|L/) {
205 } elsif ($_ eq 'c') {
207 } elsif ($_ eq 'b') {
210 } elsif ($_ eq '1') {
213 die ("$_ is unknown");
217 if ($current_arg == $#args) {
218 die ("wrong number of arguments")
219 unless ($args[$current_arg] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN/);
220 } elsif ($current_arg < $#args) {
221 die ("wrong number of arguments");
222 } elsif ($current_arg > ($#args+1)) {
223 die ("wrong number of arguments");
227 # Put the C program line together
228 # Reset some variables to start again
230 $cline = "{ \"$call_args\"";
231 @descr = split //,$descr_args;
232 for ($i=0; $i <= $#descr; $i++) {
233 # FLOAT, int, long int, long long int
234 if ($descr[$i] =~ /f|i|l|L/) {
235 $cline .= ", $args[$current_arg]";
240 if ($descr[$i] =~ /F|I/) {
244 if ($descr[$i] eq 'c') {
245 $cline .= ", $args[$current_arg], $args[$current_arg+1]";
251 @descr = split //,$descr_res;
253 if ($_ =~ /b|f|i|l|L/ ) {
254 $cline .= ", $args[$current_arg]";
256 } elsif ($_ eq 'c') {
257 $cline .= ", $args[$current_arg], $args[$current_arg+1]";
259 } elsif ($_ eq '1') {
260 push @special, $args[$current_arg];
265 $cline .= show_exceptions (($current_arg <= $#args)
266 ? $args[$current_arg]
269 # special treatment for some functions
273 my ($extra_expected) = $_;
274 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
276 $extra_expected = "0";
278 $cline .= ", $run_extra, $extra_expected";
280 print $file " $cline },\n";
283 # Generate libm-test.c
284 sub generate_testfile {
285 my ($input, $output) = @_;
289 open INPUT, $input or die ("Can't open $input: $!");
290 open OUTPUT, ">$output" or die ("Can't open $output: $!");
292 # Replace the special macros
299 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
300 &parse_args (\*OUTPUT, $descr, $args);
314 my ($test, $type, $float, $eps, $kind);
316 # $type has the following values:
317 # "normal": No complex variable
318 # "real": Real part of complex result
319 # "imag": Imaginary part of complex result
320 open ULP, $file or die ("Can't open $file: $!");
323 # ignore comments and empty lines
327 if (/Real part of:/) {
330 } elsif (/Imaginary part of:/) {
331 s/Imaginary part of: //;
336 s/^.+\"(.*)\".*$/$1/;
342 if (/Real part of/) {
345 } elsif (/Imaginary part of/) {
346 s/Imaginary part of //;
351 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
355 if (/^i?(float|double|ldouble):/) {
356 ($float, $eps) = split /\s*:\s*/,$_,2;
362 $results{$test}{$type}{'ulp'}{$float} = $eps;
363 $results{$test}{'has_ulps'} = 1;
365 if ($type =~ /^real|imag$/) {
366 $results{$test}{'type'} = 'complex';
367 } elsif ($type eq 'normal') {
368 $results{$test}{'type'} = 'normal';
370 $results{$test}{'kind'} = $kind;
373 print "Skipping unknown entry: `$_'\n";
379 # Clean up a floating point number
380 sub clean_up_number {
383 # Remove trailing zeros after the decimal point
384 if ($number =~ /\./) {
391 # Output a file which can be read in as ulps file.
392 sub print_ulps_file {
394 my ($test, $type, $float, $eps, $fct, $last_fct);
397 open NEWULP, ">$file" or die ("Can't open $file: $!");
398 print NEWULP "# Begin of automatic generation\n";
399 # first the function calls
400 foreach $test (sort keys %results) {
401 next if ($results{$test}{'kind'} ne 'test');
402 foreach $type ('real', 'imag', 'normal') {
403 if (exists $results{$test}{$type}) {
404 if (defined $results{$test}) {
405 ($fct) = ($test =~ /^(\w+)\s/);
406 if ($fct ne $last_fct) {
408 print NEWULP "\n# $fct\n";
411 if ($type eq 'normal') {
412 print NEWULP "Test \"$test\":\n";
413 } elsif ($type eq 'real') {
414 print NEWULP "Test \"Real part of: $test\":\n";
415 } elsif ($type eq 'imag') {
416 print NEWULP "Test \"Imaginary part of: $test\":\n";
418 foreach $float (@all_floats) {
419 if (exists $results{$test}{$type}{'ulp'}{$float}) {
420 print NEWULP "$float: ",
421 &clean_up_number ($results{$test}{$type}{'ulp'}{$float}),
428 print NEWULP "\n# Maximal error of functions:\n";
430 foreach $fct (sort keys %results) {
431 next if ($results{$fct}{'kind'} ne 'fct');
432 foreach $type ('real', 'imag', 'normal') {
433 if (exists $results{$fct}{$type}) {
434 if ($type eq 'normal') {
435 print NEWULP "Function: \"$fct\":\n";
436 } elsif ($type eq 'real') {
437 print NEWULP "Function: Real part of \"$fct\":\n";
438 } elsif ($type eq 'imag') {
439 print NEWULP "Function: Imaginary part of \"$fct\":\n";
441 foreach $float (@all_floats) {
442 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
443 print NEWULP "$float: ",
444 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
452 print NEWULP "# end of automatic generation\n";
457 my ($test, $type, $float) = @_;
459 return (exists $results{$test}{$type}{'ulp'}{$float}
460 ? $results{$test}{$type}{'ulp'}{$float} : "0");
463 # Return the ulps value for a single test.
464 sub get_all_ulps_for_test {
465 my ($test, $type) = @_;
466 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
468 if (exists $results{$test}{'has_ulps'}) {
469 # XXX use all_floats (change order!)
470 $ldouble = &get_ulps ($test, $type, "ldouble");
471 $double = &get_ulps ($test, $type, "double");
472 $float = &get_ulps ($test, $type, "float");
473 $ildouble = &get_ulps ($test, $type, "ildouble");
474 $idouble = &get_ulps ($test, $type, "idouble");
475 $ifloat = &get_ulps ($test, $type, "ifloat");
476 return "CHOOSE ($ldouble, $double, $float, $ildouble, $idouble, $ifloat)";
478 die "get_all_ulps_for_test called for \"$test\" with no ulps\n";
484 my ($file, $ulps_filename) = @_;
485 my ($i, $fct, $type, $ulp, $ulp_real, $ulp_imag);
486 my (%test_ulps, %func_ulps, %func_real_ulps, %func_imag_ulps);
488 open ULP, ">$file" or die ("Can't open $file: $!");
490 print ULP "/* This file is automatically generated\n";
491 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
492 print ULP " Don't change it - change instead the master files. */\n\n";
494 foreach $fct (keys %results) {
495 $type = $results{$fct}{'type'};
496 if ($type eq 'normal') {
497 $ulp = get_all_ulps_for_test ($fct, 'normal');
498 } elsif ($type eq 'complex') {
499 $ulp_real = get_all_ulps_for_test ($fct, 'real');
500 $ulp_imag = get_all_ulps_for_test ($fct, 'imag');
502 die "unknown results ($fct) type $type\n";
504 if ($results{$fct}{'kind'} eq 'fct') {
505 if ($type eq 'normal') {
506 $func_ulps{$fct} = $ulp;
508 $func_real_ulps{$fct} = $ulp_real;
509 $func_imag_ulps{$fct} = $ulp_imag;
511 } elsif ($results{$fct}{'kind'} eq 'test') {
512 if ($type eq 'normal') {
513 $test_ulps{$fct} = $ulp;
515 $test_ulps{"Real part of: $fct"} = $ulp_real;
516 $test_ulps{"Imaginary part of: $fct"} = $ulp_imag;
519 die "unknown results ($fct) kind $results{$fct}{'kind'}\n";
522 print ULP "\n/* Maximal error of functions. */\n";
523 print ULP "static const struct ulp_data func_ulps[] =\n {\n";
524 foreach $fct (sort keys %func_ulps) {
525 print ULP " { \"$fct\", $func_ulps{$fct} },\n";
528 print ULP "static const struct ulp_data func_real_ulps[] =\n {\n";
529 foreach $fct (sort keys %func_real_ulps) {
530 print ULP " { \"$fct\", $func_real_ulps{$fct} },\n";
533 print ULP "static const struct ulp_data func_imag_ulps[] =\n {\n";
534 foreach $fct (sort keys %func_imag_ulps) {
535 print ULP " { \"$fct\", $func_imag_ulps{$fct} },\n";
539 print ULP "\n/* Error of single function calls. */\n";
540 print ULP "static const struct ulp_data test_ulps[] =\n {\n";
541 foreach $fct (sort keys %test_ulps) {
542 print ULP " { \"$fct\", $test_ulps{$fct} },\n";