1*86d7f5d3SJohn Marino /* mpz_congruent_p -- test congruence of two mpz's.
2*86d7f5d3SJohn Marino
3*86d7f5d3SJohn Marino Copyright 2001, 2002, 2005 Free Software Foundation, Inc.
4*86d7f5d3SJohn Marino
5*86d7f5d3SJohn Marino This file is part of the GNU MP Library.
6*86d7f5d3SJohn Marino
7*86d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
8*86d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
9*86d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
10*86d7f5d3SJohn Marino option) any later version.
11*86d7f5d3SJohn Marino
12*86d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
13*86d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14*86d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15*86d7f5d3SJohn Marino License for more details.
16*86d7f5d3SJohn Marino
17*86d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
18*86d7f5d3SJohn Marino along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
19*86d7f5d3SJohn Marino
20*86d7f5d3SJohn Marino #include "gmp.h"
21*86d7f5d3SJohn Marino #include "gmp-impl.h"
22*86d7f5d3SJohn Marino #include "longlong.h"
23*86d7f5d3SJohn Marino
24*86d7f5d3SJohn Marino
25*86d7f5d3SJohn Marino /* For big divisors this code is only very slightly better than the user
26*86d7f5d3SJohn Marino doing a combination of mpz_sub and mpz_tdiv_r, but it's quite convenient,
27*86d7f5d3SJohn Marino and perhaps in the future can be improved, in similar ways to
28*86d7f5d3SJohn Marino mpn_divisible_p perhaps.
29*86d7f5d3SJohn Marino
30*86d7f5d3SJohn Marino The csize==1 / dsize==1 special case makes mpz_congruent_p as good as
31*86d7f5d3SJohn Marino mpz_congruent_ui_p on relevant operands, though such a combination
32*86d7f5d3SJohn Marino probably doesn't occur often.
33*86d7f5d3SJohn Marino
34*86d7f5d3SJohn Marino Alternatives:
35*86d7f5d3SJohn Marino
36*86d7f5d3SJohn Marino If c<d then it'd work to just form a%d and compare a and c (either as
37*86d7f5d3SJohn Marino a==c or a+c==d depending on the signs), but the saving from avoiding the
38*86d7f5d3SJohn Marino abs(a-c) calculation would be small compared to the division.
39*86d7f5d3SJohn Marino
40*86d7f5d3SJohn Marino Similarly if both a<d and c<d then it would work to just compare a and c
41*86d7f5d3SJohn Marino (a==c or a+c==d), but this isn't considered a particularly important case
42*86d7f5d3SJohn Marino and so isn't done for the moment.
43*86d7f5d3SJohn Marino
44*86d7f5d3SJohn Marino Low zero limbs on d could be stripped and the corresponding limbs of a
45*86d7f5d3SJohn Marino and c tested and skipped, but doing so would introduce a borrow when a
46*86d7f5d3SJohn Marino and c differ in sign and have non-zero skipped limbs. It doesn't seem
47*86d7f5d3SJohn Marino worth the complications to do this, since low zero limbs on d should
48*86d7f5d3SJohn Marino occur only rarely. */
49*86d7f5d3SJohn Marino
50*86d7f5d3SJohn Marino int
mpz_congruent_p(mpz_srcptr a,mpz_srcptr c,mpz_srcptr d)51*86d7f5d3SJohn Marino mpz_congruent_p (mpz_srcptr a, mpz_srcptr c, mpz_srcptr d)
52*86d7f5d3SJohn Marino {
53*86d7f5d3SJohn Marino mp_size_t asize, csize, dsize, sign;
54*86d7f5d3SJohn Marino mp_srcptr ap, cp, dp;
55*86d7f5d3SJohn Marino mp_ptr xp;
56*86d7f5d3SJohn Marino mp_limb_t alow, clow, dlow, dmask, r;
57*86d7f5d3SJohn Marino int result;
58*86d7f5d3SJohn Marino TMP_DECL;
59*86d7f5d3SJohn Marino
60*86d7f5d3SJohn Marino dsize = SIZ(d);
61*86d7f5d3SJohn Marino if (UNLIKELY (dsize == 0))
62*86d7f5d3SJohn Marino return (mpz_cmp (a, c) == 0);
63*86d7f5d3SJohn Marino
64*86d7f5d3SJohn Marino dsize = ABS(dsize);
65*86d7f5d3SJohn Marino dp = PTR(d);
66*86d7f5d3SJohn Marino
67*86d7f5d3SJohn Marino if (ABSIZ(a) < ABSIZ(c))
68*86d7f5d3SJohn Marino MPZ_SRCPTR_SWAP (a, c);
69*86d7f5d3SJohn Marino
70*86d7f5d3SJohn Marino asize = SIZ(a);
71*86d7f5d3SJohn Marino csize = SIZ(c);
72*86d7f5d3SJohn Marino sign = (asize ^ csize);
73*86d7f5d3SJohn Marino
74*86d7f5d3SJohn Marino asize = ABS(asize);
75*86d7f5d3SJohn Marino ap = PTR(a);
76*86d7f5d3SJohn Marino
77*86d7f5d3SJohn Marino if (csize == 0)
78*86d7f5d3SJohn Marino return mpn_divisible_p (ap, asize, dp, dsize);
79*86d7f5d3SJohn Marino
80*86d7f5d3SJohn Marino csize = ABS(csize);
81*86d7f5d3SJohn Marino cp = PTR(c);
82*86d7f5d3SJohn Marino
83*86d7f5d3SJohn Marino alow = ap[0];
84*86d7f5d3SJohn Marino clow = cp[0];
85*86d7f5d3SJohn Marino dlow = dp[0];
86*86d7f5d3SJohn Marino
87*86d7f5d3SJohn Marino /* Check a==c mod low zero bits of dlow. This might catch a few cases of
88*86d7f5d3SJohn Marino a!=c quickly, and it helps the csize==1 special cases below. */
89*86d7f5d3SJohn Marino dmask = LOW_ZEROS_MASK (dlow) & GMP_NUMB_MASK;
90*86d7f5d3SJohn Marino alow = (sign >= 0 ? alow : -alow);
91*86d7f5d3SJohn Marino if (((alow-clow) & dmask) != 0)
92*86d7f5d3SJohn Marino return 0;
93*86d7f5d3SJohn Marino
94*86d7f5d3SJohn Marino if (csize == 1)
95*86d7f5d3SJohn Marino {
96*86d7f5d3SJohn Marino if (dsize == 1)
97*86d7f5d3SJohn Marino {
98*86d7f5d3SJohn Marino cong_1:
99*86d7f5d3SJohn Marino if (sign < 0)
100*86d7f5d3SJohn Marino NEG_MOD (clow, clow, dlow);
101*86d7f5d3SJohn Marino
102*86d7f5d3SJohn Marino if (ABOVE_THRESHOLD (asize, BMOD_1_TO_MOD_1_THRESHOLD))
103*86d7f5d3SJohn Marino {
104*86d7f5d3SJohn Marino r = mpn_mod_1 (ap, asize, dlow);
105*86d7f5d3SJohn Marino if (clow < dlow)
106*86d7f5d3SJohn Marino return r == clow;
107*86d7f5d3SJohn Marino else
108*86d7f5d3SJohn Marino return r == (clow % dlow);
109*86d7f5d3SJohn Marino }
110*86d7f5d3SJohn Marino
111*86d7f5d3SJohn Marino if ((dlow & 1) == 0)
112*86d7f5d3SJohn Marino {
113*86d7f5d3SJohn Marino /* Strip low zero bits to get odd d required by modexact. If
114*86d7f5d3SJohn Marino d==e*2^n then a==c mod d if and only if both a==c mod e and
115*86d7f5d3SJohn Marino a==c mod 2^n, the latter having been done above. */
116*86d7f5d3SJohn Marino unsigned twos;
117*86d7f5d3SJohn Marino count_trailing_zeros (twos, dlow);
118*86d7f5d3SJohn Marino dlow >>= twos;
119*86d7f5d3SJohn Marino }
120*86d7f5d3SJohn Marino
121*86d7f5d3SJohn Marino r = mpn_modexact_1c_odd (ap, asize, dlow, clow);
122*86d7f5d3SJohn Marino return r == 0 || r == dlow;
123*86d7f5d3SJohn Marino }
124*86d7f5d3SJohn Marino
125*86d7f5d3SJohn Marino /* dlow==0 is avoided since we don't want to bother handling extra low
126*86d7f5d3SJohn Marino zero bits if dsecond is even (would involve borrow if a,c differ in
127*86d7f5d3SJohn Marino sign and alow,clow!=0). */
128*86d7f5d3SJohn Marino if (dsize == 2 && dlow != 0)
129*86d7f5d3SJohn Marino {
130*86d7f5d3SJohn Marino mp_limb_t dsecond = dp[1];
131*86d7f5d3SJohn Marino
132*86d7f5d3SJohn Marino if (dsecond <= dmask)
133*86d7f5d3SJohn Marino {
134*86d7f5d3SJohn Marino unsigned twos;
135*86d7f5d3SJohn Marino count_trailing_zeros (twos, dlow);
136*86d7f5d3SJohn Marino dlow = (dlow >> twos) | (dsecond << (GMP_NUMB_BITS-twos));
137*86d7f5d3SJohn Marino ASSERT_LIMB (dlow);
138*86d7f5d3SJohn Marino
139*86d7f5d3SJohn Marino /* dlow will be odd here, so the test for it even under cong_1
140*86d7f5d3SJohn Marino is unnecessary, but the rest of that code is wanted. */
141*86d7f5d3SJohn Marino goto cong_1;
142*86d7f5d3SJohn Marino }
143*86d7f5d3SJohn Marino }
144*86d7f5d3SJohn Marino }
145*86d7f5d3SJohn Marino
146*86d7f5d3SJohn Marino TMP_MARK;
147*86d7f5d3SJohn Marino xp = TMP_ALLOC_LIMBS (asize+1);
148*86d7f5d3SJohn Marino
149*86d7f5d3SJohn Marino /* calculate abs(a-c) */
150*86d7f5d3SJohn Marino if (sign >= 0)
151*86d7f5d3SJohn Marino {
152*86d7f5d3SJohn Marino /* same signs, subtract */
153*86d7f5d3SJohn Marino if (asize > csize || mpn_cmp (ap, cp, asize) >= 0)
154*86d7f5d3SJohn Marino ASSERT_NOCARRY (mpn_sub (xp, ap, asize, cp, csize));
155*86d7f5d3SJohn Marino else
156*86d7f5d3SJohn Marino ASSERT_NOCARRY (mpn_sub_n (xp, cp, ap, asize));
157*86d7f5d3SJohn Marino MPN_NORMALIZE (xp, asize);
158*86d7f5d3SJohn Marino }
159*86d7f5d3SJohn Marino else
160*86d7f5d3SJohn Marino {
161*86d7f5d3SJohn Marino /* different signs, add */
162*86d7f5d3SJohn Marino mp_limb_t carry;
163*86d7f5d3SJohn Marino carry = mpn_add (xp, ap, asize, cp, csize);
164*86d7f5d3SJohn Marino xp[asize] = carry;
165*86d7f5d3SJohn Marino asize += (carry != 0);
166*86d7f5d3SJohn Marino }
167*86d7f5d3SJohn Marino
168*86d7f5d3SJohn Marino result = mpn_divisible_p (xp, asize, dp, dsize);
169*86d7f5d3SJohn Marino
170*86d7f5d3SJohn Marino TMP_FREE;
171*86d7f5d3SJohn Marino return result;
172*86d7f5d3SJohn Marino }
173