mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-06-19 12:24:18 -04:00
Rework BitHistory, BitPattern, PacketBuilder.
Templatize PacketBuilder to optionally bit unstuff, and have flexible packet termination behavior.
This commit is contained in:
parent
0b522abbc1
commit
0789d50fdf
5 changed files with 59 additions and 69 deletions
|
@ -25,8 +25,29 @@
|
|||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
class BitHistory {
|
||||
public:
|
||||
void add(const uint_fast8_t bit) {
|
||||
history = (history << 1) | (bit & 1);
|
||||
}
|
||||
|
||||
uint32_t value() const {
|
||||
return history;
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t history { 0 };
|
||||
};
|
||||
|
||||
class BitPattern {
|
||||
public:
|
||||
constexpr BitPattern(
|
||||
) : code_ { 0 },
|
||||
mask_ { 0 },
|
||||
maximum_hanning_distance_ { 0 }
|
||||
{
|
||||
}
|
||||
|
||||
constexpr BitPattern(
|
||||
const uint32_t code,
|
||||
const size_t code_length,
|
||||
|
@ -37,16 +58,10 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
uint32_t code() const {
|
||||
return code_;
|
||||
}
|
||||
|
||||
uint32_t mask() const {
|
||||
return mask_;
|
||||
}
|
||||
|
||||
size_t maximum_hanning_distance() const {
|
||||
return maximum_hanning_distance_;
|
||||
bool operator()(const BitHistory& history, const size_t) const {
|
||||
const auto delta_bits = (history.value() ^ code_) & mask_;
|
||||
const size_t count = __builtin_popcountl(delta_bits);
|
||||
return (count <= maximum_hanning_distance_);
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -55,20 +70,4 @@ private:
|
|||
size_t maximum_hanning_distance_;
|
||||
};
|
||||
|
||||
class BitHistory {
|
||||
public:
|
||||
void add(const uint_fast8_t bit) {
|
||||
history = (history << 1) | (bit & 1);
|
||||
}
|
||||
|
||||
bool matches(const BitPattern& pattern) const {
|
||||
const auto delta_bits = (history ^ pattern.code()) & pattern.mask();
|
||||
const size_t count = __builtin_popcountl(delta_bits);
|
||||
return (count <= pattern.maximum_hanning_distance());
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t history { 0 };
|
||||
};
|
||||
|
||||
#endif/*__BIT_PATTERN_H__*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue