forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitvec.h
1444 lines (1211 loc) · 37.6 KB
/
bitvec.h
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//===========================================================================//
#ifndef BITVEC_H
#define BITVEC_H
#ifdef _WIN32
#pragma once
#endif
#include <limits.h>
#include "tier0/dbg.h"
#include "tier0/basetypes.h"
class CBitVecAccessor
{
public:
CBitVecAccessor(uint32 *pDWords, int iBit);
void operator=(int val);
operator uint32();
private:
uint32 *m_pDWords;
int m_iBit;
};
//-----------------------------------------------------------------------------
// Support functions
//-----------------------------------------------------------------------------
#define LOG2_BITS_PER_INT 5
#define BITS_PER_INT 32
#if _WIN32 && !defined(_X360)
#include <intrin.h>
#pragma intrinsic(_BitScanForward)
#endif
inline int FirstBitInWord( unsigned int elem, int offset )
{
#if _WIN32
if ( !elem )
return -1;
#if defined( _X360 )
// this implements CountTrailingZeros() / BitScanForward()
unsigned int mask = elem-1;
unsigned int comp = ~elem;
elem = mask & comp;
return (32 - _CountLeadingZeros(elem)) + offset;
#else
unsigned long out;
_BitScanForward(&out, elem);
return out + offset;
#endif
#else
static unsigned firstBitLUT[256] =
{
0,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,
3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,
3,0,1,0,2,0,1,0,7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,6,0,1,0,2,0,1,0,
3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,
4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0
};
unsigned elemByte;
elemByte = (elem & 0xFF);
if ( elemByte )
return offset + firstBitLUT[elemByte];
elem >>= 8;
offset += 8;
elemByte = (elem & 0xFF);
if ( elemByte )
return offset + firstBitLUT[elemByte];
elem >>= 8;
offset += 8;
elemByte = (elem & 0xFF);
if ( elemByte )
return offset + firstBitLUT[elemByte];
elem >>= 8;
offset += 8;
elemByte = (elem & 0xFF);
if ( elemByte )
return offset + firstBitLUT[elemByte];
return -1;
#endif
}
//-------------------------------------
inline unsigned GetEndMask( int numBits )
{
static unsigned bitStringEndMasks[] =
{
0xffffffff,
0x00000001,
0x00000003,
0x00000007,
0x0000000f,
0x0000001f,
0x0000003f,
0x0000007f,
0x000000ff,
0x000001ff,
0x000003ff,
0x000007ff,
0x00000fff,
0x00001fff,
0x00003fff,
0x00007fff,
0x0000ffff,
0x0001ffff,
0x0003ffff,
0x0007ffff,
0x000fffff,
0x001fffff,
0x003fffff,
0x007fffff,
0x00ffffff,
0x01ffffff,
0x03ffffff,
0x07ffffff,
0x0fffffff,
0x1fffffff,
0x3fffffff,
0x7fffffff,
};
return bitStringEndMasks[numBits % BITS_PER_INT];
}
inline uint GetBitForBitnum( int bitNum )
{
static int bitsForBitnum[] =
{
( 1 << 0 ),
( 1 << 1 ),
( 1 << 2 ),
( 1 << 3 ),
( 1 << 4 ),
( 1 << 5 ),
( 1 << 6 ),
( 1 << 7 ),
( 1 << 8 ),
( 1 << 9 ),
( 1 << 10 ),
( 1 << 11 ),
( 1 << 12 ),
( 1 << 13 ),
( 1 << 14 ),
( 1 << 15 ),
( 1 << 16 ),
( 1 << 17 ),
( 1 << 18 ),
( 1 << 19 ),
( 1 << 20 ),
( 1 << 21 ),
( 1 << 22 ),
( 1 << 23 ),
( 1 << 24 ),
( 1 << 25 ),
( 1 << 26 ),
( 1 << 27 ),
( 1 << 28 ),
( 1 << 29 ),
( 1 << 30 ),
( 1 << 31 ),
};
return bitsForBitnum[ (bitNum) & (BITS_PER_INT-1) ];
}
inline int GetBitForBitnumByte( int bitNum )
{
static int bitsForBitnum[] =
{
( 1 << 0 ),
( 1 << 1 ),
( 1 << 2 ),
( 1 << 3 ),
( 1 << 4 ),
( 1 << 5 ),
( 1 << 6 ),
( 1 << 7 ),
};
return bitsForBitnum[ bitNum & 7 ];
}
inline int CalcNumIntsForBits( int numBits ) { return (numBits + (BITS_PER_INT-1)) / BITS_PER_INT; }
#ifdef _X360
#define BitVec_Bit( bitNum ) GetBitForBitnum( bitNum )
#define BitVec_BitInByte( bitNum ) GetBitForBitnumByte( bitNum )
#else
#define BitVec_Bit( bitNum ) ( 1 << ( (bitNum) & (BITS_PER_INT-1) ) )
#define BitVec_BitInByte( bitNum ) ( 1 << ( (bitNum) & 7 ) )
#endif
#define BitVec_Int( bitNum ) ( (bitNum) >> LOG2_BITS_PER_INT )
//-----------------------------------------------------------------------------
// template CBitVecT
//
// Defines the operations relevant to any bit array. Simply requires a base
// class that implements GetNumBits(), Base(), GetNumDWords() & ValidateOperand()
//
// CVarBitVec and CBitVec<int> are the actual classes generally used
// by clients
//
template <class BASE_OPS>
class CBitVecT : public BASE_OPS
{
public:
CBitVecT();
CBitVecT(int numBits); // Must be initialized with the number of bits
void Init(int val = 0);
// Access the bits like an array.
CBitVecAccessor operator[](int i);
// Do NOT override bitwise operators (see note in header)
void And(const CBitVecT &andStr, CBitVecT *out) const;
void Or(const CBitVecT &orStr, CBitVecT *out) const;
void Xor(const CBitVecT &orStr, CBitVecT *out) const;
void Not(CBitVecT *out) const;
void CopyTo(CBitVecT *out) const;
void Copy( const CBitVecT<BASE_OPS> &other, int nBits=-1 );
bool Compare( const CBitVecT<BASE_OPS> &other, int nBits=-1 ) const;
bool IsAllClear(void) const; // Are all bits zero?
bool IsAllSet(void) const; // Are all bits one?
uint32 Get( uint32 bitNum ) const;
bool IsBitSet( int bitNum ) const;
void Set( int bitNum );
void Set( int bitNum, bool bNewVal );
void Clear(int bitNum);
bool TestAndSet(int bitNum);
void Set( uint32 offset, uint32 mask );
void Clear( uint32 offset, uint32 mask );
uint32 Get( uint32 offset, uint32 mask );
void SetAll(void); // Sets all bits
void ClearAll(void); // Clears all bits
uint32 GetDWord(int i) const;
void SetDWord(int i, uint32 val);
CBitVecT<BASE_OPS>& operator=(const CBitVecT<BASE_OPS> &other) { other.CopyTo( this ); return *this; }
bool operator==(const CBitVecT<BASE_OPS> &other) { return Compare( other ); }
bool operator!=(const CBitVecT<BASE_OPS> &other) { return !operator==( other ); }
static void GetOffsetMaskForBit( uint32 bitNum, uint32 *pOffset, uint32 *pMask ) { *pOffset = BitVec_Int( bitNum ); *pMask = BitVec_Bit( bitNum ); }
};
//-----------------------------------------------------------------------------
// class CVarBitVecBase
//
// Defines the operations necessary for a variable sized bit array
template <typename BITCOUNTTYPE>
class CVarBitVecBase
{
public:
bool IsFixedSize() const { return false; }
int GetNumBits(void) const { return m_numBits; }
void Resize( int numBits, bool bClearAll = false ); // resizes bit array
int GetNumDWords() const { return m_numInts; }
uint32 *Base() { return m_pInt; }
const uint32 *Base() const { return m_pInt; }
void Attach( uint32 *pBits, int numBits );
bool Detach( uint32 **ppBits, int *pNumBits );
int FindNextSetBit(int iStartBit) const; // returns -1 if no set bit was found
protected:
CVarBitVecBase();
CVarBitVecBase(int numBits);
CVarBitVecBase( const CVarBitVecBase<BITCOUNTTYPE> &from );
CVarBitVecBase &operator=( const CVarBitVecBase<BITCOUNTTYPE> &from );
~CVarBitVecBase(void);
void ValidateOperand( const CVarBitVecBase<BITCOUNTTYPE> &operand ) const { Assert(GetNumBits() == operand.GetNumBits()); }
unsigned GetEndMask() const { return ::GetEndMask( GetNumBits() ); }
private:
BITCOUNTTYPE m_numBits; // Number of bits in the bitstring
BITCOUNTTYPE m_numInts; // Number of ints to needed to store bitstring
uint32 m_iBitStringStorage; // If the bit string fits in one int, it goes here
uint32 * m_pInt; // Array of ints containing the bitstring
void AllocInts( int numInts ); // Free the allocated bits
void ReallocInts( int numInts );
void FreeInts( void ); // Free the allocated bits
};
//-----------------------------------------------------------------------------
// class CFixedBitVecBase
//
// Defines the operations necessary for a fixed sized bit array.
//
template <int bits> struct BitCountToEndMask_t { };
template <> struct BitCountToEndMask_t< 0> { enum { MASK = 0xffffffff }; };
template <> struct BitCountToEndMask_t< 1> { enum { MASK = 0x00000001 }; };
template <> struct BitCountToEndMask_t< 2> { enum { MASK = 0x00000003 }; };
template <> struct BitCountToEndMask_t< 3> { enum { MASK = 0x00000007 }; };
template <> struct BitCountToEndMask_t< 4> { enum { MASK = 0x0000000f }; };
template <> struct BitCountToEndMask_t< 5> { enum { MASK = 0x0000001f }; };
template <> struct BitCountToEndMask_t< 6> { enum { MASK = 0x0000003f }; };
template <> struct BitCountToEndMask_t< 7> { enum { MASK = 0x0000007f }; };
template <> struct BitCountToEndMask_t< 8> { enum { MASK = 0x000000ff }; };
template <> struct BitCountToEndMask_t< 9> { enum { MASK = 0x000001ff }; };
template <> struct BitCountToEndMask_t<10> { enum { MASK = 0x000003ff }; };
template <> struct BitCountToEndMask_t<11> { enum { MASK = 0x000007ff }; };
template <> struct BitCountToEndMask_t<12> { enum { MASK = 0x00000fff }; };
template <> struct BitCountToEndMask_t<13> { enum { MASK = 0x00001fff }; };
template <> struct BitCountToEndMask_t<14> { enum { MASK = 0x00003fff }; };
template <> struct BitCountToEndMask_t<15> { enum { MASK = 0x00007fff }; };
template <> struct BitCountToEndMask_t<16> { enum { MASK = 0x0000ffff }; };
template <> struct BitCountToEndMask_t<17> { enum { MASK = 0x0001ffff }; };
template <> struct BitCountToEndMask_t<18> { enum { MASK = 0x0003ffff }; };
template <> struct BitCountToEndMask_t<19> { enum { MASK = 0x0007ffff }; };
template <> struct BitCountToEndMask_t<20> { enum { MASK = 0x000fffff }; };
template <> struct BitCountToEndMask_t<21> { enum { MASK = 0x001fffff }; };
template <> struct BitCountToEndMask_t<22> { enum { MASK = 0x003fffff }; };
template <> struct BitCountToEndMask_t<23> { enum { MASK = 0x007fffff }; };
template <> struct BitCountToEndMask_t<24> { enum { MASK = 0x00ffffff }; };
template <> struct BitCountToEndMask_t<25> { enum { MASK = 0x01ffffff }; };
template <> struct BitCountToEndMask_t<26> { enum { MASK = 0x03ffffff }; };
template <> struct BitCountToEndMask_t<27> { enum { MASK = 0x07ffffff }; };
template <> struct BitCountToEndMask_t<28> { enum { MASK = 0x0fffffff }; };
template <> struct BitCountToEndMask_t<29> { enum { MASK = 0x1fffffff }; };
template <> struct BitCountToEndMask_t<30> { enum { MASK = 0x3fffffff }; };
template <> struct BitCountToEndMask_t<31> { enum { MASK = 0x7fffffff }; };
//-------------------------------------
template <int NUM_BITS>
class CFixedBitVecBase
{
public:
bool IsFixedSize() const { return true; }
int GetNumBits(void) const { return NUM_BITS; }
void Resize( int numBits, bool bClearAll = false ) { Assert(numBits == NUM_BITS); if ( bClearAll ) Plat_FastMemset( m_Ints, 0, NUM_INTS * sizeof(uint32) ); }// for syntatic consistency (for when using templates)
int GetNumDWords() const { return NUM_INTS; }
uint32 * Base() { return m_Ints; }
const uint32 * Base() const { return m_Ints; }
int FindNextSetBit(int iStartBit) const; // returns -1 if no set bit was found
protected:
CFixedBitVecBase() = default;
CFixedBitVecBase(int numBits) { Assert( numBits == NUM_BITS ); } // doesn't make sense, really. Supported to simplify templates & allow easy replacement of variable
void ValidateOperand( const CFixedBitVecBase<NUM_BITS> &operand ) const { } // no need, compiler does so statically
public: // for test code
unsigned GetEndMask() const { return static_cast<unsigned>( BitCountToEndMask_t<NUM_BITS % BITS_PER_INT>::MASK ); }
private:
enum
{
NUM_INTS = (NUM_BITS + (BITS_PER_INT-1)) / BITS_PER_INT
};
uint32 m_Ints[(NUM_BITS + (BITS_PER_INT-1)) / BITS_PER_INT];
};
//-----------------------------------------------------------------------------
//
// The actual classes used
//
// inheritance instead of typedef to allow forward declarations
class CVarBitVec : public CBitVecT< CVarBitVecBase<unsigned short> >
{
public:
CVarBitVec()
{
}
CVarBitVec(int numBits)
: CBitVecT< CVarBitVecBase<unsigned short> >(numBits)
{
}
};
class CLargeVarBitVec : public CBitVecT< CVarBitVecBase<int> >
{
public:
CLargeVarBitVec()
{
}
CLargeVarBitVec(int numBits)
: CBitVecT< CVarBitVecBase<int> >(numBits)
{
}
};
//-----------------------------------------------------------------------------
template < int NUM_BITS >
class CBitVec : public CBitVecT< CFixedBitVecBase<NUM_BITS> >
{
public:
CBitVec() = default;
CBitVec(int numBits)
: CBitVecT< CFixedBitVecBase<NUM_BITS> >(numBits)
{
}
};
//-----------------------------------------------------------------------------
typedef CBitVec<32> CDWordBitVec;
//-----------------------------------------------------------------------------
template <typename BITCOUNTTYPE>
inline CVarBitVecBase<BITCOUNTTYPE>::CVarBitVecBase()
{
Plat_FastMemset( this, 0, sizeof( *this ) );
}
//-----------------------------------------------------------------------------
template <typename BITCOUNTTYPE>
inline CVarBitVecBase<BITCOUNTTYPE>::CVarBitVecBase(int numBits)
{
Assert( numBits );
m_numBits = numBits;
// Figure out how many ints are needed
m_numInts = CalcNumIntsForBits( numBits );
m_pInt = NULL;
AllocInts( m_numInts );
}
//-----------------------------------------------------------------------------
template <typename BITCOUNTTYPE>
inline CVarBitVecBase<BITCOUNTTYPE>::CVarBitVecBase( const CVarBitVecBase<BITCOUNTTYPE> &from )
{
if ( from.m_numInts )
{
m_numBits = from.m_numBits;
m_numInts = from.m_numInts;
m_pInt = NULL;
AllocInts( m_numInts );
memcpy( m_pInt, from.m_pInt, m_numInts * sizeof(int) );
}
else
memset( this, 0, sizeof( *this ) );
}
//-----------------------------------------------------------------------------
template <typename BITCOUNTTYPE>
inline CVarBitVecBase<BITCOUNTTYPE> &CVarBitVecBase<BITCOUNTTYPE>::operator=( const CVarBitVecBase<BITCOUNTTYPE> &from )
{
Resize( from.GetNumBits() );
if ( m_pInt )
memcpy( m_pInt, from.m_pInt, m_numInts * sizeof(int) );
return (*this);
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
// Input :
// Output :
//-----------------------------------------------------------------------------
template <typename BITCOUNTTYPE>
inline CVarBitVecBase<BITCOUNTTYPE>::~CVarBitVecBase(void)
{
FreeInts();
}
//-----------------------------------------------------------------------------
template <typename BITCOUNTTYPE>
inline void CVarBitVecBase<BITCOUNTTYPE>::Attach( uint32 *pBits, int numBits )
{
FreeInts();
m_numBits = numBits;
m_numInts = CalcNumIntsForBits( numBits );
if ( m_numInts > 1 )
{
m_pInt = pBits;
}
else
{
m_iBitStringStorage = *pBits;
m_pInt = &m_iBitStringStorage;
free( pBits );
}
}
//-----------------------------------------------------------------------------
template <typename BITCOUNTTYPE>
inline bool CVarBitVecBase<BITCOUNTTYPE>::Detach( uint32 **ppBits, int *pNumBits )
{
if ( !m_numBits )
{
return false;
}
*pNumBits = m_numBits;
if ( m_numInts > 1 )
{
*ppBits = m_pInt;
}
else
{
*ppBits = (uint32 *)malloc( sizeof(uint32) );
**ppBits = m_iBitStringStorage;
free( m_pInt );
}
memset( this, 0, sizeof( *this ) );
return true;
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline CBitVecT<BASE_OPS>::CBitVecT()
{
// undef this is ints are not 4 bytes
// generate a compile error if sizeof(int) is not 4 (HACK: can't use the preprocessor so use the compiler)
COMPILE_TIME_ASSERT( sizeof(int)==4 );
// Initialize bitstring by clearing all bits
ClearAll();
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline CBitVecT<BASE_OPS>::CBitVecT(int numBits)
: BASE_OPS( numBits )
{
// undef this is ints are not 4 bytes
// generate a compile error if sizeof(int) is not 4 (HACK: can't use the preprocessor so use the compiler)
COMPILE_TIME_ASSERT( sizeof(int)==4 );
// Initialize bitstring by clearing all bits
ClearAll();
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline CBitVecAccessor CBitVecT<BASE_OPS>::operator[](int i)
{
Assert(i >= 0 && i < this->GetNumBits());
return CBitVecAccessor(this->Base(), i);
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::Init( int val )
{
if ( this->Base() )
Plat_FastMemset( this->Base(), ( val ) ? 0xff : 0, this->GetNumDWords() * sizeof(int) );
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline uint32 CBitVecT<BASE_OPS>::Get( uint32 bitNum ) const
{
Assert( bitNum < (uint32)this->GetNumBits() );
const uint32 *pInt = this->Base() + BitVec_Int( bitNum );
return ( *pInt & BitVec_Bit( bitNum ) );
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline bool CBitVecT<BASE_OPS>::IsBitSet( int bitNum ) const
{
Assert( bitNum >= 0 && bitNum < this->GetNumBits() );
const uint32 *pInt = this->Base() + BitVec_Int( bitNum );
return ( ( *pInt & BitVec_Bit( bitNum ) ) != 0 );
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::Set( int bitNum )
{
Assert( bitNum >= 0 && bitNum < this->GetNumBits() );
uint32 *pInt = this->Base() + BitVec_Int( bitNum );
*pInt |= BitVec_Bit( bitNum );
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline bool CBitVecT<BASE_OPS>::TestAndSet(int bitNum)
{
Assert( bitNum >= 0 && bitNum < this->GetNumBits() );
uint32 bitVecBit = BitVec_Bit( bitNum );
uint32 *pInt = this->Base() + BitVec_Int( bitNum );
bool bResult = ( ( *pInt & bitVecBit) != 0 );
*pInt |= bitVecBit;
return bResult;
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::Clear(int bitNum)
{
Assert( bitNum >= 0 && bitNum < this->GetNumBits() );
uint32 *pInt = this->Base() + BitVec_Int( bitNum );
*pInt &= ~BitVec_Bit( bitNum );
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::Set( int bitNum, bool bNewVal )
{
uint32 *pInt = this->Base() + BitVec_Int( bitNum );
uint32 bitMask = BitVec_Bit( bitNum );
if ( bNewVal )
{
*pInt |= bitMask;
}
else
{
*pInt &= ~bitMask;
}
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::Set( uint32 offset, uint32 mask )
{
uint32 *pInt = this->Base() + offset;
*pInt |= mask;
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::Clear( uint32 offset, uint32 mask )
{
uint32 *pInt = this->Base() + offset;
*pInt &= ~mask;
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline uint32 CBitVecT<BASE_OPS>::Get( uint32 offset, uint32 mask )
{
uint32 *pInt = this->Base() + offset;
return ( *pInt & mask );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input :
// Output :
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::And(const CBitVecT &addStr, CBitVecT *out) const
{
this->ValidateOperand( addStr );
this->ValidateOperand( *out );
uint32 * pDest = out->Base();
const uint32 *pOperand1 = this->Base();
const uint32 *pOperand2 = addStr.Base();
for (int i = this->GetNumDWords() - 1; i >= 0 ; --i)
{
pDest[i] = pOperand1[i] & pOperand2[i];
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input :
// Output :
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::Or(const CBitVecT &orStr, CBitVecT *out) const
{
this->ValidateOperand( orStr );
this->ValidateOperand( *out );
uint32 * pDest = out->Base();
const uint32 *pOperand1 = this->Base();
const uint32 *pOperand2 = orStr.Base();
for (int i = this->GetNumDWords() - 1; i >= 0; --i)
{
pDest[i] = pOperand1[i] | pOperand2[i];
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input :
// Output :
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::Xor(const CBitVecT &xorStr, CBitVecT *out) const
{
uint32 * pDest = out->Base();
const uint32 *pOperand1 = this->Base();
const uint32 *pOperand2 = xorStr.Base();
for (int i = this->GetNumDWords() - 1; i >= 0; --i)
{
pDest[i] = pOperand1[i] ^ pOperand2[i];
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input :
// Output :
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::Not(CBitVecT *out) const
{
this->ValidateOperand( *out );
uint32 * pDest = out->Base();
const uint32 *pOperand = this->Base();
for (int i = this->GetNumDWords() - 1; i >= 0; --i)
{
pDest[i] = ~(pOperand[i]);
}
}
//-----------------------------------------------------------------------------
// Purpose: Copy a bit string
// Input :
// Output :
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::CopyTo(CBitVecT *out) const
{
out->Resize( this->GetNumBits() );
this->ValidateOperand( *out );
Assert( out != this );
memcpy( out->Base(), this->Base(), this->GetNumDWords() * sizeof( int ) );
}
//-----------------------------------------------------------------------------
// Purpose: Are all bits zero?
// Input :
// Output :
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline bool CBitVecT<BASE_OPS>::IsAllClear(void) const
{
// Number of available bits may be more than the number
// actually used, so make sure to mask out unused bits
// before testing for zero
(const_cast<CBitVecT *>(this))->Base()[this->GetNumDWords()-1] &= CBitVecT<BASE_OPS>::GetEndMask(); // external semantics of const retained
for (int i = this->GetNumDWords() - 1; i >= 0; --i)
{
if ( this->Base()[i] !=0 )
{
return false;
}
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Are all bits set?
// Input :
// Output :
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline bool CBitVecT<BASE_OPS>::IsAllSet(void) const
{
// Number of available bits may be more than the number
// actually used, so make sure to mask out unused bits
// before testing for set bits
(const_cast<CBitVecT *>(this))->Base()[this->GetNumDWords()-1] |= ~CBitVecT<BASE_OPS>::GetEndMask(); // external semantics of const retained
for (int i = this->GetNumDWords() - 1; i >= 0; --i)
{
if ( this->Base()[i] != ~0 )
{
return false;
}
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Sets all bits
// Input :
// Output :
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::SetAll(void)
{
if ( this->Base() )
Plat_FastMemset( this->Base(), 0xff, this->GetNumDWords() * sizeof(int) );
}
//-----------------------------------------------------------------------------
// Purpose: Clears all bits
// Input :
// Output :
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::ClearAll(void)
{
if ( this->Base() )
Plat_FastMemset( this->Base(), 0, this->GetNumDWords() * sizeof(int) );
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::Copy( const CBitVecT<BASE_OPS> &other, int nBits )
{
if ( nBits == - 1 )
{
nBits = other.GetNumBits();
}
this->Resize( nBits );
this->ValidateOperand( other );
Assert( &other != this );
memcpy( this->Base(), other.Base(), this->GetNumDWords() * sizeof( uint32 ) );
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline bool CBitVecT<BASE_OPS>::Compare( const CBitVecT<BASE_OPS> &other, int nBits ) const
{
if ( nBits == - 1 )
{
if ( other.GetNumBits() != this->GetNumBits() )
{
return false;
}
nBits = other.GetNumBits();
}
if ( nBits > other.GetNumBits() || nBits > this->GetNumBits() )
{
return false;
}
(const_cast<CBitVecT *>(this))->Base()[this->GetNumDWords()-1] &= CBitVecT<BASE_OPS>::GetEndMask(); // external semantics of const retained
(const_cast<CBitVecT *>(&other))->Base()[this->GetNumDWords()-1] &= other.CBitVecT<BASE_OPS>::GetEndMask(); // external semantics of const retained
int nBytes = PAD_NUMBER( nBits, 8 ) >> 3;
return ( memcmp( this->Base(), other.Base(), nBytes ) == 0 );
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline uint32 CBitVecT<BASE_OPS>::GetDWord(int i) const
{
Assert(i >= 0 && i < this->GetNumDWords());
return this->Base()[i];
}
//-----------------------------------------------------------------------------
template <class BASE_OPS>
inline void CBitVecT<BASE_OPS>::SetDWord(int i, uint32 val)
{
Assert(i >= 0 && i < this->GetNumDWords());
this->Base()[i] = val;
}
//-----------------------------------------------------------------------------
inline unsigned GetStartBitMask( int startBit )
{
static unsigned int g_StartMask[32] =
{
0xffffffff,
0xfffffffe,
0xfffffffc,
0xfffffff8,
0xfffffff0,
0xffffffe0,
0xffffffc0,
0xffffff80,
0xffffff00,
0xfffffe00,
0xfffffc00,
0xfffff800,
0xfffff000,
0xffffe000,
0xffffc000,
0xffff8000,
0xffff0000,
0xfffe0000,
0xfffc0000,
0xfff80000,
0xfff00000,
0xffe00000,
0xffc00000,
0xff800000,
0xff000000,
0xfe000000,
0xfc000000,
0xf8000000,
0xf0000000,
0xe0000000,
0xc0000000,
0x80000000,
};
return g_StartMask[ startBit & 31 ];
}
template <typename BITCOUNTTYPE>
inline int CVarBitVecBase<BITCOUNTTYPE>::FindNextSetBit( int startBit ) const
{
if ( startBit < GetNumBits() )
{
int wordIndex = BitVec_Int(startBit);
unsigned int startMask = GetStartBitMask( startBit );
int lastWord = GetNumDWords()-1;
// handle non dword lengths
if ( (GetNumBits() % BITS_PER_INT) != 0 )
{
unsigned int elem = Base()[wordIndex];
elem &= startMask;
if ( wordIndex == lastWord)
{
elem &= (GetEndMask());
// there's a bit remaining in this word
if ( elem )
return FirstBitInWord(elem, wordIndex << 5);
}
else
{
// there's a bit remaining in this word
if ( elem )
return FirstBitInWord(elem, wordIndex << 5);
// iterate the words
for ( int i = wordIndex+1; i < lastWord; i++ )
{
elem = Base()[i];
if ( elem )
return FirstBitInWord(elem, i << 5);
}
elem = Base()[lastWord] & GetEndMask();
if ( elem )
return FirstBitInWord(elem, lastWord << 5);