1 /* mpn_mod_1(dividend_ptr, dividend_size, divisor_limb) --
2 Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
3 Return the single-limb remainder.
4 There are no constraints on the value of the divisor.
6 Copyright 1991, 1993, 1994, 1999, 2000, 2002, 2007, 2008, 2009 Free
7 Software Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
29 /* The size where udiv_qrnnd_preinv should be used rather than udiv_qrnnd,
30 meaning the quotient size where that should happen, the quotient size
31 being how many udiv divisions will be done.
33 The default is to use preinv always, CPUs where this doesn't suit have
34 tuned thresholds. Note in particular that preinv should certainly be
35 used if that's the only division available (USE_PREINV_ALWAYS). */
37 #ifndef MOD_1_NORM_THRESHOLD
38 #define MOD_1_NORM_THRESHOLD 0
41 #ifndef MOD_1_UNNORM_THRESHOLD
42 #define MOD_1_UNNORM_THRESHOLD 0
45 #ifndef MOD_1_1_THRESHOLD
46 #define MOD_1_1_THRESHOLD MP_SIZE_T_MAX /* default is not to use mpn_mod_1s */
49 #ifndef MOD_1_2_THRESHOLD
50 #define MOD_1_2_THRESHOLD 10
53 #ifndef MOD_1_4_THRESHOLD
54 #define MOD_1_4_THRESHOLD 120
58 /* The comments in mpn/generic/divrem_1.c apply here too.
60 As noted in the algorithms section of the manual, the shifts in the loop
61 for the unnorm case can be avoided by calculating r = a%(d*2^n), followed
62 by a final (r*2^n)%(d*2^n). In fact if it happens that a%(d*2^n) can
63 skip a division where (a*2^n)%(d*2^n) can't then there's the same number
64 of divide steps, though how often that happens depends on the assumed
65 distributions of dividend and divisor. In any case this idea is left to
66 CPU specific implementations to consider. */
69 mpn_mod_1_unnorm (mp_srcptr up, mp_size_t un, mp_limb_t d)
81 /* Skip a division if high < divisor. Having the test here before
82 normalizing will still skip as often as possible. */
83 r = up[un - 1] << GMP_NAIL_BITS;
94 /* If udiv_qrnnd doesn't need a normalized divisor, can use the simple
96 if (! UDIV_NEEDS_NORMALIZATION
97 && BELOW_THRESHOLD (un, MOD_1_UNNORM_THRESHOLD))
99 for (i = un - 1; i >= 0; i--)
101 n0 = up[i] << GMP_NAIL_BITS;
102 udiv_qrnnd (dummy, r, r, n0, d);
108 count_leading_zeros (cnt, d);
111 n1 = up[un - 1] << GMP_NAIL_BITS;
112 r = (r << cnt) | (n1 >> (GMP_LIMB_BITS - cnt));
114 if (UDIV_NEEDS_NORMALIZATION
115 && BELOW_THRESHOLD (un, MOD_1_UNNORM_THRESHOLD))
117 for (i = un - 2; i >= 0; i--)
119 n0 = up[i] << GMP_NAIL_BITS;
120 udiv_qrnnd (dummy, r, r,
121 (n1 << cnt) | (n0 >> (GMP_NUMB_BITS - cnt)),
126 udiv_qrnnd (dummy, r, r, n1 << cnt, d);
133 invert_limb (inv, d);
135 for (i = un - 2; i >= 0; i--)
137 n0 = up[i] << GMP_NAIL_BITS;
138 udiv_qrnnd_preinv (dummy, r, r,
139 (n1 << cnt) | (n0 >> (GMP_NUMB_BITS - cnt)),
144 udiv_qrnnd_preinv (dummy, r, r, n1 << cnt, d, inv);
151 mpn_mod_1_norm (mp_srcptr up, mp_size_t un, mp_limb_t d)
161 ASSERT (d & GMP_LIMB_HIGHBIT);
163 /* High limb is initial remainder, possibly with one subtract of
165 r = up[un - 1] << GMP_NAIL_BITS;
173 if (BELOW_THRESHOLD (un, MOD_1_NORM_THRESHOLD))
175 for (i = un - 1; i >= 0; i--)
177 n0 = up[i] << GMP_NAIL_BITS;
178 udiv_qrnnd (dummy, r, r, n0, d);
186 invert_limb (inv, d);
187 for (i = un - 1; i >= 0; i--)
189 n0 = up[i] << GMP_NAIL_BITS;
190 udiv_qrnnd_preinv (dummy, r, r, n0, d, inv);
198 mpn_mod_1 (mp_srcptr ap, mp_size_t n, mp_limb_t b)
203 /* Should this be handled at all? Rely on callers? Note un==0 is currently
204 required by mpz/fdiv_r_ui.c and possibly other places. */
208 if (UNLIKELY ((b & GMP_NUMB_HIGHBIT) != 0))
210 /* The functions below do not handle this large divisor. */
211 return mpn_mod_1_norm (ap, n, b);
213 else if (BELOW_THRESHOLD (n, MOD_1_1_THRESHOLD))
215 return mpn_mod_1_unnorm (ap, n, b);
217 else if (BELOW_THRESHOLD (n, MOD_1_2_THRESHOLD))
220 mpn_mod_1s_1p_cps (pre, b);
221 return mpn_mod_1s_1p (ap, n, b << pre[1], pre);
223 else if (BELOW_THRESHOLD (n, MOD_1_4_THRESHOLD) || UNLIKELY (b > GMP_NUMB_MASK / 4))
226 mpn_mod_1s_2p_cps (pre, b);
227 return mpn_mod_1s_2p (ap, n, b << pre[1], pre);
232 mpn_mod_1s_4p_cps (pre, b);
233 return mpn_mod_1s_4p (ap, n, b << pre[1], pre);