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

     1  /* mpn_bdiv_qr -- Hensel division with precomputed inverse, returning quotient
     2     and remainder.
     3  
     4     Contributed to the GNU project by Torbjorn Granlund.
     5  
     6     THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES.  IT IS ONLY
     7     SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
     8     GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
     9  
    10  Copyright 2006, 2007, 2009, 2012 Free Software Foundation, Inc.
    11  
    12  This file is part of the GNU MP Library.
    13  
    14  The GNU MP Library is free software; you can redistribute it and/or modify
    15  it under the terms of either:
    16  
    17    * the GNU Lesser General Public License as published by the Free
    18      Software Foundation; either version 3 of the License, or (at your
    19      option) any later version.
    20  
    21  or
    22  
    23    * the GNU General Public License as published by the Free Software
    24      Foundation; either version 2 of the License, or (at your option) any
    25      later version.
    26  
    27  or both in parallel, as here.
    28  
    29  The GNU MP Library is distributed in the hope that it will be useful, but
    30  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    31  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    32  for more details.
    33  
    34  You should have received copies of the GNU General Public License and the
    35  GNU Lesser General Public License along with the GNU MP Library.  If not,
    36  see https://www.gnu.org/licenses/.  */
    37  
    38  #include "gmp.h"
    39  #include "gmp-impl.h"
    40  
    41  
    42  /* Computes Q = N / D mod B^n,
    43  	    R = N - QD.  */
    44  
    45  mp_limb_t
    46  mpn_bdiv_qr (mp_ptr qp, mp_ptr rp,
    47  	     mp_srcptr np, mp_size_t nn,
    48  	     mp_srcptr dp, mp_size_t dn,
    49  	     mp_ptr tp)
    50  {
    51    mp_limb_t di;
    52    mp_limb_t rh;
    53  
    54    ASSERT (nn > dn);
    55    if (BELOW_THRESHOLD (dn, DC_BDIV_QR_THRESHOLD) ||
    56        BELOW_THRESHOLD (nn - dn, DC_BDIV_QR_THRESHOLD))
    57      {
    58        MPN_COPY (tp, np, nn);
    59        binvert_limb (di, dp[0]);  di = -di;
    60        rh = mpn_sbpi1_bdiv_qr (qp, tp, nn, dp, dn, di);
    61        MPN_COPY (rp, tp + nn - dn, dn);
    62      }
    63    else if (BELOW_THRESHOLD (dn, MU_BDIV_QR_THRESHOLD))
    64      {
    65        MPN_COPY (tp, np, nn);
    66        binvert_limb (di, dp[0]);  di = -di;
    67        rh = mpn_dcpi1_bdiv_qr (qp, tp, nn, dp, dn, di);
    68        MPN_COPY (rp, tp + nn - dn, dn);
    69      }
    70    else
    71      {
    72        rh = mpn_mu_bdiv_qr (qp, rp, np, nn, dp, dn, tp);
    73      }
    74  
    75    return rh;
    76  }
    77  
    78  mp_size_t
    79  mpn_bdiv_qr_itch (mp_size_t nn, mp_size_t dn)
    80  {
    81    if (BELOW_THRESHOLD (dn, MU_BDIV_QR_THRESHOLD))
    82      return nn;
    83    else
    84      return  mpn_mu_bdiv_qr_itch (nn, dn);
    85  }