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

     1  /* mpn_mulmid_basecase -- classical middle product algorithm
     2  
     3     Contributed by David Harvey.
     4  
     5     THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.  IT IS ONLY
     6     SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
     7     GUARANTEED THAT IT'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
     8  
     9  Copyright 2011 Free Software Foundation, Inc.
    10  
    11  This file is part of the GNU MP Library.
    12  
    13  The GNU MP Library is free software; you can redistribute it and/or modify
    14  it under the terms of either:
    15  
    16    * the GNU Lesser General Public License as published by the Free
    17      Software Foundation; either version 3 of the License, or (at your
    18      option) any later version.
    19  
    20  or
    21  
    22    * the GNU General Public License as published by the Free Software
    23      Foundation; either version 2 of the License, or (at your option) any
    24      later version.
    25  
    26  or both in parallel, as here.
    27  
    28  The GNU MP Library is distributed in the hope that it will be useful, but
    29  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    30  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    31  for more details.
    32  
    33  You should have received copies of the GNU General Public License and the
    34  GNU Lesser General Public License along with the GNU MP Library.  If not,
    35  see https://www.gnu.org/licenses/.  */
    36  
    37  
    38  #include "gmp.h"
    39  #include "gmp-impl.h"
    40  #include "longlong.h"
    41  
    42  /* Middle product of {up,un} and {vp,vn}, write result to {rp,un-vn+3}.
    43     Must have un >= vn >= 1.
    44  
    45     Neither input buffer may overlap with the output buffer. */
    46  
    47  void
    48  mpn_mulmid_basecase (mp_ptr rp,
    49                       mp_srcptr up, mp_size_t un,
    50                       mp_srcptr vp, mp_size_t vn)
    51  {
    52    mp_limb_t lo, hi;  /* last two limbs of output */
    53    mp_limb_t cy;
    54  
    55    ASSERT (un >= vn);
    56    ASSERT (vn >= 1);
    57    ASSERT (! MPN_OVERLAP_P (rp, un - vn + 3, up, un));
    58    ASSERT (! MPN_OVERLAP_P (rp, un - vn + 3, vp, vn));
    59  
    60    up += vn - 1;
    61    un -= vn - 1;
    62  
    63    /* multiply by first limb, store result */
    64    lo = mpn_mul_1 (rp, up, un, vp[0]);
    65    hi = 0;
    66  
    67    /* accumulate remaining rows */
    68    for (vn--; vn; vn--)
    69      {
    70        up--, vp++;
    71        cy = mpn_addmul_1 (rp, up, un, vp[0]);
    72        add_ssaaaa (hi, lo, hi, lo, CNST_LIMB(0), cy);
    73      }
    74  
    75    /* store final limbs */
    76  #if GMP_NAIL_BITS != 0
    77    hi = (hi << GMP_NAIL_BITS) + (lo >> GMP_NUMB_BITS);
    78    lo &= GMP_NUMB_MASK;
    79  #endif
    80  
    81    rp[un] = lo;
    82    rp[un + 1] = hi;
    83  }