blob: 5313d7aad24f7258e82d0db5f36ca379a4119f17 [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
Zane Shelley995be6c2021-02-24 15:48:55 -06005#include <util/hei_bit_string.hpp>
Zane Shelleyd5073512021-01-14 12:51:18 -06006#include <util/hei_includes.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
Zane Shelley72fd2e42022-11-12 12:14:53 -060013// ##############################################################################
14// BitString class
15// ##############################################################################
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050016
Ben Tynera8126fd2019-08-01 19:40:07 -050017// number of bits in a uint64_t
Ben Tyner7c796052020-02-03 19:00:37 -060018const 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
Ben Tyner7c796052020-02-03 19:00:37 -060020const 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 Shelley7c8faa12019-10-28 22:26:28 -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 Shelley7c8faa12019-10-28 22:26:28 -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 Shelley7c8faa12019-10-28 22:26:28 -0500104
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500105 uint8_t bf_l = *relAddr;
106 bf_l >>= bf_l_shift;
107 bf_l <<= bf_l_shift;
Zane Shelley7c8faa12019-10-28 22:26:28 -0500108
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500109 uint8_t bf_r = *relAddr;
110 bf_r <<= bf_r_shift;
111 bf_r >>= bf_r_shift;
Ben Tynera8126fd2019-08-01 19:40:07 -0500112
113 // Combine all three parts of the byte and write it out to memory.
114 *relAddr = bf_l | bf | bf_r;
115
116 // Check for any remaining bits after this target byte.
Zane Shelley83da2452019-10-25 15:45:34 -0500117 if (0 != remain_len)
Ben Tynera8126fd2019-08-01 19:40:07 -0500118 {
119 // Recursively call this function on the remaining bits.
Zane Shelley83da2452019-10-25 15:45:34 -0500120 setFieldLeft(i_pos + bf_len, remain_len, i_val << bf_len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500121 }
122}
123
124//------------------------------------------------------------------------------
125
Zane Shelley83da2452019-10-25 15:45:34 -0500126void BitString::setPattern(uint64_t i_sPos, uint64_t i_sLen, uint64_t i_pattern,
127 uint64_t i_pLen)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500128{
Ben Tynera8126fd2019-08-01 19:40:07 -0500129 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
130 HEI_ASSERT(0 < i_sLen); // must have at least one bit
131 HEI_ASSERT(i_sPos + i_sLen <= getBitLen()); // field must be within range
132 HEI_ASSERT(0 < i_pLen); // must have at least one bit
Zane Shelley83da2452019-10-25 15:45:34 -0500133 HEI_ASSERT(i_pLen <= UINT64_BIT_LEN); // i_pLen length must be valid
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500134
135 // Get a bit string for the pattern subset (right justified).
Ben Tynera8126fd2019-08-01 19:40:07 -0500136 // Note that we cannot use a BitStringBuffer here because this function
137 // could be used in the constructor of BitStringBuffer, which could causes
138 // an infinite loop.
139 uint8_t a[sizeof(i_pattern)] = {};
Zane Shelleyc4771992019-10-28 22:01:49 -0500140 BitString bs{sizeof(i_pattern) * 8, a};
Ben Tynera8126fd2019-08-01 19:40:07 -0500141 bs.setFieldRight(0, i_pLen, i_pattern);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500142
143 // Iterate the range in chunks the size of i_pLen.
Zane Shelleyd0af3582019-09-19 10:48:59 -0500144 uint64_t endPos = i_sPos + i_sLen;
Zane Shelley83da2452019-10-25 15:45:34 -0500145 for (uint64_t pos = i_sPos; pos < endPos; pos += i_pLen)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500146 {
147 // The true chunk size is either i_pLen or the leftovers at the end.
Zane Shelley83da2452019-10-25 15:45:34 -0500148 uint64_t len = std::min(i_pLen, endPos - pos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500149
Ben Tynera8126fd2019-08-01 19:40:07 -0500150 // Get this chunk's pattern value, truncate (right justified) if needed.
Zane Shelley83da2452019-10-25 15:45:34 -0500151 uint64_t pattern = bs.getFieldRight(0, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500152
153 // Set the pattern in this string.
Zane Shelley83da2452019-10-25 15:45:34 -0500154 setFieldRight(pos, len, pattern);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500155 }
156}
157
158//------------------------------------------------------------------------------
159
Zane Shelleyfe27b652019-10-28 11:33:07 -0500160void BitString::setString(const BitString& i_sStr, uint64_t i_sPos,
Zane Shelley83da2452019-10-25 15:45:34 -0500161 uint64_t i_sLen, uint64_t i_dPos)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500162{
163 // Ensure the source parameters are valid.
Zane Shelley83da2452019-10-25 15:45:34 -0500164 HEI_ASSERT(nullptr != i_sStr.getBufAddr());
165 HEI_ASSERT(0 < i_sLen); // at least one bit to copy
166 HEI_ASSERT(i_sPos + i_sLen <= i_sStr.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500167
168 // Ensure the destination has at least one bit available to copy.
Zane Shelley83da2452019-10-25 15:45:34 -0500169 HEI_ASSERT(nullptr != getBufAddr());
170 HEI_ASSERT(i_dPos < getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500171
172 // If the source length is greater than the destination length than the
173 // extra source bits are ignored.
Zane Shelley83da2452019-10-25 15:45:34 -0500174 uint64_t actLen = std::min(i_sLen, getBitLen() - i_dPos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500175
176 // The bit strings may be in overlapping memory spaces. So we need to copy
177 // the data in the correct direction to prevent overlapping.
Zane Shelleyd0af3582019-09-19 10:48:59 -0500178 uint64_t sRelOffset = 0, dRelOffset = 0;
Zane Shelleyfe27b652019-10-28 11:33:07 -0500179 uint8_t* sRelAddr = i_sStr.getRelativePosition(sRelOffset, i_sPos);
Zane Shelley7c8faa12019-10-28 22:26:28 -0500180 uint8_t* dRelAddr = getRelativePosition(dRelOffset, i_dPos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500181
182 // Copy the data.
Zane Shelley83da2452019-10-25 15:45:34 -0500183 if ((dRelAddr == sRelAddr) && (dRelOffset == sRelOffset))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500184 {
185 // Do nothing. The source and destination are the same.
186 }
Zane Shelley83da2452019-10-25 15:45:34 -0500187 else if ((dRelAddr < sRelAddr) ||
188 ((dRelAddr == sRelAddr) && (dRelOffset < sRelOffset)))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500189 {
190 // Copy the data forward.
Zane Shelley83da2452019-10-25 15:45:34 -0500191 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500192 {
Zane Shelley83da2452019-10-25 15:45:34 -0500193 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500194
Zane Shelley83da2452019-10-25 15:45:34 -0500195 uint64_t value = i_sStr.getFieldRight(i_sPos + pos, len);
196 setFieldRight(i_dPos + pos, len, value);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500197 }
198 }
199 else // Copy the data backwards.
200 {
Ben Tynera8126fd2019-08-01 19:40:07 -0500201 // Get the first position of the last chunk (byte aligned).
Zane Shelley83da2452019-10-25 15:45:34 -0500202 uint64_t lastPos = ((actLen - 1) / UINT64_BIT_LEN) * UINT64_BIT_LEN;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500203
204 // Start with the last chunk and work backwards.
Zane Shelley83da2452019-10-25 15:45:34 -0500205 for (int32_t pos = lastPos; 0 <= pos; pos -= UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500206 {
Zane Shelley7c8faa12019-10-28 22:26:28 -0500207 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelley83da2452019-10-25 15:45:34 -0500208 uint64_t value = i_sStr.getFieldRight(i_sPos + pos, len);
209 setFieldRight(i_dPos + pos, len, value);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500210 }
211 }
212}
213
214//------------------------------------------------------------------------------
215
Zane Shelleyfe27b652019-10-28 11:33:07 -0500216void BitString::maskString(const BitString& i_mask)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500217{
218 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500219 uint64_t actLen = std::min(getBitLen(), i_mask.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500220
Zane Shelley83da2452019-10-25 15:45:34 -0500221 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500222 {
Zane Shelley83da2452019-10-25 15:45:34 -0500223 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500224
Zane Shelley7c8faa12019-10-28 22:26:28 -0500225 uint64_t dVal = getFieldRight(pos, len);
Zane Shelley83da2452019-10-25 15:45:34 -0500226 uint64_t sVal = i_mask.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500227
Zane Shelley83da2452019-10-25 15:45:34 -0500228 setFieldRight(pos, len, dVal & ~sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500229 }
230}
231
232//------------------------------------------------------------------------------
233
Zane Shelleyfe27b652019-10-28 11:33:07 -0500234bool BitString::isEqual(const BitString& i_str) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500235{
Zane Shelley83da2452019-10-25 15:45:34 -0500236 if (getBitLen() != i_str.getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500237 return false; // size not equal
238
Zane Shelley83da2452019-10-25 15:45:34 -0500239 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500240 {
Zane Shelley83da2452019-10-25 15:45:34 -0500241 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500242
Zane Shelley83da2452019-10-25 15:45:34 -0500243 if (getFieldRight(pos, len) != i_str.getFieldRight(pos, len))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500244 return false; // bit strings do not match
245 }
246
247 return true; // bit strings match
248}
249
250//------------------------------------------------------------------------------
251
252bool BitString::isZero() const
253{
Zane Shelley83da2452019-10-25 15:45:34 -0500254 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500255 {
Zane Shelley83da2452019-10-25 15:45:34 -0500256 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500257
Zane Shelley83da2452019-10-25 15:45:34 -0500258 if (0 != getFieldRight(pos, len))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500259 return false; // something is non-zero
260 }
261
262 return true; // everything was zero
263}
264
265//------------------------------------------------------------------------------
266
Zane Shelley83da2452019-10-25 15:45:34 -0500267uint64_t BitString::getSetCount(uint64_t i_pos, uint64_t i_len) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500268{
Zane Shelleyd0af3582019-09-19 10:48:59 -0500269 uint64_t endPos = i_pos + i_len;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500270
Zane Shelley83da2452019-10-25 15:45:34 -0500271 HEI_ASSERT(endPos <= getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500272
Zane Shelleyd0af3582019-09-19 10:48:59 -0500273 uint64_t count = 0;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500274
Zane Shelley83da2452019-10-25 15:45:34 -0500275 for (uint64_t i = i_pos; i < endPos; i++)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500276 {
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500277 if (isBitSet(i))
278 count++;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500279 }
280
281 return count;
282}
283
284//------------------------------------------------------------------------------
285
Zane Shelleyec06f822019-12-05 22:23:19 -0600286bool BitString::operator<(const BitString& i_str) const
287{
Zane Shelley6eb61902020-05-15 22:25:58 -0500288 if (getBitLen() < i_str.getBitLen())
Zane Shelleyec06f822019-12-05 22:23:19 -0600289 {
Zane Shelley6eb61902020-05-15 22:25:58 -0500290 return true;
291 }
292 else if (getBitLen() == i_str.getBitLen())
293 {
294 // Can only compare the bit strings if the length is the same.
295 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyec06f822019-12-05 22:23:19 -0600296 {
Zane Shelley6eb61902020-05-15 22:25:58 -0500297 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
298
299 auto l_str = getFieldRight(pos, len);
300 auto r_str = i_str.getFieldRight(pos, len);
301
302 if (l_str < r_str)
303 {
304 return true;
305 }
306 // The loop can only continue if the values are equal.
307 else if (l_str > r_str)
308 {
309 return false;
310 }
Zane Shelleyec06f822019-12-05 22:23:19 -0600311 }
312 }
313
314 return false;
315}
316
317//------------------------------------------------------------------------------
318
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500319BitStringBuffer BitString::operator~() const
320{
Zane Shelley83da2452019-10-25 15:45:34 -0500321 BitStringBuffer bsb(getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500322
Zane Shelley83da2452019-10-25 15:45:34 -0500323 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500324 {
Zane Shelley83da2452019-10-25 15:45:34 -0500325 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500326
Zane Shelley83da2452019-10-25 15:45:34 -0500327 uint64_t dVal = getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500328
Zane Shelley83da2452019-10-25 15:45:34 -0500329 bsb.setFieldRight(pos, len, ~dVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500330 }
331
332 return bsb;
333}
334
335//------------------------------------------------------------------------------
336
Zane Shelleyfe27b652019-10-28 11:33:07 -0500337BitStringBuffer BitString::operator&(const BitString& i_bs) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500338{
339 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500340 uint64_t actLen = std::min(getBitLen(), i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500341
Zane Shelley83da2452019-10-25 15:45:34 -0500342 BitStringBuffer bsb(actLen);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500343
Zane Shelley83da2452019-10-25 15:45:34 -0500344 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500345 {
Zane Shelley83da2452019-10-25 15:45:34 -0500346 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500347
Zane Shelley7c8faa12019-10-28 22:26:28 -0500348 uint64_t dVal = getFieldRight(pos, len);
Zane Shelley83da2452019-10-25 15:45:34 -0500349 uint64_t sVal = i_bs.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500350
Zane Shelley83da2452019-10-25 15:45:34 -0500351 bsb.setFieldRight(pos, len, dVal & sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500352 }
353
354 return bsb;
355}
356
357//------------------------------------------------------------------------------
358
Zane Shelleyfe27b652019-10-28 11:33:07 -0500359BitStringBuffer BitString::operator|(const BitString& i_bs) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500360{
361 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500362 uint64_t actLen = std::min(getBitLen(), i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500363
Zane Shelley83da2452019-10-25 15:45:34 -0500364 BitStringBuffer bsb(actLen);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500365
Zane Shelley83da2452019-10-25 15:45:34 -0500366 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500367 {
Zane Shelley83da2452019-10-25 15:45:34 -0500368 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500369
Zane Shelley7c8faa12019-10-28 22:26:28 -0500370 uint64_t dVal = getFieldRight(pos, len);
Zane Shelley83da2452019-10-25 15:45:34 -0500371 uint64_t sVal = i_bs.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500372
Zane Shelley83da2452019-10-25 15:45:34 -0500373 bsb.setFieldRight(pos, len, dVal | sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500374 }
375
376 return bsb;
377}
378
379//------------------------------------------------------------------------------
380
Zane Shelley83da2452019-10-25 15:45:34 -0500381BitStringBuffer BitString::operator>>(uint64_t i_shift) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500382{
Zane Shelley83da2452019-10-25 15:45:34 -0500383 BitStringBuffer bsb(getBitLen()); // default all zeros
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500384
Zane Shelley83da2452019-10-25 15:45:34 -0500385 if (i_shift < getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500386 {
387 // bso overlays bsb, containing the shifted offset.
Zane Shelleyc4771992019-10-28 22:01:49 -0500388 BitString bso(bsb.getBitLen() - i_shift, bsb.getBufAddr(), i_shift);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500389
390 // Copy this into bso.
Zane Shelley83da2452019-10-25 15:45:34 -0500391 bso.setString(*this);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500392 }
393
394 return bsb;
395}
396
397//------------------------------------------------------------------------------
398
Zane Shelley83da2452019-10-25 15:45:34 -0500399BitStringBuffer BitString::operator<<(uint64_t i_shift) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500400{
Zane Shelley83da2452019-10-25 15:45:34 -0500401 BitStringBuffer bsb(getBitLen()); // default all zeros
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500402
Zane Shelley83da2452019-10-25 15:45:34 -0500403 if (i_shift < getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500404 {
405 // bso overlays *this, containing the shifted offset.
Zane Shelleyc4771992019-10-28 22:01:49 -0500406 BitString bso(this->getBitLen() - i_shift, this->getBufAddr(), i_shift);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500407
408 // Copy bso into bsb.
Zane Shelley83da2452019-10-25 15:45:34 -0500409 bsb.setString(bso);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500410 }
411
412 return bsb;
413}
414
415//------------------------------------------------------------------------------
416
Zane Shelleyfe27b652019-10-28 11:33:07 -0500417uint8_t* BitString::getRelativePosition(uint64_t& o_relPos,
Zane Shelley7c8faa12019-10-28 22:26:28 -0500418 uint64_t i_absPos) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500419{
Zane Shelley83da2452019-10-25 15:45:34 -0500420 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
421 HEI_ASSERT(i_absPos < getBitLen()); // must be a valid position
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500422
Ben Tynera8126fd2019-08-01 19:40:07 -0500423 o_relPos = (i_absPos + iv_offset) % UINT8_BIT_LEN;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500424
Zane Shelley5a78fa82022-09-16 16:49:58 -0500425 return (iv_bufAddr + ((i_absPos + iv_offset) / UINT8_BIT_LEN));
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500426}
427
Zane Shelley72fd2e42022-11-12 12:14:53 -0600428// ##############################################################################
429// BitStringBuffer class
430// ##############################################################################
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500431
Zane Shelley83da2452019-10-25 15:45:34 -0500432BitStringBuffer::BitStringBuffer(uint64_t i_bitLen) :
433 BitString(i_bitLen, nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500434{
435 initBuffer();
436}
437
438//------------------------------------------------------------------------------
439
440BitStringBuffer::~BitStringBuffer()
441{
Zane Shelley5a78fa82022-09-16 16:49:58 -0500442 delete[] getBufAddr();
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500443}
444
445//------------------------------------------------------------------------------
446
Zane Shelleyfe27b652019-10-28 11:33:07 -0500447BitStringBuffer::BitStringBuffer(const BitString& i_bs) :
Zane Shelley83da2452019-10-25 15:45:34 -0500448 BitString(i_bs.getBitLen(), nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500449{
450 initBuffer();
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500451 if (!i_bs.isZero())
452 {
453 setString(i_bs);
454 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500455}
456
457//------------------------------------------------------------------------------
458
Zane Shelleyfe27b652019-10-28 11:33:07 -0500459BitStringBuffer::BitStringBuffer(const BitStringBuffer& i_bsb) :
Zane Shelley83da2452019-10-25 15:45:34 -0500460 BitString(i_bsb.getBitLen(), nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500461{
462 initBuffer();
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500463 if (!i_bsb.isZero())
464 {
465 setString(i_bsb);
466 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500467}
468
469//------------------------------------------------------------------------------
470
Zane Shelleyfe27b652019-10-28 11:33:07 -0500471BitStringBuffer& BitStringBuffer::operator=(const BitString& i_bs)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500472{
473 // The initBuffer() function will deallocate the buffer as well, however we
474 // also need to deallocate the buffer here before we set the length.
Zane Shelley5a78fa82022-09-16 16:49:58 -0500475 delete[] getBufAddr();
Zane Shelley83da2452019-10-25 15:45:34 -0500476 setBufAddr(nullptr);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500477
Zane Shelley83da2452019-10-25 15:45:34 -0500478 setBitLen(i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500479 initBuffer();
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500480 if (!i_bs.isZero())
481 {
482 setString(i_bs);
483 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500484
485 return *this;
486}
487
488//------------------------------------------------------------------------------
489
Zane Shelleyfe27b652019-10-28 11:33:07 -0500490BitStringBuffer& BitStringBuffer::operator=(const BitStringBuffer& i_bsb)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500491{
Zane Shelley83da2452019-10-25 15:45:34 -0500492 if (this != &i_bsb) // Check for assignment to self
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500493 {
494 // The initBuffer() function will deallocate the buffer as well, however
495 // we also need to deallocate the buffer here before we set the length.
Zane Shelley5a78fa82022-09-16 16:49:58 -0500496 delete[] getBufAddr();
Zane Shelley83da2452019-10-25 15:45:34 -0500497 setBufAddr(nullptr);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500498
Zane Shelley83da2452019-10-25 15:45:34 -0500499 setBitLen(i_bsb.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500500 initBuffer();
Zane Shelley7f7a42d2019-10-28 13:28:31 -0500501 if (!i_bsb.isZero())
502 {
503 setString(i_bsb);
504 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500505 }
506
507 return *this;
508}
509
510//------------------------------------------------------------------------------
511
512void BitStringBuffer::initBuffer()
513{
514 // Deallocate the current buffer.
Zane Shelley5a78fa82022-09-16 16:49:58 -0500515 delete[] getBufAddr();
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500516
Ben Tynera8126fd2019-08-01 19:40:07 -0500517 // create new buffer, initialized to 0's
Zane Shelleyc4771992019-10-28 22:01:49 -0500518 setBufAddr(new uint8_t[getMinBytes(getBitLen())]());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500519}
520
Zane Shelley871adec2019-07-30 11:01:39 -0500521} // end namespace libhei