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

     1  /* mpz_tdiv_ui(dividend, divisor_limb) -- Return DIVDEND mod DIVISOR_LIMB.
     2  
     3  Copyright 1991, 1993, 1994, 1996-1998, 2001, 2002, 2004, 2005, 2012 Free
     4  Software Foundation, 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  unsigned long int
    37  mpz_tdiv_ui (mpz_srcptr dividend, unsigned long int divisor)
    38  {
    39    mp_size_t ns, nn;
    40    mp_ptr np;
    41    mp_limb_t rl;
    42  
    43    if (UNLIKELY (divisor == 0))
    44      DIVIDE_BY_ZERO;
    45  
    46    ns = SIZ(dividend);
    47    if (ns == 0)
    48      {
    49        return 0;
    50      }
    51  
    52    nn = ABS(ns);
    53    np = PTR(dividend);
    54  
    55  #if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
    56    if (divisor > GMP_NUMB_MAX)
    57      {
    58        mp_limb_t dp[2], rp[2];
    59        mp_ptr qp;
    60        mp_size_t rn;
    61        TMP_DECL;
    62  
    63        if (nn == 1)		/* tdiv_qr requirements; tested above for 0 */
    64  	{
    65  	  rl = np[0];
    66  	  return rl;
    67  	}
    68  
    69        TMP_MARK;
    70        dp[0] = divisor & GMP_NUMB_MASK;
    71        dp[1] = divisor >> GMP_NUMB_BITS;
    72        qp = TMP_ALLOC_LIMBS (nn - 2 + 1);
    73        mpn_tdiv_qr (qp, rp, (mp_size_t) 0, np, nn, dp, (mp_size_t) 2);
    74        TMP_FREE;
    75        rl = rp[0] + (rp[1] << GMP_NUMB_BITS);
    76        rn = 2 - (rp[1] == 0);  rn -= (rp[rn - 1] == 0);
    77      }
    78    else
    79  #endif
    80      {
    81        rl = mpn_mod_1 (np, nn, (mp_limb_t) divisor);
    82      }
    83  
    84    return rl;
    85  }