blob: cc1c073f534bad1d99bcc4a4e3baacbff9f9e714 [file] [log] [blame]
Zane Shelley871adec2019-07-30 11:01:39 -05001#pragma once
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05002
3#include <prdf_types.h>
4
Zane Shelley871adec2019-07-30 11:01:39 -05005namespace libhei
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05006{
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05007
8class BitStringBuffer;
9
10/** This type is used to take advantage of the most efficient memory reference
11 * size for a specific CPU architecture. */
12typedef uint32_t CPU_WORD;
13
14//##############################################################################
15// BitString class
16//##############################################################################
17
18/**
19 * A BitString is general purpose class providing the ability to manipulate
20 * individual bits within an allocated section of contiguous memory.
21 *
22 * A BitString does not "own" the memory, it only accesses and manipulates the
23 * bits in the range specified. Users will need to ensure memory is allocated
24 * and deallocated appropriately. As an alternative, a BitStringBuffer is a
25 * BitString that will allocate and maintain its own memory.
26 *
27 * The length of a BitString is only limited by the amount of memory that
28 * contains the data buffer.
29 *
30 * The CPU_WORD type is used internally to reference memory and as the interface
31 * type for the field. Ensure that any buffer allocated for a BitString is
32 * CPU_WORD aligned so that the BitString does not accidentally access memory
33 * beyond availability. For example, say we have a buffer allocated for 6 byte
34 * (48 bits) and those 6 bytes are allocated at the very end of accessible
35 * memory. When the BitString tries to access the second CPU_WORD, which
36 * contains the last 2 bytes of the buffer, an expection will be thrown because
37 * the BitString always access an entire CPU_WORD (4 bytes) at a time and the
38 * last two bytes are not accessible. Utilize the static function
39 * getNumCpuWords() to get the minimum number of CPU_WORDs required to allocate
40 * sufficient space in the buffer. For example, getNumCpuWords(48) returns 2.
41 *
42 * The bit positions are ordered 0 to n (left to right), where n is the bit
43 * length minus one. By default, position 0 will be the first bit of the
44 * buffer's start address. The optional constructor allows users to input an
45 * offset anywhere within the buffer, which is then used as position 0. This is
46 * useful when the data within the buffer is a right-justified.
47 */
48class BitString
49{
50 public: // constants
51
52 /** Bit length of a CPU_WORD */
53 static const uint32_t CPU_WORD_BIT_LEN;
54
55 /** A CPU_WORD with all of the bits set to 1 */
56 static const CPU_WORD CPU_WORD_MASK;
57
58 public: // functions
59
60 /**
61 * @brief Constructor
62 * @param i_bitLen The number of bits in the bit string.
63 * @param i_bufAddr The starting address of the memory buffer.
64 * @param i_offset Optional input to indicate the actual starting position
65 * of the bit string within the memory buffer.
66 * @post It is possible that i_bitLen + i_offset may not be CPU_WORD
67 * aligned, however, the memory space allocated for i_bufAddr must be
68 * CPU_WORD aligned to avoid functions in this class accessing memory
69 * outside the available memory space. Use getNumCpuWords() to
70 * calulate the number of CPU_WORDs needed to allocate sufficient
71 * memory space.
72 */
73 BitString( uint32_t i_bitLen, CPU_WORD * i_bufAddr,
74 uint32_t i_offset = 0 ) :
75 iv_bitLen(i_bitLen), iv_bufAddr(i_bufAddr), iv_offset(i_offset)
76 {}
77
78 /** @brief Destructor */
79 virtual ~BitString() {}
80
81 /** @return The number of bits in the bit string buffer. */
82 uint32_t getBitLen() const { return iv_bitLen; }
83
84 /** @return The address of the bit string buffer. Note that this may
85 * return nullptr. */
86 CPU_WORD * getBufAddr() const { return iv_bufAddr; }
87
88 /**
89 * @param i_bitLen The number of bits for a bit string.
90 * @param i_offset Optional starting position of the bit string within the
91 * memory buffer.
92 * @return The minimum number of CPU_WORDs required to allocate sufficient
93 * memory space for a bit string.
94 */
95 static uint32_t getNumCpuWords( uint32_t i_bitLen, uint32_t i_offset = 0 )
96 {
97 return (i_bitLen + i_offset + CPU_WORD_BIT_LEN-1) / CPU_WORD_BIT_LEN;
98 }
99
100 /**
101 * @brief Returns a left-justified value of the given length from the bit
102 * string starting at the given position.
103 * @param i_pos The starting position of the target range.
104 * @param i_len The number of bits of the target range.
105 * @return The value of the field range specified (left-justified).
106 * @pre nullptr != getBufAddr()
107 * @pre 0 < i_len
108 * @pre i_len <= CPU_WORD_BIT_LEN
109 * @pre i_pos + i_len <= getBitLen()
110 */
111 CPU_WORD getField( uint32_t i_pos, uint32_t i_len ) const;
112
113 /**
114 * @brief Returns a right-justified value of the given length from the bit
115 * string starting at the given position.
116 * @param i_pos The starting position of the target range.
117 * @param i_len The number of bits of the target range.
118 * @return The value of the field range specified (right-justified).
119 * @pre nullptr != getBufAddr()
120 * @pre 0 < i_len
121 * @pre i_len <= CPU_WORD_BIT_LEN
122 * @pre i_pos + i_len <= getBitLen()
123 */
124 CPU_WORD getFieldJustify( uint32_t i_pos, uint32_t i_len ) const
125 {
126 return getField(i_pos, i_len) >> (CPU_WORD_BIT_LEN - i_len);
127 }
128
129 /**
130 * @brief Sets a left-justified value of the given length into the bit
131 * string starting at the given position.
132 * @param i_pos The starting position of the target range.
133 * @param i_len The number of bits of the target range.
134 * @param i_val The left-justified value to set.
135 * @pre nullptr != getBufAddr()
136 * @pre 0 < i_len
137 * @pre i_len <= CPU_WORD_BIT_LEN
138 * @pre i_pos + i_len <= getBitLen()
139 */
140 void setField( uint32_t i_pos, uint32_t i_len, CPU_WORD i_val );
141
142 /**
143 * @brief Sets a right-justified value of the given length into the bit
144 * string starting at the given position.
145 * @param i_pos The starting position of the target range.
146 * @param i_len The number of bits of the target range.
147 * @param i_val The right-justified value to set.
148 * @pre nullptr != getBufAddr()
149 * @pre 0 < i_len
150 * @pre i_len <= CPU_WORD_BIT_LEN
151 * @pre i_pos + i_len <= getBitLen()
152 */
153 void setFieldJustify( uint32_t i_pos, uint32_t i_len, CPU_WORD i_val )
154 {
155 setField( i_pos, i_len, i_val << (CPU_WORD_BIT_LEN - i_len) );
156 }
157
158 /**
159 * @param i_pos The target position.
160 * @return True if the bit at the given position is set(1), false otherwise.
161 * @pre i_pos < getBitLen().
162 */
163 bool isBitSet( uint32_t i_pos ) const { return 0 != getField(i_pos, 1); }
164
165 /**
166 * @brief Sets the target position to 1.
167 * @param i_pos The target position.
168 * @pre i_pos < getBitLen().
169 */
170 void setBit( uint32_t i_pos ) { setFieldJustify( i_pos, 1, 1 ); }
171
172 /** @brief Sets the entire bit string to 1's. */
173 void setAll() { setPattern(CPU_WORD_MASK); }
174
175 /**
176 * @brief Sets the target position to 0.
177 * @param i_pos The target position.
178 * @pre i_pos < getBitLen().
179 */
180 void clearBit( uint32_t i_pos ) { setFieldJustify( i_pos, 1, 0 ); }
181
182 /** @brief Sets the entire bit string to 0's. */
183 void clearAll() { setPattern(0); }
184
185 /**
186 * @brief Sets a range within the string based on the pattern and length
187 * provided.
188 * @param i_sPos Starting position of this string.
189 * @param i_sLen The length of the target range.
190 * @param i_pattern The pattern to set (right justified).
191 * @param i_pLen The length of the pattern.
192 * @pre nullptr != getBufAddr()
193 * @pre 0 < i_sLen
194 * @pre i_sPos + i_sLen <= getBitLen()
195 * @pre 0 < i_pLen <= CPU_WORD_BIT_LEN
196 * @post The pattern is repeated/truncated as needed.
197 *
198 * Examples: i_sPos(0), i_sLen(10), i_pattern(0xA), i_pLen(4)
199 * Old String: 0000000000
200 * New String: 1010101010
201 *
202 * i_sPos(3), i_sLen(4), i_pattern(0x3), i_pLen(3)
203 * Old String: 0001001000
204 * New String: 0000110000
205 */
206 void setPattern( uint32_t i_sPos, uint32_t i_sLen,
207 CPU_WORD i_pattern, uint32_t i_pLen );
208
209 /**
210 * @brief Sets entire string based on the pattern and length provided.
211 * @param i_pattern The pattern to set (right justified).
212 * @param i_pLen The length of the pattern.
213 * @note See definition above for prerequisites.
214 * @post The entire string is filled with the pattern.
215 * @post The pattern is repeated/truncated as needed.
216 */
217 void setPattern( CPU_WORD i_pattern, uint32_t i_pLen )
218 {
219 setPattern( 0, getBitLen(), i_pattern, i_pLen );
220 }
221
222 /**
223 * @brief Sets entire string based on the pattern provided (length of
224 * CPU_WORD).
225 * @param i_pattern The pattern to set.
226 * @note See definition above for prerequisites.
227 * @post The entire string is filled with the pattern.
228 * @post The pattern is repeated/truncated as needed.
229 */
230 void setPattern( CPU_WORD i_pattern )
231 {
232 setPattern( i_pattern, CPU_WORD_BIT_LEN );
233 }
234
235 /**
236 * @brief Set bits in this string based on the given string.
237 * @param i_sStr The source string.
238 * @param i_sPos The starting position of the source string.
239 * @param i_sLen The number of bits to copy from the source string.
240 * @param i_dPos The starting position of the this string.
241 * @pre nullptr != getBufAddr()
242 * @pre nullptr != i_sStr.getBufAddr()
243 * @pre 0 < i_sLen
244 * @pre i_sPos + i_sLen <= i_sStr.getBitLen()
245 * @pre i_dPos < getBitLen()
246 * @post Source bits in given range are copied to this starting at i_dPos.
247 * @note If the length of the given string is greater than the length of
248 * this string, then the extra bits are ignored.
249 * @note If the length of the given string is less than the length of this
250 * string, then the extra bits in this string are not modified.
251 * @note This string and the source string may specify overlapping memory.
252 */
253 void setString( const BitString & i_sStr, uint32_t i_sPos,
254 uint32_t i_sLen, uint32_t i_dPos = 0 );
255
256 /**
257 * @brief Set bits in this string based on the provided string.
258 * @param i_sStr The source string.
259 * @note This will try to copy as much of the source as possible to this
260 * string, starting with the first bit in each string.
261 * @note See the other definition of this function for details and
262 * restrictions.
263 */
264 void setString( const BitString & i_sStr )
265 {
266 setString( i_sStr, 0, i_sStr.getBitLen() );
267 }
268
269 /**
270 * @brief Masks (clears) any bits set in this string that correspond to bits
271 * set in the given string (this & ~mask).
272 * @param i_mask The mask string.
273 * @note If the length of the given string is greater than the length of
274 * this string, then the extra bits are ignored.
275 * @note If the length of the given string is less than the length of this
276 * string, then the extra bits in this string are not modified.
277 */
278 void maskString( const BitString & i_mask );
279
280 /**
281 * @param i_str The string to compare.
282 * @return True if the strings are equivalent, false otherwise.
283 * @pre Both strings must be of equal length and have same values to be
284 * equal.
285 */
286 bool isEqual( const BitString & i_str ) const;
287
288 /** @return True if there are no bit set(1) in this bit string, false
289 * otherwise. */
290 bool isZero() const;
291
292 /**
293 * @param i_pos The starting position of the target range.
294 * @param i_len The length of the target range.
295 * @return The number of bits that are set(1) in given range of this string.
296 * @pre nullptr != getBufAddr()
297 * @pre i_pos + i_len <= getBitLen()
298 */
299 uint32_t getSetCount( uint32_t i_pos, uint32_t i_len ) const;
300
301 /** @return The number of bits that are set(1) in this string. */
302 uint32_t getSetCount() const { return getSetCount( 0, getBitLen() ); }
303
304 /** @brief Comparison operator. */
305 bool operator==( const BitString & i_str ) const { return isEqual(i_str); }
306
307 /** @brief Bitwise NOT operator. */
308 BitStringBuffer operator~() const;
309
310 /** @brief Bitwise AND operator. */
311 BitStringBuffer operator&( const BitString & i_bs ) const;
312
313 /** @brief Bitwise OR operator. */
314 BitStringBuffer operator|( const BitString & i_bs ) const;
315
316 /** @brief Right shift operator. */
317 BitStringBuffer operator>>( uint32_t i_shift ) const;
318
319 /** @brief Left shift operator. */
320 BitStringBuffer operator<<( uint32_t i_shift ) const;
321
322 protected: // functions
323
324 /**
325 * @param i_newBufAddr The starting address of the new bit string buffer.
326 * @pre Before calling this function, make sure you deallocate the old
327 * buffer to avoid memory leaks.
328 */
329 void setBufAddr( CPU_WORD * i_newBufAddr ) { iv_bufAddr = i_newBufAddr; }
330
331 /** @param i_newBitLen The new bit length of this bit string buffer. */
332 void setBitLen( uint32_t i_newBitLen ) { iv_bitLen = i_newBitLen; }
333
334 private: // functions
335
336 // Prevent the assignment operator and copy constructor from a
337 // BitStringBuffer. While technically these could be done. We run into
338 // serious problems like with the operator functions above that all return
339 // a BitStringBuffer. If we allowed these, the BitString would end up
340 // pointing to memory that is no longer in context.
341 BitString & operator=( const BitStringBuffer & i_bsb );
342 BitString( const BitStringBuffer & i_bsb );
343
344 /**
345 * @brief Given a bit position within the bit string, this function returns
346 * the address that contains the bit position and the bit position
347 * relative to that address.
348 * @param o_relPos The returned relative position.
349 * @param i_absPos The inputted absolute position.
350 * @return The relative address.
351 * @pre nullptr != getBufAddr()
352 * @pre i_absPos < getBitLen()
353 */
354 CPU_WORD * getRelativePosition( uint32_t & o_relPos,
355 uint32_t i_absPos ) const;
356
357 private: // instance variables
358
359 uint32_t iv_bitLen; ///< The bit length of this buffer.
360 CPU_WORD * iv_bufAddr; ///< The beginning address of this buffer.
361 uint32_t iv_offset; ///< Start position offset
362};
363
364//##############################################################################
365// BitStringBuffer class
366//##############################################################################
367
368/** A BitStringBuffer is a BitString that maintains its own buffer in memory. It
369 * guarantees that sufficient memory is allocated and deallocated in the
370 * constructor and destructor, respectively. In addition, the assignment
371 * operator will adjust the amount of memory needed, as necessary, for the
372 * assignment. */
373class BitStringBuffer : public BitString
374{
375 public: // functions
376
377 /**
378 * @brief Constructor
379 * @param i_bitLen Number of bits in the string.
380 */
381 explicit BitStringBuffer( uint32_t i_bitLen );
382
383 /** @brief Destructor */
384 ~BitStringBuffer();
385
386 /** @brief Copy constructor from BitString */
387 BitStringBuffer( const BitString & i_bs );
388
389 /** @brief Copy constructor from BitStringBuffer */
390 BitStringBuffer( const BitStringBuffer & i_bsb );
391
392 /** @brief Assignment from BitString */
393 BitStringBuffer & operator=( const BitString & i_bs );
394
395 /** @brief Assignment from BitStringBuffer */
396 BitStringBuffer & operator=( const BitStringBuffer & i_bsb );
397
398 private: // functions
399
400 /** @brief Deallocates the old buffer, if needed, and initializes the new
401 * buffer. */
402 void initBuffer();
403};
404
Zane Shelley871adec2019-07-30 11:01:39 -0500405} // end namespace libhei
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500406