2009-01-26 06:56:19 +01:00
|
|
|
/* mpfr_cmp2 -- exponent shift when subtracting two numbers.
|
|
|
|
|
2010-07-03 17:21:01 +02:00
|
|
|
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
|
2009-01-26 06:56:19 +01:00
|
|
|
Contributed by the Arenaire and Cacao projects, INRIA.
|
|
|
|
|
2010-07-03 17:21:01 +02:00
|
|
|
This file is part of the GNU MPFR Library.
|
2009-01-26 06:56:19 +01:00
|
|
|
|
2010-07-03 17:21:01 +02:00
|
|
|
The GNU MPFR Library is free software; you can redistribute it and/or modify
|
2009-01-26 06:56:19 +01:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
2010-07-03 17:21:01 +02:00
|
|
|
the Free Software Foundation; either version 3 of the License, or (at your
|
2009-01-26 06:56:19 +01:00
|
|
|
option) any later version.
|
|
|
|
|
2010-07-03 17:21:01 +02:00
|
|
|
The GNU MPFR Library is distributed in the hope that it will be useful, but
|
2009-01-26 06:56:19 +01:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2010-07-03 17:21:01 +02:00
|
|
|
along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
|
|
|
|
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
|
2009-01-26 06:56:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
#define MPFR_NEED_LONGLONG_H
|
|
|
|
#include "mpfr-impl.h"
|
|
|
|
|
|
|
|
/* If |b| != |c|, puts the number of canceled bits when one subtracts |c|
|
|
|
|
from |b| in *cancel. Returns the sign of the difference.
|
|
|
|
|
|
|
|
Assumes neither of b or c is NaN, +/- infinity, or +/- 0.
|
|
|
|
|
|
|
|
In other terms, if |b| != |c|, mpfr_cmp2 (b, c) returns
|
|
|
|
EXP(max(|b|,|c|)) - EXP(|b| - |c|).
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2010-07-03 17:21:01 +02:00
|
|
|
mpfr_cmp2 (mpfr_srcptr b, mpfr_srcptr c, mpfr_prec_t *cancel)
|
2009-01-26 06:56:19 +01:00
|
|
|
{
|
|
|
|
mp_limb_t *bp, *cp, bb, cc = 0, lastc = 0, dif, high_dif = 0;
|
|
|
|
mp_size_t bn, cn;
|
2010-07-03 17:21:01 +02:00
|
|
|
mpfr_uexp_t diff_exp;
|
|
|
|
mpfr_prec_t res = 0;
|
2009-01-26 06:56:19 +01:00
|
|
|
int sign;
|
|
|
|
|
|
|
|
/* b=c should not happen, since cmp2 is called only from agm
|
|
|
|
(with different variables), and from sub1 (if same b=c, then
|
|
|
|
sub1sp would be called instead */
|
|
|
|
MPFR_ASSERTD (b != c);
|
|
|
|
|
|
|
|
/* the cases b=0 or c=0 are also treated apart in agm and sub
|
|
|
|
(which calls sub1) */
|
|
|
|
MPFR_ASSERTD (MPFR_IS_PURE_FP(b));
|
|
|
|
MPFR_ASSERTD (MPFR_IS_PURE_FP(c));
|
|
|
|
|
|
|
|
if (MPFR_GET_EXP (b) >= MPFR_GET_EXP (c))
|
|
|
|
{
|
|
|
|
sign = 1;
|
2010-07-03 17:21:01 +02:00
|
|
|
diff_exp = (mpfr_uexp_t) MPFR_GET_EXP (b) - MPFR_GET_EXP (c);
|
2009-01-26 06:56:19 +01:00
|
|
|
|
|
|
|
bp = MPFR_MANT(b);
|
|
|
|
cp = MPFR_MANT(c);
|
|
|
|
|
2010-07-03 17:21:01 +02:00
|
|
|
bn = (MPFR_PREC(b) - 1) / GMP_NUMB_BITS;
|
|
|
|
cn = (MPFR_PREC(c) - 1) / GMP_NUMB_BITS; /* # of limbs of c minus 1 */
|
2009-01-26 06:56:19 +01:00
|
|
|
|
|
|
|
if (MPFR_UNLIKELY( diff_exp == 0 ))
|
|
|
|
{
|
|
|
|
while (bn >= 0 && cn >= 0 && bp[bn] == cp[cn])
|
|
|
|
{
|
|
|
|
bn--;
|
|
|
|
cn--;
|
2010-07-03 17:21:01 +02:00
|
|
|
res += GMP_NUMB_BITS;
|
2009-01-26 06:56:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MPFR_UNLIKELY (bn < 0))
|
|
|
|
{
|
|
|
|
if (MPFR_LIKELY (cn < 0)) /* b = c */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bp = cp;
|
|
|
|
bn = cn;
|
|
|
|
cn = -1;
|
|
|
|
sign = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MPFR_UNLIKELY (cn < 0))
|
|
|
|
/* c discards exactly the upper part of b */
|
|
|
|
{
|
|
|
|
unsigned int z;
|
|
|
|
|
|
|
|
MPFR_ASSERTD (bn >= 0);
|
|
|
|
|
|
|
|
while (bp[bn] == 0)
|
|
|
|
{
|
|
|
|
if (--bn < 0) /* b = c */
|
|
|
|
return 0;
|
2010-07-03 17:21:01 +02:00
|
|
|
res += GMP_NUMB_BITS;
|
2009-01-26 06:56:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
count_leading_zeros(z, bp[bn]); /* bp[bn] <> 0 */
|
|
|
|
*cancel = res + z;
|
|
|
|
return sign;
|
|
|
|
}
|
|
|
|
|
|
|
|
MPFR_ASSERTD (bn >= 0);
|
|
|
|
MPFR_ASSERTD (cn >= 0);
|
|
|
|
MPFR_ASSERTD (bp[bn] != cp[cn]);
|
|
|
|
if (bp[bn] < cp[cn])
|
|
|
|
{
|
|
|
|
mp_limb_t *tp;
|
|
|
|
mp_size_t tn;
|
|
|
|
|
|
|
|
tp = bp; bp = cp; cp = tp;
|
|
|
|
tn = bn; bn = cn; cn = tn;
|
|
|
|
sign = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* MPFR_EXP(b) >= MPFR_EXP(c) */
|
|
|
|
else /* MPFR_EXP(b) < MPFR_EXP(c) */
|
|
|
|
{
|
|
|
|
sign = -1;
|
2010-07-03 17:21:01 +02:00
|
|
|
diff_exp = (mpfr_uexp_t) MPFR_GET_EXP (c) - MPFR_GET_EXP (b);
|
2009-01-26 06:56:19 +01:00
|
|
|
|
|
|
|
bp = MPFR_MANT(c);
|
|
|
|
cp = MPFR_MANT(b);
|
|
|
|
|
2010-07-03 17:21:01 +02:00
|
|
|
bn = (MPFR_PREC(c) - 1) / GMP_NUMB_BITS;
|
|
|
|
cn = (MPFR_PREC(b) - 1) / GMP_NUMB_BITS;
|
2009-01-26 06:56:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* now we have removed the identical upper limbs of b and c
|
|
|
|
(can happen only when diff_exp = 0), and after the possible
|
2010-07-03 17:21:01 +02:00
|
|
|
swap, we have |b| > |c|: bp[bn] > cc, bn >= 0, cn >= 0,
|
|
|
|
diff_exp = EXP(b) - EXP(c).
|
|
|
|
*/
|
2009-01-26 06:56:19 +01:00
|
|
|
|
2010-07-03 17:21:01 +02:00
|
|
|
if (MPFR_LIKELY (diff_exp < GMP_NUMB_BITS))
|
2009-01-26 06:56:19 +01:00
|
|
|
{
|
|
|
|
cc = cp[cn] >> diff_exp;
|
2010-07-03 17:21:01 +02:00
|
|
|
/* warning: a shift by GMP_NUMB_BITS may give wrong results */
|
2009-01-26 06:56:19 +01:00
|
|
|
if (diff_exp)
|
2010-07-03 17:21:01 +02:00
|
|
|
lastc = cp[cn] << (GMP_NUMB_BITS - diff_exp);
|
2009-01-26 06:56:19 +01:00
|
|
|
cn--;
|
|
|
|
}
|
|
|
|
else
|
2010-07-03 17:21:01 +02:00
|
|
|
diff_exp -= GMP_NUMB_BITS; /* cc = 0 */
|
2009-01-26 06:56:19 +01:00
|
|
|
|
|
|
|
dif = bp[bn--] - cc; /* necessarily dif >= 1 */
|
2010-07-03 17:21:01 +02:00
|
|
|
MPFR_ASSERTD(dif >= 1);
|
|
|
|
|
|
|
|
/* now high_dif = 0, dif >= 1, lastc is the neglected part of cp[cn+1] */
|
2009-01-26 06:56:19 +01:00
|
|
|
|
|
|
|
while (MPFR_UNLIKELY ((cn >= 0 || lastc != 0)
|
|
|
|
&& (high_dif == 0) && (dif == 1)))
|
|
|
|
{ /* dif=1 implies diff_exp = 0 or 1 */
|
|
|
|
bb = (bn >= 0) ? bp[bn] : 0;
|
|
|
|
cc = lastc;
|
|
|
|
if (cn >= 0)
|
|
|
|
{
|
|
|
|
if (diff_exp == 0)
|
|
|
|
{
|
|
|
|
cc += cp[cn];
|
|
|
|
}
|
|
|
|
else /* diff_exp = 1 */
|
|
|
|
{
|
|
|
|
cc += cp[cn] >> 1;
|
2010-07-03 17:21:01 +02:00
|
|
|
lastc = cp[cn] << (GMP_NUMB_BITS - 1);
|
2009-01-26 06:56:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
lastc = 0;
|
|
|
|
high_dif = 1 - mpn_sub_n (&dif, &bb, &cc, 1);
|
|
|
|
bn--;
|
|
|
|
cn--;
|
2010-07-03 17:21:01 +02:00
|
|
|
res += GMP_NUMB_BITS;
|
2009-01-26 06:56:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (cn<0 and lastc=0) or (high_dif,dif)<>(0,1) */
|
|
|
|
|
|
|
|
if (MPFR_UNLIKELY (high_dif != 0)) /* high_dif == 1 */
|
|
|
|
{
|
|
|
|
res--;
|
|
|
|
if (dif != 0)
|
|
|
|
{
|
|
|
|
*cancel = res;
|
|
|
|
return sign;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* high_dif == 0 */
|
|
|
|
{
|
|
|
|
unsigned int z;
|
|
|
|
|
|
|
|
count_leading_zeros(z, dif); /* dif > 1 here */
|
|
|
|
res += z;
|
2010-07-03 17:21:01 +02:00
|
|
|
if (MPFR_LIKELY(dif != (MPFR_LIMB_ONE << (GMP_NUMB_BITS - z - 1))))
|
2009-01-26 06:56:19 +01:00
|
|
|
{ /* dif is not a power of two */
|
|
|
|
*cancel = res;
|
|
|
|
return sign;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now result is res + (low(b) < low(c)) */
|
|
|
|
while (MPFR_UNLIKELY (bn >= 0 && (cn >= 0 || lastc != 0)))
|
|
|
|
{
|
2010-07-03 17:21:01 +02:00
|
|
|
if (diff_exp >= GMP_NUMB_BITS)
|
|
|
|
diff_exp -= GMP_NUMB_BITS;
|
2009-01-26 06:56:19 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cc = lastc;
|
|
|
|
if (cn >= 0)
|
|
|
|
{
|
|
|
|
cc += cp[cn] >> diff_exp;
|
|
|
|
if (diff_exp != 0)
|
2010-07-03 17:21:01 +02:00
|
|
|
lastc = cp[cn] << (GMP_NUMB_BITS - diff_exp);
|
2009-01-26 06:56:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
lastc = 0;
|
|
|
|
cn--;
|
|
|
|
}
|
|
|
|
if (bp[bn] != cc)
|
|
|
|
{
|
|
|
|
*cancel = res + (bp[bn] < cc);
|
|
|
|
return sign;
|
|
|
|
}
|
|
|
|
bn--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bn < 0)
|
|
|
|
{
|
|
|
|
if (lastc != 0)
|
|
|
|
res++;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (cn >= 0 && cp[cn] == 0)
|
|
|
|
cn--;
|
|
|
|
if (cn >= 0)
|
|
|
|
res++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*cancel = res;
|
|
|
|
return sign;
|
|
|
|
}
|