2010-02-10 06:17:59 -05:00
|
|
|
|
/* mpn_sb_divappr_q -- Schoolbook division using the M<>ller-Granlund 3/2
|
|
|
|
|
division algorithm, returning approximate quotient. The quotient returned
|
|
|
|
|
is either correct, or one too large.
|
2009-07-27 08:43:03 -04:00
|
|
|
|
|
2010-02-10 06:17:59 -05:00
|
|
|
|
Contributed to the GNU project by Torbjorn Granlund.
|
2009-07-27 08:43:03 -04:00
|
|
|
|
|
2010-02-10 06:17:59 -05:00
|
|
|
|
THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
|
|
|
|
|
SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
|
|
|
|
|
GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
|
2009-07-27 08:43:03 -04:00
|
|
|
|
|
2010-02-10 06:17:59 -05:00
|
|
|
|
Copyright 2007, 2009 Free Software Foundation, Inc.
|
2009-07-27 08:43:03 -04:00
|
|
|
|
|
2010-03-14 15:29:08 -04:00
|
|
|
|
Copyright 2010 William Hart (minor modifications)
|
|
|
|
|
|
2010-02-10 06:17:59 -05:00
|
|
|
|
This file is part of the GNU MP Library.
|
2009-07-27 08:43:03 -04:00
|
|
|
|
|
2010-02-10 06:17:59 -05:00
|
|
|
|
The GNU MP Library is free software; you can redistribute it and/or modify
|
2009-07-27 08:43:03 -04:00
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
2010-02-10 06:17:59 -05:00
|
|
|
|
the Free Software Foundation; either version 3 of the License, or (at your
|
2009-07-27 08:43:03 -04:00
|
|
|
|
option) any later version.
|
|
|
|
|
|
2010-02-10 06:17:59 -05:00
|
|
|
|
The GNU MP Library is distributed in the hope that it will be useful, but
|
2009-07-27 08:43:03 -04: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-02-10 06:17:59 -05:00
|
|
|
|
along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
|
|
|
|
|
|
2009-07-27 08:43:03 -04:00
|
|
|
|
|
|
|
|
|
#include "mpir.h"
|
|
|
|
|
#include "gmp-impl.h"
|
|
|
|
|
#include "longlong.h"
|
|
|
|
|
|
|
|
|
|
mp_limb_t
|
2010-02-10 06:17:59 -05:00
|
|
|
|
mpn_sb_divappr_q (mp_ptr qp,
|
|
|
|
|
mp_ptr np, mp_size_t nn,
|
|
|
|
|
mp_srcptr dp, mp_size_t dn,
|
|
|
|
|
mp_limb_t dinv)
|
2009-07-27 08:43:03 -04:00
|
|
|
|
{
|
2010-02-10 06:17:59 -05:00
|
|
|
|
mp_limb_t qh;
|
|
|
|
|
mp_size_t qn, i;
|
|
|
|
|
mp_limb_t n1, n0;
|
|
|
|
|
mp_limb_t d1, d0;
|
|
|
|
|
mp_limb_t cy, cy1;
|
|
|
|
|
mp_limb_t q;
|
|
|
|
|
mp_limb_t flag;
|
|
|
|
|
|
|
|
|
|
ASSERT (dn > 2);
|
|
|
|
|
ASSERT (nn >= dn);
|
|
|
|
|
ASSERT ((dp[dn-1] & GMP_NUMB_HIGHBIT) != 0);
|
|
|
|
|
|
|
|
|
|
np += nn;
|
|
|
|
|
|
|
|
|
|
qn = nn - dn;
|
|
|
|
|
if (qn + 1 < dn)
|
2009-10-11 22:05:16 -04:00
|
|
|
|
{
|
2010-02-10 06:17:59 -05:00
|
|
|
|
dp += dn - (qn + 1);
|
|
|
|
|
dn = qn + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qh = mpn_cmp (np - dn, dp, dn) >= 0;
|
|
|
|
|
if (qh != 0)
|
|
|
|
|
mpn_sub_n (np - dn, np - dn, dp, dn);
|
|
|
|
|
|
|
|
|
|
qp += qn;
|
|
|
|
|
|
|
|
|
|
dn -= 2; /* offset dn by 2 for main division loops,
|
|
|
|
|
saving two iterations in mpn_submul_1. */
|
|
|
|
|
d1 = dp[dn + 1];
|
|
|
|
|
d0 = dp[dn + 0];
|
|
|
|
|
|
|
|
|
|
np -= 2;
|
|
|
|
|
|
|
|
|
|
n1 = np[1];
|
|
|
|
|
|
|
|
|
|
for (i = qn - (dn + 2); i >= 0; i--)
|
2009-09-29 18:55:10 -04:00
|
|
|
|
{
|
2010-02-10 06:17:59 -05:00
|
|
|
|
np--;
|
|
|
|
|
if (UNLIKELY (n1 == d1) && np[1] == d0)
|
|
|
|
|
{
|
|
|
|
|
q = GMP_NUMB_MASK;
|
|
|
|
|
mpn_submul_1 (np - dn, dp, dn + 2, q);
|
|
|
|
|
n1 = np[1]; /* update n1, last loop's value will now be invalid */
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tdiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
|
|
|
|
|
|
|
|
|
|
cy = mpn_submul_1 (np - dn, dp, dn, q);
|
|
|
|
|
|
|
|
|
|
cy1 = n0 < cy;
|
|
|
|
|
n0 = (n0 - cy) & GMP_NUMB_MASK;
|
|
|
|
|
cy = n1 < cy1;
|
|
|
|
|
n1 -= cy1;
|
|
|
|
|
np[0] = n0;
|
|
|
|
|
|
|
|
|
|
if (UNLIKELY (cy != 0))
|
|
|
|
|
{
|
|
|
|
|
n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1);
|
|
|
|
|
q--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*--qp = q;
|
2009-09-29 18:55:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-02-10 06:17:59 -05:00
|
|
|
|
flag = ~CNST_LIMB(0);
|
2009-10-07 05:58:10 -04:00
|
|
|
|
|
2010-02-10 06:17:59 -05:00
|
|
|
|
if (dn >= 0)
|
2009-10-07 05:58:10 -04:00
|
|
|
|
{
|
2010-02-10 06:17:59 -05:00
|
|
|
|
for (i = dn; i > 0; i--)
|
|
|
|
|
{
|
|
|
|
|
np--;
|
|
|
|
|
if (UNLIKELY (n1 >= (d1 & flag)))
|
|
|
|
|
{
|
|
|
|
|
q = GMP_NUMB_MASK;
|
|
|
|
|
cy = mpn_submul_1 (np - dn, dp, dn + 2, q);
|
|
|
|
|
|
|
|
|
|
if (UNLIKELY (n1 != cy))
|
|
|
|
|
{
|
|
|
|
|
if (n1 < (cy & flag))
|
|
|
|
|
{
|
|
|
|
|
q--;
|
|
|
|
|
mpn_add_n (np - dn, np - dn, dp, dn + 2);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
flag = 0;
|
|
|
|
|
}
|
|
|
|
|
n1 = np[1];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tdiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
|
|
|
|
|
|
|
|
|
|
cy = mpn_submul_1 (np - dn, dp, dn, q);
|
|
|
|
|
|
|
|
|
|
cy1 = n0 < cy;
|
|
|
|
|
n0 = (n0 - cy) & GMP_NUMB_MASK;
|
|
|
|
|
cy = n1 < cy1;
|
|
|
|
|
n1 -= cy1;
|
|
|
|
|
np[0] = n0;
|
|
|
|
|
|
|
|
|
|
if (UNLIKELY (cy != 0))
|
|
|
|
|
{
|
|
|
|
|
n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1);
|
|
|
|
|
q--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*--qp = q;
|
|
|
|
|
|
|
|
|
|
/* Truncate operands. */
|
|
|
|
|
dn--;
|
|
|
|
|
dp++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
np--;
|
|
|
|
|
if (UNLIKELY (n1 >= (d1 & flag)))
|
|
|
|
|
{
|
|
|
|
|
q = GMP_NUMB_MASK;
|
|
|
|
|
cy = mpn_submul_1 (np, dp, 2, q);
|
|
|
|
|
|
|
|
|
|
if (UNLIKELY (n1 != cy))
|
|
|
|
|
{
|
|
|
|
|
if (n1 < (cy & flag))
|
|
|
|
|
{
|
|
|
|
|
q--;
|
|
|
|
|
add_ssaaaa (np[1], np[0], np[1], np[0], dp[1], dp[0]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
flag = 0;
|
|
|
|
|
}
|
|
|
|
|
n1 = np[1];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tdiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
|
|
|
|
|
|
|
|
|
|
np[1] = n1;
|
|
|
|
|
np[0] = n0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*--qp = q;
|
2009-10-07 05:58:10 -04:00
|
|
|
|
}
|
2009-07-27 08:43:03 -04:00
|
|
|
|
|
2010-02-10 06:17:59 -05:00
|
|
|
|
ASSERT_ALWAYS (np[1] == n1);
|
|
|
|
|
|
|
|
|
|
return qh;
|
|
|
|
|
}
|