github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpn/generic/hgcd_step.c (about)

     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.h"
    36  #include "gmp-impl.h"
    37  #include "longlong.h"
    38  
    39  
    40  static void
    41  hgcd_hook (void *p, mp_srcptr gp, mp_size_t gn,
    42  	   mp_srcptr qp, mp_size_t qn, int d)
    43  {
    44    ASSERT (!gp);
    45    ASSERT (d >= 0);
    46    ASSERT (d <= 1);
    47  
    48    MPN_NORMALIZE (qp, qn);
    49    if (qn > 0)
    50      {
    51        struct hgcd_matrix *M = (struct hgcd_matrix *) p;
    52        /* NOTES: This is a bit ugly. A tp area is passed to
    53  	 gcd_subdiv_step, which stores q at the start of that area. We
    54  	 now use the rest. */
    55        mp_ptr tp = (mp_ptr) qp + qn;
    56        mpn_hgcd_matrix_update_q (M, qp, qn, d, tp);
    57      }
    58  }
    59  
    60  /* Perform a few steps, using some of mpn_hgcd2, subtraction and
    61     division. Reduces the size by almost one limb or more, but never
    62     below the given size s. Return new size for a and b, or 0 if no
    63     more steps are possible.
    64  
    65     If hgcd2 succeeds, needs temporary space for hgcd_matrix_mul_1, M->n
    66     limbs, and hgcd_mul_matrix1_inverse_vector, n limbs. If hgcd2
    67     fails, needs space for the quotient, qn <= n - s limbs, for and
    68     hgcd_matrix_update_q, qn + (size of the appropriate column of M) <=
    69     (resulting size of M) + 1.
    70  
    71     If N is the input size to the calling hgcd, then s = floor(N/2) +
    72     1, M->n < N, qn + product size <= n - s + n - s + 1 = 2 (n - s) + 1
    73     <= N.
    74  */
    75  
    76  mp_size_t
    77  mpn_hgcd_step (mp_size_t n, mp_ptr ap, mp_ptr bp, mp_size_t s,
    78  	       struct hgcd_matrix *M, mp_ptr tp)
    79  {
    80    struct hgcd_matrix1 M1;
    81    mp_limb_t mask;
    82    mp_limb_t ah, al, bh, bl;
    83  
    84    ASSERT (n > s);
    85  
    86    mask = ap[n-1] | bp[n-1];
    87    ASSERT (mask > 0);
    88  
    89    if (n == s + 1)
    90      {
    91        if (mask < 4)
    92  	goto subtract;
    93  
    94        ah = ap[n-1]; al = ap[n-2];
    95        bh = bp[n-1]; bl = bp[n-2];
    96      }
    97    else if (mask & GMP_NUMB_HIGHBIT)
    98      {
    99        ah = ap[n-1]; al = ap[n-2];
   100        bh = bp[n-1]; bl = bp[n-2];
   101      }
   102    else
   103      {
   104        int shift;
   105  
   106        count_leading_zeros (shift, mask);
   107        ah = MPN_EXTRACT_NUMB (shift, ap[n-1], ap[n-2]);
   108        al = MPN_EXTRACT_NUMB (shift, ap[n-2], ap[n-3]);
   109        bh = MPN_EXTRACT_NUMB (shift, bp[n-1], bp[n-2]);
   110        bl = MPN_EXTRACT_NUMB (shift, bp[n-2], bp[n-3]);
   111      }
   112  
   113    /* Try an mpn_hgcd2 step */
   114    if (mpn_hgcd2 (ah, al, bh, bl, &M1))
   115      {
   116        /* Multiply M <- M * M1 */
   117        mpn_hgcd_matrix_mul_1 (M, &M1, tp);
   118  
   119        /* Can't swap inputs, so we need to copy. */
   120        MPN_COPY (tp, ap, n);
   121        /* Multiply M1^{-1} (a;b) */
   122        return mpn_matrix22_mul1_inverse_vector (&M1, ap, tp, bp, n);
   123      }
   124  
   125   subtract:
   126  
   127    return mpn_gcd_subdiv_step (ap, bp, n, s, hgcd_hook, M, tp);
   128  }