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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/usr/diag/prdf/common/util/prdfBitString.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 */
/** @file BitString.C
* @brief BitString and BitStringBuffer class Definitions
*/
#define PRDFBITSTRING_CPP
#include <prdfBitString.H>
#undef PRDFBITSTRING_CPP
#include <algorithm>
namespace PRDF
{
//##############################################################################
// BitString class
//##############################################################################
uint32_t BitString::GetSetCount(uint32_t bit_position,
uint32_t leng
) const
{
uint32_t end_position = bit_position + leng;
PRDF_ASSERT(end_position <= iv_bitLen);
uint32_t count = 0;
while(bit_position < end_position)
{
if(IsSet(bit_position))
{
count++;
}
bit_position++;
}
return(count);
}
// ------------------------------------------------------------------------------------------------
CPU_WORD BitString::GetField
(
uint32_t iBitPos,
uint32_t iLen
) const
{
PRDF_ASSERT((iBitPos + iLen) <= iv_bitLen);
PRDF_ASSERT(iLen <= CPU_WORD_BIT_LEN);
CPU_WORD value = 0; //dg02a
if(getBufAddr() != NULL) //dg02a
{ //dg02a
CPU_WORD * address = GetRelativePosition(iBitPos,iBitPos);
value = *address << iBitPos;
if(iBitPos + iLen > CPU_WORD_BIT_LEN) // we need the rest of the value
{
++address;
value |= *address >> (CPU_WORD_BIT_LEN - iBitPos);
}
if(iLen < CPU_WORD_BIT_LEN) // GNUC does not handle shift overflow as expected
{ // zero bits outside desired field
value &= ((((CPU_WORD) 1) << iLen) - 1) << (CPU_WORD_BIT_LEN - iLen);
}
} //dg02a
return(value);
}
// ------------------------------------------------------------------------------------------------
CPU_WORD BitString::GetFieldJustify
(
uint32_t bit_position,
uint32_t length
) const
{
CPU_WORD value = GetField(bit_position, length);
value = RIGHT_SHIFT(length, value);
return(value);
}
// ------------------------------------------------------------------------------------------------
void BitString::SetField
(
uint32_t bit_position,
uint32_t iLen,
CPU_WORD value
)
{
PRDF_ASSERT((bit_position + iLen) <= iv_bitLen);
PRDF_ASSERT(iLen <= CPU_WORD_BIT_LEN);
if(iv_bufAddr != NULL || value != 0) //dg02a
{ //dg02a
CPU_WORD * address = GetRelativePosition(bit_position,bit_position); // dg02c
CPU_WORD mask = CPU_WORD_MASK;
mask <<= (CPU_WORD_BIT_LEN - iLen);
value &= mask;
*address &= ~(mask >> bit_position); // clear field
*address |= value >> bit_position; // set field
if(bit_position + iLen > CPU_WORD_BIT_LEN) // we overflowed into the next CPU_WORD
{
address++;
*address &= ~(mask << (CPU_WORD_BIT_LEN - bit_position));
*address |= (value << (CPU_WORD_BIT_LEN - bit_position));
}
} //dg02a
}
// ------------------------------------------------------------------------------------------------
void BitString::SetFieldJustify
(
uint32_t bit_position,
uint32_t length,
CPU_WORD value
)
{
value = LEFT_SHIFT(length, value);
SetField(bit_position, length, value);
}
// ------------------------------------------------------------------------------------------------
void BitString::SetBits
(
const BitString & string, // source string
unsigned int iPos, // source start pos
unsigned int iLen, // length
unsigned int iDpos // dest start pos
)
{
const BitString * source = &string;
bool copyforward = true;
// How Much to really move
iLen = std::min(iLen,string.getBitLen() - iPos);
iLen = std::min(iLen,getBitLen() - iDpos);
// copy the right direction to prevent overlapping
uint32_t sRelativeOffset = 0;
uint32_t dRelativeOffset = 0;
CPU_WORD * sourceAddress = NULL; //dg02a
CPU_WORD * destAddress = NULL; //dg02a
if(string.getBufAddr() != NULL) //dg02a
{ //dg02a
sourceAddress = string.GetRelativePosition(sRelativeOffset,iPos);
} // else assume source is all zeros dg02a
if(getBufAddr() != NULL) //dg02a
{ //dg02a
destAddress = GetRelativePosition(dRelativeOffset,iDpos);
} //dg02a
if((sourceAddress < destAddress) ||
((sourceAddress == destAddress) && (sRelativeOffset < dRelativeOffset)))
{
copyforward = false;
}
// else copyforward
if(copyforward)
{
while(iLen)
{
uint32_t len = std::min(iLen,(uint32_t)CPU_WORD_BIT_LEN);
CPU_WORD value = string.GetField(iPos,len);
SetField(iDpos,len,value);
iLen -= len;
iPos += len;
iDpos += len;
}
} else
{
iPos += iLen;
iDpos += iLen;
while(iLen)
{
uint32_t len = std::min(iLen,(uint32_t)CPU_WORD_BIT_LEN);
iPos -= len;
iDpos -= len;
CPU_WORD value = source->GetField(iPos,len);
SetField(iDpos,len,value);
iLen -= len;
}
}
}
// ------------------------------------------------------------------------------------------------
// Function Specification //////////////////////////////////////////
//
// Title: Pattern
//
// Purpose: This function sets the the specified bits with the
// specifed pattern. The number of bits sets is
// specified by the length and begins at the specified
// offest. The pattern is repeated as often as necessary
// as specified by the pattern_bit_length.
//
// Side-effects: Bit String may be modified.
//
// Dependencies: Parameters must specifiy valid bits in both the
// bit string and the pattern.
//
// Time Complexity: O(m) where m is the number of bits to modify
// (parameter l)
//
// Examples: o(0), l(10), pattern(0xA), pattern_bit_length(4)
// Old String: 0000000000
// New String: 1010101010
//
// o(3), l(4), pattern(0x3), pattern_bit_length(3)
// Old String: 0001001000
// New String: 0000110000
//
// End Function Specification //////////////////////////////////////
void BitString::Pattern
(
uint32_t o,
uint32_t l,
CPU_WORD pattern,
uint32_t pattern_bit_length
)
{
PRDF_ASSERT(((o + l) <= iv_bitLen) &&
(pattern_bit_length <= CPU_WORD_BIT_LEN));
uint32_t current_offset;
// current_offset = offset + o;
current_offset = o;
while(true)
{
if(l > pattern_bit_length)
{
/* Set values using full CPU_WORDs */
SetField(current_offset, pattern_bit_length, pattern);
l -= pattern_bit_length;
current_offset += pattern_bit_length;
}
else
{
/* Set value in remainder of last CPU_WORD */
SetField(current_offset, l, pattern);
break;
}
}
}
// Function Specification //////////////////////////////////////////
//
// Title: Is Set
//
// Purpose: This function determines if the specified bit position
// in the string is set(1).
//
// Side-effects: None.
//
// Dependencies: bit_position must be in the string
//
// End Function Specification //////////////////////////////////////
bool BitString::IsSet
(
uint32_t bit_position
) const
{
return (GetField(bit_position,1) != 0);
}
// Function Specification //////////////////////////////////////////////
//
// Title: Set
//
// Purpose: This function sets(1) the specified bit position in
// the string.
//
// Side-effects: Bit String may be modified.
//
// Dependencies: bit_position must be in the string
//
// End Function Specification //////////////////////////////////////////
void BitString::Set
(
uint32_t bit_position
)
{
SetField(bit_position,1,CPU_WORD_MASK);
}
// Function Specification //////////////////////////////////////////////
//
// Title: Clear
//
// Purpose: This function clears(0) the specified bit position in
// the string.
//
// Side-effects: Bit String may be modified.
//
// Dependencies: bit_position must be in the string
//
// End Function Specification //////////////////////////////////////////
void BitString::Clear
(
uint32_t bit_position
)
{
SetField(bit_position,1,0);
}
// Function Specification //////////////////////////////////////////
//
// Title: Is Equal
//
// Purpose: This function compares the values of the Bit String
// memory for each bit position in the string. If the
// Bit String lengths do not match, then the Bit Strings
// are not equal.
//
// Side-effects: None.
//
// Dependencies: None.
//
// Time Complexity: O(m) where m is the length
//
// End Function Specification //////////////////////////////////////
bool BitString::IsEqual( const BitString & i_string ) const
{
if ( iv_bitLen != i_string.iv_bitLen )
return false; // size not equal
for ( uint32_t pos = 0; pos < iv_bitLen; pos += CPU_WORD_BIT_LEN )
{
uint32_t len = std::min( iv_bitLen - pos, (uint32_t)CPU_WORD_BIT_LEN );
if ( GetField(pos, len) != i_string.GetField(pos, len) )
return false; // bit strings do not match
}
return true; // bit strings match
}
// Function Specification //////////////////////////////////////////
//
// Title: Is Zero
//
// Purpose: This function compares the values of the Bit String
// with zero.
//
// Side-effects: None.
//
// Dependencies: None.
//
// Time Complexity: O(m) where m is the length
//
// End Function Specification //////////////////////////////////////
bool BitString::IsZero() const
{
for ( uint32_t pos = 0; pos < iv_bitLen; pos += CPU_WORD_BIT_LEN )
{
uint32_t len = std::min( iv_bitLen - pos, (uint32_t)CPU_WORD_BIT_LEN );
if ( 0 != GetField(pos, len) )
return false; // something is non-zero
}
return true; // everything was zero
}
// Function Specification //////////////////////////////////////////
//
// Title: Mask
//
// Purpose: This function masks the bits in the string with the
// corresponding bits in the specified Bit String. For
// each corresponding position, if the bit in the
// parameter Bit String is set(1), the bit in this string
// is cleared(0). If the length of the parameter string
// is greater than the length of this string, then the
// extra bits are ignored. If the length of the
// parameter string are less than this the length of
// this string, then the extra bits in this string are
// not modified.
//
// Side-effects: Bit String may be modified.
//
// Dependencies: None.
//
// Time Complexity: O(m) where m is the length
//
// Examples: Parameter String: 1001
// Old String: 1100
// New String: 0100
//
// Parameter String: 100111
// Old String: 1100
// New String: 0100
//
// Parameter String: 1001
// Old String: 110001
// New String: 010001
//
// End Function Specification //////////////////////////////////////
void BitString::Mask
(
const BitString & string
)
{
CPU_WORD value, string_value;
uint32_t current_offset;
uint32_t l;
/* Use smaller length */
l = std::min(iv_bitLen, string.iv_bitLen);
current_offset = 0;
while(true)
{
if(l > CPU_WORD_BIT_LEN)
{
/* Set values using full CPU_WORDs */
value = GetField(current_offset, CPU_WORD_BIT_LEN);
string_value = string.GetField(current_offset, CPU_WORD_BIT_LEN);
SetField(current_offset, CPU_WORD_BIT_LEN,
value & (~string_value));
l -= CPU_WORD_BIT_LEN;
current_offset += CPU_WORD_BIT_LEN;
}
else
{
/* Set value in remainder of last CPU_WORD */
value = GetField(current_offset, l);
string_value = string.GetField(current_offset, l);
SetField(current_offset, l, value & (~string_value));
break;
}
}
}
//-------------------------------------------------------------------------------------------------
CPU_WORD * BitString::GetRelativePosition(uint32_t & oBitOffset, uint32_t iBitPos) const
{
PRDF_ASSERT(iv_bufAddr != NULL);
oBitOffset = iBitPos % CPU_WORD_BIT_LEN;
return iv_bufAddr + (iBitPos/CPU_WORD_BIT_LEN);
}
//-------------------------------------------------------------------------------------------------
CPU_WORD * BitStringOffset::GetRelativePosition(uint32_t & oBitOffset, uint32_t iBitPos) const
{
iBitPos += ivOffset;
return BitString::GetRelativePosition(oBitOffset,iBitPos);
}
//-------------------------------------------------------------------------------------------------
BitStringOffset::~BitStringOffset(void) {}
//-------------------------------------------------------------------------------------------------
BitStringOffset & BitStringOffset::operator=(const BitStringOffset & i_bs)
{
BitString::operator=(i_bs);
ivOffset = i_bs.ivOffset;
return *this;
}
//-------------------------------------------------------------------------------------------------
BitStringOffset & BitStringOffset::operator=(const BitString & i_bs)
{
BitString::operator=(i_bs);
ivOffset = 0;
return *this;
}
// Function Specification //////////////////////////////////////////
//
// Title: Do a bitwise NOT of the bitstring
//
// Purpose: This function returns the NOT'd value of the bitstring.
//
// Side-effects: None.
//
// Dependencies: None.
//
// Time Complexity: O(m) where m is the length of Bit String
//
// End Function Specification //////////////////////////////////////
BitStringBuffer operator~(const BitString & bs)
{
BitStringBuffer bsb(bs);
for(uint32_t pos = 0; pos < bsb.getBitLen(); pos += CPU_WORD_BIT_LEN)
{
uint32_t len = bsb.getBitLen() - pos;
len = std::min(len,CPU_WORD_BIT_LEN);
CPU_WORD value = ~(bsb.GetField(pos,len));
bsb.SetField(pos,len,value);
}
return bsb;
}
//-------------------------------------------------------------------------------------------------
BitStringBuffer BitString::operator&(const BitString & bs) const
{
BitStringBuffer bsb(std::min(this->getBitLen(), bs.getBitLen()));
for(uint32_t pos = 0;
pos < std::min(this->getBitLen(), bs.getBitLen());
pos += CPU_WORD_BIT_LEN)
{
uint32_t len = std::min(this->getBitLen(), bs.getBitLen()) - pos;
len = std::min(len,CPU_WORD_BIT_LEN);
CPU_WORD value = this->GetField(pos,len) & bs.GetField(pos,len);
bsb.SetField(pos,len,value);
}
return bsb;
}
//-------------------------------------------------------------------------------------------------
BitStringBuffer BitString::operator|(const BitString & bs) const
{
BitStringBuffer bsb(std::min(this->getBitLen(), bs.getBitLen()));
for(uint32_t pos = 0;
pos < std::min(this->getBitLen(), bs.getBitLen());
pos += CPU_WORD_BIT_LEN)
{
uint32_t len = std::min(this->getBitLen(), bs.getBitLen()) - pos;
len = std::min(len,CPU_WORD_BIT_LEN);
CPU_WORD value = this->GetField(pos,len) | bs.GetField(pos,len);
bsb.SetField(pos,len,value);
}
return bsb;
}
//-------------------------------------------------------------------------------------------------
BitStringBuffer BitString::operator>>(uint32_t count) const
{
BitStringBuffer l_bsb(this->getBitLen());
BitString * l_bsbp = &l_bsb; // dg03a - stupid trick to get to GetRelativePosition()
// l_bsb.Clear();
if(count < this->getBitLen())
{
//bso overlays bsb at offset = count
uint32_t l_dummy;
BitStringOffset bso(count,l_bsb.getBitLen() - count,
l_bsbp->GetRelativePosition(l_dummy,0)); //dg03c
bso.SetBits(*this);
}
return l_bsb;
}
//-------------------------------------------------------------------------------------------------
BitStringBuffer BitString::operator<<(uint32_t count) const
{
BitStringBuffer l_bsb(this->getBitLen());
// l_bsb.Clear();
if(count < this->getBitLen())
{
// bso overlays *this at offset = count
BitStringOffset bso(count,this->getBitLen() - count,this->getBufAddr());
l_bsb.SetBits(bso);
}
return l_bsb;
}
//##############################################################################
// BitStringBuffer class
//##############################################################################
BitStringBuffer::BitStringBuffer( uint32_t i_bitLen ) :
BitString( i_bitLen, nullptr )
{
initBuffer();
}
//------------------------------------------------------------------------------
BitStringBuffer::~BitStringBuffer()
{
delete [] getBufAddr();
}
//------------------------------------------------------------------------------
BitStringBuffer::BitStringBuffer( const BitString & i_bs ) :
BitString( i_bs.getBitLen(), nullptr )
{
initBuffer();
if ( !i_bs.IsZero() ) SetBits( i_bs );
}
//------------------------------------------------------------------------------
BitStringBuffer::BitStringBuffer( const BitStringBuffer & i_bsb ) :
BitString( i_bsb.getBitLen(), nullptr )
{
initBuffer();
if ( !i_bsb.IsZero() ) SetBits( i_bsb );
}
//------------------------------------------------------------------------------
BitStringBuffer & BitStringBuffer::operator=( const BitString & i_bs )
{
setBitLen( i_bs.getBitLen() );
initBuffer();
if ( !i_bs.IsZero() ) SetBits( i_bs );
return *this;
}
//------------------------------------------------------------------------------
BitStringBuffer & BitStringBuffer::operator=( const BitStringBuffer & i_bsb )
{
if ( this != &i_bsb ) // Check for assignment to self
{
setBitLen( i_bsb.getBitLen() );
initBuffer();
if ( !i_bsb.IsZero() ) SetBits( i_bsb );
}
return *this;
}
//------------------------------------------------------------------------------
void BitStringBuffer::initBuffer()
{
// Deallocate the current buffer.
delete [] getBufAddr();
// Allocate the new buffer.
setBufAddr( new CPU_WORD[ getNumCpuWords(getBitLen()) ] );
// Clear the new buffer.
Clear();
}
/*--------------------------------------------------------------------*/
/* IO Stream Conditional Support */
/*--------------------------------------------------------------------*/
#ifdef _USE_IOSTREAMS_
std::ostream & operator<<(std::ostream & out,
const BitString & bit_string )
{
const uint32_t bit_field_length = CPU_WORD_BIT_LEN;
out << std::hex;
for(uint32_t pos = 0; pos < bit_string.getBitLen(); pos += bit_field_length)
{
uint32_t len = bit_string.getBitLen() - pos;
len = std::min(len,bit_field_length);
CPU_WORD value = bit_string.GetField(pos,len);
out << std::setw(bit_field_length/4) << std::setfill('0') << value << " ";
}
return(out);
}
#endif
} // end namespace PRDF
|