xref: /dflybsd-src/contrib/mpfr/src/div_ui.c (revision 2786097444a0124b5d33763854de247e230c6629)
14a238c70SJohn Marino /* mpfr_div_{ui,si} -- divide a floating-point number by a machine integer
24a238c70SJohn Marino 
3*ab6d115fSJohn Marino Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 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 #define MPFR_NEED_LONGLONG_H
244a238c70SJohn Marino #include "mpfr-impl.h"
254a238c70SJohn Marino 
264a238c70SJohn Marino /* returns 0 if result exact, non-zero otherwise */
274a238c70SJohn Marino int
mpfr_div_ui(mpfr_ptr y,mpfr_srcptr x,unsigned long int u,mpfr_rnd_t rnd_mode)284a238c70SJohn Marino mpfr_div_ui (mpfr_ptr y, mpfr_srcptr x, unsigned long int u, mpfr_rnd_t rnd_mode)
294a238c70SJohn Marino {
304a238c70SJohn Marino   long i;
314a238c70SJohn Marino   int sh;
324a238c70SJohn Marino   mp_size_t xn, yn, dif;
334a238c70SJohn Marino   mp_limb_t *xp, *yp, *tmp, c, d;
344a238c70SJohn Marino   mpfr_exp_t exp;
354a238c70SJohn Marino   int inexact, middle = 1, nexttoinf;
364a238c70SJohn Marino   MPFR_TMP_DECL(marker);
374a238c70SJohn Marino 
384a238c70SJohn Marino   MPFR_LOG_FUNC
394a238c70SJohn Marino     (("x[%Pu]=%.*Rg u=%lu rnd=%d",
404a238c70SJohn Marino       mpfr_get_prec(x), mpfr_log_prec, x, u, rnd_mode),
414a238c70SJohn Marino      ("y[%Pu]=%.*Rg inexact=%d",
424a238c70SJohn Marino       mpfr_get_prec(y), mpfr_log_prec, y, inexact));
434a238c70SJohn Marino 
444a238c70SJohn Marino   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
454a238c70SJohn Marino     {
464a238c70SJohn Marino       if (MPFR_IS_NAN (x))
474a238c70SJohn Marino         {
484a238c70SJohn Marino           MPFR_SET_NAN (y);
494a238c70SJohn Marino           MPFR_RET_NAN;
504a238c70SJohn Marino         }
514a238c70SJohn Marino       else if (MPFR_IS_INF (x))
524a238c70SJohn Marino         {
534a238c70SJohn Marino           MPFR_SET_INF (y);
544a238c70SJohn Marino           MPFR_SET_SAME_SIGN (y, x);
554a238c70SJohn Marino           MPFR_RET (0);
564a238c70SJohn Marino         }
574a238c70SJohn Marino       else
584a238c70SJohn Marino         {
594a238c70SJohn Marino           MPFR_ASSERTD (MPFR_IS_ZERO(x));
604a238c70SJohn Marino           if (u == 0) /* 0/0 is NaN */
614a238c70SJohn Marino             {
624a238c70SJohn Marino               MPFR_SET_NAN(y);
634a238c70SJohn Marino               MPFR_RET_NAN;
644a238c70SJohn Marino             }
654a238c70SJohn Marino           else
664a238c70SJohn Marino             {
674a238c70SJohn Marino               MPFR_SET_ZERO(y);
684a238c70SJohn Marino               MPFR_SET_SAME_SIGN (y, x);
694a238c70SJohn Marino               MPFR_RET(0);
704a238c70SJohn Marino             }
714a238c70SJohn Marino         }
724a238c70SJohn Marino     }
734a238c70SJohn Marino   else if (MPFR_UNLIKELY (u <= 1))
744a238c70SJohn Marino     {
754a238c70SJohn Marino       if (u < 1)
764a238c70SJohn Marino         {
774a238c70SJohn Marino           /* x/0 is Inf since x != 0*/
784a238c70SJohn Marino           MPFR_SET_INF (y);
794a238c70SJohn Marino           MPFR_SET_SAME_SIGN (y, x);
804a238c70SJohn Marino           mpfr_set_divby0 ();
814a238c70SJohn Marino           MPFR_RET (0);
824a238c70SJohn Marino         }
834a238c70SJohn Marino       else /* y = x/1 = x */
844a238c70SJohn Marino         return mpfr_set (y, x, rnd_mode);
854a238c70SJohn Marino     }
864a238c70SJohn Marino   else if (MPFR_UNLIKELY (IS_POW2 (u)))
874a238c70SJohn Marino     return mpfr_div_2si (y, x, MPFR_INT_CEIL_LOG2 (u), rnd_mode);
884a238c70SJohn Marino 
894a238c70SJohn Marino   MPFR_SET_SAME_SIGN (y, x);
904a238c70SJohn Marino 
914a238c70SJohn Marino   MPFR_TMP_MARK (marker);
924a238c70SJohn Marino   xn = MPFR_LIMB_SIZE (x);
934a238c70SJohn Marino   yn = MPFR_LIMB_SIZE (y);
944a238c70SJohn Marino 
954a238c70SJohn Marino   xp = MPFR_MANT (x);
964a238c70SJohn Marino   yp = MPFR_MANT (y);
974a238c70SJohn Marino   exp = MPFR_GET_EXP (x);
984a238c70SJohn Marino 
994a238c70SJohn Marino   dif = yn + 1 - xn;
1004a238c70SJohn Marino 
1014a238c70SJohn Marino   /* we need to store yn+1 = xn + dif limbs of the quotient */
1024a238c70SJohn Marino   /* don't use tmp=yp since the mpn_lshift call below requires yp >= tmp+1 */
1034a238c70SJohn Marino   tmp = MPFR_TMP_LIMBS_ALLOC (yn + 1);
1044a238c70SJohn Marino 
1054a238c70SJohn Marino   c = (mp_limb_t) u;
1064a238c70SJohn Marino   MPFR_ASSERTN (u == c);
1074a238c70SJohn Marino   if (dif >= 0)
1084a238c70SJohn Marino     c = mpn_divrem_1 (tmp, dif, xp, xn, c); /* used all the dividend */
1094a238c70SJohn Marino   else /* dif < 0 i.e. xn > yn, don't use the (-dif) low limbs from x */
1104a238c70SJohn Marino     c = mpn_divrem_1 (tmp, 0, xp - dif, yn + 1, c);
1114a238c70SJohn Marino 
1124a238c70SJohn Marino   inexact = (c != 0);
1134a238c70SJohn Marino 
1144a238c70SJohn Marino   /* First pass in estimating next bit of the quotient, in case of RNDN    *
1154a238c70SJohn Marino    * In case we just have the right number of bits (postpone this ?),      *
1164a238c70SJohn Marino    * we need to check whether the remainder is more or less than half      *
1174a238c70SJohn Marino    * the divisor. The test must be performed with a subtraction, so as     *
1184a238c70SJohn Marino    * to prevent carries.                                                   */
1194a238c70SJohn Marino 
1204a238c70SJohn Marino   if (MPFR_LIKELY (rnd_mode == MPFR_RNDN))
1214a238c70SJohn Marino     {
1224a238c70SJohn Marino       if (c < (mp_limb_t) u - c) /* We have u > c */
1234a238c70SJohn Marino         middle = -1;
1244a238c70SJohn Marino       else if (c > (mp_limb_t) u - c)
1254a238c70SJohn Marino         middle = 1;
1264a238c70SJohn Marino       else
1274a238c70SJohn Marino         middle = 0; /* exactly in the middle */
1284a238c70SJohn Marino     }
1294a238c70SJohn Marino 
1304a238c70SJohn Marino   /* If we believe that we are right in the middle or exact, we should check
1314a238c70SJohn Marino      that we did not neglect any word of x (division large / 1 -> small). */
1324a238c70SJohn Marino 
1334a238c70SJohn Marino   for (i=0; ((inexact == 0) || (middle == 0)) && (i < -dif); i++)
1344a238c70SJohn Marino     if (xp[i])
1354a238c70SJohn Marino       inexact = middle = 1; /* larger than middle */
1364a238c70SJohn Marino 
1374a238c70SJohn Marino   /*
1384a238c70SJohn Marino      If the high limb of the result is 0 (xp[xn-1] < u), remove it.
1394a238c70SJohn Marino      Otherwise, compute the left shift to be performed to normalize.
1404a238c70SJohn Marino      In the latter case, we discard some low bits computed. They
1414a238c70SJohn Marino      contain information useful for the rounding, hence the updating
1424a238c70SJohn Marino      of middle and inexact.
1434a238c70SJohn Marino   */
1444a238c70SJohn Marino 
1454a238c70SJohn Marino   if (tmp[yn] == 0)
1464a238c70SJohn Marino     {
1474a238c70SJohn Marino       MPN_COPY(yp, tmp, yn);
1484a238c70SJohn Marino       exp -= GMP_NUMB_BITS;
1494a238c70SJohn Marino     }
1504a238c70SJohn Marino   else
1514a238c70SJohn Marino     {
1524a238c70SJohn Marino       int shlz;
1534a238c70SJohn Marino 
1544a238c70SJohn Marino       count_leading_zeros (shlz, tmp[yn]);
1554a238c70SJohn Marino 
1564a238c70SJohn Marino       /* shift left to normalize */
1574a238c70SJohn Marino       if (MPFR_LIKELY (shlz != 0))
1584a238c70SJohn Marino         {
1594a238c70SJohn Marino           mp_limb_t w = tmp[0] << shlz;
1604a238c70SJohn Marino 
1614a238c70SJohn Marino           mpn_lshift (yp, tmp + 1, yn, shlz);
1624a238c70SJohn Marino           yp[0] += tmp[0] >> (GMP_NUMB_BITS - shlz);
1634a238c70SJohn Marino 
1644a238c70SJohn Marino           if (w > (MPFR_LIMB_ONE << (GMP_NUMB_BITS - 1)))
1654a238c70SJohn Marino             { middle = 1; }
1664a238c70SJohn Marino           else if (w < (MPFR_LIMB_ONE << (GMP_NUMB_BITS - 1)))
1674a238c70SJohn Marino             { middle = -1; }
1684a238c70SJohn Marino           else
1694a238c70SJohn Marino             { middle = (c != 0); }
1704a238c70SJohn Marino 
1714a238c70SJohn Marino           inexact = inexact || (w != 0);
1724a238c70SJohn Marino           exp -= shlz;
1734a238c70SJohn Marino         }
1744a238c70SJohn Marino       else
1754a238c70SJohn Marino         { /* this happens only if u == 1 and xp[xn-1] >=
1764a238c70SJohn Marino              1<<(GMP_NUMB_BITS-1). It might be better to handle the
1774a238c70SJohn Marino              u == 1 case seperately ?
1784a238c70SJohn Marino           */
1794a238c70SJohn Marino 
1804a238c70SJohn Marino              MPN_COPY (yp, tmp + 1, yn);
1814a238c70SJohn Marino         }
1824a238c70SJohn Marino     }
1834a238c70SJohn Marino 
1844a238c70SJohn Marino   MPFR_UNSIGNED_MINUS_MODULO (sh, MPFR_PREC (y));
1854a238c70SJohn Marino   /* it remains sh bits in less significant limb of y */
1864a238c70SJohn Marino 
1874a238c70SJohn Marino   d = *yp & MPFR_LIMB_MASK (sh);
1884a238c70SJohn Marino   *yp ^= d; /* set to zero lowest sh bits */
1894a238c70SJohn Marino 
1904a238c70SJohn Marino   MPFR_TMP_FREE (marker);
1914a238c70SJohn Marino 
1924a238c70SJohn Marino   if (exp < __gmpfr_emin - 1)
1934a238c70SJohn Marino     return mpfr_underflow (y, rnd_mode == MPFR_RNDN ? MPFR_RNDZ : rnd_mode,
1944a238c70SJohn Marino                            MPFR_SIGN (y));
1954a238c70SJohn Marino 
1964a238c70SJohn Marino   if (MPFR_UNLIKELY (d == 0 && inexact == 0))
1974a238c70SJohn Marino     nexttoinf = 0;  /* result is exact */
1984a238c70SJohn Marino   else
1994a238c70SJohn Marino     {
2004a238c70SJohn Marino       MPFR_UPDATE2_RND_MODE(rnd_mode, MPFR_SIGN (y));
2014a238c70SJohn Marino       switch (rnd_mode)
2024a238c70SJohn Marino         {
2034a238c70SJohn Marino         case MPFR_RNDZ:
2044a238c70SJohn Marino           inexact = - MPFR_INT_SIGN (y);  /* result is inexact */
2054a238c70SJohn Marino           nexttoinf = 0;
2064a238c70SJohn Marino           break;
2074a238c70SJohn Marino 
2084a238c70SJohn Marino         case MPFR_RNDA:
2094a238c70SJohn Marino           inexact = MPFR_INT_SIGN (y);
2104a238c70SJohn Marino           nexttoinf = 1;
2114a238c70SJohn Marino           break;
2124a238c70SJohn Marino 
2134a238c70SJohn Marino         default: /* should be MPFR_RNDN */
2144a238c70SJohn Marino           MPFR_ASSERTD (rnd_mode == MPFR_RNDN);
2154a238c70SJohn Marino           /* We have one more significant bit in yn. */
2164a238c70SJohn Marino           if (sh && d < (MPFR_LIMB_ONE << (sh - 1)))
2174a238c70SJohn Marino             {
2184a238c70SJohn Marino               inexact = - MPFR_INT_SIGN (y);
2194a238c70SJohn Marino               nexttoinf = 0;
2204a238c70SJohn Marino             }
2214a238c70SJohn Marino           else if (sh && d > (MPFR_LIMB_ONE << (sh - 1)))
2224a238c70SJohn Marino             {
2234a238c70SJohn Marino               inexact = MPFR_INT_SIGN (y);
2244a238c70SJohn Marino               nexttoinf = 1;
2254a238c70SJohn Marino             }
2264a238c70SJohn Marino           else /* sh = 0 or d = 1 << (sh-1) */
2274a238c70SJohn Marino             {
2284a238c70SJohn Marino               /* The first case is "false" even rounding (significant bits
2294a238c70SJohn Marino                  indicate even rounding, but the result is inexact, so up) ;
2304a238c70SJohn Marino                  The second case is the case where middle should be used to
2314a238c70SJohn Marino                  decide the direction of rounding (no further bit computed) ;
2324a238c70SJohn Marino                  The third is the true even rounding.
2334a238c70SJohn Marino               */
2344a238c70SJohn Marino               if ((sh && inexact) || (!sh && middle > 0) ||
2354a238c70SJohn Marino                   (!inexact && *yp & (MPFR_LIMB_ONE << sh)))
2364a238c70SJohn Marino                 {
2374a238c70SJohn Marino                   inexact = MPFR_INT_SIGN (y);
2384a238c70SJohn Marino                   nexttoinf = 1;
2394a238c70SJohn Marino                 }
2404a238c70SJohn Marino               else
2414a238c70SJohn Marino                 {
2424a238c70SJohn Marino                   inexact = - MPFR_INT_SIGN (y);
2434a238c70SJohn Marino                   nexttoinf = 0;
2444a238c70SJohn Marino                 }
2454a238c70SJohn Marino             }
2464a238c70SJohn Marino         }
2474a238c70SJohn Marino     }
2484a238c70SJohn Marino 
2494a238c70SJohn Marino   if (nexttoinf &&
2504a238c70SJohn Marino       MPFR_UNLIKELY (mpn_add_1 (yp, yp, yn, MPFR_LIMB_ONE << sh)))
2514a238c70SJohn Marino     {
2524a238c70SJohn Marino       exp++;
2534a238c70SJohn Marino       yp[yn-1] = MPFR_LIMB_HIGHBIT;
2544a238c70SJohn Marino     }
2554a238c70SJohn Marino 
2564a238c70SJohn Marino   /* Set the exponent. Warning! One may still have an underflow. */
2574a238c70SJohn Marino   MPFR_EXP (y) = exp;
2584a238c70SJohn Marino 
2594a238c70SJohn Marino   return mpfr_check_range (y, inexact, rnd_mode);
2604a238c70SJohn Marino }
2614a238c70SJohn Marino 
2624a238c70SJohn Marino int
mpfr_div_si(mpfr_ptr y,mpfr_srcptr x,long int u,mpfr_rnd_t rnd_mode)2634a238c70SJohn Marino mpfr_div_si (mpfr_ptr y, mpfr_srcptr x, long int u, mpfr_rnd_t rnd_mode)
2644a238c70SJohn Marino {
2654a238c70SJohn Marino   int res;
2664a238c70SJohn Marino 
2674a238c70SJohn Marino   MPFR_LOG_FUNC
2684a238c70SJohn Marino     (("x[%Pu]=%.*Rg u=%ld rnd=%d",
2694a238c70SJohn Marino       mpfr_get_prec(x), mpfr_log_prec, x, u, rnd_mode),
2704a238c70SJohn Marino      ("y[%Pu]=%.*Rg inexact=%d",
2714a238c70SJohn Marino       mpfr_get_prec(y), mpfr_log_prec, y, res));
2724a238c70SJohn Marino 
2734a238c70SJohn Marino   if (u >= 0)
2744a238c70SJohn Marino     res = mpfr_div_ui (y, x, u, rnd_mode);
2754a238c70SJohn Marino   else
2764a238c70SJohn Marino     {
2774a238c70SJohn Marino       res = -mpfr_div_ui (y, x, -u, MPFR_INVERT_RND (rnd_mode));
2784a238c70SJohn Marino       MPFR_CHANGE_SIGN (y);
2794a238c70SJohn Marino     }
2804a238c70SJohn Marino   return res;
2814a238c70SJohn Marino }
282