mirror of
https://github.com/TomHarte/CLK.git
synced 2026-04-22 08:16:42 +00:00
Adjust more dangling indentation changes.
This commit is contained in:
+64
-63
@@ -26,81 +26,82 @@ constexpr uint8_t reverse_byte(uint8_t byte) {
|
||||
}
|
||||
|
||||
/*! Provides a class capable of generating a CRC from source data. */
|
||||
template <typename IntType, IntType reset_value, IntType output_xor, bool reflect_input, bool reflect_output> class Generator {
|
||||
public:
|
||||
/*!
|
||||
Instantiates a CRC16 that will compute the CRC16 specified by the supplied
|
||||
@c polynomial and @c reset_value.
|
||||
*/
|
||||
constexpr Generator(IntType polynomial) noexcept: value_(reset_value) {
|
||||
const IntType top_bit = IntType(~(IntType(~0) >> 1));
|
||||
for(int c = 0; c < 256; c++) {
|
||||
IntType shift_value = IntType(c << multibyte_shift);
|
||||
for(int b = 0; b < 8; b++) {
|
||||
IntType exclusive_or = (shift_value&top_bit) ? polynomial : 0;
|
||||
shift_value = IntType(shift_value << 1) ^ exclusive_or;
|
||||
}
|
||||
xor_table[c] = shift_value;
|
||||
template <typename IntType, IntType reset_value, IntType output_xor, bool reflect_input, bool reflect_output>
|
||||
class Generator {
|
||||
public:
|
||||
/*!
|
||||
Instantiates a CRC16 that will compute the CRC16 specified by the supplied
|
||||
@c polynomial and @c reset_value.
|
||||
*/
|
||||
constexpr Generator(IntType polynomial) noexcept: value_(reset_value) {
|
||||
const IntType top_bit = IntType(~(IntType(~0) >> 1));
|
||||
for(int c = 0; c < 256; c++) {
|
||||
IntType shift_value = IntType(c << multibyte_shift);
|
||||
for(int b = 0; b < 8; b++) {
|
||||
IntType exclusive_or = (shift_value&top_bit) ? polynomial : 0;
|
||||
shift_value = IntType(shift_value << 1) ^ exclusive_or;
|
||||
}
|
||||
xor_table[c] = shift_value;
|
||||
}
|
||||
}
|
||||
|
||||
/// Resets the CRC to the reset value.
|
||||
void reset() { value_ = reset_value; }
|
||||
/// Resets the CRC to the reset value.
|
||||
void reset() { value_ = reset_value; }
|
||||
|
||||
/// Updates the CRC to include @c byte.
|
||||
void add(uint8_t byte) {
|
||||
if constexpr (reflect_input) byte = reverse_byte(byte);
|
||||
value_ = IntType((value_ << 8) ^ xor_table[(value_ >> multibyte_shift) ^ byte]);
|
||||
}
|
||||
/// Updates the CRC to include @c byte.
|
||||
void add(uint8_t byte) {
|
||||
if constexpr (reflect_input) byte = reverse_byte(byte);
|
||||
value_ = IntType((value_ << 8) ^ xor_table[(value_ >> multibyte_shift) ^ byte]);
|
||||
}
|
||||
|
||||
/// @returns The current value of the CRC.
|
||||
inline IntType get_value() const {
|
||||
IntType result = value_ ^ output_xor;
|
||||
if constexpr (reflect_output) {
|
||||
IntType reflected_output = 0;
|
||||
for(std::size_t c = 0; c < sizeof(IntType); ++c) {
|
||||
reflected_output = IntType(reflected_output << 8) | IntType(reverse_byte(result & 0xff));
|
||||
result >>= 8;
|
||||
}
|
||||
return reflected_output;
|
||||
/// @returns The current value of the CRC.
|
||||
inline IntType get_value() const {
|
||||
IntType result = value_ ^ output_xor;
|
||||
if constexpr (reflect_output) {
|
||||
IntType reflected_output = 0;
|
||||
for(std::size_t c = 0; c < sizeof(IntType); ++c) {
|
||||
reflected_output = IntType(reflected_output << 8) | IntType(reverse_byte(result & 0xff));
|
||||
result >>= 8;
|
||||
}
|
||||
return result;
|
||||
return reflected_output;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Sets the current value of the CRC.
|
||||
inline void set_value(IntType value) { value_ = value; }
|
||||
/// Sets the current value of the CRC.
|
||||
inline void set_value(IntType value) { value_ = value; }
|
||||
|
||||
/*!
|
||||
A compound for:
|
||||
/*!
|
||||
A compound for:
|
||||
|
||||
reset()
|
||||
[add all data from @c data]
|
||||
get_value()
|
||||
*/
|
||||
template <typename Collection> IntType compute_crc(const Collection &data) {
|
||||
return compute_crc(data.begin(), data.end());
|
||||
reset()
|
||||
[add all data from @c data]
|
||||
get_value()
|
||||
*/
|
||||
template <typename Collection> IntType compute_crc(const Collection &data) {
|
||||
return compute_crc(data.begin(), data.end());
|
||||
}
|
||||
|
||||
/*!
|
||||
A compound for:
|
||||
|
||||
reset()
|
||||
[add all data from @c begin to @c end]
|
||||
get_value()
|
||||
*/
|
||||
template <typename Iterator> IntType compute_crc(Iterator begin, Iterator end) {
|
||||
reset();
|
||||
while(begin != end) {
|
||||
add(*begin);
|
||||
++begin;
|
||||
}
|
||||
return get_value();
|
||||
}
|
||||
|
||||
/*!
|
||||
A compound for:
|
||||
|
||||
reset()
|
||||
[add all data from @c begin to @c end]
|
||||
get_value()
|
||||
*/
|
||||
template <typename Iterator> IntType compute_crc(Iterator begin, Iterator end) {
|
||||
reset();
|
||||
while(begin != end) {
|
||||
add(*begin);
|
||||
++begin;
|
||||
}
|
||||
return get_value();
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr int multibyte_shift = (sizeof(IntType) * 8) - 8;
|
||||
IntType xor_table[256];
|
||||
IntType value_;
|
||||
private:
|
||||
static constexpr int multibyte_shift = (sizeof(IntType) * 8) - 8;
|
||||
IntType xor_table[256];
|
||||
IntType value_;
|
||||
};
|
||||
|
||||
/*!
|
||||
|
||||
+29
-29
@@ -40,41 +40,41 @@ template <> struct LSFRPolynomial<uint8_t> {
|
||||
in the specified int type.
|
||||
*/
|
||||
template <typename IntType = uint64_t, IntType polynomial = LSFRPolynomial<IntType>::value> class LFSR {
|
||||
public:
|
||||
/*!
|
||||
Constructs an LFSR with a random initial value.
|
||||
*/
|
||||
constexpr LFSR() noexcept {
|
||||
// Randomise the value, ensuring it doesn't end up being 0;
|
||||
// don't set any top bits, in case this is a signed type.
|
||||
while(!value_) {
|
||||
uint8_t *value_byte = reinterpret_cast<uint8_t *>(&value_);
|
||||
for(size_t c = 0; c < sizeof(IntType); ++c) {
|
||||
*value_byte = uint8_t(uint64_t(rand()) * 127 / RAND_MAX);
|
||||
++value_byte;
|
||||
}
|
||||
public:
|
||||
/*!
|
||||
Constructs an LFSR with a random initial value.
|
||||
*/
|
||||
constexpr LFSR() noexcept {
|
||||
// Randomise the value, ensuring it doesn't end up being 0;
|
||||
// don't set any top bits, in case this is a signed type.
|
||||
while(!value_) {
|
||||
uint8_t *value_byte = reinterpret_cast<uint8_t *>(&value_);
|
||||
for(size_t c = 0; c < sizeof(IntType); ++c) {
|
||||
*value_byte = uint8_t(uint64_t(rand()) * 127 / RAND_MAX);
|
||||
++value_byte;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
Constructs an LFSR with the specified initial value.
|
||||
/*!
|
||||
Constructs an LFSR with the specified initial value.
|
||||
|
||||
An initial value of 0 is invalid.
|
||||
*/
|
||||
LFSR(IntType initial_value) : value_(initial_value) {}
|
||||
An initial value of 0 is invalid.
|
||||
*/
|
||||
LFSR(IntType initial_value) : value_(initial_value) {}
|
||||
|
||||
/*!
|
||||
Advances the LSFR, returning either an @c IntType of value @c 1 or @c 0,
|
||||
determining the bit that was just shifted out.
|
||||
*/
|
||||
IntType next() {
|
||||
const auto result = IntType(value_ & 1);
|
||||
value_ = IntType((value_ >> 1) ^ (result * polynomial));
|
||||
return result;
|
||||
}
|
||||
/*!
|
||||
Advances the LSFR, returning either an @c IntType of value @c 1 or @c 0,
|
||||
determining the bit that was just shifted out.
|
||||
*/
|
||||
IntType next() {
|
||||
const auto result = IntType(value_ & 1);
|
||||
value_ = IntType((value_ >> 1) ^ (result * polynomial));
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
IntType value_ = 0;
|
||||
private:
|
||||
IntType value_ = 0;
|
||||
};
|
||||
|
||||
template <uint64_t polynomial> class LFSRv: public LFSR<typename MinIntTypeValue<polynomial>::type, polynomial> {};
|
||||
|
||||
+35
-35
@@ -28,47 +28,47 @@ namespace Numeric {
|
||||
/// = 65
|
||||
///
|
||||
template <int... Sizes> class NumericCoder {
|
||||
public:
|
||||
/// Modifies @c target to hold @c value at @c index.
|
||||
template <int index> static void encode(int &target, int value) {
|
||||
static_assert(index < sizeof...(Sizes), "Index must be within range");
|
||||
NumericEncoder<Sizes...>::template encode<index>(target, value);
|
||||
}
|
||||
public:
|
||||
/// Modifies @c target to hold @c value at @c index.
|
||||
template <int index> static void encode(int &target, int value) {
|
||||
static_assert(index < sizeof...(Sizes), "Index must be within range");
|
||||
NumericEncoder<Sizes...>::template encode<index>(target, value);
|
||||
}
|
||||
|
||||
/// @returns The value from @c source at @c index.
|
||||
template <int index> static int decode(int source) {
|
||||
static_assert(index < sizeof...(Sizes), "Index must be within range");
|
||||
return NumericDecoder<Sizes...>::template decode<index>(source);
|
||||
}
|
||||
/// @returns The value from @c source at @c index.
|
||||
template <int index> static int decode(int source) {
|
||||
static_assert(index < sizeof...(Sizes), "Index must be within range");
|
||||
return NumericDecoder<Sizes...>::template decode<index>(source);
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
|
||||
template <int size, int... Tail>
|
||||
struct NumericEncoder {
|
||||
template <int index, int i = 0, int divider = 1> static void encode(int &target, int value) {
|
||||
if constexpr (i == index) {
|
||||
const int suffix = target % divider;
|
||||
target /= divider;
|
||||
target -= target % size;
|
||||
target += value % size;
|
||||
target *= divider;
|
||||
target += suffix;
|
||||
} else {
|
||||
NumericEncoder<Tail...>::template encode<index, i+1, divider*size>(target, value);
|
||||
}
|
||||
template <int size, int... Tail>
|
||||
struct NumericEncoder {
|
||||
template <int index, int i = 0, int divider = 1> static void encode(int &target, int value) {
|
||||
if constexpr (i == index) {
|
||||
const int suffix = target % divider;
|
||||
target /= divider;
|
||||
target -= target % size;
|
||||
target += value % size;
|
||||
target *= divider;
|
||||
target += suffix;
|
||||
} else {
|
||||
NumericEncoder<Tail...>::template encode<index, i+1, divider*size>(target, value);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
template <int size, int... Tail>
|
||||
struct NumericDecoder {
|
||||
template <int index, int i = 0, int divider = 1> static int decode(int source) {
|
||||
if constexpr (i == index) {
|
||||
return (source / divider) % size;
|
||||
} else {
|
||||
return NumericDecoder<Tail...>::template decode<index, i+1, divider*size>(source);
|
||||
}
|
||||
template <int size, int... Tail>
|
||||
struct NumericDecoder {
|
||||
template <int index, int i = 0, int divider = 1> static int decode(int source) {
|
||||
if constexpr (i == index) {
|
||||
return (source / divider) % size;
|
||||
} else {
|
||||
return NumericDecoder<Tail...>::template decode<index, i+1, divider*size>(source);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user