]> git.ipfire.org Git - thirdparty/glibc.git/blob - math/gen-libm-test.pl
Add totalorder, totalorderf, totalorderl.
[thirdparty/glibc.git] / math / gen-libm-test.pl
1 #!/usr/bin/perl -w
2 # Copyright (C) 1999-2016 Free Software Foundation, Inc.
3 # This file is part of the GNU C Library.
4 # Contributed by Andreas Jaeger <aj@suse.de>, 1999.
5
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.
10
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.
15
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/>.
19
20 # This file needs to be tidied up
21 # Note that functions and tests share the same namespace.
22
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
34
35
36 use Getopt::Std;
37
38 use strict;
39
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 ($output_dir $ulps_file $srcdir);
44 use vars qw (%auto_tests);
45
46 # all_floats is sorted and contains all recognised float types
47 @all_floats = ('double', 'float', 'idouble',
48 'ifloat', 'ildouble', 'ldouble');
49
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 );
56
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",
71 );
72
73
74 # get Options
75 # Options:
76 # u: ulps-file
77 # h: help
78 # o: output-directory
79 # n: generate new ulps file
80 use vars qw($opt_u $opt_h $opt_o $opt_n);
81 getopts('u:o:nh');
82
83 $ulps_file = 'libm-test-ulps';
84 $output_dir = '';
85 ($srcdir = $0) =~ s{[^/]*$}{};
86
87 if ($opt_h) {
88 print "Usage: gen-libm-test.pl [OPTIONS]\n";
89 print " -h print this help, then exit\n";
90 print " -o DIR directory where generated files will be placed\n";
91 print " -n only generate sorted file NewUlps from libm-test-ulps\n";
92 print " -u FILE input file with ulps\n";
93 exit 0;
94 }
95
96 $ulps_file = $opt_u if ($opt_u);
97 $output_dir = $opt_o if ($opt_o);
98
99 $input = "libm-test.inc";
100 $auto_input = "${srcdir}auto-libm-test-out";
101 $output = "${output_dir}libm-test.c";
102
103 &parse_ulps ($ulps_file);
104 &parse_auto_input ($auto_input);
105 &generate_testfile ($input, $output) unless ($opt_n);
106 &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
107 &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
108
109 # Return a nicer representation
110 sub beautify {
111 my ($arg) = @_;
112 my ($tmp);
113
114 if (exists $beautify{$arg}) {
115 return $beautify{$arg};
116 }
117 if ($arg =~ /^-/) {
118 $tmp = $arg;
119 $tmp =~ s/^-//;
120 if (exists $beautify{$tmp}) {
121 return '-' . $beautify{$tmp};
122 }
123 }
124 if ($arg =~ /^-?0x[0-9a-f.]*p[-+][0-9]+f$/) {
125 $arg =~ s/f$//;
126 }
127 if ($arg =~ /[0-9]L$/) {
128 $arg =~ s/L$//;
129 }
130 return $arg;
131 }
132
133 # Return a nicer representation of a complex number
134 sub build_complex_beautify {
135 my ($r, $i) = @_;
136 my ($str1, $str2);
137
138 $str1 = &beautify ($r);
139 $str2 = &beautify ($i);
140 if ($str2 =~ /^-/) {
141 $str2 =~ s/^-//;
142 $str1 .= ' - ' . $str2;
143 } else {
144 $str1 .= ' + ' . $str2;
145 }
146 $str1 .= ' i';
147 return $str1;
148 }
149
150 # Return the text to put in an initializer for a test's exception
151 # information.
152 sub show_exceptions {
153 my ($ignore_result, $non_finite, $test_snan, $exception) = @_;
154 $ignore_result = ($ignore_result ? "IGNORE_RESULT|" : "");
155 $non_finite = ($non_finite ? "NON_FINITE|" : "");
156 $test_snan = ($test_snan ? "TEST_SNAN|" : "");
157 if (defined $exception) {
158 return ", ${ignore_result}${non_finite}${test_snan}$exception";
159 } else {
160 return ", ${ignore_result}${non_finite}${test_snan}0";
161 }
162 }
163
164 # Apply the LIT(x) macro to a literal floating point constant
165 # and strip any existing suffix.
166 sub _apply_lit {
167 my ($lit) = @_;
168 my $exp_re = "([+-])?[[:digit:]]+";
169 # Don't wrap something that does not look like a:
170 # * Hexadecimal FP value
171 # * Decimal FP value without a decimal point
172 # * Decimal value with a fraction
173 return $lit if $lit !~ /([+-])?0x[[:xdigit:]\.]+[pP]$exp_re/
174 and $lit !~ /[[:digit:]]+[eE]$exp_re/
175 and $lit !~ /[[:digit:]]*\.[[:digit:]]*([eE]$exp_re)?/;
176
177 # Strip any existing literal suffix.
178 $lit =~ s/[lLfF]$//;
179
180 return "LIT (${lit})";
181 }
182
183 # Apply LIT macro to individual tokens within an expression.
184 #
185 # This function assumes the C expression follows GNU coding
186 # standards. Specifically, a space separates each lexical
187 # token. Otherwise, this post-processing may apply LIT
188 # incorrectly, or around an entire expression.
189 sub apply_lit {
190 my ($lit) = @_;
191 my @toks = split (/ /, $lit);
192 foreach (@toks) {
193 $_ = _apply_lit ($_);
194 }
195 return join (' ', @toks);
196 }
197
198 # Parse the arguments to TEST_x_y
199 sub parse_args {
200 my ($file, $descr, $args) = @_;
201 my (@args, $descr_args, $descr_res, @descr);
202 my ($current_arg, $cline, $cline_res, $i);
203 my (@special);
204 my ($call_args);
205 my ($ignore_result_any, $ignore_result_all);
206 my ($num_res, @args_res, @start_rm, $rm);
207 my (@plus_oflow, @minus_oflow, @plus_uflow, @minus_uflow);
208 my (@errno_plus_oflow, @errno_minus_oflow);
209 my (@errno_plus_uflow, @errno_minus_uflow);
210 my ($non_finite, $test_snan);
211
212 ($descr_args, $descr_res) = split /_/,$descr, 2;
213
214 @args = split /,\s*/, $args;
215
216 $call_args = "";
217
218 # Generate first the string that's shown to the user
219 $current_arg = 1;
220 @descr = split //,$descr_args;
221 for ($i = 0; $i <= $#descr; $i++) {
222 my $comma = "";
223 if ($current_arg > 1) {
224 $comma = ', ';
225 }
226 # FLOAT, int, long int, long long int
227 if ($descr[$i] =~ /f|j|i|l|L/) {
228 $call_args .= $comma . &beautify ($args[$current_arg]);
229 ++$current_arg;
230 next;
231 }
232 # &FLOAT, &int - simplify call by not showing argument.
233 if ($descr[$i] =~ /F|I/) {
234 next;
235 }
236 # complex
237 if ($descr[$i] eq 'c') {
238 $call_args .= $comma . &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
239 $current_arg += 2;
240 next;
241 }
242
243 die ("$descr[$i] is unknown");
244 }
245
246 # Result
247 @args_res = @args[$current_arg .. $#args];
248 $num_res = 0;
249 @descr = split //,$descr_res;
250 foreach (@descr) {
251 if ($_ =~ /f|i|l|L/) {
252 ++$num_res;
253 } elsif ($_ eq 'c') {
254 $num_res += 2;
255 } elsif ($_ eq 'b') {
256 # boolean
257 ++$num_res;
258 } elsif ($_ eq '1') {
259 ++$num_res;
260 } else {
261 die ("$_ is unknown");
262 }
263 }
264 # consistency check
265 if ($#args_res == $num_res - 1) {
266 # One set of results for all rounding modes, no flags.
267 @start_rm = ( 0, 0, 0, 0 );
268 } elsif ($#args_res == $num_res) {
269 # One set of results for all rounding modes, with flags.
270 die ("wrong number of arguments")
271 unless ($args_res[$#args_res] =~ /EXCEPTION|ERRNO|IGNORE_ZERO_INF_SIGN|TEST_NAN_SIGN|NO_TEST_INLINE|XFAIL_TEST/);
272 @start_rm = ( 0, 0, 0, 0 );
273 } elsif ($#args_res == 4 * $num_res + 3) {
274 # One set of results per rounding mode, with flags.
275 @start_rm = ( 0, $num_res + 1, 2 * $num_res + 2, 3 * $num_res + 3 );
276 } else {
277 die ("wrong number of arguments");
278 }
279
280 # Put the C program line together
281 # Reset some variables to start again
282 $current_arg = 1;
283 $call_args =~ s/\"/\\\"/g;
284 $cline = "{ \"$call_args\"";
285 @descr = split //,$descr_args;
286 for ($i=0; $i <= $#descr; $i++) {
287 # FLOAT, int, long int, long long int
288 if ($descr[$i] =~ /f|j|i|l|L/) {
289 if ($descr[$i] eq "f") {
290 $cline .= ", " . &apply_lit ($args[$current_arg]);
291 } else {
292 $cline .= ", $args[$current_arg]";
293 }
294 $current_arg++;
295 next;
296 }
297 # &FLOAT, &int
298 if ($descr[$i] =~ /F|I/) {
299 next;
300 }
301 # complex
302 if ($descr[$i] eq 'c') {
303 $cline .= ", " . &apply_lit ($args[$current_arg]);
304 $cline .= ", " . &apply_lit ($args[$current_arg+1]);
305 $current_arg += 2;
306 next;
307 }
308 }
309
310 @descr = split //,$descr_res;
311 @plus_oflow = qw(max_value plus_infty max_value plus_infty);
312 @minus_oflow = qw(minus_infty minus_infty -max_value -max_value);
313 @plus_uflow = qw(plus_zero plus_zero plus_zero min_subnorm_value);
314 @minus_uflow = qw(-min_subnorm_value minus_zero minus_zero minus_zero);
315 @errno_plus_oflow = qw(0 ERRNO_ERANGE 0 ERRNO_ERANGE);
316 @errno_minus_oflow = qw(ERRNO_ERANGE ERRNO_ERANGE 0 0);
317 @errno_plus_uflow = qw(ERRNO_ERANGE ERRNO_ERANGE ERRNO_ERANGE 0);
318 @errno_minus_uflow = qw(0 ERRNO_ERANGE ERRNO_ERANGE ERRNO_ERANGE);
319 for ($rm = 0; $rm <= 3; $rm++) {
320 $current_arg = $start_rm[$rm];
321 $ignore_result_any = 0;
322 $ignore_result_all = 1;
323 $cline_res = "";
324 @special = ();
325 foreach (@descr) {
326 if ($_ =~ /b|f|j|i|l|L/ ) {
327 my ($result) = $args_res[$current_arg];
328 if ($result eq "IGNORE") {
329 $ignore_result_any = 1;
330 $result = "0";
331 } else {
332 $ignore_result_all = 0;
333 }
334 if ($_ eq "f") {
335 $result = apply_lit ($result);
336 }
337 $cline_res .= ", $result";
338 $current_arg++;
339 } elsif ($_ eq 'c') {
340 my ($result1) = $args_res[$current_arg];
341 if ($result1 eq "IGNORE") {
342 $ignore_result_any = 1;
343 $result1 = "0";
344 } else {
345 $ignore_result_all = 0;
346 }
347 my ($result2) = $args_res[$current_arg + 1];
348 if ($result2 eq "IGNORE") {
349 $ignore_result_any = 1;
350 $result2 = "0";
351 } else {
352 $ignore_result_all = 0;
353 }
354 $result1 = apply_lit ($result1);
355 $result2 = apply_lit ($result2);
356 $cline_res .= ", $result1, $result2";
357 $current_arg += 2;
358 } elsif ($_ eq '1') {
359 push @special, $args_res[$current_arg];
360 ++$current_arg;
361 }
362 }
363 if ($ignore_result_any && !$ignore_result_all) {
364 die ("some but not all function results ignored\n");
365 }
366 # Determine whether any arguments or results, for any rounding
367 # mode, are non-finite.
368 $non_finite = ($args =~ /qnan_value|snan_value|plus_infty|minus_infty/);
369 $test_snan = ($args =~ /snan_value/);
370 # Add exceptions.
371 $cline_res .= show_exceptions ($ignore_result_any,
372 $non_finite,
373 $test_snan,
374 ($current_arg <= $#args_res)
375 ? $args_res[$current_arg]
376 : undef);
377
378 # special treatment for some functions
379 $i = 0;
380 foreach (@special) {
381 ++$i;
382 my ($extra_expected) = $_;
383 my ($run_extra) = ($extra_expected ne "IGNORE" ? 1 : 0);
384 if (!$run_extra) {
385 $extra_expected = "0";
386 } else {
387 $extra_expected = apply_lit ($extra_expected);
388 }
389 $cline_res .= ", $run_extra, $extra_expected";
390 }
391 $cline_res =~ s/^, //;
392 $cline_res =~ s/plus_oflow/$plus_oflow[$rm]/g;
393 $cline_res =~ s/minus_oflow/$minus_oflow[$rm]/g;
394 $cline_res =~ s/plus_uflow/$plus_uflow[$rm]/g;
395 $cline_res =~ s/minus_uflow/$minus_uflow[$rm]/g;
396 $cline_res =~ s/ERRNO_PLUS_OFLOW/$errno_plus_oflow[$rm]/g;
397 $cline_res =~ s/ERRNO_MINUS_OFLOW/$errno_minus_oflow[$rm]/g;
398 $cline_res =~ s/ERRNO_PLUS_UFLOW/$errno_plus_uflow[$rm]/g;
399 $cline_res =~ s/ERRNO_MINUS_UFLOW/$errno_minus_uflow[$rm]/g;
400 $cline .= ", { $cline_res }";
401 }
402 print $file " $cline },\n";
403 }
404
405 # Convert a condition from auto-libm-test-out to C form.
406 sub convert_condition {
407 my ($cond) = @_;
408 my (@conds, $ret);
409 @conds = split /:/, $cond;
410 foreach (@conds) {
411 s/-/_/g;
412 s/^/TEST_COND_/;
413 }
414 $ret = join " && ", @conds;
415 return "($ret)";
416 }
417
418 # Return text to OR a value into an accumulated flags string.
419 sub or_value {
420 my ($cond) = @_;
421 if ($cond eq "0") {
422 return "";
423 } else {
424 return " | $cond";
425 }
426 }
427
428 # Return a conditional expression between two values.
429 sub cond_value {
430 my ($cond, $if, $else) = @_;
431 if ($cond eq "1") {
432 return $if;
433 } elsif ($cond eq "0") {
434 return $else;
435 } else {
436 return "($cond ? $if : $else)";
437 }
438 }
439
440 # Return text to OR a conditional expression between two values into
441 # an accumulated flags string.
442 sub or_cond_value {
443 my ($cond, $if, $else) = @_;
444 return or_value (cond_value ($cond, $if, $else));
445 }
446
447 # Generate libm-test.c
448 sub generate_testfile {
449 my ($input, $output) = @_;
450
451 open INPUT, $input or die ("Can't open $input: $!");
452 open OUTPUT, ">$output" or die ("Can't open $output: $!");
453
454 # Replace the special macros
455 while (<INPUT>) {
456 # AUTO_TESTS (function),
457 if (/^\s*AUTO_TESTS_/) {
458 my ($descr, $func, @modes, $auto_test, $num_auto_tests);
459 my (@rm_tests, $rm, $i);
460 @modes = qw(downward tonearest towardzero upward);
461 ($descr, $func) = ($_ =~ /AUTO_TESTS_(\w+)\s*\((\w+)\)/);
462 for ($rm = 0; $rm <= 3; $rm++) {
463 $rm_tests[$rm] = [sort keys %{$auto_tests{$func}{$modes[$rm]}}];
464 }
465 $num_auto_tests = scalar @{$rm_tests[0]};
466 for ($rm = 1; $rm <= 3; $rm++) {
467 if ($num_auto_tests != scalar @{$rm_tests[$rm]}) {
468 die ("inconsistent numbers of tests for $func\n");
469 }
470 for ($i = 0; $i < $num_auto_tests; $i++) {
471 if ($rm_tests[0][$i] ne $rm_tests[$rm][$i]) {
472 die ("inconsistent list of tests of $func\n");
473 }
474 }
475 }
476 if ($num_auto_tests == 0) {
477 die ("no automatic tests for $func\n");
478 }
479 foreach $auto_test (@{$rm_tests[0]}) {
480 my ($format, $inputs, $format_conv, $args_str);
481 ($format, $inputs) = split / /, $auto_test, 2;
482 $inputs =~ s/ /, /g;
483 $format_conv = convert_condition ($format);
484 print OUTPUT "#if $format_conv\n";
485 $args_str = "$func, $inputs";
486 for ($rm = 0; $rm <= 3; $rm++) {
487 my ($auto_test_out, $outputs, $flags);
488 my ($flags_conv, @flags, %flag_cond);
489 $auto_test_out = $auto_tests{$func}{$modes[$rm]}{$auto_test};
490 ($outputs, $flags) = split / : */, $auto_test_out;
491 $outputs =~ s/ /, /g;
492 @flags = split / /, $flags;
493 foreach (@flags) {
494 if (/^([^:]*):(.*)$/) {
495 my ($flag, $cond);
496 $flag = $1;
497 $cond = convert_condition ($2);
498 if (defined ($flag_cond{$flag})) {
499 if ($flag_cond{$flag} ne "1") {
500 $flag_cond{$flag} .= " || $cond";
501 }
502 } else {
503 $flag_cond{$flag} = $cond;
504 }
505 } else {
506 $flag_cond{$_} = "1";
507 }
508 }
509 $flags_conv = "";
510 if (defined ($flag_cond{"ignore-zero-inf-sign"})) {
511 $flags_conv .= or_cond_value ($flag_cond{"ignore-zero-inf-sign"},
512 "IGNORE_ZERO_INF_SIGN", "0");
513 }
514 if (defined ($flag_cond{"no-test-inline"})) {
515 $flags_conv .= or_cond_value ($flag_cond{"no-test-inline"},
516 "NO_TEST_INLINE", "0");
517 }
518 if (defined ($flag_cond{"xfail"})) {
519 $flags_conv .= or_cond_value ($flag_cond{"xfail"},
520 "XFAIL_TEST", "0");
521 }
522 my (@exc_list) = qw(divbyzero inexact invalid overflow underflow);
523 my ($exc);
524 foreach $exc (@exc_list) {
525 my ($exc_expected, $exc_ok, $no_exc, $exc_cond, $exc_ok_cond);
526 $exc_expected = "\U$exc\E_EXCEPTION";
527 $exc_ok = "\U$exc\E_EXCEPTION_OK";
528 $no_exc = "0";
529 if ($exc eq "inexact") {
530 $exc_ok = "0";
531 $no_exc = "NO_INEXACT_EXCEPTION";
532 }
533 if (defined ($flag_cond{$exc})) {
534 $exc_cond = $flag_cond{$exc};
535 } else {
536 $exc_cond = "0";
537 }
538 if (defined ($flag_cond{"$exc-ok"})) {
539 $exc_ok_cond = $flag_cond{"$exc-ok"};
540 } else {
541 $exc_ok_cond = "0";
542 }
543 $flags_conv .= or_cond_value ($exc_cond,
544 cond_value ($exc_ok_cond,
545 $exc_ok, $exc_expected),
546 cond_value ($exc_ok_cond,
547 $exc_ok, $no_exc));
548 }
549 my ($errno_expected, $errno_unknown_cond);
550 if (defined ($flag_cond{"errno-edom"})) {
551 if ($flag_cond{"errno-edom"} ne "1") {
552 die ("unexpected condition for errno-edom");
553 }
554 if (defined ($flag_cond{"errno-erange"})) {
555 die ("multiple errno values expected");
556 }
557 $errno_expected = "ERRNO_EDOM";
558 } elsif (defined ($flag_cond{"errno-erange"})) {
559 if ($flag_cond{"errno-erange"} ne "1") {
560 die ("unexpected condition for errno-erange");
561 }
562 $errno_expected = "ERRNO_ERANGE";
563 } else {
564 $errno_expected = "ERRNO_UNCHANGED";
565 }
566 if (defined ($flag_cond{"errno-edom-ok"})) {
567 if (defined ($flag_cond{"errno-erange-ok"})
568 && ($flag_cond{"errno-erange-ok"}
569 ne $flag_cond{"errno-edom-ok"})) {
570 $errno_unknown_cond = "($flag_cond{\"errno-edom-ok\"} || $flag_cond{\"errno-erange-ok\"})";
571 } else {
572 $errno_unknown_cond = $flag_cond{"errno-edom-ok"};
573 }
574 } elsif (defined ($flag_cond{"errno-erange-ok"})) {
575 $errno_unknown_cond = $flag_cond{"errno-erange-ok"};
576 } else {
577 $errno_unknown_cond = "0";
578 }
579 $flags_conv .= or_cond_value ($errno_unknown_cond,
580 "0", $errno_expected);
581 if ($flags_conv eq "") {
582 $flags_conv = ", NO_EXCEPTION";
583 } else {
584 $flags_conv =~ s/^ \|/,/;
585 }
586 $args_str .= ", $outputs$flags_conv";
587 }
588 &parse_args (\*OUTPUT, $descr, $args_str);
589 print OUTPUT "#endif\n";
590 }
591 next;
592 }
593
594 # TEST_...
595 if (/^\s*TEST_/) {
596 my ($descr, $args);
597 chop;
598 ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
599 &parse_args (\*OUTPUT, $descr, $args);
600 next;
601 }
602 print OUTPUT;
603 }
604 close INPUT;
605 close OUTPUT;
606 }
607
608
609
610 # Parse ulps file
611 sub parse_ulps {
612 my ($file) = @_;
613 my ($test, $type, $float, $eps, $float_regex);
614
615 # Build a basic regex to match type entries in the
616 # generated ULPS file.
617 foreach my $ftype (@all_floats) {
618 $float_regex .= "|" . $ftype;
619 }
620 $float_regex = "^" . substr ($float_regex, 1) . ":";
621
622 # $type has the following values:
623 # "normal": No complex variable
624 # "real": Real part of complex result
625 # "imag": Imaginary part of complex result
626 open ULP, $file or die ("Can't open $file: $!");
627 while (<ULP>) {
628 chop;
629 # ignore comments and empty lines
630 next if /^#/;
631 next if /^\s*$/;
632 if (/^Function: /) {
633 if (/Real part of/) {
634 s/Real part of //;
635 $type = 'real';
636 } elsif (/Imaginary part of/) {
637 s/Imaginary part of //;
638 $type = 'imag';
639 } else {
640 $type = 'normal';
641 }
642 ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
643 next;
644 }
645 if (/$float_regex/) {
646 ($float, $eps) = split /\s*:\s*/,$_,2;
647
648 if ($eps eq "0") {
649 # ignore
650 next;
651 } else {
652 if (!defined ($results{$test}{$type}{'ulp'}{$float})
653 || $results{$test}{$type}{'ulp'}{$float} < $eps) {
654 $results{$test}{$type}{'ulp'}{$float} = $eps;
655 $results{$test}{'has_ulps'} = 1;
656 }
657 }
658 if ($type =~ /^real|imag$/) {
659 $results{$test}{'type'} = 'complex';
660 } elsif ($type eq 'normal') {
661 $results{$test}{'type'} = 'normal';
662 }
663 next;
664 }
665 print "Skipping unknown entry: `$_'\n";
666 }
667 close ULP;
668 }
669
670
671 # Clean up a floating point number
672 sub clean_up_number {
673 my ($number) = @_;
674
675 # Remove trailing zeros after the decimal point
676 if ($number =~ /\./) {
677 $number =~ s/0+$//;
678 $number =~ s/\.$//;
679 }
680 return $number;
681 }
682
683 # Output a file which can be read in as ulps file.
684 sub print_ulps_file {
685 my ($file) = @_;
686 my ($test, $type, $float, $eps, $fct, $last_fct);
687
688 $last_fct = '';
689 open NEWULP, ">$file" or die ("Can't open $file: $!");
690 print NEWULP "# Begin of automatic generation\n";
691 print NEWULP "\n# Maximal error of functions:\n";
692
693 foreach $fct (sort keys %results) {
694 foreach $type ('real', 'imag', 'normal') {
695 if (exists $results{$fct}{$type}) {
696 if ($type eq 'normal') {
697 print NEWULP "Function: \"$fct\":\n";
698 } elsif ($type eq 'real') {
699 print NEWULP "Function: Real part of \"$fct\":\n";
700 } elsif ($type eq 'imag') {
701 print NEWULP "Function: Imaginary part of \"$fct\":\n";
702 }
703 foreach $float (@all_floats) {
704 if (exists $results{$fct}{$type}{'ulp'}{$float}) {
705 print NEWULP "$float: ",
706 &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
707 "\n";
708 }
709 }
710 print NEWULP "\n";
711 }
712 }
713 }
714 print NEWULP "# end of automatic generation\n";
715 close NEWULP;
716 }
717
718 sub get_ulps {
719 my ($test, $type, $float) = @_;
720
721 return (exists $results{$test}{$type}{'ulp'}{$float}
722 ? $results{$test}{$type}{'ulp'}{$float} : "0");
723 }
724
725 # Return the ulps value for a single test.
726 sub get_all_ulps_for_test {
727 my ($test, $type) = @_;
728 my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
729 my ($ulps_str);
730
731 if (exists $results{$test}{'has_ulps'}) {
732 foreach $float (@all_floats) {
733 $ulps_str .= &get_ulps ($test, $type, $float) . ", ";
734 }
735 return "{" . substr ($ulps_str, 0, -2) . "}";
736 } else {
737 die "get_all_ulps_for_test called for \"$test\" with no ulps\n";
738 }
739 }
740
741 # Print include file
742 sub output_ulps {
743 my ($file, $ulps_filename) = @_;
744 my ($i, $fct, $type, $ulp, $ulp_real, $ulp_imag);
745 my (%func_ulps, %func_real_ulps, %func_imag_ulps);
746
747 open ULP, ">$file" or die ("Can't open $file: $!");
748
749 print ULP "/* This file is automatically generated\n";
750 print ULP " from $ulps_filename with gen-libm-test.pl.\n";
751 print ULP " Don't change it - change instead the master files. */\n\n";
752
753 print ULP "struct ulp_data\n";
754 print ULP "{\n";
755 print ULP " const char *name;\n";
756 print ULP " FLOAT max_ulp[" . @all_floats . "];\n";
757 print ULP "};\n\n";
758
759 for ($i = 0; $i <= $#all_floats; $i++) {
760 $type = $all_floats[$i];
761 print ULP "#define ULP_";
762 if ($type =~ /^i/) {
763 print ULP "I_";
764 $type = substr $type, 1;
765 }
766 print ULP "$all_floats_pfx{$type} $i\n";
767 }
768
769 foreach $fct (keys %results) {
770 $type = $results{$fct}{'type'};
771 if ($type eq 'normal') {
772 $ulp = get_all_ulps_for_test ($fct, 'normal');
773 } elsif ($type eq 'complex') {
774 $ulp_real = get_all_ulps_for_test ($fct, 'real');
775 $ulp_imag = get_all_ulps_for_test ($fct, 'imag');
776 } else {
777 die "unknown results ($fct) type $type\n";
778 }
779 if ($type eq 'normal') {
780 $func_ulps{$fct} = $ulp;
781 } else {
782 $func_real_ulps{$fct} = $ulp_real;
783 $func_imag_ulps{$fct} = $ulp_imag;
784 }
785 }
786 print ULP "\n/* Maximal error of functions. */\n";
787 print ULP "static const struct ulp_data func_ulps[] =\n {\n";
788 foreach $fct (sort keys %func_ulps) {
789 print ULP " { \"$fct\", $func_ulps{$fct} },\n";
790 }
791 print ULP " };\n";
792 print ULP "static const struct ulp_data func_real_ulps[] =\n {\n";
793 foreach $fct (sort keys %func_real_ulps) {
794 print ULP " { \"$fct\", $func_real_ulps{$fct} },\n";
795 }
796 print ULP " };\n";
797 print ULP "static const struct ulp_data func_imag_ulps[] =\n {\n";
798 foreach $fct (sort keys %func_imag_ulps) {
799 print ULP " { \"$fct\", $func_imag_ulps{$fct} },\n";
800 }
801 print ULP " };\n";
802 close ULP;
803 }
804
805 # Parse auto-libm-test-out.
806 sub parse_auto_input {
807 my ($file) = @_;
808 open AUTO, $file or die ("Can't open $file: $!");
809 while (<AUTO>) {
810 chop;
811 next if !/^= /;
812 s/^= //;
813 if (/^(\S+) (\S+) ([^:]*) : (.*)$/) {
814 $auto_tests{$1}{$2}{$3} = $4;
815 } else {
816 die ("bad automatic test line: $_\n");
817 }
818 }
819 close AUTO;
820 }