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