x86-64/swapcontext: Restore the pointer into %rdx after syscall
[glibc.git] / math / gen-libm-test.pl
blob664fba5909873d1e3fb2da753a2a094e4cc36594
1 #!/usr/bin/perl -w
2 # Copyright (C) 1999-2018 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) or ARG_LIT(x) macro to a literal floating point constant
169 # and strip any existing suffix.
170 sub _apply_lit {
171 my ($macro, $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 "$macro (${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 ("LIT", $_);
199 return join (' ', @toks);
202 # Likewise, but apply ARG_LIT for arguments to narrowing functions.
203 sub apply_arglit {
204 my ($lit) = @_;
205 my @toks = split (/ /, $lit);
206 foreach (@toks) {
207 $_ = _apply_lit ("ARG_LIT", $_);
209 return join (' ', @toks);
212 # Parse the arguments to TEST_x_y
213 sub parse_args {
214 my ($file, $descr, $args) = @_;
215 my (@args, $descr_args, $descr_res, @descr);
216 my ($current_arg, $cline, $cline_res, $i);
217 my (@special);
218 my ($call_args);
219 my ($ignore_result_any, $ignore_result_all);
220 my ($num_res, @args_res, @start_rm, $rm);
221 my (@plus_oflow, @minus_oflow, @plus_uflow, @minus_uflow);
222 my (@errno_plus_oflow, @errno_minus_oflow);
223 my (@errno_plus_uflow, @errno_minus_uflow);
224 my (@xfail_rounding_ibm128_libgcc);
225 my ($non_finite, $test_snan);
227 ($descr_args, $descr_res) = split /_/,$descr, 2;
229 @args = split /,\s*/, $args;
231 $call_args = "";
233 # Generate first the string that's shown to the user
234 $current_arg = 1;
235 @descr = split //,$descr_args;
236 for ($i = 0; $i <= $#descr; $i++) {
237 my $comma = "";
238 if ($current_arg > 1) {
239 $comma = ', ';
241 # FLOAT, ARG_FLOAT, long double, int, unsigned int, long int, long long int
242 if ($descr[$i] =~ /f|a|j|i|u|l|L/) {
243 $call_args .= $comma . &beautify ($args[$current_arg]);
244 ++$current_arg;
245 next;
247 # Argument passed via pointer.
248 if ($descr[$i] =~ /p/) {
249 next;
251 # &FLOAT, &int - simplify call by not showing argument.
252 if ($descr[$i] =~ /F|I/) {
253 next;
255 # complex
256 if ($descr[$i] eq 'c') {
257 $call_args .= $comma . &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
258 $current_arg += 2;
259 next;
262 die ("$descr[$i] is unknown");
265 # Result
266 @args_res = @args[$current_arg .. $#args];
267 $num_res = 0;
268 @descr = split //,$descr_res;
269 foreach (@descr) {
270 if ($_ =~ /f|i|l|L|M|U/) {
271 ++$num_res;
272 } elsif ($_ eq 'c') {
273 $num_res += 2;
274 } elsif ($_ eq 'b') {
275 # boolean
276 ++$num_res;
277 } elsif ($_ eq '1') {
278 ++$num_res;
279 } else {
280 die ("$_ is unknown");
283 # consistency check
284 if ($#args_res == $num_res - 1) {
285 # One set of results for all rounding modes, no flags.
286 @start_rm = ( 0, 0, 0, 0 );
287 } elsif ($#args_res == $num_res) {
288 # One set of results for all rounding modes, with flags.
289 die ("wrong number of arguments")
290 unless ($args_res[$#args_res] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN|TEST_NAN_SIGN|NO_TEST_INLINE|XFAIL/);
291 @start_rm = ( 0, 0, 0, 0 );
292 } elsif ($#args_res == 4 * $num_res + 3) {
293 # One set of results per rounding mode, with flags.
294 @start_rm = ( 0, $num_res + 1, 2 * $num_res + 2, 3 * $num_res + 3 );
295 } else {
296 die ("wrong number of arguments");
299 # Put the C program line together
300 # Reset some variables to start again
301 $current_arg = 1;
302 $call_args =~ s/\"/\\\"/g;
303 $cline = "{ \"$call_args\"";
304 @descr = split //,$descr_args;
305 for ($i=0; $i <= $#descr; $i++) {
306 # FLOAT, ARG_FLOAT, long double, int, unsigned int, long int, long long int
307 if ($descr[$i] =~ /f|a|j|i|u|l|L/) {
308 if ($descr[$i] eq "f") {
309 $cline .= ", " . &apply_lit ($args[$current_arg]);
310 } elsif ($descr[$i] eq "a") {
311 $cline .= ", " . &apply_arglit ($args[$current_arg]);
312 } else {
313 $cline .= ", $args[$current_arg]";
315 $current_arg++;
316 next;
318 # &FLOAT, &int, argument passed via pointer
319 if ($descr[$i] =~ /F|I|p/) {
320 next;
322 # complex
323 if ($descr[$i] eq 'c') {
324 $cline .= ", " . &apply_lit ($args[$current_arg]);
325 $cline .= ", " . &apply_lit ($args[$current_arg+1]);
326 $current_arg += 2;
327 next;
331 @descr = split //,$descr_res;
332 @plus_oflow = qw(max_value plus_infty max_value plus_infty);
333 @minus_oflow = qw(minus_infty minus_infty -max_value -max_value);
334 @plus_uflow = qw(plus_zero plus_zero plus_zero min_subnorm_value);
335 @minus_uflow = qw(-min_subnorm_value minus_zero minus_zero minus_zero);
336 @errno_plus_oflow = qw(0 ERRNO_ERANGE 0 ERRNO_ERANGE);
337 @errno_minus_oflow = qw(ERRNO_ERANGE ERRNO_ERANGE 0 0);
338 @errno_plus_uflow = qw(ERRNO_ERANGE ERRNO_ERANGE ERRNO_ERANGE 0);
339 @errno_minus_uflow = qw(0 ERRNO_ERANGE ERRNO_ERANGE ERRNO_ERANGE);
340 @xfail_rounding_ibm128_libgcc = qw(XFAIL_IBM128_LIBGCC 0
341 XFAIL_IBM128_LIBGCC XFAIL_IBM128_LIBGCC);
342 for ($rm = 0; $rm <= 3; $rm++) {
343 $current_arg = $start_rm[$rm];
344 $ignore_result_any = 0;
345 $ignore_result_all = 1;
346 $cline_res = "";
347 @special = ();
348 foreach (@descr) {
349 if ($_ =~ /b|f|j|i|l|L|M|U/ ) {
350 my ($result) = $args_res[$current_arg];
351 if ($result eq "IGNORE") {
352 $ignore_result_any = 1;
353 $result = "0";
354 } else {
355 $ignore_result_all = 0;
357 if ($_ eq "f") {
358 $result = apply_lit ($result);
360 $cline_res .= ", $result";
361 $current_arg++;
362 } elsif ($_ eq 'c') {
363 my ($result1) = $args_res[$current_arg];
364 if ($result1 eq "IGNORE") {
365 $ignore_result_any = 1;
366 $result1 = "0";
367 } else {
368 $ignore_result_all = 0;
370 my ($result2) = $args_res[$current_arg + 1];
371 if ($result2 eq "IGNORE") {
372 $ignore_result_any = 1;
373 $result2 = "0";
374 } else {
375 $ignore_result_all = 0;
377 $result1 = apply_lit ($result1);
378 $result2 = apply_lit ($result2);
379 $cline_res .= ", $result1, $result2";
380 $current_arg += 2;
381 } elsif ($_ eq '1') {
382 push @special, $args_res[$current_arg];
383 ++$current_arg;
386 if ($ignore_result_any && !$ignore_result_all) {
387 die ("some but not all function results ignored\n");
389 # Determine whether any arguments or results, for any rounding
390 # mode, are non-finite.
391 $non_finite = ($args =~ /qnan_value|snan_value|plus_infty|minus_infty/);
392 $test_snan = ($args =~ /snan_value/);
393 # Add exceptions.
394 $cline_res .= show_exceptions ($ignore_result_any,
395 $non_finite,
396 $test_snan,
397 ($current_arg <= $#args_res)
398 ? $args_res[$current_arg]
399 : undef);
401 # special treatment for some functions
402 $i = 0;
403 foreach (@special) {
404 ++$i;
405 my ($extra_expected) = $_;
406 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
407 if (!$run_extra) {
408 $extra_expected = "0";
409 } else {
410 $extra_expected = apply_lit ($extra_expected);
412 $cline_res .= ", $run_extra, $extra_expected";
414 $cline_res =~ s/^, //;
415 $cline_res =~ s/plus_oflow/$plus_oflow[$rm]/g;
416 $cline_res =~ s/minus_oflow/$minus_oflow[$rm]/g;
417 $cline_res =~ s/plus_uflow/$plus_uflow[$rm]/g;
418 $cline_res =~ s/minus_uflow/$minus_uflow[$rm]/g;
419 $cline_res =~ s/ERRNO_PLUS_OFLOW/$errno_plus_oflow[$rm]/g;
420 $cline_res =~ s/ERRNO_MINUS_OFLOW/$errno_minus_oflow[$rm]/g;
421 $cline_res =~ s/ERRNO_PLUS_UFLOW/$errno_plus_uflow[$rm]/g;
422 $cline_res =~ s/ERRNO_MINUS_UFLOW/$errno_minus_uflow[$rm]/g;
423 $cline_res =~ s/XFAIL_ROUNDING_IBM128_LIBGCC/$xfail_rounding_ibm128_libgcc[$rm]/g;
424 $cline .= ", { $cline_res }";
426 print $file " $cline },\n";
429 # Convert a condition from auto-libm-test-out to C form.
430 sub convert_condition {
431 my ($cond) = @_;
432 my (@conds, $ret);
433 @conds = split /:/, $cond;
434 foreach (@conds) {
435 if ($_ !~ /^arg_fmt\(/) {
436 s/-/_/g;
438 s/^/TEST_COND_/;
440 $ret = join " && ", @conds;
441 return "($ret)";
444 # Return text to OR a value into an accumulated flags string.
445 sub or_value {
446 my ($cond) = @_;
447 if ($cond eq "0") {
448 return "";
449 } else {
450 return " | $cond";
454 # Return a conditional expression between two values.
455 sub cond_value {
456 my ($cond, $if, $else) = @_;
457 if ($cond eq "1") {
458 return $if;
459 } elsif ($cond eq "0") {
460 return $else;
461 } else {
462 return "($cond ? $if : $else)";
466 # Return text to OR a conditional expression between two values into
467 # an accumulated flags string.
468 sub or_cond_value {
469 my ($cond, $if, $else) = @_;
470 return or_value (cond_value ($cond, $if, $else));
473 # Generate libm-test.c
474 sub generate_testfile {
475 my ($input, $output) = @_;
477 open INPUT, $input or die ("Can't open $input: $!");
478 open OUTPUT, ">$output" or die ("Can't open $output: $!");
480 # Replace the special macros
481 while (<INPUT>) {
482 # AUTO_TESTS (function),
483 if (/^\s*AUTO_TESTS_/) {
484 my ($descr, $func, @modes, $auto_test, $num_auto_tests);
485 my (@rm_tests, $rm, $i);
486 @modes = qw(downward tonearest towardzero upward);
487 ($descr, $func) = ($_ =~ /AUTO_TESTS_(\w+)\s*\((\w+)\)/);
488 for ($rm = 0; $rm <= 3; $rm++) {
489 $rm_tests[$rm] = [sort keys %{$auto_tests{$func}{$modes[$rm]}}];
491 $num_auto_tests = scalar @{$rm_tests[0]};
492 for ($rm = 1; $rm <= 3; $rm++) {
493 if ($num_auto_tests != scalar @{$rm_tests[$rm]}) {
494 die ("inconsistent numbers of tests for $func\n");
496 for ($i = 0; $i < $num_auto_tests; $i++) {
497 if ($rm_tests[0][$i] ne $rm_tests[$rm][$i]) {
498 die ("inconsistent list of tests of $func\n");
502 if ($num_auto_tests == 0) {
503 die ("no automatic tests for $func\n");
505 foreach $auto_test (@{$rm_tests[0]}) {
506 my ($format, $inputs, $format_conv, $args_str);
507 ($format, $inputs) = split / /, $auto_test, 2;
508 $inputs =~ s/ /, /g;
509 $format_conv = convert_condition ($format);
510 print OUTPUT "#if $format_conv\n";
511 $args_str = "$func, $inputs";
512 for ($rm = 0; $rm <= 3; $rm++) {
513 my ($auto_test_out, $outputs, $flags);
514 my ($flags_conv, @flags, %flag_cond);
515 $auto_test_out = $auto_tests{$func}{$modes[$rm]}{$auto_test};
516 ($outputs, $flags) = split / : */, $auto_test_out;
517 $outputs =~ s/ /, /g;
518 @flags = split / /, $flags;
519 foreach (@flags) {
520 if (/^([^:]*):(.*)$/) {
521 my ($flag, $cond);
522 $flag = $1;
523 $cond = convert_condition ($2);
524 if (defined ($flag_cond{$flag})) {
525 if ($flag_cond{$flag} ne "1") {
526 $flag_cond{$flag} .= " || $cond";
528 } else {
529 $flag_cond{$flag} = $cond;
531 } else {
532 $flag_cond{$_} = "1";
535 $flags_conv = "";
536 if (defined ($flag_cond{"ignore-zero-inf-sign"})) {
537 $flags_conv .= or_cond_value ($flag_cond{"ignore-zero-inf-sign"},
538 "IGNORE_ZERO_INF_SIGN", "0");
540 if (defined ($flag_cond{"no-test-inline"})) {
541 $flags_conv .= or_cond_value ($flag_cond{"no-test-inline"},
542 "NO_TEST_INLINE", "0");
544 if (defined ($flag_cond{"xfail"})) {
545 $flags_conv .= or_cond_value ($flag_cond{"xfail"},
546 "XFAIL_TEST", "0");
548 my (@exc_list) = qw(divbyzero inexact invalid overflow underflow);
549 my ($exc);
550 foreach $exc (@exc_list) {
551 my ($exc_expected, $exc_ok, $no_exc, $exc_cond, $exc_ok_cond);
552 $exc_expected = "\U$exc\E_EXCEPTION";
553 $exc_ok = "\U$exc\E_EXCEPTION_OK";
554 $no_exc = "0";
555 if ($exc eq "inexact") {
556 $exc_ok = "0";
557 $no_exc = "NO_INEXACT_EXCEPTION";
559 if (defined ($flag_cond{$exc})) {
560 $exc_cond = $flag_cond{$exc};
561 } else {
562 $exc_cond = "0";
564 if (defined ($flag_cond{"$exc-ok"})) {
565 $exc_ok_cond = $flag_cond{"$exc-ok"};
566 } else {
567 $exc_ok_cond = "0";
569 $flags_conv .= or_cond_value ($exc_cond,
570 cond_value ($exc_ok_cond,
571 $exc_ok, $exc_expected),
572 cond_value ($exc_ok_cond,
573 $exc_ok, $no_exc));
575 my ($errno_expected, $errno_unknown_cond);
576 if (defined ($flag_cond{"errno-edom"})) {
577 if ($flag_cond{"errno-edom"} ne "1") {
578 die ("unexpected condition for errno-edom");
580 if (defined ($flag_cond{"errno-erange"})) {
581 die ("multiple errno values expected");
583 $errno_expected = "ERRNO_EDOM";
584 } elsif (defined ($flag_cond{"errno-erange"})) {
585 if ($flag_cond{"errno-erange"} ne "1") {
586 die ("unexpected condition for errno-erange");
588 $errno_expected = "ERRNO_ERANGE";
589 } else {
590 $errno_expected = "ERRNO_UNCHANGED";
592 if (defined ($flag_cond{"errno-edom-ok"})) {
593 if (defined ($flag_cond{"errno-erange-ok"})
594 && ($flag_cond{"errno-erange-ok"}
595 ne $flag_cond{"errno-edom-ok"})) {
596 $errno_unknown_cond = "($flag_cond{\"errno-edom-ok\"} || $flag_cond{\"errno-erange-ok\"})";
597 } else {
598 $errno_unknown_cond = $flag_cond{"errno-edom-ok"};
600 } elsif (defined ($flag_cond{"errno-erange-ok"})) {
601 $errno_unknown_cond = $flag_cond{"errno-erange-ok"};
602 } else {
603 $errno_unknown_cond = "0";
605 $flags_conv .= or_cond_value ($errno_unknown_cond,
606 "0", $errno_expected);
607 if ($flags_conv eq "") {
608 $flags_conv = ", NO_EXCEPTION";
609 } else {
610 $flags_conv =~ s/^ \|/,/;
612 $args_str .= ", $outputs$flags_conv";
614 &parse_args (\*OUTPUT, $descr, $args_str);
615 print OUTPUT "#endif\n";
617 next;
620 # TEST_...
621 if (/^\s*TEST_/) {
622 my ($descr, $args);
623 chop;
624 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
625 &parse_args (\*OUTPUT, $descr, $args);
626 next;
628 print OUTPUT;
630 close INPUT;
631 close OUTPUT;
636 # Parse ulps file
637 sub parse_ulps {
638 my ($file) = @_;
639 my ($test, $type, $float, $eps, $float_regex);
641 # Build a basic regex to match type entries in the
642 # generated ULPS file.
643 foreach my $ftype (@all_floats) {
644 $float_regex .= "|" . $ftype;
646 $float_regex = "^" . substr ($float_regex, 1) . ":";
648 # $type has the following values:
649 # "normal": No complex variable
650 # "real": Real part of complex result
651 # "imag": Imaginary part of complex result
652 open ULP, $file or die ("Can't open $file: $!");
653 while (<ULP>) {
654 chop;
655 # ignore comments and empty lines
656 next if /^#/;
657 next if /^\s*$/;
658 if (/^Function: /) {
659 if (/Real part of/) {
660 s/Real part of //;
661 $type = 'real';
662 } elsif (/Imaginary part of/) {
663 s/Imaginary part of //;
664 $type = 'imag';
665 } else {
666 $type = 'normal';
668 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
669 next;
671 if (/$float_regex/) {
672 ($float, $eps) = split /\s*:\s*/,$_,2;
674 if ($eps eq "0") {
675 # ignore
676 next;
677 } else {
678 if (!defined ($results{$test}{$type}{'ulp'}{$float})
679 || $results{$test}{$type}{'ulp'}{$float} < $eps) {
680 $results{$test}{$type}{'ulp'}{$float} = $eps;
681 $results{$test}{'has_ulps'} = 1;
684 if ($type =~ /^real|imag$/) {
685 $results{$test}{'type'} = 'complex';
686 } elsif ($type eq 'normal') {
687 $results{$test}{'type'} = 'normal';
689 next;
691 print "Skipping unknown entry: `$_'\n";
693 close ULP;
697 # Clean up a floating point number
698 sub clean_up_number {
699 my ($number) = @_;
701 # Remove trailing zeros after the decimal point
702 if ($number =~ /\./) {
703 $number =~ s/0+$//;
704 $number =~ s/\.$//;
706 return $number;
709 # Output a file which can be read in as ulps file.
710 sub print_ulps_file {
711 my ($file) = @_;
712 my ($test, $type, $float, $eps, $fct, $last_fct);
714 $last_fct = '';
715 open NEWULP, ">$file" or die ("Can't open $file: $!");
716 print NEWULP "# Begin of automatic generation\n";
717 print NEWULP "\n# Maximal error of functions:\n";
719 foreach $fct (sort keys %results) {
720 foreach $type ('real', 'imag', 'normal') {
721 if (exists $results{$fct}{$type}) {
722 if ($type eq 'normal') {
723 print NEWULP "Function: \"$fct\":\n";
724 } elsif ($type eq 'real') {
725 print NEWULP "Function: Real part of \"$fct\":\n";
726 } elsif ($type eq 'imag') {
727 print NEWULP "Function: Imaginary part of \"$fct\":\n";
729 foreach $float (@all_floats) {
730 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
731 print NEWULP "$float: ",
732 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
733 "\n";
736 print NEWULP "\n";
740 print NEWULP "# end of automatic generation\n";
741 close NEWULP;
744 sub get_ulps {
745 my ($test, $type, $float) = @_;
747 return (exists $results{$test}{$type}{'ulp'}{$float}
748 ? $results{$test}{$type}{'ulp'}{$float} : "0");
751 # Return the ulps value for a single test.
752 sub get_all_ulps_for_test {
753 my ($test, $type) = @_;
754 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
755 my ($ulps_str);
757 if (exists $results{$test}{'has_ulps'}) {
758 foreach $float (@all_floats) {
759 $ulps_str .= &get_ulps ($test, $type, $float) . ", ";
761 return "{" . substr ($ulps_str, 0, -2) . "}";
762 } else {
763 die "get_all_ulps_for_test called for \"$test\" with no ulps\n";
767 # Print include file
768 sub output_ulps {
769 my ($file, $ulps_filename) = @_;
770 my ($i, $fct, $type, $ulp, $ulp_real, $ulp_imag);
771 my (%func_ulps, %func_real_ulps, %func_imag_ulps);
773 open ULP, ">$file" or die ("Can't open $file: $!");
775 print ULP "/* This file is automatically generated\n";
776 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
777 print ULP " Don't change it - change instead the master files. */\n\n";
779 print ULP "struct ulp_data\n";
780 print ULP "{\n";
781 print ULP " const char *name;\n";
782 print ULP " FLOAT max_ulp[" . @all_floats . "];\n";
783 print ULP "};\n\n";
785 for ($i = 0; $i <= $#all_floats; $i++) {
786 $type = $all_floats[$i];
787 print ULP "#define ULP_";
788 if ($type =~ /^i/) {
789 print ULP "I_";
790 $type = substr $type, 1;
792 print ULP "$all_floats_pfx{$type} $i\n";
795 foreach $fct (keys %results) {
796 $type = $results{$fct}{'type'};
797 if ($type eq 'normal') {
798 $ulp = get_all_ulps_for_test ($fct, 'normal');
799 } elsif ($type eq 'complex') {
800 $ulp_real = get_all_ulps_for_test ($fct, 'real');
801 $ulp_imag = get_all_ulps_for_test ($fct, 'imag');
802 } else {
803 die "unknown results ($fct) type $type\n";
805 if ($type eq 'normal') {
806 $func_ulps{$fct} = $ulp;
807 } else {
808 $func_real_ulps{$fct} = $ulp_real;
809 $func_imag_ulps{$fct} = $ulp_imag;
812 print ULP "\n/* Maximal error of functions. */\n";
813 print ULP "static const struct ulp_data func_ulps[] =\n {\n";
814 foreach $fct (sort keys %func_ulps) {
815 print ULP " { \"$fct\", $func_ulps{$fct} },\n";
817 print ULP " };\n";
818 print ULP "static const struct ulp_data func_real_ulps[] =\n {\n";
819 foreach $fct (sort keys %func_real_ulps) {
820 print ULP " { \"$fct\", $func_real_ulps{$fct} },\n";
822 print ULP " };\n";
823 print ULP "static const struct ulp_data func_imag_ulps[] =\n {\n";
824 foreach $fct (sort keys %func_imag_ulps) {
825 print ULP " { \"$fct\", $func_imag_ulps{$fct} },\n";
827 print ULP " };\n";
828 close ULP;
831 # Parse auto-libm-test-out.
832 sub parse_auto_input {
833 my ($file) = @_;
834 open AUTO, $file or die ("Can't open $file: $!");
835 while (<AUTO>) {
836 chop;
837 next if !/^= /;
838 s/^= //;
839 if (/^(\S+) (\S+) ([^: ][^ ]* [^:]*) : (.*)$/) {
840 $auto_tests{$1}{$2}{$3} = $4;
841 } else {
842 die ("bad automatic test line: $_\n");
845 close AUTO;