1
/* mpz_congruent_2exp_p -- test congruence of mpz mod 2^n */
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
mpz_congruent_2exp_p (mpz_srcptr a, mpz_srcptr c, unsigned long d)
31
unsigned long i, dlimbs, dbits;
33
mp_limb_t dmask, alimb, climb, sum;
34
int asize_signed, csize_signed, asize, csize;
36
if (ABSIZ(a) < ABSIZ(c))
37
MPZ_SRCPTR_SWAP (a, c);
39
dlimbs = d / BITS_PER_MP_LIMB;
40
dbits = d % BITS_PER_MP_LIMB;
41
dmask = (CNST_LIMB(1) << dbits) - 1;
46
asize_signed = SIZ(a);
47
asize = ABS(asize_signed);
49
csize_signed = SIZ(c);
50
csize = ABS(csize_signed);
52
if (csize_signed == 0)
55
if ((asize_signed ^ csize_signed) >= 0)
57
/* same signs, direct comparison */
59
/* a==c for limbs in common */
60
if (mpn_cmp (ap, cp, MIN (csize, dlimbs)) != 0)
63
/* if that's all of dlimbs, then a==c for remaining bits */
65
return ((ap[dlimbs]-cp[dlimbs]) & dmask) == 0;
68
/* a remains, need all zero bits */
70
/* if d covers all of a and c, then must be exactly equal */
72
return asize == csize;
74
/* whole limbs zero */
75
for (i = csize; i < dlimbs; i++)
79
/* partial limb zero */
80
return (ap[dlimbs] & dmask) == 0;
84
/* different signs, negated comparison */
86
/* common low zero limbs, stopping at first non-zeros, which must
87
match twos complement */
91
ASSERT (i < csize); /* always have a non-zero limb on c */
97
return (sum & dmask) == 0;
100
/* require both zero, or first non-zeros as twos-complements */
108
/* further limbs matching as ones-complement */
116
sum = alimb + climb + 1;
119
return (sum & dmask) == 0;
127
/* no more c, so require all 1 bits in a */
130
return 0; /* not enough a */
133
for ( ; i < dlimbs; i++)
137
/* if only whole limbs, no further fetches from a */
145
return ((ap[dlimbs]+1) & dmask) == 0;