1 /* mpfr_get_z_2exp -- get a multiple-precision integer and an exponent
2 from a floating-point number
4 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
5 Contributed by the AriC and Caramel projects, INRIA.
7 This file is part of the GNU MPFR Library.
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.
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.
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 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
24 #include "mpfr-impl.h"
26 /* puts the significand of f into z, and returns 'exp' such that f = z * 2^exp
28 * 0 doesn't have an exponent, therefore the returned exponent in this case
29 * isn't really important. We choose to return __gmpfr_emin because
30 * 1) it is in the exponent range [__gmpfr_emin,__gmpfr_emax],
31 * 2) the smaller a number is (in absolute value), the smaller its
32 * exponent is. In other words, the f -> exp function is monotonous
33 * on nonnegative numbers. --> This is WRONG since the returned
34 * exponent is not necessarily in the exponent range!
35 * Note that this is different from the C function frexp().
37 * For NaN and infinities, we choose to set z = 0 (neutral value).
38 * The exponent doesn't really matter, so let's keep __gmpfr_emin
39 * for consistency. The erange flag is set.
43 mpfr_get_z_2exp (mpz_ptr z
, mpfr_srcptr f
)
48 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (f
)))
50 if (MPFR_UNLIKELY (MPFR_NOTZERO (f
)))
56 fn
= MPFR_LIMB_SIZE(f
);
58 /* check whether allocated space for z is enough */
59 if (MPFR_UNLIKELY (ALLOC (z
) < fn
))
62 MPFR_UNSIGNED_MINUS_MODULO (sh
, MPFR_PREC (f
));
64 mpn_rshift (PTR (z
), MPFR_MANT (f
), fn
, sh
);
66 MPN_COPY (PTR (z
), MPFR_MANT (f
), fn
);
68 SIZ(z
) = MPFR_IS_NEG (f
) ? -fn
: fn
;
70 if (MPFR_UNLIKELY ((mpfr_uexp_t
) MPFR_GET_EXP (f
) - MPFR_EXP_MIN
71 < (mpfr_uexp_t
) MPFR_PREC (f
)))
73 /* The exponent isn't representable in an mpfr_exp_t. */
78 return MPFR_GET_EXP (f
) - MPFR_PREC (f
);