github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpz/tdiv_r.c (about)

     1  /* mpz_tdiv_r(rem, dividend, divisor) -- Set REM to DIVIDEND mod DIVISOR.
     2  
     3  Copyright 1991, 1993, 1994, 2000, 2001, 2005, 2012 Free Software Foundation,
     4  Inc.
     5  
     6  This file is part of the GNU MP Library.
     7  
     8  The GNU MP Library is free software; you can redistribute it and/or modify
     9  it under the terms of either:
    10  
    11    * the GNU Lesser General Public License as published by the Free
    12      Software Foundation; either version 3 of the License, or (at your
    13      option) any later version.
    14  
    15  or
    16  
    17    * the GNU General Public License as published by the Free Software
    18      Foundation; either version 2 of the License, or (at your option) any
    19      later version.
    20  
    21  or both in parallel, as here.
    22  
    23  The GNU MP Library is distributed in the hope that it will be useful, but
    24  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    25  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    26  for more details.
    27  
    28  You should have received copies of the GNU General Public License and the
    29  GNU Lesser General Public License along with the GNU MP Library.  If not,
    30  see https://www.gnu.org/licenses/.  */
    31  
    32  #include "gmp.h"
    33  #include "gmp-impl.h"
    34  #include "longlong.h"
    35  
    36  void
    37  mpz_tdiv_r (mpz_ptr rem, mpz_srcptr num, mpz_srcptr den)
    38  {
    39    mp_size_t ql;
    40    mp_size_t ns, ds, nl, dl;
    41    mp_ptr np, dp, qp, rp;
    42    TMP_DECL;
    43  
    44    ns = SIZ (num);
    45    ds = SIZ (den);
    46    nl = ABS (ns);
    47    dl = ABS (ds);
    48    ql = nl - dl + 1;
    49  
    50    if (UNLIKELY (dl == 0))
    51      DIVIDE_BY_ZERO;
    52  
    53    rp = MPZ_REALLOC (rem, dl);
    54  
    55    if (ql <= 0)
    56      {
    57        if (num != rem)
    58  	{
    59  	  np = PTR (num);
    60  	  MPN_COPY (rp, np, nl);
    61  	  SIZ (rem) = SIZ (num);
    62  	}
    63        return;
    64      }
    65  
    66    TMP_MARK;
    67    qp = TMP_ALLOC_LIMBS (ql);
    68    np = PTR (num);
    69    dp = PTR (den);
    70  
    71    /* FIXME: We should think about how to handle the temporary allocation.
    72       Perhaps mpn_tdiv_qr should handle it, since it anyway often needs to
    73       allocate temp space.  */
    74  
    75    /* Copy denominator to temporary space if it overlaps with the remainder.  */
    76    if (dp == rp)
    77      {
    78        mp_ptr tp;
    79        tp = TMP_ALLOC_LIMBS (dl);
    80        MPN_COPY (tp, dp, dl);
    81        dp = tp;
    82      }
    83    /* Copy numerator to temporary space if it overlaps with the remainder.  */
    84    if (np == rp)
    85      {
    86        mp_ptr tp;
    87        tp = TMP_ALLOC_LIMBS (nl);
    88        MPN_COPY (tp, np, nl);
    89        np = tp;
    90      }
    91  
    92    mpn_tdiv_qr (qp, rp, 0L, np, nl, dp, dl);
    93  
    94    MPN_NORMALIZE (rp, dl);
    95  
    96    SIZ (rem) = ns >= 0 ? dl : -dl;
    97    TMP_FREE;
    98  }