xref: /dflybsd-src/contrib/mpfr/src/get_z_exp.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_get_z_2exp -- get a multiple-precision integer and an exponent
24a238c70SJohn Marino                       from a floating-point number
34a238c70SJohn Marino 
4*ab6d115fSJohn Marino Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
5*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
64a238c70SJohn Marino 
74a238c70SJohn Marino This file is part of the GNU MPFR Library.
84a238c70SJohn Marino 
94a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
104a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
114a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
124a238c70SJohn Marino option) any later version.
134a238c70SJohn Marino 
144a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
154a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
164a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
174a238c70SJohn Marino License for more details.
184a238c70SJohn Marino 
194a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
204a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
214a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
224a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
234a238c70SJohn Marino 
244a238c70SJohn Marino #include "mpfr-impl.h"
254a238c70SJohn Marino 
264a238c70SJohn Marino /* puts the significand of f into z, and returns 'exp' such that f = z * 2^exp
274a238c70SJohn Marino  *
284a238c70SJohn Marino  * 0 doesn't have an exponent, therefore the returned exponent in this case
294a238c70SJohn Marino  * isn't really important. We choose to return __gmpfr_emin because
304a238c70SJohn Marino  *   1) it is in the exponent range [__gmpfr_emin,__gmpfr_emax],
314a238c70SJohn Marino  *   2) the smaller a number is (in absolute value), the smaller its
324a238c70SJohn Marino  *      exponent is. In other words, the f -> exp function is monotonous
334a238c70SJohn Marino  *      on nonnegative numbers. --> This is WRONG since the returned
344a238c70SJohn Marino  *      exponent is not necessarily in the exponent range!
354a238c70SJohn Marino  * Note that this is different from the C function frexp().
364a238c70SJohn Marino  *
374a238c70SJohn Marino  * For NaN and infinities, we choose to set z = 0 (neutral value).
384a238c70SJohn Marino  * The exponent doesn't really matter, so let's keep __gmpfr_emin
394a238c70SJohn Marino  * for consistency. The erange flag is set.
404a238c70SJohn Marino  */
414a238c70SJohn Marino 
424a238c70SJohn Marino mpfr_exp_t
mpfr_get_z_2exp(mpz_ptr z,mpfr_srcptr f)434a238c70SJohn Marino mpfr_get_z_2exp (mpz_ptr z, mpfr_srcptr f)
444a238c70SJohn Marino {
454a238c70SJohn Marino   mp_size_t fn;
464a238c70SJohn Marino   int sh;
474a238c70SJohn Marino 
484a238c70SJohn Marino   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (f)))
494a238c70SJohn Marino     {
504a238c70SJohn Marino       if (MPFR_UNLIKELY (MPFR_NOTZERO (f)))
514a238c70SJohn Marino         MPFR_SET_ERANGE ();
524a238c70SJohn Marino       mpz_set_ui (z, 0);
534a238c70SJohn Marino       return __gmpfr_emin;
544a238c70SJohn Marino     }
554a238c70SJohn Marino 
564a238c70SJohn Marino   fn = MPFR_LIMB_SIZE(f);
574a238c70SJohn Marino 
584a238c70SJohn Marino   /* check whether allocated space for z is enough */
594a238c70SJohn Marino   if (MPFR_UNLIKELY (ALLOC (z) < fn))
604a238c70SJohn Marino     MPZ_REALLOC (z, fn);
614a238c70SJohn Marino 
624a238c70SJohn Marino   MPFR_UNSIGNED_MINUS_MODULO (sh, MPFR_PREC (f));
634a238c70SJohn Marino   if (MPFR_LIKELY (sh))
644a238c70SJohn Marino     mpn_rshift (PTR (z), MPFR_MANT (f), fn, sh);
654a238c70SJohn Marino   else
664a238c70SJohn Marino     MPN_COPY (PTR (z), MPFR_MANT (f), fn);
674a238c70SJohn Marino 
684a238c70SJohn Marino   SIZ(z) = MPFR_IS_NEG (f) ? -fn : fn;
694a238c70SJohn Marino 
704a238c70SJohn Marino   if (MPFR_UNLIKELY ((mpfr_uexp_t) MPFR_GET_EXP (f) - MPFR_EXP_MIN
714a238c70SJohn Marino                      < (mpfr_uexp_t) MPFR_PREC (f)))
724a238c70SJohn Marino     {
734a238c70SJohn Marino       /* The exponent isn't representable in an mpfr_exp_t. */
744a238c70SJohn Marino       MPFR_SET_ERANGE ();
754a238c70SJohn Marino       return MPFR_EXP_MIN;
764a238c70SJohn Marino     }
774a238c70SJohn Marino 
784a238c70SJohn Marino   return MPFR_GET_EXP (f) - MPFR_PREC (f);
794a238c70SJohn Marino }
80