Blame mpn/generic/binvert.c

Packit 5c3484
/* Compute {up,n}^(-1) mod B^n.
Packit 5c3484
Packit 5c3484
   Contributed to the GNU project by Torbjorn Granlund.
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) 2004-2007, 2009, 2012 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
Packit 5c3484
Packit 5c3484
/*
Packit 5c3484
  r[k+1] = r[k] - r[k] * (u*r[k] - 1)
Packit 5c3484
  r[k+1] = r[k] + r[k] - r[k]*(u*r[k])
Packit 5c3484
*/
Packit 5c3484
Packit 5c3484
#if TUNE_PROGRAM_BUILD
Packit 5c3484
#define NPOWS \
Packit 5c3484
 ((sizeof(mp_size_t) > 6 ? 48 : 8*sizeof(mp_size_t)))
Packit 5c3484
#else
Packit 5c3484
#define NPOWS \
Packit 5c3484
 ((sizeof(mp_size_t) > 6 ? 48 : 8*sizeof(mp_size_t)) - LOG2C (BINV_NEWTON_THRESHOLD))
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
mp_size_t
Packit 5c3484
mpn_binvert_itch (mp_size_t n)
Packit 5c3484
{
Packit 5c3484
  mp_size_t itch_local = mpn_mulmod_bnm1_next_size (n);
Packit 5c3484
  mp_size_t itch_out = mpn_mulmod_bnm1_itch (itch_local, n, (n + 1) >> 1);
Packit 5c3484
  return itch_local + itch_out;
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpn_binvert (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_ptr scratch)
Packit 5c3484
{
Packit 5c3484
  mp_ptr xp;
Packit 5c3484
  mp_size_t rn, newrn;
Packit 5c3484
  mp_size_t sizes[NPOWS], *sizp;
Packit 5c3484
  mp_limb_t di;
Packit 5c3484
Packit 5c3484
  /* Compute the computation precisions from highest to lowest, leaving the
Packit 5c3484
     base case size in 'rn'.  */
Packit 5c3484
  sizp = sizes;
Packit 5c3484
  for (rn = n; ABOVE_THRESHOLD (rn, BINV_NEWTON_THRESHOLD); rn = (rn + 1) >> 1)
Packit 5c3484
    *sizp++ = rn;
Packit 5c3484
Packit 5c3484
  xp = scratch;
Packit 5c3484
Packit 5c3484
  /* Compute a base value of rn limbs.  */
Packit 5c3484
  MPN_ZERO (xp, rn);
Packit 5c3484
  xp[0] = 1;
Packit 5c3484
  binvert_limb (di, up[0]);
Packit 5c3484
  if (BELOW_THRESHOLD (rn, DC_BDIV_Q_THRESHOLD))
Packit 5c3484
    mpn_sbpi1_bdiv_q (rp, xp, rn, up, rn, -di);
Packit 5c3484
  else
Packit 5c3484
    mpn_dcpi1_bdiv_q (rp, xp, rn, up, rn, -di);
Packit 5c3484
Packit 5c3484
  /* Use Newton iterations to get the desired precision.  */
Packit 5c3484
  for (; rn < n; rn = newrn)
Packit 5c3484
    {
Packit 5c3484
      mp_size_t m;
Packit 5c3484
      newrn = *--sizp;
Packit 5c3484
Packit 5c3484
      /* X <- UR. */
Packit 5c3484
      m = mpn_mulmod_bnm1_next_size (newrn);
Packit 5c3484
      mpn_mulmod_bnm1 (xp, m, up, newrn, rp, rn, xp + m);
Packit 5c3484
      mpn_sub_1 (xp + m, xp, rn - (m - newrn), 1);
Packit 5c3484
Packit 5c3484
      /* R = R(X/B^rn) */
Packit 5c3484
      mpn_mullo_n (rp + rn, rp, xp + rn, newrn - rn);
Packit 5c3484
      mpn_neg (rp + rn, rp + rn, newrn - rn);
Packit 5c3484
    }
Packit 5c3484
}