Blame mpn/generic/div_qr_1.c

Packit 5c3484
/* mpn_div_qr_1 -- mpn by limb division.
Packit 5c3484
Packit 5c3484
   Contributed to the GNU project by Niels Möller and Torbjörn Granlund
Packit 5c3484
Packit 5c3484
Copyright 1991, 1993, 1994, 1996, 1998-2000, 2002, 2003, 2013 Free Software
Packit 5c3484
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
#ifndef DIV_QR_1_NORM_THRESHOLD
Packit 5c3484
#define DIV_QR_1_NORM_THRESHOLD 3
Packit 5c3484
#endif
Packit 5c3484
#ifndef DIV_QR_1_UNNORM_THRESHOLD
Packit 5c3484
#define DIV_QR_1_UNNORM_THRESHOLD 3
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
#if GMP_NAIL_BITS > 0
Packit 5c3484
#error Nail bits not supported
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
/* Divides {up, n} by d. Writes the n-1 low quotient limbs at {qp,
Packit 5c3484
 * n-1}, and the high quote limb at *qh. Returns remainder. */
Packit 5c3484
mp_limb_t
Packit 5c3484
mpn_div_qr_1 (mp_ptr qp, mp_limb_t *qh, mp_srcptr up, mp_size_t n,
Packit 5c3484
	      mp_limb_t d)
Packit 5c3484
{
Packit 5c3484
  unsigned cnt;
Packit 5c3484
  mp_limb_t uh;
Packit 5c3484
Packit 5c3484
  ASSERT (n > 0);
Packit 5c3484
  ASSERT (d > 0);
Packit 5c3484
Packit 5c3484
  if (d & GMP_NUMB_HIGHBIT)
Packit 5c3484
    {
Packit 5c3484
      /* Normalized case */
Packit 5c3484
      mp_limb_t dinv, q;
Packit 5c3484
Packit 5c3484
      uh = up[--n];
Packit 5c3484
Packit 5c3484
      q = (uh >= d);
Packit 5c3484
      *qh = q;
Packit 5c3484
      uh -= (-q) & d;
Packit 5c3484
Packit 5c3484
      if (BELOW_THRESHOLD (n, DIV_QR_1_NORM_THRESHOLD))
Packit 5c3484
	{
Packit 5c3484
	  cnt = 0;
Packit 5c3484
	plain:
Packit 5c3484
	  while (n > 0)
Packit 5c3484
	    {
Packit 5c3484
	      mp_limb_t ul = up[--n];
Packit 5c3484
	      udiv_qrnnd (qp[n], uh, uh, ul, d);
Packit 5c3484
	    }
Packit 5c3484
	  return uh >> cnt;
Packit 5c3484
	}
Packit 5c3484
      invert_limb (dinv, d);
Packit 5c3484
      return mpn_div_qr_1n_pi1 (qp, up, n, uh, d, dinv);
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      /* Unnormalized case */
Packit 5c3484
      mp_limb_t dinv, ul;
Packit 5c3484
Packit 5c3484
      if (! UDIV_NEEDS_NORMALIZATION
Packit 5c3484
	  && BELOW_THRESHOLD (n, DIV_QR_1_UNNORM_THRESHOLD))
Packit 5c3484
	{
Packit 5c3484
	  uh = up[--n];
Packit 5c3484
	  udiv_qrnnd (*qh, uh, CNST_LIMB(0), uh, d);
Packit 5c3484
	  cnt = 0;
Packit 5c3484
	  goto plain;
Packit 5c3484
	}
Packit 5c3484
Packit 5c3484
      count_leading_zeros (cnt, d);
Packit 5c3484
      d <<= cnt;
Packit 5c3484
Packit 5c3484
#if HAVE_NATIVE_div_qr_1u_pi1
Packit 5c3484
      /* FIXME: Call loop doing on-the-fly normalization */
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
      /* Shift up front, use qp area for shifted copy. A bit messy,
Packit 5c3484
	 since we have only n-1 limbs available, and shift the high
Packit 5c3484
	 limb manually. */
Packit 5c3484
      uh = up[--n];
Packit 5c3484
      ul = (uh << cnt) | mpn_lshift (qp, up, n, cnt);
Packit 5c3484
      uh >>= (GMP_LIMB_BITS - cnt);
Packit 5c3484
Packit 5c3484
      if (UDIV_NEEDS_NORMALIZATION
Packit 5c3484
	  && BELOW_THRESHOLD (n, DIV_QR_1_UNNORM_THRESHOLD))
Packit 5c3484
	{
Packit 5c3484
	  udiv_qrnnd (*qh, uh, uh, ul, d);
Packit 5c3484
	  up = qp;
Packit 5c3484
	  goto plain;
Packit 5c3484
	}
Packit 5c3484
      invert_limb (dinv, d);
Packit 5c3484
Packit 5c3484
      udiv_qrnnd_preinv (*qh, uh, uh, ul, d, dinv);
Packit 5c3484
      return mpn_div_qr_1n_pi1 (qp, qp, n, uh, d, dinv) >> cnt;
Packit 5c3484
    }
Packit 5c3484
}