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

« back to all changes in this revision

Viewing changes to gmp3/mpz/cong_2exp.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_2exp_p -- test congruence of mpz mod 2^n */
 
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
 
 
27
 
 
28
int
 
29
mpz_congruent_2exp_p (mpz_srcptr a, mpz_srcptr c, unsigned long d)
 
30
{
 
31
  unsigned long  i, dlimbs, dbits;
 
32
  mp_ptr         ap, cp;
 
33
  mp_limb_t      dmask, alimb, climb, sum;
 
34
  int            asize_signed, csize_signed, asize, csize;
 
35
 
 
36
  if (ABSIZ(a) < ABSIZ(c))
 
37
    MPZ_SRCPTR_SWAP (a, c);
 
38
 
 
39
  dlimbs = d / BITS_PER_MP_LIMB;
 
40
  dbits = d % BITS_PER_MP_LIMB;
 
41
  dmask = (CNST_LIMB(1) << dbits) - 1;
 
42
 
 
43
  ap = PTR(a);
 
44
  cp = PTR(c);
 
45
 
 
46
  asize_signed = SIZ(a);
 
47
  asize = ABS(asize_signed);
 
48
 
 
49
  csize_signed = SIZ(c);
 
50
  csize = ABS(csize_signed);
 
51
 
 
52
  if (csize_signed == 0)
 
53
    goto a_zeros;
 
54
 
 
55
  if ((asize_signed ^ csize_signed) >= 0)
 
56
    {
 
57
      /* same signs, direct comparison */
 
58
 
 
59
      /* a==c for limbs in common */
 
60
      if (mpn_cmp (ap, cp, MIN (csize, dlimbs)) != 0)
 
61
        return 0;
 
62
 
 
63
      /* if that's all of dlimbs, then a==c for remaining bits */
 
64
      if (csize > dlimbs)
 
65
        return ((ap[dlimbs]-cp[dlimbs]) & dmask) == 0;
 
66
 
 
67
    a_zeros:
 
68
      /* a remains, need all zero bits */
 
69
 
 
70
      /* if d covers all of a and c, then must be exactly equal */
 
71
      if (asize <= dlimbs)
 
72
        return asize == csize;
 
73
      
 
74
      /* whole limbs zero */
 
75
      for (i = csize; i < dlimbs; i++)
 
76
        if (ap[i] != 0)
 
77
          return 0;
 
78
      
 
79
      /* partial limb zero */
 
80
      return (ap[dlimbs] & dmask) == 0;
 
81
    }
 
82
  else
 
83
    {
 
84
      /* different signs, negated comparison */
 
85
 
 
86
      /* common low zero limbs, stopping at first non-zeros, which must
 
87
         match twos complement */
 
88
      i = 0;
 
89
      for (;;)
 
90
        {
 
91
          ASSERT (i < csize);  /* always have a non-zero limb on c */
 
92
          alimb = ap[i];
 
93
          climb = cp[i];
 
94
          sum = alimb + climb;
 
95
 
 
96
          if (i >= dlimbs)
 
97
            return (sum & dmask) == 0;
 
98
          i++;
 
99
 
 
100
          /* require both zero, or first non-zeros as twos-complements */
 
101
          if (sum != 0)
 
102
            return 0;
 
103
 
 
104
          if (alimb != 0)
 
105
            break;
 
106
        }
 
107
 
 
108
      /* further limbs matching as ones-complement */
 
109
      for (;;)
 
110
        {
 
111
          if (i >= csize)
 
112
            break;
 
113
 
 
114
          alimb = ap[i];
 
115
          climb = cp[i];
 
116
          sum = alimb + climb + 1;
 
117
 
 
118
          if (i >= dlimbs)
 
119
            return (sum & dmask) == 0;
 
120
 
 
121
          if (sum != 0)
 
122
            return 0;
 
123
 
 
124
          i++;
 
125
        }
 
126
 
 
127
      /* no more c, so require all 1 bits in a */
 
128
 
 
129
      if (asize < dlimbs)
 
130
        return 0;   /* not enough a */
 
131
 
 
132
      /* whole limbs */
 
133
      for ( ; i < dlimbs; i++)
 
134
        if (ap[i] + 1 != 0)
 
135
          return 0;
 
136
 
 
137
      /* if only whole limbs, no further fetches from a */
 
138
      if (dbits == 0)
 
139
        return 1;
 
140
 
 
141
      /* need enough a */
 
142
      if (asize == dlimbs)
 
143
        return 0;
 
144
 
 
145
      return ((ap[dlimbs]+1) & dmask) == 0;
 
146
    }
 
147
}