libm-test-support.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /* Support code for testing libm functions (compiled once per type).
  2. Copyright (C) 1997-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* Part of testsuite for libm.
  16. libm-test-support.c contains functions shared by tests of different
  17. libm functions and types; it is compiled once per type.
  18. libm-test-driver.c defines the main function, and various variables
  19. that are used to configure the code in libm-test-support.c for
  20. different types and for variants such as testing inline functions.
  21. The tests of individual functions are in .inc files processed by
  22. gen-libm-test.py, with the resulting files included together with
  23. libm-test-driver.c.
  24. The per-type headers included both before libm-test-support.c and
  25. for the tests of individual functions must define the following
  26. macros:
  27. FUNC(function): Convert general function name (like cos) to name
  28. with correct suffix (e.g. cosl or cosf).
  29. FLOAT: Floating-point type to test.
  30. BUILD_COMPLEX(real, imag): Create a complex number by calling a
  31. macro such as CMPLX.
  32. PREFIX: The prefix for <float.h> macros for the type (e.g. LDBL,
  33. DBL, or FLT).
  34. TYPE_STR: The name of the type as used in ulps files, as a string.
  35. ULP_IDX, ULP_I_IDX: The array indexes for ulps values for this
  36. function.
  37. LIT: Append the correct suffix to a literal.
  38. LITM: Append the correct suffix to an M_* macro name.
  39. FTOSTR: A function similar in type to strfromf which converts a
  40. FLOAT to a string.
  41. snan_value_MACRO: The macro such as SNAN for a signaling NaN for
  42. the type.
  43. */
  44. /* Parameter handling is primitive in the moment:
  45. --verbose=[0..3] for different levels of output:
  46. 0: only error count
  47. 1: basic report on failed tests (default)
  48. 2: full report on all tests
  49. -v for full output (equals --verbose=3)
  50. -u for generation of an ULPs file
  51. */
  52. /* "Philosophy":
  53. This suite tests some aspects of the correct implementation of
  54. mathematical functions in libm. Some simple, specific parameters
  55. are tested for correctness but there's no exhaustive
  56. testing. Handling of specific inputs (e.g. infinity, not-a-number)
  57. is also tested. Correct handling of exceptions is checked
  58. against. These implemented tests should check all cases that are
  59. specified in ISO C99.
  60. NaN values: The payload of NaNs is set in inputs for functions
  61. where it is significant, and is examined in the outputs of some
  62. functions.
  63. Inline functions: Inlining functions should give an improvement in
  64. speed - but not in precission. The inlined functions return
  65. reasonable values for a reasonable range of input values. The
  66. result is not necessarily correct for all values and exceptions are
  67. not correctly raised in all cases. Problematic input and return
  68. values are infinity, not-a-number and minus zero. This suite
  69. therefore does not check these specific inputs and the exception
  70. handling for inlined mathematical functions - just the "reasonable"
  71. values are checked.
  72. Beware: The tests might fail for any of the following reasons:
  73. - Tests are wrong
  74. - Functions are wrong
  75. - Floating Point Unit not working properly
  76. - Compiler has errors
  77. With e.g. gcc 2.7.2.2 the test for cexp fails because of a compiler error.
  78. To Do: All parameter should be numbers that can be represented as
  79. exact floating point values. Currently some values cannot be
  80. represented exactly and therefore the result is not the expected
  81. result. For this we will use 36 digits so that numbers can be
  82. represented exactly. */
  83. #include "libm-test-support.h"
  84. #include <argp.h>
  85. #include <errno.h>
  86. #include <string.h>
  87. /* This header defines func_ulps, func_real_ulps and func_imag_ulps
  88. arrays. */
  89. #include "libm-test-ulps.h"
  90. /* Maximum character buffer to store a stringitized FLOAT value. */
  91. #define FSTR_MAX (128)
  92. #define ulps_file_name "ULPs" /* Name of the ULPs file. */
  93. static FILE *ulps_file; /* File to document difference. */
  94. static int output_ulps; /* Should ulps printed? */
  95. static char *output_dir; /* Directory where generated files will be written. */
  96. static int noErrors; /* number of errors */
  97. static int noTests; /* number of tests (without testing exceptions) */
  98. static int noExcTests; /* number of tests for exception flags */
  99. static int noErrnoTests;/* number of tests for errno values */
  100. static int verbose;
  101. static int output_max_error; /* Should the maximal errors printed? */
  102. static int output_points; /* Should the single function results printed? */
  103. static int ignore_max_ulp; /* Should we ignore max_ulp? */
  104. static int test_ibm128; /* Is argument or result IBM long double? */
  105. static FLOAT max_error, real_max_error, imag_max_error;
  106. static FLOAT prev_max_error, prev_real_max_error, prev_imag_max_error;
  107. static FLOAT max_valid_error;
  108. /* Sufficient numbers of digits to represent any floating-point value
  109. unambiguously (for any choice of the number of bits in the first
  110. hex digit, in the case of TYPE_HEX_DIG). When used with printf
  111. formats where the precision counts only digits after the point, 1
  112. is subtracted from these values. */
  113. #define TYPE_DECIMAL_DIG __CONCATX (PREFIX, _DECIMAL_DIG)
  114. #define TYPE_HEX_DIG ((MANT_DIG + 6) / 4)
  115. /* Converts VALUE (a floating-point number) to string and writes it to DEST.
  116. PRECISION specifies the number of fractional digits that should be printed.
  117. CONVERSION is the conversion specifier, such as in printf, e.g. 'f' or 'a'.
  118. The output is prepended with an empty space if VALUE is non-negative. */
  119. static void
  120. fmt_ftostr (char *dest, size_t size, int precision, const char *conversion,
  121. FLOAT value)
  122. {
  123. char format[64];
  124. char *ptr_format;
  125. int ret;
  126. /* Generate the format string. */
  127. ptr_format = stpcpy (format, "%.");
  128. ret = sprintf (ptr_format, "%d", precision);
  129. ptr_format += ret;
  130. ptr_format = stpcpy (ptr_format, conversion);
  131. /* Add a space to the beginning of the output string, if the floating-point
  132. number is non-negative. This mimics the behavior of the space (' ') flag
  133. in snprintf, which is not available on strfrom. */
  134. if (! signbit (value))
  135. {
  136. *dest = ' ';
  137. dest++;
  138. size--;
  139. }
  140. /* Call the float to string conversion function, e.g.: strfromd. */
  141. FTOSTR (dest, size, format, value);
  142. }
  143. /* Compare KEY (a string, with the name of a function) with ULP (a
  144. pointer to a struct ulp_data structure), returning a value less
  145. than, equal to or greater than zero for use in bsearch. */
  146. static int
  147. compare_ulp_data (const void *key, const void *ulp)
  148. {
  149. const char *keystr = key;
  150. const struct ulp_data *ulpdat = ulp;
  151. return strcmp (keystr, ulpdat->name);
  152. }
  153. static const int ulp_i_idx = ULP_I_IDX;
  154. static const int ulp_idx = ULP_IDX;
  155. /* Return the ulps for NAME in array DATA with NMEMB elements, or 0 if
  156. no ulps listed. */
  157. static FLOAT
  158. find_ulps (const char *name, const struct ulp_data *data, size_t nmemb)
  159. {
  160. const struct ulp_data *entry = bsearch (name, data, nmemb, sizeof (*data),
  161. compare_ulp_data);
  162. if (entry == NULL)
  163. return 0;
  164. else
  165. return entry->max_ulp[(flag_test_inline ? ulp_i_idx : ulp_idx)];
  166. }
  167. void
  168. init_max_error (const char *name, int exact, int testing_ibm128)
  169. {
  170. max_error = 0;
  171. real_max_error = 0;
  172. imag_max_error = 0;
  173. test_ibm128 = testing_ibm128;
  174. prev_max_error = find_ulps (name, func_ulps,
  175. sizeof (func_ulps) / sizeof (func_ulps[0]));
  176. prev_real_max_error = find_ulps (name, func_real_ulps,
  177. (sizeof (func_real_ulps)
  178. / sizeof (func_real_ulps[0])));
  179. prev_imag_max_error = find_ulps (name, func_imag_ulps,
  180. (sizeof (func_imag_ulps)
  181. / sizeof (func_imag_ulps[0])));
  182. if (testing_ibm128)
  183. /* The documented accuracy of IBM long double division is 3ulp
  184. (see libgcc/config/rs6000/ibm-ldouble-format), so do not
  185. require better accuracy for libm functions that are exactly
  186. defined for other formats. */
  187. max_valid_error = exact ? 3 : 16;
  188. else
  189. max_valid_error = exact ? 0 : 9;
  190. prev_max_error = (prev_max_error <= max_valid_error
  191. ? prev_max_error
  192. : max_valid_error);
  193. prev_real_max_error = (prev_real_max_error <= max_valid_error
  194. ? prev_real_max_error
  195. : max_valid_error);
  196. prev_imag_max_error = (prev_imag_max_error <= max_valid_error
  197. ? prev_imag_max_error
  198. : max_valid_error);
  199. feclearexcept (FE_ALL_EXCEPT);
  200. errno = 0;
  201. }
  202. static void
  203. set_max_error (FLOAT current, FLOAT *curr_max_error)
  204. {
  205. if (current > *curr_max_error && current <= max_valid_error)
  206. *curr_max_error = current;
  207. }
  208. /* Print a FLOAT. */
  209. static void
  210. print_float (FLOAT f)
  211. {
  212. /* As printf doesn't differ between a sNaN and a qNaN, do this manually. */
  213. if (issignaling (f))
  214. printf ("sNaN\n");
  215. else if (isnan (f))
  216. printf ("qNaN\n");
  217. else
  218. {
  219. char fstrn[FSTR_MAX], fstrx[FSTR_MAX];
  220. fmt_ftostr (fstrn, FSTR_MAX, TYPE_DECIMAL_DIG - 1, "e", f);
  221. fmt_ftostr (fstrx, FSTR_MAX, TYPE_HEX_DIG - 1, "a", f);
  222. printf ("%s %s\n", fstrn, fstrx);
  223. }
  224. }
  225. /* Should the message print to screen? This depends on the verbose flag,
  226. and the test status. */
  227. static int
  228. print_screen (int ok)
  229. {
  230. if (output_points
  231. && (verbose > 1
  232. || (verbose == 1 && ok == 0)))
  233. return 1;
  234. return 0;
  235. }
  236. /* Should the message print to screen? This depends on the verbose flag,
  237. and the test status. */
  238. static int
  239. print_screen_max_error (int ok)
  240. {
  241. if (output_max_error
  242. && (verbose > 1
  243. || ((verbose == 1) && (ok == 0))))
  244. return 1;
  245. return 0;
  246. }
  247. /* Update statistic counters. */
  248. static void
  249. update_stats (int ok)
  250. {
  251. ++noTests;
  252. if (!ok)
  253. ++noErrors;
  254. }
  255. static void
  256. print_function_ulps (const char *function_name, FLOAT ulp)
  257. {
  258. if (output_ulps)
  259. {
  260. char ustrn[FSTR_MAX];
  261. FTOSTR (ustrn, FSTR_MAX, "%.0f", FUNC (ceil) (ulp));
  262. fprintf (ulps_file, "Function: \"%s\":\n", function_name);
  263. fprintf (ulps_file, "%s: %s\n", qtype_str, ustrn);
  264. }
  265. }
  266. static void
  267. print_complex_function_ulps (const char *function_name, FLOAT real_ulp,
  268. FLOAT imag_ulp)
  269. {
  270. if (output_ulps)
  271. {
  272. char fstrn[FSTR_MAX];
  273. if (real_ulp != 0.0)
  274. {
  275. FTOSTR (fstrn, FSTR_MAX, "%.0f", FUNC (ceil) (real_ulp));
  276. fprintf (ulps_file, "Function: Real part of \"%s\":\n", function_name);
  277. fprintf (ulps_file, "%s: %s\n", qtype_str, fstrn);
  278. }
  279. if (imag_ulp != 0.0)
  280. {
  281. FTOSTR (fstrn, FSTR_MAX, "%.0f", FUNC (ceil) (imag_ulp));
  282. fprintf (ulps_file, "Function: Imaginary part of \"%s\":\n", function_name);
  283. fprintf (ulps_file, "%s: %s\n", qtype_str, fstrn);
  284. }
  285. }
  286. }
  287. /* Test if Floating-Point stack hasn't changed */
  288. static void
  289. fpstack_test (const char *test_name)
  290. {
  291. #if defined (__i386__) || defined (__x86_64__)
  292. static int old_stack;
  293. int sw;
  294. asm ("fnstsw" : "=a" (sw));
  295. sw >>= 11;
  296. sw &= 7;
  297. if (sw != old_stack)
  298. {
  299. printf ("FP-Stack wrong after test %s (%d, should be %d)\n",
  300. test_name, sw, old_stack);
  301. ++noErrors;
  302. old_stack = sw;
  303. }
  304. #endif
  305. }
  306. void
  307. print_max_error (const char *func_name)
  308. {
  309. int ok = 0;
  310. if (max_error == 0.0 || (max_error <= prev_max_error && !ignore_max_ulp))
  311. {
  312. ok = 1;
  313. }
  314. if (!ok)
  315. print_function_ulps (func_name, max_error);
  316. if (print_screen_max_error (ok))
  317. {
  318. char mestr[FSTR_MAX], pmestr[FSTR_MAX];
  319. FTOSTR (mestr, FSTR_MAX, "%.0f", FUNC (ceil) (max_error));
  320. FTOSTR (pmestr, FSTR_MAX, "%.0f", FUNC (ceil) (prev_max_error));
  321. printf ("Maximal error of `%s'\n", func_name);
  322. printf (" is : %s ulp\n", mestr);
  323. printf (" accepted: %s ulp\n", pmestr);
  324. }
  325. update_stats (ok);
  326. }
  327. void
  328. print_complex_max_error (const char *func_name)
  329. {
  330. int real_ok = 0, imag_ok = 0, ok;
  331. if (real_max_error == 0
  332. || (real_max_error <= prev_real_max_error && !ignore_max_ulp))
  333. {
  334. real_ok = 1;
  335. }
  336. if (imag_max_error == 0
  337. || (imag_max_error <= prev_imag_max_error && !ignore_max_ulp))
  338. {
  339. imag_ok = 1;
  340. }
  341. ok = real_ok && imag_ok;
  342. if (!ok)
  343. print_complex_function_ulps (func_name,
  344. real_ok ? 0 : real_max_error,
  345. imag_ok ? 0 : imag_max_error);
  346. if (print_screen_max_error (ok))
  347. {
  348. char rmestr[FSTR_MAX], prmestr[FSTR_MAX];
  349. char imestr[FSTR_MAX], pimestr[FSTR_MAX];
  350. FTOSTR (rmestr, FSTR_MAX, "%.0f", FUNC (ceil) (real_max_error));
  351. FTOSTR (prmestr, FSTR_MAX, "%.0f", FUNC (ceil) (prev_real_max_error));
  352. FTOSTR (imestr, FSTR_MAX, "%.0f", FUNC (ceil) (imag_max_error));
  353. FTOSTR (pimestr, FSTR_MAX, "%.0f", FUNC (ceil) (prev_imag_max_error));
  354. printf ("Maximal error of real part of: %s\n", func_name);
  355. printf (" is : %s ulp\n", rmestr);
  356. printf (" accepted: %s ulp\n", prmestr);
  357. printf ("Maximal error of imaginary part of: %s\n", func_name);
  358. printf (" is : %s ulp\n", imestr);
  359. printf (" accepted: %s ulp\n", pimestr);
  360. }
  361. update_stats (ok);
  362. }
  363. #if FE_ALL_EXCEPT
  364. /* Test whether a given exception was raised. */
  365. static void
  366. test_single_exception (const char *test_name,
  367. int exception,
  368. int exc_flag,
  369. int fe_flag,
  370. const char *flag_name)
  371. {
  372. int ok = 1;
  373. if (exception & exc_flag)
  374. {
  375. if (fetestexcept (fe_flag))
  376. {
  377. if (print_screen (1))
  378. printf ("Pass: %s: Exception \"%s\" set\n", test_name, flag_name);
  379. }
  380. else
  381. {
  382. ok = 0;
  383. if (print_screen (0))
  384. printf ("Failure: %s: Exception \"%s\" not set\n",
  385. test_name, flag_name);
  386. }
  387. }
  388. else
  389. {
  390. if (fetestexcept (fe_flag))
  391. {
  392. ok = 0;
  393. if (print_screen (0))
  394. printf ("Failure: %s: Exception \"%s\" set\n",
  395. test_name, flag_name);
  396. }
  397. else
  398. {
  399. if (print_screen (1))
  400. printf ("%s: Exception \"%s\" not set\n", test_name,
  401. flag_name);
  402. }
  403. }
  404. if (!ok)
  405. ++noErrors;
  406. }
  407. #endif
  408. /* Test whether exceptions given by EXCEPTION are raised. Ignore thereby
  409. allowed but not required exceptions.
  410. */
  411. static void
  412. test_exceptions (const char *test_name, int exception)
  413. {
  414. if (flag_test_exceptions && EXCEPTION_TESTS (FLOAT))
  415. {
  416. ++noExcTests;
  417. #ifdef FE_DIVBYZERO
  418. if ((exception & DIVIDE_BY_ZERO_EXCEPTION_OK) == 0)
  419. test_single_exception (test_name, exception,
  420. DIVIDE_BY_ZERO_EXCEPTION, FE_DIVBYZERO,
  421. "Divide by zero");
  422. #endif
  423. #ifdef FE_INVALID
  424. if ((exception & INVALID_EXCEPTION_OK) == 0)
  425. test_single_exception (test_name, exception,
  426. INVALID_EXCEPTION, FE_INVALID,
  427. "Invalid operation");
  428. #endif
  429. #ifdef FE_OVERFLOW
  430. if ((exception & OVERFLOW_EXCEPTION_OK) == 0)
  431. test_single_exception (test_name, exception, OVERFLOW_EXCEPTION,
  432. FE_OVERFLOW, "Overflow");
  433. #endif
  434. /* Spurious "underflow" and "inexact" exceptions are always
  435. allowed for IBM long double, in line with the underlying
  436. arithmetic. */
  437. #ifdef FE_UNDERFLOW
  438. if ((exception & UNDERFLOW_EXCEPTION_OK) == 0
  439. && !(test_ibm128
  440. && (exception & UNDERFLOW_EXCEPTION) == 0))
  441. test_single_exception (test_name, exception, UNDERFLOW_EXCEPTION,
  442. FE_UNDERFLOW, "Underflow");
  443. #endif
  444. #ifdef FE_INEXACT
  445. if ((exception & (INEXACT_EXCEPTION | NO_INEXACT_EXCEPTION)) != 0
  446. && !(test_ibm128
  447. && (exception & NO_INEXACT_EXCEPTION) != 0))
  448. test_single_exception (test_name, exception, INEXACT_EXCEPTION,
  449. FE_INEXACT, "Inexact");
  450. #endif
  451. }
  452. feclearexcept (FE_ALL_EXCEPT);
  453. }
  454. /* Test whether errno for TEST_NAME, set to ERRNO_VALUE, has value
  455. EXPECTED_VALUE (description EXPECTED_NAME). */
  456. static void
  457. test_single_errno (const char *test_name, int errno_value,
  458. int expected_value, const char *expected_name)
  459. {
  460. if (errno_value == expected_value)
  461. {
  462. if (print_screen (1))
  463. printf ("Pass: %s: errno set to %d (%s)\n", test_name, errno_value,
  464. expected_name);
  465. }
  466. else
  467. {
  468. ++noErrors;
  469. if (print_screen (0))
  470. printf ("Failure: %s: errno set to %d, expected %d (%s)\n",
  471. test_name, errno_value, expected_value, expected_name);
  472. }
  473. }
  474. /* Test whether errno (value ERRNO_VALUE) has been for TEST_NAME set
  475. as required by EXCEPTIONS. */
  476. static void
  477. test_errno (const char *test_name, int errno_value, int exceptions)
  478. {
  479. if (flag_test_errno)
  480. {
  481. ++noErrnoTests;
  482. if (exceptions & ERRNO_UNCHANGED)
  483. test_single_errno (test_name, errno_value, 0, "unchanged");
  484. if (exceptions & ERRNO_EDOM)
  485. test_single_errno (test_name, errno_value, EDOM, "EDOM");
  486. if (exceptions & ERRNO_ERANGE)
  487. test_single_errno (test_name, errno_value, ERANGE, "ERANGE");
  488. }
  489. }
  490. /* Returns the number of ulps that GIVEN is away from EXPECTED. */
  491. #define ULPDIFF(given, expected) \
  492. (FUNC(fabs) ((given) - (expected)) / ulp (expected))
  493. /* Returns the size of an ulp for VALUE. */
  494. static FLOAT
  495. ulp (FLOAT value)
  496. {
  497. FLOAT ulp;
  498. switch (fpclassify (value))
  499. {
  500. case FP_ZERO:
  501. /* We compute the distance to the next FP which is the same as the
  502. value of the smallest subnormal number. Previously we used
  503. 2^-(MANT_DIG - 1) which is too large a value to be useful. Note that we
  504. can't use ilogb(0), since that isn't a valid thing to do. As a point
  505. of comparison Java's ulp returns the next normal value e.g.
  506. 2^(1 - MAX_EXP) for ulp(0), but that is not what we want for
  507. glibc. */
  508. /* Fall through... */
  509. case FP_SUBNORMAL:
  510. /* The next closest subnormal value is a constant distance away. */
  511. ulp = FUNC(ldexp) (1.0, MIN_EXP - MANT_DIG);
  512. break;
  513. case FP_NORMAL:
  514. ulp = FUNC(ldexp) (1.0, FUNC(ilogb) (value) - MANT_DIG + 1);
  515. break;
  516. default:
  517. /* It should never happen. */
  518. abort ();
  519. break;
  520. }
  521. return ulp;
  522. }
  523. static void
  524. check_float_internal (const char *test_name, FLOAT computed, FLOAT expected,
  525. int exceptions,
  526. FLOAT *curr_max_error, FLOAT max_ulp)
  527. {
  528. int ok = 0;
  529. int print_diff = 0;
  530. FLOAT diff = 0;
  531. FLOAT ulps = 0;
  532. int errno_value = errno;
  533. test_exceptions (test_name, exceptions);
  534. test_errno (test_name, errno_value, exceptions);
  535. if (exceptions & IGNORE_RESULT)
  536. goto out;
  537. if (issignaling (computed) && issignaling (expected))
  538. {
  539. if ((exceptions & TEST_NAN_SIGN) != 0
  540. && signbit (computed) != signbit (expected))
  541. {
  542. ok = 0;
  543. printf ("signaling NaN has wrong sign.\n");
  544. }
  545. else if ((exceptions & TEST_NAN_PAYLOAD) != 0
  546. && (FUNC (getpayload) (&computed)
  547. != FUNC (getpayload) (&expected)))
  548. {
  549. ok = 0;
  550. printf ("signaling NaN has wrong payload.\n");
  551. }
  552. else
  553. ok = 1;
  554. }
  555. else if (issignaling (computed) || issignaling (expected))
  556. ok = 0;
  557. else if (isnan (computed) && isnan (expected))
  558. {
  559. if ((exceptions & TEST_NAN_SIGN) != 0
  560. && signbit (computed) != signbit (expected))
  561. {
  562. ok = 0;
  563. printf ("quiet NaN has wrong sign.\n");
  564. }
  565. else if ((exceptions & TEST_NAN_PAYLOAD) != 0
  566. && (FUNC (getpayload) (&computed)
  567. != FUNC (getpayload) (&expected)))
  568. {
  569. ok = 0;
  570. printf ("quiet NaN has wrong payload.\n");
  571. }
  572. else
  573. ok = 1;
  574. }
  575. else if (isinf (computed) && isinf (expected))
  576. {
  577. /* Test for sign of infinities. */
  578. if ((exceptions & IGNORE_ZERO_INF_SIGN) == 0
  579. && signbit (computed) != signbit (expected))
  580. {
  581. ok = 0;
  582. printf ("infinity has wrong sign.\n");
  583. }
  584. else
  585. ok = 1;
  586. }
  587. /* Don't calculate ULPs for infinities or any kind of NaNs. */
  588. else if (isinf (computed) || isnan (computed)
  589. || isinf (expected) || isnan (expected))
  590. ok = 0;
  591. else
  592. {
  593. diff = FUNC(fabs) (computed - expected);
  594. ulps = ULPDIFF (computed, expected);
  595. set_max_error (ulps, curr_max_error);
  596. print_diff = 1;
  597. if ((exceptions & IGNORE_ZERO_INF_SIGN) == 0
  598. && computed == 0.0 && expected == 0.0
  599. && signbit(computed) != signbit (expected))
  600. ok = 0;
  601. else if (ulps <= max_ulp && !ignore_max_ulp)
  602. ok = 1;
  603. else
  604. ok = 0;
  605. }
  606. if (print_screen (ok))
  607. {
  608. if (!ok)
  609. printf ("Failure: ");
  610. printf ("Test: %s\n", test_name);
  611. printf ("Result:\n");
  612. printf (" is: ");
  613. print_float (computed);
  614. printf (" should be: ");
  615. print_float (expected);
  616. if (print_diff)
  617. {
  618. char dstrn[FSTR_MAX], dstrx[FSTR_MAX];
  619. char ustrn[FSTR_MAX], mustrn[FSTR_MAX];
  620. fmt_ftostr (dstrn, FSTR_MAX, TYPE_DECIMAL_DIG - 1, "e", diff);
  621. fmt_ftostr (dstrx, FSTR_MAX, TYPE_HEX_DIG - 1, "a", diff);
  622. fmt_ftostr (ustrn, FSTR_MAX, 4, "f", ulps);
  623. fmt_ftostr (mustrn, FSTR_MAX, 4, "f", max_ulp);
  624. printf (" difference: %s %s\n", dstrn, dstrx);
  625. printf (" ulp : %s\n", ustrn);
  626. printf (" max.ulp : %s\n", mustrn);
  627. }
  628. }
  629. update_stats (ok);
  630. out:
  631. fpstack_test (test_name);
  632. feclearexcept (FE_ALL_EXCEPT);
  633. errno = 0;
  634. }
  635. void
  636. check_float (const char *test_name, FLOAT computed, FLOAT expected,
  637. int exceptions)
  638. {
  639. check_float_internal (test_name, computed, expected,
  640. exceptions, &max_error, prev_max_error);
  641. }
  642. void
  643. check_complex (const char *test_name, CFLOAT computed,
  644. CFLOAT expected,
  645. int exception)
  646. {
  647. FLOAT part_comp, part_exp;
  648. char *str;
  649. if (asprintf (&str, "Real part of: %s", test_name) == -1)
  650. abort ();
  651. part_comp = __real__ computed;
  652. part_exp = __real__ expected;
  653. check_float_internal (str, part_comp, part_exp,
  654. exception, &real_max_error, prev_real_max_error);
  655. free (str);
  656. if (asprintf (&str, "Imaginary part of: %s", test_name) == -1)
  657. abort ();
  658. part_comp = __imag__ computed;
  659. part_exp = __imag__ expected;
  660. /* Don't check again for exceptions or errno, just pass through the
  661. other relevant flags. */
  662. check_float_internal (str, part_comp, part_exp,
  663. exception & (IGNORE_ZERO_INF_SIGN
  664. | TEST_NAN_SIGN
  665. | IGNORE_RESULT),
  666. &imag_max_error, prev_imag_max_error);
  667. free (str);
  668. }
  669. /* Check that computed and expected values are equal (int values). */
  670. void
  671. check_int (const char *test_name, int computed, int expected,
  672. int exceptions)
  673. {
  674. int ok = 0;
  675. int errno_value = errno;
  676. test_exceptions (test_name, exceptions);
  677. test_errno (test_name, errno_value, exceptions);
  678. if (exceptions & IGNORE_RESULT)
  679. goto out;
  680. noTests++;
  681. if (computed == expected)
  682. ok = 1;
  683. if (print_screen (ok))
  684. {
  685. if (!ok)
  686. printf ("Failure: ");
  687. printf ("Test: %s\n", test_name);
  688. printf ("Result:\n");
  689. printf (" is: %d\n", computed);
  690. printf (" should be: %d\n", expected);
  691. }
  692. update_stats (ok);
  693. out:
  694. fpstack_test (test_name);
  695. feclearexcept (FE_ALL_EXCEPT);
  696. errno = 0;
  697. }
  698. /* Check that computed and expected values are equal (long int values). */
  699. void
  700. check_long (const char *test_name, long int computed, long int expected,
  701. int exceptions)
  702. {
  703. int ok = 0;
  704. int errno_value = errno;
  705. test_exceptions (test_name, exceptions);
  706. test_errno (test_name, errno_value, exceptions);
  707. if (exceptions & IGNORE_RESULT)
  708. goto out;
  709. noTests++;
  710. if (computed == expected)
  711. ok = 1;
  712. if (print_screen (ok))
  713. {
  714. if (!ok)
  715. printf ("Failure: ");
  716. printf ("Test: %s\n", test_name);
  717. printf ("Result:\n");
  718. printf (" is: %ld\n", computed);
  719. printf (" should be: %ld\n", expected);
  720. }
  721. update_stats (ok);
  722. out:
  723. fpstack_test (test_name);
  724. feclearexcept (FE_ALL_EXCEPT);
  725. errno = 0;
  726. }
  727. /* Check that computed value is true/false. */
  728. void
  729. check_bool (const char *test_name, int computed, int expected,
  730. int exceptions)
  731. {
  732. int ok = 0;
  733. int errno_value = errno;
  734. test_exceptions (test_name, exceptions);
  735. test_errno (test_name, errno_value, exceptions);
  736. if (exceptions & IGNORE_RESULT)
  737. goto out;
  738. noTests++;
  739. if ((computed == 0) == (expected == 0))
  740. ok = 1;
  741. if (print_screen (ok))
  742. {
  743. if (!ok)
  744. printf ("Failure: ");
  745. printf ("Test: %s\n", test_name);
  746. printf ("Result:\n");
  747. printf (" is: %d\n", computed);
  748. printf (" should be: %d\n", expected);
  749. }
  750. update_stats (ok);
  751. out:
  752. fpstack_test (test_name);
  753. feclearexcept (FE_ALL_EXCEPT);
  754. errno = 0;
  755. }
  756. /* check that computed and expected values are equal (long int values) */
  757. void
  758. check_longlong (const char *test_name, long long int computed,
  759. long long int expected,
  760. int exceptions)
  761. {
  762. int ok = 0;
  763. int errno_value = errno;
  764. test_exceptions (test_name, exceptions);
  765. test_errno (test_name, errno_value, exceptions);
  766. if (exceptions & IGNORE_RESULT)
  767. goto out;
  768. noTests++;
  769. if (computed == expected)
  770. ok = 1;
  771. if (print_screen (ok))
  772. {
  773. if (!ok)
  774. printf ("Failure:");
  775. printf ("Test: %s\n", test_name);
  776. printf ("Result:\n");
  777. printf (" is: %lld\n", computed);
  778. printf (" should be: %lld\n", expected);
  779. }
  780. update_stats (ok);
  781. out:
  782. fpstack_test (test_name);
  783. feclearexcept (FE_ALL_EXCEPT);
  784. errno = 0;
  785. }
  786. /* Check that computed and expected values are equal (intmax_t values). */
  787. void
  788. check_intmax_t (const char *test_name, intmax_t computed,
  789. intmax_t expected, int exceptions)
  790. {
  791. int ok = 0;
  792. int errno_value = errno;
  793. test_exceptions (test_name, exceptions);
  794. test_errno (test_name, errno_value, exceptions);
  795. if (exceptions & IGNORE_RESULT)
  796. goto out;
  797. noTests++;
  798. if (computed == expected)
  799. ok = 1;
  800. if (print_screen (ok))
  801. {
  802. if (!ok)
  803. printf ("Failure:");
  804. printf ("Test: %s\n", test_name);
  805. printf ("Result:\n");
  806. printf (" is: %jd\n", computed);
  807. printf (" should be: %jd\n", expected);
  808. }
  809. update_stats (ok);
  810. out:
  811. fpstack_test (test_name);
  812. feclearexcept (FE_ALL_EXCEPT);
  813. errno = 0;
  814. }
  815. /* Check that computed and expected values are equal (uintmax_t values). */
  816. void
  817. check_uintmax_t (const char *test_name, uintmax_t computed,
  818. uintmax_t expected, int exceptions)
  819. {
  820. int ok = 0;
  821. int errno_value = errno;
  822. test_exceptions (test_name, exceptions);
  823. test_errno (test_name, errno_value, exceptions);
  824. if (exceptions & IGNORE_RESULT)
  825. goto out;
  826. noTests++;
  827. if (computed == expected)
  828. ok = 1;
  829. if (print_screen (ok))
  830. {
  831. if (!ok)
  832. printf ("Failure:");
  833. printf ("Test: %s\n", test_name);
  834. printf ("Result:\n");
  835. printf (" is: %ju\n", computed);
  836. printf (" should be: %ju\n", expected);
  837. }
  838. update_stats (ok);
  839. out:
  840. fpstack_test (test_name);
  841. feclearexcept (FE_ALL_EXCEPT);
  842. errno = 0;
  843. }
  844. /* Return whether a test with flags EXCEPTIONS should be run. */
  845. int
  846. enable_test (int exceptions)
  847. {
  848. if (exceptions & XFAIL_TEST)
  849. return 0;
  850. if (flag_test_inline && (exceptions & NO_TEST_INLINE))
  851. return 0;
  852. if (flag_test_finite && (exceptions & NON_FINITE) != 0)
  853. return 0;
  854. if ((!SNAN_TESTS (FLOAT) || !snan_tests_arg)
  855. && (exceptions & TEST_SNAN) != 0)
  856. return 0;
  857. if (flag_test_mathvec && (exceptions & NO_TEST_MATHVEC) != 0)
  858. return 0;
  859. return 1;
  860. }
  861. static void
  862. initialize (void)
  863. {
  864. fpstack_test ("start *init*");
  865. /* Clear all exceptions. From now on we must not get random exceptions. */
  866. feclearexcept (FE_ALL_EXCEPT);
  867. errno = 0;
  868. /* Test to make sure we start correctly. */
  869. fpstack_test ("end *init*");
  870. }
  871. /* Definitions of arguments for argp functions. */
  872. static const struct argp_option options[] =
  873. {
  874. { "verbose", 'v', "NUMBER", 0, "Level of verbosity (0..3)"},
  875. { "ulps-file", 'u', NULL, 0, "Output ulps to file ULPs"},
  876. { "no-max-error", 'f', NULL, 0,
  877. "Don't output maximal errors of functions"},
  878. { "no-points", 'p', NULL, 0,
  879. "Don't output results of functions invocations"},
  880. { "ignore-max-ulp", 'i', "yes/no", 0,
  881. "Ignore given maximal errors"},
  882. { "output-dir", 'o', "DIR", 0,
  883. "Directory where generated files will be placed"},
  884. { NULL, 0, NULL, 0, NULL }
  885. };
  886. /* Prototype for option handler. */
  887. static error_t parse_opt (int key, char *arg, struct argp_state *state);
  888. /* Data structure to communicate with argp functions. */
  889. static struct argp argp =
  890. {
  891. options, parse_opt, NULL, doc,
  892. };
  893. /* Handle program arguments. */
  894. static error_t
  895. parse_opt (int key, char *arg, struct argp_state *state)
  896. {
  897. switch (key)
  898. {
  899. case 'f':
  900. output_max_error = 0;
  901. break;
  902. case 'i':
  903. if (strcmp (arg, "yes") == 0)
  904. ignore_max_ulp = 1;
  905. else if (strcmp (arg, "no") == 0)
  906. ignore_max_ulp = 0;
  907. break;
  908. case 'o':
  909. output_dir = (char *) malloc (strlen (arg) + 1);
  910. if (output_dir != NULL)
  911. strcpy (output_dir, arg);
  912. else
  913. return errno;
  914. break;
  915. case 'p':
  916. output_points = 0;
  917. break;
  918. case 'u':
  919. output_ulps = 1;
  920. break;
  921. case 'v':
  922. if (optarg)
  923. verbose = (unsigned int) strtoul (optarg, NULL, 0);
  924. else
  925. verbose = 3;
  926. break;
  927. default:
  928. return ARGP_ERR_UNKNOWN;
  929. }
  930. return 0;
  931. }
  932. /* Verify that our ulp () implementation is behaving as expected
  933. or abort. */
  934. static void
  935. check_ulp (void)
  936. {
  937. FLOAT ulps, ulpx, value;
  938. int i;
  939. /* Check ulp of zero is a subnormal value... */
  940. ulps = ulp (0x0.0p0);
  941. if (fpclassify (ulps) != FP_SUBNORMAL)
  942. {
  943. fprintf (stderr, "ulp (0x0.0p0) is not FP_SUBNORMAL!\n");
  944. exit (EXIT_FAILURE);
  945. }
  946. /* Check that the ulp of one is a normal value... */
  947. ulps = ulp (LIT(1.0));
  948. if (fpclassify (ulps) != FP_NORMAL)
  949. {
  950. fprintf (stderr, "ulp (1.0L) is not FP_NORMAL\n");
  951. exit (EXIT_FAILURE);
  952. }
  953. /* Compute the next subnormal value using nextafter to validate ulp.
  954. We allow +/- 1 ulp around the represented value. */
  955. value = FUNC(nextafter) (0, 1);
  956. ulps = ULPDIFF (value, 0);
  957. ulpx = ulp (LIT(1.0));
  958. if (ulps < (LIT(1.0) - ulpx) || ulps > (LIT(1.0) + ulpx))
  959. {
  960. fprintf (stderr, "Value outside of 1 +/- 1ulp.\n");
  961. exit (EXIT_FAILURE);
  962. }
  963. /* Compute the nearest representable number from 10 towards 20.
  964. The result is 10 + 1ulp. We use this to check the ulp function.
  965. We allow +/- 1 ulp around the represented value. */
  966. value = FUNC(nextafter) (10, 20);
  967. ulps = ULPDIFF (value, 10);
  968. ulpx = ulp (LIT(1.0));
  969. if (ulps < (LIT(1.0) - ulpx) || ulps > (LIT(1.0) + ulpx))
  970. {
  971. fprintf (stderr, "Value outside of 1 +/- 1ulp.\n");
  972. exit (EXIT_FAILURE);
  973. }
  974. /* This gives one more ulp. */
  975. value = FUNC(nextafter) (value, 20);
  976. ulps = ULPDIFF (value, 10);
  977. ulpx = ulp (LIT(2.0));
  978. if (ulps < (LIT(2.0) - ulpx) || ulps > (LIT(2.0) + ulpx))
  979. {
  980. fprintf (stderr, "Value outside of 2 +/- 1ulp.\n");
  981. exit (EXIT_FAILURE);
  982. }
  983. /* And now calculate 100 ulp. */
  984. for (i = 2; i < 100; i++)
  985. value = FUNC(nextafter) (value, 20);
  986. ulps = ULPDIFF (value, 10);
  987. ulpx = ulp (LIT(100.0));
  988. if (ulps < (LIT(100.0) - ulpx) || ulps > (LIT(100.0) + ulpx))
  989. {
  990. fprintf (stderr, "Value outside of 100 +/- 1ulp.\n");
  991. exit (EXIT_FAILURE);
  992. }
  993. }
  994. /* Do all initialization for a test run with arguments given by ARGC
  995. and ARGV. */
  996. void
  997. libm_test_init (int argc, char **argv)
  998. {
  999. int remaining;
  1000. char *ulps_file_path;
  1001. size_t dir_len = 0;
  1002. verbose = 1;
  1003. output_ulps = 0;
  1004. output_max_error = 1;
  1005. output_points = 1;
  1006. output_dir = NULL;
  1007. /* XXX set to 0 for releases. */
  1008. ignore_max_ulp = 0;
  1009. /* Parse and process arguments. */
  1010. argp_parse (&argp, argc, argv, 0, &remaining, NULL);
  1011. if (remaining != argc)
  1012. {
  1013. fprintf (stderr, "wrong number of arguments");
  1014. argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
  1015. exit (EXIT_FAILURE);
  1016. }
  1017. if (output_ulps)
  1018. {
  1019. if (output_dir != NULL)
  1020. dir_len = strlen (output_dir);
  1021. ulps_file_path = (char *) malloc (dir_len + strlen (ulps_file_name) + 1);
  1022. if (ulps_file_path == NULL)
  1023. {
  1024. perror ("can't allocate path for `ULPs' file: ");
  1025. exit (1);
  1026. }
  1027. sprintf (ulps_file_path, "%s%s", output_dir == NULL ? "" : output_dir, ulps_file_name);
  1028. ulps_file = fopen (ulps_file_path, "a");
  1029. if (ulps_file == NULL)
  1030. {
  1031. perror ("can't open file `ULPs' for writing: ");
  1032. exit (1);
  1033. }
  1034. }
  1035. initialize ();
  1036. fputs (test_msg, stdout);
  1037. check_ulp ();
  1038. }
  1039. /* Process the test results, returning the exit status. */
  1040. int
  1041. libm_test_finish (void)
  1042. {
  1043. if (output_ulps)
  1044. fclose (ulps_file);
  1045. printf ("\nTest suite completed:\n");
  1046. printf (" %d test cases plus %d tests for exception flags and\n"
  1047. " %d tests for errno executed.\n",
  1048. noTests, noExcTests, noErrnoTests);
  1049. if (noErrors)
  1050. {
  1051. printf (" %d errors occurred.\n", noErrors);
  1052. return 1;
  1053. }
  1054. printf (" All tests passed successfully.\n");
  1055. return 0;
  1056. }