(root)/
mpfr-4.2.1/
tests/
tlog2p1.c
       1  /* Test file for mpfr_log2p1.
       2  
       3  Copyright 2001-2023 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  #include "mpfr-test.h"
      24  
      25  #define TEST_FUNCTION mpfr_log2p1
      26  #define TEST_RANDOM_EMAX 80
      27  #include "tgeneric.c"
      28  
      29  static void
      30  special (void)
      31  {
      32    mpfr_t x;
      33    int inex;
      34  
      35    mpfr_init2 (x, MPFR_PREC_MIN);
      36  
      37    mpfr_set_nan (x);
      38    mpfr_clear_flags ();
      39    inex = mpfr_log2p1 (x, x, MPFR_RNDN);
      40    MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
      41    MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
      42  
      43    mpfr_set_inf (x, -1);
      44    mpfr_clear_flags ();
      45    inex = mpfr_log2p1 (x, x, MPFR_RNDN);
      46    MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
      47    MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
      48  
      49    mpfr_set_inf (x, 1);
      50    mpfr_clear_flags ();
      51    inex = mpfr_log2p1 (x, x, MPFR_RNDN);
      52    MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) > 0 && inex == 0);
      53    MPFR_ASSERTN (__gmpfr_flags == 0);
      54  
      55    mpfr_set_ui (x, 0, MPFR_RNDN);
      56    mpfr_clear_flags ();
      57    inex = mpfr_log2p1 (x, x, MPFR_RNDN);
      58    MPFR_ASSERTN (mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_POS (x) && inex == 0);
      59    MPFR_ASSERTN (__gmpfr_flags == 0);
      60    mpfr_neg (x, x, MPFR_RNDN);
      61    mpfr_clear_flags ();
      62    inex = mpfr_log2p1 (x, x, MPFR_RNDN);
      63    MPFR_ASSERTN (mpfr_cmp_ui (x, 0) == 0 && MPFR_IS_NEG (x) && inex == 0);
      64    MPFR_ASSERTN (__gmpfr_flags == 0);
      65  
      66    mpfr_set_si (x, -1, MPFR_RNDN);
      67    mpfr_clear_flags ();
      68    inex = mpfr_log2p1 (x, x, MPFR_RNDN);
      69    MPFR_ASSERTN (mpfr_inf_p (x) && mpfr_sgn (x) < 0 && inex == 0);
      70    MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_DIVBY0);
      71  
      72    mpfr_set_si (x, -2, MPFR_RNDN);
      73    mpfr_clear_flags ();
      74    inex = mpfr_log2p1 (x, x, MPFR_RNDN);
      75    MPFR_ASSERTN (mpfr_nan_p (x) && inex == 0);
      76    MPFR_ASSERTN (__gmpfr_flags == MPFR_FLAGS_NAN);
      77  
      78    /* include one hard-coded test */
      79    mpfr_set_prec (x, 32);
      80    mpfr_set_ui (x, 17, MPFR_RNDN);
      81    inex = mpfr_log2p1 (x, x, MPFR_RNDN);
      82    MPFR_ASSERTN (mpfr_cmp_ui_2exp (x, 1119355719UL, -28) == 0);
      83    MPFR_ASSERTN (inex < 0);
      84  
      85    mpfr_clear (x);
      86  }
      87  
      88  /* check exact cases, when 1+x = 2^k */
      89  static void
      90  check_exact (void)
      91  {
      92    mpfr_exp_t k;
      93    mpfr_t x;
      94    int inex, r;
      95  
      96  #define KMAX 100
      97    mpfr_init2 (x, KMAX);
      98    for (k = -KMAX; k <= KMAX; k++)
      99      RND_LOOP (r)
     100        {
     101          mpfr_set_ui_2exp (x, 1, k, (mpfr_rnd_t) r);
     102          inex = mpfr_sub_ui (x, x, 1, (mpfr_rnd_t) r);
     103          MPFR_ASSERTN(inex == 0);
     104          inex = mpfr_log2p1 (x, x, (mpfr_rnd_t) r);
     105          MPFR_ASSERTN(mpfr_cmp_si0 (x, k) == 0);
     106          MPFR_ASSERTN(inex == 0);
     107        }
     108    mpfr_clear (x);
     109  }
     110  
     111  int
     112  main (int argc, char *argv[])
     113  {
     114    tests_start_mpfr ();
     115  
     116    special ();
     117  
     118    check_exact ();
     119  
     120    test_generic (MPFR_PREC_MIN, 100, 50);
     121  
     122    tests_end_mpfr ();
     123    return 0;
     124  }