1/* mpfr_set_f -- set a MPFR number from a GNU MPF number 2 3Copyright 1999-2023 Free Software Foundation, Inc. 4Contributed by the AriC and Caramba projects, INRIA. 5 6This file is part of the GNU MPFR Library. 7 8The GNU MPFR Library is free software; you can redistribute it and/or modify 9it under the terms of the GNU Lesser General Public License as published by 10the Free Software Foundation; either version 3 of the License, or (at your 11option) any later version. 12 13The GNU MPFR Library is distributed in the hope that it will be useful, but 14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16License for more details. 17 18You should have received a copy of the GNU Lesser General Public License 19along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 20https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 2151 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23#define MPFR_NEED_LONGLONG_H 24#define MPFR_NEED_MPF_INTERNALS 25#include "mpfr-impl.h" 26 27#ifndef MPFR_USE_MINI_GMP 28int 29mpfr_set_f (mpfr_ptr y, mpf_srcptr x, mpfr_rnd_t rnd_mode) 30{ 31 mp_limb_t *my, *mx, *tmp; 32 int cnt; 33 mp_size_t sx, sy; 34 int inexact, carry = 0; 35 MPFR_TMP_DECL(marker); 36 37 sx = ABSIZ(x); /* number of limbs of the mantissa of x */ 38 39 if (sx == 0) /* x is zero */ 40 { 41 MPFR_SET_ZERO(y); 42 MPFR_SET_POS(y); 43 return 0; /* 0 is exact */ 44 } 45 46 if (SIZ(x) * MPFR_FROM_SIGN_TO_INT(MPFR_SIGN(y)) < 0) 47 MPFR_CHANGE_SIGN (y); 48 49 sy = MPFR_LIMB_SIZE (y); 50 my = MPFR_MANT(y); 51 mx = PTR(x); 52 53 count_leading_zeros(cnt, mx[sx - 1]); 54 55 if (sy <= sx) /* we may have to round even when sy = sx */ 56 { 57 mpfr_prec_t xprec = (mpfr_prec_t) sx * GMP_NUMB_BITS; 58 59 MPFR_TMP_MARK(marker); 60 tmp = MPFR_TMP_LIMBS_ALLOC (sx); 61 if (cnt) 62 mpn_lshift (tmp, mx, sx, cnt); 63 else 64 /* FIXME: we may avoid the copy here, and directly call mpfr_round_raw 65 on mx instead of tmp */ 66 MPN_COPY (tmp, mx, sx); 67 carry = mpfr_round_raw (my, tmp, xprec, (SIZ(x) < 0), MPFR_PREC(y), 68 rnd_mode, &inexact); 69 if (MPFR_UNLIKELY(carry)) /* result is a power of two */ 70 my[sy - 1] = MPFR_LIMB_HIGHBIT; 71 MPFR_TMP_FREE(marker); 72 } 73 else 74 { 75 if (cnt) 76 mpn_lshift (my + sy - sx, mx, sx, cnt); 77 else 78 MPN_COPY (my + sy - sx, mx, sx); 79 MPN_ZERO(my, sy - sx); 80 /* no rounding necessary, since y has a larger mantissa */ 81 inexact = 0; 82 } 83 84 /* warning: EXP(x) * GMP_NUMB_BITS may exceed the maximal exponent */ 85 if (EXP(x) > 1 + (__gmpfr_emax - 1) / GMP_NUMB_BITS) 86 { 87 /* EXP(x) >= 2 + floor((__gmpfr_emax-1)/GMP_NUMB_BITS) 88 EXP(x) >= 2 + (__gmpfr_emax - GMP_NUMB_BITS) / GMP_NUMB_BITS 89 >= 1 + __gmpfr_emax / GMP_NUMB_BITS 90 EXP(x) * GMP_NUMB_BITS >= __gmpfr_emax + GMP_NUMB_BITS 91 Since 0 <= cnt <= GMP_NUMB_BITS-1, and 0 <= carry <= 1, 92 we have then EXP(x) * GMP_NUMB_BITS - cnt + carry > __gmpfr_emax */ 93 return mpfr_overflow (y, rnd_mode, MPFR_SIGN (y)); 94 } 95 else 96 { 97 /* Do not use MPFR_SET_EXP as the exponent may be out of range. */ 98 MPFR_EXP (y) = EXP (x) * GMP_NUMB_BITS - cnt + carry; 99 } 100 101 return mpfr_check_range (y, inexact, rnd_mode); 102} 103#endif 104