1
/* mpz_congruent_p -- test congruence of two mpz's */
4
Copyright 2001 Free Software Foundation, Inc.
6
This file is part of the GNU MP Library.
8
The GNU MP Library is free software; you can redistribute it and/or modify
9
it under the terms of the GNU Lesser General Public License as published by
10
the Free Software Foundation; either version 2.1 of the License, or (at your
11
option) any later version.
13
The GNU MP Library is distributed in the hope that it will be useful, but
14
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16
License for more details.
18
You should have received a copy of the GNU Lesser General Public License
19
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29
/* For big divisors this code is only very slightly better than the user
30
doing a combination of mpz_sub and mpz_tdiv_r, but it's quite convenient,
31
and perhaps in the future can be improved, in similar ways to
32
mpn_divisible_p perhaps.
34
The csize==1 / dsize==1 special case makes mpz_congruent_p as good as
35
mpz_congruent_ui_p on relevant operands, though such a combination
36
probably doesn't occur often.
40
If c<d then it'd work to just form a%d and compare a and c (either as
41
a==c or a+c==d depending on the signs), but the saving from avoiding the
42
abs(a-c) calculation would be small compared to the division.
44
Similarly if both a<d and c<d then it would work to just compare a and c
45
(a==c or a+c==d), but this isn't considered a particularly important case
46
and so isn't done for the moment.
48
Low zero limbs on d could be stripped and the corresponding limbs of a
49
and c tested and skipped, but doing so would introduce a borrow when a
50
and c differ in sign and have non-zero skipped limbs. It doesn't seem
51
worth the complications to do this, since low zero limbs on d should
55
mpz_congruent_p (mpz_srcptr a, mpz_srcptr c, mpz_srcptr d)
57
mp_size_t asize, csize, dsize, xor;
60
mp_limb_t alow, clow, dlow, dmask, r;
70
if (ABSIZ(a) < ABSIZ(c))
71
MPZ_SRCPTR_SWAP (a, c);
75
xor = (asize ^ csize);
81
return mpn_divisible_p (ap, asize, dp, dsize);
90
/* Check a==c mod low zero bits of dlow. This might catch a few cases of
91
a!=c quickly, and it helps the csize==1 special cases below. */
92
dmask = LOW_ZEROS_MASK (dlow);
93
alow = (xor >= 0 ? alow : -alow);
94
if (((alow-clow) & dmask) != 0)
103
NEG_MOD (clow, clow, dlow);
105
if (BELOW_THRESHOLD (asize, MODEXACT_1_ODD_THRESHOLD))
107
r = mpn_mod_1 (ap, asize, dlow);
111
return r == (clow % dlow);
116
/* Strip low zero bits to get odd d required by modexact. If
117
d==e*2^n then a==c mod d if and only if both a==c mod e and
118
a==c mod 2^n, the latter having been done above. */
120
count_trailing_zeros (twos, dlow);
124
r = mpn_modexact_1c_odd (ap, asize, dlow, clow);
125
return r == 0 || r == dlow;
128
/* dlow==0 is avoided since we don't want to bother handling extra low
129
zero bits if dsecond is even (would involve borrow if a,c differ in
130
sign and alow,clow!=0). */
131
if (dsize == 2 && dlow != 0)
133
mp_limb_t dsecond = dp[1];
135
if (dsecond <= dmask)
138
count_trailing_zeros (twos, dlow);
139
dlow = (dlow >> twos) | (dsecond << (BITS_PER_MP_LIMB-twos));
141
/* dlow will be odd here, so the test for it even under cong_1
142
is unnecessary, but the rest of that code is wanted. */
149
xp = TMP_ALLOC_LIMBS (asize+1);
151
/* calculate abs(a-c) */
154
/* same signs, subtract */
155
if (asize > csize || mpn_cmp (ap, cp, asize) >= 0)
156
ASSERT_NOCARRY (mpn_sub (xp, ap, asize, cp, csize));
158
ASSERT_NOCARRY (mpn_sub_n (xp, cp, ap, asize));
159
MPN_NORMALIZE (xp, asize);
163
/* different signs, add */
165
carry = mpn_add (xp, ap, asize, cp, csize);
167
asize += (carry != 0);
170
result = mpn_divisible_p (xp, asize, dp, dsize);