Use Pimpl idiom for LookupBackend
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
project(trigdx LANGUAGES CXX)
|
project(trigdx LANGUAGES CXX)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 11)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
option(USE_MKL "Enable Intel MKL backend" OFF)
|
option(USE_MKL "Enable Intel MKL backend" OFF)
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cmath>
|
#include <cstddef>
|
||||||
#include <vector>
|
#include <memory>
|
||||||
|
|
||||||
#include "interface.hpp"
|
#include "interface.hpp"
|
||||||
|
|
||||||
template <size_t NR_SAMPLES> class LookupBackend : public Backend {
|
template <size_t NR_SAMPLES> class LookupBackend : public Backend {
|
||||||
public:
|
public:
|
||||||
|
LookupBackend();
|
||||||
|
~LookupBackend() override;
|
||||||
|
|
||||||
void init() override;
|
void init() override;
|
||||||
void compute_sinf(size_t n, const float *x, float *s) const override;
|
void compute_sinf(size_t n, const float *x, float *s) const override;
|
||||||
void compute_cosf(size_t n, const float *x, float *c) const override;
|
void compute_cosf(size_t n, const float *x, float *c) const override;
|
||||||
@@ -14,9 +17,6 @@ public:
|
|||||||
float *c) const override;
|
float *c) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<float> lookup;
|
struct Impl;
|
||||||
static constexpr size_t MASK = NR_SAMPLES - 1;
|
std::unique_ptr<Impl> impl;
|
||||||
static constexpr float SCALE = NR_SAMPLES / (2.0f * float(M_PI));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "lookup.tpp" // include implementation
|
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
template <size_t NR_SAMPLES>
|
|
||||||
void LookupBackend<NR_SAMPLES>::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));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t NR_SAMPLES>
|
|
||||||
void LookupBackend<NR_SAMPLES>::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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t NR_SAMPLES>
|
|
||||||
void LookupBackend<NR_SAMPLES>::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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <size_t NR_SAMPLES>
|
|
||||||
void LookupBackend<NR_SAMPLES>::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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
target_include_directories(trigdx PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
|
|||||||
72
src/lookup.cpp
Normal file
72
src/lookup.cpp
Normal 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>;
|
||||||
Reference in New Issue
Block a user