1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-01-23 01:16:10 +00:00

Roll in some random style improvements.

This commit is contained in:
Thomas Harte
2025-09-08 20:38:50 -04:00
parent 96fd0b7892
commit cad42beef4
3 changed files with 12 additions and 11 deletions

View File

@@ -16,7 +16,7 @@ namespace Numeric {
/// keeping the least-significant bit in its original position.
///
/// i.e. if @c input is abcdefgh then the result is 0a0b0c0d0e0f0g0h
constexpr uint16_t spread_bits(uint8_t input) {
constexpr uint16_t spread_bits(const uint8_t input) {
uint16_t result = uint16_t(input); // 0000 0000 abcd efgh
result = (result | (result << 4)) & 0x0f0f; // 0000 abcd 0000 efgh
result = (result | (result << 2)) & 0x3333; // 00ab 00cd 00ef 00gh
@@ -26,11 +26,12 @@ constexpr uint16_t spread_bits(uint8_t input) {
/// Performs the opposite action to @c spread_bits; given the 16-bit input
/// @c abcd @c efgh @c ijkl @c mnop, returns the byte value @c bdfhjlnp
/// i.e. every other bit is retained, keeping the least-significant bit in place.
constexpr uint8_t unspread_bits(uint16_t input) {
input &= 0x5555; // 0a0b 0c0d 0e0f 0g0h
input = (input | (input >> 1)) & 0x3333; // 00ab 00cd 00ef 00gh
input = (input | (input >> 2)) & 0x0f0f; // 0000 abcd 0000 efgh
return uint8_t(input | (input >> 4)); // 0000 0000 abcd efgh
constexpr uint8_t unspread_bits(const uint16_t input) {
uint16_t result = input;
result &= 0x5555; // 0a0b 0c0d 0e0f 0g0h
result = (result | (result >> 1)) & 0x3333; // 00ab 00cd 00ef 00gh
result = (result | (result >> 2)) & 0x0f0f; // 0000 abcd 0000 efgh
return uint8_t(result | (result >> 4)); // 0000 0000 abcd efgh
}
}

View File

@@ -61,7 +61,7 @@ public:
An initial value of 0 is invalid.
*/
LFSR(IntType initial_value) : value_(initial_value) {}
LFSR(const IntType initial_value) : value_(initial_value) {}
/*!
Advances the LSFR, returning either an @c IntType of value @c 1 or @c 0,

View File

@@ -30,13 +30,13 @@ namespace Numeric {
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) {
template <int index> static void encode(int &target, const 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) {
template <int index> static int decode(const int source) {
static_assert(index < sizeof...(Sizes), "Index must be within range");
return NumericDecoder<Sizes...>::template decode<index>(source);
}
@@ -45,7 +45,7 @@ private:
template <int size, int... Tail>
struct NumericEncoder {
template <int index, int i = 0, int divider = 1> static void encode(int &target, int value) {
template <int index, int i = 0, int divider = 1> static void encode(int &target, const int value) {
if constexpr (i == index) {
const int suffix = target % divider;
target /= divider;
@@ -61,7 +61,7 @@ private:
template <int size, int... Tail>
struct NumericDecoder {
template <int index, int i = 0, int divider = 1> static int decode(int source) {
template <int index, int i = 0, int divider = 1> static int decode(const int source) {
if constexpr (i == index) {
return (source / divider) % size;
} else {