THIS FILE CONTAINS INTERNAL FUNCTIONS WITH MUTABLE INTERFACES. IT IS ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
Copyright 2013 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
or
* the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
or both in parallel, as here.
The GNU MP Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received copies of the GNU General Public License and the GNU Lesser General Public License along with the GNU MP Library. If not,
see https://www.gnu.org/licenses/. */
#include"gmp-impl.h" #include"longlong.h"
#if GMP_NAIL_BITS > 0 #error Nail bits not supported #endif
/* Divides (uh B^n + {up, n}) by d, storing the quotient at {qp, n}.
Requires that uh < d. */
mp_limb_t
mpn_div_qr_1n_pi1 (mp_ptr qp, mp_srcptr up, mp_size_t n, mp_limb_t uh,
mp_limb_t d, mp_limb_t dinv)
{
ASSERT (n > 0);
ASSERT (uh < d);
ASSERT (d & GMP_NUMB_HIGHBIT);
ASSERT (MPN_SAME_OR_SEPARATE_P (qp, up, n));
do
{
mp_limb_t q, ul;
ul = up[--n];
udiv_qrnnd_preinv (q, uh, uh, ul, d, dinv);
qp[n] = q;
} while (n > 0);
return uh;
}
#elif DIV_QR_1N_METHOD == 2
/* The main idea of this algorithm is to write B^2 = d (B + dinv) + B2, where 1 <= B2 < d. Similarly to mpn_mod_1_1p, each iteration can then replace
u1 B^2 = u1 B2 (mod d)
which gives a very short critical path for computing the remainder (with some tricks to handle the carry when the next two lower limbs are added in). To also get the quotient, include the corresponding multiple of d in the expression,
u1 B^2 = u1 B2 + (u1 dinv + u1 B) d
We get the quotient by accumulating the (u1 dinv + u1 B) terms. The two multiplies, u1 * B2 and u1 * dinv, are independent, and can be executed in parallel.
*/
mp_limb_t
mpn_div_qr_1n_pi1 (mp_ptr qp, mp_srcptr up, mp_size_t n, mp_limb_t u1,
mp_limb_t d, mp_limb_t dinv)
{
mp_limb_t B2;
mp_limb_t u0, u2;
mp_limb_t q0, q1;
mp_limb_t p0, p1;
mp_limb_t t;
mp_size_t j;
/* This variant handles carry from the u update earlier. This gives a longer critical path, but reduces the work needed for the
quotients. */
mp_limb_t
mpn_div_qr_1n_pi1 (mp_ptr qp, mp_srcptr up, mp_size_t n, mp_limb_t u1,
mp_limb_t d, mp_limb_t dinv)
{
mp_limb_t B2;
mp_limb_t cy, u0;
mp_limb_t q0, q1;
mp_limb_t p0, p1;
mp_limb_t t;
mp_size_t j;
/* FIXME: Keep q1 in a variable between iterations, to reduce number
of memory accesses. */ for (j = n-2; j-- > 0; )
{
mp_limb_t q2, cy;
mp_limb_t t1, t0;
/* After read of up[n-1], to allow qp == up. */
qp[n-1] = q1 - u2;
/* FIXME: Keep q1 in a variable between iterations, to reduce number
of memory accesses. */ for (j = n-2; j-- > 0; )
{
mp_limb_t q2, cy;
mp_limb_t t1, t0;
/* Additions for the q update. *After* u1 -= u2 & d adjustment. * +-------+ * |u1 * v | * +---+---+ * | u1| * +---+ * | 1 | (conditional on {u1, u0} carry) * +---+ * + | q0| * -+---+---+---+ * | q2| q1| q0| * +---+---+---+ * * Additions for the u update. *Before* u1 -= u2 & d adjstment. * +-------+ * |u1 * B2| * +---+---+ * |u0 |u-1| * +---+---+ + + |B2(B-d)| (conditional on u2) * -+---+---+---+ * |u2 |u1 | u0| * +---+---+---+ *
*/ /* Multiply with unadjusted u1, to shorten critical path. */
umul_ppmm (p1, p0, u1, B2);
u1 -= (d & u2);
ADDC_LIMB (q2, q1, u1, q0);
umul_ppmm (t1, t0, u1, dinv);
add_mssaaaa (cy, u1, u0, u0, up[j], u2 & B2d1, u2 & B2d0);
add_mssaaaa (u2, u1, u0, u1, u0, p1, p0);
u2 += cy;
ASSERT(-u2 <= 1);
/* t1 <= B-2, so u2 can be added in without overflow. */
add_ssaaaa (q2, q1, q2, q1, CNST_LIMB(0), t1 - u2);
q0 = t0;
/* Final q update */
qp[j+1] = q1;
MPN_INCR_U (qp+j+2, n-j-2, q2);
}
u1 -= u2 & d;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.