1 /* mpfr_round_p -- check if an approximation is roundable. 2 3 Copyright 2005-2018 Free Software Foundation, Inc. 4 Contributed by the AriC and Caramba projects, INRIA. 5 6 This file is part of the GNU MPFR Library. 7 8 The GNU MPFR Library is free software; you can redistribute it and/or modify 9 it under the terms of the GNU Lesser General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or (at your 11 option) any later version. 12 13 The GNU MPFR Library is distributed in the hope that it will be useful, but 14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16 License for more details. 17 18 You should have received a copy of the GNU Lesser General Public License 19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see 20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., 21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23 #include "mpfr-impl.h" 24 25 /* Check against mpfr_can_round? */ 26 #if MPFR_WANT_ASSERT >= 2 27 int mpfr_round_p_2 (mp_limb_t *, mp_size_t, mpfr_exp_t, mpfr_prec_t); 28 int 29 mpfr_round_p (mp_limb_t *bp, mp_size_t bn, mpfr_exp_t err0, mpfr_prec_t prec) 30 { 31 int i1, i2; 32 33 MPFR_ASSERTN(bp[bn - 1] & MPFR_LIMB_HIGHBIT); 34 35 i1 = mpfr_round_p_2 (bp, bn, err0, prec); 36 37 /* Note: since revision 10747, mpfr_can_round_raw is supposed to be always 38 correct, whereas mpfr_round_p_2 might return 0 in some cases where one 39 could round, for example with err0=67 and prec=54: 40 b = 1111101101010001100011111011100010100011101111011011101111111111 41 thus we cannot compare i1 and i2, we only can check that we don't have 42 i1 <> 0 and i2 = 0. 43 */ 44 i2 = mpfr_can_round_raw (bp, bn, MPFR_SIGN_POS, err0, 45 MPFR_RNDN, MPFR_RNDZ, prec); 46 if (i1 && (i2 == 0)) 47 { 48 fprintf (stderr, "mpfr_round_p(%d) != mpfr_can_round(%d)!\n" 49 "bn = %lu, err0 = %ld, prec = %lu\nbp = ", i1, i2, 50 (unsigned long) bn, (long) err0, (unsigned long) prec); 51 gmp_fprintf (stderr, "%NX\n", bp, bn); 52 MPFR_ASSERTN (0); 53 } 54 55 return i1; 56 } 57 # define mpfr_round_p mpfr_round_p_2 58 #endif /* MPFR_WANT_ASSERT >= 2 */ 59 60 /* 61 * Assuming {bp, bn} is an approximation of a non-singular number 62 * with error at most equal to 2^(EXP(b)-err0) (`err0' bits of b are known) 63 * of direction unknown, check if we can round b toward zero with 64 * precision prec. 65 */ 66 int 67 mpfr_round_p (mp_limb_t *bp, mp_size_t bn, mpfr_exp_t err0, mpfr_prec_t prec) 68 { 69 mpfr_prec_t err; 70 mp_size_t k, n; 71 mp_limb_t tmp, mask; 72 int s; 73 74 MPFR_ASSERTD(bp[bn - 1] & MPFR_LIMB_HIGHBIT); 75 76 err = (mpfr_prec_t) bn * GMP_NUMB_BITS; 77 if (MPFR_UNLIKELY (err0 <= 0 || (mpfr_uexp_t) err0 <= prec || prec >= err)) 78 return 0; /* can't round */ 79 err = MIN (err, (mpfr_uexp_t) err0); 80 81 k = prec / GMP_NUMB_BITS; 82 s = GMP_NUMB_BITS - prec % GMP_NUMB_BITS; 83 n = err / GMP_NUMB_BITS - k; 84 85 MPFR_ASSERTD (n >= 0); 86 MPFR_ASSERTD (bn > k); 87 88 /* Check first limb */ 89 bp += bn - 1 - k; 90 tmp = *bp--; 91 mask = s == GMP_NUMB_BITS ? MPFR_LIMB_MAX : MPFR_LIMB_MASK (s); 92 tmp &= mask; 93 94 if (MPFR_LIKELY (n == 0)) 95 { 96 /* prec and error are in the same limb */ 97 s = GMP_NUMB_BITS - err % GMP_NUMB_BITS; 98 MPFR_ASSERTD (s < GMP_NUMB_BITS); 99 tmp >>= s; 100 mask >>= s; 101 return tmp != 0 && tmp != mask; 102 } 103 else if (MPFR_UNLIKELY (tmp == 0)) 104 { 105 /* Check if all (n-1) limbs are 0 */ 106 while (--n) 107 if (*bp-- != 0) 108 return 1; 109 /* Check if final error limb is 0 */ 110 s = GMP_NUMB_BITS - err % GMP_NUMB_BITS; 111 if (s == GMP_NUMB_BITS) 112 return 0; 113 tmp = *bp >> s; 114 return tmp != 0; 115 } 116 else if (MPFR_UNLIKELY (tmp == mask)) 117 { 118 /* Check if all (n-1) limbs are 11111111111111111 */ 119 while (--n) 120 if (*bp-- != MPFR_LIMB_MAX) 121 return 1; 122 /* Check if final error limb is 0 */ 123 s = GMP_NUMB_BITS - err % GMP_NUMB_BITS; 124 if (s == GMP_NUMB_BITS) 125 return 0; 126 tmp = *bp >> s; 127 return tmp != (MPFR_LIMB_MAX >> s); 128 } 129 else 130 { 131 /* First limb is different from 000000 or 1111111 */ 132 return 1; 133 } 134 } 135