gen-libm-test.pl 23 KB

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