beta-0.89.2
[luatex.git] / source / libs / mpfr / mpfr-3.1.3 / src / get_z_exp.c
blob1431b013ef98f4abb2922cef372974dc04191239
1 /* mpfr_get_z_2exp -- get a multiple-precision integer and an exponent
2 from a floating-point number
4 Copyright 2000-2015 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.
42 mpfr_exp_t
43 mpfr_get_z_2exp (mpz_ptr z, mpfr_srcptr f)
45 mp_size_t fn;
46 int sh;
48 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (f)))
50 if (MPFR_UNLIKELY (MPFR_NOTZERO (f)))
51 MPFR_SET_ERANGE ();
52 mpz_set_ui (z, 0);
53 return __gmpfr_emin;
56 fn = MPFR_LIMB_SIZE(f);
58 /* check whether allocated space for z is enough */
59 if (MPFR_UNLIKELY (ALLOC (z) < fn))
60 MPZ_REALLOC (z, fn);
62 MPFR_UNSIGNED_MINUS_MODULO (sh, MPFR_PREC (f));
63 if (MPFR_LIKELY (sh))
64 mpn_rshift (PTR (z), MPFR_MANT (f), fn, sh);
65 else
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. */
74 MPFR_SET_ERANGE ();
75 return MPFR_EXP_MIN;
78 return MPFR_GET_EXP (f) - MPFR_PREC (f);