123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212 |
- #include "libm-test-support.h"
- #include <argp.h>
- #include <errno.h>
- #include <string.h>
- #include "libm-test-ulps.h"
- #define FSTR_MAX (128)
- #define ulps_file_name "ULPs"
- static FILE *ulps_file;
- static int output_ulps;
- static char *output_dir;
- static int noErrors;
- static int noTests;
- static int noExcTests;
- static int noErrnoTests;
- static int verbose;
- static int output_max_error;
- static int output_points;
- static int ignore_max_ulp;
- static int test_ibm128;
- static FLOAT max_error, real_max_error, imag_max_error;
- static FLOAT prev_max_error, prev_real_max_error, prev_imag_max_error;
- static FLOAT max_valid_error;
- #define TYPE_DECIMAL_DIG __CONCATX (PREFIX, _DECIMAL_DIG)
- #define TYPE_HEX_DIG ((MANT_DIG + 6) / 4)
- static void
- fmt_ftostr (char *dest, size_t size, int precision, const char *conversion,
- FLOAT value)
- {
- char format[64];
- char *ptr_format;
- int ret;
-
- ptr_format = stpcpy (format, "%.");
- ret = sprintf (ptr_format, "%d", precision);
- ptr_format += ret;
- ptr_format = stpcpy (ptr_format, conversion);
-
- if (! signbit (value))
- {
- *dest = ' ';
- dest++;
- size--;
- }
-
- FTOSTR (dest, size, format, value);
- }
- static int
- compare_ulp_data (const void *key, const void *ulp)
- {
- const char *keystr = key;
- const struct ulp_data *ulpdat = ulp;
- return strcmp (keystr, ulpdat->name);
- }
- static const int ulp_i_idx = ULP_I_IDX;
- static const int ulp_idx = ULP_IDX;
- static FLOAT
- find_ulps (const char *name, const struct ulp_data *data, size_t nmemb)
- {
- const struct ulp_data *entry = bsearch (name, data, nmemb, sizeof (*data),
- compare_ulp_data);
- if (entry == NULL)
- return 0;
- else
- return entry->max_ulp[(flag_test_inline ? ulp_i_idx : ulp_idx)];
- }
- void
- init_max_error (const char *name, int exact, int testing_ibm128)
- {
- max_error = 0;
- real_max_error = 0;
- imag_max_error = 0;
- test_ibm128 = testing_ibm128;
- prev_max_error = find_ulps (name, func_ulps,
- sizeof (func_ulps) / sizeof (func_ulps[0]));
- prev_real_max_error = find_ulps (name, func_real_ulps,
- (sizeof (func_real_ulps)
- / sizeof (func_real_ulps[0])));
- prev_imag_max_error = find_ulps (name, func_imag_ulps,
- (sizeof (func_imag_ulps)
- / sizeof (func_imag_ulps[0])));
- if (testing_ibm128)
-
- max_valid_error = exact ? 3 : 16;
- else
- max_valid_error = exact ? 0 : 9;
- prev_max_error = (prev_max_error <= max_valid_error
- ? prev_max_error
- : max_valid_error);
- prev_real_max_error = (prev_real_max_error <= max_valid_error
- ? prev_real_max_error
- : max_valid_error);
- prev_imag_max_error = (prev_imag_max_error <= max_valid_error
- ? prev_imag_max_error
- : max_valid_error);
- feclearexcept (FE_ALL_EXCEPT);
- errno = 0;
- }
- static void
- set_max_error (FLOAT current, FLOAT *curr_max_error)
- {
- if (current > *curr_max_error && current <= max_valid_error)
- *curr_max_error = current;
- }
- static void
- print_float (FLOAT f)
- {
-
- if (issignaling (f))
- printf ("sNaN\n");
- else if (isnan (f))
- printf ("qNaN\n");
- else
- {
- char fstrn[FSTR_MAX], fstrx[FSTR_MAX];
- fmt_ftostr (fstrn, FSTR_MAX, TYPE_DECIMAL_DIG - 1, "e", f);
- fmt_ftostr (fstrx, FSTR_MAX, TYPE_HEX_DIG - 1, "a", f);
- printf ("%s %s\n", fstrn, fstrx);
- }
- }
- static int
- print_screen (int ok)
- {
- if (output_points
- && (verbose > 1
- || (verbose == 1 && ok == 0)))
- return 1;
- return 0;
- }
- static int
- print_screen_max_error (int ok)
- {
- if (output_max_error
- && (verbose > 1
- || ((verbose == 1) && (ok == 0))))
- return 1;
- return 0;
- }
- static void
- update_stats (int ok)
- {
- ++noTests;
- if (!ok)
- ++noErrors;
- }
- static void
- print_function_ulps (const char *function_name, FLOAT ulp)
- {
- if (output_ulps)
- {
- char ustrn[FSTR_MAX];
- FTOSTR (ustrn, FSTR_MAX, "%.0f", FUNC (ceil) (ulp));
- fprintf (ulps_file, "Function: \"%s\":\n", function_name);
- fprintf (ulps_file, "%s: %s\n", qtype_str, ustrn);
- }
- }
- static void
- print_complex_function_ulps (const char *function_name, FLOAT real_ulp,
- FLOAT imag_ulp)
- {
- if (output_ulps)
- {
- char fstrn[FSTR_MAX];
- if (real_ulp != 0.0)
- {
- FTOSTR (fstrn, FSTR_MAX, "%.0f", FUNC (ceil) (real_ulp));
- fprintf (ulps_file, "Function: Real part of \"%s\":\n", function_name);
- fprintf (ulps_file, "%s: %s\n", qtype_str, fstrn);
- }
- if (imag_ulp != 0.0)
- {
- FTOSTR (fstrn, FSTR_MAX, "%.0f", FUNC (ceil) (imag_ulp));
- fprintf (ulps_file, "Function: Imaginary part of \"%s\":\n", function_name);
- fprintf (ulps_file, "%s: %s\n", qtype_str, fstrn);
- }
- }
- }
- static void
- fpstack_test (const char *test_name)
- {
- #if defined (__i386__) || defined (__x86_64__)
- static int old_stack;
- int sw;
- asm ("fnstsw" : "=a" (sw));
- sw >>= 11;
- sw &= 7;
- if (sw != old_stack)
- {
- printf ("FP-Stack wrong after test %s (%d, should be %d)\n",
- test_name, sw, old_stack);
- ++noErrors;
- old_stack = sw;
- }
- #endif
- }
- void
- print_max_error (const char *func_name)
- {
- int ok = 0;
- if (max_error == 0.0 || (max_error <= prev_max_error && !ignore_max_ulp))
- {
- ok = 1;
- }
- if (!ok)
- print_function_ulps (func_name, max_error);
- if (print_screen_max_error (ok))
- {
- char mestr[FSTR_MAX], pmestr[FSTR_MAX];
- FTOSTR (mestr, FSTR_MAX, "%.0f", FUNC (ceil) (max_error));
- FTOSTR (pmestr, FSTR_MAX, "%.0f", FUNC (ceil) (prev_max_error));
- printf ("Maximal error of `%s'\n", func_name);
- printf (" is : %s ulp\n", mestr);
- printf (" accepted: %s ulp\n", pmestr);
- }
- update_stats (ok);
- }
- void
- print_complex_max_error (const char *func_name)
- {
- int real_ok = 0, imag_ok = 0, ok;
- if (real_max_error == 0
- || (real_max_error <= prev_real_max_error && !ignore_max_ulp))
- {
- real_ok = 1;
- }
- if (imag_max_error == 0
- || (imag_max_error <= prev_imag_max_error && !ignore_max_ulp))
- {
- imag_ok = 1;
- }
- ok = real_ok && imag_ok;
- if (!ok)
- print_complex_function_ulps (func_name,
- real_ok ? 0 : real_max_error,
- imag_ok ? 0 : imag_max_error);
- if (print_screen_max_error (ok))
- {
- char rmestr[FSTR_MAX], prmestr[FSTR_MAX];
- char imestr[FSTR_MAX], pimestr[FSTR_MAX];
- FTOSTR (rmestr, FSTR_MAX, "%.0f", FUNC (ceil) (real_max_error));
- FTOSTR (prmestr, FSTR_MAX, "%.0f", FUNC (ceil) (prev_real_max_error));
- FTOSTR (imestr, FSTR_MAX, "%.0f", FUNC (ceil) (imag_max_error));
- FTOSTR (pimestr, FSTR_MAX, "%.0f", FUNC (ceil) (prev_imag_max_error));
- printf ("Maximal error of real part of: %s\n", func_name);
- printf (" is : %s ulp\n", rmestr);
- printf (" accepted: %s ulp\n", prmestr);
- printf ("Maximal error of imaginary part of: %s\n", func_name);
- printf (" is : %s ulp\n", imestr);
- printf (" accepted: %s ulp\n", pimestr);
- }
- update_stats (ok);
- }
- #if FE_ALL_EXCEPT
- static void
- test_single_exception (const char *test_name,
- int exception,
- int exc_flag,
- int fe_flag,
- const char *flag_name)
- {
- int ok = 1;
- if (exception & exc_flag)
- {
- if (fetestexcept (fe_flag))
- {
- if (print_screen (1))
- printf ("Pass: %s: Exception \"%s\" set\n", test_name, flag_name);
- }
- else
- {
- ok = 0;
- if (print_screen (0))
- printf ("Failure: %s: Exception \"%s\" not set\n",
- test_name, flag_name);
- }
- }
- else
- {
- if (fetestexcept (fe_flag))
- {
- ok = 0;
- if (print_screen (0))
- printf ("Failure: %s: Exception \"%s\" set\n",
- test_name, flag_name);
- }
- else
- {
- if (print_screen (1))
- printf ("%s: Exception \"%s\" not set\n", test_name,
- flag_name);
- }
- }
- if (!ok)
- ++noErrors;
- }
- #endif
- static void
- test_exceptions (const char *test_name, int exception)
- {
- if (flag_test_exceptions && EXCEPTION_TESTS (FLOAT))
- {
- ++noExcTests;
- #ifdef FE_DIVBYZERO
- if ((exception & DIVIDE_BY_ZERO_EXCEPTION_OK) == 0)
- test_single_exception (test_name, exception,
- DIVIDE_BY_ZERO_EXCEPTION, FE_DIVBYZERO,
- "Divide by zero");
- #endif
- #ifdef FE_INVALID
- if ((exception & INVALID_EXCEPTION_OK) == 0)
- test_single_exception (test_name, exception,
- INVALID_EXCEPTION, FE_INVALID,
- "Invalid operation");
- #endif
- #ifdef FE_OVERFLOW
- if ((exception & OVERFLOW_EXCEPTION_OK) == 0)
- test_single_exception (test_name, exception, OVERFLOW_EXCEPTION,
- FE_OVERFLOW, "Overflow");
- #endif
-
- #ifdef FE_UNDERFLOW
- if ((exception & UNDERFLOW_EXCEPTION_OK) == 0
- && !(test_ibm128
- && (exception & UNDERFLOW_EXCEPTION) == 0))
- test_single_exception (test_name, exception, UNDERFLOW_EXCEPTION,
- FE_UNDERFLOW, "Underflow");
- #endif
- #ifdef FE_INEXACT
- if ((exception & (INEXACT_EXCEPTION | NO_INEXACT_EXCEPTION)) != 0
- && !(test_ibm128
- && (exception & NO_INEXACT_EXCEPTION) != 0))
- test_single_exception (test_name, exception, INEXACT_EXCEPTION,
- FE_INEXACT, "Inexact");
- #endif
- }
- feclearexcept (FE_ALL_EXCEPT);
- }
- static void
- test_single_errno (const char *test_name, int errno_value,
- int expected_value, const char *expected_name)
- {
- if (errno_value == expected_value)
- {
- if (print_screen (1))
- printf ("Pass: %s: errno set to %d (%s)\n", test_name, errno_value,
- expected_name);
- }
- else
- {
- ++noErrors;
- if (print_screen (0))
- printf ("Failure: %s: errno set to %d, expected %d (%s)\n",
- test_name, errno_value, expected_value, expected_name);
- }
- }
- static void
- test_errno (const char *test_name, int errno_value, int exceptions)
- {
- if (flag_test_errno)
- {
- ++noErrnoTests;
- if (exceptions & ERRNO_UNCHANGED)
- test_single_errno (test_name, errno_value, 0, "unchanged");
- if (exceptions & ERRNO_EDOM)
- test_single_errno (test_name, errno_value, EDOM, "EDOM");
- if (exceptions & ERRNO_ERANGE)
- test_single_errno (test_name, errno_value, ERANGE, "ERANGE");
- }
- }
- #define ULPDIFF(given, expected) \
- (FUNC(fabs) ((given) - (expected)) / ulp (expected))
- static FLOAT
- ulp (FLOAT value)
- {
- FLOAT ulp;
- switch (fpclassify (value))
- {
- case FP_ZERO:
-
-
- case FP_SUBNORMAL:
-
- ulp = FUNC(ldexp) (1.0, MIN_EXP - MANT_DIG);
- break;
- case FP_NORMAL:
- ulp = FUNC(ldexp) (1.0, FUNC(ilogb) (value) - MANT_DIG + 1);
- break;
- default:
-
- abort ();
- break;
- }
- return ulp;
- }
- static void
- check_float_internal (const char *test_name, FLOAT computed, FLOAT expected,
- int exceptions,
- FLOAT *curr_max_error, FLOAT max_ulp)
- {
- int ok = 0;
- int print_diff = 0;
- FLOAT diff = 0;
- FLOAT ulps = 0;
- int errno_value = errno;
- test_exceptions (test_name, exceptions);
- test_errno (test_name, errno_value, exceptions);
- if (exceptions & IGNORE_RESULT)
- goto out;
- if (issignaling (computed) && issignaling (expected))
- {
- if ((exceptions & TEST_NAN_SIGN) != 0
- && signbit (computed) != signbit (expected))
- {
- ok = 0;
- printf ("signaling NaN has wrong sign.\n");
- }
- else if ((exceptions & TEST_NAN_PAYLOAD) != 0
- && (FUNC (getpayload) (&computed)
- != FUNC (getpayload) (&expected)))
- {
- ok = 0;
- printf ("signaling NaN has wrong payload.\n");
- }
- else
- ok = 1;
- }
- else if (issignaling (computed) || issignaling (expected))
- ok = 0;
- else if (isnan (computed) && isnan (expected))
- {
- if ((exceptions & TEST_NAN_SIGN) != 0
- && signbit (computed) != signbit (expected))
- {
- ok = 0;
- printf ("quiet NaN has wrong sign.\n");
- }
- else if ((exceptions & TEST_NAN_PAYLOAD) != 0
- && (FUNC (getpayload) (&computed)
- != FUNC (getpayload) (&expected)))
- {
- ok = 0;
- printf ("quiet NaN has wrong payload.\n");
- }
- else
- ok = 1;
- }
- else if (isinf (computed) && isinf (expected))
- {
-
- if ((exceptions & IGNORE_ZERO_INF_SIGN) == 0
- && signbit (computed) != signbit (expected))
- {
- ok = 0;
- printf ("infinity has wrong sign.\n");
- }
- else
- ok = 1;
- }
-
- else if (isinf (computed) || isnan (computed)
- || isinf (expected) || isnan (expected))
- ok = 0;
- else
- {
- diff = FUNC(fabs) (computed - expected);
- ulps = ULPDIFF (computed, expected);
- set_max_error (ulps, curr_max_error);
- print_diff = 1;
- if ((exceptions & IGNORE_ZERO_INF_SIGN) == 0
- && computed == 0.0 && expected == 0.0
- && signbit(computed) != signbit (expected))
- ok = 0;
- else if (ulps <= max_ulp && !ignore_max_ulp)
- ok = 1;
- else
- ok = 0;
- }
- if (print_screen (ok))
- {
- if (!ok)
- printf ("Failure: ");
- printf ("Test: %s\n", test_name);
- printf ("Result:\n");
- printf (" is: ");
- print_float (computed);
- printf (" should be: ");
- print_float (expected);
- if (print_diff)
- {
- char dstrn[FSTR_MAX], dstrx[FSTR_MAX];
- char ustrn[FSTR_MAX], mustrn[FSTR_MAX];
- fmt_ftostr (dstrn, FSTR_MAX, TYPE_DECIMAL_DIG - 1, "e", diff);
- fmt_ftostr (dstrx, FSTR_MAX, TYPE_HEX_DIG - 1, "a", diff);
- fmt_ftostr (ustrn, FSTR_MAX, 4, "f", ulps);
- fmt_ftostr (mustrn, FSTR_MAX, 4, "f", max_ulp);
- printf (" difference: %s %s\n", dstrn, dstrx);
- printf (" ulp : %s\n", ustrn);
- printf (" max.ulp : %s\n", mustrn);
- }
- }
- update_stats (ok);
- out:
- fpstack_test (test_name);
- feclearexcept (FE_ALL_EXCEPT);
- errno = 0;
- }
- void
- check_float (const char *test_name, FLOAT computed, FLOAT expected,
- int exceptions)
- {
- check_float_internal (test_name, computed, expected,
- exceptions, &max_error, prev_max_error);
- }
- void
- check_complex (const char *test_name, CFLOAT computed,
- CFLOAT expected,
- int exception)
- {
- FLOAT part_comp, part_exp;
- char *str;
- if (asprintf (&str, "Real part of: %s", test_name) == -1)
- abort ();
- part_comp = __real__ computed;
- part_exp = __real__ expected;
- check_float_internal (str, part_comp, part_exp,
- exception, &real_max_error, prev_real_max_error);
- free (str);
- if (asprintf (&str, "Imaginary part of: %s", test_name) == -1)
- abort ();
- part_comp = __imag__ computed;
- part_exp = __imag__ expected;
-
- check_float_internal (str, part_comp, part_exp,
- exception & (IGNORE_ZERO_INF_SIGN
- | TEST_NAN_SIGN
- | IGNORE_RESULT),
- &imag_max_error, prev_imag_max_error);
- free (str);
- }
- void
- check_int (const char *test_name, int computed, int expected,
- int exceptions)
- {
- int ok = 0;
- int errno_value = errno;
- test_exceptions (test_name, exceptions);
- test_errno (test_name, errno_value, exceptions);
- if (exceptions & IGNORE_RESULT)
- goto out;
- noTests++;
- if (computed == expected)
- ok = 1;
- if (print_screen (ok))
- {
- if (!ok)
- printf ("Failure: ");
- printf ("Test: %s\n", test_name);
- printf ("Result:\n");
- printf (" is: %d\n", computed);
- printf (" should be: %d\n", expected);
- }
- update_stats (ok);
- out:
- fpstack_test (test_name);
- feclearexcept (FE_ALL_EXCEPT);
- errno = 0;
- }
- void
- check_long (const char *test_name, long int computed, long int expected,
- int exceptions)
- {
- int ok = 0;
- int errno_value = errno;
- test_exceptions (test_name, exceptions);
- test_errno (test_name, errno_value, exceptions);
- if (exceptions & IGNORE_RESULT)
- goto out;
- noTests++;
- if (computed == expected)
- ok = 1;
- if (print_screen (ok))
- {
- if (!ok)
- printf ("Failure: ");
- printf ("Test: %s\n", test_name);
- printf ("Result:\n");
- printf (" is: %ld\n", computed);
- printf (" should be: %ld\n", expected);
- }
- update_stats (ok);
- out:
- fpstack_test (test_name);
- feclearexcept (FE_ALL_EXCEPT);
- errno = 0;
- }
- void
- check_bool (const char *test_name, int computed, int expected,
- int exceptions)
- {
- int ok = 0;
- int errno_value = errno;
- test_exceptions (test_name, exceptions);
- test_errno (test_name, errno_value, exceptions);
- if (exceptions & IGNORE_RESULT)
- goto out;
- noTests++;
- if ((computed == 0) == (expected == 0))
- ok = 1;
- if (print_screen (ok))
- {
- if (!ok)
- printf ("Failure: ");
- printf ("Test: %s\n", test_name);
- printf ("Result:\n");
- printf (" is: %d\n", computed);
- printf (" should be: %d\n", expected);
- }
- update_stats (ok);
- out:
- fpstack_test (test_name);
- feclearexcept (FE_ALL_EXCEPT);
- errno = 0;
- }
- void
- check_longlong (const char *test_name, long long int computed,
- long long int expected,
- int exceptions)
- {
- int ok = 0;
- int errno_value = errno;
- test_exceptions (test_name, exceptions);
- test_errno (test_name, errno_value, exceptions);
- if (exceptions & IGNORE_RESULT)
- goto out;
- noTests++;
- if (computed == expected)
- ok = 1;
- if (print_screen (ok))
- {
- if (!ok)
- printf ("Failure:");
- printf ("Test: %s\n", test_name);
- printf ("Result:\n");
- printf (" is: %lld\n", computed);
- printf (" should be: %lld\n", expected);
- }
- update_stats (ok);
- out:
- fpstack_test (test_name);
- feclearexcept (FE_ALL_EXCEPT);
- errno = 0;
- }
- void
- check_intmax_t (const char *test_name, intmax_t computed,
- intmax_t expected, int exceptions)
- {
- int ok = 0;
- int errno_value = errno;
- test_exceptions (test_name, exceptions);
- test_errno (test_name, errno_value, exceptions);
- if (exceptions & IGNORE_RESULT)
- goto out;
- noTests++;
- if (computed == expected)
- ok = 1;
- if (print_screen (ok))
- {
- if (!ok)
- printf ("Failure:");
- printf ("Test: %s\n", test_name);
- printf ("Result:\n");
- printf (" is: %jd\n", computed);
- printf (" should be: %jd\n", expected);
- }
- update_stats (ok);
- out:
- fpstack_test (test_name);
- feclearexcept (FE_ALL_EXCEPT);
- errno = 0;
- }
- void
- check_uintmax_t (const char *test_name, uintmax_t computed,
- uintmax_t expected, int exceptions)
- {
- int ok = 0;
- int errno_value = errno;
- test_exceptions (test_name, exceptions);
- test_errno (test_name, errno_value, exceptions);
- if (exceptions & IGNORE_RESULT)
- goto out;
- noTests++;
- if (computed == expected)
- ok = 1;
- if (print_screen (ok))
- {
- if (!ok)
- printf ("Failure:");
- printf ("Test: %s\n", test_name);
- printf ("Result:\n");
- printf (" is: %ju\n", computed);
- printf (" should be: %ju\n", expected);
- }
- update_stats (ok);
- out:
- fpstack_test (test_name);
- feclearexcept (FE_ALL_EXCEPT);
- errno = 0;
- }
- int
- enable_test (int exceptions)
- {
- if (exceptions & XFAIL_TEST)
- return 0;
- if (flag_test_inline && (exceptions & NO_TEST_INLINE))
- return 0;
- if (flag_test_finite && (exceptions & NON_FINITE) != 0)
- return 0;
- if ((!SNAN_TESTS (FLOAT) || !snan_tests_arg)
- && (exceptions & TEST_SNAN) != 0)
- return 0;
- if (flag_test_mathvec && (exceptions & NO_TEST_MATHVEC) != 0)
- return 0;
- return 1;
- }
- static void
- initialize (void)
- {
- fpstack_test ("start *init*");
-
- feclearexcept (FE_ALL_EXCEPT);
- errno = 0;
-
- fpstack_test ("end *init*");
- }
- static const struct argp_option options[] =
- {
- { "verbose", 'v', "NUMBER", 0, "Level of verbosity (0..3)"},
- { "ulps-file", 'u', NULL, 0, "Output ulps to file ULPs"},
- { "no-max-error", 'f', NULL, 0,
- "Don't output maximal errors of functions"},
- { "no-points", 'p', NULL, 0,
- "Don't output results of functions invocations"},
- { "ignore-max-ulp", 'i', "yes/no", 0,
- "Ignore given maximal errors"},
- { "output-dir", 'o', "DIR", 0,
- "Directory where generated files will be placed"},
- { NULL, 0, NULL, 0, NULL }
- };
- static error_t parse_opt (int key, char *arg, struct argp_state *state);
- static struct argp argp =
- {
- options, parse_opt, NULL, doc,
- };
- static error_t
- parse_opt (int key, char *arg, struct argp_state *state)
- {
- switch (key)
- {
- case 'f':
- output_max_error = 0;
- break;
- case 'i':
- if (strcmp (arg, "yes") == 0)
- ignore_max_ulp = 1;
- else if (strcmp (arg, "no") == 0)
- ignore_max_ulp = 0;
- break;
- case 'o':
- output_dir = (char *) malloc (strlen (arg) + 1);
- if (output_dir != NULL)
- strcpy (output_dir, arg);
- else
- return errno;
- break;
- case 'p':
- output_points = 0;
- break;
- case 'u':
- output_ulps = 1;
- break;
- case 'v':
- if (optarg)
- verbose = (unsigned int) strtoul (optarg, NULL, 0);
- else
- verbose = 3;
- break;
- default:
- return ARGP_ERR_UNKNOWN;
- }
- return 0;
- }
- static void
- check_ulp (void)
- {
- FLOAT ulps, ulpx, value;
- int i;
-
- ulps = ulp (0x0.0p0);
- if (fpclassify (ulps) != FP_SUBNORMAL)
- {
- fprintf (stderr, "ulp (0x0.0p0) is not FP_SUBNORMAL!\n");
- exit (EXIT_FAILURE);
- }
-
- ulps = ulp (LIT(1.0));
- if (fpclassify (ulps) != FP_NORMAL)
- {
- fprintf (stderr, "ulp (1.0L) is not FP_NORMAL\n");
- exit (EXIT_FAILURE);
- }
-
- value = FUNC(nextafter) (0, 1);
- ulps = ULPDIFF (value, 0);
- ulpx = ulp (LIT(1.0));
- if (ulps < (LIT(1.0) - ulpx) || ulps > (LIT(1.0) + ulpx))
- {
- fprintf (stderr, "Value outside of 1 +/- 1ulp.\n");
- exit (EXIT_FAILURE);
- }
-
- value = FUNC(nextafter) (10, 20);
- ulps = ULPDIFF (value, 10);
- ulpx = ulp (LIT(1.0));
- if (ulps < (LIT(1.0) - ulpx) || ulps > (LIT(1.0) + ulpx))
- {
- fprintf (stderr, "Value outside of 1 +/- 1ulp.\n");
- exit (EXIT_FAILURE);
- }
-
- value = FUNC(nextafter) (value, 20);
- ulps = ULPDIFF (value, 10);
- ulpx = ulp (LIT(2.0));
- if (ulps < (LIT(2.0) - ulpx) || ulps > (LIT(2.0) + ulpx))
- {
- fprintf (stderr, "Value outside of 2 +/- 1ulp.\n");
- exit (EXIT_FAILURE);
- }
-
- for (i = 2; i < 100; i++)
- value = FUNC(nextafter) (value, 20);
- ulps = ULPDIFF (value, 10);
- ulpx = ulp (LIT(100.0));
- if (ulps < (LIT(100.0) - ulpx) || ulps > (LIT(100.0) + ulpx))
- {
- fprintf (stderr, "Value outside of 100 +/- 1ulp.\n");
- exit (EXIT_FAILURE);
- }
- }
- void
- libm_test_init (int argc, char **argv)
- {
- int remaining;
- char *ulps_file_path;
- size_t dir_len = 0;
- verbose = 1;
- output_ulps = 0;
- output_max_error = 1;
- output_points = 1;
- output_dir = NULL;
-
- ignore_max_ulp = 0;
-
- argp_parse (&argp, argc, argv, 0, &remaining, NULL);
- if (remaining != argc)
- {
- fprintf (stderr, "wrong number of arguments");
- argp_help (&argp, stdout, ARGP_HELP_SEE, program_invocation_short_name);
- exit (EXIT_FAILURE);
- }
- if (output_ulps)
- {
- if (output_dir != NULL)
- dir_len = strlen (output_dir);
- ulps_file_path = (char *) malloc (dir_len + strlen (ulps_file_name) + 1);
- if (ulps_file_path == NULL)
- {
- perror ("can't allocate path for `ULPs' file: ");
- exit (1);
- }
- sprintf (ulps_file_path, "%s%s", output_dir == NULL ? "" : output_dir, ulps_file_name);
- ulps_file = fopen (ulps_file_path, "a");
- if (ulps_file == NULL)
- {
- perror ("can't open file `ULPs' for writing: ");
- exit (1);
- }
- }
- initialize ();
- fputs (test_msg, stdout);
- check_ulp ();
- }
- int
- libm_test_finish (void)
- {
- if (output_ulps)
- fclose (ulps_file);
- printf ("\nTest suite completed:\n");
- printf (" %d test cases plus %d tests for exception flags and\n"
- " %d tests for errno executed.\n",
- noTests, noExcTests, noErrnoTests);
- if (noErrors)
- {
- printf (" %d errors occurred.\n", noErrors);
- return 1;
- }
- printf (" All tests passed successfully.\n");
- return 0;
- }
|