blob: 24de34e444ccf6d66e45fa7a725e0ba23f98b8fd [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 Shelley83da2452019-10-25 15:45:34 -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 Shelley83da2452019-10-25 15:45:34 -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;
Ben Tynera8126fd2019-08-01 19:40:07 -0500104 uint8_t bf_l = *relAddr; bf_l >>= bf_l_shift; bf_l <<= bf_l_shift;
105 uint8_t bf_r = *relAddr; bf_r <<= bf_r_shift; bf_r >>= bf_r_shift;
106
107 // Combine all three parts of the byte and write it out to memory.
108 *relAddr = bf_l | bf | bf_r;
109
110 // Check for any remaining bits after this target byte.
Zane Shelley83da2452019-10-25 15:45:34 -0500111 if (0 != remain_len)
Ben Tynera8126fd2019-08-01 19:40:07 -0500112 {
113 // Recursively call this function on the remaining bits.
Zane Shelley83da2452019-10-25 15:45:34 -0500114 setFieldLeft(i_pos + bf_len, remain_len, i_val << bf_len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500115 }
116}
117
118//------------------------------------------------------------------------------
119
Zane Shelley83da2452019-10-25 15:45:34 -0500120void BitString::setPattern(uint64_t i_sPos, uint64_t i_sLen, uint64_t i_pattern,
121 uint64_t i_pLen)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500122{
Ben Tynera8126fd2019-08-01 19:40:07 -0500123
124 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
125 HEI_ASSERT(0 < i_sLen); // must have at least one bit
126 HEI_ASSERT(i_sPos + i_sLen <= getBitLen()); // field must be within range
127 HEI_ASSERT(0 < i_pLen); // must have at least one bit
Zane Shelley83da2452019-10-25 15:45:34 -0500128 HEI_ASSERT(i_pLen <= UINT64_BIT_LEN); // i_pLen length must be valid
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500129
130 // Get a bit string for the pattern subset (right justified).
Ben Tynera8126fd2019-08-01 19:40:07 -0500131 // Note that we cannot use a BitStringBuffer here because this function
132 // could be used in the constructor of BitStringBuffer, which could causes
133 // an infinite loop.
134 uint8_t a[sizeof(i_pattern)] = {};
135 BitString bs { sizeof(i_pattern)*8, a };
136 bs.setFieldRight(0, i_pLen, i_pattern);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500137
138 // Iterate the range in chunks the size of i_pLen.
Zane Shelleyd0af3582019-09-19 10:48:59 -0500139 uint64_t endPos = i_sPos + i_sLen;
Zane Shelley83da2452019-10-25 15:45:34 -0500140 for (uint64_t pos = i_sPos; pos < endPos; pos += i_pLen)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500141 {
142 // The true chunk size is either i_pLen or the leftovers at the end.
Zane Shelley83da2452019-10-25 15:45:34 -0500143 uint64_t len = std::min(i_pLen, endPos - pos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500144
Ben Tynera8126fd2019-08-01 19:40:07 -0500145 // Get this chunk's pattern value, truncate (right justified) if needed.
Zane Shelley83da2452019-10-25 15:45:34 -0500146 uint64_t pattern = bs.getFieldRight(0, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500147
148 // Set the pattern in this string.
Zane Shelley83da2452019-10-25 15:45:34 -0500149 setFieldRight(pos, len, pattern);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500150 }
151}
152
153//------------------------------------------------------------------------------
154
Zane Shelley83da2452019-10-25 15:45:34 -0500155void BitString::setString(const BitString & i_sStr, uint64_t i_sPos,
156 uint64_t i_sLen, uint64_t i_dPos)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500157{
158 // Ensure the source parameters are valid.
Zane Shelley83da2452019-10-25 15:45:34 -0500159 HEI_ASSERT(nullptr != i_sStr.getBufAddr());
160 HEI_ASSERT(0 < i_sLen); // at least one bit to copy
161 HEI_ASSERT(i_sPos + i_sLen <= i_sStr.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500162
163 // Ensure the destination has at least one bit available to copy.
Zane Shelley83da2452019-10-25 15:45:34 -0500164 HEI_ASSERT(nullptr != getBufAddr());
165 HEI_ASSERT(i_dPos < getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500166
167 // If the source length is greater than the destination length than the
168 // extra source bits are ignored.
Zane Shelley83da2452019-10-25 15:45:34 -0500169 uint64_t actLen = std::min(i_sLen, getBitLen() - i_dPos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500170
171 // The bit strings may be in overlapping memory spaces. So we need to copy
172 // the data in the correct direction to prevent overlapping.
Zane Shelleyd0af3582019-09-19 10:48:59 -0500173 uint64_t sRelOffset = 0, dRelOffset = 0;
Zane Shelley83da2452019-10-25 15:45:34 -0500174 uint8_t * sRelAddr = i_sStr.getRelativePosition(sRelOffset, i_sPos);
175 uint8_t * dRelAddr = getRelativePosition(dRelOffset, i_dPos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500176
177 // Copy the data.
Zane Shelley83da2452019-10-25 15:45:34 -0500178 if ((dRelAddr == sRelAddr) && (dRelOffset == sRelOffset))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500179 {
180 // Do nothing. The source and destination are the same.
181 }
Zane Shelley83da2452019-10-25 15:45:34 -0500182 else if ((dRelAddr < sRelAddr) ||
183 ((dRelAddr == sRelAddr) && (dRelOffset < sRelOffset)))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500184 {
185 // Copy the data forward.
Zane Shelley83da2452019-10-25 15:45:34 -0500186 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500187 {
Zane Shelley83da2452019-10-25 15:45:34 -0500188 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500189
Zane Shelley83da2452019-10-25 15:45:34 -0500190 uint64_t value = i_sStr.getFieldRight(i_sPos + pos, len);
191 setFieldRight(i_dPos + pos, len, value);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500192 }
193 }
194 else // Copy the data backwards.
195 {
Ben Tynera8126fd2019-08-01 19:40:07 -0500196 // Get the first position of the last chunk (byte aligned).
Zane Shelley83da2452019-10-25 15:45:34 -0500197 uint64_t lastPos = ((actLen - 1) / UINT64_BIT_LEN) * UINT64_BIT_LEN;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500198
199 // Start with the last chunk and work backwards.
Zane Shelley83da2452019-10-25 15:45:34 -0500200 for (int32_t pos = lastPos; 0 <= pos; pos -= UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500201 {
Zane Shelley83da2452019-10-25 15:45:34 -0500202 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
203 uint64_t value = i_sStr.getFieldRight(i_sPos + pos, len);
204 setFieldRight(i_dPos + pos, len, value);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500205 }
206 }
207}
208
209//------------------------------------------------------------------------------
210
Zane Shelley83da2452019-10-25 15:45:34 -0500211void BitString::maskString(const BitString & i_mask)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500212{
213 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500214 uint64_t actLen = std::min(getBitLen(), i_mask.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500215
Zane Shelley83da2452019-10-25 15:45:34 -0500216 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500217 {
Zane Shelley83da2452019-10-25 15:45:34 -0500218 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500219
Zane Shelley83da2452019-10-25 15:45:34 -0500220 uint64_t dVal = getFieldRight(pos, len);
221 uint64_t sVal = i_mask.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500222
Zane Shelley83da2452019-10-25 15:45:34 -0500223 setFieldRight(pos, len, dVal & ~sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500224 }
225}
226
227//------------------------------------------------------------------------------
228
Zane Shelley83da2452019-10-25 15:45:34 -0500229bool BitString::isEqual(const BitString & i_str) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500230{
Zane Shelley83da2452019-10-25 15:45:34 -0500231 if (getBitLen() != i_str.getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500232 return false; // size not equal
233
Zane Shelley83da2452019-10-25 15:45:34 -0500234 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500235 {
Zane Shelley83da2452019-10-25 15:45:34 -0500236 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500237
Zane Shelley83da2452019-10-25 15:45:34 -0500238 if (getFieldRight(pos, len) != i_str.getFieldRight(pos, len))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500239 return false; // bit strings do not match
240 }
241
242 return true; // bit strings match
243}
244
245//------------------------------------------------------------------------------
246
247bool BitString::isZero() const
248{
Zane Shelley83da2452019-10-25 15:45:34 -0500249 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500250 {
Zane Shelley83da2452019-10-25 15:45:34 -0500251 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500252
Zane Shelley83da2452019-10-25 15:45:34 -0500253 if (0 != getFieldRight(pos, len))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500254 return false; // something is non-zero
255 }
256
257 return true; // everything was zero
258}
259
260//------------------------------------------------------------------------------
261
Zane Shelley83da2452019-10-25 15:45:34 -0500262uint64_t BitString::getSetCount(uint64_t i_pos, uint64_t i_len) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500263{
Zane Shelleyd0af3582019-09-19 10:48:59 -0500264 uint64_t endPos = i_pos + i_len;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500265
Zane Shelley83da2452019-10-25 15:45:34 -0500266 HEI_ASSERT(endPos <= getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500267
Zane Shelleyd0af3582019-09-19 10:48:59 -0500268 uint64_t count = 0;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500269
Zane Shelley83da2452019-10-25 15:45:34 -0500270 for (uint64_t i = i_pos; i < endPos; i++)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500271 {
Zane Shelley83da2452019-10-25 15:45:34 -0500272 if (isBitSet(i)) count++;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500273 }
274
275 return count;
276}
277
278//------------------------------------------------------------------------------
279
280BitStringBuffer BitString::operator~() const
281{
Zane Shelley83da2452019-10-25 15:45:34 -0500282 BitStringBuffer bsb(getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500283
Zane Shelley83da2452019-10-25 15:45:34 -0500284 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500285 {
Zane Shelley83da2452019-10-25 15:45:34 -0500286 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500287
Zane Shelley83da2452019-10-25 15:45:34 -0500288 uint64_t dVal = getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500289
Zane Shelley83da2452019-10-25 15:45:34 -0500290 bsb.setFieldRight(pos, len, ~dVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500291 }
292
293 return bsb;
294}
295
296//------------------------------------------------------------------------------
297
Zane Shelley83da2452019-10-25 15:45:34 -0500298BitStringBuffer BitString::operator&(const BitString & i_bs) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500299{
300 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500301 uint64_t actLen = std::min(getBitLen(), i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500302
Zane Shelley83da2452019-10-25 15:45:34 -0500303 BitStringBuffer bsb(actLen);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500304
Zane Shelley83da2452019-10-25 15:45:34 -0500305 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500306 {
Zane Shelley83da2452019-10-25 15:45:34 -0500307 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500308
Zane Shelley83da2452019-10-25 15:45:34 -0500309 uint64_t dVal = getFieldRight(pos, len);
310 uint64_t sVal = i_bs.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500311
Zane Shelley83da2452019-10-25 15:45:34 -0500312 bsb.setFieldRight(pos, len, dVal & sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500313 }
314
315 return bsb;
316}
317
318//------------------------------------------------------------------------------
319
Zane Shelley83da2452019-10-25 15:45:34 -0500320BitStringBuffer BitString::operator|(const BitString & i_bs) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500321{
322 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500323 uint64_t actLen = std::min(getBitLen(), i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500324
Zane Shelley83da2452019-10-25 15:45:34 -0500325 BitStringBuffer bsb(actLen);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500326
Zane Shelley83da2452019-10-25 15:45:34 -0500327 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500328 {
Zane Shelley83da2452019-10-25 15:45:34 -0500329 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500330
Zane Shelley83da2452019-10-25 15:45:34 -0500331 uint64_t dVal = getFieldRight(pos, len);
332 uint64_t sVal = i_bs.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500333
Zane Shelley83da2452019-10-25 15:45:34 -0500334 bsb.setFieldRight(pos, len, dVal | sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500335 }
336
337 return bsb;
338}
339
340//------------------------------------------------------------------------------
341
Zane Shelley83da2452019-10-25 15:45:34 -0500342BitStringBuffer BitString::operator>>(uint64_t i_shift) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500343{
Zane Shelley83da2452019-10-25 15:45:34 -0500344 BitStringBuffer bsb(getBitLen()); // default all zeros
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500345
Zane Shelley83da2452019-10-25 15:45:34 -0500346 if (i_shift < getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500347 {
348 // bso overlays bsb, containing the shifted offset.
Zane Shelley83da2452019-10-25 15:45:34 -0500349 BitString bso (bsb.getBitLen() - i_shift, bsb.getBufAddr(), i_shift);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500350
351 // Copy this into bso.
Zane Shelley83da2452019-10-25 15:45:34 -0500352 bso.setString(*this);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500353 }
354
355 return bsb;
356}
357
358//------------------------------------------------------------------------------
359
Zane Shelley83da2452019-10-25 15:45:34 -0500360BitStringBuffer BitString::operator<<(uint64_t i_shift) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500361{
Zane Shelley83da2452019-10-25 15:45:34 -0500362 BitStringBuffer bsb(getBitLen()); // default all zeros
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500363
Zane Shelley83da2452019-10-25 15:45:34 -0500364 if (i_shift < getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500365 {
366 // bso overlays *this, containing the shifted offset.
Zane Shelley83da2452019-10-25 15:45:34 -0500367 BitString bso (this->getBitLen() - i_shift, this->getBufAddr(),
368 i_shift);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500369
370 // Copy bso into bsb.
Zane Shelley83da2452019-10-25 15:45:34 -0500371 bsb.setString(bso);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500372 }
373
374 return bsb;
375}
376
377//------------------------------------------------------------------------------
378
Zane Shelley83da2452019-10-25 15:45:34 -0500379uint8_t * BitString::getRelativePosition(uint64_t & o_relPos,
380 uint64_t i_absPos) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500381{
Zane Shelley83da2452019-10-25 15:45:34 -0500382 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
383 HEI_ASSERT(i_absPos < getBitLen()); // must be a valid position
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500384
Ben Tynera8126fd2019-08-01 19:40:07 -0500385 o_relPos = (i_absPos + iv_offset) % UINT8_BIT_LEN;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500386
Ben Tynera8126fd2019-08-01 19:40:07 -0500387 return ((uint8_t *)iv_bufAddr + ((i_absPos + iv_offset) / UINT8_BIT_LEN));
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500388}
389
390//##############################################################################
391// BitStringBuffer class
392//##############################################################################
393
Zane Shelley83da2452019-10-25 15:45:34 -0500394BitStringBuffer::BitStringBuffer(uint64_t i_bitLen) :
395 BitString(i_bitLen, nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500396{
397 initBuffer();
398}
399
400//------------------------------------------------------------------------------
401
402BitStringBuffer::~BitStringBuffer()
403{
Ben Tynera8126fd2019-08-01 19:40:07 -0500404 delete [] (uint8_t *)getBufAddr();
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500405}
406
407//------------------------------------------------------------------------------
408
Zane Shelley83da2452019-10-25 15:45:34 -0500409BitStringBuffer::BitStringBuffer(const BitString & i_bs) :
410 BitString(i_bs.getBitLen(), nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500411{
412 initBuffer();
Zane Shelley83da2452019-10-25 15:45:34 -0500413 if (!i_bs.isZero()) setString(i_bs);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500414}
415
416//------------------------------------------------------------------------------
417
Zane Shelley83da2452019-10-25 15:45:34 -0500418BitStringBuffer::BitStringBuffer(const BitStringBuffer & i_bsb) :
419 BitString(i_bsb.getBitLen(), nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500420{
421 initBuffer();
Zane Shelley83da2452019-10-25 15:45:34 -0500422 if (!i_bsb.isZero()) setString(i_bsb);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500423}
424
425//------------------------------------------------------------------------------
426
Zane Shelley83da2452019-10-25 15:45:34 -0500427BitStringBuffer & BitStringBuffer::operator=(const BitString & i_bs)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500428{
429 // The initBuffer() function will deallocate the buffer as well, however we
430 // also need to deallocate the buffer here before we set the length.
Ben Tynera8126fd2019-08-01 19:40:07 -0500431 delete [] (uint8_t *)getBufAddr();
Zane Shelley83da2452019-10-25 15:45:34 -0500432 setBufAddr(nullptr);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500433
Zane Shelley83da2452019-10-25 15:45:34 -0500434 setBitLen(i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500435 initBuffer();
Zane Shelley83da2452019-10-25 15:45:34 -0500436 if (!i_bs.isZero()) setString(i_bs);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500437
438 return *this;
439}
440
441//------------------------------------------------------------------------------
442
Zane Shelley83da2452019-10-25 15:45:34 -0500443BitStringBuffer & BitStringBuffer::operator=(const BitStringBuffer & i_bsb)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500444{
Zane Shelley83da2452019-10-25 15:45:34 -0500445 if (this != &i_bsb) // Check for assignment to self
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500446 {
447 // The initBuffer() function will deallocate the buffer as well, however
448 // we also need to deallocate the buffer here before we set the length.
Ben Tynera8126fd2019-08-01 19:40:07 -0500449 delete [] (uint8_t *)getBufAddr();
Zane Shelley83da2452019-10-25 15:45:34 -0500450 setBufAddr(nullptr);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500451
Zane Shelley83da2452019-10-25 15:45:34 -0500452 setBitLen(i_bsb.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500453 initBuffer();
Zane Shelley83da2452019-10-25 15:45:34 -0500454 if (!i_bsb.isZero()) setString(i_bsb);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500455 }
456
457 return *this;
458}
459
460//------------------------------------------------------------------------------
461
462void BitStringBuffer::initBuffer()
463{
464 // Deallocate the current buffer.
Ben Tynera8126fd2019-08-01 19:40:07 -0500465 delete [] (uint8_t *)getBufAddr();
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500466
Ben Tynera8126fd2019-08-01 19:40:07 -0500467 // create new buffer, initialized to 0's
Zane Shelley83da2452019-10-25 15:45:34 -0500468 setBufAddr(new uint8_t[ getMinBytes(getBitLen()) ]());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500469}
470
Zane Shelley871adec2019-07-30 11:01:39 -0500471} // end namespace libhei