Fix or add int_select international_call_prefixes
[glibc.git] / math / gen-libm-test.pl
blob8baeeb23b6aa5159bb389b7f965d9bd2dcd6ad6e
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', 'float128', 'idouble',
48 'ifloat', 'ifloat128', '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",
55 "float128" => "FLT128",
58 %beautify =
59 ( "minus_zero" => "-0",
60 "plus_zero" => "+0",
61 "-0x0p+0f" => "-0",
62 "-0x0p+0" => "-0",
63 "-0x0p+0L" => "-0",
64 "0x0p+0f" => "+0",
65 "0x0p+0" => "+0",
66 "0x0p+0L" => "+0",
67 "minus_infty" => "-inf",
68 "plus_infty" => "inf",
69 "qnan_value" => "qNaN",
70 "snan_value" => "sNaN",
71 "snan_value_ld" => "sNaN",
75 # get Options
76 # Options:
77 # a: auto-libm-test-out input file
78 # c: .inc input file
79 # u: ulps-file
80 # n: new ulps file
81 # C: libm-test.c output file
82 # H: libm-test-ulps.h output file
83 # h: help
84 use vars qw($opt_a $opt_c $opt_u $opt_n $opt_C $opt_H $opt_h);
85 getopts('a:c:u:n:C:H:h');
87 $ulps_file = 'libm-test-ulps';
89 if ($opt_h) {
90 print "Usage: gen-libm-test.pl [OPTIONS]\n";
91 print " -h print this help, then exit\n";
92 print " -a FILE input file with automatically generated tests\n";
93 print " -c FILE input file .inc file with tests\n";
94 print " -u FILE input file with ulps\n";
95 print " -n FILE generate sorted file FILE from libm-test-ulps\n";
96 print " -C FILE generate output C file FILE from libm-test.inc\n";
97 print " -H FILE generate output ulps header FILE from libm-test-ulps\n";
98 exit 0;
101 $ulps_file = $opt_u if ($opt_u);
103 $input = $opt_c if ($opt_c);
104 $auto_input = $opt_a if ($opt_a);
105 $output = $opt_C if ($opt_C);
107 &parse_ulps ($ulps_file) if ($opt_H || $opt_n);
108 &parse_auto_input ($auto_input) if ($opt_C);
109 &generate_testfile ($input, $output) if ($opt_C);
110 &output_ulps ($opt_H, $ulps_file) if ($opt_H);
111 &print_ulps_file ($opt_n) 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 =~ /^-?0x[0-9a-f.]*p[-+][0-9]+f$/) {
129 $arg =~ s/f$//;
131 if ($arg =~ /[0-9]L$/) {
132 $arg =~ s/L$//;
134 return $arg;
137 # Return a nicer representation of a complex number
138 sub build_complex_beautify {
139 my ($r, $i) = @_;
140 my ($str1, $str2);
142 $str1 = &beautify ($r);
143 $str2 = &beautify ($i);
144 if ($str2 =~ /^-/) {
145 $str2 =~ s/^-//;
146 $str1 .= ' - ' . $str2;
147 } else {
148 $str1 .= ' + ' . $str2;
150 $str1 .= ' i';
151 return $str1;
154 # Return the text to put in an initializer for a test's exception
155 # information.
156 sub show_exceptions {
157 my ($ignore_result, $non_finite, $test_snan, $exception) = @_;
158 $ignore_result = ($ignore_result ? "IGNORE_RESULT|" : "");
159 $non_finite = ($non_finite ? "NON_FINITE|" : "");
160 $test_snan = ($test_snan ? "TEST_SNAN|" : "");
161 if (defined $exception) {
162 return ", ${ignore_result}${non_finite}${test_snan}$exception";
163 } else {
164 return ", ${ignore_result}${non_finite}${test_snan}0";
168 # Apply the LIT(x) macro to a literal floating point constant
169 # and strip any existing suffix.
170 sub _apply_lit {
171 my ($lit) = @_;
172 my $exp_re = "([+-])?[[:digit:]]+";
173 # Don't wrap something that does not look like a:
174 # * Hexadecimal FP value
175 # * Decimal FP value without a decimal point
176 # * Decimal value with a fraction
177 return $lit if $lit !~ /([+-])?0x[[:xdigit:]\.]+[pP]$exp_re/
178 and $lit !~ /[[:digit:]]+[eE]$exp_re/
179 and $lit !~ /[[:digit:]]*\.[[:digit:]]*([eE]$exp_re)?/;
181 # Strip any existing literal suffix.
182 $lit =~ s/[lLfF]$//;
184 return "LIT (${lit})";
187 # Apply LIT macro to individual tokens within an expression.
189 # This function assumes the C expression follows GNU coding
190 # standards. Specifically, a space separates each lexical
191 # token. Otherwise, this post-processing may apply LIT
192 # incorrectly, or around an entire expression.
193 sub apply_lit {
194 my ($lit) = @_;
195 my @toks = split (/ /, $lit);
196 foreach (@toks) {
197 $_ = _apply_lit ($_);
199 return join (' ', @toks);
202 # Parse the arguments to TEST_x_y
203 sub parse_args {
204 my ($file, $descr, $args) = @_;
205 my (@args, $descr_args, $descr_res, @descr);
206 my ($current_arg, $cline, $cline_res, $i);
207 my (@special);
208 my ($call_args);
209 my ($ignore_result_any, $ignore_result_all);
210 my ($num_res, @args_res, @start_rm, $rm);
211 my (@plus_oflow, @minus_oflow, @plus_uflow, @minus_uflow);
212 my (@errno_plus_oflow, @errno_minus_oflow);
213 my (@errno_plus_uflow, @errno_minus_uflow);
214 my (@xfail_rounding_ibm128_libgcc);
215 my ($non_finite, $test_snan);
217 ($descr_args, $descr_res) = split /_/,$descr, 2;
219 @args = split /,\s*/, $args;
221 $call_args = "";
223 # Generate first the string that's shown to the user
224 $current_arg = 1;
225 @descr = split //,$descr_args;
226 for ($i = 0; $i <= $#descr; $i++) {
227 my $comma = "";
228 if ($current_arg > 1) {
229 $comma = ', ';
231 # FLOAT, long double, int, unsigned int, long int, long long int
232 if ($descr[$i] =~ /f|j|i|u|l|L/) {
233 $call_args .= $comma . &beautify ($args[$current_arg]);
234 ++$current_arg;
235 next;
237 # Argument passed via pointer.
238 if ($descr[$i] =~ /p/) {
239 next;
241 # &FLOAT, &int - simplify call by not showing argument.
242 if ($descr[$i] =~ /F|I/) {
243 next;
245 # complex
246 if ($descr[$i] eq 'c') {
247 $call_args .= $comma . &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
248 $current_arg += 2;
249 next;
252 die ("$descr[$i] is unknown");
255 # Result
256 @args_res = @args[$current_arg .. $#args];
257 $num_res = 0;
258 @descr = split //,$descr_res;
259 foreach (@descr) {
260 if ($_ =~ /f|i|l|L|M|U/) {
261 ++$num_res;
262 } elsif ($_ eq 'c') {
263 $num_res += 2;
264 } elsif ($_ eq 'b') {
265 # boolean
266 ++$num_res;
267 } elsif ($_ eq '1') {
268 ++$num_res;
269 } else {
270 die ("$_ is unknown");
273 # consistency check
274 if ($#args_res == $num_res - 1) {
275 # One set of results for all rounding modes, no flags.
276 @start_rm = ( 0, 0, 0, 0 );
277 } elsif ($#args_res == $num_res) {
278 # One set of results for all rounding modes, with flags.
279 die ("wrong number of arguments")
280 unless ($args_res[$#args_res] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN|TEST_NAN_SIGN|NO_TEST_INLINE|XFAIL/);
281 @start_rm = ( 0, 0, 0, 0 );
282 } elsif ($#args_res == 4 * $num_res + 3) {
283 # One set of results per rounding mode, with flags.
284 @start_rm = ( 0, $num_res + 1, 2 * $num_res + 2, 3 * $num_res + 3 );
285 } else {
286 die ("wrong number of arguments");
289 # Put the C program line together
290 # Reset some variables to start again
291 $current_arg = 1;
292 $call_args =~ s/\"/\\\"/g;
293 $cline = "{ \"$call_args\"";
294 @descr = split //,$descr_args;
295 for ($i=0; $i <= $#descr; $i++) {
296 # FLOAT, int, long int, long long int
297 if ($descr[$i] =~ /f|j|i|u|l|L/) {
298 if ($descr[$i] eq "f") {
299 $cline .= ", " . &apply_lit ($args[$current_arg]);
300 } else {
301 $cline .= ", $args[$current_arg]";
303 $current_arg++;
304 next;
306 # &FLOAT, &int, argument passed via pointer
307 if ($descr[$i] =~ /F|I|p/) {
308 next;
310 # complex
311 if ($descr[$i] eq 'c') {
312 $cline .= ", " . &apply_lit ($args[$current_arg]);
313 $cline .= ", " . &apply_lit ($args[$current_arg+1]);
314 $current_arg += 2;
315 next;
319 @descr = split //,$descr_res;
320 @plus_oflow = qw(max_value plus_infty max_value plus_infty);
321 @minus_oflow = qw(minus_infty minus_infty -max_value -max_value);
322 @plus_uflow = qw(plus_zero plus_zero plus_zero min_subnorm_value);
323 @minus_uflow = qw(-min_subnorm_value minus_zero minus_zero minus_zero);
324 @errno_plus_oflow = qw(0 ERRNO_ERANGE 0 ERRNO_ERANGE);
325 @errno_minus_oflow = qw(ERRNO_ERANGE ERRNO_ERANGE 0 0);
326 @errno_plus_uflow = qw(ERRNO_ERANGE ERRNO_ERANGE ERRNO_ERANGE 0);
327 @errno_minus_uflow = qw(0 ERRNO_ERANGE ERRNO_ERANGE ERRNO_ERANGE);
328 @xfail_rounding_ibm128_libgcc = qw(XFAIL_IBM128_LIBGCC 0
329 XFAIL_IBM128_LIBGCC XFAIL_IBM128_LIBGCC);
330 for ($rm = 0; $rm <= 3; $rm++) {
331 $current_arg = $start_rm[$rm];
332 $ignore_result_any = 0;
333 $ignore_result_all = 1;
334 $cline_res = "";
335 @special = ();
336 foreach (@descr) {
337 if ($_ =~ /b|f|j|i|l|L|M|U/ ) {
338 my ($result) = $args_res[$current_arg];
339 if ($result eq "IGNORE") {
340 $ignore_result_any = 1;
341 $result = "0";
342 } else {
343 $ignore_result_all = 0;
345 if ($_ eq "f") {
346 $result = apply_lit ($result);
348 $cline_res .= ", $result";
349 $current_arg++;
350 } elsif ($_ eq 'c') {
351 my ($result1) = $args_res[$current_arg];
352 if ($result1 eq "IGNORE") {
353 $ignore_result_any = 1;
354 $result1 = "0";
355 } else {
356 $ignore_result_all = 0;
358 my ($result2) = $args_res[$current_arg + 1];
359 if ($result2 eq "IGNORE") {
360 $ignore_result_any = 1;
361 $result2 = "0";
362 } else {
363 $ignore_result_all = 0;
365 $result1 = apply_lit ($result1);
366 $result2 = apply_lit ($result2);
367 $cline_res .= ", $result1, $result2";
368 $current_arg += 2;
369 } elsif ($_ eq '1') {
370 push @special, $args_res[$current_arg];
371 ++$current_arg;
374 if ($ignore_result_any && !$ignore_result_all) {
375 die ("some but not all function results ignored\n");
377 # Determine whether any arguments or results, for any rounding
378 # mode, are non-finite.
379 $non_finite = ($args =~ /qnan_value|snan_value|plus_infty|minus_infty/);
380 $test_snan = ($args =~ /snan_value/);
381 # Add exceptions.
382 $cline_res .= show_exceptions ($ignore_result_any,
383 $non_finite,
384 $test_snan,
385 ($current_arg <= $#args_res)
386 ? $args_res[$current_arg]
387 : undef);
389 # special treatment for some functions
390 $i = 0;
391 foreach (@special) {
392 ++$i;
393 my ($extra_expected) = $_;
394 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
395 if (!$run_extra) {
396 $extra_expected = "0";
397 } else {
398 $extra_expected = apply_lit ($extra_expected);
400 $cline_res .= ", $run_extra, $extra_expected";
402 $cline_res =~ s/^, //;
403 $cline_res =~ s/plus_oflow/$plus_oflow[$rm]/g;
404 $cline_res =~ s/minus_oflow/$minus_oflow[$rm]/g;
405 $cline_res =~ s/plus_uflow/$plus_uflow[$rm]/g;
406 $cline_res =~ s/minus_uflow/$minus_uflow[$rm]/g;
407 $cline_res =~ s/ERRNO_PLUS_OFLOW/$errno_plus_oflow[$rm]/g;
408 $cline_res =~ s/ERRNO_MINUS_OFLOW/$errno_minus_oflow[$rm]/g;
409 $cline_res =~ s/ERRNO_PLUS_UFLOW/$errno_plus_uflow[$rm]/g;
410 $cline_res =~ s/ERRNO_MINUS_UFLOW/$errno_minus_uflow[$rm]/g;
411 $cline_res =~ s/XFAIL_ROUNDING_IBM128_LIBGCC/$xfail_rounding_ibm128_libgcc[$rm]/g;
412 $cline .= ", { $cline_res }";
414 print $file " $cline },\n";
417 # Convert a condition from auto-libm-test-out to C form.
418 sub convert_condition {
419 my ($cond) = @_;
420 my (@conds, $ret);
421 @conds = split /:/, $cond;
422 foreach (@conds) {
423 s/-/_/g;
424 s/^/TEST_COND_/;
426 $ret = join " && ", @conds;
427 return "($ret)";
430 # Return text to OR a value into an accumulated flags string.
431 sub or_value {
432 my ($cond) = @_;
433 if ($cond eq "0") {
434 return "";
435 } else {
436 return " | $cond";
440 # Return a conditional expression between two values.
441 sub cond_value {
442 my ($cond, $if, $else) = @_;
443 if ($cond eq "1") {
444 return $if;
445 } elsif ($cond eq "0") {
446 return $else;
447 } else {
448 return "($cond ? $if : $else)";
452 # Return text to OR a conditional expression between two values into
453 # an accumulated flags string.
454 sub or_cond_value {
455 my ($cond, $if, $else) = @_;
456 return or_value (cond_value ($cond, $if, $else));
459 # Generate libm-test.c
460 sub generate_testfile {
461 my ($input, $output) = @_;
463 open INPUT, $input or die ("Can't open $input: $!");
464 open OUTPUT, ">$output" or die ("Can't open $output: $!");
466 # Replace the special macros
467 while (<INPUT>) {
468 # AUTO_TESTS (function),
469 if (/^\s*AUTO_TESTS_/) {
470 my ($descr, $func, @modes, $auto_test, $num_auto_tests);
471 my (@rm_tests, $rm, $i);
472 @modes = qw(downward tonearest towardzero upward);
473 ($descr, $func) = ($_ =~ /AUTO_TESTS_(\w+)\s*\((\w+)\)/);
474 for ($rm = 0; $rm <= 3; $rm++) {
475 $rm_tests[$rm] = [sort keys %{$auto_tests{$func}{$modes[$rm]}}];
477 $num_auto_tests = scalar @{$rm_tests[0]};
478 for ($rm = 1; $rm <= 3; $rm++) {
479 if ($num_auto_tests != scalar @{$rm_tests[$rm]}) {
480 die ("inconsistent numbers of tests for $func\n");
482 for ($i = 0; $i < $num_auto_tests; $i++) {
483 if ($rm_tests[0][$i] ne $rm_tests[$rm][$i]) {
484 die ("inconsistent list of tests of $func\n");
488 if ($num_auto_tests == 0) {
489 die ("no automatic tests for $func\n");
491 foreach $auto_test (@{$rm_tests[0]}) {
492 my ($format, $inputs, $format_conv, $args_str);
493 ($format, $inputs) = split / /, $auto_test, 2;
494 $inputs =~ s/ /, /g;
495 $format_conv = convert_condition ($format);
496 print OUTPUT "#if $format_conv\n";
497 $args_str = "$func, $inputs";
498 for ($rm = 0; $rm <= 3; $rm++) {
499 my ($auto_test_out, $outputs, $flags);
500 my ($flags_conv, @flags, %flag_cond);
501 $auto_test_out = $auto_tests{$func}{$modes[$rm]}{$auto_test};
502 ($outputs, $flags) = split / : */, $auto_test_out;
503 $outputs =~ s/ /, /g;
504 @flags = split / /, $flags;
505 foreach (@flags) {
506 if (/^([^:]*):(.*)$/) {
507 my ($flag, $cond);
508 $flag = $1;
509 $cond = convert_condition ($2);
510 if (defined ($flag_cond{$flag})) {
511 if ($flag_cond{$flag} ne "1") {
512 $flag_cond{$flag} .= " || $cond";
514 } else {
515 $flag_cond{$flag} = $cond;
517 } else {
518 $flag_cond{$_} = "1";
521 $flags_conv = "";
522 if (defined ($flag_cond{"ignore-zero-inf-sign"})) {
523 $flags_conv .= or_cond_value ($flag_cond{"ignore-zero-inf-sign"},
524 "IGNORE_ZERO_INF_SIGN", "0");
526 if (defined ($flag_cond{"no-test-inline"})) {
527 $flags_conv .= or_cond_value ($flag_cond{"no-test-inline"},
528 "NO_TEST_INLINE", "0");
530 if (defined ($flag_cond{"xfail"})) {
531 $flags_conv .= or_cond_value ($flag_cond{"xfail"},
532 "XFAIL_TEST", "0");
534 my (@exc_list) = qw(divbyzero inexact invalid overflow underflow);
535 my ($exc);
536 foreach $exc (@exc_list) {
537 my ($exc_expected, $exc_ok, $no_exc, $exc_cond, $exc_ok_cond);
538 $exc_expected = "\U$exc\E_EXCEPTION";
539 $exc_ok = "\U$exc\E_EXCEPTION_OK";
540 $no_exc = "0";
541 if ($exc eq "inexact") {
542 $exc_ok = "0";
543 $no_exc = "NO_INEXACT_EXCEPTION";
545 if (defined ($flag_cond{$exc})) {
546 $exc_cond = $flag_cond{$exc};
547 } else {
548 $exc_cond = "0";
550 if (defined ($flag_cond{"$exc-ok"})) {
551 $exc_ok_cond = $flag_cond{"$exc-ok"};
552 } else {
553 $exc_ok_cond = "0";
555 $flags_conv .= or_cond_value ($exc_cond,
556 cond_value ($exc_ok_cond,
557 $exc_ok, $exc_expected),
558 cond_value ($exc_ok_cond,
559 $exc_ok, $no_exc));
561 my ($errno_expected, $errno_unknown_cond);
562 if (defined ($flag_cond{"errno-edom"})) {
563 if ($flag_cond{"errno-edom"} ne "1") {
564 die ("unexpected condition for errno-edom");
566 if (defined ($flag_cond{"errno-erange"})) {
567 die ("multiple errno values expected");
569 $errno_expected = "ERRNO_EDOM";
570 } elsif (defined ($flag_cond{"errno-erange"})) {
571 if ($flag_cond{"errno-erange"} ne "1") {
572 die ("unexpected condition for errno-erange");
574 $errno_expected = "ERRNO_ERANGE";
575 } else {
576 $errno_expected = "ERRNO_UNCHANGED";
578 if (defined ($flag_cond{"errno-edom-ok"})) {
579 if (defined ($flag_cond{"errno-erange-ok"})
580 && ($flag_cond{"errno-erange-ok"}
581 ne $flag_cond{"errno-edom-ok"})) {
582 $errno_unknown_cond = "($flag_cond{\"errno-edom-ok\"} || $flag_cond{\"errno-erange-ok\"})";
583 } else {
584 $errno_unknown_cond = $flag_cond{"errno-edom-ok"};
586 } elsif (defined ($flag_cond{"errno-erange-ok"})) {
587 $errno_unknown_cond = $flag_cond{"errno-erange-ok"};
588 } else {
589 $errno_unknown_cond = "0";
591 $flags_conv .= or_cond_value ($errno_unknown_cond,
592 "0", $errno_expected);
593 if ($flags_conv eq "") {
594 $flags_conv = ", NO_EXCEPTION";
595 } else {
596 $flags_conv =~ s/^ \|/,/;
598 $args_str .= ", $outputs$flags_conv";
600 &parse_args (\*OUTPUT, $descr, $args_str);
601 print OUTPUT "#endif\n";
603 next;
606 # TEST_...
607 if (/^\s*TEST_/) {
608 my ($descr, $args);
609 chop;
610 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
611 &parse_args (\*OUTPUT, $descr, $args);
612 next;
614 print OUTPUT;
616 close INPUT;
617 close OUTPUT;
622 # Parse ulps file
623 sub parse_ulps {
624 my ($file) = @_;
625 my ($test, $type, $float, $eps, $float_regex);
627 # Build a basic regex to match type entries in the
628 # generated ULPS file.
629 foreach my $ftype (@all_floats) {
630 $float_regex .= "|" . $ftype;
632 $float_regex = "^" . substr ($float_regex, 1) . ":";
634 # $type has the following values:
635 # "normal": No complex variable
636 # "real": Real part of complex result
637 # "imag": Imaginary part of complex result
638 open ULP, $file or die ("Can't open $file: $!");
639 while (<ULP>) {
640 chop;
641 # ignore comments and empty lines
642 next if /^#/;
643 next if /^\s*$/;
644 if (/^Function: /) {
645 if (/Real part of/) {
646 s/Real part of //;
647 $type = 'real';
648 } elsif (/Imaginary part of/) {
649 s/Imaginary part of //;
650 $type = 'imag';
651 } else {
652 $type = 'normal';
654 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
655 next;
657 if (/$float_regex/) {
658 ($float, $eps) = split /\s*:\s*/,$_,2;
660 if ($eps eq "0") {
661 # ignore
662 next;
663 } else {
664 if (!defined ($results{$test}{$type}{'ulp'}{$float})
665 || $results{$test}{$type}{'ulp'}{$float} < $eps) {
666 $results{$test}{$type}{'ulp'}{$float} = $eps;
667 $results{$test}{'has_ulps'} = 1;
670 if ($type =~ /^real|imag$/) {
671 $results{$test}{'type'} = 'complex';
672 } elsif ($type eq 'normal') {
673 $results{$test}{'type'} = 'normal';
675 next;
677 print "Skipping unknown entry: `$_'\n";
679 close ULP;
683 # Clean up a floating point number
684 sub clean_up_number {
685 my ($number) = @_;
687 # Remove trailing zeros after the decimal point
688 if ($number =~ /\./) {
689 $number =~ s/0+$//;
690 $number =~ s/\.$//;
692 return $number;
695 # Output a file which can be read in as ulps file.
696 sub print_ulps_file {
697 my ($file) = @_;
698 my ($test, $type, $float, $eps, $fct, $last_fct);
700 $last_fct = '';
701 open NEWULP, ">$file" or die ("Can't open $file: $!");
702 print NEWULP "# Begin of automatic generation\n";
703 print NEWULP "\n# Maximal error of functions:\n";
705 foreach $fct (sort keys %results) {
706 foreach $type ('real', 'imag', 'normal') {
707 if (exists $results{$fct}{$type}) {
708 if ($type eq 'normal') {
709 print NEWULP "Function: \"$fct\":\n";
710 } elsif ($type eq 'real') {
711 print NEWULP "Function: Real part of \"$fct\":\n";
712 } elsif ($type eq 'imag') {
713 print NEWULP "Function: Imaginary part of \"$fct\":\n";
715 foreach $float (@all_floats) {
716 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
717 print NEWULP "$float: ",
718 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
719 "\n";
722 print NEWULP "\n";
726 print NEWULP "# end of automatic generation\n";
727 close NEWULP;
730 sub get_ulps {
731 my ($test, $type, $float) = @_;
733 return (exists $results{$test}{$type}{'ulp'}{$float}
734 ? $results{$test}{$type}{'ulp'}{$float} : "0");
737 # Return the ulps value for a single test.
738 sub get_all_ulps_for_test {
739 my ($test, $type) = @_;
740 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
741 my ($ulps_str);
743 if (exists $results{$test}{'has_ulps'}) {
744 foreach $float (@all_floats) {
745 $ulps_str .= &get_ulps ($test, $type, $float) . ", ";
747 return "{" . substr ($ulps_str, 0, -2) . "}";
748 } else {
749 die "get_all_ulps_for_test called for \"$test\" with no ulps\n";
753 # Print include file
754 sub output_ulps {
755 my ($file, $ulps_filename) = @_;
756 my ($i, $fct, $type, $ulp, $ulp_real, $ulp_imag);
757 my (%func_ulps, %func_real_ulps, %func_imag_ulps);
759 open ULP, ">$file" or die ("Can't open $file: $!");
761 print ULP "/* This file is automatically generated\n";
762 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
763 print ULP " Don't change it - change instead the master files. */\n\n";
765 print ULP "struct ulp_data\n";
766 print ULP "{\n";
767 print ULP " const char *name;\n";
768 print ULP " FLOAT max_ulp[" . @all_floats . "];\n";
769 print ULP "};\n\n";
771 for ($i = 0; $i <= $#all_floats; $i++) {
772 $type = $all_floats[$i];
773 print ULP "#define ULP_";
774 if ($type =~ /^i/) {
775 print ULP "I_";
776 $type = substr $type, 1;
778 print ULP "$all_floats_pfx{$type} $i\n";
781 foreach $fct (keys %results) {
782 $type = $results{$fct}{'type'};
783 if ($type eq 'normal') {
784 $ulp = get_all_ulps_for_test ($fct, 'normal');
785 } elsif ($type eq 'complex') {
786 $ulp_real = get_all_ulps_for_test ($fct, 'real');
787 $ulp_imag = get_all_ulps_for_test ($fct, 'imag');
788 } else {
789 die "unknown results ($fct) type $type\n";
791 if ($type eq 'normal') {
792 $func_ulps{$fct} = $ulp;
793 } else {
794 $func_real_ulps{$fct} = $ulp_real;
795 $func_imag_ulps{$fct} = $ulp_imag;
798 print ULP "\n/* Maximal error of functions. */\n";
799 print ULP "static const struct ulp_data func_ulps[] =\n {\n";
800 foreach $fct (sort keys %func_ulps) {
801 print ULP " { \"$fct\", $func_ulps{$fct} },\n";
803 print ULP " };\n";
804 print ULP "static const struct ulp_data func_real_ulps[] =\n {\n";
805 foreach $fct (sort keys %func_real_ulps) {
806 print ULP " { \"$fct\", $func_real_ulps{$fct} },\n";
808 print ULP " };\n";
809 print ULP "static const struct ulp_data func_imag_ulps[] =\n {\n";
810 foreach $fct (sort keys %func_imag_ulps) {
811 print ULP " { \"$fct\", $func_imag_ulps{$fct} },\n";
813 print ULP " };\n";
814 close ULP;
817 # Parse auto-libm-test-out.
818 sub parse_auto_input {
819 my ($file) = @_;
820 open AUTO, $file or die ("Can't open $file: $!");
821 while (<AUTO>) {
822 chop;
823 next if !/^= /;
824 s/^= //;
825 if (/^(\S+) (\S+) ([^:]*) : (.*)$/) {
826 $auto_tests{$1}{$2}{$3} = $4;
827 } else {
828 die ("bad automatic test line: $_\n");
831 close AUTO;