Skip to content

Commit

Permalink
clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
windytan committed Jul 22, 2024
1 parent 9192434 commit caf3eb3
Show file tree
Hide file tree
Showing 25 changed files with 377 additions and 308 deletions.
5 changes: 4 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ Standard: c++14
ColumnLimit: 100
AllowShortIfStatementsOnASingleLine: false
AlignArrayOfStructures: Left
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true
AlignConsecutiveShortCaseStatements:
Expand Down
23 changes: 23 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Checks: >
bugprone-*,
-bugprone-easily-swappable-parameters,
-bugprone-implicit-widening-of-multiplication-result,
cert-*,
-cert-err58-cpp,
cppcoreguidelines-*,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-pro-bounds-constant-array-index,
google-explicit-constructor,
hicpp-*,
-hicpp-braces-around-statements,
-hicpp-uppercase-literal-suffix,
misc-*,
-misc-non-private-member-variables-in-classes,
modernize-*,
-modernize-use-trailing-return-type,
performance-*,
portability-*,
readability-misplaced-array-index,
readability-redundant-*,
readability-reference-to-constructed-temporary,
readability-suspicious-call-argument
22 changes: 22 additions & 0 deletions .github/workflows/tidy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: tidy

on:
push:
branches: [ master, dev ]
pull_request:
branches: [ master ]

jobs:
clang-tidy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: install dependencies
run: sudo apt install libsndfile1-dev libliquid-dev nlohmann-json3-dev meson clang-tidy
- name: meson setup
run: meson setup -Dwerror=true build
- name: compile
run: cd build && meson compile
- name: run clang-tidy
run: clang-tidy -p build `find src -name '*.cc' -o -name '*.h'`
11 changes: 6 additions & 5 deletions src/block_sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ constexpr int kMaxTolerableBLER = 85;

constexpr int kMaxErrorsToleratedOver50Blocks{kMaxTolerableBLER / 2};

constexpr int kBlockLength = 26;
constexpr unsigned kBlockBitmask = (1 << kBlockLength) - 1;
constexpr int kCheckwordLength = 10;
constexpr std::uint32_t kBlockLength = 26;
constexpr std::uint32_t kBlockBitmask = (1U << kBlockLength) - 1U;
constexpr std::uint32_t kCheckwordLength = 10;

// Each offset word is associated with one block number
eBlockNumber getBlockNumberForOffset(Offset offset) {
Expand Down Expand Up @@ -113,7 +113,8 @@ uint32_t calculateSyndrome(uint32_t vec) {
uint32_t result{};

for (size_t k = 0; k < parity_check_matrix.size(); k++)
result ^= parity_check_matrix[parity_check_matrix.size() - 1 - k] * ((vec >> k) & 0b1);
result ^= static_cast<std::uint32_t>(parity_check_matrix[parity_check_matrix.size() - 1U - k] *
(static_cast<std::uint32_t>(vec >> k) & 1U));

return result;
}
Expand Down Expand Up @@ -237,7 +238,7 @@ void BlockStream::acquireSync(Block block) {
}

void BlockStream::pushBit(bool bit) {
input_register_ = (input_register_ << 1) + bit;
input_register_ = (input_register_ << 1U) + bit;
num_bits_until_next_block_--;
bitcount_++;

Expand Down
12 changes: 10 additions & 2 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@
namespace redsea {

constexpr float kBitsPerSecond = 1187.5f;
constexpr float kMinimumSampleRate_Hz = 128000.0f;
// Minimum sensible rate to still have RDS below Nyquist
constexpr float kMinimumSampleRate_Hz = 128'000.f;
constexpr int kNumBlerAverageGroups = 12;
constexpr float kTargetSampleRate_Hz = 171000.0f;
constexpr float kTargetSampleRate_Hz = 171'000.f;

// Limit of the resamp_rrrf object
constexpr float kLiquidMinimumResamplerRatio = 0.004f;
constexpr float kMaximumSampleRate_Hz = 40'000'000.f;
static_assert(kMaximumSampleRate_Hz < kTargetSampleRate_Hz / kLiquidMinimumResamplerRatio, "");

constexpr float kMaxResampleRatio = kTargetSampleRate_Hz / kMinimumSampleRate_Hz;

struct BitBuffer {
std::chrono::time_point<std::chrono::system_clock> time_received;
Expand Down
27 changes: 17 additions & 10 deletions src/dsp/liquid_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern "C" {
}
#pragma clang diagnostic pop

template<typename T>
template <typename T>
struct Maybe {
T data;
bool valid;
Expand All @@ -41,7 +41,9 @@ namespace liquid {
class AGC {
public:
AGC(float bw, float initial_gain);
AGC(const AGC&) = delete;
AGC(const AGC&) = delete;
AGC& operator=(const AGC&) = delete;

~AGC();
std::complex<float> execute(std::complex<float> s);

Expand All @@ -52,7 +54,9 @@ class AGC {
class FIRFilter {
public:
FIRFilter(unsigned int len, float fc, float As = 60.0f, float mu = 0.0f);
FIRFilter(const FIRFilter&) = delete;
FIRFilter(const FIRFilter&) = delete;
FIRFilter& operator=(const FIRFilter&) = delete;

~FIRFilter();
void push(std::complex<float> s);
std::complex<float> execute();
Expand All @@ -65,7 +69,8 @@ class FIRFilter {
class NCO {
public:
NCO(liquid_ncotype type, float freq);
NCO(const NCO&) = delete;
NCO(const NCO&) = delete;
NCO& operator=(const NCO&) = delete;
~NCO();
std::complex<float> mixDown(std::complex<float> s);
void step();
Expand All @@ -80,9 +85,9 @@ class NCO {

class SymSync {
public:
SymSync(liquid_firfilt_type ftype, unsigned k, unsigned m,
float beta, unsigned num_filters);
SymSync(const SymSync&) = delete;
SymSync(liquid_firfilt_type ftype, unsigned k, unsigned m, float beta, unsigned num_filters);
SymSync(const SymSync&) = delete;
SymSync& operator=(const SymSync&) = delete;
~SymSync();
void reset();
void setBandwidth(float);
Expand All @@ -97,7 +102,8 @@ class SymSync {
class Modem {
public:
explicit Modem(modulation_scheme scheme);
Modem(const Modem&) = delete;
Modem(const Modem&) = delete;
Modem& operator=(const Modem&) = delete;
~Modem();
unsigned int demodulate(std::complex<float> sample);
float getPhaseError();
Expand All @@ -113,7 +119,8 @@ class Modem {
class Resampler {
public:
explicit Resampler(float ratio, unsigned int length);
Resampler(const Resampler&) = delete;
Resampler(const Resampler&) = delete;
Resampler& operator=(const Resampler&) = delete;
~Resampler();
unsigned int execute(float in, float* out);

Expand All @@ -123,4 +130,4 @@ class Resampler {

} // namespace liquid

#endif // DSP_LIQUID_WRAPPERS_H_
#endif // DSP_LIQUID_WRAPPERS_H_
41 changes: 28 additions & 13 deletions src/dsp/subcarrier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
#include "src/dsp/subcarrier.h"

#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
Expand Down Expand Up @@ -51,7 +52,7 @@ constexpr float hertz2step(float Hz) {
// At correct clock phase, evaluate symbol in
// constellation {-1,0} => 0, {1,0} => 1
Maybe<bool> BiphaseDecoder::push(std::complex<float> psk_symbol) {
Maybe<bool> result;
Maybe<bool> result{};

const auto biphase_symbol = (psk_symbol - prev_psk_symbol_) * 0.5f;
result.data = biphase_symbol.real() >= 0.0f;
Expand Down Expand Up @@ -112,33 +113,47 @@ void Subcarrier::reset() {

/** MPX to bits
*/
BitBuffer Subcarrier::processChunk(MPXBuffer<>& chunk) {
BitBuffer Subcarrier::processChunk(MPXBuffer& input_chunk) {
if (resample_ratio_ != 1.0f) {
unsigned int i_resampled = 0;
for (size_t i = 0; i < chunk.used_size; i++) {
static float buf[4];
const auto num_resampled = resampler_.execute(chunk.data[i], buf);
std::size_t i_resampled{};

for (unsigned int j = 0; j < num_resampled; j++) {
resampled_buffer_.data[i_resampled] = buf[j];
// ceil(resample_ratio) is enough, as per liquid-dsp's API, but std::ceil is not constexpr in
// C++14
constexpr std::size_t kMaxResamplerOutputSize = static_cast<std::size_t>(kMaxResampleRatio) + 1;
std::array<float, kMaxResamplerOutputSize> resamp_output{};

for (size_t i_input{}; i_input < input_chunk.used_size; i_input++) {
const auto num_resampled =
resampler_.execute(input_chunk.data[i_input], resamp_output.data());

// Always true as per liquid-dsp API
assert(num_resampled <= resamp_output.size());

for (unsigned int i_buf{}; i_buf < num_resampled; i_buf++) {
// Must always be true due to our selection of maximum resampler ratio and extra room in
// the chunk
assert(i_resampled < resampled_chunk_.data.size());

resampled_chunk_.data[i_resampled] = resamp_output[i_buf];
i_resampled++;
}
}
resampled_buffer_.used_size = i_resampled;
resampled_chunk_.used_size = i_resampled;
assert(resampled_chunk_.used_size <= resampled_chunk_.data.size());
}

MPXBuffer<>& buf = (resample_ratio_ == 1.0f ? chunk : resampled_buffer_);
MPXBuffer& chunk = (resample_ratio_ == 1.0f ? input_chunk : resampled_chunk_);

constexpr int decimate_ratio =
static_cast<int>(kTargetSampleRate_Hz / kBitsPerSecond / 2 / kSamplesPerSymbol);

BitBuffer bitbuffer;
bitbuffer.time_received = chunk.time_received;
bitbuffer.time_received = input_chunk.time_received;

for (size_t i = 0; i < buf.used_size; i++) {
for (size_t i = 0; i < chunk.used_size; i++) {
// Mix RDS to baseband for filtering purposes
const std::complex<float> sample_baseband =
oscillator_.mixDown(std::complex<float>(buf.data[i]));
oscillator_.mixDown(std::complex<float>(chunk.data[i]));

fir_lpf_.push(sample_baseband);

Expand Down
4 changes: 2 additions & 2 deletions src/dsp/subcarrier.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Subcarrier {
explicit Subcarrier(const Options& options);
~Subcarrier() = default;
bool eof() const;
BitBuffer processChunk(MPXBuffer<>& chunk);
BitBuffer processChunk(MPXBuffer& input_chunk);
void reset();
float getSecondsSinceLastReset() const;

Expand All @@ -72,7 +72,7 @@ class Subcarrier {
liquid::Modem modem_;
liquid::Resampler resampler_;

MPXBuffer<> resampled_buffer_{};
MPXBuffer resampled_chunk_{};

bool is_eof_{false};

Expand Down
Loading

0 comments on commit caf3eb3

Please sign in to comment.