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

     1  /* Alpha mpn_divexact_1 -- mpn by limb exact division.
     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  /*      cycles/limb
    41     EV4:    47.0
    42     EV5:    30.0
    43     EV6:    15.0
    44  */
    45  
    46  
    47  /* The dependent chain is as follows (the same as modexact), and this is
    48     what the code runs as.
    49  
    50         ev4    ev5   ev6
    51          1      1     1    sub    y = x - h
    52         23     13     7    mulq   q = y * inverse
    53         23     15     7    umulh  h = high (q * d)
    54         --     --    --
    55         47     30    15
    56  
    57     The time to load src[i+1] and establish x hides under the umulh latency.  */
    58  
    59  void
    60  mpn_divexact_1 (mp_ptr dst, mp_srcptr src, mp_size_t size, mp_limb_t divisor)
    61  {
    62    mp_limb_t  inverse, lshift_mask, s, sr, s_next, c, h, x, y, q, dummy;
    63    unsigned   rshift, lshift;
    64  
    65    ASSERT (size >= 1);
    66    ASSERT (divisor != 0);
    67    ASSERT (MPN_SAME_OR_SEPARATE_P (dst, src, size));
    68    ASSERT_MPN (src, size);
    69    ASSERT_LIMB (divisor);
    70  
    71    s_next = *src++;   /* src[0] */
    72  
    73    rshift = 0;
    74    lshift_mask = 0;
    75    if ((divisor & 1) == 0)
    76      {
    77        count_trailing_zeros (rshift, divisor);
    78        lshift_mask = MP_LIMB_T_MAX;
    79        divisor >>= rshift;
    80      }
    81  
    82    binvert_limb (inverse, divisor);
    83    lshift = 64 - rshift;
    84  
    85    c = 0;
    86    h = 0;
    87    sr = s_next >> rshift;
    88  
    89    size--;
    90    if (LIKELY (size != 0))
    91      {
    92        do
    93          {
    94            s_next = *src++;      /* src[i+1] */
    95            s = sr | ((s_next << lshift) & lshift_mask);
    96            x = s - c;
    97            c = s < c;
    98            sr = s_next >> rshift;
    99  
   100            y = x - h;
   101            c += (x < h);
   102            q = y * inverse;
   103            *dst++ = q;
   104            umul_ppmm (h, dummy, q, divisor);
   105  
   106            size--;
   107          }
   108        while (size != 0);
   109      }
   110  
   111    x = sr - c;
   112    y = x - h;
   113    q = y * inverse;
   114    *dst = q;         /* dst[size-1] */
   115  }