(root)/
mpfr-4.2.1/
src/
fits_u.h
       1  /* mpfr_fits_*_p -- test whether an mpfr fits a C unsigned type.
       2  
       3  Copyright 2003-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-impl.h"
      24  
      25  int
      26  FUNCTION (mpfr_srcptr f, mpfr_rnd_t rnd)
      27  {
      28    mpfr_flags_t saved_flags;
      29    mpfr_exp_t e;
      30    int prec;
      31    TYPE s;
      32    mpfr_t x;
      33    int res;
      34  
      35    if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (f)))
      36      return MPFR_IS_ZERO (f) ? 1 : 0;  /* Zero always fits */
      37  
      38    e = MPFR_GET_EXP (f);
      39  
      40    if (MPFR_IS_NEG (f))
      41      return e >= 1 ? 0  /* f <= -1 does not fit */
      42        : rnd != MPFR_RNDN ? MPFR_IS_LIKE_RNDU (rnd, -1)  /* directed mode */
      43        : e < 0 ? 1  /* f > -1/2 fits in MPFR_RNDN */
      44        : mpfr_powerof2_raw(f);  /* -1/2 fits, -1 < f < -1/2 don't */
      45  
      46    /* Now it fits if
      47       (a) f <= MAXIMUM
      48       (b) round(f, prec(slong), rnd) <= MAXIMUM */
      49  
      50    /* first compute prec(MAXIMUM); fits in an int */
      51    for (s = MAXIMUM, prec = 0; s != 0; s /= 2, prec ++);
      52  
      53    /* MAXIMUM needs prec bits, i.e. MAXIMUM = 2^prec - 1 */
      54  
      55    /* if e <= prec - 1, then f < 2^(prec-1) < MAXIMUM */
      56    if (e <= prec - 1)
      57      return 1;
      58  
      59    /* if e >= prec + 1, then f >= 2^prec > MAXIMUM */
      60    if (e >= prec + 1)
      61      return 0;
      62  
      63    MPFR_ASSERTD (e == prec);
      64  
      65    /* hard case: first round to prec bits, then check */
      66    saved_flags = __gmpfr_flags;
      67    mpfr_init2 (x, prec);
      68    /* For MPFR_RNDF, if f > 0 fits with RNDU, it will also fit with RNDD. */
      69    mpfr_set (x, f, (rnd != MPFR_RNDF) ? rnd : MPFR_RNDU);
      70    /* Warning! Due to the rounding, x can be an infinity. Here we use
      71       the fact that singular numbers have a special exponent field,
      72       thus well-defined and different from e, in which case this means
      73       that the number does not fit. That's why we use MPFR_EXP, not
      74       MPFR_GET_EXP. */
      75    res = MPFR_EXP (x) == e;
      76    mpfr_clear (x);
      77    __gmpfr_flags = saved_flags;
      78    return res;
      79  }