"Fossies" - the Fresh Open Source Software Archive

Member "mpfr-4.0.2/tests/tget_sj.c" (7 Jan 2019, 12337 Bytes) of package /linux/misc/mpfr-4.0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "tget_sj.c": 4.0.1_vs_4.0.2.

    1 /* Test file for mpfr_get_sj and mpfr_get_uj.
    2 
    3 Copyright 2004-2019 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 https://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 /* Needed due to the inclusion of mpfr-intmax.h */
   24 #ifdef HAVE_CONFIG_H
   25 # include "config.h"
   26 #endif
   27 
   28 #include "mpfr-intmax.h"
   29 #include "mpfr-test.h"
   30 
   31 #ifndef _MPFR_H_HAVE_INTMAX_T
   32 
   33 int
   34 main (void)
   35 {
   36   return 77;
   37 }
   38 
   39 #else
   40 
   41 #ifdef MPFR_PRINTF_MAXLM
   42 #define PRMAX(SPEC,V) printf (" %" MPFR_PRINTF_MAXLM SPEC ",", V)
   43 #else
   44 #define PRMAX(SPEC,V) (void) 0
   45 #endif
   46 
   47 static void
   48 check_sj (intmax_t s, mpfr_ptr x)
   49 {
   50   mpfr_exp_t emin, emax;
   51   mpfr_t y;
   52   int i;
   53 
   54   mpfr_init2 (y, MPFR_PREC (x) + 2);
   55 
   56   emin = mpfr_get_emin ();
   57   emax = mpfr_get_emax ();
   58 
   59   for (i = -1; i <= 1; i++)
   60     {
   61       int rnd;
   62       int inex;
   63       int fi, e;
   64       mpfr_flags_t flags[2] = { 0, MPFR_FLAGS_ALL }, ex_flags, gt_flags;
   65 
   66       inex = mpfr_set_si_2exp (y, i, -2, MPFR_RNDN);
   67       MPFR_ASSERTN (inex == 0);
   68       inex = mpfr_add (y, y, x, MPFR_RNDN);
   69       MPFR_ASSERTN (inex == 0);
   70       /* y = x + i/4, with -1 <= i <= 1 */
   71       RND_LOOP (rnd)
   72         for (fi = 0; fi < numberof (flags); fi++)
   73           {
   74             intmax_t r;
   75 
   76             if (rnd == MPFR_RNDZ && i < 0 && s >= 0)
   77               continue;
   78             if (rnd == MPFR_RNDZ && i > 0 && s <= 0)
   79               continue;
   80             if (rnd == MPFR_RNDD && i < 0)
   81               continue;
   82             if (rnd == MPFR_RNDU && i > 0)
   83               continue;
   84             if (rnd == MPFR_RNDA && ((MPFR_IS_POS(y) && i > 0) ||
   85                                      (MPFR_IS_NEG(y) && i < 0)))
   86               continue;
   87 
   88             for (e = 0; e < 2; e++)
   89               {
   90                 if (e)
   91                   {
   92                     mpfr_exp_t ey;
   93 
   94                     if (MPFR_IS_ZERO (y))
   95                       break;
   96                     ey = MPFR_GET_EXP (y);
   97                     set_emin (ey);
   98                     set_emax (ey);
   99                   }
  100                 /* rint (y) == x == s */
  101                 __gmpfr_flags = ex_flags = flags[fi];
  102                 if (i != 0)
  103                   ex_flags |= MPFR_FLAGS_INEXACT;
  104                 r = mpfr_get_sj (y, (mpfr_rnd_t) rnd);
  105                 gt_flags = __gmpfr_flags;
  106                 set_emin (emin);
  107                 set_emax (emax);
  108                 if ((r != s || gt_flags != ex_flags) && rnd != MPFR_RNDF)
  109                   {
  110                     printf ("Error in check_sj for fi = %d, y = ", fi);
  111                     mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
  112                     printf (" in %s%s\n",
  113                             mpfr_print_rnd_mode ((mpfr_rnd_t) rnd),
  114                             e ? ", reduced exponent range" : "");
  115                     printf ("Expected:");
  116                     PRMAX ("d", s);
  117                     flags_out (ex_flags);
  118                     printf ("Got:     ");
  119                     PRMAX ("d", r);
  120                     flags_out (gt_flags);
  121                     exit (1);
  122                   }
  123               }
  124           }
  125     }
  126 
  127   mpfr_clear (y);
  128 }
  129 
  130 static void
  131 check_uj (uintmax_t u, mpfr_ptr x)
  132 {
  133   mpfr_exp_t emin, emax;
  134   mpfr_t y;
  135   int i;
  136 
  137   mpfr_init2 (y, MPFR_PREC (x) + 2);
  138 
  139   emin = mpfr_get_emin ();
  140   emax = mpfr_get_emax ();
  141 
  142   for (i = -1; i <= 1; i++)
  143     {
  144       int rnd;
  145       int inex;
  146       int fi, e;
  147       mpfr_flags_t flags[2] = { 0, MPFR_FLAGS_ALL }, ex_flags, gt_flags;
  148 
  149       inex = mpfr_set_si_2exp (y, i, -2, MPFR_RNDN);
  150       MPFR_ASSERTN (inex == 0);
  151       inex = mpfr_add (y, y, x, MPFR_RNDN);
  152       MPFR_ASSERTN (inex == 0);
  153       /* y = x + i/4, with -1 <= i <= 1 */
  154       RND_LOOP (rnd)
  155         for (fi = 0; fi < numberof (flags); fi++)
  156           {
  157             uintmax_t r;
  158 
  159             if (rnd == MPFR_RNDZ && i < 0)
  160               continue;
  161             if (rnd == MPFR_RNDD && i < 0)
  162               continue;
  163             if (rnd == MPFR_RNDU && i > 0)
  164               continue;
  165             if (rnd == MPFR_RNDA && ((MPFR_IS_POS(y) && i > 0) ||
  166                                      (MPFR_IS_NEG(y) && i < 0)))
  167               continue;
  168 
  169             for (e = 0; e < 2; e++)
  170               {
  171                 if (e)
  172                   {
  173                     mpfr_exp_t ey;
  174 
  175                     if (MPFR_IS_ZERO (y))
  176                       break;
  177                     ey = MPFR_GET_EXP (y);
  178                     set_emin (ey);
  179                     set_emax (ey);
  180                   }
  181                 /* rint (y) == x == u */
  182                 __gmpfr_flags = ex_flags = flags[fi];
  183                 if (i != 0)
  184                   ex_flags |= MPFR_FLAGS_INEXACT;
  185                 r = mpfr_get_uj (y, (mpfr_rnd_t) rnd);
  186                 gt_flags = __gmpfr_flags;
  187                 set_emin (emin);
  188                 set_emax (emax);
  189                 if ((r != u || gt_flags != ex_flags) && rnd != MPFR_RNDF)
  190                   {
  191                     printf ("Error in check_uj for fi = %d, y = ", fi);
  192                     mpfr_out_str (stdout, 2, 0, y, MPFR_RNDN);
  193                     printf (" in %s%s\n",
  194                             mpfr_print_rnd_mode ((mpfr_rnd_t) rnd),
  195                             e ? ", reduced exponent range" : "");
  196                     printf ("Expected:");
  197                     PRMAX ("u", u);
  198                     flags_out (ex_flags);
  199                     printf ("Got:     ");
  200                     PRMAX ("u", r);
  201                     flags_out (gt_flags);
  202                     exit (1);
  203                   }
  204               }
  205           }
  206     }
  207 
  208   mpfr_clear (y);
  209 }
  210 
  211 #define CHECK_ERANGE(F,FMT,RES,INPUT,VALUE,E)                           \
  212   do                                                                    \
  213     {                                                                   \
  214       __gmpfr_flags = ex_flags = flags[fi];                             \
  215       RES = F (x, (mpfr_rnd_t) rnd);                                    \
  216       gt_flags = __gmpfr_flags;                                         \
  217       if (E)                                                            \
  218         ex_flags |= MPFR_FLAGS_ERANGE;                                  \
  219       if (RES == VALUE && gt_flags == ex_flags)                         \
  220         continue;                                                       \
  221       printf ("Error in check_erange for %s, %s, fi = %d on %s\n",      \
  222               #F, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd), fi, INPUT);   \
  223       printf ("Expected:");                                             \
  224       PRMAX (FMT, VALUE);                                               \
  225       flags_out (ex_flags);                                             \
  226       printf ("Got:     ");                                             \
  227       PRMAX (FMT, RES);                                                 \
  228       flags_out (gt_flags);                                             \
  229       exit (1);                                                         \
  230     }                                                                   \
  231   while (0)
  232 
  233 #define CHECK_ERANGE_U(INPUT,VALUE,E) \
  234   CHECK_ERANGE (mpfr_get_uj, "u", u, INPUT, (uintmax_t) VALUE, E)
  235 #define CHECK_ERANGE_S(INPUT,VALUE,E) \
  236   CHECK_ERANGE (mpfr_get_sj, "d", d, INPUT, (intmax_t) VALUE, E)
  237 
  238 static void
  239 check_erange (void)
  240 {
  241   mpfr_t x;
  242   uintmax_t u;
  243   intmax_t d;
  244   int rnd;
  245   int fi;
  246   mpfr_flags_t flags[3] = { 0, MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE,
  247                             MPFR_FLAGS_ALL }, ex_flags, gt_flags;
  248 
  249   /* Test for ERANGE flag + correct behavior if overflow */
  250 
  251   mpfr_init2 (x, 256);
  252 
  253   RND_LOOP (rnd)
  254     for (fi = 0; fi < numberof (flags); fi++)
  255       {
  256         mpfr_set_uj (x, MPFR_UINTMAX_MAX, MPFR_RNDN);
  257         CHECK_ERANGE_U ("UINTMAX_MAX", MPFR_UINTMAX_MAX, 0);
  258         mpfr_add_ui (x, x, 1, MPFR_RNDN);
  259         CHECK_ERANGE_U ("UINTMAX_MAX+1", MPFR_UINTMAX_MAX, 1);
  260         mpfr_set_sj (x, -1, MPFR_RNDN);
  261         CHECK_ERANGE_U ("-1", 0, 1);
  262         mpfr_set_sj (x, MPFR_INTMAX_MAX, MPFR_RNDN);
  263         CHECK_ERANGE_S ("INTMAX_MAX", MPFR_INTMAX_MAX, 0);
  264         mpfr_add_ui (x, x, 1, MPFR_RNDN);
  265         CHECK_ERANGE_S ("INTMAX_MAX+1", MPFR_INTMAX_MAX, 1);
  266         mpfr_set_sj (x, MPFR_INTMAX_MIN, MPFR_RNDN);
  267         CHECK_ERANGE_S ("INTMAX_MIN", MPFR_INTMAX_MIN, 0);
  268         mpfr_sub_ui (x, x, 1, MPFR_RNDN);
  269         CHECK_ERANGE_S ("INTMAX_MIN-1", MPFR_INTMAX_MIN, 1);
  270         mpfr_set_nan (x);
  271         CHECK_ERANGE_U ("NaN", 0, 1);
  272         CHECK_ERANGE_S ("NaN", 0, 1);
  273       }
  274 
  275   mpfr_clear (x);
  276 }
  277 
  278 static void
  279 test_get_uj_smallneg (void)
  280 {
  281   mpfr_t x;
  282   int i;
  283 
  284   mpfr_init2 (x, 64);
  285 
  286   for (i = 1; i <= 4; i++)
  287     {
  288       int r;
  289 
  290       mpfr_set_si_2exp (x, -i, -2, MPFR_RNDN);
  291       RND_LOOP (r)
  292         {
  293           intmax_t s;
  294           uintmax_t u;
  295 
  296           mpfr_clear_erangeflag ();
  297           s = mpfr_get_sj (x, r != MPFR_RNDF ? (mpfr_rnd_t) r : MPFR_RNDA);
  298           if (mpfr_erangeflag_p ())
  299             {
  300               printf ("ERROR for get_sj + ERANGE + small negative op"
  301                       " for rnd = %s and x = -%d/4\n",
  302                       mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
  303               exit (1);
  304             }
  305           u = mpfr_get_uj (x, (mpfr_rnd_t) r);
  306           if (u != 0)
  307             {
  308               printf ("ERROR for get_uj + ERANGE + small negative op"
  309                       " for rnd = %s and x = -%d/4\n",
  310                       mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
  311 #ifdef MPFR_PRINTF_MAXLM
  312               printf ("Expected 0, got %" MPFR_PRINTF_MAXLM "u\n", u);
  313 #endif
  314               exit (1);
  315             }
  316           if ((s == 0) ^ !mpfr_erangeflag_p ())
  317             {
  318               const char *Not = s == 0 ? "" : " not";
  319 
  320               printf ("ERROR for get_uj + ERANGE + small negative op"
  321                       " for rnd = %s and x = -%d/4\n",
  322                       mpfr_print_rnd_mode ((mpfr_rnd_t) r), i);
  323               printf ("The rounded integer ");
  324 #ifdef MPFR_PRINTF_MAXLM
  325               printf("(%" MPFR_PRINTF_MAXLM "d) ", s);
  326 #endif
  327               printf("is%s representable in unsigned long,\n"
  328                      "but the erange flag is%s set.\n", Not, Not);
  329               exit (1);
  330             }
  331         }
  332     }
  333 
  334   mpfr_clear (x);
  335 }
  336 
  337 int
  338 main (void)
  339 {
  340   mpfr_prec_t prec;
  341   mpfr_t x, y;
  342   intmax_t s;
  343   uintmax_t u;
  344 
  345   tests_start_mpfr ();
  346 
  347   for (u = MPFR_UINTMAX_MAX, prec = 0; u != 0; u /= 2, prec++)
  348     { }
  349 
  350   mpfr_init2 (x, prec + 4);
  351   mpfr_init2 (y, prec + 4);
  352 
  353   mpfr_set_ui (x, 0, MPFR_RNDN);
  354   check_sj (0, x);
  355   check_uj (0, x);
  356 
  357   mpfr_set_ui (x, 1, MPFR_RNDN);
  358   check_sj (1, x);
  359   check_uj (1, x);
  360 
  361   mpfr_neg (x, x, MPFR_RNDN);
  362   check_sj (-1, x);
  363 
  364   mpfr_set_si_2exp (x, 1, prec, MPFR_RNDN);
  365   mpfr_sub_ui (x, x, 1, MPFR_RNDN); /* UINTMAX_MAX */
  366 
  367   mpfr_div_ui (y, x, 2, MPFR_RNDZ);
  368   mpfr_trunc (y, y); /* INTMAX_MAX */
  369   for (s = MPFR_INTMAX_MAX; s != 0; s /= 17)
  370     {
  371       check_sj (s, y);
  372       mpfr_div_ui (y, y, 17, MPFR_RNDZ);
  373       mpfr_trunc (y, y);
  374     }
  375 
  376   mpfr_div_ui (y, x, 2, MPFR_RNDZ);
  377   mpfr_trunc (y, y); /* INTMAX_MAX */
  378   mpfr_neg (y, y, MPFR_RNDN);
  379   if (MPFR_INTMAX_MIN + MPFR_INTMAX_MAX != 0)
  380     mpfr_sub_ui (y, y, 1, MPFR_RNDN); /* INTMAX_MIN */
  381   for (s = MPFR_INTMAX_MIN; s != 0; s /= 17)
  382     {
  383       check_sj (s, y);
  384       mpfr_div_ui (y, y, 17, MPFR_RNDZ);
  385       mpfr_trunc (y, y);
  386     }
  387 
  388   for (u = MPFR_UINTMAX_MAX; u != 0; u /= 17)
  389     {
  390       check_uj (u, x);
  391       mpfr_div_ui (x, x, 17, MPFR_RNDZ);
  392       mpfr_trunc (x, x);
  393     }
  394 
  395   mpfr_clear (x);
  396   mpfr_clear (y);
  397 
  398   check_erange ();
  399   test_get_uj_smallneg ();
  400 
  401   tests_end_mpfr ();
  402   return 0;
  403 }
  404 
  405 #endif