1 /* mpn_mod_1_1p (ap, n, b, cps)
2 Divide (ap,,n) by b. Return the single-limb remainder.
4 Contributed to the GNU project by Torbjorn Granlund.
5 Based on a suggestion by Peter L. Montgomery.
7 THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
8 SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
9 GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
11 Copyright 2008, 2009 Free Software Foundation, Inc.
13 This file is part of the GNU MP Library.
15 The GNU MP Library is free software; you can redistribute it and/or modify
16 it under the terms of the GNU Lesser General Public License as published by
17 the Free Software Foundation; either version 3 of the License, or (at your
18 option) any later version.
20 The GNU MP Library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
23 License for more details.
25 You should have received a copy of the GNU Lesser General Public License
26 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
33 mpn_mod_1_1p_cps (mp_limb_t cps[4], mp_limb_t b)
36 mp_limb_t B1modb, B2modb;
39 count_leading_zeros (cnt, b);
44 if (UNLIKELY (cnt == 0))
47 B1modb = -b * ((bi >> (GMP_LIMB_BITS-cnt)) | (CNST_LIMB(1) << cnt));
48 ASSERT (B1modb <= b); /* NB: not fully reduced mod b */
49 udiv_rnd_preinv (B2modb, B1modb, b, bi);
53 cps[2] = B1modb >> cnt;
54 cps[3] = B2modb >> cnt;
58 mpn_mod_1_1p (mp_srcptr ap, mp_size_t n, mp_limb_t b, mp_limb_t bmodb[4])
60 mp_limb_t rh, rl, bi, q, ph, pl, r;
61 mp_limb_t B1modb, B2modb;
66 ASSERT (n >= 2); /* fix tuneup.c if this is changed */
71 umul_ppmm (ph, pl, ap[n - 1], B1modb);
72 add_ssaaaa (rh, rl, ph, pl, 0, ap[n - 2]);
74 for (i = n - 3; i >= 0; i -= 1)
77 + LO(rr) * (B mod b) <= (B-1)(b-1)
78 + HI(rr) * (B^2 mod b) <= (B-1)(b-1)
80 umul_ppmm (ph, pl, rl, B1modb);
81 add_ssaaaa (ph, pl, ph, pl, 0, ap[i]);
83 umul_ppmm (rh, rl, rh, B2modb);
84 add_ssaaaa (rh, rl, rh, rl, ph, pl);
90 if (LIKELY (cnt != 0))
91 rh = (rh << cnt) | (rl >> (GMP_LIMB_BITS - cnt));
93 mask = -(mp_limb_t) (rh >= b);
96 udiv_qrnnd_preinv (q, r, rh, rl << cnt, b, bi);