beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / mod_1_3.c
blobf4137f4315566b44de543b68a6b940101999c013
1 /* mpn_mod_1s_3p (ap, n, b, cps)
2 Divide (ap,,n) by b. Return the single-limb remainder.
3 Requires that d < B / 3.
5 Contributed to the GNU project by Torbjorn Granlund.
6 Based on a suggestion by Peter L. Montgomery.
8 THE FUNCTIONS IN THIS FILE ARE INTERNAL WITH MUTABLE INTERFACES. IT IS ONLY
9 SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
10 GUARANTEED THAT THEY WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
12 Copyright 2008-2010, 2013 Free Software Foundation, Inc.
14 This file is part of the GNU MP Library.
16 The GNU MP Library is free software; you can redistribute it and/or modify
17 it under the terms of either:
19 * the GNU Lesser General Public License as published by the Free
20 Software Foundation; either version 3 of the License, or (at your
21 option) any later version.
25 * the GNU General Public License as published by the Free Software
26 Foundation; either version 2 of the License, or (at your option) any
27 later version.
29 or both in parallel, as here.
31 The GNU MP Library is distributed in the hope that it will be useful, but
32 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
33 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
34 for more details.
36 You should have received copies of the GNU General Public License and the
37 GNU Lesser General Public License along with the GNU MP Library. If not,
38 see https://www.gnu.org/licenses/. */
40 #include "gmp.h"
41 #include "gmp-impl.h"
42 #include "longlong.h"
44 void
45 mpn_mod_1s_3p_cps (mp_limb_t cps[6], mp_limb_t b)
47 mp_limb_t bi;
48 mp_limb_t B1modb, B2modb, B3modb, B4modb;
49 int cnt;
51 ASSERT (b <= (~(mp_limb_t) 0) / 3);
53 count_leading_zeros (cnt, b);
55 b <<= cnt;
56 invert_limb (bi, b);
58 cps[0] = bi;
59 cps[1] = cnt;
61 B1modb = -b * ((bi >> (GMP_LIMB_BITS-cnt)) | (CNST_LIMB(1) << cnt));
62 ASSERT (B1modb <= b); /* NB: not fully reduced mod b */
63 cps[2] = B1modb >> cnt;
65 udiv_rnnd_preinv (B2modb, B1modb, CNST_LIMB(0), b, bi);
66 cps[3] = B2modb >> cnt;
68 udiv_rnnd_preinv (B3modb, B2modb, CNST_LIMB(0), b, bi);
69 cps[4] = B3modb >> cnt;
71 udiv_rnnd_preinv (B4modb, B3modb, CNST_LIMB(0), b, bi);
72 cps[5] = B4modb >> cnt;
74 #if WANT_ASSERT
76 int i;
77 b = cps[2];
78 for (i = 3; i <= 5; i++)
80 b += cps[i];
81 ASSERT (b >= cps[i]);
84 #endif
87 mp_limb_t
88 mpn_mod_1s_3p (mp_srcptr ap, mp_size_t n, mp_limb_t b, const mp_limb_t cps[6])
90 mp_limb_t rh, rl, bi, ph, pl, ch, cl, r;
91 mp_limb_t B1modb, B2modb, B3modb, B4modb;
92 mp_size_t i;
93 int cnt;
95 ASSERT (n >= 1);
97 B1modb = cps[2];
98 B2modb = cps[3];
99 B3modb = cps[4];
100 B4modb = cps[5];
102 /* We compute n mod 3 in a tricky way, which works except for when n is so
103 close to the maximum size that we don't need to support it. The final
104 cast to int is a workaround for HP cc. */
105 switch ((int) ((mp_limb_t) n * MODLIMB_INVERSE_3 >> (GMP_NUMB_BITS - 2)))
107 case 0:
108 umul_ppmm (ph, pl, ap[n - 2], B1modb);
109 add_ssaaaa (ph, pl, ph, pl, CNST_LIMB(0), ap[n - 3]);
110 umul_ppmm (rh, rl, ap[n - 1], B2modb);
111 add_ssaaaa (rh, rl, rh, rl, ph, pl);
112 n -= 3;
113 break;
114 case 2: /* n mod 3 = 1 */
115 rh = 0;
116 rl = ap[n - 1];
117 n -= 1;
118 break;
119 case 1: /* n mod 3 = 2 */
120 rh = ap[n - 1];
121 rl = ap[n - 2];
122 n -= 2;
123 break;
126 for (i = n - 3; i >= 0; i -= 3)
128 /* rr = ap[i] < B
129 + ap[i+1] * (B mod b) <= (B-1)(b-1)
130 + ap[i+2] * (B^2 mod b) <= (B-1)(b-1)
131 + LO(rr) * (B^3 mod b) <= (B-1)(b-1)
132 + HI(rr) * (B^4 mod b) <= (B-1)(b-1)
134 umul_ppmm (ph, pl, ap[i + 1], B1modb);
135 add_ssaaaa (ph, pl, ph, pl, CNST_LIMB(0), ap[i + 0]);
137 umul_ppmm (ch, cl, ap[i + 2], B2modb);
138 add_ssaaaa (ph, pl, ph, pl, ch, cl);
140 umul_ppmm (ch, cl, rl, B3modb);
141 add_ssaaaa (ph, pl, ph, pl, ch, cl);
143 umul_ppmm (rh, rl, rh, B4modb);
144 add_ssaaaa (rh, rl, rh, rl, ph, pl);
147 umul_ppmm (rh, cl, rh, B1modb);
148 add_ssaaaa (rh, rl, rh, rl, CNST_LIMB(0), cl);
150 cnt = cps[1];
151 bi = cps[0];
153 r = (rh << cnt) | (rl >> (GMP_LIMB_BITS - cnt));
154 udiv_rnnd_preinv (r, r, rl << cnt, b, bi);
156 return r >> cnt;