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

     1  /* mpn_dc_div_q -- divide-and-conquer division, returning exact quotient
     2     only.
     3  
     4     Contributed to the GNU project by Torbjorn Granlund and Marco Bodrato.
     5  
     6     THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.  IT IS ONLY
     7     SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
     8     GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
     9  
    10  Copyright 2006, 2007, 2009, 2010 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  mp_limb_t
    43  mpn_dcpi1_div_q (mp_ptr qp, mp_ptr np, mp_size_t nn,
    44  		 mp_srcptr dp, mp_size_t dn, gmp_pi1_t *dinv)
    45  {
    46    mp_ptr tp, wp;
    47    mp_limb_t qh;
    48    mp_size_t qn;
    49    TMP_DECL;
    50  
    51    TMP_MARK;
    52  
    53    ASSERT (dn >= 6);
    54    ASSERT (nn - dn >= 3);
    55    ASSERT (dp[dn-1] & GMP_NUMB_HIGHBIT);
    56  
    57    tp = TMP_ALLOC_LIMBS (nn + 1);
    58    MPN_COPY (tp + 1, np, nn);
    59    tp[0] = 0;
    60  
    61    qn = nn - dn;
    62    wp = TMP_ALLOC_LIMBS (qn + 1);
    63  
    64    qh = mpn_dcpi1_divappr_q (wp, tp, nn + 1, dp, dn, dinv);
    65  
    66    if (wp[0] == 0)
    67      {
    68        mp_limb_t cy;
    69  
    70        if (qn > dn)
    71  	mpn_mul (tp, wp + 1, qn, dp, dn);
    72        else
    73  	mpn_mul (tp, dp, dn, wp + 1, qn);
    74  
    75        cy = (qh != 0) ? mpn_add_n (tp + qn, tp + qn, dp, dn) : 0;
    76  
    77        if (cy || mpn_cmp (tp, np, nn) > 0) /* At most is wrong by one, no cycle. */
    78  	qh -= mpn_sub_1 (qp, wp + 1, qn, 1);
    79        else /* Same as below */
    80  	MPN_COPY (qp, wp + 1, qn);
    81      }
    82    else
    83      MPN_COPY (qp, wp + 1, qn);
    84  
    85    TMP_FREE;
    86    return qh;
    87  }