blob: 39a585cf40c103fa12fa5d278411c2c523c93e25 [file] [log] [blame]
Ben Tynera8126fd2019-08-01 19:40:07 -05001/** @file hei_bit_string.cpp
2 * @brief BitString and BitStringBuffer class definitions
3 */
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05004
Ben Tynera8126fd2019-08-01 19:40:07 -05005#include <hei_user_defines.hpp>
Zane Shelleyca9f6252019-10-25 21:17:30 -05006#include <util/hei_bit_string.hpp>
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05007
8#include <algorithm>
9
Zane Shelley871adec2019-07-30 11:01:39 -050010namespace libhei
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050011{
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050012
13//##############################################################################
14// BitString class
15//##############################################################################
16
Ben Tynera8126fd2019-08-01 19:40:07 -050017// number of bits in a uint64_t
Zane Shelleyd0af3582019-09-19 10:48:59 -050018constexpr uint64_t BitString::UINT64_BIT_LEN = sizeof(uint64_t) * 8;
Ben Tynera8126fd2019-08-01 19:40:07 -050019// number of bits in a uint8_t
Zane Shelleyd0af3582019-09-19 10:48:59 -050020constexpr uint64_t BitString::UINT8_BIT_LEN = sizeof(uint8_t) * 8;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050021
22//------------------------------------------------------------------------------
23
Zane Shelley83da2452019-10-25 15:45:34 -050024uint64_t BitString::getFieldRight(uint64_t i_pos, uint64_t i_len) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050025{
Zane Shelley83da2452019-10-25 15:45:34 -050026 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
27 HEI_ASSERT(0 < i_len); // must have at least one bit
28 HEI_ASSERT(i_len <= UINT64_BIT_LEN); // i_len length must be valid
29 HEI_ASSERT(i_pos + i_len <= getBitLen()); // field must be within range
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050030
Ben Tynera8126fd2019-08-01 19:40:07 -050031 // Get the relative address of this byte and the relative starting position
32 // within the byte.
Zane Shelleyd0af3582019-09-19 10:48:59 -050033 uint64_t relPos = 0;
Zane Shelleyfe27b652019-10-28 11:33:07 -050034 uint8_t* relAddr = getRelativePosition(relPos, i_pos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050035
Ben Tynera8126fd2019-08-01 19:40:07 -050036 // Get the length of the target bit field within this byte and the length of
37 // the bit field for any remaining bits.
Zane Shelleyd0af3582019-09-19 10:48:59 -050038 uint64_t bf_len = i_len;
39 uint64_t remain_len = 0;
Zane Shelley83da2452019-10-25 15:45:34 -050040 if (UINT8_BIT_LEN < relPos + i_len)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050041 {
Ben Tynera8126fd2019-08-01 19:40:07 -050042 // The target bit field crosses a byte boundary. So truncate the bit
43 // length for this byte and update the remaining length.
44 bf_len = UINT8_BIT_LEN - relPos;
45 remain_len = i_len - bf_len;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050046 }
47
Ben Tynera8126fd2019-08-01 19:40:07 -050048 // Get the target bit field within this byte (right justified).
49 uint8_t bf = *relAddr;
50 bf <<= relPos; // Mask off uneeded bits on the left side.
51 bf >>= UINT8_BIT_LEN - bf_len; // Right justify the value.
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050052
Ben Tynera8126fd2019-08-01 19:40:07 -050053 // Check for any remaining bits after this target byte.
Zane Shelley83da2452019-10-25 15:45:34 -050054 if (0 != remain_len)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050055 {
Ben Tynera8126fd2019-08-01 19:40:07 -050056 // Recursively call this function on the remaining bits and push them
57 // into the right side of the return value.
58 uint64_t val = static_cast<uint64_t>(bf) << remain_len;
Zane Shelley83da2452019-10-25 15:45:34 -050059 return val | getFieldRight(i_pos + bf_len, remain_len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050060 }
61
Ben Tynera8126fd2019-08-01 19:40:07 -050062 // Nothing more to do. Simply return this bit field.
63 return bf;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050064}
65
66//------------------------------------------------------------------------------
67
Zane Shelley83da2452019-10-25 15:45:34 -050068void BitString::setFieldLeft(uint64_t i_pos, uint64_t i_len, uint64_t i_val)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050069{
Zane Shelley83da2452019-10-25 15:45:34 -050070 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
71 HEI_ASSERT(0 < i_len); // must have at least one bit
72 HEI_ASSERT(i_len <= UINT64_BIT_LEN); // i_len length must be valid
73 HEI_ASSERT(i_pos + i_len <= getBitLen()); // field must be within range
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050074
Ben Tynera8126fd2019-08-01 19:40:07 -050075 // Get the relative address of this byte and the relative starting position
76 // within the byte.
Zane Shelleyd0af3582019-09-19 10:48:59 -050077 uint64_t relPos = 0;
Zane Shelleyfe27b652019-10-28 11:33:07 -050078 uint8_t* relAddr = getRelativePosition(relPos, i_pos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050079
Ben Tynera8126fd2019-08-01 19:40:07 -050080 // Get the length of the target bit field within this byte and the length of
81 // the bit field for any remaining bits.
Zane Shelleyd0af3582019-09-19 10:48:59 -050082 uint64_t bf_len = i_len;
83 uint64_t remain_len = 0;
Zane Shelley83da2452019-10-25 15:45:34 -050084 if (UINT8_BIT_LEN < relPos + i_len)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050085 {
Ben Tynera8126fd2019-08-01 19:40:07 -050086 // The target bit field crosses a byte boundary. So truncate the bit
87 // length for this byte and update the remaining length.
88 bf_len = UINT8_BIT_LEN - relPos;
89 remain_len = i_len - bf_len;
90 }
91
92 // It is possible there are bits in this byte on either side of the target
93 // bit field that must be preserved. Get the length of each of those bit
94 // fields.
Zane Shelleyd0af3582019-09-19 10:48:59 -050095 uint64_t bf_l_len = relPos;
96 uint64_t bf_r_len = UINT8_BIT_LEN - (bf_l_len + bf_len);
Ben Tynera8126fd2019-08-01 19:40:07 -050097
98 // Get the target bit field from the left justified inputed value.
99 uint8_t bf = (i_val >> (UINT64_BIT_LEN - bf_len)) << bf_r_len;
100
101 // Get the bit fields on either side of the target bit field.
Zane Shelleyd0af3582019-09-19 10:48:59 -0500102 uint64_t bf_l_shift = UINT8_BIT_LEN - bf_l_len;
103 uint64_t bf_r_shift = UINT8_BIT_LEN - bf_r_len;
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500104 uint8_t bf_l = *relAddr;
105 bf_l >>= bf_l_shift;
106 bf_l <<= bf_l_shift;
107 uint8_t bf_r = *relAddr;
108 bf_r <<= bf_r_shift;
109 bf_r >>= bf_r_shift;
Ben Tynera8126fd2019-08-01 19:40:07 -0500110
111 // Combine all three parts of the byte and write it out to memory.
112 *relAddr = bf_l | bf | bf_r;
113
114 // Check for any remaining bits after this target byte.
Zane Shelley83da2452019-10-25 15:45:34 -0500115 if (0 != remain_len)
Ben Tynera8126fd2019-08-01 19:40:07 -0500116 {
117 // Recursively call this function on the remaining bits.
Zane Shelley83da2452019-10-25 15:45:34 -0500118 setFieldLeft(i_pos + bf_len, remain_len, i_val << bf_len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500119 }
120}
121
122//------------------------------------------------------------------------------
123
Zane Shelley83da2452019-10-25 15:45:34 -0500124void BitString::setPattern(uint64_t i_sPos, uint64_t i_sLen, uint64_t i_pattern,
125 uint64_t i_pLen)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500126{
Ben Tynera8126fd2019-08-01 19:40:07 -0500127
128 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
129 HEI_ASSERT(0 < i_sLen); // must have at least one bit
130 HEI_ASSERT(i_sPos + i_sLen <= getBitLen()); // field must be within range
131 HEI_ASSERT(0 < i_pLen); // must have at least one bit
Zane Shelley83da2452019-10-25 15:45:34 -0500132 HEI_ASSERT(i_pLen <= UINT64_BIT_LEN); // i_pLen length must be valid
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500133
134 // Get a bit string for the pattern subset (right justified).
Ben Tynera8126fd2019-08-01 19:40:07 -0500135 // Note that we cannot use a BitStringBuffer here because this function
136 // could be used in the constructor of BitStringBuffer, which could causes
137 // an infinite loop.
138 uint8_t a[sizeof(i_pattern)] = {};
Zane Shelleyfe27b652019-10-28 11:33:07 -0500139 BitString bs { sizeof(i_pattern) * 8, a };
Ben Tynera8126fd2019-08-01 19:40:07 -0500140 bs.setFieldRight(0, i_pLen, i_pattern);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500141
142 // Iterate the range in chunks the size of i_pLen.
Zane Shelleyd0af3582019-09-19 10:48:59 -0500143 uint64_t endPos = i_sPos + i_sLen;
Zane Shelley83da2452019-10-25 15:45:34 -0500144 for (uint64_t pos = i_sPos; pos < endPos; pos += i_pLen)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500145 {
146 // The true chunk size is either i_pLen or the leftovers at the end.
Zane Shelley83da2452019-10-25 15:45:34 -0500147 uint64_t len = std::min(i_pLen, endPos - pos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500148
Ben Tynera8126fd2019-08-01 19:40:07 -0500149 // Get this chunk's pattern value, truncate (right justified) if needed.
Zane Shelley83da2452019-10-25 15:45:34 -0500150 uint64_t pattern = bs.getFieldRight(0, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500151
152 // Set the pattern in this string.
Zane Shelley83da2452019-10-25 15:45:34 -0500153 setFieldRight(pos, len, pattern);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500154 }
155}
156
157//------------------------------------------------------------------------------
158
Zane Shelleyfe27b652019-10-28 11:33:07 -0500159void BitString::setString(const BitString& i_sStr, uint64_t i_sPos,
Zane Shelley83da2452019-10-25 15:45:34 -0500160 uint64_t i_sLen, uint64_t i_dPos)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500161{
162 // Ensure the source parameters are valid.
Zane Shelley83da2452019-10-25 15:45:34 -0500163 HEI_ASSERT(nullptr != i_sStr.getBufAddr());
164 HEI_ASSERT(0 < i_sLen); // at least one bit to copy
165 HEI_ASSERT(i_sPos + i_sLen <= i_sStr.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500166
167 // Ensure the destination has at least one bit available to copy.
Zane Shelley83da2452019-10-25 15:45:34 -0500168 HEI_ASSERT(nullptr != getBufAddr());
169 HEI_ASSERT(i_dPos < getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500170
171 // If the source length is greater than the destination length than the
172 // extra source bits are ignored.
Zane Shelley83da2452019-10-25 15:45:34 -0500173 uint64_t actLen = std::min(i_sLen, getBitLen() - i_dPos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500174
175 // The bit strings may be in overlapping memory spaces. So we need to copy
176 // the data in the correct direction to prevent overlapping.
Zane Shelleyd0af3582019-09-19 10:48:59 -0500177 uint64_t sRelOffset = 0, dRelOffset = 0;
Zane Shelleyfe27b652019-10-28 11:33:07 -0500178 uint8_t* sRelAddr = i_sStr.getRelativePosition(sRelOffset, i_sPos);
179 uint8_t* dRelAddr = getRelativePosition(dRelOffset, i_dPos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500180
181 // Copy the data.
Zane Shelley83da2452019-10-25 15:45:34 -0500182 if ((dRelAddr == sRelAddr) && (dRelOffset == sRelOffset))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500183 {
184 // Do nothing. The source and destination are the same.
185 }
Zane Shelley83da2452019-10-25 15:45:34 -0500186 else if ((dRelAddr < sRelAddr) ||
187 ((dRelAddr == sRelAddr) && (dRelOffset < sRelOffset)))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500188 {
189 // Copy the data forward.
Zane Shelley83da2452019-10-25 15:45:34 -0500190 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500191 {
Zane Shelley83da2452019-10-25 15:45:34 -0500192 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500193
Zane Shelley83da2452019-10-25 15:45:34 -0500194 uint64_t value = i_sStr.getFieldRight(i_sPos + pos, len);
195 setFieldRight(i_dPos + pos, len, value);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500196 }
197 }
198 else // Copy the data backwards.
199 {
Ben Tynera8126fd2019-08-01 19:40:07 -0500200 // Get the first position of the last chunk (byte aligned).
Zane Shelley83da2452019-10-25 15:45:34 -0500201 uint64_t lastPos = ((actLen - 1) / UINT64_BIT_LEN) * UINT64_BIT_LEN;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500202
203 // Start with the last chunk and work backwards.
Zane Shelley83da2452019-10-25 15:45:34 -0500204 for (int32_t pos = lastPos; 0 <= pos; pos -= UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500205 {
Zane Shelley83da2452019-10-25 15:45:34 -0500206 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
207 uint64_t value = i_sStr.getFieldRight(i_sPos + pos, len);
208 setFieldRight(i_dPos + pos, len, value);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500209 }
210 }
211}
212
213//------------------------------------------------------------------------------
214
Zane Shelleyfe27b652019-10-28 11:33:07 -0500215void BitString::maskString(const BitString& i_mask)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500216{
217 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500218 uint64_t actLen = std::min(getBitLen(), i_mask.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500219
Zane Shelley83da2452019-10-25 15:45:34 -0500220 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500221 {
Zane Shelley83da2452019-10-25 15:45:34 -0500222 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500223
Zane Shelley83da2452019-10-25 15:45:34 -0500224 uint64_t dVal = getFieldRight(pos, len);
225 uint64_t sVal = i_mask.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500226
Zane Shelley83da2452019-10-25 15:45:34 -0500227 setFieldRight(pos, len, dVal & ~sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500228 }
229}
230
231//------------------------------------------------------------------------------
232
Zane Shelleyfe27b652019-10-28 11:33:07 -0500233bool BitString::isEqual(const BitString& i_str) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500234{
Zane Shelley83da2452019-10-25 15:45:34 -0500235 if (getBitLen() != i_str.getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500236 return false; // size not equal
237
Zane Shelley83da2452019-10-25 15:45:34 -0500238 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500239 {
Zane Shelley83da2452019-10-25 15:45:34 -0500240 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500241
Zane Shelley83da2452019-10-25 15:45:34 -0500242 if (getFieldRight(pos, len) != i_str.getFieldRight(pos, len))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500243 return false; // bit strings do not match
244 }
245
246 return true; // bit strings match
247}
248
249//------------------------------------------------------------------------------
250
251bool BitString::isZero() const
252{
Zane Shelley83da2452019-10-25 15:45:34 -0500253 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500254 {
Zane Shelley83da2452019-10-25 15:45:34 -0500255 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500256
Zane Shelley83da2452019-10-25 15:45:34 -0500257 if (0 != getFieldRight(pos, len))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500258 return false; // something is non-zero
259 }
260
261 return true; // everything was zero
262}
263
264//------------------------------------------------------------------------------
265
Zane Shelley83da2452019-10-25 15:45:34 -0500266uint64_t BitString::getSetCount(uint64_t i_pos, uint64_t i_len) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500267{
Zane Shelleyd0af3582019-09-19 10:48:59 -0500268 uint64_t endPos = i_pos + i_len;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500269
Zane Shelley83da2452019-10-25 15:45:34 -0500270 HEI_ASSERT(endPos <= getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500271
Zane Shelleyd0af3582019-09-19 10:48:59 -0500272 uint64_t count = 0;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500273
Zane Shelley83da2452019-10-25 15:45:34 -0500274 for (uint64_t i = i_pos; i < endPos; i++)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500275 {
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500276 if (isBitSet(i))
277 count++;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500278 }
279
280 return count;
281}
282
283//------------------------------------------------------------------------------
284
285BitStringBuffer BitString::operator~() const
286{
Zane Shelley83da2452019-10-25 15:45:34 -0500287 BitStringBuffer bsb(getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500288
Zane Shelley83da2452019-10-25 15:45:34 -0500289 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500290 {
Zane Shelley83da2452019-10-25 15:45:34 -0500291 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500292
Zane Shelley83da2452019-10-25 15:45:34 -0500293 uint64_t dVal = getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500294
Zane Shelley83da2452019-10-25 15:45:34 -0500295 bsb.setFieldRight(pos, len, ~dVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500296 }
297
298 return bsb;
299}
300
301//------------------------------------------------------------------------------
302
Zane Shelleyfe27b652019-10-28 11:33:07 -0500303BitStringBuffer BitString::operator&(const BitString& i_bs) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500304{
305 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500306 uint64_t actLen = std::min(getBitLen(), i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500307
Zane Shelley83da2452019-10-25 15:45:34 -0500308 BitStringBuffer bsb(actLen);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500309
Zane Shelley83da2452019-10-25 15:45:34 -0500310 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500311 {
Zane Shelley83da2452019-10-25 15:45:34 -0500312 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500313
Zane Shelley83da2452019-10-25 15:45:34 -0500314 uint64_t dVal = getFieldRight(pos, len);
315 uint64_t sVal = i_bs.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500316
Zane Shelley83da2452019-10-25 15:45:34 -0500317 bsb.setFieldRight(pos, len, dVal & sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500318 }
319
320 return bsb;
321}
322
323//------------------------------------------------------------------------------
324
Zane Shelleyfe27b652019-10-28 11:33:07 -0500325BitStringBuffer BitString::operator|(const BitString& i_bs) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500326{
327 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500328 uint64_t actLen = std::min(getBitLen(), i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500329
Zane Shelley83da2452019-10-25 15:45:34 -0500330 BitStringBuffer bsb(actLen);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500331
Zane Shelley83da2452019-10-25 15:45:34 -0500332 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500333 {
Zane Shelley83da2452019-10-25 15:45:34 -0500334 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500335
Zane Shelley83da2452019-10-25 15:45:34 -0500336 uint64_t dVal = getFieldRight(pos, len);
337 uint64_t sVal = i_bs.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500338
Zane Shelley83da2452019-10-25 15:45:34 -0500339 bsb.setFieldRight(pos, len, dVal | sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500340 }
341
342 return bsb;
343}
344
345//------------------------------------------------------------------------------
346
Zane Shelley83da2452019-10-25 15:45:34 -0500347BitStringBuffer BitString::operator>>(uint64_t i_shift) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500348{
Zane Shelley83da2452019-10-25 15:45:34 -0500349 BitStringBuffer bsb(getBitLen()); // default all zeros
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500350
Zane Shelley83da2452019-10-25 15:45:34 -0500351 if (i_shift < getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500352 {
353 // bso overlays bsb, containing the shifted offset.
Zane Shelley83da2452019-10-25 15:45:34 -0500354 BitString bso (bsb.getBitLen() - i_shift, bsb.getBufAddr(), i_shift);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500355
356 // Copy this into bso.
Zane Shelley83da2452019-10-25 15:45:34 -0500357 bso.setString(*this);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500358 }
359
360 return bsb;
361}
362
363//------------------------------------------------------------------------------
364
Zane Shelley83da2452019-10-25 15:45:34 -0500365BitStringBuffer BitString::operator<<(uint64_t i_shift) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500366{
Zane Shelley83da2452019-10-25 15:45:34 -0500367 BitStringBuffer bsb(getBitLen()); // default all zeros
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500368
Zane Shelley83da2452019-10-25 15:45:34 -0500369 if (i_shift < getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500370 {
371 // bso overlays *this, containing the shifted offset.
Zane Shelley83da2452019-10-25 15:45:34 -0500372 BitString bso (this->getBitLen() - i_shift, this->getBufAddr(),
373 i_shift);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500374
375 // Copy bso into bsb.
Zane Shelley83da2452019-10-25 15:45:34 -0500376 bsb.setString(bso);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500377 }
378
379 return bsb;
380}
381
382//------------------------------------------------------------------------------
383
Zane Shelleyfe27b652019-10-28 11:33:07 -0500384uint8_t* BitString::getRelativePosition(uint64_t& o_relPos,
385 uint64_t i_absPos) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500386{
Zane Shelley83da2452019-10-25 15:45:34 -0500387 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
388 HEI_ASSERT(i_absPos < getBitLen()); // must be a valid position
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500389
Ben Tynera8126fd2019-08-01 19:40:07 -0500390 o_relPos = (i_absPos + iv_offset) % UINT8_BIT_LEN;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500391
Zane Shelleyfe27b652019-10-28 11:33:07 -0500392 return ((uint8_t*)iv_bufAddr + ((i_absPos + iv_offset) / UINT8_BIT_LEN));
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500393}
394
395//##############################################################################
396// BitStringBuffer class
397//##############################################################################
398
Zane Shelley83da2452019-10-25 15:45:34 -0500399BitStringBuffer::BitStringBuffer(uint64_t i_bitLen) :
400 BitString(i_bitLen, nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500401{
402 initBuffer();
403}
404
405//------------------------------------------------------------------------------
406
407BitStringBuffer::~BitStringBuffer()
408{
Zane Shelleyfe27b652019-10-28 11:33:07 -0500409 delete [] (uint8_t*)getBufAddr();
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500410}
411
412//------------------------------------------------------------------------------
413
Zane Shelleyfe27b652019-10-28 11:33:07 -0500414BitStringBuffer::BitStringBuffer(const BitString& i_bs) :
Zane Shelley83da2452019-10-25 15:45:34 -0500415 BitString(i_bs.getBitLen(), nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500416{
417 initBuffer();
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500418 if (!i_bs.isZero())
419 {
420 setString(i_bs);
421 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500422}
423
424//------------------------------------------------------------------------------
425
Zane Shelleyfe27b652019-10-28 11:33:07 -0500426BitStringBuffer::BitStringBuffer(const BitStringBuffer& i_bsb) :
Zane Shelley83da2452019-10-25 15:45:34 -0500427 BitString(i_bsb.getBitLen(), nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500428{
429 initBuffer();
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500430 if (!i_bsb.isZero())
431 {
432 setString(i_bsb);
433 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500434}
435
436//------------------------------------------------------------------------------
437
Zane Shelleyfe27b652019-10-28 11:33:07 -0500438BitStringBuffer& BitStringBuffer::operator=(const BitString& i_bs)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500439{
440 // The initBuffer() function will deallocate the buffer as well, however we
441 // also need to deallocate the buffer here before we set the length.
Zane Shelleyfe27b652019-10-28 11:33:07 -0500442 delete [] (uint8_t*)getBufAddr();
Zane Shelley83da2452019-10-25 15:45:34 -0500443 setBufAddr(nullptr);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500444
Zane Shelley83da2452019-10-25 15:45:34 -0500445 setBitLen(i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500446 initBuffer();
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500447 if (!i_bs.isZero())
448 {
449 setString(i_bs);
450 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500451
452 return *this;
453}
454
455//------------------------------------------------------------------------------
456
Zane Shelleyfe27b652019-10-28 11:33:07 -0500457BitStringBuffer& BitStringBuffer::operator=(const BitStringBuffer& i_bsb)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500458{
Zane Shelley83da2452019-10-25 15:45:34 -0500459 if (this != &i_bsb) // Check for assignment to self
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500460 {
461 // The initBuffer() function will deallocate the buffer as well, however
462 // we also need to deallocate the buffer here before we set the length.
Zane Shelleyfe27b652019-10-28 11:33:07 -0500463 delete [] (uint8_t*)getBufAddr();
Zane Shelley83da2452019-10-25 15:45:34 -0500464 setBufAddr(nullptr);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500465
Zane Shelley83da2452019-10-25 15:45:34 -0500466 setBitLen(i_bsb.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500467 initBuffer();
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500468 if (!i_bsb.isZero())
469 {
470 setString(i_bsb);
471 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500472 }
473
474 return *this;
475}
476
477//------------------------------------------------------------------------------
478
479void BitStringBuffer::initBuffer()
480{
481 // Deallocate the current buffer.
Zane Shelleyfe27b652019-10-28 11:33:07 -0500482 delete [] (uint8_t*)getBufAddr();
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500483
Ben Tynera8126fd2019-08-01 19:40:07 -0500484 // create new buffer, initialized to 0's
Zane Shelley83da2452019-10-25 15:45:34 -0500485 setBufAddr(new uint8_t[ getMinBytes(getBitLen()) ]());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500486}
487
Zane Shelley871adec2019-07-30 11:01:39 -0500488} // end namespace libhei