Update math functions to C++ standard library (#7685)
* use c++ std::* math functions This updates usages of sin, cos, tan, pow, exp, log, log10, sqrt, fmod, fabs, and fabsf, excluding any usages that look like they might be part of a submodule or 3rd-party code. There's probably some std math functions not listed here that haven't been updated yet. * fix std::sqrt typo lmao one always sneaks by * Apply code review suggestions - std::pow(2, x) -> std::exp2(x) - std::pow(10, x) -> lmms::fastPow10f(x) - std::pow(x, 2) -> x * x, std::pow(x, 3) -> x * x * x, etc. - Resolve TODOs, fix typos, and so forth Co-authored-by: Rossmaxx <74815851+Rossmaxx@users.noreply.github.com> * Fix double -> float truncation, DrumSynth fix I mistakenly introduced a bug in my recent PR regarding template constants, in which a -1 that was supposed to appear outside of an abs() instead was moved inside it, screwing up the generated waveform. I fixed that and also simplified the function by factoring out the phase domain wrapping using the new `ediv()` function from this PR. It should behave how it's supposed to now... assuming all my parentheses are in the right place lol * Annotate magic numbers with TODOs for C++20 * On second thought, why wait? What else is lmms::numbers for? * begone inline Co-authored-by: Rossmaxx <74815851+Rossmaxx@users.noreply.github.com> * begone other inline Co-authored-by: Rossmaxx <74815851+Rossmaxx@users.noreply.github.com> * Re-inline function in lmms_math.h For functions, constexpr implies inline so this just re-adds inline to the one that isn't constexpr yet * Formatting fixes, readability improvements Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com> * Fix previously missed pow() calls, cleanup Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com> * Just delete ediv() entirely lmao No ediv(), no std::fmod(), no std::remainder(), just std::floor(). It should all work for negative phase inputs as well. If I end up needing ediv() in the future, I can add it then. * Simplify DrumSynth triangle waveform This reuses more work and is also a lot more easy to visualize. It's probably a meaningless micro-optimization, but it might be worth changing it back to a switch-case and just calculating ph_tau and saw01 at the beginning of the function in all code paths, even if it goes unused for the first two cases. Guess I'll see if anybody has strong opinions about it. * Move multiplication inside abs() * Clean up a few more pow(x, 2) -> x * x * Remove numbers::inv_pi, numbers::inv_tau * delete spooky leading 0 Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com> --------- Co-authored-by: Rossmaxx <74815851+Rossmaxx@users.noreply.github.com> Co-authored-by: Dalton Messmer <messmer.dalton@gmail.com>
This commit is contained in:
@@ -806,8 +806,8 @@ public:
|
||||
// other filters
|
||||
_freq = std::clamp(_freq, minFreq(), 20000.0f);
|
||||
const float omega = numbers::tau_v<float> * _freq * m_sampleRatio;
|
||||
const float tsin = sinf( omega ) * 0.5f;
|
||||
const float tcos = cosf( omega );
|
||||
const float tsin = std::sin(omega) * 0.5f;
|
||||
const float tcos = std::cos(omega);
|
||||
|
||||
const float alpha = tsin / _q;
|
||||
|
||||
|
||||
@@ -289,7 +289,7 @@ namespace lmms::DspEffectLibrary
|
||||
{
|
||||
if( in >= m_threshold || in < -m_threshold )
|
||||
{
|
||||
return ( fabsf( fabsf( fmodf( in - m_threshold, m_threshold*4 ) ) - m_threshold*2 ) - m_threshold ) * m_gain;
|
||||
return (std::abs(std::abs(std::fmod(in - m_threshold, m_threshold * 4)) - m_threshold * 2) - m_threshold) * m_gain;
|
||||
}
|
||||
return in * m_gain;
|
||||
}
|
||||
@@ -303,7 +303,7 @@ namespace lmms::DspEffectLibrary
|
||||
|
||||
sample_t nextSample( sample_t in )
|
||||
{
|
||||
return m_gain * ( in * ( fabsf( in )+m_threshold ) / ( in*in +( m_threshold-1 )* fabsf( in ) + 1 ) );
|
||||
return m_gain * (in * (std::abs(in) + m_threshold) / (in * in + (m_threshold - 1) * std::abs(in) + 1));
|
||||
}
|
||||
} ;
|
||||
|
||||
@@ -330,8 +330,8 @@ namespace lmms::DspEffectLibrary
|
||||
{
|
||||
const float toRad = numbers::pi_v<float> / 180;
|
||||
const sample_t tmp = inLeft;
|
||||
inLeft += inRight * sinf( m_wideCoeff * ( .5 * toRad ) );
|
||||
inRight -= tmp * sinf( m_wideCoeff * ( .5 * toRad ) );
|
||||
inLeft += inRight * std::sin(m_wideCoeff * toRad * .5f);
|
||||
inRight -= tmp * std::sin(m_wideCoeff * toRad * .5f);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -77,11 +77,10 @@ public:
|
||||
|
||||
void tick( float *l, float *r )
|
||||
{
|
||||
*l = sinf( m_phase );
|
||||
*r = sinf( m_phase + m_offset );
|
||||
*l = std::sin(m_phase);
|
||||
*r = std::sin(m_phase + m_offset);
|
||||
m_phase += m_increment;
|
||||
|
||||
while (m_phase >= numbers::tau) { m_phase -= numbers::tau; }
|
||||
while (m_phase >= numbers::tau) { m_phase -= numbers::tau; }
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -84,7 +84,7 @@ public:
|
||||
m_sum -= m_buffer[ m_pos ];
|
||||
m_sum += m_buffer[ m_pos ] = in * in;
|
||||
++m_pos %= m_size;
|
||||
return sqrtf( m_sum * m_sizef );
|
||||
return std::sqrt(m_sum * m_sizef);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -65,6 +65,7 @@ inline constexpr double inv_e = e_v<double>;
|
||||
|
||||
//TODO C++20: Use std::floating_point instead of typename
|
||||
//TODO C++20: Use std::numbers::sqrt2_v<T> instead of literal value
|
||||
//TODO C++26: Remove since std::sqrt(2.0) is constexpr
|
||||
template<typename T>
|
||||
inline constexpr T sqrt2_v = T(1.41421356237309504880168872420969807856967187537695);
|
||||
inline constexpr double sqrt2 = sqrt2_v<double>;
|
||||
|
||||
@@ -119,7 +119,7 @@ inline float sign(float val)
|
||||
}
|
||||
|
||||
|
||||
//! if val >= 0.0f, returns sqrtf(val), else: -sqrtf(-val)
|
||||
//! if val >= 0.0f, returns sqrt(val), else: -sqrt(-val)
|
||||
inline float sqrt_neg(float val)
|
||||
{
|
||||
return std::sqrt(std::abs(val)) * sign(val);
|
||||
|
||||
Reference in New Issue
Block a user