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

     1  /* mpn_divrem_2 -- Divide natural numbers, producing both remainder and
     2     quotient.  The divisor is two limbs.
     3  
     4     THIS FILE CONTAINS INTERNAL FUNCTIONS WITH MUTABLE INTERFACES.  IT IS ONLY
     5     SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
     6     GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
     7  
     8  
     9  Copyright 1993-1996, 1999-2002 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  #include "longlong.h"
    40  
    41  
    42  /* Divide num {np,nn} by den {dp,2} and write the nn-2 least significant
    43     quotient limbs at qp and the 2 long remainder at np.  If qxn is non-zero,
    44     generate that many fraction bits and append them after the other quotient
    45     limbs.  Return the most significant limb of the quotient, this is always 0
    46     or 1.
    47  
    48     Preconditions:
    49     1. The most significant bit of the divisor must be set.
    50     2. qp must either not overlap with the input operands at all, or
    51        qp >= np + 2 must hold true.  (This means that it's possible to put
    52        the quotient in the high part of {np,nn}, right above the remainder.
    53     3. nn >= 2, even if qxn is non-zero.  */
    54  
    55  mp_limb_t
    56  mpn_divrem_2 (mp_ptr qp, mp_size_t qxn,
    57  	      mp_ptr np, mp_size_t nn,
    58  	      mp_srcptr dp)
    59  {
    60    mp_limb_t most_significant_q_limb;
    61    mp_size_t i;
    62    mp_limb_t r1, r0, d1, d0;
    63    gmp_pi1_t di;
    64  
    65    ASSERT (nn >= 2);
    66    ASSERT (qxn >= 0);
    67    ASSERT (dp[1] & GMP_NUMB_HIGHBIT);
    68    ASSERT (! MPN_OVERLAP_P (qp, nn-2+qxn, np, nn) || qp >= np+2);
    69    ASSERT_MPN (np, nn);
    70    ASSERT_MPN (dp, 2);
    71  
    72    np += nn - 2;
    73    d1 = dp[1];
    74    d0 = dp[0];
    75    r1 = np[1];
    76    r0 = np[0];
    77  
    78    most_significant_q_limb = 0;
    79    if (r1 >= d1 && (r1 > d1 || r0 >= d0))
    80      {
    81  #if GMP_NAIL_BITS == 0
    82        sub_ddmmss (r1, r0, r1, r0, d1, d0);
    83  #else
    84        r0 = r0 - d0;
    85        r1 = r1 - d1 - (r0 >> GMP_LIMB_BITS - 1);
    86        r0 &= GMP_NUMB_MASK;
    87  #endif
    88        most_significant_q_limb = 1;
    89      }
    90  
    91    invert_pi1 (di, d1, d0);
    92  
    93    qp += qxn;
    94  
    95    for (i = nn - 2 - 1; i >= 0; i--)
    96      {
    97        mp_limb_t n0, q;
    98        n0 = np[-1];
    99        udiv_qr_3by2 (q, r1, r0, r1, r0, n0, d1, d0, di.inv32);
   100        np--;
   101        qp[i] = q;
   102      }
   103  
   104    if (UNLIKELY (qxn != 0))
   105      {
   106        qp -= qxn;
   107        for (i = qxn - 1; i >= 0; i--)
   108  	{
   109  	  mp_limb_t q;
   110  	  udiv_qr_3by2 (q, r1, r0, r1, r0, CNST_LIMB(0), d1, d0, di.inv32);
   111  	  qp[i] = q;
   112  	}
   113      }
   114  
   115    np[1] = r1;
   116    np[0] = r0;
   117  
   118    return most_significant_q_limb;
   119  }