kernel - Reenable acpi sleep states
[dragonfly.git] / contrib / mpfr / set_f.c
blob6c329d16f5606c0c5c3f0ab6ec7b0dab316b05cd
1 /* mpfr_set_f -- set a MPFR number from a GNU MPF number
3 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
6 This file is part of the GNU MPFR Library.
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 2.1 of the License, or (at your
11 option) any later version.
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.
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.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #define MPFR_NEED_LONGLONG_H
24 #include "mpfr-impl.h"
26 int
27 mpfr_set_f (mpfr_ptr y, mpf_srcptr x, mp_rnd_t rnd_mode)
29 mp_limb_t *my, *mx, *tmp;
30 unsigned long cnt, sx, sy;
31 int inexact, carry = 0;
32 MPFR_TMP_DECL(marker);
34 sx = ABS(SIZ(x)); /* number of limbs of the mantissa of x */
36 if (sx == 0) /* x is zero */
38 MPFR_CLEAR_FLAGS (y);
39 MPFR_SET_ZERO(y);
40 MPFR_SET_POS(y);
41 return 0; /* 0 is exact */
44 if (SIZ(x) * MPFR_FROM_SIGN_TO_INT(MPFR_SIGN(y)) < 0)
45 MPFR_CHANGE_SIGN (y);
47 MPFR_CLEAR_FLAGS (y);
49 sy = 1 + (MPFR_PREC(y) - 1) / BITS_PER_MP_LIMB;
50 my = MPFR_MANT(y);
51 mx = PTR(x);
53 count_leading_zeros(cnt, mx[sx - 1]);
55 if (sy <= sx) /* we may have to round even when sy = sx */
57 unsigned long xprec = sx * BITS_PER_MP_LIMB;
59 MPFR_TMP_MARK(marker);
60 tmp = (mp_limb_t*) MPFR_TMP_ALLOC(sx * BYTES_PER_MP_LIMB);
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);
73 else
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;
84 /* warning: EXP(x) * BITS_PER_MP_LIMB may exceed the maximal exponent */
85 if (EXP(x) > 1 + (__gmpfr_emax - 1) / BITS_PER_MP_LIMB)
87 /* EXP(x) >= 2 + floor((__gmpfr_emax-1)/BITS_PER_MP_LIMB)
88 EXP(x) >= 2 + (__gmpfr_emax - BITS_PER_MP_LIMB) / BITS_PER_MP_LIMB
89 >= 1 + __gmpfr_emax / BITS_PER_MP_LIMB
90 EXP(x) * BITS_PER_MP_LIMB >= __gmpfr_emax + BITS_PER_MP_LIMB
91 Since 0 <= cnt <= BITS_PER_MP_LIMB-1, and 0 <= carry <= 1,
92 we have then EXP(x) * BITS_PER_MP_LIMB - cnt + carry > __gmpfr_emax */
93 return mpfr_overflow (y, rnd_mode, MPFR_SIGN (y));
95 else
97 /* Do not use MPFR_SET_EXP as the exponent may be out of range. */
98 MPFR_EXP (y) = EXP (x) * BITS_PER_MP_LIMB - (mp_exp_t) cnt + carry;
101 return mpfr_check_range (y, inexact, rnd_mode);