Home | History | Annotate | Line # | Download | only in tests
tset_si.c revision 1.1.1.3.4.1
      1 /* Test file for mpfr_set_si, mpfr_set_ui, mpfr_get_si and mpfr_get_ui.
      2 
      3 Copyright 1999, 2001-2018 Free Software Foundation, Inc.
      4 Contributed by the AriC and Caramba projects, INRIA.
      5 
      6 This file is part of the GNU MPFR Library.
      7 
      8 The GNU MPFR Library is free software; you can redistribute it and/or modify
      9 it under the terms of the GNU Lesser General Public License as published by
     10 the Free Software Foundation; either version 3 of the License, or (at your
     11 option) any later version.
     12 
     13 The GNU MPFR Library is distributed in the hope that it will be useful, but
     14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     16 License for more details.
     17 
     18 You should have received a copy of the GNU Lesser General Public License
     19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     22 
     23 #include "mpfr-test.h"
     24 
     25 #define PRINT_ERROR(str) \
     26   do { printf ("Error for %s\n", str); exit (1); } while (0)
     27 
     28 static void
     29 test_2exp (void)
     30 {
     31   mpfr_t x;
     32   int res;
     33 
     34   mpfr_init2 (x, 32);
     35 
     36   mpfr_set_ui_2exp (x, 1, 0, MPFR_RNDN);
     37   if (mpfr_cmp_ui (x, 1) != 0)
     38     PRINT_ERROR ("(1U,0)");
     39 
     40   mpfr_set_ui_2exp (x, 1024, -10, MPFR_RNDN);
     41   if (mpfr_cmp_ui(x, 1) != 0)
     42     PRINT_ERROR ("(1024U,-10)");
     43 
     44   mpfr_set_ui_2exp (x, 1024, 10, MPFR_RNDN);
     45   if (mpfr_cmp_ui (x, 1024 * 1024) != 0)
     46     PRINT_ERROR ("(1024U,+10)");
     47 
     48   mpfr_set_si_2exp (x, -1024L * 1024L, -10, MPFR_RNDN);
     49   if (mpfr_cmp_si (x, -1024) != 0)
     50     PRINT_ERROR ("(1M,-10)");
     51 
     52   mpfr_set_ui_2exp (x, 0x92345678, 16, MPFR_RNDN);
     53   if (mpfr_cmp_str (x, "92345678@4", 16, MPFR_RNDN) != 0)
     54     PRINT_ERROR ("(x92345678U,+16)");
     55 
     56   mpfr_set_si_2exp (x, -0x1ABCDEF0, -256, MPFR_RNDN);
     57   if (mpfr_cmp_str (x, "-1ABCDEF0@-64", 16, MPFR_RNDN) != 0)
     58     PRINT_ERROR ("(-x1ABCDEF0,-256)");
     59 
     60   mpfr_set_prec (x, 2);
     61   res = mpfr_set_si_2exp (x, 7, 10, MPFR_RNDU);
     62   if (mpfr_cmp_ui (x, 1<<13) != 0 || res <= 0)
     63     PRINT_ERROR ("Prec 2 + si_2exp");
     64 
     65   res = mpfr_set_ui_2exp (x, 7, 10, MPFR_RNDU);
     66   if (mpfr_cmp_ui (x, 1<<13) != 0 || res <= 0)
     67     PRINT_ERROR ("Prec 2 + ui_2exp");
     68 
     69   mpfr_clear_flags ();
     70   mpfr_set_ui_2exp (x, 17, MPFR_EMAX_MAX, MPFR_RNDN);
     71   if (!mpfr_inf_p (x) || MPFR_IS_NEG (x))
     72     PRINT_ERROR ("mpfr_set_ui_2exp and overflow (bad result)");
     73   if (!mpfr_overflow_p ())
     74     PRINT_ERROR ("mpfr_set_ui_2exp and overflow (overflow flag not set)");
     75 
     76   mpfr_clear_flags ();
     77   mpfr_set_si_2exp (x, 17, MPFR_EMAX_MAX, MPFR_RNDN);
     78   if (!mpfr_inf_p (x) || MPFR_IS_NEG (x))
     79     PRINT_ERROR ("mpfr_set_si_2exp (pos) and overflow (bad result)");
     80   if (!mpfr_overflow_p ())
     81     PRINT_ERROR ("mpfr_set_si_2exp (pos) and overflow (overflow flag not set)");
     82 
     83   mpfr_clear_flags ();
     84   mpfr_set_si_2exp (x, -17, MPFR_EMAX_MAX, MPFR_RNDN);
     85   if (!mpfr_inf_p (x) || MPFR_IS_POS (x))
     86     PRINT_ERROR ("mpfr_set_si_2exp (neg) and overflow (bad result)");
     87   if (!mpfr_overflow_p ())
     88     PRINT_ERROR ("mpfr_set_si_2exp (neg) and overflow (overflow flag not set)");
     89 
     90   mpfr_clear (x);
     91 }
     92 
     93 static void
     94 test_macros (void)
     95 {
     96   mpfr_t x[3];
     97   mpfr_ptr p;
     98   int r;
     99 
    100   /* Note: the ++'s below allow one to check that the corresponding
    101      arguments are evaluated only once by the macros. */
    102 
    103   mpfr_inits (x[0], x[1], x[2], (mpfr_ptr) 0);
    104   p = x[0];
    105   r = 0;
    106   mpfr_set_ui (p++, 0, (mpfr_rnd_t) r++);
    107   if (p != x[1] || r != 1)
    108     {
    109       printf ("Error in mpfr_set_ui macro: p - x[0] = %d (expecting 1), "
    110               "r = %d (expecting 1)\n", (int) (p - x[0]), r);
    111       exit (1);
    112     }
    113   p = x[0];
    114   r = 0;
    115   mpfr_set_si (p++, 0, (mpfr_rnd_t) r++);
    116   if (p != x[1] || r != 1)
    117     {
    118       printf ("Error in mpfr_set_si macro: p - x[0] = %d (expecting 1), "
    119               "r = %d (expecting 1)\n", (int) (p - x[0]), r);
    120       exit (1);
    121     }
    122   mpfr_clears (x[0], x[1], x[2], (mpfr_ptr) 0);
    123 }
    124 
    125 static void
    126 test_macros_keyword (void)
    127 {
    128   mpfr_t x;
    129   unsigned long i;
    130 
    131   mpfr_init2 (x, 64);
    132 #define MKN 0x1000000
    133 #define long short
    134   mpfr_set_ui (x, MKN, MPFR_RNDN);
    135 #undef long
    136   i = mpfr_get_ui (x, MPFR_RNDN);
    137   if (i != MKN)
    138     {
    139       printf ("Error in test_macros_keyword: expected 0x%lx, got 0x%lx.\n",
    140               (unsigned long) MKN, i);
    141       exit (1);
    142     }
    143   mpfr_clear (x);
    144 }
    145 
    146 static void
    147 test_get_ui_smallneg (void)
    148 {
    149   mpfr_t x;
    150   int i;
    151 
    152   mpfr_init2 (x, 64);
    153 
    154   for (i = 1; i <= 4; i++)
    155     {
    156       int r;
    157 
    158       mpfr_set_si_2exp (x, -i, -2, MPFR_RNDN);
    159       RND_LOOP (r)
    160         {
    161           long s;
    162           unsigned long u;
    163 
    164           mpfr_clear_erangeflag ();
    165           s = mpfr_get_si (x, r != MPFR_RNDF ? (mpfr_rnd_t) r : MPFR_RNDA);
    166           if (mpfr_erangeflag_p ())
    167             {
    168               printf ("ERROR for get_si + ERANGE + small negative op"
    169                       " for rnd = %s and x = -%d/4\n",
    170                       mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
    171               exit (1);
    172             }
    173           u = mpfr_get_ui (x, (mpfr_rnd_t) r);
    174           if (u != 0)
    175             {
    176               printf ("ERROR for get_ui + ERANGE + small negative op"
    177                       " for rnd = %s and x = -%d/4\n",
    178                       mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
    179               printf ("Expected 0, got %lu\n", u);
    180               exit (1);
    181             }
    182           if ((s == 0) ^ !mpfr_erangeflag_p ())
    183             {
    184               const char *Not = s == 0 ? "" : " not";
    185 
    186               printf ("ERROR for get_ui + ERANGE + small negative op"
    187                       " for rnd = %s and x = -%d/4\n",
    188                       mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
    189               printf ("The rounding integer (%ld) is%s representable in "
    190                       "unsigned long,\nbut the erange flag is%s set.\n",
    191                       s, Not, Not);
    192               exit (1);
    193             }
    194         }
    195     }
    196 
    197   mpfr_clear (x);
    198 }
    199 
    200 /* Test mpfr_get_si and mpfr_get_ui, on values around some particular
    201  * integers (see ts[] and tu[]): x = t?[i] + j/4, where '?' is 's' or
    202  * 'u', and j is an integer from -8 to 8.
    203  */
    204 static void get_tests (void)
    205 {
    206   mpfr_exp_t emin, emax;
    207   mpfr_t x, z;
    208   long ts[5] = { LONG_MIN, LONG_MAX, -17, 0, 17 };
    209   unsigned long tu[3] = { 0, ULONG_MAX, 17 };
    210   int s, i, j, odd, ctr = 0;
    211   int inex;
    212   int r;
    213 
    214   emin = mpfr_get_emin ();
    215   emax = mpfr_get_emax ();
    216 
    217   /* We need the bitsize of an unsigned long + 3 bits (1 additional bit for
    218    * the cases >= ULONG_MAX + 1; 2 additional bits for the fractional part).
    219    */
    220   mpfr_init2 (x, sizeof (unsigned long) * CHAR_BIT + 3);
    221 
    222   mpfr_init2 (z, MPFR_PREC_MIN);
    223   mpfr_set_ui_2exp (z, 1, -2, MPFR_RNDN);  /* z = 1/4 */
    224 
    225   for (s = 1; s >= 0; s--)
    226     for (i = 0; i < (s ? 5 : 3); i++)
    227       {
    228         odd = (s ? (unsigned long) ts[i] : tu[i]) & 1;
    229         inex = s ?
    230           mpfr_set_si (x, ts[i], MPFR_RNDN) :
    231           mpfr_set_ui (x, tu[i], MPFR_RNDN);
    232         MPFR_ASSERTN (inex == 0);
    233         inex = mpfr_sub_ui (x, x, 2, MPFR_RNDN);
    234         MPFR_ASSERTN (inex == 0);
    235         for (j = -8; j <= 8; j++)
    236           {
    237             /* Test x = t?[i] + j/4 in each non-RNDF rounding mode... */
    238             RND_LOOP_NO_RNDF (r)
    239               {
    240                 mpfr_flags_t ex_flags, flags;
    241                 int e, k, overflow;
    242 
    243                 ctr++;  /* for the check below */
    244 
    245                 /* Let's determine k such that the rounded integer should
    246                    be t?[i] + k, assuming an unbounded exponent range. */
    247                 k = (j + 8 +
    248                      (MPFR_IS_LIKE_RNDD (r, MPFR_SIGN (x)) ? 0 :
    249                       MPFR_IS_LIKE_RNDU (r, MPFR_SIGN (x)) ? 3 :
    250                       2)) / 4 - 2;
    251                 if (r == MPFR_RNDN && ((unsigned int) j & 3) == 2 &&
    252                     ((odd + k) & 1))
    253                   k--;  /* even rounding */
    254 
    255                 /* Overflow cases. Note that with the above choices:
    256                    _ t?[0] == minval(type)
    257                    _ t?[1] == maxval(type)
    258                 */
    259                 overflow = (i == 0 && k < 0) || (i == 1 && k > 0);
    260 
    261                 /* Expected flags. Note that in case of overflow, only the
    262                    erange flag is set. Otherwise, the result is inexact iff
    263                    j mod 1 != 0, i.e. the last two bits are not 00. */
    264                 ex_flags = overflow ? MPFR_FLAGS_ERANGE
    265                   : ((unsigned int) j & 3) != 0 ? MPFR_FLAGS_INEXACT : 0;
    266 
    267                 mpfr_clear_flags ();
    268 
    269 #define GET_TESTS_TEST(TYPE,TZ,F,C,FMT)                                 \
    270                 do {                                                    \
    271                   TYPE a, d;                                            \
    272                                                                         \
    273                   a = TZ[i] + (overflow ? 0 : k);                       \
    274                   if (e)                                                \
    275                     {                                                   \
    276                       mpfr_exp_t ex;                                    \
    277                       ex = MPFR_GET_EXP (x);                            \
    278                       set_emin (ex);                                    \
    279                       set_emax (ex);                                    \
    280                     }                                                   \
    281                   d = F (x, (mpfr_rnd_t) r);                            \
    282                   flags = __gmpfr_flags;                                \
    283                   set_emin (emin);                                      \
    284                   set_emax (emax);                                      \
    285                   if (flags != ex_flags || a != d)                      \
    286                     {                                                   \
    287                       printf ("Error in get_tests for " #F " on %s%s\n", \
    288                               mpfr_print_rnd_mode ((mpfr_rnd_t) r),     \
    289                               e ? ", reduced exponent range" : "");     \
    290                       printf ("x = t" C "[%d] + (%d/4) = ", i, j);      \
    291                       mpfr_out_str (stdout, 10, 0, x, MPFR_RNDN);       \
    292                       printf ("\n--> k = %d\n", k);                     \
    293                       printf ("Expected %l" FMT "\n", a);               \
    294                       printf ("Got      %l" FMT "\n", d);               \
    295                       printf ("Expected flags:");                       \
    296                       flags_out (ex_flags);                             \
    297                       printf ("Got flags:     ");                       \
    298                       flags_out (flags);                                \
    299                       exit (1);                                         \
    300                     }                                                   \
    301                 } while (0)
    302 
    303                 for (e = 0; e < 2; e++)
    304                   {
    305                     if (e && MPFR_IS_ZERO (x))
    306                       break;
    307                     if (s)
    308                       GET_TESTS_TEST (long,
    309                                       ts, mpfr_get_si, "s", "d");
    310                     else
    311                       GET_TESTS_TEST (unsigned long,
    312                                       tu, mpfr_get_ui, "u", "u");
    313                   }
    314               }
    315             inex = mpfr_add (x, x, z, MPFR_RNDN);
    316             MPFR_ASSERTN (inex == 0);
    317           }
    318       }
    319 
    320   /* Check that we have tested everything: 8 = 5 + 3 integers t?[i]
    321    * with 17 = 8 - (-8) + 1 additional terms (j/4) for each integer,
    322    * and each non-RNDF rounding mode.
    323    */
    324   MPFR_ASSERTN (ctr == 8 * 17 * ((int) MPFR_RND_MAX - 1));
    325 
    326   mpfr_clear (x);
    327   mpfr_clear (z);
    328 }
    329 
    330 /* FIXME: Comparing against mpfr_get_si/ui is not ideal, it'd be better to
    331    have all tests examine the bits in mpfr_t for what should come out.  */
    332 
    333 int
    334 main (int argc, char *argv[])
    335 {
    336   mpfr_t x;
    337   long k, z, d, N;
    338   unsigned long zl, dl;
    339   int inex;
    340   int r;
    341   mpfr_exp_t emin, emax;
    342   int flag;
    343 
    344   tests_start_mpfr ();
    345 
    346   get_tests ();
    347 
    348   mpfr_init2 (x, 100);
    349 
    350   N = (argc == 1) ? 100000 : atol (argv[1]);
    351 
    352   for (k = 1; k <= N; k++)
    353     {
    354       z = (long) (randlimb () & LONG_MAX) + LONG_MIN / 2;
    355       inex = mpfr_set_si (x, z, MPFR_RNDZ);
    356       d = mpfr_get_si (x, MPFR_RNDZ);
    357       if (d != z)
    358         {
    359           printf ("Error in mpfr_set_si: expected %ld got %ld\n", z, d);
    360           exit (1);
    361         }
    362       if (inex)
    363         {
    364           printf ("Error in mpfr_set_si: inex value incorrect for %ld: %d\n",
    365                   z, inex);
    366           exit (1);
    367         }
    368     }
    369 
    370   for (k = 1; k <= N; k++)
    371     {
    372       zl = randlimb ();
    373       inex = mpfr_set_ui (x, zl, MPFR_RNDZ);
    374       dl = mpfr_get_ui (x, MPFR_RNDZ);
    375       if (dl != zl)
    376         {
    377           printf ("Error in mpfr_set_ui: expected %lu got %lu\n", zl, dl);
    378           exit (1);
    379         }
    380       if (inex)
    381         {
    382           printf ("Error in mpfr_set_ui: inex value incorrect for %lu: %d\n",
    383                   zl, inex);
    384           exit (1);
    385         }
    386     }
    387 
    388   mpfr_set_prec (x, 2);
    389   if (mpfr_set_si (x, 5, MPFR_RNDZ) >= 0)
    390     {
    391       printf ("Wrong inexact flag for x=5, rnd=MPFR_RNDZ\n");
    392       exit (1);
    393     }
    394 
    395   mpfr_set_prec (x, 2);
    396   if (mpfr_set_si (x, -5, MPFR_RNDZ) <= 0)
    397     {
    398       printf ("Wrong inexact flag for x=-5, rnd=MPFR_RNDZ\n");
    399       exit (1);
    400     }
    401 
    402   mpfr_set_prec (x, 3);
    403   inex = mpfr_set_si (x, 77617, MPFR_RNDD); /* should be 65536 */
    404   if (MPFR_MANT(x)[0] != MPFR_LIMB_HIGHBIT || inex >= 0)
    405     {
    406       printf ("Error in mpfr_set_si(x:3, 77617, MPFR_RNDD)\n");
    407       mpfr_dump (x);
    408       exit (1);
    409     }
    410   inex = mpfr_set_ui (x, 77617, MPFR_RNDD); /* should be 65536 */
    411   if (MPFR_MANT(x)[0] != MPFR_LIMB_HIGHBIT || inex >= 0)
    412     {
    413       printf ("Error in mpfr_set_ui(x:3, 77617, MPFR_RNDD)\n");
    414       mpfr_dump (x);
    415       exit (1);
    416     }
    417 
    418   mpfr_set_prec (x, 2);
    419   inex = mpfr_set_si (x, 33096, MPFR_RNDU);
    420   if (mpfr_get_si (x, MPFR_RNDZ) != 49152 || inex <= 0)
    421     {
    422       printf ("Error in mpfr_set_si, exp. 49152, got %ld, inex %d\n",
    423               mpfr_get_si (x, MPFR_RNDZ), inex);
    424       exit (1);
    425     }
    426   inex = mpfr_set_ui (x, 33096, MPFR_RNDU);
    427   if (mpfr_get_si (x, MPFR_RNDZ) != 49152)
    428     {
    429       printf ("Error in mpfr_set_ui, exp. 49152, got %ld, inex %d\n",
    430               mpfr_get_si (x, MPFR_RNDZ), inex);
    431       exit (1);
    432     }
    433   /* Also test the mpfr_set_ui function (instead of macro). */
    434   inex = (mpfr_set_ui) (x, 33096, MPFR_RNDU);
    435   if (mpfr_get_si (x, MPFR_RNDZ) != 49152)
    436     {
    437       printf ("Error in mpfr_set_ui function, exp. 49152, got %ld, inex %d\n",
    438               mpfr_get_si (x, MPFR_RNDZ), inex);
    439       exit (1);
    440     }
    441 
    442   for (r = 0 ; r < MPFR_RND_MAX ; r++)
    443     {
    444       mpfr_set_si (x, -1, (mpfr_rnd_t) r);
    445       mpfr_set_ui (x, 0, (mpfr_rnd_t) r);
    446       if (MPFR_IS_NEG (x) || mpfr_get_ui (x, (mpfr_rnd_t) r) != 0)
    447         {
    448           printf ("mpfr_set_ui (x, 0) gives -0 for %s\n",
    449                   mpfr_print_rnd_mode ((mpfr_rnd_t) r));
    450           exit (1);
    451         }
    452 
    453       mpfr_set_si (x, -1, (mpfr_rnd_t) r);
    454       mpfr_set_si (x, 0, (mpfr_rnd_t) r);
    455       if (MPFR_IS_NEG (x) || mpfr_get_si (x, (mpfr_rnd_t) r) != 0)
    456         {
    457           printf ("mpfr_set_si (x, 0) gives -0 for %s\n",
    458                   mpfr_print_rnd_mode ((mpfr_rnd_t) r));
    459           exit (1);
    460         }
    461     }
    462 
    463   /* check potential bug in case mp_limb_t is unsigned */
    464   emax = mpfr_get_emax ();
    465   set_emax (0);
    466   mpfr_set_si (x, -1, MPFR_RNDN);
    467   if (mpfr_sgn (x) >= 0)
    468     {
    469       printf ("mpfr_set_si (x, -1) fails\n");
    470       exit (1);
    471     }
    472   set_emax (emax);
    473 
    474   emax = mpfr_get_emax ();
    475   set_emax (5);
    476   mpfr_set_prec (x, 2);
    477   mpfr_set_si (x, -31, MPFR_RNDN);
    478   if (mpfr_sgn (x) >= 0)
    479     {
    480       printf ("mpfr_set_si (x, -31) fails\n");
    481       exit (1);
    482     }
    483   set_emax (emax);
    484 
    485   /* test for get_ui */
    486   mpfr_set_ui (x, 0, MPFR_RNDN);
    487   MPFR_ASSERTN(mpfr_get_ui (x, MPFR_RNDN) == 0);
    488   mpfr_set_ui (x, ULONG_MAX, MPFR_RNDU);
    489   mpfr_nextabove (x);
    490   mpfr_get_ui (x, MPFR_RNDU);
    491 
    492   /* another test for get_ui */
    493   mpfr_set_prec (x, 10);
    494   mpfr_set_str_binary (x, "10.101");
    495   dl = mpfr_get_ui (x, MPFR_RNDN);
    496   MPFR_ASSERTN (dl == 3);
    497 
    498   mpfr_set_str_binary (x, "-1.0");
    499   mpfr_get_ui (x, MPFR_RNDN);
    500 
    501   mpfr_set_str_binary (x, "0.1");
    502   dl = mpfr_get_ui (x, MPFR_RNDN);
    503   MPFR_ASSERTN (dl == 0);
    504   dl = mpfr_get_ui (x, MPFR_RNDZ);
    505   MPFR_ASSERTN (dl == 0);
    506   dl = mpfr_get_ui (x, MPFR_RNDD);
    507   MPFR_ASSERTN (dl == 0);
    508   dl = mpfr_get_ui (x, MPFR_RNDU);
    509   MPFR_ASSERTN (dl == 1);
    510 
    511   /* coverage tests */
    512   mpfr_set_prec (x, 2);
    513   mpfr_set_si (x, -7, MPFR_RNDD);
    514   MPFR_ASSERTN(mpfr_cmp_si (x, -8) == 0);
    515   mpfr_set_prec (x, 2);
    516   mpfr_set_ui (x, 7, MPFR_RNDU);
    517   MPFR_ASSERTN(mpfr_cmp_ui (x, 8) == 0);
    518   emax = mpfr_get_emax ();
    519   set_emax (3);
    520   mpfr_set_ui (x, 7, MPFR_RNDU);
    521   MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
    522   set_emax (1);
    523   MPFR_ASSERTN( mpfr_set_ui (x, 7, MPFR_RNDU) );
    524   MPFR_ASSERTN(mpfr_inf_p (x) && mpfr_sgn (x) > 0);
    525   set_emax (emax);
    526   mpfr_set_ui_2exp (x, 17, -50, MPFR_RNDN);
    527   MPFR_ASSERTN (mpfr_get_ui (x, MPFR_RNDD) == 0);
    528   MPFR_ASSERTN (mpfr_get_si (x, MPFR_RNDD) == 0);
    529 
    530   /* Test for ERANGE flag + correct behavior if overflow */
    531   mpfr_set_prec (x, 256);
    532   mpfr_set_ui (x, ULONG_MAX, MPFR_RNDN);
    533   mpfr_clear_erangeflag ();
    534   dl = mpfr_get_ui (x, MPFR_RNDN);
    535   if (dl != ULONG_MAX || mpfr_erangeflag_p ())
    536     {
    537       printf ("ERROR for get_ui + ERANGE + ULONG_MAX (1)\n");
    538       exit (1);
    539     }
    540   mpfr_add_ui (x, x, 1, MPFR_RNDN);
    541   dl = mpfr_get_ui (x, MPFR_RNDN);
    542   if (dl != ULONG_MAX || !mpfr_erangeflag_p ())
    543     {
    544       printf ("ERROR for get_ui + ERANGE + ULONG_MAX (2)\n");
    545       exit (1);
    546     }
    547   mpfr_set_si (x, -1, MPFR_RNDN);
    548   mpfr_clear_erangeflag ();
    549   dl = mpfr_get_ui (x, MPFR_RNDN);
    550   if (dl != 0 || !mpfr_erangeflag_p ())
    551     {
    552       printf ("ERROR for get_ui + ERANGE + -1 \n");
    553       exit (1);
    554     }
    555   mpfr_set_si (x, LONG_MAX, MPFR_RNDN);
    556   mpfr_clear_erangeflag ();
    557   d = mpfr_get_si (x, MPFR_RNDN);
    558   if (d != LONG_MAX || mpfr_erangeflag_p ())
    559     {
    560       printf ("ERROR for get_si + ERANGE + LONG_MAX (1): %ld\n", d);
    561       exit (1);
    562     }
    563   mpfr_add_ui (x, x, 1, MPFR_RNDN);
    564   d = mpfr_get_si (x, MPFR_RNDN);
    565   if (d != LONG_MAX || !mpfr_erangeflag_p ())
    566     {
    567       printf ("ERROR for get_si + ERANGE + LONG_MAX (2)\n");
    568       exit (1);
    569     }
    570   mpfr_set_si (x, LONG_MIN, MPFR_RNDN);
    571   mpfr_clear_erangeflag ();
    572   d = mpfr_get_si (x, MPFR_RNDN);
    573   if (d != LONG_MIN || mpfr_erangeflag_p ())
    574     {
    575       printf ("ERROR for get_si + ERANGE + LONG_MIN (1)\n");
    576       exit (1);
    577     }
    578   mpfr_sub_ui (x, x, 1, MPFR_RNDN);
    579   d = mpfr_get_si (x, MPFR_RNDN);
    580   if (d != LONG_MIN || !mpfr_erangeflag_p ())
    581     {
    582       printf ("ERROR for get_si + ERANGE + LONG_MIN (2)\n");
    583       exit (1);
    584     }
    585 
    586   mpfr_set_nan (x);
    587   mpfr_clear_flags ();
    588   d = mpfr_get_ui (x, MPFR_RNDN);
    589   if (d != 0 || __gmpfr_flags != MPFR_FLAGS_ERANGE)
    590     {
    591       printf ("ERROR for get_ui + NaN\n");
    592       exit (1);
    593     }
    594   mpfr_clear_erangeflag ();
    595   d = mpfr_get_si (x, MPFR_RNDN);
    596   if (d != 0 || __gmpfr_flags != MPFR_FLAGS_ERANGE)
    597     {
    598       printf ("ERROR for get_si + NaN\n");
    599       exit (1);
    600     }
    601 
    602   emin = mpfr_get_emin ();
    603   mpfr_set_prec (x, 2);
    604 
    605   mpfr_set_emin (4);
    606   mpfr_clear_flags ();
    607   mpfr_set_ui (x, 7, MPFR_RNDU);
    608   flag = mpfr_underflow_p ();
    609   mpfr_set_emin (emin);
    610   if (mpfr_cmp_ui (x, 8) != 0)
    611     {
    612       printf ("Error for mpfr_set_ui (x, 7, MPFR_RNDU), prec = 2, emin = 4\n");
    613       exit (1);
    614     }
    615   if (flag)
    616     {
    617       printf ("mpfr_set_ui (x, 7, MPFR_RNDU) should not underflow "
    618               "with prec = 2, emin = 4\n");
    619       exit (1);
    620     }
    621 
    622   mpfr_set_emin (4);
    623   mpfr_clear_flags ();
    624   mpfr_set_si (x, -7, MPFR_RNDD);
    625   flag = mpfr_underflow_p ();
    626   mpfr_set_emin (emin);
    627   if (mpfr_cmp_si (x, -8) != 0)
    628     {
    629       printf ("Error for mpfr_set_si (x, -7, MPFR_RNDD), prec = 2, emin = 4\n");
    630       exit (1);
    631     }
    632   if (flag)
    633     {
    634       printf ("mpfr_set_si (x, -7, MPFR_RNDD) should not underflow "
    635               "with prec = 2, emin = 4\n");
    636       exit (1);
    637     }
    638 
    639   mpfr_clear (x);
    640 
    641   test_2exp ();
    642   test_macros ();
    643   test_macros_keyword ();
    644   test_get_ui_smallneg ();
    645   tests_end_mpfr ();
    646   return 0;
    647 }
    648