Blame mpn/generic/redc_2.c

Packit 5c3484
/* mpn_redc_2.  Set rp[] <- up[]/R^n mod mp[].  Clobber up[].
Packit 5c3484
   mp[] is n limbs; up[] is 2n limbs.
Packit 5c3484
Packit 5c3484
   THIS IS AN INTERNAL FUNCTION WITH A MUTABLE INTERFACE.  IT IS ONLY
Packit 5c3484
   SAFE TO REACH THIS FUNCTION THROUGH DOCUMENTED INTERFACES.
Packit 5c3484
Packit 5c3484
Copyright (C) 2000-2002, 2004, 2008, 2012 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
#include "longlong.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
#if GMP_NAIL_BITS != 0
Packit 5c3484
you lose
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
/* For testing purposes, define our own mpn_addmul_2 if there is none already
Packit 5c3484
   available.  */
Packit 5c3484
#ifndef HAVE_NATIVE_mpn_addmul_2
Packit 5c3484
#undef mpn_addmul_2
Packit 5c3484
static mp_limb_t
Packit 5c3484
mpn_addmul_2 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_srcptr vp)
Packit 5c3484
{
Packit 5c3484
  rp[n] = mpn_addmul_1 (rp, up, n, vp[0]);
Packit 5c3484
  return mpn_addmul_1 (rp + 1, up, n, vp[1]);
Packit 5c3484
}
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
#if defined (__GNUC__) && ! defined (NO_ASM) \
Packit 5c3484
  && defined (__ia64) && W_TYPE_SIZE == 64
Packit 5c3484
#define umul2low(ph, pl, uh, ul, vh, vl) \
Packit 5c3484
  do {									\
Packit 5c3484
    mp_limb_t _ph, _pl;							\
Packit 5c3484
    __asm__ ("xma.hu %0 = %3, %5, f0\n\t"				\
Packit 5c3484
	     "xma.l %1 = %3, %5, f0\n\t"				\
Packit 5c3484
	     ";;\n\t"							\
Packit 5c3484
	     "xma.l %0 = %3, %4, %0\n\t"				\
Packit 5c3484
	     ";;\n\t"							\
Packit 5c3484
	     "xma.l %0 = %2, %5, %0"					\
Packit 5c3484
	     : "=&f" (ph), "=&f" (pl)					\
Packit 5c3484
	     : "f" (uh), "f" (ul), "f" (vh), "f" (vl));			\
Packit 5c3484
  } while (0)
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
#ifndef umul2low
Packit 5c3484
#define umul2low(ph, pl, uh, ul, vh, vl) \
Packit 5c3484
  do {									\
Packit 5c3484
    mp_limb_t _ph, _pl;							\
Packit 5c3484
    umul_ppmm (_ph, _pl, ul, vl);					\
Packit 5c3484
    (ph) = _ph + (ul) * (vh) + (uh) * (vl);				\
Packit 5c3484
    (pl) = _pl;								\
Packit 5c3484
  } while (0)
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
mp_limb_t
Packit 5c3484
mpn_redc_2 (mp_ptr rp, mp_ptr up, mp_srcptr mp, mp_size_t n, mp_srcptr mip)
Packit 5c3484
{
Packit 5c3484
  mp_limb_t q[2];
Packit 5c3484
  mp_size_t j;
Packit 5c3484
  mp_limb_t upn;
Packit 5c3484
  mp_limb_t cy;
Packit 5c3484
Packit 5c3484
  ASSERT (n > 0);
Packit 5c3484
  ASSERT_MPN (up, 2*n);
Packit 5c3484
Packit 5c3484
  if ((n & 1) != 0)
Packit 5c3484
    {
Packit 5c3484
      up[0] = mpn_addmul_1 (up, mp, n, (up[0] * mip[0]) & GMP_NUMB_MASK);
Packit 5c3484
      up++;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  for (j = n - 2; j >= 0; j -= 2)
Packit 5c3484
    {
Packit 5c3484
      umul2low (q[1], q[0], mip[1], mip[0], up[1], up[0]);
Packit 5c3484
      upn = up[n];		/* mpn_addmul_2 overwrites this */
Packit 5c3484
      up[1] = mpn_addmul_2 (up, mp, n, q);
Packit 5c3484
      up[0] = up[n];
Packit 5c3484
      up[n] = upn;
Packit 5c3484
      up += 2;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  cy = mpn_add_n (rp, up, up - n, n);
Packit 5c3484
  return cy;
Packit 5c3484
}