/src/gmp-6.2.1/mpz/hamdist.c
Line  | Count  | Source (jump to first uncovered line)  | 
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  | 0  | { | 
37  | 0  |   mp_srcptr      up, vp;  | 
38  | 0  |   mp_size_t      usize, vsize;  | 
39  | 0  |   mp_bitcnt_t    count;  | 
40  |  | 
  | 
41  | 0  |   usize = SIZ(u);  | 
42  | 0  |   vsize = SIZ(v);  | 
43  |  | 
  | 
44  | 0  |   up = PTR(u);  | 
45  | 0  |   vp = PTR(v);  | 
46  |  | 
  | 
47  | 0  |   if (usize >= 0)  | 
48  | 0  |     { | 
49  | 0  |       if (vsize < 0)  | 
50  | 0  |   return ~ (mp_bitcnt_t) 0;  | 
51  |  |  | 
52  |  |       /* positive/positive */  | 
53  |  |  | 
54  | 0  |       if (usize < vsize)  | 
55  | 0  |   MPN_SRCPTR_SWAP (up,usize, vp,vsize);  | 
56  |  | 
  | 
57  | 0  |       count = 0;  | 
58  | 0  |       if (vsize != 0)  | 
59  | 0  |   count = mpn_hamdist (up, vp, vsize);  | 
60  |  | 
  | 
61  | 0  |       usize -= vsize;  | 
62  | 0  |       if (usize != 0)  | 
63  | 0  |   count += mpn_popcount (up + vsize, usize);  | 
64  |  | 
  | 
65  | 0  |       return count;  | 
66  | 0  |     }  | 
67  | 0  |   else  | 
68  | 0  |     { | 
69  | 0  |       mp_limb_t  ulimb, vlimb;  | 
70  | 0  |       mp_size_t  old_vsize, step;  | 
71  |  | 
  | 
72  | 0  |       if (vsize >= 0)  | 
73  | 0  |   return ~ (mp_bitcnt_t) 0;  | 
74  |  |  | 
75  |  |       /* negative/negative */  | 
76  |  |  | 
77  | 0  |       usize = -usize;  | 
78  | 0  |       vsize = -vsize;  | 
79  |  |  | 
80  |  |       /* skip common low zeros */  | 
81  | 0  |       for (;;)  | 
82  | 0  |   { | 
83  | 0  |     ASSERT (usize > 0);  | 
84  | 0  |     ASSERT (vsize > 0);  | 
85  |  |  | 
86  | 0  |     usize--;  | 
87  | 0  |     vsize--;  | 
88  |  | 
  | 
89  | 0  |     ulimb = *up++;  | 
90  | 0  |     vlimb = *vp++;  | 
91  |  | 
  | 
92  | 0  |     if (ulimb != 0)  | 
93  | 0  |       break;  | 
94  |  |  | 
95  | 0  |     if (vlimb != 0)  | 
96  | 0  |       { | 
97  | 0  |         MPN_SRCPTR_SWAP (up,usize, vp,vsize);  | 
98  | 0  |         ulimb = vlimb;  | 
99  | 0  |         vlimb = 0;  | 
100  | 0  |         break;  | 
101  | 0  |       }  | 
102  | 0  |   }  | 
103  |  |  | 
104  |  |       /* twos complement first non-zero limbs (ulimb is non-zero, but vlimb  | 
105  |  |    might be zero) */  | 
106  | 0  |       ulimb = -ulimb;  | 
107  | 0  |       vlimb = -vlimb;  | 
108  | 0  |       popc_limb (count, (ulimb ^ vlimb) & GMP_NUMB_MASK);  | 
109  |  | 
  | 
110  | 0  |       if (vlimb == 0)  | 
111  | 0  |   { | 
112  | 0  |     mp_bitcnt_t  twoscount;  | 
113  |  |  | 
114  |  |     /* first non-zero of v */  | 
115  | 0  |     old_vsize = vsize;  | 
116  | 0  |     do  | 
117  | 0  |       { | 
118  | 0  |         ASSERT (vsize > 0);  | 
119  | 0  |         vsize--;  | 
120  | 0  |         vlimb = *vp++;  | 
121  | 0  |       }  | 
122  | 0  |     while (vlimb == 0);  | 
123  |  |  | 
124  |  |     /* part of u corresponding to skipped v zeros */  | 
125  | 0  |     step = old_vsize - vsize - 1;  | 
126  | 0  |     count += step * GMP_NUMB_BITS;  | 
127  | 0  |     step = MIN (step, usize);  | 
128  | 0  |     if (step != 0)  | 
129  | 0  |       { | 
130  | 0  |         count -= mpn_popcount (up, step);  | 
131  | 0  |         usize -= step;  | 
132  | 0  |         up += step;  | 
133  | 0  |       }  | 
134  |  |  | 
135  |  |     /* First non-zero vlimb as twos complement, xor with ones  | 
136  |  |        complement ulimb.  Note -v^(~0^u) == (v-1)^u. */  | 
137  | 0  |     vlimb--;  | 
138  | 0  |     if (usize != 0)  | 
139  | 0  |       { | 
140  | 0  |         usize--;  | 
141  | 0  |         vlimb ^= *up++;  | 
142  | 0  |       }  | 
143  | 0  |     popc_limb (twoscount, vlimb);  | 
144  | 0  |     count += twoscount;  | 
145  | 0  |   }  | 
146  |  |  | 
147  |  |       /* Overlapping part of u and v, if any.  Ones complement both, so just  | 
148  |  |    plain hamdist. */  | 
149  | 0  |       step = MIN (usize, vsize);  | 
150  | 0  |       if (step != 0)  | 
151  | 0  |   { | 
152  | 0  |     count += mpn_hamdist (up, vp, step);  | 
153  | 0  |     usize -= step;  | 
154  | 0  |     vsize -= step;  | 
155  | 0  |     up += step;  | 
156  | 0  |     vp += step;  | 
157  | 0  |   }  | 
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  | 0  |       if (usize != 0)  | 
162  | 0  |   { | 
163  | 0  |   remaining:  | 
164  | 0  |     count += mpn_popcount (up, usize);  | 
165  | 0  |   }  | 
166  | 0  |       else if (vsize != 0)  | 
167  | 0  |   { | 
168  | 0  |     up = vp;  | 
169  | 0  |     usize = vsize;  | 
170  | 0  |     goto remaining;  | 
171  | 0  |   }  | 
172  | 0  |       return count;  | 
173  | 0  |     }  | 
174  | 0  | }  |