Home | History | Annotate | Line # | Download | only in tests
tgeneric.c revision 1.1.1.3
      1 /* Generic test file for functions with one or two arguments (the second being
      2    either mpfr_t or double or unsigned long).
      3 
      4 Copyright 2001-2016 Free Software Foundation, Inc.
      5 Contributed by the AriC and Caramba projects, INRIA.
      6 
      7 This file is part of the GNU MPFR Library.
      8 
      9 The GNU MPFR Library is free software; you can redistribute it and/or modify
     10 it under the terms of the GNU Lesser General Public License as published by
     11 the Free Software Foundation; either version 3 of the License, or (at your
     12 option) any later version.
     13 
     14 The GNU MPFR Library is distributed in the hope that it will be useful, but
     15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     17 License for more details.
     18 
     19 You should have received a copy of the GNU Lesser General Public License
     20 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     21 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     23 
     24 /* Define TWO_ARGS for two-argument functions like mpfr_pow.
     25    Define DOUBLE_ARG1 or DOUBLE_ARG2 for function with a double operand in
     26    first or second place like sub_d or d_sub.
     27    Define ULONG_ARG1 or ULONG_ARG2 for function with an unsigned long
     28    operand in first or second place like sub_ui or ui_sub. */
     29 
     30 #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2) || \
     31   defined(ULONG_ARG1) || defined(ULONG_ARG2)
     32 #define TWO_ARGS_ALL
     33 #endif
     34 
     35 #ifndef TEST_RANDOM_POS
     36 /* For the random function: one number on two is negative. */
     37 #define TEST_RANDOM_POS 256
     38 #endif
     39 
     40 #ifndef TEST_RANDOM_POS2
     41 /* For the random function: one number on two is negative. */
     42 #define TEST_RANDOM_POS2 256
     43 #endif
     44 
     45 #ifndef TEST_RANDOM_EMIN
     46 #define TEST_RANDOM_EMIN -256
     47 #endif
     48 
     49 #ifndef TEST_RANDOM_EMAX
     50 #define TEST_RANDOM_EMAX 255
     51 #endif
     52 
     53 #ifndef TEST_RANDOM_ALWAYS_SCALE
     54 #define TEST_RANDOM_ALWAYS_SCALE 0
     55 #endif
     56 
     57 /* If the MPFR_SUSPICIOUS_OVERFLOW test fails but this is not a bug,
     58    then define TGENERIC_SO_TEST with an adequate test (possibly 0) to
     59    omit this particular case. */
     60 #ifndef TGENERIC_SO_TEST
     61 #define TGENERIC_SO_TEST 1
     62 #endif
     63 
     64 #define STR(F) #F
     65 #define MAKE_STR(S) STR(S)
     66 
     67 /* The (void *) below is needed to avoid a warning with gcc 4.2+ and functions
     68  * with 2 arguments. See <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36299>.
     69  */
     70 #define TGENERIC_FAIL(S, X, U)                                          \
     71   do                                                                    \
     72     {                                                                   \
     73       printf ("tgeneric: %s\nx = ", (S));                               \
     74       mpfr_dump (X);;                                                   \
     75       if ((void *) (U) != 0)                                            \
     76         {                                                               \
     77           printf ("u = ");                                              \
     78           mpfr_dump (U);                                                \
     79         }                                                               \
     80       printf ("yprec = %u, rnd_mode = %s, inexact = %d\nflags =",       \
     81               (unsigned int) yprec, mpfr_print_rnd_mode (rnd),          \
     82               compare);                                                 \
     83       flags_out (flags);                                                \
     84       exit (1);                                                         \
     85     }                                                                   \
     86   while (0)
     87 
     88 #define TGENERIC_CHECK_AUX(S, EXPR, U)                          \
     89   do                                                            \
     90     if (!(EXPR))                                                \
     91       TGENERIC_FAIL (S " for " MAKE_STR(TEST_FUNCTION), x, U);  \
     92   while (0)
     93 
     94 #undef TGENERIC_CHECK
     95 #if defined(TWO_ARGS_ALL)
     96 #define TGENERIC_CHECK(S, EXPR) TGENERIC_CHECK_AUX(S, EXPR, u)
     97 #else
     98 #define TGENERIC_CHECK(S, EXPR) TGENERIC_CHECK_AUX(S, EXPR, 0)
     99 #endif
    100 
    101 #ifdef DEBUG_TGENERIC
    102 #define TGENERIC_IAUX(F,P,X,U)                                          \
    103   do                                                                    \
    104     {                                                                   \
    105       printf ("tgeneric: testing function " STR(F)                      \
    106               ", %s, target prec = %lu\nx = ",                          \
    107               mpfr_print_rnd_mode (rnd), (unsigned long) (P));          \
    108       mpfr_dump (X);                                                    \
    109       if ((void *) (U) != 0)                                            \
    110         {                                                               \
    111           printf ("u = ");                                              \
    112           mpfr_dump (U);                                                \
    113         }                                                               \
    114     }                                                                   \
    115   while (0)
    116 #undef TGENERIC_INFO
    117 #if defined(TWO_ARGS_ALL)
    118 #define TGENERIC_INFO(F,P) TGENERIC_IAUX(F,P,x,u)
    119 #else
    120 #define TGENERIC_INFO(F,P) TGENERIC_IAUX(F,P,x,0)
    121 #endif
    122 #endif  /* DEBUG_TGENERIC */
    123 
    124 /* For some functions (for example cos), the argument reduction is too
    125    expensive when using mpfr_get_emax(). Then simply define REDUCE_EMAX
    126    to some reasonable value before including tgeneric.c. */
    127 #ifndef REDUCE_EMAX
    128 #define REDUCE_EMAX mpfr_get_emax ()
    129 #endif
    130 
    131 static void
    132 test_generic (mpfr_prec_t p0, mpfr_prec_t p1, unsigned int nmax)
    133 {
    134   mpfr_prec_t prec, xprec, yprec;
    135   mpfr_t x, y, z, t, w;
    136 #if defined(TWO_ARGS_ALL)
    137   mpfr_t u;
    138 #endif
    139 #if defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2)
    140   double d;
    141 #endif
    142 #if defined(ULONG_ARG1) || defined(ULONG_ARG2)
    143   unsigned long i;
    144 #endif
    145   mpfr_rnd_t rnd;
    146   int inexact, compare, compare2;
    147   unsigned int n;
    148   unsigned long ctrt = 0, ctrn = 0;
    149   int test_of = 1, test_uf = 1;
    150   mpfr_exp_t old_emin, old_emax;
    151 
    152   old_emin = mpfr_get_emin ();
    153   old_emax = mpfr_get_emax ();
    154 
    155   mpfr_inits2 (MPFR_PREC_MIN, x, y, z, t, w, (mpfr_ptr) 0);
    156 #if defined(TWO_ARGS_ALL)
    157   mpfr_init2 (u, MPFR_PREC_MIN);
    158 #endif
    159 
    160   /* generic test */
    161   for (prec = p0; prec <= p1; prec++)
    162     {
    163       mpfr_set_prec (z, prec);
    164       mpfr_set_prec (t, prec);
    165       yprec = prec + 10;
    166       mpfr_set_prec (y, yprec);
    167       mpfr_set_prec (w, yprec);
    168 
    169       /* Note: in precision p1, we test 4 special cases. */
    170       for (n = 0; n < (prec == p1 ? nmax + 4 : nmax); n++)
    171         {
    172           int infinite_input = 0;
    173           unsigned int flags;
    174           mpfr_exp_t oemin, oemax;
    175 
    176           xprec = prec;
    177           if (randlimb () & 1)
    178             {
    179               /* In half cases, modify the precision of the inputs:
    180                  If the base precision (for the result) is small,
    181                  take a larger input precision in general, else
    182                  take a smaller precision. */
    183               xprec *= (prec < 16 ? 256.0 : 1.0) *
    184                 (double) randlimb () / MP_LIMB_T_MAX;
    185               if (xprec < MPFR_PREC_MIN)
    186                 xprec = MPFR_PREC_MIN;
    187             }
    188           mpfr_set_prec (x, xprec);
    189 #if defined(TWO_ARGS)
    190           mpfr_set_prec (u, xprec);
    191 #elif defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2)
    192           mpfr_set_prec (u, IEEE_DBL_MANT_DIG);
    193 #elif defined(ULONG_ARG1) || defined(ULONG_ARG2)
    194           mpfr_set_prec (u, sizeof (unsigned long) * CHAR_BIT);
    195 #endif
    196 
    197           if (n > 3 || prec < p1)
    198             {
    199 #if defined(RAND_FUNCTION)
    200               RAND_FUNCTION (x);
    201 #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2)
    202               RAND_FUNCTION (u);
    203 #endif
    204 #else  /* ! defined(RAND_FUNCTION) */
    205               tests_default_random (x, TEST_RANDOM_POS,
    206                                     TEST_RANDOM_EMIN, TEST_RANDOM_EMAX,
    207                                     TEST_RANDOM_ALWAYS_SCALE);
    208 #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2)
    209               tests_default_random (u, TEST_RANDOM_POS2,
    210                                     TEST_RANDOM_EMIN, TEST_RANDOM_EMAX,
    211                                     TEST_RANDOM_ALWAYS_SCALE);
    212 #endif
    213 #endif  /* ! defined(RAND_FUNCTION) */
    214             }
    215           else
    216             {
    217               /* Special cases tested in precision p1 if n <= 3. They are
    218                  useful really in the extended exponent range. */
    219 #if (defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2)) && defined(MPFR_ERRDIVZERO)
    220               goto next_n;
    221 #endif
    222               set_emin (MPFR_EMIN_MIN);
    223               set_emax (MPFR_EMAX_MAX);
    224               if (n <= 1)
    225                 {
    226                   mpfr_set_si (x, n == 0 ? 1 : -1, MPFR_RNDN);
    227                   mpfr_set_exp (x, mpfr_get_emin ());
    228 #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2)
    229                   mpfr_set_si (u, randlimb () % 2 == 0 ? 1 : -1, MPFR_RNDN);
    230                   mpfr_set_exp (u, mpfr_get_emin ());
    231 #endif
    232                 }
    233               else  /* 2 <= n <= 3 */
    234                 {
    235                   if (getenv ("MPFR_CHECK_MAX") == NULL)
    236                     goto next_n;
    237                   mpfr_set_si (x, n == 0 ? 1 : -1, MPFR_RNDN);
    238                   mpfr_setmax (x, REDUCE_EMAX);
    239 #if defined(TWO_ARGS) || defined(DOUBLE_ARG1) || defined(DOUBLE_ARG2)
    240                   mpfr_set_si (u, randlimb () % 2 == 0 ? 1 : -1, MPFR_RNDN);
    241                   mpfr_setmax (u, mpfr_get_emax ());
    242 #endif
    243                 }
    244             }
    245 
    246 #if defined(ULONG_ARG1) || defined(ULONG_ARG2)
    247           i = randlimb ();
    248           inexact = mpfr_set_ui (u, i, MPFR_RNDN);
    249           MPFR_ASSERTN (inexact == 0);
    250 #endif
    251 
    252           /* Exponent range for the test. */
    253           oemin = mpfr_get_emin ();
    254           oemax = mpfr_get_emax ();
    255 
    256           rnd = RND_RAND ();
    257           mpfr_clear_flags ();
    258 #ifdef DEBUG_TGENERIC
    259           TGENERIC_INFO (TEST_FUNCTION, MPFR_PREC (y));
    260 #endif
    261 #if defined(TWO_ARGS)
    262           compare = TEST_FUNCTION (y, x, u, rnd);
    263 #elif defined(DOUBLE_ARG1)
    264           d = mpfr_get_d (u, rnd);
    265           compare = TEST_FUNCTION (y, d, x, rnd);
    266           /* d can be infinite due to overflow in mpfr_get_d */
    267           infinite_input |= DOUBLE_ISINF (d);
    268 #elif defined(DOUBLE_ARG2)
    269           d = mpfr_get_d (u, rnd);
    270           compare = TEST_FUNCTION (y, x, d, rnd);
    271           /* d can be infinite due to overflow in mpfr_get_d */
    272           infinite_input |= DOUBLE_ISINF (d);
    273 #elif defined(ULONG_ARG1)
    274           compare = TEST_FUNCTION (y, i, x, rnd);
    275 #elif defined(ULONG_ARG2)
    276           compare = TEST_FUNCTION (y, x, i, rnd);
    277 #else
    278           compare = TEST_FUNCTION (y, x, rnd);
    279 #endif
    280           flags = __gmpfr_flags;
    281           if (mpfr_get_emin () != oemin ||
    282               mpfr_get_emax () != oemax)
    283             {
    284               printf ("tgeneric: the exponent range has been modified"
    285                       " by the tested function!\n");
    286               exit (1);
    287             }
    288           TGENERIC_CHECK ("bad inexact flag",
    289                           (compare != 0) ^ (mpfr_inexflag_p () == 0));
    290           ctrt++;
    291 
    292           /* Tests in a reduced exponent range. */
    293           {
    294             unsigned int oldflags = flags;
    295             mpfr_exp_t e, emin, emax;
    296 
    297             /* Determine the smallest exponent range containing the
    298                exponents of the mpfr_t inputs (x, and u if TWO_ARGS)
    299                and output (y). */
    300             emin = MPFR_EMAX_MAX;
    301             emax = MPFR_EMIN_MIN;
    302             if (MPFR_IS_PURE_FP (x))
    303               {
    304                 e = MPFR_GET_EXP (x);
    305                 if (e < emin)
    306                   emin = e;
    307                 if (e > emax)
    308                   emax = e;
    309               }
    310 #if defined(TWO_ARGS)
    311             if (MPFR_IS_PURE_FP (u))
    312               {
    313                 e = MPFR_GET_EXP (u);
    314                 if (e < emin)
    315                   emin = e;
    316                 if (e > emax)
    317                   emax = e;
    318               }
    319 #endif
    320             if (MPFR_IS_PURE_FP (y))
    321               {
    322                 e = MPFR_GET_EXP (y);
    323                 if (test_of && e - 1 >= emax)
    324                   {
    325                     unsigned int ex_flags;
    326 
    327                     mpfr_set_emax (e - 1);
    328                     mpfr_clear_flags ();
    329 #if defined(TWO_ARGS)
    330                     inexact = TEST_FUNCTION (w, x, u, rnd);
    331 #elif defined(DOUBLE_ARG1)
    332                     inexact = TEST_FUNCTION (w, d, x, rnd);
    333 #elif defined(DOUBLE_ARG2)
    334                     inexact = TEST_FUNCTION (w, x, d, rnd);
    335 #elif defined(ULONG_ARG1)
    336                     inexact = TEST_FUNCTION (w, i, x, rnd);
    337 #elif defined(ULONG_ARG2)
    338                     inexact = TEST_FUNCTION (w, x, i, rnd);
    339 #else
    340                     inexact = TEST_FUNCTION (w, x, rnd);
    341 #endif
    342                     flags = __gmpfr_flags;
    343                     mpfr_set_emax (oemax);
    344                     ex_flags = MPFR_FLAGS_OVERFLOW | MPFR_FLAGS_INEXACT;
    345                     if (flags != ex_flags)
    346                       {
    347                         printf ("tgeneric: error for " MAKE_STR(TEST_FUNCTION)
    348                                 ", reduced exponent range [%"
    349                                 MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC
    350                                 "d] (overflow test) on:\n",
    351                                 (mpfr_eexp_t) oemin, (mpfr_eexp_t) e - 1);
    352                         printf ("x = ");
    353                         mpfr_dump (x);
    354 #if defined(TWO_ARGS_ALL)
    355                         printf ("u = ");
    356                         mpfr_dump (u);
    357 #endif
    358                         printf ("yprec = %u, rnd_mode = %s\n",
    359                                 (unsigned int) yprec,
    360                                 mpfr_print_rnd_mode (rnd));
    361                         printf ("Expected flags =");
    362                         flags_out (ex_flags);
    363                         printf ("     got flags =");
    364                         flags_out (flags);
    365                         printf ("inex = %d, w = ", inexact);
    366                         mpfr_dump (w);
    367                         exit (1);
    368                       }
    369                     test_of = 0;  /* Overflow is tested only once. */
    370                   }
    371                 if (test_uf && e + 1 <= emin)
    372                   {
    373                     unsigned int ex_flags;
    374 
    375                     mpfr_set_emin (e + 1);
    376                     mpfr_clear_flags ();
    377 #if defined(TWO_ARGS)
    378                     inexact = TEST_FUNCTION (w, x, u, rnd);
    379 #elif defined(DOUBLE_ARG1)
    380                     inexact = TEST_FUNCTION (w, d, x, rnd);
    381 #elif defined(DOUBLE_ARG2)
    382                     inexact = TEST_FUNCTION (w, x, d, rnd);
    383 #elif defined(ULONG_ARG1)
    384                     inexact = TEST_FUNCTION (w, i, x, rnd);
    385 #elif defined(ULONG_ARG2)
    386                     inexact = TEST_FUNCTION (w, x, i, rnd);
    387 #else
    388                     inexact = TEST_FUNCTION (w, x, rnd);
    389 #endif
    390                     flags = __gmpfr_flags;
    391                     mpfr_set_emin (oemin);
    392                     ex_flags = MPFR_FLAGS_UNDERFLOW | MPFR_FLAGS_INEXACT;
    393                     if (flags != ex_flags)
    394                       {
    395                         printf ("tgeneric: error for " MAKE_STR(TEST_FUNCTION)
    396                                 ", reduced exponent range [%"
    397                                 MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC
    398                                 "d] (underflow test) on:\n",
    399                                 (mpfr_eexp_t) e + 1, (mpfr_eexp_t) oemax);
    400                         printf ("x = ");
    401                         mpfr_dump (x);
    402 #if defined(TWO_ARGS_ALL)
    403                         printf ("u = ");
    404                         mpfr_dump (u);
    405 #endif
    406                         printf ("yprec = %u, rnd_mode = %s\n",
    407                                 (unsigned int) yprec,
    408                                 mpfr_print_rnd_mode (rnd));
    409                         printf ("Expected flags =");
    410                         flags_out (ex_flags);
    411                         printf ("     got flags =");
    412                         flags_out (flags);
    413                         printf ("inex = %d, w = ", inexact);
    414                         mpfr_dump (w);
    415                         exit (1);
    416                       }
    417                     test_uf = 0;  /* Underflow is tested only once. */
    418                   }
    419                 if (e < emin)
    420                   emin = e;
    421                 if (e > emax)
    422                   emax = e;
    423               }
    424             if (emin > emax)
    425               emin = emax;  /* case where all values are singular */
    426             /* Consistency test in a reduced exponent range. Doing it
    427                for the first 10 samples and for prec == p1 (which has
    428                some special cases) should be sufficient. */
    429             if (ctrt <= 10 || prec == p1)
    430               {
    431                 mpfr_set_emin (emin);
    432                 mpfr_set_emax (emax);
    433 #ifdef DEBUG_TGENERIC
    434                 /* Useful information in case of assertion failure. */
    435                 printf ("tgeneric: reduced exponent range [%"
    436                         MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC "d]\n",
    437                         (mpfr_eexp_t) emin, (mpfr_eexp_t) emax);
    438 #endif
    439                 mpfr_clear_flags ();
    440 #if defined(TWO_ARGS)
    441                 inexact = TEST_FUNCTION (w, x, u, rnd);
    442 #elif defined(DOUBLE_ARG1)
    443                 inexact = TEST_FUNCTION (w, d, x, rnd);
    444 #elif defined(DOUBLE_ARG2)
    445                 inexact = TEST_FUNCTION (w, x, d, rnd);
    446 #elif defined(ULONG_ARG1)
    447                 inexact = TEST_FUNCTION (w, i, x, rnd);
    448 #elif defined(ULONG_ARG2)
    449                 inexact = TEST_FUNCTION (w, x, i, rnd);
    450 #else
    451                 inexact = TEST_FUNCTION (w, x, rnd);
    452 #endif
    453                 flags = __gmpfr_flags;
    454                 mpfr_set_emin (oemin);
    455                 mpfr_set_emax (oemax);
    456                 if (! (SAME_VAL (w, y) &&
    457                        SAME_SIGN (inexact, compare) &&
    458                        flags == oldflags))
    459                   {
    460                     printf ("tgeneric: error for " MAKE_STR(TEST_FUNCTION)
    461                             ", reduced exponent range [%"
    462                             MPFR_EXP_FSPEC "d,%" MPFR_EXP_FSPEC "d] on:\n",
    463                             (mpfr_eexp_t) emin, (mpfr_eexp_t) emax);
    464                     printf ("x = ");
    465                     mpfr_dump (x);
    466 #if defined(TWO_ARGS_ALL)
    467                     printf ("u = ");
    468                     mpfr_dump (u);
    469 #endif
    470                     printf ("yprec = %u, rnd_mode = %s\n",
    471                             (unsigned int) yprec, mpfr_print_rnd_mode (rnd));
    472                     printf ("Expected:\n  y = ");
    473                     mpfr_dump (y);
    474                     printf ("  inex = %d, flags =", compare);
    475                     flags_out (oldflags);
    476                     printf ("Got:\n  w = ");
    477                     mpfr_dump (w);
    478                     printf ("  inex = %d, flags =", inexact);
    479                     flags_out (flags);
    480                     exit (1);
    481                   }
    482               }
    483             __gmpfr_flags = oldflags;  /* restore the flags */
    484           }
    485 
    486           if (MPFR_IS_SINGULAR (y))
    487             {
    488               if (MPFR_IS_NAN (y) || mpfr_nanflag_p ())
    489                 TGENERIC_CHECK ("bad NaN flag",
    490                                 MPFR_IS_NAN (y) && mpfr_nanflag_p ());
    491               else if (MPFR_IS_INF (y))
    492                 {
    493                   TGENERIC_CHECK ("bad overflow flag",
    494                                   (compare != 0) ^ (mpfr_overflow_p () == 0));
    495                   TGENERIC_CHECK ("bad divide-by-zero flag",
    496                                   (compare == 0 && !infinite_input) ^
    497                                   (mpfr_divby0_p () == 0));
    498                 }
    499               else if (MPFR_IS_ZERO (y))
    500                 TGENERIC_CHECK ("bad underflow flag",
    501                                 (compare != 0) ^ (mpfr_underflow_p () == 0));
    502             }
    503           else if (mpfr_divby0_p ())
    504             {
    505               TGENERIC_CHECK ("both overflow and divide-by-zero",
    506                               ! mpfr_overflow_p ());
    507               TGENERIC_CHECK ("both underflow and divide-by-zero",
    508                               ! mpfr_underflow_p ());
    509               TGENERIC_CHECK ("bad compare value (divide-by-zero)",
    510                               compare == 0);
    511             }
    512           else if (mpfr_overflow_p ())
    513             {
    514               TGENERIC_CHECK ("both underflow and overflow",
    515                               ! mpfr_underflow_p ());
    516               TGENERIC_CHECK ("bad compare value (overflow)", compare != 0);
    517               mpfr_nexttoinf (y);
    518               TGENERIC_CHECK ("should have been max MPFR number (overflow)",
    519                               MPFR_IS_INF (y));
    520             }
    521           else if (mpfr_underflow_p ())
    522             {
    523               TGENERIC_CHECK ("bad compare value (underflow)", compare != 0);
    524               mpfr_nexttozero (y);
    525               TGENERIC_CHECK ("should have been min MPFR number (underflow)",
    526                               MPFR_IS_ZERO (y));
    527             }
    528           else if (mpfr_can_round (y, yprec, rnd, rnd, prec))
    529             {
    530               ctrn++;
    531               mpfr_set (t, y, rnd);
    532               /* Risk of failures are known when some flags are already set
    533                  before the function call. Do not set the erange flag, as
    534                  it will remain set after the function call and no checks
    535                  are performed in such a case (see the mpfr_erangeflag_p
    536                  test below). */
    537               if (randlimb () & 1)
    538                 __gmpfr_flags = MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE;
    539 #ifdef DEBUG_TGENERIC
    540               TGENERIC_INFO (TEST_FUNCTION, MPFR_PREC (z));
    541 #endif
    542               /* Let's increase the precision of the inputs in a random way.
    543                  In most cases, this doesn't make any difference, but for
    544                  the mpfr_fmod bug fixed in r6230, this triggers the bug. */
    545               mpfr_prec_round (x, mpfr_get_prec (x) + (randlimb () & 15),
    546                                MPFR_RNDN);
    547 #if defined(TWO_ARGS)
    548               mpfr_prec_round (u, mpfr_get_prec (u) + (randlimb () & 15),
    549                                MPFR_RNDN);
    550               inexact = TEST_FUNCTION (z, x, u, rnd);
    551 #elif defined(DOUBLE_ARG1)
    552               inexact = TEST_FUNCTION (z, d, x, rnd);
    553 #elif defined(DOUBLE_ARG2)
    554               inexact = TEST_FUNCTION (z, x, d, rnd);
    555 #elif defined(ULONG_ARG1)
    556               inexact = TEST_FUNCTION (z, i, x, rnd);
    557 #elif defined(ULONG_ARG2)
    558               inexact = TEST_FUNCTION (z, x, i, rnd);
    559 #else
    560               inexact = TEST_FUNCTION (z, x, rnd);
    561 #endif
    562               if (mpfr_erangeflag_p ())
    563                 goto next_n;
    564               if (! mpfr_equal_p (t, z))
    565                 {
    566                   printf ("tgeneric: results differ for "
    567                           MAKE_STR(TEST_FUNCTION) " on\n  x = ");
    568                   mpfr_dump (x);
    569 #if defined(TWO_ARGS_ALL)
    570                   printf ("  u = ");
    571                   mpfr_dump (u);
    572 #endif
    573                   printf ("  prec = %u, rnd_mode = %s\n",
    574                           (unsigned int) prec, mpfr_print_rnd_mode (rnd));
    575                   printf ("Got      ");
    576                   mpfr_dump (z);
    577                   printf ("Expected ");
    578                   mpfr_dump (t);
    579                   printf ("Approx   ");
    580                   mpfr_dump (y);
    581                   exit (1);
    582                 }
    583               compare2 = mpfr_cmp (t, y);
    584               /* if rounding to nearest, cannot know the sign of t - f(x)
    585                  because of composed rounding: y = o(f(x)) and t = o(y) */
    586               if (compare * compare2 >= 0)
    587                 compare = compare + compare2;
    588               else
    589                 compare = inexact; /* cannot determine sign(t-f(x)) */
    590               if (! SAME_SIGN (inexact, compare))
    591                 {
    592                   printf ("Wrong inexact flag for rnd=%s: expected %d, got %d"
    593                           "\n", mpfr_print_rnd_mode (rnd), compare, inexact);
    594                   printf ("x = ");
    595                   mpfr_dump (x);
    596 #if defined(TWO_ARGS_ALL)
    597                   printf ("u = ");
    598                   mpfr_dump (u);
    599 #endif
    600                   printf ("y = ");
    601                   mpfr_dump (y);
    602                   printf ("t = ");
    603                   mpfr_dump (t);
    604                   exit (1);
    605                 }
    606             }
    607           else if (getenv ("MPFR_SUSPICIOUS_OVERFLOW") != NULL)
    608             {
    609               /* For developers only! */
    610               MPFR_ASSERTN (MPFR_IS_PURE_FP (y));
    611               mpfr_nexttoinf (y);
    612               if (MPFR_IS_INF (y) && MPFR_IS_LIKE_RNDZ (rnd, MPFR_IS_NEG (y))
    613                   && !mpfr_overflow_p () && TGENERIC_SO_TEST)
    614                 {
    615                   printf ("Possible bug! |y| is the maximum finite number "
    616                           "and has been obtained when\nrounding toward zero"
    617                           " (%s). Thus there is a very probable overflow,\n"
    618                           "but the overflow flag is not set!\n",
    619                           mpfr_print_rnd_mode (rnd));
    620                   printf ("x = ");
    621                   mpfr_dump (x);
    622 #if defined(TWO_ARGS_ALL)
    623                   printf ("u = ");
    624                   mpfr_dump (u);
    625 #endif
    626                   exit (1);
    627                 }
    628             }
    629 
    630         next_n:
    631           /* In case the exponent range has been changed by
    632              tests_default_random() or for special values... */
    633           mpfr_set_emin (old_emin);
    634           mpfr_set_emax (old_emax);
    635         }
    636     }
    637 
    638 #ifndef TGENERIC_NOWARNING
    639   if (3 * ctrn < 2 * ctrt)
    640     printf ("Warning! Too few normal cases in generic tests (%lu / %lu)\n",
    641             ctrn, ctrt);
    642 #endif
    643 
    644   mpfr_clears (x, y, z, t, w, (mpfr_ptr) 0);
    645 #if defined(TWO_ARGS_ALL)
    646   mpfr_clear (u);
    647 #endif
    648 }
    649 
    650 #undef TEST_RANDOM_POS
    651 #undef TEST_RANDOM_POS2
    652 #undef TEST_RANDOM_EMIN
    653 #undef TEST_RANDOM_EMAX
    654 #undef TEST_RANDOM_ALWAYS_SCALE
    655 #undef RAND_FUNCTION
    656 #undef TWO_ARGS
    657 #undef TWO_ARGS_ALL
    658 #undef DOUBLE_ARG1
    659 #undef DOUBLE_ARG2
    660 #undef ULONG_ARG1
    661 #undef ULONG_ARG2
    662 #undef TEST_FUNCTION
    663 #undef test_generic
    664