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

     1  /* mpn_redc_2.  Set rp[] <- up[]/R^n mod mp[].  Clobber up[].
     2     mp[] is n limbs; up[] is 2n limbs.
     3  
     4     THIS IS AN INTERNAL FUNCTION WITH A MUTABLE INTERFACE.  IT IS ONLY
     5     SAFE TO REACH THIS FUNCTION THROUGH DOCUMENTED INTERFACES.
     6  
     7  Copyright (C) 2000-2002, 2004, 2008, 2012 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  #if GMP_NAIL_BITS != 0
    41  you lose
    42  #endif
    43  
    44  /* For testing purposes, define our own mpn_addmul_2 if there is none already
    45     available.  */
    46  #ifndef HAVE_NATIVE_mpn_addmul_2
    47  #undef mpn_addmul_2
    48  static mp_limb_t
    49  mpn_addmul_2 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_srcptr vp)
    50  {
    51    rp[n] = mpn_addmul_1 (rp, up, n, vp[0]);
    52    return mpn_addmul_1 (rp + 1, up, n, vp[1]);
    53  }
    54  #endif
    55  
    56  #if defined (__GNUC__) && ! defined (NO_ASM) \
    57    && defined (__ia64) && W_TYPE_SIZE == 64
    58  #define umul2low(ph, pl, uh, ul, vh, vl) \
    59    do {									\
    60      mp_limb_t _ph, _pl;							\
    61      __asm__ ("xma.hu %0 = %3, %5, f0\n\t"				\
    62  	     "xma.l %1 = %3, %5, f0\n\t"				\
    63  	     ";;\n\t"							\
    64  	     "xma.l %0 = %3, %4, %0\n\t"				\
    65  	     ";;\n\t"							\
    66  	     "xma.l %0 = %2, %5, %0"					\
    67  	     : "=&f" (ph), "=&f" (pl)					\
    68  	     : "f" (uh), "f" (ul), "f" (vh), "f" (vl));			\
    69    } while (0)
    70  #endif
    71  
    72  #ifndef umul2low
    73  #define umul2low(ph, pl, uh, ul, vh, vl) \
    74    do {									\
    75      mp_limb_t _ph, _pl;							\
    76      umul_ppmm (_ph, _pl, ul, vl);					\
    77      (ph) = _ph + (ul) * (vh) + (uh) * (vl);				\
    78      (pl) = _pl;								\
    79    } while (0)
    80  #endif
    81  
    82  mp_limb_t
    83  mpn_redc_2 (mp_ptr rp, mp_ptr up, mp_srcptr mp, mp_size_t n, mp_srcptr mip)
    84  {
    85    mp_limb_t q[2];
    86    mp_size_t j;
    87    mp_limb_t upn;
    88    mp_limb_t cy;
    89  
    90    ASSERT (n > 0);
    91    ASSERT_MPN (up, 2*n);
    92  
    93    if ((n & 1) != 0)
    94      {
    95        up[0] = mpn_addmul_1 (up, mp, n, (up[0] * mip[0]) & GMP_NUMB_MASK);
    96        up++;
    97      }
    98  
    99    for (j = n - 2; j >= 0; j -= 2)
   100      {
   101        umul2low (q[1], q[0], mip[1], mip[0], up[1], up[0]);
   102        upn = up[n];		/* mpn_addmul_2 overwrites this */
   103        up[1] = mpn_addmul_2 (up, mp, n, q);
   104        up[0] = up[n];
   105        up[n] = upn;
   106        up += 2;
   107      }
   108  
   109    cy = mpn_add_n (rp, up, up - n, n);
   110    return cy;
   111  }