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

     1  /* mpf_div_ui -- Divide a float with an unsigned integer.
     2  
     3  Copyright 1993, 1994, 1996, 2000-2002, 2004, 2005, 2012 Free Software
     4  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  void
    37  mpf_div_ui (mpf_ptr r, mpf_srcptr u, unsigned long int v)
    38  {
    39    mp_srcptr up;
    40    mp_ptr rp, tp, rtp;
    41    mp_size_t usize;
    42    mp_size_t rsize, tsize;
    43    mp_size_t sign_quotient;
    44    mp_size_t prec;
    45    mp_limb_t q_limb;
    46    mp_exp_t rexp;
    47    TMP_DECL;
    48  
    49  #if BITS_PER_ULONG > GMP_NUMB_BITS  /* avoid warnings about shift amount */
    50    if (v > GMP_NUMB_MAX)
    51      {
    52        mpf_t vf;
    53        mp_limb_t vl[2];
    54        SIZ(vf) = 2;
    55        EXP(vf) = 2;
    56        PTR(vf) = vl;
    57        vl[0] = v & GMP_NUMB_MASK;
    58        vl[1] = v >> GMP_NUMB_BITS;
    59        mpf_div (r, u, vf);
    60        return;
    61      }
    62  #endif
    63  
    64    if (UNLIKELY (v == 0))
    65      DIVIDE_BY_ZERO;
    66  
    67    usize = u->_mp_size;
    68  
    69    if (usize == 0)
    70      {
    71        r->_mp_size = 0;
    72        r->_mp_exp = 0;
    73        return;
    74      }
    75  
    76    sign_quotient = usize;
    77    usize = ABS (usize);
    78    prec = r->_mp_prec;
    79  
    80    TMP_MARK;
    81  
    82    rp = r->_mp_d;
    83    up = u->_mp_d;
    84  
    85    tsize = 1 + prec;
    86    tp = TMP_ALLOC_LIMBS (tsize + 1);
    87  
    88    if (usize > tsize)
    89      {
    90        up += usize - tsize;
    91        usize = tsize;
    92        rtp = tp;
    93      }
    94    else
    95      {
    96        MPN_ZERO (tp, tsize - usize);
    97        rtp = tp + (tsize - usize);
    98      }
    99  
   100    /* Move the dividend to the remainder.  */
   101    MPN_COPY (rtp, up, usize);
   102  
   103    mpn_divmod_1 (rp, tp, tsize, (mp_limb_t) v);
   104    q_limb = rp[tsize - 1];
   105  
   106    rsize = tsize - (q_limb == 0);
   107    rexp = u->_mp_exp - (q_limb == 0);
   108    r->_mp_size = sign_quotient >= 0 ? rsize : -rsize;
   109    r->_mp_exp = rexp;
   110    TMP_FREE;
   111  }