gen-libm-test.pl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. #!/usr/bin/perl -w
  2. # Copyright (C) 1999 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, write to the Free
  15. # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. # 02111-1307 USA.
  17. # This file needs to be tidied up
  18. # Note that functions and tests share the same namespace.
  19. # Information about tests are stored in: %results
  20. # $results{$test}{"kind"} is either "fct" or "test" and flags whether this
  21. # is a maximal error of a function or a single test.
  22. # $results{$test}{"type"} is the result type, e.g. normal or complex.
  23. # $results{$test}{"has_ulps"} is set if deltas exist.
  24. # $results{$test}{"has_fails"} is set if exptected failures exist.
  25. # In the following description $type and $float are:
  26. # - $type is either "normal", "real" (for the real part of a complex number)
  27. # or "imag" (for the imaginary part # of a complex number).
  28. # - $float is either of float, ifloat, double, idouble, ldouble, ildouble;
  29. # It represents the underlying floating point type (float, double or long
  30. # double) and if inline functions (the leading i stands for inline)
  31. # are used.
  32. # $results{$test}{$type}{"fail"}{$float} is defined and has a 1 if
  33. # the test is expected to fail
  34. # $results{$test}{$type}{"ulp"}{$float} is defined and has a delta as value
  35. use Getopt::Std;
  36. use strict;
  37. use vars qw ($input $output);
  38. use vars qw (%results);
  39. use vars qw (@tests @functions);
  40. use vars qw ($count);
  41. use vars qw (%beautify @all_floats);
  42. use vars qw ($output_dir $ulps_file);
  43. # all_floats is sorted and contains all recognised float types
  44. @all_floats = ('double', 'float', 'idouble',
  45. 'ifloat', 'ildouble', 'ldouble');
  46. %beautify =
  47. ( "minus_zero" => "-0",
  48. "plus_zero" => "+0",
  49. "minus_infty" => "-inf",
  50. "plus_infty" => "inf",
  51. "nan_value" => "NaN",
  52. "M_El" => "e",
  53. "M_E2l" => "e^2",
  54. "M_E3l" => "e^3",
  55. "M_LOG10El", "log10(e)",
  56. "M_PIl" => "pi",
  57. "M_PI_34l" => "3/4 pi",
  58. "M_PI_2l" => "pi/2",
  59. "M_PI_4l" => "pi/4",
  60. "M_PI_6l" => "pi/6",
  61. "M_PI_34_LOG10El" => "3/4 pi*log10(e)",
  62. "M_PI_LOG10El" => "pi*log10(e)",
  63. "M_PI2_LOG10El" => "pi/2*log10(e)",
  64. "M_PI4_LOG10El" => "pi/4*log10(e)",
  65. "M_LOG_SQRT_PIl" => "log(sqrt(pi))",
  66. "M_LOG_2_SQRT_PIl" => "log(2*sqrt(pi))",
  67. "M_2_SQRT_PIl" => "2 sqrt (pi)",
  68. "M_SQRT_PIl" => "sqrt (pi)",
  69. "INVALID_EXCEPTION" => "invalid exception",
  70. "DIVIDE_BY_ZERO_EXCEPTION" => "division by zero exception",
  71. "INVALID_EXCEPTION_OK" => "invalid exception allowed",
  72. "DIVIDE_BY_ZERO_EXCEPTION_OK" => "division by zero exception allowed",
  73. "EXCEPTIONS_OK" => "exceptions allowed",
  74. "IGNORE_ZERO_INF_SIGN" => "sign of zero/inf not specified",
  75. "INVALID_EXCEPTION|IGNORE_ZERO_INF_SIGN" => "invalid exception and sign of zero/inf not specified"
  76. );
  77. # get Options
  78. # Options:
  79. # u: ulps-file
  80. # h: help
  81. # o: output-directory
  82. # n: generate new ulps file
  83. use vars qw($opt_u $opt_h $opt_o $opt_n);
  84. getopts('u:o:nh');
  85. $ulps_file = 'libm-test-ulps';
  86. $output_dir = '';
  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. $ulps_file = $opt_u if ($opt_u);
  96. $output_dir = $opt_o if ($opt_o);
  97. $input = "libm-test.inc";
  98. $output = "${output_dir}libm-test.c";
  99. $count = 0;
  100. &parse_ulps ($ulps_file);
  101. &generate_testfile ($input, $output) unless ($opt_n);
  102. &output_ulps ("${output_dir}libm-test-ulps.h", $ulps_file) unless ($opt_n);
  103. &print_ulps_file ("${output_dir}NewUlps") if ($opt_n);
  104. # Return a nicer representation
  105. sub beautify {
  106. my ($arg) = @_;
  107. my ($tmp);
  108. if (exists $beautify{$arg}) {
  109. return $beautify{$arg};
  110. }
  111. if ($arg =~ /^-/) {
  112. $tmp = $arg;
  113. $tmp =~ s/^-//;
  114. if (exists $beautify{$tmp}) {
  115. return '-' . $beautify{$tmp};
  116. }
  117. }
  118. if ($arg =~ /[0-9]L$/) {
  119. $arg =~ s/L$//;
  120. }
  121. return $arg;
  122. }
  123. # Return a nicer representation of a complex number
  124. sub build_complex_beautify {
  125. my ($r, $i) = @_;
  126. my ($str1, $str2);
  127. $str1 = &beautify ($r);
  128. $str2 = &beautify ($i);
  129. if ($str2 =~ /^-/) {
  130. $str2 =~ s/^-//;
  131. $str1 .= ' - ' . $str2;
  132. } else {
  133. $str1 .= ' + ' . $str2;
  134. }
  135. $str1 .= ' i';
  136. return $str1;
  137. }
  138. # Return name of a variable
  139. sub get_variable {
  140. my ($number) = @_;
  141. return "x" if ($number == 1);
  142. return "y" if ($number == 2);
  143. return "z" if ($number == 3);
  144. # return x1,x2,...
  145. $number =-3;
  146. return "x$number";
  147. }
  148. # Add a new test to internal data structures and fill in the
  149. # ulps, failures and exception information for the C line.
  150. sub new_test {
  151. my ($test, $exception) = @_;
  152. my $rest;
  153. # Add ulp, xfail
  154. if (exists $results{$test}{'has_ulps'}) {
  155. $rest = ", DELTA$count";
  156. } else {
  157. $rest = ', 0';
  158. }
  159. if (exists $results{$test}{'has_fails'}) {
  160. $rest .= ", FAIL$count";
  161. } else {
  162. $rest .= ', 0';
  163. }
  164. if (defined $exception) {
  165. $rest .= ", $exception";
  166. } else {
  167. $rest .= ', 0';
  168. }
  169. $rest .= ");\n";
  170. # We must increment here to keep @tests and count in sync
  171. push @tests, $test;
  172. ++$count;
  173. return $rest;
  174. }
  175. # Treat some functions especially.
  176. # Currently only sincos needs extra treatment.
  177. sub special_functions {
  178. my ($file, $args) = @_;
  179. my (@args, $str, $test, $cline);
  180. @args = split /,\s*/, $args;
  181. unless ($args[0] =~ /sincos/) {
  182. die ("Don't know how to handle $args[0] extra.");
  183. }
  184. print $file " FUNC (sincos) ($args[1], &sin_res, &cos_res);\n";
  185. $str = 'sincos (' . &beautify ($args[1]) . ', &sin_res, &cos_res)';
  186. # handle sin
  187. $test = $str . ' puts ' . &beautify ($args[2]) . ' in sin_res';
  188. if ($#args == 4) {
  189. $test .= " plus " . &beautify ($args[4]);
  190. }
  191. $cline = " check_float (\"$test\", sin_res, $args[2]";
  192. $cline .= &new_test ($test, $args[4]);
  193. print $file $cline;
  194. # handle cos
  195. $test = $str . ' puts ' . &beautify ($args[3]) . ' in cos_res';
  196. $cline = " check_float (\"$test\", cos_res, $args[3]";
  197. # only tests once for exception
  198. $cline .= &new_test ($test, undef);
  199. print $file $cline;
  200. }
  201. # Parse the arguments to TEST_x_y
  202. sub parse_args {
  203. my ($file, $descr, $args) = @_;
  204. my (@args, $str, $descr_args, $descr_res, @descr);
  205. my ($current_arg, $cline, $i);
  206. my ($pre, $post, @special);
  207. my ($extra_var, $call, $c_call);
  208. if ($descr eq 'extra') {
  209. &special_functions ($file, $args);
  210. return;
  211. }
  212. ($descr_args, $descr_res) = split /_/,$descr, 2;
  213. @args = split /,\s*/, $args;
  214. $call = "$args[0] (";
  215. # Generate first the string that's shown to the user
  216. $current_arg = 1;
  217. $extra_var = 0;
  218. @descr = split //,$descr_args;
  219. for ($i = 0; $i <= $#descr; $i++) {
  220. if ($i >= 1) {
  221. $call .= ', ';
  222. }
  223. # FLOAT, int, long int, long long int
  224. if ($descr[$i] =~ /f|i|l|L/) {
  225. $call .= &beautify ($args[$current_arg]);
  226. ++$current_arg;
  227. next;
  228. }
  229. # &FLOAT, &int - argument is added here
  230. if ($descr[$i] =~ /F|I/) {
  231. ++$extra_var;
  232. $call .= '&' . &get_variable ($extra_var);
  233. next;
  234. }
  235. # complex
  236. if ($descr[$i] eq 'c') {
  237. $call .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
  238. $current_arg += 2;
  239. next;
  240. }
  241. die ("$descr[$i] is unknown");
  242. }
  243. $call .= ')';
  244. $str = "$call == ";
  245. # Result
  246. @descr = split //,$descr_res;
  247. foreach (@descr) {
  248. if ($_ =~ /f|i|l|L/) {
  249. $str .= &beautify ($args[$current_arg]);
  250. ++$current_arg;
  251. } elsif ($_ eq 'c') {
  252. $str .= &build_complex_beautify ($args[$current_arg], $args[$current_arg+1]);
  253. $current_arg += 2;
  254. } elsif ($_ eq 'b') {
  255. # boolean
  256. $str .= ($args[$current_arg] == 0) ? "false" : "true";
  257. ++$current_arg;
  258. } elsif ($_ eq '1') {
  259. ++$current_arg;
  260. } else {
  261. die ("$_ is unknown");
  262. }
  263. }
  264. # consistency check
  265. if ($current_arg == $#args) {
  266. die ("wrong number of arguments")
  267. unless ($args[$current_arg] =~ /EXCEPTION|IGNORE_ZERO_INF_SIGN/);
  268. } elsif ($current_arg < $#args) {
  269. die ("wrong number of arguments");
  270. } elsif ($current_arg > ($#args+1)) {
  271. die ("wrong number of arguments");
  272. }
  273. # check for exceptions
  274. if ($current_arg <= $#args) {
  275. $str .= " plus " . &beautify ($args[$current_arg]);
  276. }
  277. # Put the C program line together
  278. # Reset some variables to start again
  279. $current_arg = 1;
  280. $extra_var = 0;
  281. if (substr($descr_res,0,1) eq 'f') {
  282. $cline = 'check_float'
  283. } elsif (substr($descr_res,0,1) eq 'b') {
  284. $cline = 'check_bool';
  285. } elsif (substr($descr_res,0,1) eq 'c') {
  286. $cline = 'check_complex';
  287. } elsif (substr($descr_res,0,1) eq 'i') {
  288. $cline = 'check_int';
  289. } elsif (substr($descr_res,0,1) eq 'l') {
  290. $cline = 'check_long';
  291. } elsif (substr($descr_res,0,1) eq 'L') {
  292. $cline = 'check_longlong';
  293. }
  294. # Special handling for some macros:
  295. $cline .= " (\"$str\", ";
  296. if ($args[0] =~ /fpclassify|isnormal|isfinite|signbit/) {
  297. $c_call = "$args[0] (";
  298. } else {
  299. $c_call = " FUNC($args[0]) (";
  300. }
  301. @descr = split //,$descr_args;
  302. for ($i=0; $i <= $#descr; $i++) {
  303. if ($i >= 1) {
  304. $c_call .= ', ';
  305. }
  306. # FLOAT, int, long int, long long int
  307. if ($descr[$i] =~ /f|i|l|L/) {
  308. $c_call .= $args[$current_arg];
  309. $current_arg++;
  310. next;
  311. }
  312. # &FLOAT, &int
  313. if ($descr[$i] =~ /F|I/) {
  314. ++$extra_var;
  315. $c_call .= '&' . &get_variable ($extra_var);
  316. next;
  317. }
  318. # complex
  319. if ($descr[$i] eq 'c') {
  320. $c_call .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
  321. $current_arg += 2;
  322. next;
  323. }
  324. }
  325. $c_call .= ')';
  326. $cline .= "$c_call, ";
  327. @descr = split //,$descr_res;
  328. foreach (@descr) {
  329. if ($_ =~ /b|f|i|l|L/ ) {
  330. $cline .= $args[$current_arg];
  331. $current_arg++;
  332. } elsif ($_ eq 'c') {
  333. $cline .= "BUILD_COMPLEX ($args[$current_arg], $args[$current_arg+1])";
  334. $current_arg += 2;
  335. } elsif ($_ eq '1') {
  336. push @special, $args[$current_arg];
  337. ++$current_arg;
  338. }
  339. }
  340. # Add ulp, xfail
  341. $cline .= &new_test ($str, ($current_arg <= $#args) ? $args[$current_arg] : undef);
  342. # special treatment for some functions
  343. if ($args[0] eq 'frexp') {
  344. if (defined $special[0] && $special[0] ne "IGNORE") {
  345. my ($str) = "$call sets x to $special[0]";
  346. $post = " check_int (\"$str\", x, $special[0]";
  347. $post .= &new_test ($str, undef);
  348. }
  349. } elsif ($args[0] eq 'gamma' || $args[0] eq 'lgamma') {
  350. $pre = " signgam = 0;\n";
  351. if (defined $special[0] && $special[0] ne "IGNORE") {
  352. my ($str) = "$call sets signgam to $special[0]";
  353. $post = " check_int (\"$str\", signgam, $special[0]";
  354. $post .= &new_test ($str, undef);
  355. }
  356. } elsif ($args[0] eq 'modf') {
  357. if (defined $special[0] && $special[0] ne "IGNORE") {
  358. my ($str) = "$call sets x to $special[0]";
  359. $post = " check_float (\"$str\", x, $special[0]";
  360. $post .= &new_test ($str, undef);
  361. }
  362. } elsif ($args[0] eq 'remquo') {
  363. if (defined $special[0] && $special[0] ne "IGNORE") {
  364. my ($str) = "$call sets x to $special[0]";
  365. $post = " check_int (\"$str\", x, $special[0]";
  366. $post .= &new_test ($str, undef);
  367. }
  368. }
  369. print $file $pre if (defined $pre);
  370. print $file " $cline";
  371. print $file $post if (defined $post);
  372. }
  373. # Generate libm-test.c
  374. sub generate_testfile {
  375. my ($input, $output) = @_;
  376. my ($lasttext);
  377. my (@args, $i, $str);
  378. open INPUT, $input or die ("Can't open $input: $!");
  379. open OUTPUT, ">$output" or die ("Can't open $output: $!");
  380. # Replace the special macros
  381. while (<INPUT>) {
  382. # TEST_...
  383. if (/^\s*TEST_/) {
  384. my ($descr, $args);
  385. chop;
  386. ($descr, $args) = ($_ =~ /TEST_(\w+)\s*\((.*)\)/);
  387. &parse_args (\*OUTPUT, $descr, $args);
  388. next;
  389. }
  390. # START (function)
  391. if (/START/) {
  392. print OUTPUT " init_max_error ();\n";
  393. next;
  394. }
  395. # END (function)
  396. if (/END/) {
  397. my ($fct, $line, $type);
  398. if (/complex/) {
  399. s/,\s*complex\s*//;
  400. $type = 'complex';
  401. } else {
  402. $type = 'normal';
  403. }
  404. ($fct) = ($_ =~ /END\s*\((.*)\)/);
  405. if ($type eq 'complex') {
  406. $line = " print_complex_max_error (\"$fct\", ";
  407. } else {
  408. $line = " print_max_error (\"$fct\", ";
  409. }
  410. if (exists $results{$fct}{'has_ulps'}) {
  411. $line .= "DELTA$fct";
  412. } else {
  413. $line .= '0';
  414. }
  415. if (exists $results{$fct}{'has_fails'}) {
  416. $line .= ", FAIL$fct";
  417. } else {
  418. $line .= ', 0';
  419. }
  420. $line .= ");\n";
  421. print OUTPUT $line;
  422. push @functions, $fct;
  423. next;
  424. }
  425. print OUTPUT;
  426. }
  427. close INPUT;
  428. close OUTPUT;
  429. }
  430. # Parse ulps file
  431. sub parse_ulps {
  432. my ($file) = @_;
  433. my ($test, $type, $float, $eps, $kind);
  434. # $type has the following values:
  435. # "normal": No complex variable
  436. # "real": Real part of complex result
  437. # "imag": Imaginary part of complex result
  438. open ULP, $file or die ("Can't open $file: $!");
  439. while (<ULP>) {
  440. chop;
  441. # ignore comments and empty lines
  442. next if /^#/;
  443. next if /^\s*$/;
  444. if (/^Test/) {
  445. if (/Real part of:/) {
  446. s/Real part of: //;
  447. $type = 'real';
  448. } elsif (/Imaginary part of:/) {
  449. s/Imaginary part of: //;
  450. $type = 'imag';
  451. } else {
  452. $type = 'normal';
  453. }
  454. s/^.+\"(.*)\".*$/$1/;
  455. $test = $_;
  456. $kind = 'test';
  457. next;
  458. }
  459. if (/^Function: /) {
  460. if (/Real part of/) {
  461. s/Real part of //;
  462. $type = 'real';
  463. } elsif (/Imaginary part of/) {
  464. s/Imaginary part of //;
  465. $type = 'imag';
  466. } else {
  467. $type = 'normal';
  468. }
  469. ($test) = ($_ =~ /^Function:\s*\"([a-zA-Z0-9_]+)\"/);
  470. $kind = 'fct';
  471. next;
  472. }
  473. if (/^i?(float|double|ldouble):/) {
  474. ($float, $eps) = split /\s*:\s*/,$_,2;
  475. if ($eps eq 'fail') {
  476. $results{$test}{$type}{'fail'}{$float} = 1;
  477. $results{$test}{'has_fails'} = 1;
  478. } elsif ($eps eq "0") {
  479. # ignore
  480. next;
  481. } else {
  482. $results{$test}{$type}{'ulp'}{$float} = $eps;
  483. $results{$test}{'has_ulps'} = 1;
  484. }
  485. if ($type =~ /^real|imag$/) {
  486. $results{$test}{'type'} = 'complex';
  487. } elsif ($type eq 'normal') {
  488. $results{$test}{'type'} = 'normal';
  489. }
  490. $results{$test}{'kind'} = $kind;
  491. next;
  492. }
  493. print "Skipping unknown entry: `$_'\n";
  494. }
  495. close ULP;
  496. }
  497. # Clean up a floating point number
  498. sub clean_up_number {
  499. my ($number) = @_;
  500. # Remove trailing zeros
  501. $number =~ s/0+$//;
  502. $number =~ s/\.$//;
  503. return $number;
  504. }
  505. # Output a file which can be read in as ulps file.
  506. sub print_ulps_file {
  507. my ($file) = @_;
  508. my ($test, $type, $float, $eps, $fct, $last_fct);
  509. $last_fct = '';
  510. open NEWULP, ">$file" or die ("Can't open $file: $!");
  511. print NEWULP "# Begin of automatic generation\n";
  512. # first the function calls
  513. foreach $test (sort keys %results) {
  514. next if ($results{$test}{'kind'} ne 'test');
  515. foreach $type ('real', 'imag', 'normal') {
  516. if (exists $results{$test}{$type}) {
  517. if (defined $results{$test}) {
  518. ($fct) = ($test =~ /^(\w+)\s/);
  519. if ($fct ne $last_fct) {
  520. $last_fct = $fct;
  521. print NEWULP "\n# $fct\n";
  522. }
  523. }
  524. if ($type eq 'normal') {
  525. print NEWULP "Test \"$test\":\n";
  526. } elsif ($type eq 'real') {
  527. print NEWULP "Test \"Real part of: $test\":\n";
  528. } elsif ($type eq 'imag') {
  529. print NEWULP "Test \"Imaginary part of: $test\":\n";
  530. }
  531. foreach $float (@all_floats) {
  532. if (exists $results{$test}{$type}{'ulp'}{$float}) {
  533. print NEWULP "$float: ",
  534. &clean_up_number ($results{$test}{$type}{'ulp'}{$float}),
  535. "\n";
  536. }
  537. if (exists $results{$test}{$type}{'fail'}{$float}) {
  538. print NEWULP "$float: fail\n";
  539. }
  540. }
  541. }
  542. }
  543. }
  544. print NEWULP "\n# Maximal error of functions:\n";
  545. foreach $fct (sort keys %results) {
  546. next if ($results{$fct}{'kind'} ne 'fct');
  547. foreach $type ('real', 'imag', 'normal') {
  548. if (exists $results{$fct}{$type}) {
  549. if ($type eq 'normal') {
  550. print NEWULP "Function: \"$fct\":\n";
  551. } elsif ($type eq 'real') {
  552. print NEWULP "Function: Real part of \"$fct\":\n";
  553. } elsif ($type eq 'imag') {
  554. print NEWULP "Function: Imaginary part of \"$fct\":\n";
  555. }
  556. foreach $float (@all_floats) {
  557. if (exists $results{$fct}{$type}{'ulp'}{$float}) {
  558. print NEWULP "$float: ",
  559. &clean_up_number ($results{$fct}{$type}{'ulp'}{$float}),
  560. "\n";
  561. }
  562. if (exists $results{$fct}{$type}{'fail'}{$float}) {
  563. print NEWULP "$float: fail\n";
  564. }
  565. }
  566. print NEWULP "\n";
  567. }
  568. }
  569. }
  570. print NEWULP "# end of automatic generation\n";
  571. close NEWULP;
  572. }
  573. sub get_ulps {
  574. my ($test, $type, $float) = @_;
  575. if ($type eq 'complex') {
  576. my ($res);
  577. # Return 0 instead of BUILD_COMPLEX (0,0)
  578. if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
  579. !exists $results{$test}{'imag'}{'ulp'}{$float}) {
  580. return "0";
  581. }
  582. $res = 'BUILD_COMPLEX (';
  583. $res .= (exists $results{$test}{'real'}{'ulp'}{$float}
  584. ? $results{$test}{'real'}{'ulp'}{$float} : "0");
  585. $res .= ', ';
  586. $res .= (exists $results{$test}{'imag'}{'ulp'}{$float}
  587. ? $results{$test}{'imag'}{'ulp'}{$float} : "0");
  588. $res .= ')';
  589. return $res;
  590. }
  591. return (exists $results{$test}{'normal'}{'ulp'}{$float}
  592. ? $results{$test}{'normal'}{'ulp'}{$float} : "0");
  593. }
  594. sub get_failure {
  595. my ($test, $type, $float) = @_;
  596. if ($type eq 'complex') {
  597. # return x,y
  598. my ($res);
  599. # Return 0 instead of BUILD_COMPLEX_INT (0,0)
  600. if (!exists $results{$test}{'real'}{'ulp'}{$float} &&
  601. !exists $results{$test}{'imag'}{'ulp'}{$float}) {
  602. return "0";
  603. }
  604. $res = 'BUILD_COMPLEX_INT (';
  605. $res .= (exists $results{$test}{'real'}{'fail'}{$float}
  606. ? $results{$test}{'real'}{'fail'}{$float} : "0");
  607. $res .= ', ';
  608. $res .= (exists $results{$test}{'imag'}{'fail'}{$float}
  609. ? $results{$test}{'imag'}{'fail'}{$float} : "0");
  610. $res .= ')';
  611. return $res;
  612. }
  613. return (exists $results{$test}{'normal'}{'fail'}{$float}
  614. ? $results{$test}{'normal'}{'fail'}{$float} : "0");
  615. }
  616. # Output the defines for a single test
  617. sub output_test {
  618. my ($file, $test, $name) = @_;
  619. my ($ldouble, $double, $float, $ildouble, $idouble, $ifloat);
  620. my ($type);
  621. # Do we have ulps/failures?
  622. if (!exists $results{$test}{'type'}) {
  623. return;
  624. }
  625. $type = $results{$test}{'type'};
  626. if (exists $results{$test}{'has_ulps'}) {
  627. # XXX use all_floats (change order!)
  628. $ldouble = &get_ulps ($test, $type, "ldouble");
  629. $double = &get_ulps ($test, $type, "double");
  630. $float = &get_ulps ($test, $type, "float");
  631. $ildouble = &get_ulps ($test, $type, "ildouble");
  632. $idouble = &get_ulps ($test, $type, "idouble");
  633. $ifloat = &get_ulps ($test, $type, "ifloat");
  634. print $file "#define DELTA$name CHOOSE($ldouble, $double, $float, $ildouble, $idouble, $ifloat)\t/* $test */\n";
  635. }
  636. if (exists $results{$test}{'has_fails'}) {
  637. $ldouble = &get_failure ($test, "ldouble");
  638. $double = &get_failure ($test, "double");
  639. $float = &get_failure ($test, "float");
  640. $ildouble = &get_failure ($test, "ildouble");
  641. $idouble = &get_failure ($test, "idouble");
  642. $ifloat = &get_failure ($test, "ifloat");
  643. print $file "#define FAIL$name CHOOSE($ldouble, $double, $float $ildouble, $idouble, $ifloat)\t/* $test */\n";
  644. }
  645. }
  646. # Print include file
  647. sub output_ulps {
  648. my ($file, $ulps_filename) = @_;
  649. my ($i, $fct);
  650. open ULP, ">$file" or die ("Can't open $file: $!");
  651. print ULP "/* This file is automatically generated\n";
  652. print ULP " from $ulps_filename with gen-libm-test.pl.\n";
  653. print ULP " Don't change it - change instead the master files. */\n\n";
  654. print ULP "\n/* Maximal error of functions. */\n";
  655. foreach $fct (@functions) {
  656. output_test (\*ULP, $fct, $fct);
  657. }
  658. print ULP "\n/* Error of single function calls. */\n";
  659. for ($i = 0; $i < $count; $i++) {
  660. output_test (\*ULP, $tests[$i], $i);
  661. }
  662. close ULP;
  663. }