Annotation of OpenXM_contrib/gmp/mpfr/set_si.c, Revision 1.1.1.2
1.1.1.2 ! ohara 1: /* mpfr_set_si -- set a MPFR number from a machine signed integer
1.1 maekawa 2:
1.1.1.2 ! ohara 3: Copyright 1999, 2000, 2001 Free Software Foundation.
1.1 maekawa 4:
5: This file is part of the MPFR Library.
6:
7: The MPFR Library is free software; you can redistribute it and/or modify
1.1.1.2 ! ohara 8: it under the terms of the GNU Lesser General Public License as published by
! 9: the Free Software Foundation; either version 2.1 of the License, or (at your
1.1 maekawa 10: option) any later version.
11:
12: The MPFR Library is distributed in the hope that it will be useful, but
13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.1.1.2 ! ohara 14: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
1.1 maekawa 15: License for more details.
16:
1.1.1.2 ! ohara 17: You should have received a copy of the GNU Lesser General Public License
1.1 maekawa 18: along with the MPFR Library; see the file COPYING.LIB. If not, write to
19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20: MA 02111-1307, USA. */
21:
22: #include "gmp.h"
23: #include "gmp-impl.h"
24: #include "longlong.h"
25: #include "mpfr.h"
1.1.1.2 ! ohara 26: #include "mpfr-impl.h"
1.1 maekawa 27:
1.1.1.2 ! ohara 28: int
! 29: mpfr_set_si (mpfr_ptr x, long i, mp_rnd_t rnd_mode)
1.1 maekawa 30: {
1.1.1.2 ! ohara 31: int inex;
! 32: mp_size_t xn;
! 33: unsigned int cnt, nbits;
! 34: mp_limb_t ai, *xp;
! 35:
! 36: MPFR_CLEAR_FLAGS(x);
! 37: if (i == 0)
! 38: {
! 39: MPFR_SET_ZERO(x);
! 40: MPFR_SET_POS(x);
! 41: MPFR_RET(0);
! 42: }
! 43:
! 44: xn = (MPFR_PREC(x)-1)/BITS_PER_MP_LIMB;
! 45: ai = SAFE_ABS(long, i);
! 46: count_leading_zeros(cnt, ai);
1.1 maekawa 47:
1.1.1.2 ! ohara 48: xp = MPFR_MANT(x);
! 49: xp[xn] = ai << cnt;
1.1 maekawa 50: /* don't forget to put zero in lower limbs */
1.1.1.2 ! ohara 51: MPN_ZERO(xp, xn);
! 52: /* set sign */
! 53: if ((i < 0) ^ (MPFR_SIGN(x) < 0))
! 54: MPFR_CHANGE_SIGN(x);
! 55:
! 56: MPFR_EXP(x) = nbits = BITS_PER_MP_LIMB - cnt;
! 57: inex = mpfr_check_range(x, rnd_mode);
! 58: if (inex)
! 59: return inex; /* underflow or overflow */
! 60:
! 61: /* round if MPFR_PREC(x) smaller than length of i */
! 62: if (MPFR_PREC(x) < nbits)
! 63: {
! 64: int carry;
! 65:
! 66: carry = mpfr_round_raw(xp+xn, xp+xn, nbits, (i < 0), MPFR_PREC(x),
! 67: rnd_mode, &inex);
! 68: if (carry)
! 69: {
! 70: mp_exp_t exp = MPFR_EXP(x);
! 71:
! 72: if (exp == __mpfr_emax)
! 73: return mpfr_set_overflow(x, rnd_mode, (i < 0 ? -1 : 1));
! 74:
! 75: MPFR_EXP(x)++;
! 76: xp[xn] = GMP_LIMB_HIGHBIT;
! 77: }
! 78: }
1.1 maekawa 79:
1.1.1.2 ! ohara 80: MPFR_RET(inex);
1.1 maekawa 81: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>