1 /* mpz_congruent_p -- test congruence of two mpz's.
3 Copyright 2001, 2002, 2005 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
36 /* For big divisors this code is only very slightly better than the user
37 doing a combination of mpz_sub and mpz_tdiv_r, but it's quite convenient,
38 and perhaps in the future can be improved, in similar ways to
39 mpn_divisible_p perhaps.
41 The csize==1 / dsize==1 special case makes mpz_congruent_p as good as
42 mpz_congruent_ui_p on relevant operands, though such a combination
43 probably doesn't occur often.
47 If c<d then it'd work to just form a%d and compare a and c (either as
48 a==c or a+c==d depending on the signs), but the saving from avoiding the
49 abs(a-c) calculation would be small compared to the division.
51 Similarly if both a<d and c<d then it would work to just compare a and c
52 (a==c or a+c==d), but this isn't considered a particularly important case
53 and so isn't done for the moment.
55 Low zero limbs on d could be stripped and the corresponding limbs of a
56 and c tested and skipped, but doing so would introduce a borrow when a
57 and c differ in sign and have non-zero skipped limbs. It doesn't seem
58 worth the complications to do this, since low zero limbs on d should
62 mpz_congruent_p (mpz_srcptr a
, mpz_srcptr c
, mpz_srcptr d
)
64 mp_size_t asize
, csize
, dsize
, sign
;
67 mp_limb_t alow
, clow
, dlow
, dmask
, r
;
72 if (UNLIKELY (dsize
== 0))
73 return (mpz_cmp (a
, c
) == 0);
78 if (ABSIZ(a
) < ABSIZ(c
))
79 MPZ_SRCPTR_SWAP (a
, c
);
83 sign
= (asize
^ csize
);
89 return mpn_divisible_p (ap
, asize
, dp
, dsize
);
98 /* Check a==c mod low zero bits of dlow. This might catch a few cases of
99 a!=c quickly, and it helps the csize==1 special cases below. */
100 dmask
= LOW_ZEROS_MASK (dlow
) & GMP_NUMB_MASK
;
101 alow
= (sign
>= 0 ? alow
: -alow
);
102 if (((alow
-clow
) & dmask
) != 0)
111 NEG_MOD (clow
, clow
, dlow
);
113 if (ABOVE_THRESHOLD (asize
, BMOD_1_TO_MOD_1_THRESHOLD
))
115 r
= mpn_mod_1 (ap
, asize
, dlow
);
119 return r
== (clow
% dlow
);
124 /* Strip low zero bits to get odd d required by modexact. If
125 d==e*2^n then a==c mod d if and only if both a==c mod e and
126 a==c mod 2^n, the latter having been done above. */
128 count_trailing_zeros (twos
, dlow
);
132 r
= mpn_modexact_1c_odd (ap
, asize
, dlow
, clow
);
133 return r
== 0 || r
== dlow
;
136 /* dlow==0 is avoided since we don't want to bother handling extra low
137 zero bits if dsecond is even (would involve borrow if a,c differ in
138 sign and alow,clow!=0). */
139 if (dsize
== 2 && dlow
!= 0)
141 mp_limb_t dsecond
= dp
[1];
143 if (dsecond
<= dmask
)
146 count_trailing_zeros (twos
, dlow
);
147 dlow
= (dlow
>> twos
) | (dsecond
<< (GMP_NUMB_BITS
-twos
));
150 /* dlow will be odd here, so the test for it even under cong_1
151 is unnecessary, but the rest of that code is wanted. */
158 xp
= TMP_ALLOC_LIMBS (asize
+1);
160 /* calculate abs(a-c) */
163 /* same signs, subtract */
164 if (asize
> csize
|| mpn_cmp (ap
, cp
, asize
) >= 0)
165 ASSERT_NOCARRY (mpn_sub (xp
, ap
, asize
, cp
, csize
));
167 ASSERT_NOCARRY (mpn_sub_n (xp
, cp
, ap
, asize
));
168 MPN_NORMALIZE (xp
, asize
);
172 /* different signs, add */
174 carry
= mpn_add (xp
, ap
, asize
, cp
, csize
);
176 asize
+= (carry
!= 0);
179 result
= mpn_divisible_p (xp
, asize
, dp
, dsize
);