xref: /dragonfly/contrib/mpfr/src/round_prec.c (revision ab6d115f)
14a238c70SJohn Marino /* mpfr_round_raw_generic, mpfr_round_raw2, mpfr_round_raw, mpfr_prec_round,
24a238c70SJohn Marino    mpfr_can_round, mpfr_can_round_raw -- various rounding functions
34a238c70SJohn Marino 
4*ab6d115fSJohn Marino Copyright 1999, 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 #define mpfr_round_raw_generic mpfr_round_raw
274a238c70SJohn Marino #define flag 0
284a238c70SJohn Marino #define use_inexp 1
294a238c70SJohn Marino #include "round_raw_generic.c"
304a238c70SJohn Marino 
314a238c70SJohn Marino #define mpfr_round_raw_generic mpfr_round_raw_2
324a238c70SJohn Marino #define flag 1
334a238c70SJohn Marino #define use_inexp 0
344a238c70SJohn Marino #include "round_raw_generic.c"
354a238c70SJohn Marino 
364a238c70SJohn Marino /* Seems to be unused. Remove comment to implement it.
374a238c70SJohn Marino #define mpfr_round_raw_generic mpfr_round_raw_3
384a238c70SJohn Marino #define flag 1
394a238c70SJohn Marino #define use_inexp 1
404a238c70SJohn Marino #include "round_raw_generic.c"
414a238c70SJohn Marino */
424a238c70SJohn Marino 
434a238c70SJohn Marino #define mpfr_round_raw_generic mpfr_round_raw_4
444a238c70SJohn Marino #define flag 0
454a238c70SJohn Marino #define use_inexp 0
464a238c70SJohn Marino #include "round_raw_generic.c"
474a238c70SJohn Marino 
484a238c70SJohn Marino int
mpfr_prec_round(mpfr_ptr x,mpfr_prec_t prec,mpfr_rnd_t rnd_mode)494a238c70SJohn Marino mpfr_prec_round (mpfr_ptr x, mpfr_prec_t prec, mpfr_rnd_t rnd_mode)
504a238c70SJohn Marino {
514a238c70SJohn Marino   mp_limb_t *tmp, *xp;
524a238c70SJohn Marino   int carry, inexact;
534a238c70SJohn Marino   mpfr_prec_t nw, ow;
544a238c70SJohn Marino   MPFR_TMP_DECL(marker);
554a238c70SJohn Marino 
564a238c70SJohn Marino   MPFR_ASSERTN(prec >= MPFR_PREC_MIN && prec <= MPFR_PREC_MAX);
574a238c70SJohn Marino 
58*ab6d115fSJohn Marino   nw = MPFR_PREC2LIMBS (prec); /* needed allocated limbs */
594a238c70SJohn Marino 
604a238c70SJohn Marino   /* check if x has enough allocated space for the significand */
614a238c70SJohn Marino   /* Get the number of limbs from the precision.
624a238c70SJohn Marino      (Compatible with all allocation methods) */
63*ab6d115fSJohn Marino   ow = MPFR_LIMB_SIZE (x);
644a238c70SJohn Marino   if (nw > ow)
654a238c70SJohn Marino     {
664a238c70SJohn Marino       /* FIXME: Variable can't be created using custom allocation,
674a238c70SJohn Marino          MPFR_DECL_INIT or GROUP_ALLOC: How to detect? */
684a238c70SJohn Marino       ow = MPFR_GET_ALLOC_SIZE(x);
694a238c70SJohn Marino       if (nw > ow)
704a238c70SJohn Marino        {
714a238c70SJohn Marino          /* Realloc significand */
724a238c70SJohn Marino          mpfr_limb_ptr tmpx = (mpfr_limb_ptr) (*__gmp_reallocate_func)
734a238c70SJohn Marino            (MPFR_GET_REAL_PTR(x), MPFR_MALLOC_SIZE(ow), MPFR_MALLOC_SIZE(nw));
744a238c70SJohn Marino          MPFR_SET_MANT_PTR(x, tmpx); /* mant ptr must be set
754a238c70SJohn Marino                                         before alloc size */
764a238c70SJohn Marino          MPFR_SET_ALLOC_SIZE(x, nw); /* new number of allocated limbs */
774a238c70SJohn Marino        }
784a238c70SJohn Marino     }
794a238c70SJohn Marino 
804a238c70SJohn Marino   if (MPFR_UNLIKELY( MPFR_IS_SINGULAR(x) ))
814a238c70SJohn Marino     {
824a238c70SJohn Marino       MPFR_PREC(x) = prec; /* Special value: need to set prec */
834a238c70SJohn Marino       if (MPFR_IS_NAN(x))
844a238c70SJohn Marino         MPFR_RET_NAN;
854a238c70SJohn Marino       MPFR_ASSERTD(MPFR_IS_INF(x) || MPFR_IS_ZERO(x));
864a238c70SJohn Marino       return 0; /* infinity and zero are exact */
874a238c70SJohn Marino     }
884a238c70SJohn Marino 
894a238c70SJohn Marino   /* x is a non-zero real number */
904a238c70SJohn Marino 
914a238c70SJohn Marino   MPFR_TMP_MARK(marker);
924a238c70SJohn Marino   tmp = MPFR_TMP_LIMBS_ALLOC (nw);
934a238c70SJohn Marino   xp = MPFR_MANT(x);
944a238c70SJohn Marino   carry = mpfr_round_raw (tmp, xp, MPFR_PREC(x), MPFR_IS_NEG(x),
954a238c70SJohn Marino                           prec, rnd_mode, &inexact);
964a238c70SJohn Marino   MPFR_PREC(x) = prec;
974a238c70SJohn Marino 
984a238c70SJohn Marino   if (MPFR_UNLIKELY(carry))
994a238c70SJohn Marino     {
1004a238c70SJohn Marino       mpfr_exp_t exp = MPFR_EXP (x);
1014a238c70SJohn Marino 
1024a238c70SJohn Marino       if (MPFR_UNLIKELY(exp == __gmpfr_emax))
1034a238c70SJohn Marino         (void) mpfr_overflow(x, rnd_mode, MPFR_SIGN(x));
1044a238c70SJohn Marino       else
1054a238c70SJohn Marino         {
1064a238c70SJohn Marino           MPFR_ASSERTD (exp < __gmpfr_emax);
1074a238c70SJohn Marino           MPFR_SET_EXP (x, exp + 1);
1084a238c70SJohn Marino           xp[nw - 1] = MPFR_LIMB_HIGHBIT;
1094a238c70SJohn Marino           if (nw - 1 > 0)
1104a238c70SJohn Marino             MPN_ZERO(xp, nw - 1);
1114a238c70SJohn Marino         }
1124a238c70SJohn Marino     }
1134a238c70SJohn Marino   else
1144a238c70SJohn Marino     MPN_COPY(xp, tmp, nw);
1154a238c70SJohn Marino 
1164a238c70SJohn Marino   MPFR_TMP_FREE(marker);
1174a238c70SJohn Marino   return inexact;
1184a238c70SJohn Marino }
1194a238c70SJohn Marino 
1204a238c70SJohn Marino /* assumption: GMP_NUMB_BITS is a power of 2 */
1214a238c70SJohn Marino 
1224a238c70SJohn Marino /* assuming b is an approximation to x in direction rnd1 with error at
1234a238c70SJohn Marino    most 2^(MPFR_EXP(b)-err), returns 1 if one is able to round exactly
1244a238c70SJohn Marino    x to precision prec with direction rnd2, and 0 otherwise.
1254a238c70SJohn Marino 
1264a238c70SJohn Marino    Side effects: none.
1274a238c70SJohn Marino */
1284a238c70SJohn Marino 
1294a238c70SJohn Marino int
mpfr_can_round(mpfr_srcptr b,mpfr_exp_t err,mpfr_rnd_t rnd1,mpfr_rnd_t rnd2,mpfr_prec_t prec)1304a238c70SJohn Marino mpfr_can_round (mpfr_srcptr b, mpfr_exp_t err, mpfr_rnd_t rnd1,
1314a238c70SJohn Marino                 mpfr_rnd_t rnd2, mpfr_prec_t prec)
1324a238c70SJohn Marino {
1334a238c70SJohn Marino   if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(b)))
1344a238c70SJohn Marino     return 0; /* We cannot round if Zero, Nan or Inf */
1354a238c70SJohn Marino   else
1364a238c70SJohn Marino     return mpfr_can_round_raw (MPFR_MANT(b), MPFR_LIMB_SIZE(b),
1374a238c70SJohn Marino                                MPFR_SIGN(b), err, rnd1, rnd2, prec);
1384a238c70SJohn Marino }
1394a238c70SJohn Marino 
1404a238c70SJohn Marino int
mpfr_can_round_raw(const mp_limb_t * bp,mp_size_t bn,int neg,mpfr_exp_t err0,mpfr_rnd_t rnd1,mpfr_rnd_t rnd2,mpfr_prec_t prec)1414a238c70SJohn Marino mpfr_can_round_raw (const mp_limb_t *bp, mp_size_t bn, int neg, mpfr_exp_t err0,
1424a238c70SJohn Marino                     mpfr_rnd_t rnd1, mpfr_rnd_t rnd2, mpfr_prec_t prec)
1434a238c70SJohn Marino {
1444a238c70SJohn Marino   mpfr_prec_t err;
1454a238c70SJohn Marino   mp_size_t k, k1, tn;
1464a238c70SJohn Marino   int s, s1;
1474a238c70SJohn Marino   mp_limb_t cc, cc2;
1484a238c70SJohn Marino   mp_limb_t *tmp;
1494a238c70SJohn Marino   MPFR_TMP_DECL(marker);
1504a238c70SJohn Marino 
1514a238c70SJohn Marino   if (MPFR_UNLIKELY(err0 < 0 || (mpfr_uexp_t) err0 <= prec))
1524a238c70SJohn Marino     return 0;  /* can't round */
1534a238c70SJohn Marino   else if (MPFR_UNLIKELY (prec > (mpfr_prec_t) bn * GMP_NUMB_BITS))
1544a238c70SJohn Marino     { /* then ulp(b) < precision < error */
1554a238c70SJohn Marino       return rnd2 == MPFR_RNDN && (mpfr_uexp_t) err0 - 2 >= prec;
1564a238c70SJohn Marino       /* can round only in rounding to the nearest and err0 >= prec + 2 */
1574a238c70SJohn Marino     }
1584a238c70SJohn Marino 
1594a238c70SJohn Marino   MPFR_ASSERT_SIGN(neg);
1604a238c70SJohn Marino   neg = MPFR_IS_NEG_SIGN(neg);
1614a238c70SJohn Marino 
1624a238c70SJohn Marino   /* if the error is smaller than ulp(b), then anyway it will propagate
1634a238c70SJohn Marino      up to ulp(b) */
1644a238c70SJohn Marino   err = ((mpfr_uexp_t) err0 > (mpfr_prec_t) bn * GMP_NUMB_BITS) ?
1654a238c70SJohn Marino     (mpfr_prec_t) bn * GMP_NUMB_BITS : (mpfr_prec_t) err0;
1664a238c70SJohn Marino 
1674a238c70SJohn Marino   /* warning: if k = m*GMP_NUMB_BITS, consider limb m-1 and not m */
1684a238c70SJohn Marino   k = (err - 1) / GMP_NUMB_BITS;
1694a238c70SJohn Marino   MPFR_UNSIGNED_MINUS_MODULO(s, err);
1704a238c70SJohn Marino   /* the error corresponds to bit s in limb k, the most significant limb
1714a238c70SJohn Marino      being limb 0 */
1724a238c70SJohn Marino 
1734a238c70SJohn Marino   k1 = (prec - 1) / GMP_NUMB_BITS;
1744a238c70SJohn Marino   MPFR_UNSIGNED_MINUS_MODULO(s1, prec);
1754a238c70SJohn Marino   /* the last significant bit is bit s1 in limb k1 */
1764a238c70SJohn Marino 
1774a238c70SJohn Marino   /* don't need to consider the k1 most significant limbs */
1784a238c70SJohn Marino   k -= k1;
1794a238c70SJohn Marino   bn -= k1;
1804a238c70SJohn Marino   prec -= (mpfr_prec_t) k1 * GMP_NUMB_BITS;
1814a238c70SJohn Marino 
1824a238c70SJohn Marino   /* if when adding or subtracting (1 << s) in bp[bn-1-k], it does not
1834a238c70SJohn Marino      change bp[bn-1] >> s1, then we can round */
1844a238c70SJohn Marino   MPFR_TMP_MARK(marker);
1854a238c70SJohn Marino   tn = bn;
1864a238c70SJohn Marino   k++; /* since we work with k+1 everywhere */
1874a238c70SJohn Marino   tmp = MPFR_TMP_LIMBS_ALLOC (tn);
1884a238c70SJohn Marino   if (bn > k)
1894a238c70SJohn Marino     MPN_COPY (tmp, bp, bn - k);
1904a238c70SJohn Marino 
1914a238c70SJohn Marino   MPFR_ASSERTD (k > 0);
1924a238c70SJohn Marino 
1934a238c70SJohn Marino   /* Transform RNDD and RNDU to Zero / Away */
1944a238c70SJohn Marino   MPFR_ASSERTD((neg == 0) || (neg ==1));
1954a238c70SJohn Marino   if (MPFR_IS_RNDUTEST_OR_RNDDNOTTEST(rnd1, neg))
1964a238c70SJohn Marino     rnd1 = MPFR_RNDZ;
1974a238c70SJohn Marino 
1984a238c70SJohn Marino   switch (rnd1)
1994a238c70SJohn Marino     {
2004a238c70SJohn Marino     case MPFR_RNDZ:
2014a238c70SJohn Marino       /* Round to Zero */
2024a238c70SJohn Marino       cc = (bp[bn - 1] >> s1) & 1;
2034a238c70SJohn Marino       /* mpfr_round_raw2 returns 1 if one should add 1 at ulp(b,prec),
2044a238c70SJohn Marino          and 0 otherwise */
2054a238c70SJohn Marino       cc ^= mpfr_round_raw2 (bp, bn, neg, rnd2, prec);
2064a238c70SJohn Marino       /* cc is the new value of bit s1 in bp[bn-1] */
2074a238c70SJohn Marino       /* now round b + 2^(MPFR_EXP(b)-err) */
2084a238c70SJohn Marino       cc2 = mpn_add_1 (tmp + bn - k, bp + bn - k, k, MPFR_LIMB_ONE << s);
2094a238c70SJohn Marino       break;
2104a238c70SJohn Marino     case MPFR_RNDN:
2114a238c70SJohn Marino       /* Round to nearest */
2124a238c70SJohn Marino        /* first round b+2^(MPFR_EXP(b)-err) */
2134a238c70SJohn Marino       cc = mpn_add_1 (tmp + bn - k, bp + bn - k, k, MPFR_LIMB_ONE << s);
2144a238c70SJohn Marino       cc = (tmp[bn - 1] >> s1) & 1; /* gives 0 when cc=1 */
2154a238c70SJohn Marino       cc ^= mpfr_round_raw2 (tmp, bn, neg, rnd2, prec);
2164a238c70SJohn Marino       /* now round b-2^(MPFR_EXP(b)-err) */
2174a238c70SJohn Marino       cc2 = mpn_sub_1 (tmp + bn - k, bp + bn - k, k, MPFR_LIMB_ONE << s);
2184a238c70SJohn Marino       break;
2194a238c70SJohn Marino     default:
2204a238c70SJohn Marino       /* Round away */
2214a238c70SJohn Marino       cc = (bp[bn - 1] >> s1) & 1;
2224a238c70SJohn Marino       cc ^= mpfr_round_raw2 (bp, bn, neg, rnd2, prec);
2234a238c70SJohn Marino       /* now round b +/- 2^(MPFR_EXP(b)-err) */
2244a238c70SJohn Marino       cc2 = mpn_sub_1 (tmp + bn - k, bp + bn - k, k, MPFR_LIMB_ONE << s);
2254a238c70SJohn Marino       break;
2264a238c70SJohn Marino     }
2274a238c70SJohn Marino 
2284a238c70SJohn Marino   /* if cc2 is 1, then a carry or borrow propagates to the next limb */
2294a238c70SJohn Marino   if (cc2 && cc)
2304a238c70SJohn Marino     {
2314a238c70SJohn Marino       MPFR_TMP_FREE(marker);
2324a238c70SJohn Marino       return 0;
2334a238c70SJohn Marino     }
2344a238c70SJohn Marino 
2354a238c70SJohn Marino   cc2 = (tmp[bn - 1] >> s1) & 1;
2364a238c70SJohn Marino   cc2 ^= mpfr_round_raw2 (tmp, bn, neg, rnd2, prec);
2374a238c70SJohn Marino 
2384a238c70SJohn Marino   MPFR_TMP_FREE(marker);
2394a238c70SJohn Marino   return cc == cc2;
2404a238c70SJohn Marino }
241