Optimise usage of pow using fast equivalent and exp2 (#7548)

* replace std::pow with better performing equivalents

* revert one instance where I swapped to fastPow10f

* Negative slope instead of multiplying -1

Co-authored-by: saker <sakertooth@gmail.com>

---------

Co-authored-by: saker <sakertooth@gmail.com>
This commit is contained in:
Rossmaxx
2024-11-21 14:29:51 +05:30
committed by GitHub
parent 7e65a87ba8
commit 26d5241dd7
6 changed files with 18 additions and 17 deletions

View File

@@ -237,7 +237,7 @@ public:
static inline float freqFromWaveTableBand(int band)
{
return 440.0f * std::pow(2.0f, (band * OscillatorConstants::SEMITONES_PER_TABLE - 69.0f) / 12.0f);
return 440.0f * std::exp2((band * OscillatorConstants::SEMITONES_PER_TABLE - 69.0f) / 12.0f);
}
private:

View File

@@ -352,14 +352,14 @@ void FreeBoyInstrument::playNote(NotePlayHandle* nph, SampleFrame* workingBuffer
// a unique frequency, we can start by guessing s = r = 0 here and then skip r = 0 in the loop.
char clock_freq = 0;
char div_ratio = 0;
float closest_freq = 524288.0 / (0.5 * std::pow(2.0, clock_freq + 1.0));
float closest_freq = 524288.0 / (0.5 * std::exp2(clock_freq + 1.0));
// This nested for loop iterates over all possible combinations of clock frequency and dividing
// ratio and chooses the combination whose resulting frequency is closest to the note frequency
for (char s = 0; s < 16; ++s)
{
for (char r = 1; r < 8; ++r)
{
float f = 524288.0 / (r * std::pow(2.0, s + 1.0));
float f = 524288.0 / (r * std::exp2(s + 1.0));
if (std::fabs(freq - closest_freq) > std::fabs(freq - f))
{
closest_freq = f;

View File

@@ -1462,14 +1462,14 @@ void MonstroInstrument::updateSamplerate()
void MonstroInstrument::updateSlope1()
{
const float slope = m_env1Slope.value();
m_slope[0] = std::pow(10.f, slope * -1.0f );
m_slope[0] = fastPow10f(-slope);
}
void MonstroInstrument::updateSlope2()
{
const float slope = m_env2Slope.value();
m_slope[1] = std::pow(10.f, slope * -1.0f );
m_slope[1] = fastPow10f(-slope);
}

View File

@@ -147,7 +147,7 @@ void MultitapEchoControls::lpSamplesChanged( int begin, int end )
const float * samples = m_lpGraph.samples();
for( int i = begin; i <= end; ++i )
{
m_effect->m_lpFreq[i] = 20.0f * std::pow(10.f, samples[i] );
m_effect->m_lpFreq[i] = 20.0f * fastPow10f(samples[i]);
}
m_effect->updateFilters( begin, end );
}

View File

@@ -214,7 +214,7 @@ void SlicerT::findSlices()
float magnitude = std::sqrt(real * real + imag * imag);
// using L2-norm (euclidean distance)
float diff = std::sqrt(std::pow(magnitude - prevMags[j], 2));
float diff = std::abs(magnitude - prevMags[j]);
spectralFlux += diff;
prevMags[j] = magnitude;

View File

@@ -30,6 +30,8 @@
#include <cstring>
#include <sstream>
#include "lmms_math.h"
#ifdef _MSC_VER
// not #if LMMS_BUILD_WIN32 because we have strncasecmp in mingw
#define strcasecmp _stricmp
@@ -403,13 +405,13 @@ int DrumSynth::GetDSFileSamples(QString dsfile, int16_t*& wave, int channels, sa
timestretch *= Fs / 44100.f;
DGain = 1.0f; // leave this here!
DGain = static_cast<float>(std::pow(10.0, 0.05 * GetPrivateProfileFloat(sec, "Level", 0, dsfile)));
DGain = fastPow10f(0.05 * GetPrivateProfileFloat(sec, "Level", 0, dsfile));
MasterTune = GetPrivateProfileFloat(sec, "Tuning", 0.0, dsfile);
MasterTune = static_cast<float>(std::pow(1.0594631f, MasterTune + mem_tune));
MasterTune = std::pow(1.0594631f, MasterTune + mem_tune);
MainFilter = 2 * GetPrivateProfileInt(sec, "Filter", 0, dsfile);
MFres = 0.0101f * GetPrivateProfileFloat(sec, "Resonance", 0.0, dsfile);
MFres = static_cast<float>(std::pow(MFres, 0.5f));
MFres = std::sqrt(MFres);
HighPass = GetPrivateProfileInt(sec, "HighPass", 0, dsfile);
GetEnv(7, sec, "FilterEnv", dsfile);
@@ -455,7 +457,7 @@ int DrumSynth::GetDSFileSamples(QString dsfile, int16_t*& wave, int channels, sa
TDroopRate = GetPrivateProfileFloat(sec, "Droop", 0.f, dsfile);
if (TDroopRate > 0.f)
{
TDroopRate = static_cast<float>(std::pow(10.0f, (TDroopRate - 20.0f) / 30.0f));
TDroopRate = fastPow10f((TDroopRate - 20.0f) / 30.0f);
TDroopRate = TDroopRate * -4.f / envData[1][MAX];
TDroop = 1;
F2 = F1 + ((F2 - F1) / (1.f - static_cast<float>(exp(TDroopRate * envData[1][MAX]))));
@@ -482,7 +484,7 @@ int DrumSynth::GetDSFileSamples(QString dsfile, int16_t*& wave, int channels, sa
OW1 = GetPrivateProfileInt(sec, "Wave1", 0, dsfile);
OW2 = GetPrivateProfileInt(sec, "Wave2", 0, dsfile);
OBal2 = static_cast<float>(GetPrivateProfileInt(sec, "Param", 50, dsfile));
ODrive = static_cast<float>(std::pow(OBal2, 3.0f)) / std::pow(50.0f, 3.0f);
ODrive = (OBal2 * OBal2 * OBal2) / 125000.0f;
OBal2 *= 0.01f;
OBal1 = 1.f - OBal2;
Ophi1 = Tphi;
@@ -556,9 +558,8 @@ int DrumSynth::GetDSFileSamples(QString dsfile, int16_t*& wave, int channels, sa
{
DAtten = DGain * static_cast<short>(LoudestEnv());
clippoint = DAtten > 32700 ? 32700 : static_cast<short>(DAtten);
DAtten = static_cast<float>(std::pow(2.0, 2.0 * GetPrivateProfileInt(sec, "Bits", 0, dsfile)));
DGain = DAtten * DGain
* static_cast<float>(std::pow(10.0, 0.05 * GetPrivateProfileInt(sec, "Clipping", 0, dsfile)));
DAtten = std::exp2(2.0 * GetPrivateProfileInt(sec, "Bits", 0, dsfile));
DGain = DAtten * DGain * fastPow10f(0.05 * GetPrivateProfileInt(sec, "Clipping", 0, dsfile));
}
// prepare envelopes
@@ -856,7 +857,7 @@ int DrumSynth::GetDSFileSamples(QString dsfile, int16_t*& wave, int channels, sa
MFtmp = envData[7][ENV];
if (MFtmp > 0.2f)
{
MFfb = 1.001f - static_cast<float>(std::pow(10.0f, MFtmp - 1));
MFfb = 1.001f - fastPow10f(MFtmp - 1);
}
else
{
@@ -883,7 +884,7 @@ int DrumSynth::GetDSFileSamples(QString dsfile, int16_t*& wave, int channels, sa
MFtmp = envData[7][ENV];
if (MFtmp > 0.2f)
{
MFfb = 1.001f - static_cast<float>(std::pow(10.0f, MFtmp - 1));
MFfb = 1.001f - fastPow10f(MFtmp - 1);
}
else
{