Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/mpz/hamdist.c
Line
Count
Source
1
/* mpz_hamdist -- calculate hamming distance.
2
3
Copyright 1994, 1996, 2001, 2002, 2009-2011 Free Software Foundation, Inc.
4
5
This file is part of the GNU MP Library.
6
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it 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
14
or
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
20
or both in parallel, as here.
21
22
The GNU MP Library is distributed in the hope that it will be useful, but
23
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
for more details.
26
27
You should have received copies of the GNU General Public License and the
28
GNU Lesser General Public License along with the GNU MP Library.  If not,
29
see https://www.gnu.org/licenses/.  */
30
31
#include "gmp-impl.h"
32
33
34
mp_bitcnt_t
35
mpz_hamdist (mpz_srcptr u, mpz_srcptr v) __GMP_NOTHROW
36
36
{
37
36
  mp_srcptr      up, vp;
38
36
  mp_size_t      usize, vsize;
39
36
  mp_bitcnt_t    count;
40
41
36
  usize = SIZ(u);
42
36
  vsize = SIZ(v);
43
44
36
  up = PTR(u);
45
36
  vp = PTR(v);
46
47
36
  if (usize >= 0)
48
19
    {
49
19
      if (vsize < 0)
50
1
  return ~ (mp_bitcnt_t) 0;
51
52
      /* positive/positive */
53
54
18
      if (usize < vsize)
55
4
  MPN_SRCPTR_SWAP (up,usize, vp,vsize);
56
57
18
      count = 0;
58
18
      if (vsize != 0)
59
14
  count = mpn_hamdist (up, vp, vsize);
60
61
18
      usize -= vsize;
62
18
      if (usize != 0)
63
4
  count += mpn_popcount (up + vsize, usize);
64
65
18
      return count;
66
19
    }
67
17
  else
68
17
    {
69
17
      mp_limb_t  ulimb, vlimb;
70
17
      mp_size_t  old_vsize, step;
71
72
17
      if (vsize >= 0)
73
2
  return ~ (mp_bitcnt_t) 0;
74
75
      /* negative/negative */
76
77
15
      usize = -usize;
78
15
      vsize = -vsize;
79
80
      /* skip common low zeros */
81
15
      for (;;)
82
38
  {
83
38
    ASSERT (usize > 0);
84
38
    ASSERT (vsize > 0);
85
86
38
    usize--;
87
38
    vsize--;
88
89
38
    ulimb = *up++;
90
38
    vlimb = *vp++;
91
92
38
    if (ulimb != 0)
93
12
      break;
94
95
26
    if (vlimb != 0)
96
3
      {
97
3
        MPN_SRCPTR_SWAP (up,usize, vp,vsize);
98
3
        ulimb = vlimb;
99
3
        vlimb = 0;
100
3
        break;
101
3
      }
102
26
  }
103
104
      /* twos complement first non-zero limbs (ulimb is non-zero, but vlimb
105
   might be zero) */
106
15
      ulimb = -ulimb;
107
15
      vlimb = -vlimb;
108
15
      popc_limb (count, (ulimb ^ vlimb) & GMP_NUMB_MASK);
109
110
15
      if (vlimb == 0)
111
11
  {
112
11
    mp_bitcnt_t  twoscount;
113
114
    /* first non-zero of v */
115
11
    old_vsize = vsize;
116
11
    do
117
72
      {
118
72
        ASSERT (vsize > 0);
119
72
        vsize--;
120
72
        vlimb = *vp++;
121
72
      }
122
72
    while (vlimb == 0);
123
124
    /* part of u corresponding to skipped v zeros */
125
11
    step = old_vsize - vsize - 1;
126
11
    count += step * GMP_NUMB_BITS;
127
11
    step = MIN (step, usize);
128
11
    if (step != 0)
129
5
      {
130
5
        count -= mpn_popcount (up, step);
131
5
        usize -= step;
132
5
        up += step;
133
5
      }
134
135
    /* First non-zero vlimb as twos complement, xor with ones
136
       complement ulimb.  Note -v^(~0^u) == (v-1)^u. */
137
11
    vlimb--;
138
11
    if (usize != 0)
139
5
      {
140
5
        usize--;
141
5
        vlimb ^= *up++;
142
5
      }
143
11
    popc_limb (twoscount, vlimb);
144
11
    count += twoscount;
145
11
  }
146
147
      /* Overlapping part of u and v, if any.  Ones complement both, so just
148
   plain hamdist. */
149
15
      step = MIN (usize, vsize);
150
15
      if (step != 0)
151
7
  {
152
7
    count += mpn_hamdist (up, vp, step);
153
7
    usize -= step;
154
7
    vsize -= step;
155
7
    up += step;
156
7
    vp += step;
157
7
  }
158
159
      /* Remaining high part of u or v, if any, ones complement but xor
160
   against all ones in the other, so plain popcount. */
161
15
      if (usize != 0)
162
4
  {
163
14
  remaining:
164
14
    count += mpn_popcount (up, usize);
165
14
  }
166
11
      else if (vsize != 0)
167
10
  {
168
10
    up = vp;
169
10
    usize = vsize;
170
10
    goto remaining;
171
10
  }
172
15
      return count;
173
15
    }
174
36
}