blob: cf408cd1b92ba286fe493984e42495451e43afe9 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2008, 2009 Free Software Foundation, Inc.
3*
4*
5* This software is distributed under the terms of the GNU Affero Public License.
6* See the COPYING file in the main directory for details.
7*
8* This use of this software may be subject to additional restrictions.
9* See the LEGAL file in the main directory for details.
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Affero General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Affero General Public License for more details.
20
21 You should have received a copy of the GNU Affero General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24*/
25
26
27
28
29#include "BitVector.h"
30#include <iostream>
31#include <stdio.h>
kurtis.heimerl5a872472013-05-31 21:47:25 +000032#include <sstream>
Alexander Chemeris38b69872017-03-17 18:20:09 -070033#include <math.h>
dburgess82c46ff2011-10-07 02:40:51 +000034
35using namespace std;
36
37
38/**
39 Apply a Galois polymonial to a binary seqeunce.
40 @param val The input sequence.
41 @param poly The polynomial.
42 @param order The order of the polynomial.
43 @return Single-bit result.
44*/
45unsigned applyPoly(uint64_t val, uint64_t poly, unsigned order)
46{
47 uint64_t prod = val & poly;
48 unsigned sum = prod;
49 for (unsigned i=1; i<order; i++) sum ^= prod>>i;
50 return sum & 0x01;
51}
52
53
54
55
56
57
58BitVector::BitVector(const char *valString)
59 :Vector<char>(strlen(valString))
60{
61 uint32_t accum = 0;
62 for (size_t i=0; i<size(); i++) {
63 accum <<= 1;
64 if (valString[i]=='1') accum |= 0x01;
65 mStart[i] = accum;
66 }
67}
68
69
70
71
72
73uint64_t BitVector::peekField(size_t readIndex, unsigned length) const
74{
75 uint64_t accum = 0;
76 char *dp = mStart + readIndex;
77 assert(dp+length <= mEnd);
78 for (unsigned i=0; i<length; i++) {
79 accum = (accum<<1) | ((*dp++) & 0x01);
80 }
81 return accum;
82}
83
84
85
86
87uint64_t BitVector::peekFieldReversed(size_t readIndex, unsigned length) const
88{
89 uint64_t accum = 0;
90 char *dp = mStart + readIndex + length - 1;
91 assert(dp<mEnd);
92 for (int i=(length-1); i>=0; i--) {
93 accum = (accum<<1) | ((*dp--) & 0x01);
94 }
95 return accum;
96}
97
98
99
100
101uint64_t BitVector::readField(size_t& readIndex, unsigned length) const
102{
103 const uint64_t retVal = peekField(readIndex,length);
104 readIndex += length;
105 return retVal;
106}
107
108
109uint64_t BitVector::readFieldReversed(size_t& readIndex, unsigned length) const
110{
111 const uint64_t retVal = peekFieldReversed(readIndex,length);
112 readIndex += length;
113 return retVal;
114}
115
116
117
118
119
120void BitVector::fillField(size_t writeIndex, uint64_t value, unsigned length)
121{
122 char *dpBase = mStart + writeIndex;
123 char *dp = dpBase + length - 1;
124 assert(dp < mEnd);
125 while (dp>=dpBase) {
126 *dp-- = value & 0x01;
127 value >>= 1;
128 }
129}
130
131
132void BitVector::fillFieldReversed(size_t writeIndex, uint64_t value, unsigned length)
133{
134 char *dp = mStart + writeIndex;
135 char *dpEnd = dp + length - 1;
136 assert(dpEnd < mEnd);
137 while (dp<=dpEnd) {
138 *dp++ = value & 0x01;
139 value >>= 1;
140 }
141}
142
143
144
145
146void BitVector::writeField(size_t& writeIndex, uint64_t value, unsigned length)
147{
148 fillField(writeIndex,value,length);
149 writeIndex += length;
150}
151
152
153void BitVector::writeFieldReversed(size_t& writeIndex, uint64_t value, unsigned length)
154{
155 fillFieldReversed(writeIndex,value,length);
156 writeIndex += length;
157}
158
159
160void BitVector::invert()
161{
162 for (size_t i=0; i<size(); i++) {
163 mStart[i] = ~mStart[i];
164 }
165}
166
167
168
169
170void BitVector::reverse8()
171{
172 assert(size()>=8);
173
174 char tmp0 = mStart[0];
175 mStart[0] = mStart[7];
176 mStart[7] = tmp0;
177
178 char tmp1 = mStart[1];
179 mStart[1] = mStart[6];
180 mStart[6] = tmp1;
181
182 char tmp2 = mStart[2];
183 mStart[2] = mStart[5];
184 mStart[5] = tmp2;
185
186 char tmp3 = mStart[3];
187 mStart[3] = mStart[4];
188 mStart[4] = tmp3;
189}
190
191
192
193void BitVector::LSB8MSB()
194{
195 if (size()<8) return;
196 size_t size8 = 8*(size()/8);
197 size_t iTop = size8 - 8;
198 for (size_t i=0; i<=iTop; i+=8) segment(i,8).reverse8();
199}
200
201
202
dburgess82c46ff2011-10-07 02:40:51 +0000203unsigned BitVector::sum() const
204{
205 unsigned sum = 0;
206 for (size_t i=0; i<size(); i++) sum += mStart[i] & 0x01;
207 return sum;
208}
209
210
211
212
213void BitVector::map(const unsigned *map, size_t mapSize, BitVector& dest) const
214{
215 for (unsigned i=0; i<mapSize; i++) {
216 dest.mStart[i] = mStart[map[i]];
217 }
218}
219
220
221
222
223void BitVector::unmap(const unsigned *map, size_t mapSize, BitVector& dest) const
224{
225 for (unsigned i=0; i<mapSize; i++) {
226 dest.mStart[map[i]] = mStart[i];
227 }
228}
229
230
231
232
233
234
235
dburgess82c46ff2011-10-07 02:40:51 +0000236ostream& operator<<(ostream& os, const BitVector& hv)
237{
238 for (size_t i=0; i<hv.size(); i++) {
239 if (hv.bit(i)) os << '1';
240 else os << '0';
241 }
242 return os;
243}
244
245
246
247
dburgess82c46ff2011-10-07 02:40:51 +0000248SoftVector::SoftVector(const BitVector& source)
249{
250 resize(source.size());
251 for (size_t i=0; i<size(); i++) {
252 if (source.bit(i)) mStart[i]=1.0F;
Alexander Chemeris38b69872017-03-17 18:20:09 -0700253 else mStart[i]=-1.0F;
dburgess82c46ff2011-10-07 02:40:51 +0000254 }
255}
256
257
258BitVector SoftVector::sliced() const
259{
260 size_t sz = size();
261 BitVector newSig(sz);
262 for (size_t i=0; i<sz; i++) {
Alexander Chemeris38b69872017-03-17 18:20:09 -0700263 if (mStart[i]>0.0F) newSig[i]=1;
dburgess82c46ff2011-10-07 02:40:51 +0000264 else newSig[i] = 0;
265 }
266 return newSig;
267}
268
269
kurtis.heimerl5a872472013-05-31 21:47:25 +0000270float SoftVector::getEnergy(float *plow) const
271{
272 const SoftVector &vec = *this;
273 int len = vec.size();
274 float avg = 0; float low = 1;
275 for (int i = 0; i < len; i++) {
Alexander Chemeris38b69872017-03-17 18:20:09 -0700276 float energy = fabsf(vec[i]);
kurtis.heimerl5a872472013-05-31 21:47:25 +0000277 if (energy < low) low = energy;
278 avg += energy/len;
279 }
280 if (plow) { *plow = low; }
281 return avg;
282}
283
dburgess82c46ff2011-10-07 02:40:51 +0000284
285ostream& operator<<(ostream& os, const SoftVector& sv)
286{
287 for (size_t i=0; i<sv.size(); i++) {
Alexander Chemeris38b69872017-03-17 18:20:09 -0700288 if (sv[i]<-0.5) os << "0";
289 else if (sv[i]<-0.25) os << "o";
290 else if (sv[i]<0.0) os << ".";
291 else if (sv[i]>0.5) os << "1";
292 else if (sv[i]>0.25) os << "|";
293 else if (sv[i]>0.0) os << "'";
dburgess82c46ff2011-10-07 02:40:51 +0000294 else os << "-";
295 }
296 return os;
297}
298
299
300
301void BitVector::pack(unsigned char* targ) const
302{
303 // Assumes MSB-first packing.
304 unsigned bytes = size()/8;
305 for (unsigned i=0; i<bytes; i++) {
306 targ[i] = peekField(i*8,8);
307 }
308 unsigned whole = bytes*8;
309 unsigned rem = size() - whole;
310 if (rem==0) return;
311 targ[bytes] = peekField(whole,rem) << (8-rem);
312}
313
314
315void BitVector::unpack(const unsigned char* src)
316{
317 // Assumes MSB-first packing.
318 unsigned bytes = size()/8;
319 for (unsigned i=0; i<bytes; i++) {
320 fillField(i*8,src[i],8);
321 }
322 unsigned whole = bytes*8;
323 unsigned rem = size() - whole;
324 if (rem==0) return;
kurtis.heimerl7645fca2012-03-08 07:13:15 +0000325 fillField(whole,src[bytes] >> (8-rem),rem);
dburgess82c46ff2011-10-07 02:40:51 +0000326}
327
328void BitVector::hex(ostream& os) const
329{
330 os << std::hex;
331 unsigned digits = size()/4;
332 size_t wp=0;
333 for (unsigned i=0; i<digits; i++) {
334 os << readField(wp,4);
335 }
336 os << std::dec;
337}
338
kurtis.heimerl5a872472013-05-31 21:47:25 +0000339std::string BitVector::hexstr() const
340{
341 std::ostringstream ss;
342 hex(ss);
343 return ss.str();
344}
345
346
dburgess82c46ff2011-10-07 02:40:51 +0000347bool BitVector::unhex(const char* src)
348{
349 // Assumes MSB-first packing.
350 unsigned int val;
351 unsigned digits = size()/4;
352 for (unsigned i=0; i<digits; i++) {
353 if (sscanf(src+i, "%1x", &val) < 1) {
354 return false;
355 }
356 fillField(i*4,val,4);
357 }
358 unsigned whole = digits*4;
359 unsigned rem = size() - whole;
360 if (rem>0) {
361 if (sscanf(src+digits, "%1x", &val) < 1) {
362 return false;
363 }
364 fillField(whole,val,rem);
365 }
366 return true;
367}
368
369// vim: ts=4 sw=4