14a238c70SJohn Marino /* mpfr_round_p -- check if an approximation is roundable.
24a238c70SJohn Marino
3*ab6d115fSJohn Marino Copyright 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 #include "mpfr-impl.h"
244a238c70SJohn Marino
254a238c70SJohn Marino /* Check against mpfr_can_round? */
264a238c70SJohn Marino #ifdef WANT_ASSERT
274a238c70SJohn Marino # if WANT_ASSERT >= 2
284a238c70SJohn Marino int mpfr_round_p_2 (mp_limb_t *, mp_size_t, mpfr_exp_t, mpfr_prec_t);
294a238c70SJohn Marino int
mpfr_round_p(mp_limb_t * bp,mp_size_t bn,mpfr_exp_t err0,mpfr_prec_t prec)304a238c70SJohn Marino mpfr_round_p (mp_limb_t *bp, mp_size_t bn, mpfr_exp_t err0, mpfr_prec_t prec)
314a238c70SJohn Marino {
324a238c70SJohn Marino int i1, i2;
334a238c70SJohn Marino
344a238c70SJohn Marino i1 = mpfr_round_p_2 (bp, bn, err0, prec);
354a238c70SJohn Marino i2 = mpfr_can_round_raw (bp, bn, MPFR_SIGN_POS, err0,
364a238c70SJohn Marino MPFR_RNDN, MPFR_RNDZ, prec);
374a238c70SJohn Marino if (i1 != i2)
384a238c70SJohn Marino {
394a238c70SJohn Marino fprintf (stderr, "mpfr_round_p(%d) != mpfr_can_round(%d)!\n"
404a238c70SJohn Marino "bn = %lu, err0 = %ld, prec = %lu\nbp = ", i1, i2,
414a238c70SJohn Marino (unsigned long) bn, (long) err0, (unsigned long) prec);
424a238c70SJohn Marino gmp_fprintf (stderr, "%NX\n", bp, bn);
434a238c70SJohn Marino MPFR_ASSERTN (0);
444a238c70SJohn Marino }
454a238c70SJohn Marino return i1;
464a238c70SJohn Marino }
474a238c70SJohn Marino # define mpfr_round_p mpfr_round_p_2
484a238c70SJohn Marino # endif
494a238c70SJohn Marino #endif
504a238c70SJohn Marino
514a238c70SJohn Marino /*
524a238c70SJohn Marino * Assuming {bp, bn} is an approximation of a non-singular number
534a238c70SJohn Marino * with error at most equal to 2^(EXP(b)-err0) (`err0' bits of b are known)
544a238c70SJohn Marino * of direction unknown, check if we can round b toward zero with
554a238c70SJohn Marino * precision prec.
564a238c70SJohn Marino */
574a238c70SJohn Marino int
mpfr_round_p(mp_limb_t * bp,mp_size_t bn,mpfr_exp_t err0,mpfr_prec_t prec)584a238c70SJohn Marino mpfr_round_p (mp_limb_t *bp, mp_size_t bn, mpfr_exp_t err0, mpfr_prec_t prec)
594a238c70SJohn Marino {
604a238c70SJohn Marino mpfr_prec_t err;
614a238c70SJohn Marino mp_size_t k, n;
624a238c70SJohn Marino mp_limb_t tmp, mask;
634a238c70SJohn Marino int s;
644a238c70SJohn Marino
654a238c70SJohn Marino err = (mpfr_prec_t) bn * GMP_NUMB_BITS;
664a238c70SJohn Marino if (MPFR_UNLIKELY (err0 <= 0 || (mpfr_uexp_t) err0 <= prec || prec >= err))
674a238c70SJohn Marino return 0; /* can't round */
684a238c70SJohn Marino err = MIN (err, (mpfr_uexp_t) err0);
694a238c70SJohn Marino
704a238c70SJohn Marino k = prec / GMP_NUMB_BITS;
714a238c70SJohn Marino s = GMP_NUMB_BITS - prec%GMP_NUMB_BITS;
724a238c70SJohn Marino n = err / GMP_NUMB_BITS - k;
734a238c70SJohn Marino
744a238c70SJohn Marino MPFR_ASSERTD (n >= 0);
754a238c70SJohn Marino MPFR_ASSERTD (bn > k);
764a238c70SJohn Marino
774a238c70SJohn Marino /* Check first limb */
784a238c70SJohn Marino bp += bn-1-k;
794a238c70SJohn Marino tmp = *bp--;
804a238c70SJohn Marino mask = s == GMP_NUMB_BITS ? MP_LIMB_T_MAX : MPFR_LIMB_MASK (s);
814a238c70SJohn Marino tmp &= mask;
824a238c70SJohn Marino
834a238c70SJohn Marino if (MPFR_LIKELY (n == 0))
844a238c70SJohn Marino {
854a238c70SJohn Marino /* prec and error are in the same limb */
864a238c70SJohn Marino s = GMP_NUMB_BITS - err % GMP_NUMB_BITS;
874a238c70SJohn Marino MPFR_ASSERTD (s < GMP_NUMB_BITS);
884a238c70SJohn Marino tmp >>= s;
894a238c70SJohn Marino mask >>= s;
904a238c70SJohn Marino return tmp != 0 && tmp != mask;
914a238c70SJohn Marino }
924a238c70SJohn Marino else if (MPFR_UNLIKELY (tmp == 0))
934a238c70SJohn Marino {
944a238c70SJohn Marino /* Check if all (n-1) limbs are 0 */
954a238c70SJohn Marino while (--n)
964a238c70SJohn Marino if (*bp-- != 0)
974a238c70SJohn Marino return 1;
984a238c70SJohn Marino /* Check if final error limb is 0 */
994a238c70SJohn Marino s = GMP_NUMB_BITS - err % GMP_NUMB_BITS;
1004a238c70SJohn Marino if (s == GMP_NUMB_BITS)
1014a238c70SJohn Marino return 0;
1024a238c70SJohn Marino tmp = *bp >> s;
1034a238c70SJohn Marino return tmp != 0;
1044a238c70SJohn Marino }
1054a238c70SJohn Marino else if (MPFR_UNLIKELY (tmp == mask))
1064a238c70SJohn Marino {
1074a238c70SJohn Marino /* Check if all (n-1) limbs are 11111111111111111 */
1084a238c70SJohn Marino while (--n)
1094a238c70SJohn Marino if (*bp-- != MP_LIMB_T_MAX)
1104a238c70SJohn Marino return 1;
1114a238c70SJohn Marino /* Check if final error limb is 0 */
1124a238c70SJohn Marino s = GMP_NUMB_BITS - err % GMP_NUMB_BITS;
1134a238c70SJohn Marino if (s == GMP_NUMB_BITS)
1144a238c70SJohn Marino return 0;
1154a238c70SJohn Marino tmp = *bp >> s;
1164a238c70SJohn Marino return tmp != (MP_LIMB_T_MAX >> s);
1174a238c70SJohn Marino }
1184a238c70SJohn Marino else
1194a238c70SJohn Marino {
1204a238c70SJohn Marino /* First limb is different from 000000 or 1111111 */
1214a238c70SJohn Marino return 1;
1224a238c70SJohn Marino }
1234a238c70SJohn Marino }
124