commit 0ee7d565d5986051c38d2b2a3e439221492eb7cb
parent c2e896a32a36a34fd536f3b1218c35238dad7ef1
Author: Matt Demanett <matt@demanett.net>
Date: Thu, 4 Jan 2018 22:41:57 -0500
Approximately double speed of sine oscillator in benchmark by rewriting it to allow vectorization.
Diffstat:
2 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/src/dsp/oscillator.cpp b/src/dsp/oscillator.cpp
@@ -8,13 +8,28 @@ void SineOscillator::updateDeltaTheta() {
float sampleTime = 1.0 / _sampleRate;
float cycleTime = 1.0 / _frequency;
float _deltaTheta = (sampleTime / cycleTime) * 2.0f * M_PI;
- _sinDeltaTheta = sinf(_deltaTheta);
- _cosDeltaTheta = cosf(_deltaTheta);
+ for (int i = 0; i < _n; ++i) {
+ _sinDeltaTheta[i] = sinf((i + 1) * _deltaTheta);
+ _cosDeltaTheta[i] = cosf((i + 1) * _deltaTheta);
+ }
}
float SineOscillator::_next() {
- float x2 = _x*_cosDeltaTheta - _y*_sinDeltaTheta;
- _y = _x*_sinDeltaTheta + _y*_cosDeltaTheta;
- _x = x2;
- return _y;
+ if (_step == 0) {
+ const int n1 = _n - 1;
+ for (int i = 0; i < n1; ++i) {
+ _x[i] = _x[n1];
+ _y[i] = _y[n1];
+ }
+
+ float t[_n];
+ for (int i = 0; i < _n; ++i) {
+ t[i] = _x[i]*_cosDeltaTheta[i] - _y[i]*_sinDeltaTheta[i];
+ _y[i] = _x[i]*_sinDeltaTheta[i] + _y[i]*_cosDeltaTheta[i];
+ _x[i] = t[i];
+ }
+ }
+ float out = _y[_step];
+ _step = ++_step % _n;
+ return out;
}
diff --git a/src/dsp/oscillator.hpp b/src/dsp/oscillator.hpp
@@ -35,10 +35,12 @@ struct OscillatorGenerator : Generator {
};
struct SineOscillator : OscillatorGenerator {
- float _x;
- float _y;
- float _sinDeltaTheta;
- float _cosDeltaTheta;
+ static const int _n = 4;
+ int _step = 0;
+ float _x[_n];
+ float _y[_n];
+ float _sinDeltaTheta[_n];
+ float _cosDeltaTheta[_n];
SineOscillator(
float sampleRate,
@@ -46,9 +48,11 @@ struct SineOscillator : OscillatorGenerator {
float amplitude = 1.0
)
: OscillatorGenerator(sampleRate, frequency)
- , _x(amplitude)
- , _y(0.0)
{
+ for (int i = 0; i < _n; ++i) {
+ _x[i] = amplitude;
+ _y[i] = 0.0;
+ }
updateDeltaTheta();
}