|
Packit |
5c3484 |
/* invert.c -- Compute floor((B^{2n}-1)/U) - B^n.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
Contributed to the GNU project by Marco Bodrato.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
|
|
Packit |
5c3484 |
SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
|
|
Packit |
5c3484 |
GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
Copyright (C) 2007, 2009, 2010, 2012, 2014-2015 Free Software Foundation, Inc.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
This file is part of the GNU MP Library.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library is free software; you can redistribute it and/or modify
|
|
Packit |
5c3484 |
it under the terms of either:
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
* the GNU Lesser General Public License as published by the Free
|
|
Packit |
5c3484 |
Software Foundation; either version 3 of the License, or (at your
|
|
Packit |
5c3484 |
option) any later version.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
or
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
* the GNU General Public License as published by the Free Software
|
|
Packit |
5c3484 |
Foundation; either version 2 of the License, or (at your option) any
|
|
Packit |
5c3484 |
later version.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
or both in parallel, as here.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library is distributed in the hope that it will be useful, but
|
|
Packit |
5c3484 |
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
Packit |
5c3484 |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
Packit |
5c3484 |
for more details.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
You should have received copies of the GNU General Public License and the
|
|
Packit |
5c3484 |
GNU Lesser General Public License along with the GNU MP Library. If not,
|
|
Packit |
5c3484 |
see https://www.gnu.org/licenses/. */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#include "gmp.h"
|
|
Packit |
5c3484 |
#include "gmp-impl.h"
|
|
Packit |
5c3484 |
#include "longlong.h"
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
mpn_invert (mp_ptr ip, mp_srcptr dp, mp_size_t n, mp_ptr scratch)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
ASSERT (n > 0);
|
|
Packit |
5c3484 |
ASSERT (dp[n-1] & GMP_NUMB_HIGHBIT);
|
|
Packit |
5c3484 |
ASSERT (! MPN_OVERLAP_P (ip, n, dp, n));
|
|
Packit |
5c3484 |
ASSERT (! MPN_OVERLAP_P (ip, n, scratch, mpn_invertappr_itch(n)));
|
|
Packit |
5c3484 |
ASSERT (! MPN_OVERLAP_P (dp, n, scratch, mpn_invertappr_itch(n)));
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (n == 1)
|
|
Packit |
5c3484 |
invert_limb (*ip, *dp);
|
|
Packit |
5c3484 |
else if (BELOW_THRESHOLD (n, INV_APPR_THRESHOLD))
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
/* Maximum scratch needed by this branch: 2*n */
|
|
Packit |
5c3484 |
mp_size_t i;
|
|
Packit |
5c3484 |
mp_ptr xp;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
xp = scratch; /* 2 * n limbs */
|
|
Packit |
5c3484 |
/* n > 1 here */
|
|
Packit |
5c3484 |
i = n;
|
|
Packit |
5c3484 |
do
|
|
Packit |
5c3484 |
xp[--i] = GMP_NUMB_MAX;
|
|
Packit |
5c3484 |
while (i);
|
|
Packit |
5c3484 |
mpn_com (xp + n, dp, n);
|
|
Packit |
5c3484 |
if (n == 2) {
|
|
Packit |
5c3484 |
mpn_divrem_2 (ip, 0, xp, 4, dp);
|
|
Packit |
5c3484 |
} else {
|
|
Packit |
5c3484 |
gmp_pi1_t inv;
|
|
Packit |
5c3484 |
invert_pi1 (inv, dp[n-1], dp[n-2]);
|
|
Packit |
5c3484 |
/* FIXME: should we use dcpi1_div_q, for big sizes? */
|
|
Packit |
5c3484 |
mpn_sbpi1_div_q (ip, xp, 2 * n, dp, n, inv.inv32);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
else { /* Use approximated inverse; correct the result if needed. */
|
|
Packit |
5c3484 |
mp_limb_t e; /* The possible error in the approximate inverse */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
ASSERT ( mpn_invert_itch (n) >= mpn_invertappr_itch (n) );
|
|
Packit |
5c3484 |
e = mpn_ni_invertappr (ip, dp, n, scratch);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (UNLIKELY (e)) { /* Assume the error can only be "0" (no error) or "1". */
|
|
Packit |
5c3484 |
/* Code to detect and correct the "off by one" approximation. */
|
|
Packit |
5c3484 |
mpn_mul_n (scratch, ip, dp, n);
|
|
Packit |
5c3484 |
e = mpn_add_n (scratch, scratch, dp, n); /* FIXME: we only need e.*/
|
|
Packit |
5c3484 |
if (LIKELY(e)) /* The high part can not give a carry by itself. */
|
|
Packit |
5c3484 |
e = mpn_add_nc (scratch + n, scratch + n, dp, n, e); /* FIXME:e */
|
|
Packit |
5c3484 |
/* If the value was wrong (no carry), correct it (increment). */
|
|
Packit |
5c3484 |
e ^= CNST_LIMB (1);
|
|
Packit |
5c3484 |
MPN_INCR_U (ip, n, e);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|