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

     1  /* mpn_preinv_divrem_1 -- mpn by limb division with pre-inverted divisor.
     2  
     3     THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
     4     CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
     5     FUTURE GNU MP RELEASES.
     6  
     7  Copyright 2000-2003 Free Software Foundation, Inc.
     8  
     9  This file is part of the GNU MP Library.
    10  
    11  The GNU MP Library is free software; you can redistribute it and/or modify
    12  it under the terms of either:
    13  
    14    * the GNU Lesser General Public License as published by the Free
    15      Software Foundation; either version 3 of the License, or (at your
    16      option) any later version.
    17  
    18  or
    19  
    20    * the GNU General Public License as published by the Free Software
    21      Foundation; either version 2 of the License, or (at your option) any
    22      later version.
    23  
    24  or both in parallel, as here.
    25  
    26  The GNU MP Library is distributed in the hope that it will be useful, but
    27  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    28  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    29  for more details.
    30  
    31  You should have received copies of the GNU General Public License and the
    32  GNU Lesser General Public License along with the GNU MP Library.  If not,
    33  see https://www.gnu.org/licenses/.  */
    34  
    35  #include "gmp.h"
    36  #include "gmp-impl.h"
    37  #include "longlong.h"
    38  
    39  
    40  /* Don't bloat a shared library with unused code. */
    41  #if USE_PREINV_DIVREM_1
    42  
    43  /* Same test here for skipping one divide step as in mpn_divrem_1.
    44  
    45     The main reason for a separate shift==0 case is that not all CPUs give
    46     zero for "n0 >> GMP_LIMB_BITS" which would arise in the general case
    47     code used on shift==0.  shift==0 is also reasonably common in mp_bases
    48     big_base, for instance base==10 on a 64-bit limb.
    49  
    50     Under shift!=0 it would be possible to call mpn_lshift to adjust the
    51     dividend all in one go (into the quotient space say), rather than
    52     limb-by-limb in the loop.  This might help if mpn_lshift is a lot faster
    53     than what the compiler can generate for EXTRACT.  But this is left to CPU
    54     specific implementations to consider, especially since EXTRACT isn't on
    55     the dependent chain.
    56  
    57     If size==0 then the result is simply xsize limbs of zeros, but nothing
    58     special is done for that, since it wouldn't be a usual call, and
    59     certainly never arises from mpn_get_str which is our main caller.  */
    60  
    61  mp_limb_t
    62  mpn_preinv_divrem_1 (mp_ptr qp, mp_size_t xsize,
    63  		     mp_srcptr ap, mp_size_t size, mp_limb_t d_unnorm,
    64  		     mp_limb_t dinv, int shift)
    65  {
    66    mp_limb_t  ahigh, qhigh, r;
    67    mp_size_t  i;
    68    mp_limb_t  n1, n0;
    69    mp_limb_t  d;
    70  
    71    ASSERT (xsize >= 0);
    72    ASSERT (size >= 1);
    73    ASSERT (d_unnorm != 0);
    74  #if WANT_ASSERT
    75    {
    76      int        want_shift;
    77      mp_limb_t  want_dinv;
    78      count_leading_zeros (want_shift, d_unnorm);
    79      ASSERT (shift == want_shift);
    80      invert_limb (want_dinv, d_unnorm << shift);
    81      ASSERT (dinv == want_dinv);
    82    }
    83  #endif
    84    /* FIXME: What's the correct overlap rule when xsize!=0? */
    85    ASSERT (MPN_SAME_OR_SEPARATE_P (qp+xsize, ap, size));
    86  
    87    ahigh = ap[size-1];
    88    d = d_unnorm << shift;
    89    qp += (size + xsize - 1);   /* dest high limb */
    90  
    91    if (shift == 0)
    92      {
    93        /* High quotient limb is 0 or 1, and skip a divide step. */
    94        r = ahigh;
    95        qhigh = (r >= d);
    96        r = (qhigh ? r-d : r);
    97        *qp-- = qhigh;
    98        size--;
    99  
   100        for (i = size-1; i >= 0; i--)
   101  	{
   102  	  n0 = ap[i];
   103  	  udiv_qrnnd_preinv (*qp, r, r, n0, d, dinv);
   104  	  qp--;
   105  	}
   106      }
   107    else
   108      {
   109        r = 0;
   110        if (ahigh < d_unnorm)
   111  	{
   112  	  r = ahigh << shift;
   113  	  *qp-- = 0;
   114  	  size--;
   115  	  if (size == 0)
   116  	    goto done_integer;
   117  	}
   118  
   119        n1 = ap[size-1];
   120        r |= n1 >> (GMP_LIMB_BITS - shift);
   121  
   122        for (i = size-2; i >= 0; i--)
   123  	{
   124  	  ASSERT (r < d);
   125  	  n0 = ap[i];
   126  	  udiv_qrnnd_preinv (*qp, r, r,
   127  			     ((n1 << shift) | (n0 >> (GMP_LIMB_BITS - shift))),
   128  			     d, dinv);
   129  	  qp--;
   130  	  n1 = n0;
   131  	}
   132        udiv_qrnnd_preinv (*qp, r, r, n1 << shift, d, dinv);
   133        qp--;
   134      }
   135  
   136   done_integer:
   137    for (i = 0; i < xsize; i++)
   138      {
   139        udiv_qrnnd_preinv (*qp, r, r, CNST_LIMB(0), d, dinv);
   140        qp--;
   141      }
   142  
   143    return r >> shift;
   144  }
   145  
   146  #endif /* USE_PREINV_DIVREM_1 */