blob: 25443a60180094f4c2a4e6c045d6ccf8bc21e642 [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 <util/hei_bit_string.hpp>
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05006
Ben Tynera8126fd2019-08-01 19:40:07 -05007#include <hei_user_defines.hpp>
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05008
9#include <algorithm>
10
Zane Shelley871adec2019-07-30 11:01:39 -050011namespace libhei
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050012{
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050013
14//##############################################################################
15// BitString class
16//##############################################################################
17
Ben Tynera8126fd2019-08-01 19:40:07 -050018// number of bits in a uint64_t
Zane Shelleyd0af3582019-09-19 10:48:59 -050019constexpr uint64_t BitString::UINT64_BIT_LEN = sizeof(uint64_t) * 8;
Ben Tynera8126fd2019-08-01 19:40:07 -050020// number of bits in a uint8_t
Zane Shelleyd0af3582019-09-19 10:48:59 -050021constexpr uint64_t BitString::UINT8_BIT_LEN = sizeof(uint8_t) * 8;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050022
23//------------------------------------------------------------------------------
24
Zane Shelley83da2452019-10-25 15:45:34 -050025uint64_t BitString::getFieldRight(uint64_t i_pos, uint64_t i_len) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050026{
Zane Shelley83da2452019-10-25 15:45:34 -050027 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
28 HEI_ASSERT(0 < i_len); // must have at least one bit
29 HEI_ASSERT(i_len <= UINT64_BIT_LEN); // i_len length must be valid
30 HEI_ASSERT(i_pos + i_len <= getBitLen()); // field must be within range
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050031
Ben Tynera8126fd2019-08-01 19:40:07 -050032 // Get the relative address of this byte and the relative starting position
33 // within the byte.
Zane Shelleyd0af3582019-09-19 10:48:59 -050034 uint64_t relPos = 0;
Zane Shelley83da2452019-10-25 15:45:34 -050035 uint8_t * relAddr = getRelativePosition(relPos, i_pos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050036
Ben Tynera8126fd2019-08-01 19:40:07 -050037 // Get the length of the target bit field within this byte and the length of
38 // the bit field for any remaining bits.
Zane Shelleyd0af3582019-09-19 10:48:59 -050039 uint64_t bf_len = i_len;
40 uint64_t remain_len = 0;
Zane Shelley83da2452019-10-25 15:45:34 -050041 if (UINT8_BIT_LEN < relPos + i_len)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050042 {
Ben Tynera8126fd2019-08-01 19:40:07 -050043 // The target bit field crosses a byte boundary. So truncate the bit
44 // length for this byte and update the remaining length.
45 bf_len = UINT8_BIT_LEN - relPos;
46 remain_len = i_len - bf_len;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050047 }
48
Ben Tynera8126fd2019-08-01 19:40:07 -050049 // Get the target bit field within this byte (right justified).
50 uint8_t bf = *relAddr;
51 bf <<= relPos; // Mask off uneeded bits on the left side.
52 bf >>= UINT8_BIT_LEN - bf_len; // Right justify the value.
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050053
Ben Tynera8126fd2019-08-01 19:40:07 -050054 // Check for any remaining bits after this target byte.
Zane Shelley83da2452019-10-25 15:45:34 -050055 if (0 != remain_len)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050056 {
Ben Tynera8126fd2019-08-01 19:40:07 -050057 // Recursively call this function on the remaining bits and push them
58 // into the right side of the return value.
59 uint64_t val = static_cast<uint64_t>(bf) << remain_len;
Zane Shelley83da2452019-10-25 15:45:34 -050060 return val | getFieldRight(i_pos + bf_len, remain_len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050061 }
62
Ben Tynera8126fd2019-08-01 19:40:07 -050063 // Nothing more to do. Simply return this bit field.
64 return bf;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050065}
66
67//------------------------------------------------------------------------------
68
Zane Shelley83da2452019-10-25 15:45:34 -050069void BitString::setFieldLeft(uint64_t i_pos, uint64_t i_len, uint64_t i_val)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050070{
Zane Shelley83da2452019-10-25 15:45:34 -050071 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
72 HEI_ASSERT(0 < i_len); // must have at least one bit
73 HEI_ASSERT(i_len <= UINT64_BIT_LEN); // i_len length must be valid
74 HEI_ASSERT(i_pos + i_len <= getBitLen()); // field must be within range
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050075
Ben Tynera8126fd2019-08-01 19:40:07 -050076 // Get the relative address of this byte and the relative starting position
77 // within the byte.
Zane Shelleyd0af3582019-09-19 10:48:59 -050078 uint64_t relPos = 0;
Zane Shelley83da2452019-10-25 15:45:34 -050079 uint8_t * relAddr = getRelativePosition(relPos, i_pos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050080
Ben Tynera8126fd2019-08-01 19:40:07 -050081 // Get the length of the target bit field within this byte and the length of
82 // the bit field for any remaining bits.
Zane Shelleyd0af3582019-09-19 10:48:59 -050083 uint64_t bf_len = i_len;
84 uint64_t remain_len = 0;
Zane Shelley83da2452019-10-25 15:45:34 -050085 if (UINT8_BIT_LEN < relPos + i_len)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050086 {
Ben Tynera8126fd2019-08-01 19:40:07 -050087 // The target bit field crosses a byte boundary. So truncate the bit
88 // length for this byte and update the remaining length.
89 bf_len = UINT8_BIT_LEN - relPos;
90 remain_len = i_len - bf_len;
91 }
92
93 // It is possible there are bits in this byte on either side of the target
94 // bit field that must be preserved. Get the length of each of those bit
95 // fields.
Zane Shelleyd0af3582019-09-19 10:48:59 -050096 uint64_t bf_l_len = relPos;
97 uint64_t bf_r_len = UINT8_BIT_LEN - (bf_l_len + bf_len);
Ben Tynera8126fd2019-08-01 19:40:07 -050098
99 // Get the target bit field from the left justified inputed value.
100 uint8_t bf = (i_val >> (UINT64_BIT_LEN - bf_len)) << bf_r_len;
101
102 // Get the bit fields on either side of the target bit field.
Zane Shelleyd0af3582019-09-19 10:48:59 -0500103 uint64_t bf_l_shift = UINT8_BIT_LEN - bf_l_len;
104 uint64_t bf_r_shift = UINT8_BIT_LEN - bf_r_len;
Ben Tynera8126fd2019-08-01 19:40:07 -0500105 uint8_t bf_l = *relAddr; bf_l >>= bf_l_shift; bf_l <<= bf_l_shift;
106 uint8_t bf_r = *relAddr; bf_r <<= bf_r_shift; bf_r >>= bf_r_shift;
107
108 // Combine all three parts of the byte and write it out to memory.
109 *relAddr = bf_l | bf | bf_r;
110
111 // Check for any remaining bits after this target byte.
Zane Shelley83da2452019-10-25 15:45:34 -0500112 if (0 != remain_len)
Ben Tynera8126fd2019-08-01 19:40:07 -0500113 {
114 // Recursively call this function on the remaining bits.
Zane Shelley83da2452019-10-25 15:45:34 -0500115 setFieldLeft(i_pos + bf_len, remain_len, i_val << bf_len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500116 }
117}
118
119//------------------------------------------------------------------------------
120
Zane Shelley83da2452019-10-25 15:45:34 -0500121void BitString::setPattern(uint64_t i_sPos, uint64_t i_sLen, uint64_t i_pattern,
122 uint64_t i_pLen)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500123{
Ben Tynera8126fd2019-08-01 19:40:07 -0500124
125 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
126 HEI_ASSERT(0 < i_sLen); // must have at least one bit
127 HEI_ASSERT(i_sPos + i_sLen <= getBitLen()); // field must be within range
128 HEI_ASSERT(0 < i_pLen); // must have at least one bit
Zane Shelley83da2452019-10-25 15:45:34 -0500129 HEI_ASSERT(i_pLen <= UINT64_BIT_LEN); // i_pLen length must be valid
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500130
131 // Get a bit string for the pattern subset (right justified).
Ben Tynera8126fd2019-08-01 19:40:07 -0500132 // Note that we cannot use a BitStringBuffer here because this function
133 // could be used in the constructor of BitStringBuffer, which could causes
134 // an infinite loop.
135 uint8_t a[sizeof(i_pattern)] = {};
136 BitString bs { sizeof(i_pattern)*8, a };
137 bs.setFieldRight(0, i_pLen, i_pattern);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500138
139 // Iterate the range in chunks the size of i_pLen.
Zane Shelleyd0af3582019-09-19 10:48:59 -0500140 uint64_t endPos = i_sPos + i_sLen;
Zane Shelley83da2452019-10-25 15:45:34 -0500141 for (uint64_t pos = i_sPos; pos < endPos; pos += i_pLen)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500142 {
143 // The true chunk size is either i_pLen or the leftovers at the end.
Zane Shelley83da2452019-10-25 15:45:34 -0500144 uint64_t len = std::min(i_pLen, endPos - pos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500145
Ben Tynera8126fd2019-08-01 19:40:07 -0500146 // Get this chunk's pattern value, truncate (right justified) if needed.
Zane Shelley83da2452019-10-25 15:45:34 -0500147 uint64_t pattern = bs.getFieldRight(0, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500148
149 // Set the pattern in this string.
Zane Shelley83da2452019-10-25 15:45:34 -0500150 setFieldRight(pos, len, pattern);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500151 }
152}
153
154//------------------------------------------------------------------------------
155
Zane Shelley83da2452019-10-25 15:45:34 -0500156void BitString::setString(const BitString & i_sStr, uint64_t i_sPos,
157 uint64_t i_sLen, uint64_t i_dPos)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500158{
159 // Ensure the source parameters are valid.
Zane Shelley83da2452019-10-25 15:45:34 -0500160 HEI_ASSERT(nullptr != i_sStr.getBufAddr());
161 HEI_ASSERT(0 < i_sLen); // at least one bit to copy
162 HEI_ASSERT(i_sPos + i_sLen <= i_sStr.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500163
164 // Ensure the destination has at least one bit available to copy.
Zane Shelley83da2452019-10-25 15:45:34 -0500165 HEI_ASSERT(nullptr != getBufAddr());
166 HEI_ASSERT(i_dPos < getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500167
168 // If the source length is greater than the destination length than the
169 // extra source bits are ignored.
Zane Shelley83da2452019-10-25 15:45:34 -0500170 uint64_t actLen = std::min(i_sLen, getBitLen() - i_dPos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500171
172 // The bit strings may be in overlapping memory spaces. So we need to copy
173 // the data in the correct direction to prevent overlapping.
Zane Shelleyd0af3582019-09-19 10:48:59 -0500174 uint64_t sRelOffset = 0, dRelOffset = 0;
Zane Shelley83da2452019-10-25 15:45:34 -0500175 uint8_t * sRelAddr = i_sStr.getRelativePosition(sRelOffset, i_sPos);
176 uint8_t * dRelAddr = getRelativePosition(dRelOffset, i_dPos);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500177
178 // Copy the data.
Zane Shelley83da2452019-10-25 15:45:34 -0500179 if ((dRelAddr == sRelAddr) && (dRelOffset == sRelOffset))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500180 {
181 // Do nothing. The source and destination are the same.
182 }
Zane Shelley83da2452019-10-25 15:45:34 -0500183 else if ((dRelAddr < sRelAddr) ||
184 ((dRelAddr == sRelAddr) && (dRelOffset < sRelOffset)))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500185 {
186 // Copy the data forward.
Zane Shelley83da2452019-10-25 15:45:34 -0500187 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500188 {
Zane Shelley83da2452019-10-25 15:45:34 -0500189 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500190
Zane Shelley83da2452019-10-25 15:45:34 -0500191 uint64_t value = i_sStr.getFieldRight(i_sPos + pos, len);
192 setFieldRight(i_dPos + pos, len, value);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500193 }
194 }
195 else // Copy the data backwards.
196 {
Ben Tynera8126fd2019-08-01 19:40:07 -0500197 // Get the first position of the last chunk (byte aligned).
Zane Shelley83da2452019-10-25 15:45:34 -0500198 uint64_t lastPos = ((actLen - 1) / UINT64_BIT_LEN) * UINT64_BIT_LEN;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500199
200 // Start with the last chunk and work backwards.
Zane Shelley83da2452019-10-25 15:45:34 -0500201 for (int32_t pos = lastPos; 0 <= pos; pos -= UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500202 {
Zane Shelley83da2452019-10-25 15:45:34 -0500203 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
204 uint64_t value = i_sStr.getFieldRight(i_sPos + pos, len);
205 setFieldRight(i_dPos + pos, len, value);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500206 }
207 }
208}
209
210//------------------------------------------------------------------------------
211
Zane Shelley83da2452019-10-25 15:45:34 -0500212void BitString::maskString(const BitString & i_mask)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500213{
214 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500215 uint64_t actLen = std::min(getBitLen(), i_mask.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500216
Zane Shelley83da2452019-10-25 15:45:34 -0500217 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500218 {
Zane Shelley83da2452019-10-25 15:45:34 -0500219 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500220
Zane Shelley83da2452019-10-25 15:45:34 -0500221 uint64_t dVal = getFieldRight(pos, len);
222 uint64_t sVal = i_mask.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500223
Zane Shelley83da2452019-10-25 15:45:34 -0500224 setFieldRight(pos, len, dVal & ~sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500225 }
226}
227
228//------------------------------------------------------------------------------
229
Zane Shelley83da2452019-10-25 15:45:34 -0500230bool BitString::isEqual(const BitString & i_str) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500231{
Zane Shelley83da2452019-10-25 15:45:34 -0500232 if (getBitLen() != i_str.getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500233 return false; // size not equal
234
Zane Shelley83da2452019-10-25 15:45:34 -0500235 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500236 {
Zane Shelley83da2452019-10-25 15:45:34 -0500237 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500238
Zane Shelley83da2452019-10-25 15:45:34 -0500239 if (getFieldRight(pos, len) != i_str.getFieldRight(pos, len))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500240 return false; // bit strings do not match
241 }
242
243 return true; // bit strings match
244}
245
246//------------------------------------------------------------------------------
247
248bool BitString::isZero() const
249{
Zane Shelley83da2452019-10-25 15:45:34 -0500250 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500251 {
Zane Shelley83da2452019-10-25 15:45:34 -0500252 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500253
Zane Shelley83da2452019-10-25 15:45:34 -0500254 if (0 != getFieldRight(pos, len))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500255 return false; // something is non-zero
256 }
257
258 return true; // everything was zero
259}
260
261//------------------------------------------------------------------------------
262
Zane Shelley83da2452019-10-25 15:45:34 -0500263uint64_t BitString::getSetCount(uint64_t i_pos, uint64_t i_len) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500264{
Zane Shelleyd0af3582019-09-19 10:48:59 -0500265 uint64_t endPos = i_pos + i_len;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500266
Zane Shelley83da2452019-10-25 15:45:34 -0500267 HEI_ASSERT(endPos <= getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500268
Zane Shelleyd0af3582019-09-19 10:48:59 -0500269 uint64_t count = 0;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500270
Zane Shelley83da2452019-10-25 15:45:34 -0500271 for (uint64_t i = i_pos; i < endPos; i++)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500272 {
Zane Shelley83da2452019-10-25 15:45:34 -0500273 if (isBitSet(i)) count++;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500274 }
275
276 return count;
277}
278
279//------------------------------------------------------------------------------
280
281BitStringBuffer BitString::operator~() const
282{
Zane Shelley83da2452019-10-25 15:45:34 -0500283 BitStringBuffer bsb(getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500284
Zane Shelley83da2452019-10-25 15:45:34 -0500285 for (uint64_t pos = 0; pos < getBitLen(); pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500286 {
Zane Shelley83da2452019-10-25 15:45:34 -0500287 uint64_t len = std::min(getBitLen() - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500288
Zane Shelley83da2452019-10-25 15:45:34 -0500289 uint64_t dVal = getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500290
Zane Shelley83da2452019-10-25 15:45:34 -0500291 bsb.setFieldRight(pos, len, ~dVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500292 }
293
294 return bsb;
295}
296
297//------------------------------------------------------------------------------
298
Zane Shelley83da2452019-10-25 15:45:34 -0500299BitStringBuffer BitString::operator&(const BitString & i_bs) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500300{
301 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500302 uint64_t actLen = std::min(getBitLen(), i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500303
Zane Shelley83da2452019-10-25 15:45:34 -0500304 BitStringBuffer bsb(actLen);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500305
Zane Shelley83da2452019-10-25 15:45:34 -0500306 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500307 {
Zane Shelley83da2452019-10-25 15:45:34 -0500308 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500309
Zane Shelley83da2452019-10-25 15:45:34 -0500310 uint64_t dVal = getFieldRight(pos, len);
311 uint64_t sVal = i_bs.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500312
Zane Shelley83da2452019-10-25 15:45:34 -0500313 bsb.setFieldRight(pos, len, dVal & sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500314 }
315
316 return bsb;
317}
318
319//------------------------------------------------------------------------------
320
Zane Shelley83da2452019-10-25 15:45:34 -0500321BitStringBuffer BitString::operator|(const BitString & i_bs) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500322{
323 // Get the length of the smallest string.
Zane Shelley83da2452019-10-25 15:45:34 -0500324 uint64_t actLen = std::min(getBitLen(), i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500325
Zane Shelley83da2452019-10-25 15:45:34 -0500326 BitStringBuffer bsb(actLen);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500327
Zane Shelley83da2452019-10-25 15:45:34 -0500328 for (uint64_t pos = 0; pos < actLen; pos += UINT64_BIT_LEN)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500329 {
Zane Shelley83da2452019-10-25 15:45:34 -0500330 uint64_t len = std::min(actLen - pos, UINT64_BIT_LEN);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500331
Zane Shelley83da2452019-10-25 15:45:34 -0500332 uint64_t dVal = getFieldRight(pos, len);
333 uint64_t sVal = i_bs.getFieldRight(pos, len);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500334
Zane Shelley83da2452019-10-25 15:45:34 -0500335 bsb.setFieldRight(pos, len, dVal | sVal);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500336 }
337
338 return bsb;
339}
340
341//------------------------------------------------------------------------------
342
Zane Shelley83da2452019-10-25 15:45:34 -0500343BitStringBuffer BitString::operator>>(uint64_t i_shift) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500344{
Zane Shelley83da2452019-10-25 15:45:34 -0500345 BitStringBuffer bsb(getBitLen()); // default all zeros
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500346
Zane Shelley83da2452019-10-25 15:45:34 -0500347 if (i_shift < getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500348 {
349 // bso overlays bsb, containing the shifted offset.
Zane Shelley83da2452019-10-25 15:45:34 -0500350 BitString bso (bsb.getBitLen() - i_shift, bsb.getBufAddr(), i_shift);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500351
352 // Copy this into bso.
Zane Shelley83da2452019-10-25 15:45:34 -0500353 bso.setString(*this);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500354 }
355
356 return bsb;
357}
358
359//------------------------------------------------------------------------------
360
Zane Shelley83da2452019-10-25 15:45:34 -0500361BitStringBuffer BitString::operator<<(uint64_t i_shift) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500362{
Zane Shelley83da2452019-10-25 15:45:34 -0500363 BitStringBuffer bsb(getBitLen()); // default all zeros
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500364
Zane Shelley83da2452019-10-25 15:45:34 -0500365 if (i_shift < getBitLen())
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500366 {
367 // bso overlays *this, containing the shifted offset.
Zane Shelley83da2452019-10-25 15:45:34 -0500368 BitString bso (this->getBitLen() - i_shift, this->getBufAddr(),
369 i_shift);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500370
371 // Copy bso into bsb.
Zane Shelley83da2452019-10-25 15:45:34 -0500372 bsb.setString(bso);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500373 }
374
375 return bsb;
376}
377
378//------------------------------------------------------------------------------
379
Zane Shelley83da2452019-10-25 15:45:34 -0500380uint8_t * BitString::getRelativePosition(uint64_t & o_relPos,
381 uint64_t i_absPos) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500382{
Zane Shelley83da2452019-10-25 15:45:34 -0500383 HEI_ASSERT(nullptr != getBufAddr()); // must to have a valid address
384 HEI_ASSERT(i_absPos < getBitLen()); // must be a valid position
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500385
Ben Tynera8126fd2019-08-01 19:40:07 -0500386 o_relPos = (i_absPos + iv_offset) % UINT8_BIT_LEN;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500387
Ben Tynera8126fd2019-08-01 19:40:07 -0500388 return ((uint8_t *)iv_bufAddr + ((i_absPos + iv_offset) / UINT8_BIT_LEN));
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500389}
390
391//##############################################################################
392// BitStringBuffer class
393//##############################################################################
394
Zane Shelley83da2452019-10-25 15:45:34 -0500395BitStringBuffer::BitStringBuffer(uint64_t i_bitLen) :
396 BitString(i_bitLen, nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500397{
398 initBuffer();
399}
400
401//------------------------------------------------------------------------------
402
403BitStringBuffer::~BitStringBuffer()
404{
Ben Tynera8126fd2019-08-01 19:40:07 -0500405 delete [] (uint8_t *)getBufAddr();
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500406}
407
408//------------------------------------------------------------------------------
409
Zane Shelley83da2452019-10-25 15:45:34 -0500410BitStringBuffer::BitStringBuffer(const BitString & i_bs) :
411 BitString(i_bs.getBitLen(), nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500412{
413 initBuffer();
Zane Shelley83da2452019-10-25 15:45:34 -0500414 if (!i_bs.isZero()) setString(i_bs);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500415}
416
417//------------------------------------------------------------------------------
418
Zane Shelley83da2452019-10-25 15:45:34 -0500419BitStringBuffer::BitStringBuffer(const BitStringBuffer & i_bsb) :
420 BitString(i_bsb.getBitLen(), nullptr)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500421{
422 initBuffer();
Zane Shelley83da2452019-10-25 15:45:34 -0500423 if (!i_bsb.isZero()) setString(i_bsb);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500424}
425
426//------------------------------------------------------------------------------
427
Zane Shelley83da2452019-10-25 15:45:34 -0500428BitStringBuffer & BitStringBuffer::operator=(const BitString & i_bs)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500429{
430 // The initBuffer() function will deallocate the buffer as well, however we
431 // also need to deallocate the buffer here before we set the length.
Ben Tynera8126fd2019-08-01 19:40:07 -0500432 delete [] (uint8_t *)getBufAddr();
Zane Shelley83da2452019-10-25 15:45:34 -0500433 setBufAddr(nullptr);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500434
Zane Shelley83da2452019-10-25 15:45:34 -0500435 setBitLen(i_bs.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500436 initBuffer();
Zane Shelley83da2452019-10-25 15:45:34 -0500437 if (!i_bs.isZero()) setString(i_bs);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500438
439 return *this;
440}
441
442//------------------------------------------------------------------------------
443
Zane Shelley83da2452019-10-25 15:45:34 -0500444BitStringBuffer & BitStringBuffer::operator=(const BitStringBuffer & i_bsb)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500445{
Zane Shelley83da2452019-10-25 15:45:34 -0500446 if (this != &i_bsb) // Check for assignment to self
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500447 {
448 // The initBuffer() function will deallocate the buffer as well, however
449 // we also need to deallocate the buffer here before we set the length.
Ben Tynera8126fd2019-08-01 19:40:07 -0500450 delete [] (uint8_t *)getBufAddr();
Zane Shelley83da2452019-10-25 15:45:34 -0500451 setBufAddr(nullptr);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500452
Zane Shelley83da2452019-10-25 15:45:34 -0500453 setBitLen(i_bsb.getBitLen());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500454 initBuffer();
Zane Shelley83da2452019-10-25 15:45:34 -0500455 if (!i_bsb.isZero()) setString(i_bsb);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500456 }
457
458 return *this;
459}
460
461//------------------------------------------------------------------------------
462
463void BitStringBuffer::initBuffer()
464{
465 // Deallocate the current buffer.
Ben Tynera8126fd2019-08-01 19:40:07 -0500466 delete [] (uint8_t *)getBufAddr();
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500467
Ben Tynera8126fd2019-08-01 19:40:07 -0500468 // create new buffer, initialized to 0's
Zane Shelley83da2452019-10-25 15:45:34 -0500469 setBufAddr(new uint8_t[ getMinBytes(getBitLen()) ]());
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500470}
471
Zane Shelley871adec2019-07-30 11:01:39 -0500472} // end namespace libhei