github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpf/t-get_d_2exp.c (about)

     1  /* Test mpf_get_d_2exp.
     2  
     3  Copyright 2002, 2003 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library test suite.
     6  
     7  The GNU MP Library test suite is free software; you can redistribute it
     8  and/or modify it under the terms of the GNU General Public License as
     9  published by the Free Software Foundation; either version 3 of the License,
    10  or (at your option) any later version.
    11  
    12  The GNU MP Library test suite is distributed in the hope that it will be
    13  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    15  Public License for more details.
    16  
    17  You should have received a copy of the GNU General Public License along with
    18  the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
    19  
    20  #include <stdio.h>
    21  #include <stdlib.h>
    22  #include "gmp.h"
    23  #include "gmp-impl.h"
    24  #include "tests.h"
    25  
    26  
    27  static void
    28  check_onebit (void)
    29  {
    30    static const long data[] = {
    31      -513, -512, -511, -65, -64, -63, -32, -1,
    32      0, 1, 32, 53, 54, 64, 128, 256, 511, 512, 513
    33    };
    34    mpf_t   f;
    35    double  got, want;
    36    long    got_exp, want_exp;
    37    int     i;
    38  
    39    mpf_init2 (f, 1024L);
    40  
    41    for (i = 0; i < numberof (data); i++)
    42      {
    43        mpf_set_ui (f, 1L);
    44        if (data[i] >= 0)
    45          mpf_mul_2exp (f, f, data[i]);
    46        else
    47          mpf_div_2exp (f, f, -data[i]);
    48        want = 0.5;
    49        want_exp = data[i] + 1;
    50  
    51        got = mpf_get_d_2exp (&got_exp, f);
    52        if (got != want || got_exp != want_exp)
    53          {
    54            printf    ("mpf_get_d_2exp wrong on 2**%ld\n", data[i]);
    55            mpf_trace ("   f    ", f);
    56            d_trace   ("   want ", want);
    57            d_trace   ("   got  ", got);
    58            printf    ("   want exp %ld\n", want_exp);
    59            printf    ("   got exp  %ld\n", got_exp);
    60            abort();
    61          }
    62      }
    63    mpf_clear (f);
    64  }
    65  
    66  /* Check that hardware rounding doesn't make mpf_get_d_2exp return a value
    67     outside its defined range. */
    68  static void
    69  check_round (void)
    70  {
    71    static const unsigned long data[] = { 1, 32, 53, 54, 64, 128, 256, 512 };
    72    mpf_t   f;
    73    double  got;
    74    long    got_exp;
    75    int     i, rnd_mode, old_rnd_mode;
    76  
    77    mpf_init2 (f, 1024L);
    78    old_rnd_mode = tests_hardware_getround ();
    79  
    80    for (rnd_mode = 0; rnd_mode < 4; rnd_mode++)
    81      {
    82        tests_hardware_setround (rnd_mode);
    83  
    84        for (i = 0; i < numberof (data); i++)
    85          {
    86            mpf_set_ui (f, 1L);
    87            mpf_mul_2exp (f, f, data[i]);
    88            mpf_sub_ui (f, f, 1L);
    89  
    90            got = mpf_get_d_2exp (&got_exp, f);
    91            if (got < 0.5 || got >= 1.0)
    92              {
    93                printf    ("mpf_get_d_2exp bad on 2**%lu-1\n", data[i]);
    94                printf    ("result out of range, expect 0.5 <= got < 1.0\n");
    95                printf    ("   rnd_mode = %d\n", rnd_mode);
    96                printf    ("   data[i]  = %lu\n", data[i]);
    97                mpf_trace ("   f    ", f);
    98                d_trace   ("   got  ", got);
    99                printf    ("   got exp  %ld\n", got_exp);
   100                abort();
   101              }
   102          }
   103      }
   104  
   105    mpf_clear (f);
   106    tests_hardware_setround (old_rnd_mode);
   107  }
   108  
   109  
   110  int
   111  main (void)
   112  {
   113    tests_start ();
   114    mp_trace_base = 16;
   115  
   116    check_onebit ();
   117    check_round ();
   118  
   119    tests_end ();
   120    exit (0);
   121  }