Blame mpn/generic/dcpi1_div_q.c

Packit 5c3484
/* mpn_dc_div_q -- divide-and-conquer division, returning exact quotient
Packit 5c3484
   only.
Packit 5c3484
Packit 5c3484
   Contributed to the GNU project by Torbjorn Granlund and Marco Bodrato.
Packit 5c3484
Packit 5c3484
   THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.  IT IS ONLY
Packit 5c3484
   SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
Packit 5c3484
   GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
Packit 5c3484
Packit 5c3484
Copyright 2006, 2007, 2009, 2010 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
mp_limb_t
Packit 5c3484
mpn_dcpi1_div_q (mp_ptr qp, mp_ptr np, mp_size_t nn,
Packit 5c3484
		 mp_srcptr dp, mp_size_t dn, gmp_pi1_t *dinv)
Packit 5c3484
{
Packit 5c3484
  mp_ptr tp, wp;
Packit 5c3484
  mp_limb_t qh;
Packit 5c3484
  mp_size_t qn;
Packit 5c3484
  TMP_DECL;
Packit 5c3484
Packit 5c3484
  TMP_MARK;
Packit 5c3484
Packit 5c3484
  ASSERT (dn >= 6);
Packit 5c3484
  ASSERT (nn - dn >= 3);
Packit 5c3484
  ASSERT (dp[dn-1] & GMP_NUMB_HIGHBIT);
Packit 5c3484
Packit 5c3484
  tp = TMP_ALLOC_LIMBS (nn + 1);
Packit 5c3484
  MPN_COPY (tp + 1, np, nn);
Packit 5c3484
  tp[0] = 0;
Packit 5c3484
Packit 5c3484
  qn = nn - dn;
Packit 5c3484
  wp = TMP_ALLOC_LIMBS (qn + 1);
Packit 5c3484
Packit 5c3484
  qh = mpn_dcpi1_divappr_q (wp, tp, nn + 1, dp, dn, dinv);
Packit 5c3484
Packit 5c3484
  if (wp[0] == 0)
Packit 5c3484
    {
Packit 5c3484
      mp_limb_t cy;
Packit 5c3484
Packit 5c3484
      if (qn > dn)
Packit 5c3484
	mpn_mul (tp, wp + 1, qn, dp, dn);
Packit 5c3484
      else
Packit 5c3484
	mpn_mul (tp, dp, dn, wp + 1, qn);
Packit 5c3484
Packit 5c3484
      cy = (qh != 0) ? mpn_add_n (tp + qn, tp + qn, dp, dn) : 0;
Packit 5c3484
Packit 5c3484
      if (cy || mpn_cmp (tp, np, nn) > 0) /* At most is wrong by one, no cycle. */
Packit 5c3484
	qh -= mpn_sub_1 (qp, wp + 1, qn, 1);
Packit 5c3484
      else /* Same as below */
Packit 5c3484
	MPN_COPY (qp, wp + 1, qn);
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    MPN_COPY (qp, wp + 1, qn);
Packit 5c3484
Packit 5c3484
  TMP_FREE;
Packit 5c3484
  return qh;
Packit 5c3484
}