(root)/
gmp-6.3.0/
mpz/
tdiv_qr_ui.c
       1  /* mpz_tdiv_qr_ui(quot,rem,dividend,short_divisor) --
       2     Set QUOT to DIVIDEND / SHORT_DIVISOR
       3     and REM to DIVIDEND mod SHORT_DIVISOR.
       4  
       5  Copyright 1991, 1993, 1994, 1996, 1998, 2001, 2002, 2004, 2012, 2015
       6  Free Software Foundation, Inc.
       7  
       8  This file is part of the GNU MP Library.
       9  
      10  The GNU MP Library is free software; you can redistribute it and/or modify
      11  it under the terms of either:
      12  
      13    * the GNU Lesser General Public License as published by the Free
      14      Software Foundation; either version 3 of the License, or (at your
      15      option) any later version.
      16  
      17  or
      18  
      19    * the GNU General Public License as published by the Free Software
      20      Foundation; either version 2 of the License, or (at your option) any
      21      later version.
      22  
      23  or both in parallel, as here.
      24  
      25  The GNU MP Library is distributed in the hope that it will be useful, but
      26  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
      27  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      28  for more details.
      29  
      30  You should have received copies of the GNU General Public License and the
      31  GNU Lesser General Public License along with the GNU MP Library.  If not,
      32  see https://www.gnu.org/licenses/.  */
      33  
      34  #include "gmp-impl.h"
      35  
      36  unsigned long int
      37  mpz_tdiv_qr_ui (mpz_ptr quot, mpz_ptr rem, mpz_srcptr dividend, unsigned long int divisor)
      38  {
      39    mp_size_t ns, nn, qn;
      40    mp_ptr np, qp;
      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        SIZ(quot) = 0;
      50        SIZ(rem) = 0;
      51        return 0;
      52      }
      53  
      54    nn = ABS(ns);
      55    qp = MPZ_REALLOC (quot, nn);
      56    np = PTR(dividend);
      57  
      58  #if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
      59    if (divisor > GMP_NUMB_MAX)
      60      {
      61        mp_limb_t dp[2];
      62        mp_ptr rp;
      63        mp_size_t rn;
      64  
      65        if (nn == 1)		/* tdiv_qr requirements; tested above for 0 */
      66  	{
      67  	  SIZ(quot) = 0;
      68  	  rl = np[0];
      69  	  SIZ(rem) = ns >= 0 ? 1 : -1;
      70  	  MPZ_NEWALLOC (rem, 1)[0] = rl;
      71  	  return rl;
      72  	}
      73  
      74        rp = MPZ_REALLOC (rem, 2);
      75  
      76        dp[0] = divisor & GMP_NUMB_MASK;
      77        dp[1] = divisor >> GMP_NUMB_BITS;
      78        mpn_tdiv_qr (qp, rp, (mp_size_t) 0, np, nn, dp, (mp_size_t) 2);
      79        rl = rp[0] + (rp[1] << GMP_NUMB_BITS);
      80        qn = nn - 2 + 1; qn -= qp[qn - 1] == 0; qn -= qn != 0 && qp[qn - 1] == 0;
      81        rn = 2 - (rp[1] == 0);  rn -= (rp[rn - 1] == 0);
      82        SIZ(rem) = ns >= 0 ? rn : -rn;
      83      }
      84    else
      85  #endif
      86      {
      87        rl = mpn_divrem_1 (qp, (mp_size_t) 0, np, nn, (mp_limb_t) divisor);
      88        if (rl == 0)
      89  	SIZ(rem) = 0;
      90        else
      91  	{
      92  	  SIZ(rem) = ns >= 0 ? 1 : -1;
      93  	  MPZ_NEWALLOC (rem, 1)[0] = rl;
      94  	}
      95        qn = nn - (qp[nn - 1] == 0);
      96      }
      97  
      98    SIZ(quot) = ns >= 0 ? qn : -qn;
      99    return rl;
     100  }