~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to gmp3/mpz/cong.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

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