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 Free Software Foundation, Inc.
8
This file is part of the GNU MP Library.
10
The GNU MP Library is free software; you can redistribute it and/or modify
11
it under the terms of the GNU Lesser General Public License as published by
12
the Free Software Foundation; either version 2.1 of the License, or (at your
13
option) any later version.
15
The GNU MP Library is distributed in the hope that it will be useful, but
16
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18
License for more details.
20
You should have received a copy of the GNU Lesser General Public License
21
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
22
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23
MA 02111-1307, USA. */
30
/* The size where udiv_qrnnd_preinv should be used rather than udiv_qrnnd,
31
meaning the quotient size where that should happen, the quotient size
32
being how many udiv divisions will be done.
34
The default is to use preinv always, CPUs where this doesn't suit have
35
tuned thresholds. Note in particular that preinv should certainly be
36
used if that's the only division available (USE_PREINV_ALWAYS). */
38
#ifndef MOD_1_NORM_THRESHOLD
39
#define MOD_1_NORM_THRESHOLD 0
41
#ifndef MOD_1_UNNORM_THRESHOLD
42
#define MOD_1_UNNORM_THRESHOLD 0
46
/* The comments in mpn/generic/divrem_1.c apply here too.
48
As noted in the algorithms section of the manual, the shifts in the loop
49
for the unnorm case can be avoided by calculating r = a%(d*2^n), followed
50
by a final (r*2^n)%(d*2^n). In fact if it happens that a%(d*2^n) can
51
skip a division where (a*2^n)%(d*2^n) can't then there's the same number
52
of divide steps, though how often that happens depends on the assumed
53
distributions of dividend and divisor. In any case this idea is left to
54
CPU specific implementations to consider. */
57
mpn_mod_1 (mp_srcptr up, mp_size_t un, mp_limb_t d)
66
/* Botch: Should this be handled at all? Rely on callers?
67
But note un==0 is currently required by mpz/fdiv_r_ui.c and possibly
74
if ((d & GMP_LIMB_HIGHBIT) != 0)
76
/* High limb is initial remainder, possibly with one subtract of
78
r = up[un - 1] << GMP_NAIL_BITS;
86
if (BELOW_THRESHOLD (un, MOD_1_NORM_THRESHOLD))
89
for (i = un - 1; i >= 0; i--)
91
n0 = up[i] << GMP_NAIL_BITS;
92
udiv_qrnnd (dummy, r, r, n0, d);
100
invert_limb (inv, d);
101
for (i = un - 1; i >= 0; i--)
103
n0 = up[i] << GMP_NAIL_BITS;
104
udiv_qrnnd_preinv (dummy, r, r, n0, d, inv);
114
/* Skip a division if high < divisor. Having the test here before
115
normalizing will still skip as often as possible. */
116
r = up[un - 1] << GMP_NAIL_BITS;
127
/* If udiv_qrnnd doesn't need a normalized divisor, can use the simple
129
if (! UDIV_NEEDS_NORMALIZATION
130
&& BELOW_THRESHOLD (un, MOD_1_UNNORM_THRESHOLD))
133
count_leading_zeros (norm, d);
136
n1 = up[un - 1] << GMP_NAIL_BITS;
137
r = (r << norm) | (n1 >> (GMP_LIMB_BITS - norm));
139
if (UDIV_NEEDS_NORMALIZATION
140
&& BELOW_THRESHOLD (un, MOD_1_UNNORM_THRESHOLD))
142
for (i = un - 2; i >= 0; i--)
144
n0 = up[i] << GMP_NAIL_BITS;
145
udiv_qrnnd (dummy, r, r,
146
(n1 << norm) | (n0 >> (GMP_NUMB_BITS - norm)),
151
udiv_qrnnd (dummy, r, r, n1 << norm, d);
158
invert_limb (inv, d);
160
for (i = un - 2; i >= 0; i--)
162
n0 = up[i] << GMP_NAIL_BITS;
163
udiv_qrnnd_preinv (dummy, r, r,
164
(n1 << norm) | (n0 >> (GMP_NUMB_BITS - norm)),
169
udiv_qrnnd_preinv (dummy, r, r, n1 << norm, d, inv);