1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/usr/diag/prdf/common/util/prdfBitKey.C $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2012,2017 */
/* [+] International Business Machines Corp. */
/* */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
/* implied. See the License for the specific language governing */
/* permissions and limitations under the License. */
/* */
/* IBM_PROLOG_END_TAG */
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#define BitKey_C
//#include <prdfAssert.h>
#include <prdfBitKey.H>
#include <prdfBitString.H>
#include <string.h>
#undef BitKey_C
namespace PRDF
{
//------------------------------------------------------------------------------
// Local
//------------------------------------------------------------------------------
// # of bit32's needed for this bit_count
inline uint32_t getWordSize(uint32_t bitCount)
{
return (bitCount/32) + ((bitCount%32)? 1:0);
}
//------------------------------------------------------------------------------
// member function definitions
//------------------------------------------------------------------------------
BitKey::BitKey(void)
: iv_Capacity(0), iv_storage1(0)
{
iv_rep.storage2 = 0;
}
//------------------------------------------------------------------------------
BitKey::BitKey(uint32_t i_bitPos)
: iv_Capacity(0), iv_storage1(0)
{
iv_rep.storage2 = 0;
setBit(i_bitPos);
}
//------------------------------------------------------------------------------
BitKey::BitKey(const uint8_t * i_array,uint8_t i_size)
: iv_Capacity(0), iv_storage1(0)
{
iv_rep.storage2 = 0;
while(i_size)
{
setBit(*i_array);
--i_size;
++i_array;
}
}
//------------------------------------------------------------------------------
BitKey::BitKey( const std::vector <uint8_t> & i_bitList )
: iv_Capacity(0), iv_storage1(0)
{
iv_rep.storage2 = 0;
std::vector< uint8_t >::const_iterator itList = i_bitList.begin();
while( itList != i_bitList.end() )
{
setBit( *itList );
itList++;
}
}
//------------------------------------------------------------------------------
BitKey::BitKey(const char * i_ble)
: iv_Capacity(0), iv_storage1(0)
{
iv_rep.storage2 = 0;
while(*i_ble != 0)
{
setBit((*i_ble) - 1);
++i_ble;
}
}
//------------------------------------------------------------------------------
BitKey::~BitKey(void)
{
if(!IsDirect()) delete [] iv_rep.buffer;
}
//------------------------------------------------------------------------------
BitKey::BitKey (const BitKey & bit_list)
: iv_Capacity(bit_list.iv_Capacity), iv_storage1(bit_list.iv_storage1)
{
if(IsDirect())
{
iv_rep.storage2 = bit_list.iv_rep.storage2;
}
else
{
uint32_t size = getWordSize(iv_Capacity);
iv_rep.buffer = new uint32_t[size];
memcpy(iv_rep.buffer,bit_list.iv_rep.buffer,4*size);
}
}
//------------------------------------------------------------------------------
BitKey & BitKey::operator=(const BitKey & bit_list)
{
if(iv_Capacity)
{
BitString bs(iv_Capacity,DataPtr());
bs.clearAll();
}
ReAllocate(bit_list.iv_Capacity);
if(IsDirect()) // implies bit_list is also direct
{
iv_storage1 = bit_list.iv_storage1;
iv_rep.storage2 = bit_list.iv_rep.storage2;
}
else
{
const uint32_t * dataPtr = NULL;
if(bit_list.IsDirect())
{
dataPtr = &bit_list.iv_storage1;
} else
{
dataPtr = bit_list.iv_rep.buffer;
}
memcpy(iv_rep.buffer,dataPtr,4*getWordSize(bit_list.iv_Capacity));
}
return(*this);
}
//------------------------------------------------------------------------------
BitKey & BitKey::operator=(const BitString & bit_string)
{
if(iv_Capacity)
{
BitString bs(iv_Capacity,DataPtr());
bs.clearAll();
}
ReAllocate(bit_string.getBitLen());
BitString dbs(iv_Capacity,DataPtr());
dbs.setString(bit_string);
return(*this);
}
//------------------------------------------------------------------------------
BitKey & BitKey::operator=(const char * string_ptr)
{
if(iv_Capacity)
{
BitString bs(iv_Capacity,DataPtr());
bs.clearAll();
}
while(*string_ptr != '\0')
{
uint32_t bit_position = (uint32_t) ((*string_ptr) - 1);
setBit(bit_position);
++string_ptr;
}
return(*this);
}
//------------------------------------------------------------------------------
bool BitKey::operator==(const BitKey & that) const
{
bool result = true;
const uint32_t * mydata = cDataPtr();
const uint32_t * yodata = that.cDataPtr();
uint32_t mysize = getWordSize(iv_Capacity);
uint32_t yosize = getWordSize(that.iv_Capacity);
uint32_t smsize = (yosize < mysize)? yosize : mysize;
// If size is different than the extra must be zero
for(uint32_t i = 0; (i < smsize) && (result == true); ++i,++mydata,++yodata)
{
result = (*mydata == *yodata);
}
if(result && (yosize > mysize))
{
for(yosize -= mysize; yosize != 0 && result; --yosize, ++yodata)
{
result = *yodata == 0x00000000;
}
}
else if (result && (mysize > yosize))
{
for(mysize -= yosize; mysize != 0 && result; --mysize, ++mydata)
{
result = *mydata == 0x00000000;
}
}
return result;
}
//------------------------------------------------------------------------------
// Candidate funciton for bs class
bool BitKey::isSubset(const BitKey & that) const
{
bool result = true;
const uint32_t * mydata = cDataPtr();
const uint32_t * yodata = that.cDataPtr();
uint32_t mysize = getWordSize(iv_Capacity);
uint32_t yosize = getWordSize(that.iv_Capacity);
uint32_t smsize = (yosize < mysize)? yosize : mysize;
// size can be non-zero with no bits on - so if that has no bits than use
// operator==
BitKey zero;
// only true if both are empty - eg not bits on"
if(that == zero) result = operator==(that);
// if yosize <= mysize than just match smallest amount of data
// if yozize > mysize than extra yodata must be zero
for(uint32_t i = 0; (i < smsize) && (result == true); ++i,++mydata,++yodata)
{
result = (*mydata & *yodata) == *yodata;
}
if(result && (yosize > mysize))
{
for(yosize -= mysize; yosize != 0 && result; --yosize, ++yodata)
{
result = *yodata == 0x00000000;
}
}
return result;
}
//------------------------------------------------------------------------------
// get bit position of nth bit that is set
uint32_t BitKey::getListValue(uint32_t n) const
{
++n;
uint32_t setCount = 0;
uint32_t bitPos = 0xffffffff;
BitString bs(iv_Capacity,(CPU_WORD *)cDataPtr());
for(uint32_t i = 0; i < iv_Capacity; ++i)
{
if(bs.IsSet(i)) ++setCount;
if(setCount == n)
{
bitPos = i;
break;
}
}
return bitPos;
}
//------------------------------------------------------------------------------
uint32_t BitKey::size(void) const
{
const BitString bs(iv_Capacity,(CPU_WORD *)cDataPtr());
return bs.GetSetCount();
}
//------------------------------------------------------------------------------
void BitKey::removeBit(uint32_t n)
{
if(n < size())
{
BitString bs(iv_Capacity,DataPtr());
bs.Clear(getListValue(n));
}
}
//------------------------------------------------------------------------------
void BitKey::removeBit(void)
{
BitString bs(iv_Capacity,DataPtr());
uint32_t i = iv_Capacity;
while(i != 0)
{
--i;
if(bs.IsSet(i))
{
bs.Clear(i);
break;
}
}
}
//------------------------------------------------------------------------------
void BitKey::removeBits(const BitKey & i_bk)
{
BitString mybs(iv_Capacity,(CPU_WORD *)DataPtr());
const BitString yobs(i_bk.iv_Capacity,(CPU_WORD *)i_bk.cDataPtr());
mybs.maskString(yobs);
}
//------------------------------------------------------------------------------
void BitKey::setBit(uint32_t i_bitValue)
{
if(i_bitValue >= iv_Capacity)
{
ReAllocate(i_bitValue+1);
}
BitString bs(iv_Capacity,DataPtr());
bs.Set(i_bitValue);
}
//------------------------------------------------------------------------------
void BitKey::ReAllocate(uint32_t i_len)
{
if(i_len > iv_Capacity) // never shrink
{
bool wasDirect = IsDirect();
uint32_t oldSize = iv_Capacity;
uint32_t * oldPtr = DataPtr();
uint32_t wordsize = getWordSize(i_len);
iv_Capacity = 32*wordsize;
bool isDirect = IsDirect();
if(!isDirect) // to indirect
{
uint32_t * newBuffer = new uint32_t[wordsize];
BitString dbs(iv_Capacity,newBuffer);
dbs.clearAll();
BitString sbs(oldSize,oldPtr);
if ( !sbs.IsZero() ) dbs.setString(sbs);
iv_storage1 = 0;
if(!wasDirect) // from indirect
{
delete [] iv_rep.buffer;
}
iv_rep.buffer = newBuffer;
}
}
}
} // end namespace PRDF
|