1/* mpz_congruent_ui_p -- test congruence of mpz and ulong.
2
3Copyright 2000-2002, 2012 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of either:
9
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.
13
14or
15
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
18    later version.
19
20or both in parallel, as here.
21
22The GNU MP Library is distributed in the hope that it will be useful, but
23WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25for more details.
26
27You should have received copies of the GNU General Public License and the
28GNU Lesser General Public License along with the GNU MP Library.  If not,
29see https://www.gnu.org/licenses/.  */
30
31#include "gmp-impl.h"
32#include "longlong.h"
33
34
35/* There's some explicit checks for c<d since it seems reasonably likely an
36   application might use that in a test.
37
38   Hopefully the compiler can generate something good for r==(c%d), though
39   if modexact is being used exclusively then that's not reached.  */
40
41int
42mpz_congruent_ui_p (mpz_srcptr a, unsigned long cu, unsigned long du)
43{
44  mp_srcptr  ap;
45  mp_size_t  asize;
46  mp_limb_t  c, d, r;
47
48  if (UNLIKELY (du == 0))
49    return (mpz_cmp_ui (a, cu) == 0);
50
51  asize = SIZ(a);
52  if (asize == 0)
53    {
54      if (cu < du)
55	return cu == 0;
56      else
57	return (cu % du) == 0;
58    }
59
60  /* For nails don't try to be clever if c or d is bigger than a limb, just
61     fake up some mpz_t's and go to the main mpz_congruent_p.  */
62  if (du > GMP_NUMB_MAX || cu > GMP_NUMB_MAX)
63    {
64      mp_limb_t  climbs[2], dlimbs[2];
65      mpz_t      cz, dz;
66
67      ALLOC(cz) = 2;
68      PTR(cz) = climbs;
69      ALLOC(dz) = 2;
70      PTR(dz) = dlimbs;
71
72      mpz_set_ui (cz, cu);
73      mpz_set_ui (dz, du);
74      return mpz_congruent_p (a, cz, dz);
75    }
76
77  /* NEG_MOD works on limbs, so convert ulong to limb */
78  c = cu;
79  d = du;
80
81  if (asize < 0)
82    {
83      asize = -asize;
84      NEG_MOD (c, c, d);
85    }
86
87  ap = PTR (a);
88
89  if (ABOVE_THRESHOLD (asize, BMOD_1_TO_MOD_1_THRESHOLD))
90    {
91      r = mpn_mod_1 (ap, asize, d);
92      if (c < d)
93	return r == c;
94      else
95	return r == (c % d);
96    }
97
98  if ((d & 1) == 0)
99    {
100      /* Strip low zero bits to get odd d required by modexact.  If
101	 d==e*2^n then a==c mod d if and only if both a==c mod 2^n
102	 and a==c mod e.  */
103
104      unsigned	twos;
105
106      if ((ap[0]-c) & LOW_ZEROS_MASK (d))
107	return 0;
108
109      count_trailing_zeros (twos, d);
110      d >>= twos;
111    }
112
113  r = mpn_modexact_1c_odd (ap, asize, d, c);
114  return r == 0 || r == d;
115}
116