(root)/
gmp-6.3.0/
mpn/
generic/
hgcd_step.c
       1  /* hgcd_step.c.
       2  
       3     THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES.  IT IS ONLY
       4     SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
       5     GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
       6  
       7  Copyright 2003-2005, 2008, 2011, 2012 Free Software Foundation, Inc.
       8  
       9  This file is part of the GNU MP Library.
      10  
      11  The GNU MP Library is free software; you can redistribute it and/or modify
      12  it under the terms of either:
      13  
      14    * the GNU Lesser General Public License as published by the Free
      15      Software Foundation; either version 3 of the License, or (at your
      16      option) any later version.
      17  
      18  or
      19  
      20    * the GNU General Public License as published by the Free Software
      21      Foundation; either version 2 of the License, or (at your option) any
      22      later version.
      23  
      24  or both in parallel, as here.
      25  
      26  The GNU MP Library is distributed in the hope that it will be useful, but
      27  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
      28  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      29  for more details.
      30  
      31  You should have received copies of the GNU General Public License and the
      32  GNU Lesser General Public License along with the GNU MP Library.  If not,
      33  see https://www.gnu.org/licenses/.  */
      34  
      35  #include "gmp-impl.h"
      36  #include "longlong.h"
      37  
      38  
      39  static void
      40  hgcd_hook (void *p, mp_srcptr gp, mp_size_t gn,
      41  	   mp_srcptr qp, mp_size_t qn, int d)
      42  {
      43    ASSERT (!gp);
      44    ASSERT (d >= 0);
      45    ASSERT (d <= 1);
      46  
      47    MPN_NORMALIZE (qp, qn);
      48    if (qn > 0)
      49      {
      50        struct hgcd_matrix *M = (struct hgcd_matrix *) p;
      51        /* NOTES: This is a bit ugly. A tp area is passed to
      52  	 gcd_subdiv_step, which stores q at the start of that area. We
      53  	 now use the rest. */
      54        mp_ptr tp = (mp_ptr) qp + qn;
      55        mpn_hgcd_matrix_update_q (M, qp, qn, d, tp);
      56      }
      57  }
      58  
      59  /* Perform a few steps, using some of mpn_hgcd2, subtraction and
      60     division. Reduces the size by almost one limb or more, but never
      61     below the given size s. Return new size for a and b, or 0 if no
      62     more steps are possible.
      63  
      64     If hgcd2 succeeds, needs temporary space for hgcd_matrix_mul_1, M->n
      65     limbs, and hgcd_mul_matrix1_inverse_vector, n limbs. If hgcd2
      66     fails, needs space for the quotient, qn <= n - s limbs, for and
      67     hgcd_matrix_update_q, qn + (size of the appropriate column of M) <=
      68     (resulting size of M) + 1.
      69  
      70     If N is the input size to the calling hgcd, then s = floor(N/2) +
      71     1, M->n < N, qn + product size <= n - s + n - s + 1 = 2 (n - s) + 1
      72     <= N.
      73  */
      74  
      75  mp_size_t
      76  mpn_hgcd_step (mp_size_t n, mp_ptr ap, mp_ptr bp, mp_size_t s,
      77  	       struct hgcd_matrix *M, mp_ptr tp)
      78  {
      79    struct hgcd_matrix1 M1;
      80    mp_limb_t mask;
      81    mp_limb_t ah, al, bh, bl;
      82  
      83    ASSERT (n > s);
      84  
      85    mask = ap[n-1] | bp[n-1];
      86    ASSERT (mask > 0);
      87  
      88    if (n == s + 1)
      89      {
      90        if (mask < 4)
      91  	goto subtract;
      92  
      93        ah = ap[n-1]; al = ap[n-2];
      94        bh = bp[n-1]; bl = bp[n-2];
      95      }
      96    else if (mask & GMP_NUMB_HIGHBIT)
      97      {
      98        ah = ap[n-1]; al = ap[n-2];
      99        bh = bp[n-1]; bl = bp[n-2];
     100      }
     101    else
     102      {
     103        int shift;
     104  
     105        count_leading_zeros (shift, mask);
     106        ah = MPN_EXTRACT_NUMB (shift, ap[n-1], ap[n-2]);
     107        al = MPN_EXTRACT_NUMB (shift, ap[n-2], ap[n-3]);
     108        bh = MPN_EXTRACT_NUMB (shift, bp[n-1], bp[n-2]);
     109        bl = MPN_EXTRACT_NUMB (shift, bp[n-2], bp[n-3]);
     110      }
     111  
     112    /* Try an mpn_hgcd2 step */
     113    if (mpn_hgcd2 (ah, al, bh, bl, &M1))
     114      {
     115        /* Multiply M <- M * M1 */
     116        mpn_hgcd_matrix_mul_1 (M, &M1, tp);
     117  
     118        /* Can't swap inputs, so we need to copy. */
     119        MPN_COPY (tp, ap, n);
     120        /* Multiply M1^{-1} (a;b) */
     121        return mpn_matrix22_mul1_inverse_vector (&M1, ap, tp, bp, n);
     122      }
     123  
     124   subtract:
     125  
     126    return mpn_gcd_subdiv_step (ap, bp, n, s, hgcd_hook, M, tp);
     127  }