blob: 127ce616151d16a9ebc993f7c309042f976682fa [file] [log] [blame]
Zane Shelley871adec2019-07-30 11:01:39 -05001#pragma once
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05002
Ben Tynera8126fd2019-08-01 19:40:07 -05003#include <stdint.h>
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05004
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
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050010//##############################################################################
11// BitString class
12//##############################################################################
13
14/**
15 * A BitString is general purpose class providing the ability to manipulate
16 * individual bits within an allocated section of contiguous memory.
17 *
18 * A BitString does not "own" the memory, it only accesses and manipulates the
19 * bits in the range specified. Users will need to ensure memory is allocated
20 * and deallocated appropriately. As an alternative, a BitStringBuffer is a
21 * BitString that will allocate and maintain its own memory.
22 *
23 * The length of a BitString is only limited by the amount of memory that
24 * contains the data buffer.
25 *
Ben Tynera8126fd2019-08-01 19:40:07 -050026 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050027 *
Ben Tynera8126fd2019-08-01 19:40:07 -050028 * - The bit positions are ordered 0 to n (left to right), where n is the bit
29 * length minus one.
30 * - The data stored in memory is assumed to be in big-endian byte format.
31 *
32 * So, for example:
33 *
34 * uint8_t a[2]; // 16 bits of memory
35 * BitString bs { 16, a }; // init BitString for a
36 * bs.setFieldRight( 0, 16, 0x1122 ); // set all 16 bits to 0x1122
37 *
38 * Results in:
39 *
40 * a[0] == bs.getFieldRight(0, 8) (i.e. 0x11)
41 * a[1] == bs.getFieldRight(8, 8) (i.e. 0x22)
42 *
43 * It is very important you do NOT do this:
44 *
45 * uint16_t x = 0x1122; // 16 bits of memory
46 * BitString bs { 16, &x }; // init BitString for x
47 *
48 * The results are undefined, or at least not portable. For example:
49 *
50 * Big-endian:
51 * x is stored in memory as |0x11|0x22|.
52 * Therefore, bs.getFieldRight(0, 8) returns 0x11.
53 *
54 * Little-endian:
55 * x is stored in memory as |0x22|0x11|.
56 * Therefore, bs.getFieldRight(0, 8) returns 0x22.
57 *
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050058 */
59class BitString
60{
Ben Tynera8126fd2019-08-01 19:40:07 -050061 private: // constants
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050062
Zane Shelleyd0af3582019-09-19 10:48:59 -050063 static const uint64_t UINT64_BIT_LEN;
64 static const uint64_t UINT8_BIT_LEN;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050065
66 public: // functions
67
68 /**
69 * @brief Constructor
70 * @param i_bitLen The number of bits in the bit string.
71 * @param i_bufAddr The starting address of the memory buffer.
Ben Tynera8126fd2019-08-01 19:40:07 -050072 * @param i_offset By default, position 0 will be the first bit of the
73 * buffer's start address. However, this parameter can be
74 * used to indicate that position 0 actually starts
75 * somewhere in the middle of the buffer.
76 * @pre Use getMinBytes() to calulate the minimum number of bytes needed
77 * to allocate sufficient memory space for this bit string.
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050078 */
Zane Shelleyd0af3582019-09-19 10:48:59 -050079 BitString( uint64_t i_bitLen, void * i_bufAddr,
80 uint64_t i_offset = 0 ) :
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050081 iv_bitLen(i_bitLen), iv_bufAddr(i_bufAddr), iv_offset(i_offset)
82 {}
83
84 /** @brief Destructor */
85 virtual ~BitString() {}
86
87 /** @return The number of bits in the bit string buffer. */
Zane Shelleyd0af3582019-09-19 10:48:59 -050088 uint64_t getBitLen() const { return iv_bitLen; }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050089
90 /** @return The address of the bit string buffer. Note that this may
91 * return nullptr. */
Ben Tynera8126fd2019-08-01 19:40:07 -050092 void * getBufAddr() const { return iv_bufAddr; }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050093
94 /**
95 * @param i_bitLen The number of bits for a bit string.
96 * @param i_offset Optional starting position of the bit string within the
97 * memory buffer.
Ben Tynera8126fd2019-08-01 19:40:07 -050098 * @return The minimum number of bytes required to allocate sufficient
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050099 * memory space for a bit string.
100 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500101 static uint64_t getMinBytes( uint64_t i_bitLen, uint64_t i_offset = 0 )
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500102 {
Ben Tynera8126fd2019-08-01 19:40:07 -0500103 return (i_bitLen + i_offset + UINT8_BIT_LEN-1) / UINT8_BIT_LEN;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500104 }
105
106 /**
107 * @brief Returns a left-justified value of the given length from the bit
108 * string starting at the given position.
109 * @param i_pos The starting position of the target range.
110 * @param i_len The number of bits of the target range.
111 * @return The value of the field range specified (left-justified).
112 * @pre nullptr != getBufAddr()
113 * @pre 0 < i_len
Ben Tynera8126fd2019-08-01 19:40:07 -0500114 * @pre i_len <= UINT64_BIT_LEN
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500115 * @pre i_pos + i_len <= getBitLen()
116 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500117 uint64_t getFieldLeft( uint64_t i_pos, uint64_t i_len ) const
Ben Tynera8126fd2019-08-01 19:40:07 -0500118 {
119 return getFieldRight(i_pos, i_len) << (UINT64_BIT_LEN - i_len);
120 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500121
122 /**
123 * @brief Returns a right-justified value of the given length from the bit
124 * string starting at the given position.
125 * @param i_pos The starting position of the target range.
126 * @param i_len The number of bits of the target range.
127 * @return The value of the field range specified (right-justified).
128 * @pre nullptr != getBufAddr()
129 * @pre 0 < i_len
Ben Tynera8126fd2019-08-01 19:40:07 -0500130 * @pre i_len <= UINT64_BIT_LEN
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500131 * @pre i_pos + i_len <= getBitLen()
132 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500133 uint64_t getFieldRight( uint64_t i_pos, uint64_t i_len ) const;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500134
135 /**
136 * @brief Sets a left-justified value of the given length into the bit
137 * string starting at the given position.
138 * @param i_pos The starting position of the target range.
139 * @param i_len The number of bits of the target range.
140 * @param i_val The left-justified value to set.
141 * @pre nullptr != getBufAddr()
142 * @pre 0 < i_len
Ben Tynera8126fd2019-08-01 19:40:07 -0500143 * @pre i_len <= UINT64_BIT_LEN
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500144 * @pre i_pos + i_len <= getBitLen()
145 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500146 void setFieldLeft( uint64_t i_pos, uint64_t i_len, uint64_t i_val );
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500147
148 /**
149 * @brief Sets a right-justified value of the given length into the bit
150 * string starting at the given position.
151 * @param i_pos The starting position of the target range.
152 * @param i_len The number of bits of the target range.
153 * @param i_val The right-justified value to set.
154 * @pre nullptr != getBufAddr()
155 * @pre 0 < i_len
Ben Tynera8126fd2019-08-01 19:40:07 -0500156 * @pre i_len <= UINT64_BIT_LEN
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500157 * @pre i_pos + i_len <= getBitLen()
158 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500159 void setFieldRight( uint64_t i_pos, uint64_t i_len, uint64_t i_val )
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500160 {
Ben Tynera8126fd2019-08-01 19:40:07 -0500161 setFieldLeft( i_pos, i_len, i_val << (UINT64_BIT_LEN - i_len) );
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500162 }
163
164 /**
165 * @param i_pos The target position.
166 * @return True if the bit at the given position is set(1), false otherwise.
167 * @pre i_pos < getBitLen().
168 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500169 bool isBitSet( uint64_t i_pos ) const
Ben Tynera8126fd2019-08-01 19:40:07 -0500170 {
171 return 0 != getFieldRight(i_pos, 1);
172 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500173
174 /**
175 * @brief Sets the target position to 1.
176 * @param i_pos The target position.
177 * @pre i_pos < getBitLen().
178 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500179 void setBit( uint64_t i_pos ) { setFieldRight( i_pos, 1, 1 ); }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500180
181 /** @brief Sets the entire bit string to 1's. */
Ben Tynera8126fd2019-08-01 19:40:07 -0500182 void setAll() { setPattern(UINT64_MAX); }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500183
184 /**
185 * @brief Sets the target position to 0.
186 * @param i_pos The target position.
187 * @pre i_pos < getBitLen().
188 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500189 void clearBit( uint64_t i_pos ) { setFieldRight( i_pos, 1, 0 ); }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500190
191 /** @brief Sets the entire bit string to 0's. */
192 void clearAll() { setPattern(0); }
193
194 /**
195 * @brief Sets a range within the string based on the pattern and length
196 * provided.
197 * @param i_sPos Starting position of this string.
198 * @param i_sLen The length of the target range.
199 * @param i_pattern The pattern to set (right justified).
200 * @param i_pLen The length of the pattern.
201 * @pre nullptr != getBufAddr()
202 * @pre 0 < i_sLen
203 * @pre i_sPos + i_sLen <= getBitLen()
Ben Tynera8126fd2019-08-01 19:40:07 -0500204 * @pre 0 < i_pLen <= UINT64_BIT_LEN
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500205 * @post The pattern is repeated/truncated as needed.
206 *
207 * Examples: i_sPos(0), i_sLen(10), i_pattern(0xA), i_pLen(4)
208 * Old String: 0000000000
209 * New String: 1010101010
210 *
211 * i_sPos(3), i_sLen(4), i_pattern(0x3), i_pLen(3)
212 * Old String: 0001001000
213 * New String: 0000110000
214 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500215 void setPattern( uint64_t i_sPos, uint64_t i_sLen,
216 uint64_t i_pattern, uint64_t i_pLen );
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500217
218 /**
219 * @brief Sets entire string based on the pattern and length provided.
220 * @param i_pattern The pattern to set (right justified).
221 * @param i_pLen The length of the pattern.
222 * @note See definition above for prerequisites.
223 * @post The entire string is filled with the pattern.
224 * @post The pattern is repeated/truncated as needed.
225 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500226 void setPattern( uint64_t i_pattern, uint64_t i_pLen )
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500227 {
228 setPattern( 0, getBitLen(), i_pattern, i_pLen );
229 }
230
231 /**
Ben Tynera8126fd2019-08-01 19:40:07 -0500232 * @brief Sets entire string based on the pattern provided.
233 * @param i_pattern The pattern to set (right justified).
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500234 * @note See definition above for prerequisites.
235 * @post The entire string is filled with the pattern.
236 * @post The pattern is repeated/truncated as needed.
237 */
Ben Tynera8126fd2019-08-01 19:40:07 -0500238 void setPattern( uint64_t i_pattern )
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500239 {
Ben Tynera8126fd2019-08-01 19:40:07 -0500240 setPattern( i_pattern, sizeof(i_pattern) * 8 );
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500241 }
242
243 /**
244 * @brief Set bits in this string based on the given string.
245 * @param i_sStr The source string.
246 * @param i_sPos The starting position of the source string.
247 * @param i_sLen The number of bits to copy from the source string.
248 * @param i_dPos The starting position of the this string.
249 * @pre nullptr != getBufAddr()
250 * @pre nullptr != i_sStr.getBufAddr()
251 * @pre 0 < i_sLen
252 * @pre i_sPos + i_sLen <= i_sStr.getBitLen()
253 * @pre i_dPos < getBitLen()
254 * @post Source bits in given range are copied to this starting at i_dPos.
255 * @note If the length of the given string is greater than the length of
256 * this string, then the extra bits are ignored.
257 * @note If the length of the given string is less than the length of this
258 * string, then the extra bits in this string are not modified.
259 * @note This string and the source string may specify overlapping memory.
260 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500261 void setString( const BitString & i_sStr, uint64_t i_sPos,
262 uint64_t i_sLen, uint64_t i_dPos = 0 );
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500263
264 /**
265 * @brief Set bits in this string based on the provided string.
266 * @param i_sStr The source string.
267 * @note This will try to copy as much of the source as possible to this
268 * string, starting with the first bit in each string.
269 * @note See the other definition of this function for details and
270 * restrictions.
271 */
272 void setString( const BitString & i_sStr )
273 {
274 setString( i_sStr, 0, i_sStr.getBitLen() );
275 }
276
277 /**
278 * @brief Masks (clears) any bits set in this string that correspond to bits
279 * set in the given string (this & ~mask).
Ben Tynera8126fd2019-08-01 19:40:07 -0500280 * @param i_mask The mask string (right justified).
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500281 * @note If the length of the given string is greater than the length of
282 * this string, then the extra bits are ignored.
283 * @note If the length of the given string is less than the length of this
284 * string, then the extra bits in this string are not modified.
285 */
286 void maskString( const BitString & i_mask );
287
288 /**
289 * @param i_str The string to compare.
290 * @return True if the strings are equivalent, false otherwise.
291 * @pre Both strings must be of equal length and have same values to be
292 * equal.
293 */
294 bool isEqual( const BitString & i_str ) const;
295
296 /** @return True if there are no bit set(1) in this bit string, false
297 * otherwise. */
298 bool isZero() const;
299
300 /**
301 * @param i_pos The starting position of the target range.
302 * @param i_len The length of the target range.
303 * @return The number of bits that are set(1) in given range of this string.
304 * @pre nullptr != getBufAddr()
305 * @pre i_pos + i_len <= getBitLen()
306 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500307 uint64_t getSetCount( uint64_t i_pos, uint64_t i_len ) const;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500308
309 /** @return The number of bits that are set(1) in this string. */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500310 uint64_t getSetCount() const { return getSetCount( 0, getBitLen() ); }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500311
312 /** @brief Comparison operator. */
313 bool operator==( const BitString & i_str ) const { return isEqual(i_str); }
314
315 /** @brief Bitwise NOT operator. */
316 BitStringBuffer operator~() const;
317
318 /** @brief Bitwise AND operator. */
319 BitStringBuffer operator&( const BitString & i_bs ) const;
320
321 /** @brief Bitwise OR operator. */
322 BitStringBuffer operator|( const BitString & i_bs ) const;
323
324 /** @brief Right shift operator. */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500325 BitStringBuffer operator>>( uint64_t i_shift ) const;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500326
327 /** @brief Left shift operator. */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500328 BitStringBuffer operator<<( uint64_t i_shift ) const;
329
330 /**
331 * @brief Explicitly disables copy from BitString.
332 *
333 * Prevents assigning a BitString & to a BitString, which would strip
334 * polymorphism.
335 */
336 BitString( const BitString & i_bs ) = delete;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500337
Ben Tynera8126fd2019-08-01 19:40:07 -0500338 /**
339 * @brief Explicitly disables assignment from BitStringBuffer.
340 *
341 * Allowing this would be dangerous if the BitStringBuffer goes out of scope
342 * because the BitString would point to memory that is no longer in context.
343 */
344 BitString & operator=( const BitStringBuffer & i_bsb ) = delete;
345
346 /**
347 * @brief Explicitly disables copy from BitStringBuffer.
348 *
349 * Allowing this would be dangerous if the BitStringBuffer goes out of scope
350 * because the BitString would point to memory that is no longer in context.
351 */
352 BitString( const BitStringBuffer & i_bsb ) = delete;
353
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500354 protected: // functions
355
356 /**
357 * @param i_newBufAddr The starting address of the new bit string buffer.
358 * @pre Before calling this function, make sure you deallocate the old
359 * buffer to avoid memory leaks.
360 */
Ben Tynera8126fd2019-08-01 19:40:07 -0500361 void setBufAddr( void * i_newBufAddr ) { iv_bufAddr = i_newBufAddr; }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500362
363 /** @param i_newBitLen The new bit length of this bit string buffer. */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500364 void setBitLen( uint64_t i_newBitLen ) { iv_bitLen = i_newBitLen; }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500365
366 private: // functions
367
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500368 /**
369 * @brief Given a bit position within the bit string, this function returns
370 * the address that contains the bit position and the bit position
371 * relative to that address.
372 * @param o_relPos The returned relative position.
373 * @param i_absPos The inputted absolute position.
374 * @return The relative address.
375 * @pre nullptr != getBufAddr()
376 * @pre i_absPos < getBitLen()
377 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500378 uint8_t * getRelativePosition( uint64_t & o_relPos,
379 uint64_t i_absPos ) const;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500380
381 private: // instance variables
382
Zane Shelleyd0af3582019-09-19 10:48:59 -0500383 uint64_t iv_bitLen; ///< The bit length of this buffer.
Ben Tynera8126fd2019-08-01 19:40:07 -0500384 void * iv_bufAddr; ///< The beginning address of this buffer.
Zane Shelleyd0af3582019-09-19 10:48:59 -0500385 uint64_t iv_offset; ///< Start position offset
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500386};
387
388//##############################################################################
389// BitStringBuffer class
390//##############################################################################
391
392/** A BitStringBuffer is a BitString that maintains its own buffer in memory. It
393 * guarantees that sufficient memory is allocated and deallocated in the
394 * constructor and destructor, respectively. In addition, the assignment
395 * operator will adjust the amount of memory needed, as necessary, for the
396 * assignment. */
397class BitStringBuffer : public BitString
398{
399 public: // functions
400
401 /**
402 * @brief Constructor
403 * @param i_bitLen Number of bits in the string.
404 */
Zane Shelleyd0af3582019-09-19 10:48:59 -0500405 explicit BitStringBuffer( uint64_t i_bitLen );
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500406
407 /** @brief Destructor */
408 ~BitStringBuffer();
409
410 /** @brief Copy constructor from BitString */
411 BitStringBuffer( const BitString & i_bs );
412
413 /** @brief Copy constructor from BitStringBuffer */
414 BitStringBuffer( const BitStringBuffer & i_bsb );
415
416 /** @brief Assignment from BitString */
417 BitStringBuffer & operator=( const BitString & i_bs );
418
419 /** @brief Assignment from BitStringBuffer */
420 BitStringBuffer & operator=( const BitStringBuffer & i_bsb );
421
422 private: // functions
423
424 /** @brief Deallocates the old buffer, if needed, and initializes the new
425 * buffer. */
426 void initBuffer();
427};
428
Zane Shelley871adec2019-07-30 11:01:39 -0500429} // end namespace libhei
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500430