S390: Move utf8-utf32-z9.c to multiarch folder and use s390_libc_ifunc_expr macro.
[glibc.git] / math / gen-libm-test.pl
bloba931bc542f1ba2c162547d2569a8523b26546aae
1 #!/usr/bin/perl -w
2 # Copyright (C) 1999-2017 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}{"type"} is the result type, e.g. normal or complex.
25 # $results{$test}{"has_ulps"} is set if deltas exist.
26 # In the following description $type and $float are:
27 # - $type is either "normal", "real" (for the real part of a complex number)
28 # or "imag" (for the imaginary part # of a complex number).
29 # - $float is either of float, ifloat, double, idouble, ldouble, ildouble;
30 # It represents the underlying floating point type (float, double or long
31 # double) and if inline functions (the leading i stands for inline)
32 # are used.
33 # $results{$test}{$type}{"ulp"}{$float} is defined and has a delta as value
36 use Getopt::Std;
38 use strict;
40 use vars qw ($input $output $auto_input);
41 use vars qw (%results);
42 use vars qw (%beautify @all_floats %all_floats_pfx);
43 use vars qw ($ulps_file);
44 use vars qw (%auto_tests);
46 # all_floats is sorted and contains all recognised float types
47 @all_floats = ('double', 'float', 'idouble',
48 'ifloat', 'ildouble', 'ldouble');
50 # all_floats_pfx maps C types to their C like prefix for macros.
51 %all_floats_pfx =
52 ( "double" => "DBL",
53 "ldouble" => "LDBL",
54 "float" => "FLT",
57 %beautify =
58 ( "minus_zero" => "-0",
59 "plus_zero" => "+0",
60 "-0x0p+0f" => "-0",
61 "-0x0p+0" => "-0",
62 "-0x0p+0L" => "-0",
63 "0x0p+0f" => "+0",
64 "0x0p+0" => "+0",
65 "0x0p+0L" => "+0",
66 "minus_infty" => "-inf",
67 "plus_infty" => "inf",
68 "qnan_value" => "qNaN",
69 "snan_value" => "sNaN",
70 "snan_value_ld" => "sNaN",
74 # get Options
75 # Options:
76 # a: auto-libm-test-out input file
77 # c: .inc input file
78 # u: ulps-file
79 # n: new ulps file
80 # C: libm-test.c output file
81 # H: libm-test-ulps.h output file
82 # h: help
83 use vars qw($opt_a $opt_c $opt_u $opt_n $opt_C $opt_H $opt_h);
84 getopts('a:c:u:n:C:H:h');
86 $ulps_file = 'libm-test-ulps';
88 if ($opt_h) {
89 print "Usage: gen-libm-test.pl [OPTIONS]\n";
90 print " -h print this help, then exit\n";
91 print " -a FILE input file with automatically generated tests\n";
92 print " -c FILE input file .inc file with tests\n";
93 print " -u FILE input file with ulps\n";
94 print " -n FILE generate sorted file FILE from libm-test-ulps\n";
95 print " -C FILE generate output C file FILE from libm-test.inc\n";
96 print " -H FILE generate output ulps header FILE from libm-test-ulps\n";
97 exit 0;
100 $ulps_file = $opt_u if ($opt_u);
102 $input = $opt_c if ($opt_c);
103 $auto_input = $opt_a if ($opt_a);
104 $output = $opt_C if ($opt_C);
106 &parse_ulps ($ulps_file) if ($opt_H || $opt_n);
107 &parse_auto_input ($auto_input) if ($opt_C);
108 &generate_testfile ($input, $output) if ($opt_C);
109 &output_ulps ($opt_H, $ulps_file) if ($opt_H);
110 &print_ulps_file ($opt_n) if ($opt_n);
112 # Return a nicer representation
113 sub beautify {
114 my ($arg) = @_;
115 my ($tmp);
117 if (exists $beautify{$arg}) {
118 return $beautify{$arg};
120 if ($arg =~ /^-/) {
121 $tmp = $arg;
122 $tmp =~ s/^-//;
123 if (exists $beautify{$tmp}) {
124 return '-' . $beautify{$tmp};
127 if ($arg =~ /^-?0x[0-9a-f.]*p[-+][0-9]+f$/) {
128 $arg =~ s/f$//;
130 if ($arg =~ /[0-9]L$/) {
131 $arg =~ s/L$//;
133 return $arg;
136 # Return a nicer representation of a complex number
137 sub build_complex_beautify {
138 my ($r, $i) = @_;
139 my ($str1, $str2);
141 $str1 = &beautify ($r);
142 $str2 = &beautify ($i);
143 if ($str2 =~ /^-/) {
144 $str2 =~ s/^-//;
145 $str1 .= ' - ' . $str2;
146 } else {
147 $str1 .= ' + ' . $str2;
149 $str1 .= ' i';
150 return $str1;
153 # Return the text to put in an initializer for a test's exception
154 # information.
155 sub show_exceptions {
156 my ($ignore_result, $non_finite, $test_snan, $exception) = @_;
157 $ignore_result = ($ignore_result ? "IGNORE_RESULT|" : "");
158 $non_finite = ($non_finite ? "NON_FINITE|" : "");
159 $test_snan = ($test_snan ? "TEST_SNAN|" : "");
160 if (defined $exception) {
161 return ", ${ignore_result}${non_finite}${test_snan}$exception";
162 } else {
163 return ", ${ignore_result}${non_finite}${test_snan}0";
167 # Apply the LIT(x) macro to a literal floating point constant
168 # and strip any existing suffix.
169 sub _apply_lit {
170 my ($lit) = @_;
171 my $exp_re = "([+-])?[[:digit:]]+";
172 # Don't wrap something that does not look like a:
173 # * Hexadecimal FP value
174 # * Decimal FP value without a decimal point
175 # * Decimal value with a fraction
176 return $lit if $lit !~ /([+-])?0x[[:xdigit:]\.]+[pP]$exp_re/
177 and $lit !~ /[[:digit:]]+[eE]$exp_re/
178 and $lit !~ /[[:digit:]]*\.[[:digit:]]*([eE]$exp_re)?/;
180 # Strip any existing literal suffix.
181 $lit =~ s/[lLfF]$//;
183 return "LIT (${lit})";
186 # Apply LIT macro to individual tokens within an expression.
188 # This function assumes the C expression follows GNU coding
189 # standards. Specifically, a space separates each lexical
190 # token. Otherwise, this post-processing may apply LIT
191 # incorrectly, or around an entire expression.
192 sub apply_lit {
193 my ($lit) = @_;
194 my @toks = split (/ /, $lit);
195 foreach (@toks) {
196 $_ = _apply_lit ($_);
198 return join (' ', @toks);
201 # Parse the arguments to TEST_x_y
202 sub parse_args {
203 my ($file, $descr, $args) = @_;
204 my (@args, $descr_args, $descr_res, @descr);
205 my ($current_arg, $cline, $cline_res, $i);
206 my (@special);
207 my ($call_args);
208 my ($ignore_result_any, $ignore_result_all);
209 my ($num_res, @args_res, @start_rm, $rm);
210 my (@plus_oflow, @minus_oflow, @plus_uflow, @minus_uflow);
211 my (@errno_plus_oflow, @errno_minus_oflow);
212 my (@errno_plus_uflow, @errno_minus_uflow);
213 my (@xfail_rounding_ibm128_libgcc);
214 my ($non_finite, $test_snan);
216 ($descr_args, $descr_res) = split /_/,$descr, 2;
218 @args = split /,\s*/, $args;
220 $call_args = "";
222 # Generate first the string that's shown to the user
223 $current_arg = 1;
224 @descr = split //,$descr_args;
225 for ($i = 0; $i <= $#descr; $i++) {
226 my $comma = "";
227 if ($current_arg > 1) {
228 $comma = ', ';
230 # FLOAT, long double, int, unsigned int, long int, long long int
231 if ($descr[$i] =~ /f|j|i|u|l|L/) {
232 $call_args .= $comma . &beautify ($args[$current_arg]);
233 ++$current_arg;
234 next;
236 # Argument passed via pointer.
237 if ($descr[$i] =~ /p/) {
238 next;
240 # &FLOAT, &int - simplify call by not showing argument.
241 if ($descr[$i] =~ /F|I/) {
242 next;
244 # complex
245 if ($descr[$i] eq 'c') {
246 $call_args .= $comma . &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
247 $current_arg += 2;
248 next;
251 die ("$descr[$i] is unknown");
254 # Result
255 @args_res = @args[$current_arg .. $#args];
256 $num_res = 0;
257 @descr = split //,$descr_res;
258 foreach (@descr) {
259 if ($_ =~ /f|i|l|L|M|U/) {
260 ++$num_res;
261 } elsif ($_ eq 'c') {
262 $num_res += 2;
263 } elsif ($_ eq 'b') {
264 # boolean
265 ++$num_res;
266 } elsif ($_ eq '1') {
267 ++$num_res;
268 } else {
269 die ("$_ is unknown");
272 # consistency check
273 if ($#args_res == $num_res - 1) {
274 # One set of results for all rounding modes, no flags.
275 @start_rm = ( 0, 0, 0, 0 );
276 } elsif ($#args_res == $num_res) {
277 # One set of results for all rounding modes, with flags.
278 die ("wrong number of arguments")
279 unless ($args_res[$#args_res] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN|TEST_NAN_SIGN|NO_TEST_INLINE|XFAIL/);
280 @start_rm = ( 0, 0, 0, 0 );
281 } elsif ($#args_res == 4 * $num_res + 3) {
282 # One set of results per rounding mode, with flags.
283 @start_rm = ( 0, $num_res + 1, 2 * $num_res + 2, 3 * $num_res + 3 );
284 } else {
285 die ("wrong number of arguments");
288 # Put the C program line together
289 # Reset some variables to start again
290 $current_arg = 1;
291 $call_args =~ s/\"/\\\"/g;
292 $cline = "{ \"$call_args\"";
293 @descr = split //,$descr_args;
294 for ($i=0; $i <= $#descr; $i++) {
295 # FLOAT, int, long int, long long int
296 if ($descr[$i] =~ /f|j|i|u|l|L/) {
297 if ($descr[$i] eq "f") {
298 $cline .= ", " . &apply_lit ($args[$current_arg]);
299 } else {
300 $cline .= ", $args[$current_arg]";
302 $current_arg++;
303 next;
305 # &FLOAT, &int, argument passed via pointer
306 if ($descr[$i] =~ /F|I|p/) {
307 next;
309 # complex
310 if ($descr[$i] eq 'c') {
311 $cline .= ", " . &apply_lit ($args[$current_arg]);
312 $cline .= ", " . &apply_lit ($args[$current_arg+1]);
313 $current_arg += 2;
314 next;
318 @descr = split //,$descr_res;
319 @plus_oflow = qw(max_value plus_infty max_value plus_infty);
320 @minus_oflow = qw(minus_infty minus_infty -max_value -max_value);
321 @plus_uflow = qw(plus_zero plus_zero plus_zero min_subnorm_value);
322 @minus_uflow = qw(-min_subnorm_value minus_zero minus_zero minus_zero);
323 @errno_plus_oflow = qw(0 ERRNO_ERANGE 0 ERRNO_ERANGE);
324 @errno_minus_oflow = qw(ERRNO_ERANGE ERRNO_ERANGE 0 0);
325 @errno_plus_uflow = qw(ERRNO_ERANGE ERRNO_ERANGE ERRNO_ERANGE 0);
326 @errno_minus_uflow = qw(0 ERRNO_ERANGE ERRNO_ERANGE ERRNO_ERANGE);
327 @xfail_rounding_ibm128_libgcc = qw(XFAIL_IBM128_LIBGCC 0
328 XFAIL_IBM128_LIBGCC XFAIL_IBM128_LIBGCC);
329 for ($rm = 0; $rm <= 3; $rm++) {
330 $current_arg = $start_rm[$rm];
331 $ignore_result_any = 0;
332 $ignore_result_all = 1;
333 $cline_res = "";
334 @special = ();
335 foreach (@descr) {
336 if ($_ =~ /b|f|j|i|l|L|M|U/ ) {
337 my ($result) = $args_res[$current_arg];
338 if ($result eq "IGNORE") {
339 $ignore_result_any = 1;
340 $result = "0";
341 } else {
342 $ignore_result_all = 0;
344 if ($_ eq "f") {
345 $result = apply_lit ($result);
347 $cline_res .= ", $result";
348 $current_arg++;
349 } elsif ($_ eq 'c') {
350 my ($result1) = $args_res[$current_arg];
351 if ($result1 eq "IGNORE") {
352 $ignore_result_any = 1;
353 $result1 = "0";
354 } else {
355 $ignore_result_all = 0;
357 my ($result2) = $args_res[$current_arg + 1];
358 if ($result2 eq "IGNORE") {
359 $ignore_result_any = 1;
360 $result2 = "0";
361 } else {
362 $ignore_result_all = 0;
364 $result1 = apply_lit ($result1);
365 $result2 = apply_lit ($result2);
366 $cline_res .= ", $result1, $result2";
367 $current_arg += 2;
368 } elsif ($_ eq '1') {
369 push @special, $args_res[$current_arg];
370 ++$current_arg;
373 if ($ignore_result_any && !$ignore_result_all) {
374 die ("some but not all function results ignored\n");
376 # Determine whether any arguments or results, for any rounding
377 # mode, are non-finite.
378 $non_finite = ($args =~ /qnan_value|snan_value|plus_infty|minus_infty/);
379 $test_snan = ($args =~ /snan_value/);
380 # Add exceptions.
381 $cline_res .= show_exceptions ($ignore_result_any,
382 $non_finite,
383 $test_snan,
384 ($current_arg <= $#args_res)
385 ? $args_res[$current_arg]
386 : undef);
388 # special treatment for some functions
389 $i = 0;
390 foreach (@special) {
391 ++$i;
392 my ($extra_expected) = $_;
393 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
394 if (!$run_extra) {
395 $extra_expected = "0";
396 } else {
397 $extra_expected = apply_lit ($extra_expected);
399 $cline_res .= ", $run_extra, $extra_expected";
401 $cline_res =~ s/^, //;
402 $cline_res =~ s/plus_oflow/$plus_oflow[$rm]/g;
403 $cline_res =~ s/minus_oflow/$minus_oflow[$rm]/g;
404 $cline_res =~ s/plus_uflow/$plus_uflow[$rm]/g;
405 $cline_res =~ s/minus_uflow/$minus_uflow[$rm]/g;
406 $cline_res =~ s/ERRNO_PLUS_OFLOW/$errno_plus_oflow[$rm]/g;
407 $cline_res =~ s/ERRNO_MINUS_OFLOW/$errno_minus_oflow[$rm]/g;
408 $cline_res =~ s/ERRNO_PLUS_UFLOW/$errno_plus_uflow[$rm]/g;
409 $cline_res =~ s/ERRNO_MINUS_UFLOW/$errno_minus_uflow[$rm]/g;
410 $cline_res =~ s/XFAIL_ROUNDING_IBM128_LIBGCC/$xfail_rounding_ibm128_libgcc[$rm]/g;
411 $cline .= ", { $cline_res }";
413 print $file " $cline },\n";
416 # Convert a condition from auto-libm-test-out to C form.
417 sub convert_condition {
418 my ($cond) = @_;
419 my (@conds, $ret);
420 @conds = split /:/, $cond;
421 foreach (@conds) {
422 s/-/_/g;
423 s/^/TEST_COND_/;
425 $ret = join " && ", @conds;
426 return "($ret)";
429 # Return text to OR a value into an accumulated flags string.
430 sub or_value {
431 my ($cond) = @_;
432 if ($cond eq "0") {
433 return "";
434 } else {
435 return " | $cond";
439 # Return a conditional expression between two values.
440 sub cond_value {
441 my ($cond, $if, $else) = @_;
442 if ($cond eq "1") {
443 return $if;
444 } elsif ($cond eq "0") {
445 return $else;
446 } else {
447 return "($cond ? $if : $else)";
451 # Return text to OR a conditional expression between two values into
452 # an accumulated flags string.
453 sub or_cond_value {
454 my ($cond, $if, $else) = @_;
455 return or_value (cond_value ($cond, $if, $else));
458 # Generate libm-test.c
459 sub generate_testfile {
460 my ($input, $output) = @_;
462 open INPUT, $input or die ("Can't open $input: $!");
463 open OUTPUT, ">$output" or die ("Can't open $output: $!");
465 # Replace the special macros
466 while (<INPUT>) {
467 # AUTO_TESTS (function),
468 if (/^\s*AUTO_TESTS_/) {
469 my ($descr, $func, @modes, $auto_test, $num_auto_tests);
470 my (@rm_tests, $rm, $i);
471 @modes = qw(downward tonearest towardzero upward);
472 ($descr, $func) = ($_ =~ /AUTO_TESTS_(\w+)\s*\((\w+)\)/);
473 for ($rm = 0; $rm <= 3; $rm++) {
474 $rm_tests[$rm] = [sort keys %{$auto_tests{$func}{$modes[$rm]}}];
476 $num_auto_tests = scalar @{$rm_tests[0]};
477 for ($rm = 1; $rm <= 3; $rm++) {
478 if ($num_auto_tests != scalar @{$rm_tests[$rm]}) {
479 die ("inconsistent numbers of tests for $func\n");
481 for ($i = 0; $i < $num_auto_tests; $i++) {
482 if ($rm_tests[0][$i] ne $rm_tests[$rm][$i]) {
483 die ("inconsistent list of tests of $func\n");
487 if ($num_auto_tests == 0) {
488 die ("no automatic tests for $func\n");
490 foreach $auto_test (@{$rm_tests[0]}) {
491 my ($format, $inputs, $format_conv, $args_str);
492 ($format, $inputs) = split / /, $auto_test, 2;
493 $inputs =~ s/ /, /g;
494 $format_conv = convert_condition ($format);
495 print OUTPUT "#if $format_conv\n";
496 $args_str = "$func, $inputs";
497 for ($rm = 0; $rm <= 3; $rm++) {
498 my ($auto_test_out, $outputs, $flags);
499 my ($flags_conv, @flags, %flag_cond);
500 $auto_test_out = $auto_tests{$func}{$modes[$rm]}{$auto_test};
501 ($outputs, $flags) = split / : */, $auto_test_out;
502 $outputs =~ s/ /, /g;
503 @flags = split / /, $flags;
504 foreach (@flags) {
505 if (/^([^:]*):(.*)$/) {
506 my ($flag, $cond);
507 $flag = $1;
508 $cond = convert_condition ($2);
509 if (defined ($flag_cond{$flag})) {
510 if ($flag_cond{$flag} ne "1") {
511 $flag_cond{$flag} .= " || $cond";
513 } else {
514 $flag_cond{$flag} = $cond;
516 } else {
517 $flag_cond{$_} = "1";
520 $flags_conv = "";
521 if (defined ($flag_cond{"ignore-zero-inf-sign"})) {
522 $flags_conv .= or_cond_value ($flag_cond{"ignore-zero-inf-sign"},
523 "IGNORE_ZERO_INF_SIGN", "0");
525 if (defined ($flag_cond{"no-test-inline"})) {
526 $flags_conv .= or_cond_value ($flag_cond{"no-test-inline"},
527 "NO_TEST_INLINE", "0");
529 if (defined ($flag_cond{"xfail"})) {
530 $flags_conv .= or_cond_value ($flag_cond{"xfail"},
531 "XFAIL_TEST", "0");
533 my (@exc_list) = qw(divbyzero inexact invalid overflow underflow);
534 my ($exc);
535 foreach $exc (@exc_list) {
536 my ($exc_expected, $exc_ok, $no_exc, $exc_cond, $exc_ok_cond);
537 $exc_expected = "\U$exc\E_EXCEPTION";
538 $exc_ok = "\U$exc\E_EXCEPTION_OK";
539 $no_exc = "0";
540 if ($exc eq "inexact") {
541 $exc_ok = "0";
542 $no_exc = "NO_INEXACT_EXCEPTION";
544 if (defined ($flag_cond{$exc})) {
545 $exc_cond = $flag_cond{$exc};
546 } else {
547 $exc_cond = "0";
549 if (defined ($flag_cond{"$exc-ok"})) {
550 $exc_ok_cond = $flag_cond{"$exc-ok"};
551 } else {
552 $exc_ok_cond = "0";
554 $flags_conv .= or_cond_value ($exc_cond,
555 cond_value ($exc_ok_cond,
556 $exc_ok, $exc_expected),
557 cond_value ($exc_ok_cond,
558 $exc_ok, $no_exc));
560 my ($errno_expected, $errno_unknown_cond);
561 if (defined ($flag_cond{"errno-edom"})) {
562 if ($flag_cond{"errno-edom"} ne "1") {
563 die ("unexpected condition for errno-edom");
565 if (defined ($flag_cond{"errno-erange"})) {
566 die ("multiple errno values expected");
568 $errno_expected = "ERRNO_EDOM";
569 } elsif (defined ($flag_cond{"errno-erange"})) {
570 if ($flag_cond{"errno-erange"} ne "1") {
571 die ("unexpected condition for errno-erange");
573 $errno_expected = "ERRNO_ERANGE";
574 } else {
575 $errno_expected = "ERRNO_UNCHANGED";
577 if (defined ($flag_cond{"errno-edom-ok"})) {
578 if (defined ($flag_cond{"errno-erange-ok"})
579 && ($flag_cond{"errno-erange-ok"}
580 ne $flag_cond{"errno-edom-ok"})) {
581 $errno_unknown_cond = "($flag_cond{\"errno-edom-ok\"} || $flag_cond{\"errno-erange-ok\"})";
582 } else {
583 $errno_unknown_cond = $flag_cond{"errno-edom-ok"};
585 } elsif (defined ($flag_cond{"errno-erange-ok"})) {
586 $errno_unknown_cond = $flag_cond{"errno-erange-ok"};
587 } else {
588 $errno_unknown_cond = "0";
590 $flags_conv .= or_cond_value ($errno_unknown_cond,
591 "0", $errno_expected);
592 if ($flags_conv eq "") {
593 $flags_conv = ", NO_EXCEPTION";
594 } else {
595 $flags_conv =~ s/^ \|/,/;
597 $args_str .= ", $outputs$flags_conv";
599 &parse_args (\*OUTPUT, $descr, $args_str);
600 print OUTPUT "#endif\n";
602 next;
605 # TEST_...
606 if (/^\s*TEST_/) {
607 my ($descr, $args);
608 chop;
609 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
610 &parse_args (\*OUTPUT, $descr, $args);
611 next;
613 print OUTPUT;
615 close INPUT;
616 close OUTPUT;
621 # Parse ulps file
622 sub parse_ulps {
623 my ($file) = @_;
624 my ($test, $type, $float, $eps, $float_regex);
626 # Build a basic regex to match type entries in the
627 # generated ULPS file.
628 foreach my $ftype (@all_floats) {
629 $float_regex .= "|" . $ftype;
631 $float_regex = "^" . substr ($float_regex, 1) . ":";
633 # $type has the following values:
634 # "normal": No complex variable
635 # "real": Real part of complex result
636 # "imag": Imaginary part of complex result
637 open ULP, $file or die ("Can't open $file: $!");
638 while (<ULP>) {
639 chop;
640 # ignore comments and empty lines
641 next if /^#/;
642 next if /^\s*$/;
643 if (/^Function: /) {
644 if (/Real part of/) {
645 s/Real part of //;
646 $type = 'real';
647 } elsif (/Imaginary part of/) {
648 s/Imaginary part of //;
649 $type = 'imag';
650 } else {
651 $type = 'normal';
653 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
654 next;
656 if (/$float_regex/) {
657 ($float, $eps) = split /\s*:\s*/,$_,2;
659 if ($eps eq "0") {
660 # ignore
661 next;
662 } else {
663 if (!defined ($results{$test}{$type}{'ulp'}{$float})
664 || $results{$test}{$type}{'ulp'}{$float} < $eps) {
665 $results{$test}{$type}{'ulp'}{$float} = $eps;
666 $results{$test}{'has_ulps'} = 1;
669 if ($type =~ /^real|imag$/) {
670 $results{$test}{'type'} = 'complex';
671 } elsif ($type eq 'normal') {
672 $results{$test}{'type'} = 'normal';
674 next;
676 print "Skipping unknown entry: `$_'\n";
678 close ULP;
682 # Clean up a floating point number
683 sub clean_up_number {
684 my ($number) = @_;
686 # Remove trailing zeros after the decimal point
687 if ($number =~ /\./) {
688 $number =~ s/0+$//;
689 $number =~ s/\.$//;
691 return $number;
694 # Output a file which can be read in as ulps file.
695 sub print_ulps_file {
696 my ($file) = @_;
697 my ($test, $type, $float, $eps, $fct, $last_fct);
699 $last_fct = '';
700 open NEWULP, ">$file" or die ("Can't open $file: $!");
701 print NEWULP "# Begin of automatic generation\n";
702 print NEWULP "\n# Maximal error of functions:\n";
704 foreach $fct (sort keys %results) {
705 foreach $type ('real', 'imag', 'normal') {
706 if (exists $results{$fct}{$type}) {
707 if ($type eq 'normal') {
708 print NEWULP "Function: \"$fct\":\n";
709 } elsif ($type eq 'real') {
710 print NEWULP "Function: Real part of \"$fct\":\n";
711 } elsif ($type eq 'imag') {
712 print NEWULP "Function: Imaginary part of \"$fct\":\n";
714 foreach $float (@all_floats) {
715 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
716 print NEWULP "$float: ",
717 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
718 "\n";
721 print NEWULP "\n";
725 print NEWULP "# end of automatic generation\n";
726 close NEWULP;
729 sub get_ulps {
730 my ($test, $type, $float) = @_;
732 return (exists $results{$test}{$type}{'ulp'}{$float}
733 ? $results{$test}{$type}{'ulp'}{$float} : "0");
736 # Return the ulps value for a single test.
737 sub get_all_ulps_for_test {
738 my ($test, $type) = @_;
739 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
740 my ($ulps_str);
742 if (exists $results{$test}{'has_ulps'}) {
743 foreach $float (@all_floats) {
744 $ulps_str .= &get_ulps ($test, $type, $float) . ", ";
746 return "{" . substr ($ulps_str, 0, -2) . "}";
747 } else {
748 die "get_all_ulps_for_test called for \"$test\" with no ulps\n";
752 # Print include file
753 sub output_ulps {
754 my ($file, $ulps_filename) = @_;
755 my ($i, $fct, $type, $ulp, $ulp_real, $ulp_imag);
756 my (%func_ulps, %func_real_ulps, %func_imag_ulps);
758 open ULP, ">$file" or die ("Can't open $file: $!");
760 print ULP "/* This file is automatically generated\n";
761 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
762 print ULP " Don't change it - change instead the master files. */\n\n";
764 print ULP "struct ulp_data\n";
765 print ULP "{\n";
766 print ULP " const char *name;\n";
767 print ULP " FLOAT max_ulp[" . @all_floats . "];\n";
768 print ULP "};\n\n";
770 for ($i = 0; $i <= $#all_floats; $i++) {
771 $type = $all_floats[$i];
772 print ULP "#define ULP_";
773 if ($type =~ /^i/) {
774 print ULP "I_";
775 $type = substr $type, 1;
777 print ULP "$all_floats_pfx{$type} $i\n";
780 foreach $fct (keys %results) {
781 $type = $results{$fct}{'type'};
782 if ($type eq 'normal') {
783 $ulp = get_all_ulps_for_test ($fct, 'normal');
784 } elsif ($type eq 'complex') {
785 $ulp_real = get_all_ulps_for_test ($fct, 'real');
786 $ulp_imag = get_all_ulps_for_test ($fct, 'imag');
787 } else {
788 die "unknown results ($fct) type $type\n";
790 if ($type eq 'normal') {
791 $func_ulps{$fct} = $ulp;
792 } else {
793 $func_real_ulps{$fct} = $ulp_real;
794 $func_imag_ulps{$fct} = $ulp_imag;
797 print ULP "\n/* Maximal error of functions. */\n";
798 print ULP "static const struct ulp_data func_ulps[] =\n {\n";
799 foreach $fct (sort keys %func_ulps) {
800 print ULP " { \"$fct\", $func_ulps{$fct} },\n";
802 print ULP " };\n";
803 print ULP "static const struct ulp_data func_real_ulps[] =\n {\n";
804 foreach $fct (sort keys %func_real_ulps) {
805 print ULP " { \"$fct\", $func_real_ulps{$fct} },\n";
807 print ULP " };\n";
808 print ULP "static const struct ulp_data func_imag_ulps[] =\n {\n";
809 foreach $fct (sort keys %func_imag_ulps) {
810 print ULP " { \"$fct\", $func_imag_ulps{$fct} },\n";
812 print ULP " };\n";
813 close ULP;
816 # Parse auto-libm-test-out.
817 sub parse_auto_input {
818 my ($file) = @_;
819 open AUTO, $file or die ("Can't open $file: $!");
820 while (<AUTO>) {
821 chop;
822 next if !/^= /;
823 s/^= //;
824 if (/^(\S+) (\S+) ([^:]*) : (.*)$/) {
825 $auto_tests{$1}{$2}{$3} = $4;
826 } else {
827 die ("bad automatic test line: $_\n");
830 close AUTO;