xref: /dflybsd-src/contrib/mpfr/src/frac.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_frac -- Fractional part of a floating-point number.
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino 
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino 
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino 
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino 
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino 
234a238c70SJohn Marino 
244a238c70SJohn Marino #define MPFR_NEED_LONGLONG_H
254a238c70SJohn Marino #include "mpfr-impl.h"
264a238c70SJohn Marino 
274a238c70SJohn Marino /* Optimization note: it is not a good idea to call mpfr_integer_p,
284a238c70SJohn Marino    as some cases will take longer (the number may be parsed twice). */
294a238c70SJohn Marino 
304a238c70SJohn Marino int
mpfr_frac(mpfr_ptr r,mpfr_srcptr u,mpfr_rnd_t rnd_mode)314a238c70SJohn Marino mpfr_frac (mpfr_ptr r, mpfr_srcptr u, mpfr_rnd_t rnd_mode)
324a238c70SJohn Marino {
334a238c70SJohn Marino   mpfr_exp_t re, ue;
344a238c70SJohn Marino   mpfr_prec_t uq;
354a238c70SJohn Marino   mp_size_t un, tn, t0;
364a238c70SJohn Marino   mp_limb_t *up, *tp, k;
374a238c70SJohn Marino   int sh;
384a238c70SJohn Marino   mpfr_t tmp;
394a238c70SJohn Marino   mpfr_ptr t;
404a238c70SJohn Marino   int inex;
414a238c70SJohn Marino   MPFR_SAVE_EXPO_DECL (expo);
424a238c70SJohn Marino 
434a238c70SJohn Marino   /* Special cases */
444a238c70SJohn Marino   if (MPFR_UNLIKELY(MPFR_IS_NAN(u)))
454a238c70SJohn Marino     {
464a238c70SJohn Marino       MPFR_SET_NAN(r);
474a238c70SJohn Marino       MPFR_RET_NAN;
484a238c70SJohn Marino     }
494a238c70SJohn Marino   else if (MPFR_UNLIKELY(MPFR_IS_INF(u) || mpfr_integer_p (u)))
504a238c70SJohn Marino     {
514a238c70SJohn Marino       MPFR_SET_SAME_SIGN(r, u);
524a238c70SJohn Marino       MPFR_SET_ZERO(r);
534a238c70SJohn Marino       MPFR_RET(0);  /* zero is exact */
544a238c70SJohn Marino     }
554a238c70SJohn Marino 
564a238c70SJohn Marino   ue = MPFR_GET_EXP (u);
574a238c70SJohn Marino   if (ue <= 0)  /* |u| < 1 */
584a238c70SJohn Marino     return mpfr_set (r, u, rnd_mode);
594a238c70SJohn Marino 
604a238c70SJohn Marino   /* Now |u| >= 1, meaning that an overflow is not possible. */
614a238c70SJohn Marino 
624a238c70SJohn Marino   uq = MPFR_PREC(u);
634a238c70SJohn Marino   un = (uq - 1) / GMP_NUMB_BITS;  /* index of most significant limb */
644a238c70SJohn Marino   un -= (mp_size_t) (ue / GMP_NUMB_BITS);
654a238c70SJohn Marino   /* now the index of the MSL containing bits of the fractional part */
664a238c70SJohn Marino 
674a238c70SJohn Marino   up = MPFR_MANT(u);
684a238c70SJohn Marino   sh = ue % GMP_NUMB_BITS;
694a238c70SJohn Marino   k = up[un] << sh;
704a238c70SJohn Marino   /* the first bit of the fractional part is the MSB of k */
714a238c70SJohn Marino 
724a238c70SJohn Marino   if (k != 0)
734a238c70SJohn Marino     {
744a238c70SJohn Marino       int cnt;
754a238c70SJohn Marino 
764a238c70SJohn Marino       count_leading_zeros(cnt, k);
774a238c70SJohn Marino       /* first bit 1 of the fractional part -> MSB of the number */
784a238c70SJohn Marino       re = -cnt;
794a238c70SJohn Marino       sh += cnt;
804a238c70SJohn Marino       MPFR_ASSERTN (sh < GMP_NUMB_BITS);
814a238c70SJohn Marino       k <<= cnt;
824a238c70SJohn Marino     }
834a238c70SJohn Marino   else
844a238c70SJohn Marino     {
854a238c70SJohn Marino       re = sh - GMP_NUMB_BITS;
864a238c70SJohn Marino       /* searching for the first bit 1 (exists since u isn't an integer) */
874a238c70SJohn Marino       while (up[--un] == 0)
884a238c70SJohn Marino         re -= GMP_NUMB_BITS;
894a238c70SJohn Marino       MPFR_ASSERTN(un >= 0);
904a238c70SJohn Marino       k = up[un];
914a238c70SJohn Marino       count_leading_zeros(sh, k);
924a238c70SJohn Marino       re -= sh;
934a238c70SJohn Marino       k <<= sh;
944a238c70SJohn Marino     }
954a238c70SJohn Marino   /* The exponent of r will be re */
964a238c70SJohn Marino   /* un: index of the limb of u that contains the first bit 1 of the FP */
974a238c70SJohn Marino 
984a238c70SJohn Marino   t = (mp_size_t) (MPFR_PREC(r) - 1) / GMP_NUMB_BITS < un ?
994a238c70SJohn Marino     (mpfr_init2 (tmp, (un + 1) * GMP_NUMB_BITS), tmp) : r;
1004a238c70SJohn Marino   /* t has enough precision to contain the fractional part of u */
1014a238c70SJohn Marino   /* If we use a temporary variable, we take the non-significant bits
1024a238c70SJohn Marino      of u into account, because of the mpn_lshift below. */
1034a238c70SJohn Marino   MPFR_SET_SAME_SIGN(t, u);
1044a238c70SJohn Marino 
1054a238c70SJohn Marino   /* Put the fractional part of u into t */
1064a238c70SJohn Marino   tn = (MPFR_PREC(t) - 1) / GMP_NUMB_BITS;
1074a238c70SJohn Marino   MPFR_ASSERTN(tn >= un);
1084a238c70SJohn Marino   t0 = tn - un;
1094a238c70SJohn Marino   tp = MPFR_MANT(t);
1104a238c70SJohn Marino   if (sh == 0)
1114a238c70SJohn Marino     MPN_COPY_DECR(tp + t0, up, un + 1);
1124a238c70SJohn Marino   else /* warning: un may be 0 here */
1134a238c70SJohn Marino     tp[tn] = k | ((un) ? mpn_lshift (tp + t0, up, un, sh) : (mp_limb_t) 0);
1144a238c70SJohn Marino   if (t0 > 0)
1154a238c70SJohn Marino     MPN_ZERO(tp, t0);
1164a238c70SJohn Marino 
1174a238c70SJohn Marino   MPFR_SAVE_EXPO_MARK (expo);
1184a238c70SJohn Marino 
1194a238c70SJohn Marino   if (t != r)
1204a238c70SJohn Marino     { /* t is tmp */
1214a238c70SJohn Marino       MPFR_EXP (t) = 0;  /* should be re, but not necessarily in the range */
1224a238c70SJohn Marino       inex = mpfr_set (r, t, rnd_mode);  /* no underflow */
1234a238c70SJohn Marino       mpfr_clear (t);
1244a238c70SJohn Marino       MPFR_EXP (r) += re;
1254a238c70SJohn Marino     }
1264a238c70SJohn Marino   else
1274a238c70SJohn Marino     { /* There may be remaining non-significant bits in t (= r). */
1284a238c70SJohn Marino       int carry;
1294a238c70SJohn Marino 
1304a238c70SJohn Marino       MPFR_EXP (r) = re;
1314a238c70SJohn Marino       carry = mpfr_round_raw (tp, tp,
1324a238c70SJohn Marino                               (mpfr_prec_t) (tn + 1) * GMP_NUMB_BITS,
1334a238c70SJohn Marino                               MPFR_IS_NEG (r), MPFR_PREC (r), rnd_mode,
1344a238c70SJohn Marino                               &inex);
1354a238c70SJohn Marino       if (carry)
1364a238c70SJohn Marino         {
1374a238c70SJohn Marino           tp[tn] = MPFR_LIMB_HIGHBIT;
1384a238c70SJohn Marino           MPFR_EXP (r) ++;
1394a238c70SJohn Marino         }
1404a238c70SJohn Marino     }
1414a238c70SJohn Marino 
1424a238c70SJohn Marino   MPFR_SAVE_EXPO_FREE (expo);
1434a238c70SJohn Marino   return mpfr_check_range (r, inex, rnd_mode);
1444a238c70SJohn Marino }
145