1 /* mpz_congruent_2exp_p -- test congruence of mpz mod 2^n.
3 Copyright 2001, 2002 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 the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
25 mpz_congruent_2exp_p (mpz_srcptr a, mpz_srcptr c, mp_bitcnt_t d)
30 mp_limb_t dmask, alimb, climb, sum;
31 mp_size_t asize_signed, csize_signed, asize, csize;
33 if (ABSIZ(a) < ABSIZ(c))
34 MPZ_SRCPTR_SWAP (a, c);
36 dlimbs = d / GMP_NUMB_BITS;
37 dbits = d % GMP_NUMB_BITS;
38 dmask = (CNST_LIMB(1) << dbits) - 1;
43 asize_signed = SIZ(a);
44 asize = ABS(asize_signed);
46 csize_signed = SIZ(c);
47 csize = ABS(csize_signed);
49 if (csize_signed == 0)
52 if ((asize_signed ^ csize_signed) >= 0)
54 /* same signs, direct comparison */
56 /* a==c for limbs in common */
57 if (mpn_cmp (ap, cp, MIN (csize, dlimbs)) != 0)
60 /* if that's all of dlimbs, then a==c for remaining bits */
62 return ((ap[dlimbs]-cp[dlimbs]) & dmask) == 0;
65 /* a remains, need all zero bits */
67 /* if d covers all of a and c, then must be exactly equal */
69 return asize == csize;
71 /* whole limbs zero */
72 for (i = csize; i < dlimbs; i++)
76 /* partial limb zero */
77 return (ap[dlimbs] & dmask) == 0;
81 /* different signs, negated comparison */
83 /* common low zero limbs, stopping at first non-zeros, which must
84 match twos complement */
88 ASSERT (i < csize); /* always have a non-zero limb on c */
91 sum = (alimb + climb) & GMP_NUMB_MASK;
94 return (sum & dmask) == 0;
97 /* require both zero, or first non-zeros as twos-complements */
105 /* further limbs matching as ones-complement */
113 sum = (alimb + climb + 1) & GMP_NUMB_MASK;
116 return (sum & dmask) == 0;
124 /* no more c, so require all 1 bits in a */
127 return 0; /* not enough a */
130 for ( ; i < dlimbs; i++)
131 if (ap[i] != GMP_NUMB_MAX)
134 /* if only whole limbs, no further fetches from a */
142 return ((ap[dlimbs]+1) & dmask) == 0;