Skip to content

Commit 2c322d8

Browse files
committed
Optimize away unnecessary inverse sqrt
1 parent 40bf22c commit 2c322d8

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/rvoice/fluid_iir_filter.cpp

+23-11
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ static inline void fluid_iir_filter_calculate_coefficients(R fres,
7878
* bandwidth readjustment'. */
7979

8080
unsigned tab_idx = (fres - 1500) / CENTS_STEP;
81+
R rem = (fres - 1500) / (unsigned int)CENTS_STEP;
82+
LOG_FILTER("%f hz vs. %f hz",
83+
fluid_ct2hz((tab_idx * CENTS_STEP) + 1500),
84+
fluid_ct2hz(fres));
8185
R sin_coeff = sincos_table[tab_idx].sin;
8286
R cos_coeff = sincos_table[tab_idx].cos;
8387
R alpha_coeff = sin_coeff / (2.0f * q);
@@ -181,15 +185,6 @@ fluid_iir_filter_apply_local(fluid_iir_filter_t *iir_filter, fluid_real_t *dsp_b
181185
/* IIR filter sample history */
182186
fluid_real_t dsp_hist1 = iir_filter->hist1;
183187
fluid_real_t dsp_hist2 = iir_filter->hist2;
184-
185-
/* IIR filter coefficients */
186-
enum { N_COEFFS =
187-
#ifdef DBG_FILTER
188-
1
189-
#else
190-
16
191-
#endif
192-
};
193188

194189
/* IIR filter coefficients */
195190
float dsp_a1 = iir_filter->a1;
@@ -204,6 +199,11 @@ fluid_iir_filter_apply_local(fluid_iir_filter_t *iir_filter, fluid_real_t *dsp_b
204199
fluid_real_t dsp_amp_incr = iir_filter->amp_incr;
205200
float fres = iir_filter->last_fres;
206201
float q = iir_filter->last_q;
202+
float filter_gain = 1.0f;
203+
if (GAIN_NORM)
204+
{
205+
filter_gain /= std::sqrt(q);
206+
}
207207

208208
const float fres_incr = iir_filter->fres_incr;
209209
const float q_incr = iir_filter->q_incr;
@@ -253,15 +253,27 @@ fluid_iir_filter_apply_local(fluid_iir_filter_t *iir_filter, fluid_real_t *dsp_b
253253
--fres_incr_count;
254254
fres += fres_incr;
255255
}
256-
if(q_incr_count > 0)
256+
if (q_incr_count > 0)
257257
{
258258
--q_incr_count;
259259
q += q_incr;
260+
if (GAIN_NORM)
261+
{
262+
filter_gain = 1.0f / std::sqrt(q);
263+
}
264+
}
265+
266+
// Recalculate the filter coefficients without computing the inverse sqrt for the filter gain.
267+
// Recalculating the filter gain is only required, when the Q value changes. When only fres changes, use the last filter gain.
268+
fluid_iir_filter_calculate_coefficients<IIR_COEFF_T, false /*GAIN_NORM*/, TYPE>(fres, q, output_rate, &dsp_a1, &dsp_a2, &dsp_b02, &dsp_b1);
269+
if (GAIN_NORM)
270+
{
271+
// b1 must always be written back to global memory with the gain factor multiplied in
272+
dsp_b1 *= filter_gain;
260273
}
261274

262275
LOG_FILTER("last_fres: %.2f Hz | target_fres: %.2f Hz |---| last_q: %.4f | target_q: %.4f", iir_filter->last_fres, iir_filter->target_fres, iir_filter->last_q, iir_filter->target_q);
263276

264-
fluid_iir_filter_calculate_coefficients<IIR_COEFF_T, GAIN_NORM, TYPE>(fres, q, output_rate, &dsp_a1, &dsp_a2, &dsp_b02, &dsp_b1);
265277
}
266278
}
267279

0 commit comments

Comments
 (0)