Blame mpn/generic/dive_1.c

Packit 5c3484
/* mpn_divexact_1 -- mpn by limb exact division.
Packit 5c3484
Packit 5c3484
   THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
Packit 5c3484
   CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
Packit 5c3484
   FUTURE GNU MP RELEASES.
Packit 5c3484
Packit 5c3484
Copyright 2000-2003, 2005, 2013 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
Packit 5c3484
Packit 5c3484
/* Divide a={src,size} by d=divisor and store the quotient in q={dst,size}.
Packit 5c3484
   q will only be correct if d divides a exactly.
Packit 5c3484
Packit 5c3484
   A separate loop is used for shift==0 because n<
Packit 5c3484
   give zero on all CPUs (for instance it doesn't on the x86s).  This
Packit 5c3484
   separate loop might run faster too, helping odd divisors.
Packit 5c3484
Packit 5c3484
   Possibilities:
Packit 5c3484
Packit 5c3484
   mpn_divexact_1c could be created, accepting and returning c.  This would
Packit 5c3484
   let a long calculation be done piece by piece.  Currently there's no
Packit 5c3484
   particular need for that, and not returning c means that a final umul can
Packit 5c3484
   be skipped.
Packit 5c3484
Packit 5c3484
   Another use for returning c would be letting the caller know whether the
Packit 5c3484
   division was in fact exact.  It would work just to return the carry bit
Packit 5c3484
   "c=(l>s)" and let the caller do a final umul if interested.
Packit 5c3484
Packit 5c3484
   When the divisor is even, the factors of two could be handled with a
Packit 5c3484
   separate mpn_rshift, instead of shifting on the fly.  That might be
Packit 5c3484
   faster on some CPUs and would mean just the shift==0 style loop would be
Packit 5c3484
   needed.
Packit 5c3484
Packit 5c3484
   If n<
Packit 5c3484
   shift==0 loop is unnecessary, and could be eliminated if there's no great
Packit 5c3484
   speed difference.
Packit 5c3484
Packit 5c3484
   It's not clear whether "/" is the best way to handle size==1.  Alpha gcc
Packit 5c3484
   2.95 for instance has a poor "/" and might prefer the modular method.
Packit 5c3484
   Perhaps a tuned parameter should control this.
Packit 5c3484
Packit 5c3484
   If src[size-1] < divisor then dst[size-1] will be zero, and one divide
Packit 5c3484
   step could be skipped.  A test at last step for s
Packit 5c3484
   even case) might be a good way to do that.  But if this code is often
Packit 5c3484
   used with small divisors then it might not be worth bothering  */
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpn_divexact_1 (mp_ptr dst, mp_srcptr src, mp_size_t size, mp_limb_t divisor)
Packit 5c3484
{
Packit 5c3484
  mp_size_t  i;
Packit 5c3484
  mp_limb_t  c, h, l, ls, s, s_next, inverse, dummy;
Packit 5c3484
  unsigned   shift;
Packit 5c3484
Packit 5c3484
  ASSERT (size >= 1);
Packit 5c3484
  ASSERT (divisor != 0);
Packit 5c3484
  ASSERT (MPN_SAME_OR_SEPARATE_P (dst, src, size));
Packit 5c3484
  ASSERT_MPN (src, size);
Packit 5c3484
  ASSERT_LIMB (divisor);
Packit 5c3484
Packit 5c3484
  if ((divisor & 1) == 0)
Packit 5c3484
    {
Packit 5c3484
      count_trailing_zeros (shift, divisor);
Packit 5c3484
      divisor >>= shift;
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    shift = 0;
Packit 5c3484
Packit 5c3484
  binvert_limb (inverse, divisor);
Packit 5c3484
  divisor <<= GMP_NAIL_BITS;
Packit 5c3484
Packit 5c3484
  if (shift != 0)
Packit 5c3484
    {
Packit 5c3484
      c = 0;
Packit 5c3484
Packit 5c3484
      s = src[0];
Packit 5c3484
Packit 5c3484
      for (i = 1; i < size; i++)
Packit 5c3484
	{
Packit 5c3484
	  s_next = src[i];
Packit 5c3484
	  ls = ((s >> shift) | (s_next << (GMP_NUMB_BITS-shift))) & GMP_NUMB_MASK;
Packit 5c3484
	  s = s_next;
Packit 5c3484
Packit 5c3484
	  SUBC_LIMB (c, l, ls, c);
Packit 5c3484
Packit 5c3484
	  l = (l * inverse) & GMP_NUMB_MASK;
Packit 5c3484
	  dst[i - 1] = l;
Packit 5c3484
Packit 5c3484
	  umul_ppmm (h, dummy, l, divisor);
Packit 5c3484
	  c += h;
Packit 5c3484
	}
Packit 5c3484
Packit 5c3484
      ls = s >> shift;
Packit 5c3484
      l = ls - c;
Packit 5c3484
      l = (l * inverse) & GMP_NUMB_MASK;
Packit 5c3484
      dst[size - 1] = l;
Packit 5c3484
    }
Packit 5c3484
  else
Packit 5c3484
    {
Packit 5c3484
      s = src[0];
Packit 5c3484
Packit 5c3484
      l = (s * inverse) & GMP_NUMB_MASK;
Packit 5c3484
      dst[0] = l;
Packit 5c3484
      c = 0;
Packit 5c3484
Packit 5c3484
      for (i = 1; i < size; i++)
Packit 5c3484
	{
Packit 5c3484
	  umul_ppmm (h, dummy, l, divisor);
Packit 5c3484
	  c += h;
Packit 5c3484
Packit 5c3484
	  s = src[i];
Packit 5c3484
	  SUBC_LIMB (c, l, s, c);
Packit 5c3484
Packit 5c3484
	  l = (l * inverse) & GMP_NUMB_MASK;
Packit 5c3484
	  dst[i] = l;
Packit 5c3484
	}
Packit 5c3484
    }
Packit 5c3484
}