Skip to content

Commit

Permalink
Move to using the FastMath from BasicBlocks
Browse files Browse the repository at this point in the history
part of the big SC/XT refactor share project I'm chipping away
at and also aprt of surge-synthesizer/surge#6532

This will make this library depend on sst-basic-blocks (and that dependency
will get stronger as we move forward). Youc an auto-grab it with CPM if you
want.
  • Loading branch information
baconpaul committed Apr 17, 2023
1 parent a0ce9e8 commit 6cae48d
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 243 deletions.
23 changes: 23 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ option(SST_FILTERS_BUILD_EXAMPLES "Add targets for building and running sst-filt

if (SST_FILTERS_BUILD_TESTS OR SST_FILTERS_BUILD_EXAMPLES)
message(STATUS "Importing SIMDE with CPM")

if (NOT TARGET sst-basic-blocks)
CPMAddPackage(NAME sst-basic-blocks
GITHUB_REPOSITORY surge-synthesizer/sst-basic-blocks
GIT_TAG main
)
endif()

CPMAddPackage(NAME simde
GITHUB_REPOSITORY simd-everywhere/simde
VERSION 0.7.2
Expand All @@ -37,10 +45,25 @@ if (SST_FILTERS_BUILD_TESTS OR SST_FILTERS_BUILD_EXAMPLES)
target_include_directories(simde INTERFACE ${simde_SOURCE_DIR})
endif ()

if (SST_GET_BASIC_BLOCKS)
CPMAddPackage(NAME sst-basic-blocks
GITHUB_REPOSITORY surge-synthesizer/sst-basic-blocks
GIT_TAG main
)
endif()

if (NOT TARGET sst-basic-blocks)
message(FATAL_ERROR "sst-basic-blocks is not available in this context. Set SST_GET_BASIC_BLOCKS=1 or add it")
else()
target_link_libraries(${PROJECT_NAME} INTERFACE sst-basic-blocks)
endif()

if (SST_FILTERS_BUILD_TESTS)
add_subdirectory(tests)
endif ()

if(SST_FILTERS_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()


8 changes: 4 additions & 4 deletions include/sst/filters/CutoffWarp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "QuadFilterUnit.h"
#include "FilterCoefficientMaker.h"
#include "sst/utilities/basic_dsp.h"
#include "sst/utilities/FastMath.h"
#include "sst/basic-blocks/dsp/FastMath.h"

/**
* This namespace contains an adaptation of the filter found at
Expand Down Expand Up @@ -89,7 +89,7 @@ static inline __m128 doNLFilter(const __m128 input, const __m128 a1, const __m12
nf = ojd_waveshaper_ps(out);
break;
default: // SAT_TANH; the removed SAT_SINE and others are also caught here
nf = utilities::DSP::fasttanhSSEclamped(out);
nf = basic_blocks::dsp::fasttanhSSEclamped(out);
break;
}

Expand Down Expand Up @@ -136,8 +136,8 @@ void makeCoefficients(FilterCoefficientMaker<TuningProvider> *cm, float freq, fl
const float normalisedFreq = 2.0f * clampedFrequency(freq, sampleRate, provider) / sampleRate;
const float wc = (float)M_PI * normalisedFreq;

const float wsin = utilities::DSP::fastsin(wc);
const float wcos = utilities::DSP::fastcos(wc);
const float wsin = basic_blocks::dsp::fastsin(wc);
const float wcos = basic_blocks::dsp::fastcos(wc);
const float alpha = wsin / (2.0f * q);

// note we actually calculate the reciprocal of a0 because we only use a0 to normalize the
Expand Down
3 changes: 2 additions & 1 deletion include/sst/filters/DiodeLadder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SST_FILTERS_DIODELADDER_H

#include "sst/utilities/basic_dsp.h"
#include "sst/basic-blocks/dsp/FastMath.h"
#include "QuadFilterUnit.h"
#include "FilterCoefficientMaker.h"

Expand Down Expand Up @@ -79,7 +80,7 @@ void makeCoefficients(FilterCoefficientMaker<TuningProvider> *cm, float freq, fl
float sampleRate, float sampleRateInv, TuningProvider *provider)
{
const float wd = clampedFrequency(freq, sampleRate, provider) * 2.0f * (float)M_PI;
const float wa = (2.0f * sampleRate) * utilities::DSP::fasttan(wd * sampleRateInv * 0.5f);
const float wa = (2.0f * sampleRate) * basic_blocks::dsp::fasttan(wd * sampleRateInv * 0.5f);
const float g = wa * sampleRateInv * 0.5f;

const float G4 = 0.5f * g / (1.0f + g);
Expand Down
8 changes: 4 additions & 4 deletions include/sst/filters/K35Filter.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef SST_FILTERS_K35FILTER_H
#define SST_FILTERS_K35FILTER_H

#include "sst/utilities/FastMath.h"
#include "sst/basic-blocks/dsp/FastMath.h"
#include "QuadFilterUnit.h"
#include "FilterCoefficientMaker.h"

Expand Down Expand Up @@ -72,7 +72,7 @@ void makeCoefficients(FilterCoefficientMaker<TuningProvider> *cm, float freq, fl
float C[n_cm_coeffs];

const float wd = clampedFrequency(freq, sampleRate, provider) * 2.0f * (float)M_PI;
const float wa = (2.0f * sampleRate) * utilities::DSP::fasttan(wd * sampleRateInv * 0.5f);
const float wa = (2.0f * sampleRate) * basic_blocks::dsp::fasttan(wd * sampleRateInv * 0.5f);
const float g = wa * sampleRateInv * 0.5f;
const float gp1 = (1.0f + g); // g plus 1
const float G = g / gp1;
Expand Down Expand Up @@ -120,7 +120,7 @@ inline __m128 process_lp(QuadFilterUnitState *__restrict f, __m128 input)
const __m128 s35 = A(M(f->C[k35_lb], f->R[k35_2z]), M(f->C[k35_hb], f->R[k35_hz]));
// alpha * (y1 + s35)
const __m128 u_clean = M(f->C[k35_alpha], A(y1, s35));
const __m128 u_driven = utilities::DSP::fasttanhSSEclamped(M(u_clean, f->C[k35_saturation]));
const __m128 u_driven = basic_blocks::dsp::fasttanhSSEclamped(M(u_clean, f->C[k35_saturation]));
const __m128 u =
A(M(u_clean, f->C[k35_saturation_blend_inv]), M(u_driven, f->C[k35_saturation_blend]));

Expand All @@ -145,7 +145,7 @@ inline __m128 process_hp(QuadFilterUnitState *__restrict f, __m128 input)

// mk * lpf2(u)
const __m128 y_clean = M(f->C[k35_k], u);
const __m128 y_driven = utilities::DSP::fasttanhSSEclamped(M(y_clean, f->C[k35_saturation]));
const __m128 y_driven = basic_blocks::dsp::fasttanhSSEclamped(M(y_clean, f->C[k35_saturation]));
const __m128 y =
A(M(y_clean, f->C[k35_saturation_blend_inv]), M(y_driven, f->C[k35_saturation_blend]));

Expand Down
10 changes: 5 additions & 5 deletions include/sst/filters/ResonanceWarp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "QuadFilterUnit.h"
#include "FilterCoefficientMaker.h"
#include "sst/utilities/basic_dsp.h"
#include "sst/utilities/FastMath.h"
#include "sst/basic-blocks/dsp/FastMath.h"

/**
* This contains an adaptation of the filter found at
Expand Down Expand Up @@ -53,8 +53,8 @@ static inline __m128 doNLFilter(const __m128 input, const __m128 a1, const __m12
switch (sat)
{
case SAT_TANH:
z1 = utilities::DSP::fasttanhSSEclamped(z1);
z2 = utilities::DSP::fasttanhSSEclamped(z2);
z1 = basic_blocks::dsp::fasttanhSSEclamped(z1);
z2 = basic_blocks::dsp::fasttanhSSEclamped(z2);
break;
default:
z1 = utilities::softclip_ps(z1); // note, this is a bit different to Jatin's softclipper
Expand Down Expand Up @@ -98,8 +98,8 @@ void makeCoefficients(FilterCoefficientMaker<TuningProvider> *cm, float freq, fl

const float wc = 2.0f * (float)M_PI * clampedFrequency(freq, sampleRate, provider) / sampleRate;

const float wsin = utilities::DSP::fastsin(wc);
const float wcos = utilities::DSP::fastcos(wc);
const float wsin = basic_blocks::dsp::fastsin(wc);
const float wcos = basic_blocks::dsp::fastcos(wc);
const float alpha = wsin / (2.0f * q);

// note we actually calculate the reciprocal of a0 because we only use a0 to normalize the
Expand Down
5 changes: 3 additions & 2 deletions include/sst/filters/TriPoleFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "QuadFilterUnit.h"
#include "FilterCoefficientMaker.h"
#include "sst/basic-blocks/dsp/FastMath.h"

/**
* This filter is an emulation of the "Threeler" VCF by
Expand Down Expand Up @@ -261,7 +262,7 @@ static inline __m128 res_func_ps(__m128 x)
auto x_less_than = _mm_cmplt_ps(x_abs, F(max_val));

auto y =
A(N(utilities::DSP::fastexpSSE(M(F(beta_exp), N(utilities::abs_ps(A(x, F(c))))))), F(bias));
A(N(basic_blocks::dsp::fastexpSSE(M(F(beta_exp), N(utilities::abs_ps(A(x, F(c))))))), F(bias));
y = M(sign_ps(x), M(y, F(oneOverMult)));

return _mm_or_ps(_mm_and_ps(x_less_than, M(x, F(oneOverMult))), _mm_andnot_ps(x_less_than, y));
Expand All @@ -274,7 +275,7 @@ static inline __m128 res_deriv_ps(__m128 x)
auto x_abs = utilities::abs_ps(x);
auto x_less_than = _mm_cmplt_ps(x_abs, F(max_val));

auto y = A(utilities::DSP::fastexpSSE(M(F(beta_exp), N(utilities::abs_ps(A(x, F(c)))))),
auto y = A(basic_blocks::dsp::fastexpSSE(M(F(beta_exp), N(utilities::abs_ps(A(x, F(c)))))),
F(betaExpOverMult));

return _mm_or_ps(_mm_and_ps(x_less_than, F(one)), _mm_andnot_ps(x_less_than, y));
Expand Down
10 changes: 5 additions & 5 deletions include/sst/filters/VintageLadders.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "sst/utilities/globals.h"
#include "sst/utilities/basic_dsp.h"
#include "sst/utilities/FastMath.h"
#include "sst/basic-blocks/dsp/FastMath.h"
#include "QuadFilterUnit.h"
#include "FilterCoefficientMaker.h"

Expand Down Expand Up @@ -365,7 +365,7 @@ inline __m128 process(QuadFilterUnitState *__restrict f, __m128 in)
auto acr = A(M(mneg39364, fc2), A(M(m18409, fc), m09968));

// auto tune = (1.0 - exp(-((2 * M_PI) * f * fcr))) / thermal;
auto tune = M(S(one, utilities::DSP::fastexpSSE(M(neg2pi, M(fr, fcr)))), oneoverthermal);
auto tune = M(S(one, basic_blocks::dsp::fastexpSSE(M(neg2pi, M(fr, fcr)))), oneoverthermal);
// auto resquad = 4.0 * res * arc;
auto resquad = M(four, M(res, acr));

Expand All @@ -380,7 +380,7 @@ inline __m128 process(QuadFilterUnitState *__restrict f, __m128 in)

// delay[0] = stage[0] = delay[0] + tune * (tanh(input * thermal) - stageTanh[0]);
f->R[h_stage + 0] =
A(f->R[h_delay + 0], M(tune, S(utilities::DSP::fasttanhSSEclamped(M(input, thermal)),
A(f->R[h_delay + 0], M(tune, S(basic_blocks::dsp::fasttanhSSEclamped(M(input, thermal)),
f->R[h_stageTanh + 0])));
f->R[h_delay + 0] = f->R[h_stage + 0];

Expand All @@ -391,11 +391,11 @@ inline __m128 process(QuadFilterUnitState *__restrict f, __m128 in)

// stage[k] = delay[k] + tune * ((stageTanh[k-1] = tanh(input * thermal)) - (k != 3 ?
// stageTanh[k] : tanh(delay[k] * thermal)));
f->R[h_stageTanh + k - 1] = utilities::DSP::fasttanhSSEclamped(M(input, thermal));
f->R[h_stageTanh + k - 1] = basic_blocks::dsp::fasttanhSSEclamped(M(input, thermal));
f->R[h_stage + k] =
A(f->R[h_delay + k], M(tune, S(f->R[h_stageTanh + k - 1],
(k != 3 ? f->R[h_stageTanh + k]
: utilities::DSP::fasttanhSSEclamped(
: basic_blocks::dsp::fasttanhSSEclamped(
M(f->R[h_delay + k], thermal))))));

// delay[k] = stage[k];
Expand Down
Loading

0 comments on commit 6cae48d

Please sign in to comment.