Blame mpn/generic/hgcd.c

Packit 5c3484
/* hgcd.c.
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'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
Packit 5c3484
Packit 5c3484
Copyright 2003-2005, 2008, 2011, 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
#include "longlong.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* Size analysis for hgcd:
Packit 5c3484
Packit 5c3484
   For the recursive calls, we have n1 <= ceil(n / 2). Then the
Packit 5c3484
   storage need is determined by the storage for the recursive call
Packit 5c3484
   computing M1, and hgcd_matrix_adjust and hgcd_matrix_mul calls that use M1
Packit 5c3484
   (after this, the storage needed for M1 can be recycled).
Packit 5c3484
Packit 5c3484
   Let S(r) denote the required storage. For M1 we need 4 * (ceil(n1/2) + 1)
Packit 5c3484
   = 4 * (ceil(n/4) + 1), for the hgcd_matrix_adjust call, we need n + 2,
Packit 5c3484
   and for the hgcd_matrix_mul, we may need 3 ceil(n/2) + 8. In total,
Packit 5c3484
   4 * ceil(n/4) + 3 ceil(n/2) + 12 <= 10 ceil(n/4) + 12.
Packit 5c3484
Packit 5c3484
   For the recursive call, we need S(n1) = S(ceil(n/2)).
Packit 5c3484
Packit 5c3484
   S(n) <= 10*ceil(n/4) + 12 + S(ceil(n/2))
Packit 5c3484
	<= 10*(ceil(n/4) + ... + ceil(n/2^(1+k))) + 12k + S(ceil(n/2^k))
Packit 5c3484
	<= 10*(2 ceil(n/4) + k) + 12k + S(ceil(n/2^k))
Packit 5c3484
	<= 20 ceil(n/4) + 22k + S(ceil(n/2^k))
Packit 5c3484
*/
Packit 5c3484
Packit 5c3484
mp_size_t
Packit 5c3484
mpn_hgcd_itch (mp_size_t n)
Packit 5c3484
{
Packit 5c3484
  unsigned k;
Packit 5c3484
  int count;
Packit 5c3484
  mp_size_t nscaled;
Packit 5c3484
Packit 5c3484
  if (BELOW_THRESHOLD (n, HGCD_THRESHOLD))
Packit 5c3484
    return n;
Packit 5c3484
Packit 5c3484
  /* Get the recursion depth. */
Packit 5c3484
  nscaled = (n - 1) / (HGCD_THRESHOLD - 1);
Packit 5c3484
  count_leading_zeros (count, nscaled);
Packit 5c3484
  k = GMP_LIMB_BITS - count;
Packit 5c3484
Packit 5c3484
  return 20 * ((n+3) / 4) + 22 * k + HGCD_THRESHOLD;
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
/* Reduces a,b until |a-b| fits in n/2 + 1 limbs. Constructs matrix M
Packit 5c3484
   with elements of size at most (n+1)/2 - 1. Returns new size of a,
Packit 5c3484
   b, or zero if no reduction is possible. */
Packit 5c3484
Packit 5c3484
mp_size_t
Packit 5c3484
mpn_hgcd (mp_ptr ap, mp_ptr bp, mp_size_t n,
Packit 5c3484
	  struct hgcd_matrix *M, mp_ptr tp)
Packit 5c3484
{
Packit 5c3484
  mp_size_t s = n/2 + 1;
Packit 5c3484
Packit 5c3484
  mp_size_t nn;
Packit 5c3484
  int success = 0;
Packit 5c3484
Packit 5c3484
  if (n <= s)
Packit 5c3484
    /* Happens when n <= 2, a fairly uninteresting case but exercised
Packit 5c3484
       by the random inputs of the testsuite. */
Packit 5c3484
    return 0;
Packit 5c3484
Packit 5c3484
  ASSERT ((ap[n-1] | bp[n-1]) > 0);
Packit 5c3484
Packit 5c3484
  ASSERT ((n+1)/2 - 1 < M->alloc);
Packit 5c3484
Packit 5c3484
  if (ABOVE_THRESHOLD (n, HGCD_THRESHOLD))
Packit 5c3484
    {
Packit 5c3484
      mp_size_t n2 = (3*n)/4 + 1;
Packit 5c3484
      mp_size_t p = n/2;
Packit 5c3484
Packit 5c3484
      nn = mpn_hgcd_reduce (M, ap, bp, n, p, tp);
Packit 5c3484
      if (nn)
Packit 5c3484
	{
Packit 5c3484
	  n = nn;
Packit 5c3484
	  success = 1;
Packit 5c3484
	}
Packit 5c3484
Packit 5c3484
      /* NOTE: It appears this loop never runs more than once (at
Packit 5c3484
	 least when not recursing to hgcd_appr). */
Packit 5c3484
      while (n > n2)
Packit 5c3484
	{
Packit 5c3484
	  /* Needs n + 1 storage */
Packit 5c3484
	  nn = mpn_hgcd_step (n, ap, bp, s, M, tp);
Packit 5c3484
	  if (!nn)
Packit 5c3484
	    return success ? n : 0;
Packit 5c3484
Packit 5c3484
	  n = nn;
Packit 5c3484
	  success = 1;
Packit 5c3484
	}
Packit 5c3484
Packit 5c3484
      if (n > s + 2)
Packit 5c3484
	{
Packit 5c3484
	  struct hgcd_matrix M1;
Packit 5c3484
	  mp_size_t scratch;
Packit 5c3484
Packit 5c3484
	  p = 2*s - n + 1;
Packit 5c3484
	  scratch = MPN_HGCD_MATRIX_INIT_ITCH (n-p);
Packit 5c3484
Packit 5c3484
	  mpn_hgcd_matrix_init(&M1, n - p, tp);
Packit 5c3484
Packit 5c3484
	  /* FIXME: Should use hgcd_reduce, but that may require more
Packit 5c3484
	     scratch space, which requires review. */
Packit 5c3484
Packit 5c3484
	  nn = mpn_hgcd (ap + p, bp + p, n - p, &M1, tp + scratch);
Packit 5c3484
	  if (nn > 0)
Packit 5c3484
	    {
Packit 5c3484
	      /* We always have max(M) > 2^{-(GMP_NUMB_BITS + 1)} max(M1) */
Packit 5c3484
	      ASSERT (M->n + 2 >= M1.n);
Packit 5c3484
Packit 5c3484
	      /* Furthermore, assume M ends with a quotient (1, q; 0, 1),
Packit 5c3484
		 then either q or q + 1 is a correct quotient, and M1 will
Packit 5c3484
		 start with either (1, 0; 1, 1) or (2, 1; 1, 1). This
Packit 5c3484
		 rules out the case that the size of M * M1 is much
Packit 5c3484
		 smaller than the expected M->n + M1->n. */
Packit 5c3484
Packit 5c3484
	      ASSERT (M->n + M1.n < M->alloc);
Packit 5c3484
Packit 5c3484
	      /* Needs 2 (p + M->n) <= 2 (2*s - n2 + 1 + n2 - s - 1)
Packit 5c3484
		 = 2*s <= 2*(floor(n/2) + 1) <= n + 2. */
Packit 5c3484
	      n = mpn_hgcd_matrix_adjust (&M1, p + nn, ap, bp, p, tp + scratch);
Packit 5c3484
Packit 5c3484
	      /* We need a bound for of M->n + M1.n. Let n be the original
Packit 5c3484
		 input size. Then
Packit 5c3484
Packit 5c3484
		 ceil(n/2) - 1 >= size of product >= M.n + M1.n - 2
Packit 5c3484
Packit 5c3484
		 and it follows that
Packit 5c3484
Packit 5c3484
		 M.n + M1.n <= ceil(n/2) + 1
Packit 5c3484
Packit 5c3484
		 Then 3*(M.n + M1.n) + 5 <= 3 * ceil(n/2) + 8 is the
Packit 5c3484
		 amount of needed scratch space. */
Packit 5c3484
	      mpn_hgcd_matrix_mul (M, &M1, tp + scratch);
Packit 5c3484
	      success = 1;
Packit 5c3484
	    }
Packit 5c3484
	}
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  for (;;)
Packit 5c3484
    {
Packit 5c3484
      /* Needs s+3 < n */
Packit 5c3484
      nn = mpn_hgcd_step (n, ap, bp, s, M, tp);
Packit 5c3484
      if (!nn)
Packit 5c3484
	return success ? n : 0;
Packit 5c3484
Packit 5c3484
      n = nn;
Packit 5c3484
      success = 1;
Packit 5c3484
    }
Packit 5c3484
}