Upgrade MPFR from 3.1.0 to 3.1.2 on the vendor branch
[dragonfly.git] / contrib / mpfr / src / get_z_exp.c
1 /* mpfr_get_z_2exp -- get a multiple-precision integer and an exponent
2                       from a floating-point number
3
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.
6
7 This file is part of the GNU MPFR Library.
8
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.
13
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.
18
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. */
23
24 #include "mpfr-impl.h"
25
26 /* puts the significand of f into z, and returns 'exp' such that f = z * 2^exp
27  *
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().
36  *
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.
40  */
41
42 mpfr_exp_t
43 mpfr_get_z_2exp (mpz_ptr z, mpfr_srcptr f)
44 {
45   mp_size_t fn;
46   int sh;
47
48   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (f)))
49     {
50       if (MPFR_UNLIKELY (MPFR_NOTZERO (f)))
51         MPFR_SET_ERANGE ();
52       mpz_set_ui (z, 0);
53       return __gmpfr_emin;
54     }
55
56   fn = MPFR_LIMB_SIZE(f);
57
58   /* check whether allocated space for z is enough */
59   if (MPFR_UNLIKELY (ALLOC (z) < fn))
60     MPZ_REALLOC (z, fn);
61
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);
67
68   SIZ(z) = MPFR_IS_NEG (f) ? -fn : fn;
69
70   if (MPFR_UNLIKELY ((mpfr_uexp_t) MPFR_GET_EXP (f) - MPFR_EXP_MIN
71                      < (mpfr_uexp_t) MPFR_PREC (f)))
72     {
73       /* The exponent isn't representable in an mpfr_exp_t. */
74       MPFR_SET_ERANGE ();
75       return MPFR_EXP_MIN;
76     }
77
78   return MPFR_GET_EXP (f) - MPFR_PREC (f);
79 }