Initial commit
This commit is contained in:
22
include/trigdx/interface.hpp
Normal file
22
include/trigdx/interface.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
// Base interface for all math backends
|
||||
class Backend {
|
||||
public:
|
||||
virtual ~Backend() = default;
|
||||
|
||||
// Optional initialization
|
||||
virtual void init() {}
|
||||
|
||||
// Compute sine for n elements
|
||||
virtual void compute_sinf(size_t n, const float *x, float *s) const = 0;
|
||||
|
||||
// Compute cosine for n elements
|
||||
virtual void compute_cosf(size_t n, const float *x, float *c) const = 0;
|
||||
|
||||
// Compute sine and cosine for n elements
|
||||
virtual void compute_sincosf(size_t n, const float *x, float *s,
|
||||
float *c) const = 0;
|
||||
};
|
||||
22
include/trigdx/lookup.hpp
Normal file
22
include/trigdx/lookup.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
#include "interface.hpp"
|
||||
|
||||
template <size_t NR_SAMPLES> class LookupBackend : public Backend {
|
||||
public:
|
||||
void init() 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_sincosf(size_t n, const float *x, float *s,
|
||||
float *c) const override;
|
||||
|
||||
private:
|
||||
std::vector<float> lookup;
|
||||
static constexpr size_t MASK = NR_SAMPLES - 1;
|
||||
static constexpr float SCALE = NR_SAMPLES / (2.0f * float(M_PI));
|
||||
};
|
||||
|
||||
#include "lookup.tpp" // include implementation
|
||||
44
include/trigdx/lookup.tpp
Normal file
44
include/trigdx/lookup.tpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#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];
|
||||
}
|
||||
}
|
||||
13
include/trigdx/mkl.hpp
Normal file
13
include/trigdx/mkl.hpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "interface.hpp"
|
||||
|
||||
class MKLBackend : public Backend {
|
||||
public:
|
||||
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_sincosf(size_t n, const float *x, float *s,
|
||||
float *c) const override;
|
||||
};
|
||||
13
include/trigdx/reference.hpp
Normal file
13
include/trigdx/reference.hpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "interface.hpp"
|
||||
|
||||
class ReferenceBackend : public Backend {
|
||||
public:
|
||||
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_sincosf(size_t n, const float *x, float *s,
|
||||
float *c) const override;
|
||||
};
|
||||
Reference in New Issue
Block a user