Home | History | Annotate | Line # | Download | only in src
      1 /* mpfr_set_si_2exp -- set a MPFR number from a machine signed integer with
      2    a shift
      3 
      4 Copyright 2004, 2006-2023 Free Software Foundation, Inc.
      5 Contributed by the AriC and Caramba projects, INRIA.
      6 
      7 This file is part of the GNU MPFR Library.
      8 
      9 The GNU MPFR Library is free software; you can redistribute it and/or modify
     10 it under the terms of the GNU Lesser General Public License as published by
     11 the Free Software Foundation; either version 3 of the License, or (at your
     12 option) any later version.
     13 
     14 The GNU MPFR Library is distributed in the hope that it will be useful, but
     15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     17 License for more details.
     18 
     19 You should have received a copy of the GNU Lesser General Public License
     20 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     21 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     23 
     24 #define MPFR_NEED_LONGLONG_H
     25 #include "mpfr-impl.h"
     26 
     27 int
     28 mpfr_set_si_2exp (mpfr_ptr x, long i, mpfr_exp_t e, mpfr_rnd_t rnd_mode)
     29 {
     30   if (i == 0)
     31     {
     32       MPFR_SET_ZERO (x);
     33       MPFR_SET_POS (x);
     34       MPFR_RET (0);
     35     }
     36 #ifdef MPFR_LONG_WITHIN_LIMB
     37   else
     38     {
     39       mp_size_t xn;
     40       int cnt, nbits;
     41       mp_limb_t ai, *xp;
     42       int inex = 0;
     43 
     44       /* Early underflow/overflow checking is necessary to avoid
     45          integer overflow or errors due to special exponent values. */
     46       if (MPFR_UNLIKELY (e < __gmpfr_emin - (mpfr_exp_t)
     47                          (sizeof (unsigned long) * CHAR_BIT + 1)))
     48         return mpfr_underflow (x, rnd_mode == MPFR_RNDN ?
     49                                MPFR_RNDZ : rnd_mode, i < 0 ? -1 : 1);
     50       if (MPFR_UNLIKELY (e >= __gmpfr_emax))
     51         return mpfr_overflow (x, rnd_mode, i < 0 ? -1 : 1);
     52 
     53       ai = SAFE_ABS (unsigned long, i);
     54       MPFR_ASSERTN (SAFE_ABS (unsigned long, i) == ai);
     55 
     56       /* Position of the highest limb */
     57       xn = (MPFR_PREC (x) - 1) / GMP_NUMB_BITS;
     58       count_leading_zeros (cnt, ai);
     59       MPFR_ASSERTD (cnt < GMP_NUMB_BITS);  /* OK since i != 0 */
     60 
     61       xp = MPFR_MANT(x);
     62       xp[xn] = ai << cnt;
     63       /* Zero the xn lower limbs. */
     64       MPN_ZERO(xp, xn);
     65       MPFR_SET_SIGN (x, i < 0 ? MPFR_SIGN_NEG : MPFR_SIGN_POS);
     66 
     67       nbits = GMP_NUMB_BITS - cnt;
     68       e += nbits;  /* exponent _before_ the rounding */
     69 
     70       /* round if MPFR_PREC(x) smaller than length of i */
     71       if (MPFR_UNLIKELY (MPFR_PREC (x) < nbits) &&
     72           MPFR_UNLIKELY (mpfr_round_raw (xp + xn, xp + xn, nbits, i < 0,
     73                                          MPFR_PREC (x), rnd_mode, &inex)))
     74         {
     75           e++;
     76           xp[xn] = MPFR_LIMB_HIGHBIT;
     77         }
     78 
     79       MPFR_EXP (x) = e;
     80       return mpfr_check_range (x, inex, rnd_mode);
     81     }
     82 #else
     83   /* if a long does not fit into a limb, we use mpfr_set_z_2exp */
     84   {
     85     mpz_t z;
     86     int inex;
     87     mpz_init_set_si (z, i);
     88     inex = mpfr_set_z_2exp (x, z, e, rnd_mode);
     89     mpz_clear (z);
     90     return inex;
     91   }
     92 #endif
     93 }
     94