manual/message.texi: Fix english and clarify.
[glibc.git] / math / gen-libm-test.pl
blob2f9389898a60fd8db9c39ab7148a4a8d0c4a7e92
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) = @_;
167 my $rest;
169 # Add ulp.
170 if (exists $results{$test}{'has_ulps'}) {
171 $rest = ", DELTA$count";
172 } else {
173 $rest = ', 0';
175 if (defined $exception) {
176 $rest .= ", $exception";
177 } else {
178 $rest .= ', 0';
180 $rest .= ");\n";
181 # We must increment here to keep @tests and count in sync
182 push @tests, $test;
183 ++$count;
184 return $rest;
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.");
198 print $file " {\n";
199 print $file " FUNC (sincos) ($args[1], &sin_res, &cos_res);\n";
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 = " check_float (\"$test\", sin_res, $args[2]";
206 $cline .= &new_test ($test, $args[4]);
207 print $file $cline;
209 # handle cos
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);
214 print $file $cline;
215 print $file " }\n";
218 # Parse the arguments to TEST_x_y
219 sub parse_args {
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);
228 return;
230 ($descr_args, $descr_res) = split /_/,$descr, 2;
232 @args = split /,\s*/, $args;
234 $call = "$fct (";
236 # Generate first the string that's shown to the user
237 $current_arg = 1;
238 $extra_var = 0;
239 @descr = split //,$descr_args;
240 for ($i = 0; $i <= $#descr; $i++) {
241 if ($i >= 1) {
242 $call .= ', ';
244 # FLOAT, int, long int, long long int
245 if ($descr[$i] =~ /f|i|l|L/) {
246 $call .= &beautify ($args[$current_arg]);
247 ++$current_arg;
248 next;
250 # &FLOAT, &int - argument is added here
251 if ($descr[$i] =~ /F|I/) {
252 ++$extra_var;
253 $call .= '&' . &get_variable ($extra_var);
254 next;
256 # complex
257 if ($descr[$i] eq 'c') {
258 $call .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
259 $current_arg += 2;
260 next;
263 die ("$descr[$i] is unknown");
265 $call .= ')';
266 $str = "$call == ";
268 # Result
269 @descr = split //,$descr_res;
270 foreach (@descr) {
271 if ($_ =~ /f|i|l|L/) {
272 $str .= &beautify ($args[$current_arg]);
273 ++$current_arg;
274 } elsif ($_ eq 'c') {
275 $str .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
276 $current_arg += 2;
277 } elsif ($_ eq 'b') {
278 # boolean
279 $str .= ($args[$current_arg] == 0) ? "false" : "true";
280 ++$current_arg;
281 } elsif ($_ eq '1') {
282 ++$current_arg;
283 } else {
284 die ("$_ is unknown");
287 # consistency check
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
300 $current_arg = 1;
301 $extra_var = 0;
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] (";
321 } else {
322 $c_call = " FUNC($args[0]) (";
324 @descr = split //,$descr_args;
325 for ($i=0; $i <= $#descr; $i++) {
326 if ($i >= 1) {
327 $c_call .= ', ';
329 # FLOAT, int, long int, long long int
330 if ($descr[$i] =~ /f|i|l|L/) {
331 $c_call .= $args[$current_arg];
332 $current_arg++;
333 next;
335 # &FLOAT, &int
336 if ($descr[$i] =~ /F|I/) {
337 ++$extra_var;
338 $c_call .= '&' . &get_variable ($extra_var);
339 next;
341 # complex
342 if ($descr[$i] eq 'c') {
343 $c_call .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
344 $current_arg += 2;
345 next;
348 $c_call .= ')';
349 $cline .= "$c_call, ";
351 @descr = split //,$descr_res;
352 foreach (@descr) {
353 if ($_ =~ /b|f|i|l|L/ ) {
354 $cline .= $args[$current_arg];
355 $current_arg++;
356 } elsif ($_ eq 'c') {
357 $cline .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
358 $current_arg += 2;
359 } elsif ($_ eq '1') {
360 push @special, $args[$current_arg];
361 ++$current_arg;
364 # Add ulp.
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) {
399 print $file " {\n";
400 print $file " $pre" if (defined $pre);
401 print $file " $cline";
402 print $file " $post" if (defined $post);
403 print $file " }\n";
404 } else {
405 print $file " $cline";
409 # Generate libm-test.c
410 sub generate_testfile {
411 my ($input, $output) = @_;
412 my ($lasttext);
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
419 while (<INPUT>) {
421 # TEST_...
422 if (/^\s*TEST_/) {
423 my ($descr, $args);
424 chop;
425 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
426 &parse_args (\*OUTPUT, $descr, $thisfct, $args);
427 next;
429 # START (function)
430 if (/START/) {
431 ($thisfct) = ($_ =~ /START\s*\((.*)\)/);
432 print OUTPUT " init_max_error ();\n";
433 next;
435 # END (function)
436 if (/END/) {
437 my ($fct, $line, $type);
438 if (/complex/) {
439 s/,\s*complex\s*//;
440 $type = 'complex';
441 } else {
442 $type = 'normal';
444 ($fct) = ($_ =~ /END\s*\((.*)\)/);
445 if ($type eq 'complex') {
446 $line = " print_complex_max_error (\"$fct\", ";
447 } else {
448 $line = " print_max_error (\"$fct\", ";
450 if (exists $results{$fct}{'has_ulps'}) {
451 $line .= "DELTA$fct";
452 } else {
453 $line .= '0';
455 $line .= ");\n";
456 print OUTPUT $line;
457 push @functions, $fct;
458 next;
460 print OUTPUT;
462 close INPUT;
463 close OUTPUT;
468 # Parse ulps file
469 sub parse_ulps {
470 my ($file) = @_;
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: $!");
478 while (<ULP>) {
479 chop;
480 # ignore comments and empty lines
481 next if /^#/;
482 next if /^\s*$/;
483 if (/^Test/) {
484 if (/Real part of:/) {
485 s/Real part of: //;
486 $type = 'real';
487 } elsif (/Imaginary part of:/) {
488 s/Imaginary part of: //;
489 $type = 'imag';
490 } else {
491 $type = 'normal';
493 s/^.+\"(.*)\".*$/$1/;
494 $test = $_;
495 $kind = 'test';
496 next;
498 if (/^Function: /) {
499 if (/Real part of/) {
500 s/Real part of //;
501 $type = 'real';
502 } elsif (/Imaginary part of/) {
503 s/Imaginary part of //;
504 $type = 'imag';
505 } else {
506 $type = 'normal';
508 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
509 $kind = 'fct';
510 next;
512 if (/^i?(float|double|ldouble):/) {
513 ($float, $eps) = split /\s*:\s*/,$_,2;
515 if ($eps eq "0") {
516 # ignore
517 next;
518 } else {
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;
528 next;
530 print "Skipping unknown entry: `$_'\n";
532 close ULP;
536 # Clean up a floating point number
537 sub clean_up_number {
538 my ($number) = @_;
540 # Remove trailing zeros after the decimal point
541 if ($number =~ /\./) {
542 $number =~ s/0+$//;
543 $number =~ s/\.$//;
545 return $number;
548 # Output a file which can be read in as ulps file.
549 sub print_ulps_file {
550 my ($file) = @_;
551 my ($test, $type, $float, $eps, $fct, $last_fct);
553 $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) {
564 $last_fct = $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}),
579 "\n";
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}),
602 "\n";
605 print NEWULP "\n";
609 print NEWULP "# end of automatic generation\n";
610 close NEWULP;
613 sub get_ulps {
614 my ($test, $type, $float) = @_;
616 if ($type eq 'complex') {
617 my ($res);
618 # Return 0 instead of BUILD_COMPLEX (0,0)
619 if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
620 !exists $results{$test}{'imag'}{'ulp'}{$float}) {
621 return "0";
623 $res = 'BUILD_COMPLEX (';
624 $res .= (exists $results{$test}{'real'}{'ulp'}{$float}
625 ? $results{$test}{'real'}{'ulp'}{$float} : "0");
626 $res .= ', ';
627 $res .= (exists $results{$test}{'imag'}{'ulp'}{$float}
628 ? $results{$test}{'imag'}{'ulp'}{$float} : "0");
629 $res .= ')';
630 return $res;
632 return (exists $results{$test}{'normal'}{'ulp'}{$float}
633 ? $results{$test}{'normal'}{'ulp'}{$float} : "0");
636 # Output the defines for a single test
637 sub output_test {
638 my ($file, $test, $name) = @_;
639 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
640 my ($type);
642 # Do we have ulps?
643 if (!exists $results{$test}{'type'}) {
644 return;
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";
659 # Print include file
660 sub output_ulps {
661 my ($file, $ulps_filename) = @_;
662 my ($i, $fct);
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);
679 close ULP;