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

     1  /* mpn_sub_err1_n -- sub_n with one error term
     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  #include "gmp.h"
    38  #include "gmp-impl.h"
    39  
    40  /*
    41    Computes:
    42  
    43    (1) {rp,n} := {up,n} - {vp,n} (just like mpn_sub_n) with incoming borrow cy,
    44    return value is borrow out.
    45  
    46    (2) Let c[i+1] = borrow from i-th limb subtraction (c[0] = cy).
    47    Computes c[1]*yp[n-1] + ... + c[n]*yp[0], stores two-limb result at ep.
    48  
    49    Requires n >= 1.
    50  
    51    None of the outputs may overlap each other or any of the inputs, except
    52    that {rp,n} may be equal to {up,n} or {vp,n}.
    53  */
    54  mp_limb_t
    55  mpn_sub_err1_n (mp_ptr rp, mp_srcptr up, mp_srcptr vp,
    56  		mp_ptr ep, mp_srcptr yp,
    57                  mp_size_t n, mp_limb_t cy)
    58  {
    59    mp_limb_t el, eh, ul, vl, yl, zl, rl, sl, cy1, cy2;
    60  
    61    ASSERT (n >= 1);
    62    ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
    63    ASSERT (MPN_SAME_OR_SEPARATE_P (rp, vp, n));
    64    ASSERT (! MPN_OVERLAP_P (rp, n, yp, n));
    65    ASSERT (! MPN_OVERLAP_P (ep, 2, up, n));
    66    ASSERT (! MPN_OVERLAP_P (ep, 2, vp, n));
    67    ASSERT (! MPN_OVERLAP_P (ep, 2, yp, n));
    68    ASSERT (! MPN_OVERLAP_P (ep, 2, rp, n));
    69  
    70    yp += n - 1;
    71    el = eh = 0;
    72  
    73    do
    74      {
    75        yl = *yp--;
    76        ul = *up++;
    77        vl = *vp++;
    78  
    79        /* ordinary sub_n */
    80        SUBC_LIMB (cy1, sl, ul, vl);
    81        SUBC_LIMB (cy2, rl, sl, cy);
    82        cy = cy1 | cy2;
    83        *rp++ = rl;
    84  
    85        /* update (eh:el) */
    86        zl = (-cy) & yl;
    87        el += zl;
    88        eh += el < zl;
    89      }
    90    while (--n);
    91  
    92  #if GMP_NAIL_BITS != 0
    93    eh = (eh << GMP_NAIL_BITS) + (el >> GMP_NUMB_BITS);
    94    el &= GMP_NUMB_MASK;
    95  #endif
    96  
    97    ep[0] = el;
    98    ep[1] = eh;
    99  
   100    return cy;
   101  }