(root)/
gmp-6.3.0/
mpn/
generic/
redc_2.c
       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-impl.h"
      36  #include "longlong.h"
      37  
      38  
      39  #if GMP_NAIL_BITS != 0
      40  you lose
      41  #endif
      42  
      43  /* For testing purposes, define our own mpn_addmul_2 if there is none already
      44     available.  */
      45  #ifndef HAVE_NATIVE_mpn_addmul_2
      46  #undef mpn_addmul_2
      47  static mp_limb_t
      48  mpn_addmul_2 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_srcptr vp)
      49  {
      50    rp[n] = mpn_addmul_1 (rp, up, n, vp[0]);
      51    return mpn_addmul_1 (rp + 1, up, n, vp[1]);
      52  }
      53  #endif
      54  
      55  #if defined (__GNUC__) && ! defined (NO_ASM) \
      56    && defined (__ia64) && W_TYPE_SIZE == 64
      57  #define umul2low(ph, pl, uh, ul, vh, vl) \
      58    do {									\
      59      mp_limb_t _ph, _pl;							\
      60      __asm__ ("xma.hu %0 = %3, %5, f0\n\t"				\
      61  	     "xma.l %1 = %3, %5, f0\n\t"				\
      62  	     ";;\n\t"							\
      63  	     "xma.l %0 = %3, %4, %0\n\t"				\
      64  	     ";;\n\t"							\
      65  	     "xma.l %0 = %2, %5, %0"					\
      66  	     : "=&f" (ph), "=&f" (pl)					\
      67  	     : "f" (uh), "f" (ul), "f" (vh), "f" (vl));			\
      68    } while (0)
      69  #endif
      70  
      71  #ifndef umul2low
      72  #define umul2low(ph, pl, uh, ul, vh, vl) \
      73    do {									\
      74      mp_limb_t _ph, _pl;							\
      75      umul_ppmm (_ph, _pl, ul, vl);					\
      76      (ph) = _ph + (ul) * (vh) + (uh) * (vl);				\
      77      (pl) = _pl;								\
      78    } while (0)
      79  #endif
      80  
      81  mp_limb_t
      82  mpn_redc_2 (mp_ptr rp, mp_ptr up, mp_srcptr mp, mp_size_t n, mp_srcptr mip)
      83  {
      84    mp_limb_t q[2];
      85    mp_size_t j;
      86    mp_limb_t upn;
      87    mp_limb_t cy;
      88  
      89    ASSERT (n > 0);
      90    ASSERT_MPN (up, 2*n);
      91  
      92    if ((n & 1) != 0)
      93      {
      94        up[0] = mpn_addmul_1 (up, mp, n, (up[0] * mip[0]) & GMP_NUMB_MASK);
      95        up++;
      96      }
      97  
      98    for (j = n - 2; j >= 0; j -= 2)
      99      {
     100        umul2low (q[1], q[0], mip[1], mip[0], up[1], up[0]);
     101        upn = up[n];		/* mpn_addmul_2 overwrites this */
     102        up[1] = mpn_addmul_2 (up, mp, n, q);
     103        up[0] = up[n];
     104        up[n] = upn;
     105        up += 2;
     106      }
     107  
     108    cy = mpn_add_n (rp, up, up - n, n);
     109    return cy;
     110  }