Use Pimpl idiom for LookupBackend

This commit is contained in:
Bram Veenboer
2025-08-01 14:01:14 +02:00
parent a189a80cc2
commit 1dfb2e928e
5 changed files with 81 additions and 53 deletions

View File

@@ -1,4 +1,4 @@
add_library(trigdx reference.cpp)
add_library(trigdx reference.cpp lookup.cpp)
target_include_directories(trigdx PUBLIC ${PROJECT_SOURCE_DIR}/include)

72
src/lookup.cpp Normal file
View File

@@ -0,0 +1,72 @@
#include <cmath>
#include <memory>
#include <vector>
#include "trigdx/lookup.hpp"
template <size_t NR_SAMPLES> struct LookupBackend<NR_SAMPLES>::Impl {
std::vector<float> lookup;
static constexpr size_t MASK = NR_SAMPLES - 1;
static constexpr float SCALE = NR_SAMPLES / (2.0f * float(M_PI));
void init() {
lookup.resize(NR_SAMPLES);
for (size_t i = 0; i < NR_SAMPLES; ++i)
lookup[i] = std::sinf(i * (2.0f * float(M_PI) / NR_SAMPLES));
}
void compute_sinf(size_t n, const float *x, float *s) const {
for (size_t i = 0; i < n; ++i) {
size_t idx = static_cast<size_t>(x[i] * SCALE) & MASK;
s[i] = lookup[idx];
}
}
void compute_cosf(size_t n, const float *x, float *c) const {
for (size_t i = 0; i < n; ++i) {
size_t idx = static_cast<size_t>(x[i] * SCALE) & MASK;
size_t idx_cos = (idx + NR_SAMPLES / 4) & MASK;
c[i] = lookup[idx_cos];
}
}
void compute_sincosf(size_t n, const float *x, float *s, float *c) const {
for (size_t i = 0; i < n; ++i) {
size_t idx = static_cast<size_t>(x[i] * SCALE) & MASK;
size_t idx_cos = (idx + NR_SAMPLES / 4) & MASK;
s[i] = lookup[idx];
c[i] = lookup[idx_cos];
}
}
};
template <size_t NR_SAMPLES>
LookupBackend<NR_SAMPLES>::LookupBackend() : impl(std::make_unique<Impl>()) {}
template <size_t NR_SAMPLES>
LookupBackend<NR_SAMPLES>::~LookupBackend() = default;
template <size_t NR_SAMPLES> void LookupBackend<NR_SAMPLES>::init() {
impl->init();
}
template <size_t NR_SAMPLES>
void LookupBackend<NR_SAMPLES>::compute_sinf(size_t n, const float *x,
float *s) const {
impl->compute_sinf(n, x, s);
}
template <size_t NR_SAMPLES>
void LookupBackend<NR_SAMPLES>::compute_cosf(size_t n, const float *x,
float *c) const {
impl->compute_cosf(n, x, c);
}
template <size_t NR_SAMPLES>
void LookupBackend<NR_SAMPLES>::compute_sincosf(size_t n, const float *x,
float *s, float *c) const {
impl->compute_sincosf(n, x, s, c);
}
template class LookupBackend<16384>;
template class LookupBackend<32768>;