54 static_assert(Lanes == 2 || Lanes == 4 || Lanes == 8 || Lanes == 16,
"Lanes must be 2, 4, 8 or 16");
65 steps_ = (std::max(1, sections) + Lanes - 1) / Lanes;
66 sections_ = std::max(1, sections);
67 channels_ = std::max(1, channels);
68 const auto total =
static_cast<size_t>(steps_ * Lanes);
69 for (
auto* v : { &b0_, &b1_, &b2_, &a1_, &a2_, &db0_, &db1_, &db2_, &da1_, &da2_ })
70 v->assign(total, 0.0);
71 std::fill(b0_.begin(), b0_.end(), 1.0);
72 for (
auto* v : { &t0_, &t1_, &t2_, &u1_, &u2_ }) v->assign(total, 0.0);
73 std::fill(t0_.begin(), t0_.end(), 1.0);
75 state_.assign(
static_cast<size_t>(channels_), std::vector<double>(total * 4, 0.0));
76 pipe_.assign(
static_cast<size_t>(channels_), std::array<double, Lanes> {});
81 [[nodiscard]]
static constexpr int getLatency() noexcept {
return Lanes - 1; }
86 const size_t i = slot(section);
87 if (i == npos)
return;
88 b0_[i] = t0_[i] = c.b0; b1_[i] = t1_[i] = c.b1; b2_[i] = t2_[i] = c.b2;
89 a1_[i] = u1_[i] = c.a1; a2_[i] = u2_[i] = c.a2;
90 db0_[i] = db1_[i] = db2_[i] = da1_[i] = da2_[i] = 0.0;
99 const size_t i = slot(section);
100 if (i == npos)
return;
101 t0_[i] = c.b0; t1_[i] = c.b1; t2_[i] = c.b2; u1_[i] = c.a1; u2_[i] = c.a2;
110 const size_t total = b0_.size();
113 b0_ = t0_; b1_ = t1_; b2_ = t2_; a1_ = u1_; a2_ = u2_;
117 const double inv = 1.0 / samples;
118 for (
size_t i = 0; i < total; ++i)
120 db0_[i] = (t0_[i] - b0_[i]) * inv; db1_[i] = (t1_[i] - b1_[i]) * inv; db2_[i] = (t2_[i] - b2_[i]) * inv;
121 da1_[i] = (u1_[i] - a1_[i]) * inv; da2_[i] = (u2_[i] - a2_[i]) * inv;
137 const size_t i = slot(section);
138 if (i == npos)
return c;
139 c.
b0 = b0_[i]; c.
b1 = b1_[i]; c.
b2 = b2_[i]; c.
a1 = a1_[i]; c.
a2 = a2_[i];
146 for (
auto& s : state_) std::fill(s.begin(), s.end(), 0.0);
147 for (
auto& p : pipe_) p.fill(0.0);
153 const int nCh = std::min(buffer.getNumChannels(), channels_);
154 const int nS = buffer.getNumSamples();
155 for (
int n = 0; n < nS; ++n)
157 for (
int ch = 0; ch < nCh; ++ch)
159 T* data = buffer.getChannel(ch);
160 data[n] =
static_cast<T
>(step(ch,
static_cast<double>(data[n])));
162 if (rampLeft_ > 0) advanceRamp();
167 static constexpr size_t npos =
static_cast<size_t>(-1);
170 [[nodiscard]]
size_t slot(
int section)
const noexcept
172 if (section < 0 || section >= steps_ * Lanes)
return npos;
173 return static_cast<size_t>((section % steps_) * Lanes + section / steps_);
176 double step(
int ch,
double x)
noexcept
178 auto& pipe = pipe_[
static_cast<size_t>(ch)];
179 double* st = state_[
static_cast<size_t>(ch)].data();
180 alignas(64) std::array<double, Lanes> v {};
182 for (
int j = 1; j < Lanes; ++j) v[static_cast<size_t>(j)] = pipe[
static_cast<size_t>(j - 1)];
183 constexpr int W = std::min(simd::kVecWidth<double>, Lanes);
184 using O = simd::Vec<double, W>;
185 using V =
typename O::V;
186 for (
int k = 0; k < steps_; ++k)
188 const size_t base =
static_cast<size_t>(k * Lanes);
189 double* x1 = st + 4 * base;
190 double* x2 = x1 + Lanes;
191 double* y1 = x2 + Lanes;
192 double* y2 = y1 + Lanes;
193 for (
int j = 0; j < Lanes; j += W)
195 const auto i = base +
static_cast<size_t>(j);
196 const V in = O::load(v.data() + j);
197 const V px1 = O::load(x1 + j), px2 = O::load(x2 + j);
198 const V py1 = O::load(y1 + j), py2 = O::load(y2 + j);
199 const V ff = O::madd(O::load(b0_.data() + i), in,
200 O::madd(O::load(b1_.data() + i), px1, O::mul(O::load(b2_.data() + i), px2)));
201 const V fb = O::madd(O::load(a1_.data() + i), py1, O::mul(O::load(a2_.data() + i), py2));
202 const V y = O::sub(ff, fb);
203 O::store(x2 + j, px1);
204 O::store(x1 + j, in);
205 O::store(y2 + j, py1);
207 O::store(v.data() + j, y);
210 for (
int j = 0; j < Lanes; ++j) pipe[static_cast<size_t>(j)] = v[
static_cast<size_t>(j)];
211 return v[
static_cast<size_t>(Lanes - 1)];
214 void advanceRamp() noexcept
216 if (--rampLeft_ == 0)
218 b0_ = t0_; b1_ = t1_; b2_ = t2_; a1_ = u1_; a2_ = u2_;
221 const size_t total = b0_.size();
222 for (
size_t i = 0; i < total; ++i)
224 b0_[i] += db0_[i]; b1_[i] += db1_[i]; b2_[i] += db2_[i];
225 a1_[i] += da1_[i]; a2_[i] += da2_[i];
229 int steps_ = 1, sections_ = 1, channels_ = 1;
231 std::vector<double> b0_, b1_, b2_, a1_, a2_, db0_, db1_, db2_, da1_, da2_;
232 std::vector<double> t0_, t1_, t2_, u1_, u2_;
233 std::vector<std::vector<double>> state_;
234 std::vector<std::array<double, Lanes>> pipe_;