MIPS: soft-fp NaN representation corrections
[glibc.git] / math / gen-libm-test.pl
blob217d74d2d268da486ae8cc56eaa2009e7365ce4f
1 #!/usr/bin/perl -w
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)
34 # are used.
35 # $results{$test}{$type}{"ulp"}{$float} is defined and has a delta as value
38 use Getopt::Std;
40 use strict;
42 use vars qw ($input $output);
43 use vars qw (%results);
44 use vars qw (@tests @functions);
45 use vars qw ($count);
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');
53 %beautify =
54 ( "minus_zero" => "-0",
55 "plus_zero" => "+0",
56 "minus_infty" => "-inf",
57 "plus_infty" => "inf",
58 "qnan_value" => "qNaN",
59 "M_El" => "e",
60 "M_E2l" => "e^2",
61 "M_E3l" => "e^3",
62 "M_LOG10El", "log10(e)",
63 "M_PIl" => "pi",
64 "M_PI_34l" => "3/4 pi",
65 "M_PI_2l" => "pi/2",
66 "M_PI_4l" => "pi/4",
67 "M_PI_6l" => "pi/6",
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)",
79 # get Options
80 # Options:
81 # u: ulps-file
82 # h: help
83 # o: output-directory
84 # n: generate new ulps file
85 use vars qw($opt_u $opt_h $opt_o $opt_n);
86 getopts('u:o:nh');
88 $ulps_file = 'libm-test-ulps';
89 $output_dir = '';
91 if ($opt_h) {
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";
97 exit 0;
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";
106 $count = 0;
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
114 sub beautify {
115 my ($arg) = @_;
116 my ($tmp);
118 if (exists $beautify{$arg}) {
119 return $beautify{$arg};
121 if ($arg =~ /^-/) {
122 $tmp = $arg;
123 $tmp =~ s/^-//;
124 if (exists $beautify{$tmp}) {
125 return '-' . $beautify{$tmp};
128 if ($arg =~ /[0-9]L$/) {
129 $arg =~ s/L$//;
131 return $arg;
134 # Return a nicer representation of a complex number
135 sub build_complex_beautify {
136 my ($r, $i) = @_;
137 my ($str1, $str2);
139 $str1 = &beautify ($r);
140 $str2 = &beautify ($i);
141 if ($str2 =~ /^-/) {
142 $str2 =~ s/^-//;
143 $str1 .= ' - ' . $str2;
144 } else {
145 $str1 .= ' + ' . $str2;
147 $str1 .= ' i';
148 return $str1;
151 # Return name of a variable
152 sub get_variable {
153 my ($number) = @_;
155 return "x" if ($number == 1);
156 return "y" if ($number == 2);
157 return "z" if ($number == 3);
158 # return x1,x2,...
159 $number =-3;
160 return "x$number";
163 # Add a new test to internal data structures and fill in the
164 # ulps and exception information for the C line.
165 sub new_test {
166 my ($test, $exception, $show_exception) = @_;
167 my $rest;
169 # Add ulp.
170 if (exists $results{$test}{'has_ulps'}) {
171 $rest = ", DELTA$count";
172 } else {
173 $rest = ', 0';
175 if ($show_exception) {
176 if (defined $exception) {
177 $rest .= ", $exception";
178 } else {
179 $rest .= ', 0';
182 # We must increment here to keep @tests and count in sync
183 push @tests, $test;
184 ++$count;
185 return $rest;
188 # Treat some functions especially.
189 # Currently only sincos needs extra treatment.
190 sub special_functions {
191 my ($file, $args) = @_;
192 my (@args, $str, $test, $cline);
194 @args = split /,\s*/, $args;
196 unless ($args[0] =~ /sincos/) {
197 die ("Don't know how to handle $args[0] extra.");
199 $cline = " { $args[1]";
201 $str = 'sincos (' . &beautify ($args[1]) . ', &sin_res, &cos_res)';
202 # handle sin
203 $test = $str . ' puts ' . &beautify ($args[2]) . ' in sin_res';
205 $cline .= ", \"$test\", $args[2]";
206 $cline .= &new_test ($test, $args[4], 0);
208 # handle cos
209 $test = $str . ' puts ' . &beautify ($args[3]) . ' in cos_res';
210 $cline .= ", \"$test\", $args[3]";
211 $cline .= &new_test ($test, $args[4], 1);
212 $cline .= " },\n";
213 print $file $cline;
216 # Parse the arguments to TEST_x_y
217 sub parse_args {
218 my ($file, $descr, $fct, $args) = @_;
219 my (@args, $str, $descr_args, $descr_res, @descr);
220 my ($current_arg, $cline, $i);
221 my (@special);
222 my ($extra_var, $call);
224 if ($descr eq 'extra') {
225 &special_functions ($file, $args);
226 return;
228 ($descr_args, $descr_res) = split /_/,$descr, 2;
230 @args = split /,\s*/, $args;
232 $call = "$fct (";
234 # Generate first the string that's shown to the user
235 $current_arg = 1;
236 $extra_var = 0;
237 @descr = split //,$descr_args;
238 for ($i = 0; $i <= $#descr; $i++) {
239 if ($i >= 1) {
240 $call .= ', ';
242 # FLOAT, int, long int, long long int
243 if ($descr[$i] =~ /f|i|l|L/) {
244 $call .= &beautify ($args[$current_arg]);
245 ++$current_arg;
246 next;
248 # &FLOAT, &int - argument is added here
249 if ($descr[$i] =~ /F|I/) {
250 ++$extra_var;
251 $call .= '&' . &get_variable ($extra_var);
252 next;
254 # complex
255 if ($descr[$i] eq 'c') {
256 $call .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
257 $current_arg += 2;
258 next;
261 die ("$descr[$i] is unknown");
263 $call .= ')';
264 $str = "$call == ";
266 # Result
267 @descr = split //,$descr_res;
268 foreach (@descr) {
269 if ($_ =~ /f|i|l|L/) {
270 $str .= &beautify ($args[$current_arg]);
271 ++$current_arg;
272 } elsif ($_ eq 'c') {
273 $str .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
274 $current_arg += 2;
275 } elsif ($_ eq 'b') {
276 # boolean
277 $str .= ($args[$current_arg] == 0) ? "false" : "true";
278 ++$current_arg;
279 } elsif ($_ eq '1') {
280 ++$current_arg;
281 } else {
282 die ("$_ is unknown");
285 # consistency check
286 if ($current_arg == $#args) {
287 die ("wrong number of arguments")
288 unless ($args[$current_arg] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN/);
289 } elsif ($current_arg < $#args) {
290 die ("wrong number of arguments");
291 } elsif ($current_arg > ($#args+1)) {
292 die ("wrong number of arguments");
296 # Put the C program line together
297 # Reset some variables to start again
298 $current_arg = 1;
299 $extra_var = 0;
300 $cline = "{ \"$str\"";
301 @descr = split //,$descr_args;
302 for ($i=0; $i <= $#descr; $i++) {
303 # FLOAT, int, long int, long long int
304 if ($descr[$i] =~ /f|i|l|L/) {
305 $cline .= ", $args[$current_arg]";
306 $current_arg++;
307 next;
309 # &FLOAT, &int
310 if ($descr[$i] =~ /F|I/) {
311 next;
313 # complex
314 if ($descr[$i] eq 'c') {
315 $cline .= ", $args[$current_arg], $args[$current_arg+1]";
316 $current_arg += 2;
317 next;
320 $cline .= ", ";
322 @descr = split //,$descr_res;
323 foreach (@descr) {
324 if ($_ =~ /b|f|i|l|L/ ) {
325 $cline .= $args[$current_arg];
326 $current_arg++;
327 } elsif ($_ eq 'c') {
328 $cline .= "$args[$current_arg], $args[$current_arg+1]";
329 $current_arg += 2;
330 } elsif ($_ eq '1') {
331 push @special, $args[$current_arg];
332 ++$current_arg;
335 # Add ulp.
336 $cline .= &new_test ($str, ($current_arg <= $#args) ? $args[$current_arg] : undef, 1);
338 # special treatment for some functions
339 if ($args[0] eq 'frexp') {
340 if (defined $special[0]) {
341 my ($extra_expected) = $special[0];
342 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
343 my ($str) = "$call sets x to $extra_expected";
344 if (!$run_extra) {
345 $str = "";
346 $extra_expected = "0";
348 $cline .= ", \"$str\", 123456789, $run_extra, $extra_expected";
349 if ($run_extra) {
350 $cline .= &new_test ($str, undef, 0);
351 } else {
352 $cline .= ", 0";
355 } elsif ($args[0] eq 'gamma' || $args[0] eq 'lgamma') {
356 if (defined $special[0]) {
357 my ($extra_expected) = $special[0];
358 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
359 my ($str) = "$call sets signgam to $extra_expected";
360 if (!$run_extra) {
361 $str = "";
362 $extra_expected = "0";
364 $cline .= ", \"$str\", 0, $run_extra, $extra_expected";
365 if ($run_extra) {
366 $cline .= &new_test ($str, undef, 0);
367 } else {
368 $cline .= ", 0";
371 } elsif ($args[0] eq 'modf') {
372 if (defined $special[0]) {
373 my ($extra_expected) = $special[0];
374 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
375 my ($str) = "$call sets x to $extra_expected";
376 if (!$run_extra) {
377 $str = "";
378 $extra_expected = "0";
380 $cline .= ", \"$str\", 123.456789, $run_extra, $extra_expected";
381 if ($run_extra) {
382 $cline .= &new_test ($str, undef, 0);
383 } else {
384 $cline .= ", 0";
387 } elsif ($args[0] eq 'remquo') {
388 if (defined $special[0]) {
389 my ($extra_expected) = $special[0];
390 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
391 my ($str) = "$call sets x to $extra_expected";
392 if (!$run_extra) {
393 $str = "";
394 $extra_expected = "0";
396 $cline .= ", \"$str\", 123456789, $run_extra, $extra_expected";
397 if ($run_extra) {
398 $cline .= &new_test ($str, undef, 0);
399 } else {
400 $cline .= ", 0";
404 print $file " $cline },\n";
407 # Generate libm-test.c
408 sub generate_testfile {
409 my ($input, $output) = @_;
410 my ($lasttext);
411 my (@args, $i, $str, $thisfct);
413 open INPUT, $input or die ("Can't open $input: $!");
414 open OUTPUT, ">$output" or die ("Can't open $output: $!");
416 # Replace the special macros
417 while (<INPUT>) {
419 # TEST_...
420 if (/^\s*TEST_/) {
421 my ($descr, $args);
422 chop;
423 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
424 &parse_args (\*OUTPUT, $descr, $thisfct, $args);
425 next;
427 # START_DATA (function)
428 if (/START_DATA/) {
429 ($thisfct) = ($_ =~ /START_DATA\s*\((.*)\)/);
430 next;
432 # START (function)
433 if (/START/) {
434 ($thisfct) = ($_ =~ /START\s*\((.*)\)/);
435 print OUTPUT " init_max_error ();\n";
436 next;
438 # END_DATA (function)
439 if (/END_DATA/) {
440 next;
442 # END (function)
443 if (/END/) {
444 my ($fct, $line, $type);
445 if (/complex/) {
446 s/,\s*complex\s*//;
447 $type = 'complex';
448 } else {
449 $type = 'normal';
451 ($fct) = ($_ =~ /END\s*\((.*)\)/);
452 if ($type eq 'complex') {
453 $line = " print_complex_max_error (\"$fct\", ";
454 } else {
455 $line = " print_max_error (\"$fct\", ";
457 if (exists $results{$fct}{'has_ulps'}) {
458 $line .= "DELTA$fct";
459 } else {
460 $line .= '0';
462 $line .= ");\n";
463 print OUTPUT $line;
464 push @functions, $fct;
465 next;
467 print OUTPUT;
469 close INPUT;
470 close OUTPUT;
475 # Parse ulps file
476 sub parse_ulps {
477 my ($file) = @_;
478 my ($test, $type, $float, $eps, $kind);
480 # $type has the following values:
481 # "normal": No complex variable
482 # "real": Real part of complex result
483 # "imag": Imaginary part of complex result
484 open ULP, $file or die ("Can't open $file: $!");
485 while (<ULP>) {
486 chop;
487 # ignore comments and empty lines
488 next if /^#/;
489 next if /^\s*$/;
490 if (/^Test/) {
491 if (/Real part of:/) {
492 s/Real part of: //;
493 $type = 'real';
494 } elsif (/Imaginary part of:/) {
495 s/Imaginary part of: //;
496 $type = 'imag';
497 } else {
498 $type = 'normal';
500 s/^.+\"(.*)\".*$/$1/;
501 $test = $_;
502 $kind = 'test';
503 next;
505 if (/^Function: /) {
506 if (/Real part of/) {
507 s/Real part of //;
508 $type = 'real';
509 } elsif (/Imaginary part of/) {
510 s/Imaginary part of //;
511 $type = 'imag';
512 } else {
513 $type = 'normal';
515 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
516 $kind = 'fct';
517 next;
519 if (/^i?(float|double|ldouble):/) {
520 ($float, $eps) = split /\s*:\s*/,$_,2;
522 if ($eps eq "0") {
523 # ignore
524 next;
525 } else {
526 $results{$test}{$type}{'ulp'}{$float} = $eps;
527 $results{$test}{'has_ulps'} = 1;
529 if ($type =~ /^real|imag$/) {
530 $results{$test}{'type'} = 'complex';
531 } elsif ($type eq 'normal') {
532 $results{$test}{'type'} = 'normal';
534 $results{$test}{'kind'} = $kind;
535 next;
537 print "Skipping unknown entry: `$_'\n";
539 close ULP;
543 # Clean up a floating point number
544 sub clean_up_number {
545 my ($number) = @_;
547 # Remove trailing zeros after the decimal point
548 if ($number =~ /\./) {
549 $number =~ s/0+$//;
550 $number =~ s/\.$//;
552 return $number;
555 # Output a file which can be read in as ulps file.
556 sub print_ulps_file {
557 my ($file) = @_;
558 my ($test, $type, $float, $eps, $fct, $last_fct);
560 $last_fct = '';
561 open NEWULP, ">$file" or die ("Can't open $file: $!");
562 print NEWULP "# Begin of automatic generation\n";
563 # first the function calls
564 foreach $test (sort keys %results) {
565 next if ($results{$test}{'kind'} ne 'test');
566 foreach $type ('real', 'imag', 'normal') {
567 if (exists $results{$test}{$type}) {
568 if (defined $results{$test}) {
569 ($fct) = ($test =~ /^(\w+)\s/);
570 if ($fct ne $last_fct) {
571 $last_fct = $fct;
572 print NEWULP "\n# $fct\n";
575 if ($type eq 'normal') {
576 print NEWULP "Test \"$test\":\n";
577 } elsif ($type eq 'real') {
578 print NEWULP "Test \"Real part of: $test\":\n";
579 } elsif ($type eq 'imag') {
580 print NEWULP "Test \"Imaginary part of: $test\":\n";
582 foreach $float (@all_floats) {
583 if (exists $results{$test}{$type}{'ulp'}{$float}) {
584 print NEWULP "$float: ",
585 &clean_up_number ($results{$test}{$type}{'ulp'}{$float}),
586 "\n";
592 print NEWULP "\n# Maximal error of functions:\n";
594 foreach $fct (sort keys %results) {
595 next if ($results{$fct}{'kind'} ne 'fct');
596 foreach $type ('real', 'imag', 'normal') {
597 if (exists $results{$fct}{$type}) {
598 if ($type eq 'normal') {
599 print NEWULP "Function: \"$fct\":\n";
600 } elsif ($type eq 'real') {
601 print NEWULP "Function: Real part of \"$fct\":\n";
602 } elsif ($type eq 'imag') {
603 print NEWULP "Function: Imaginary part of \"$fct\":\n";
605 foreach $float (@all_floats) {
606 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
607 print NEWULP "$float: ",
608 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
609 "\n";
612 print NEWULP "\n";
616 print NEWULP "# end of automatic generation\n";
617 close NEWULP;
620 sub get_ulps {
621 my ($test, $type, $float) = @_;
623 if ($type eq 'complex') {
624 my ($res);
625 # Return 0 instead of BUILD_COMPLEX_ULP (0,0)
626 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
627 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
628 return "0";
630 $res = 'BUILD_COMPLEX_ULP (';
631 $res .= (exists $results{$test}{'real'}{'ulp'}{$float}
632 ? $results{$test}{'real'}{'ulp'}{$float} : "0");
633 $res .= ', ';
634 $res .= (exists $results{$test}{'imag'}{'ulp'}{$float}
635 ? $results{$test}{'imag'}{'ulp'}{$float} : "0");
636 $res .= ')';
637 return $res;
639 return (exists $results{$test}{'normal'}{'ulp'}{$float}
640 ? $results{$test}{'normal'}{'ulp'}{$float} : "0");
643 # Output the defines for a single test
644 sub output_test {
645 my ($file, $test, $name) = @_;
646 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
647 my ($type);
649 # Do we have ulps?
650 if (!exists $results{$test}{'type'}) {
651 return;
653 $type = $results{$test}{'type'};
654 if (exists $results{$test}{'has_ulps'}) {
655 # XXX use all_floats (change order!)
656 $ldouble = &get_ulps ($test, $type, "ldouble");
657 $double = &get_ulps ($test, $type, "double");
658 $float = &get_ulps ($test, $type, "float");
659 $ildouble = &get_ulps ($test, $type, "ildouble");
660 $idouble = &get_ulps ($test, $type, "idouble");
661 $ifloat = &get_ulps ($test, $type, "ifloat");
662 print $file "#define DELTA$name CHOOSE($ldouble, $double, $float, $ildouble, $idouble, $ifloat)\t/* $test */\n";
666 # Print include file
667 sub output_ulps {
668 my ($file, $ulps_filename) = @_;
669 my ($i, $fct);
671 open ULP, ">$file" or die ("Can't open $file: $!");
673 print ULP "/* This file is automatically generated\n";
674 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
675 print ULP " Don't change it - change instead the master files. */\n\n";
677 print ULP "\n/* Maximal error of functions. */\n";
678 foreach $fct (@functions) {
679 output_test (\*ULP, $fct, $fct);
682 print ULP "\n/* Error of single function calls. */\n";
683 for ($i = 0; $i < $count; $i++) {
684 output_test (\*ULP, $tests[$i], $i);
686 close ULP;