Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup lmms_math.h #7382

Merged
merged 36 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5c5ec5d
simplified fraction and absfraction functions
Rossmaxx Jul 16, 2024
1dded27
removed unused fastSqrt() function
Rossmaxx Jul 16, 2024
e30614c
unused absMin() and absMax()
Rossmaxx Jul 16, 2024
d845331
simplified numDigitsAsInt() function
Rossmaxx Jul 16, 2024
b1bc173
removed fastPow()
Rossmaxx Jul 16, 2024
0853f5a
move roundAt to math header
Rossmaxx Jul 16, 2024
41b1e86
use lmms namespace for roundAt
Rossmaxx Jul 16, 2024
6e7be74
fixed compilation
Rossmaxx Jul 16, 2024
2ab7411
patch up calculation flaw
Rossmaxx Jul 16, 2024
e4614c3
patch up calculation flaw
Rossmaxx Jul 16, 2024
311941c
Stupid semicolon miss
Rossmaxx Jul 16, 2024
aa1408f
Fixed tests failing.
Rossmaxx Jul 16, 2024
bb82c0d
Negate diff
Rossmaxx Jul 16, 2024
9287e4e
Another semicolon
Rossmaxx Jul 16, 2024
cb069f9
Code review from saker
Rossmaxx Jul 17, 2024
cb3be41
it's const
Rossmaxx Jul 19, 2024
7ebb8c2
use std::trunc()
Rossmaxx Jul 19, 2024
30c61cb
Merge branch 'master' into cleanup-lmms-math
Rossmaxx Aug 9, 2024
49d452a
fixup after fixing merge conflicts
Rossmaxx Aug 9, 2024
ca613d0
remove unused fastFma and fastFmal functions.
Rossmaxx Aug 9, 2024
05af0eb
don't fix style in unmodified line
Rossmaxx Aug 9, 2024
dea9ae8
remove lmms_basics include, not needed
Rossmaxx Aug 9, 2024
164f788
some negation of diff
Rossmaxx Aug 9, 2024
f24f713
use signedPowf from lmms_math in NES
Rossmaxx Aug 9, 2024
21477bc
removed fastRand function, unused
Rossmaxx Aug 9, 2024
8d996d1
remove fastFmal
Rossmaxx Aug 9, 2024
3782334
remove unused sinc function
Rossmaxx Aug 9, 2024
7131d1c
cleanup signedPowf
Rossmaxx Aug 9, 2024
c6b60df
fix build
Rossmaxx Aug 9, 2024
3175e81
add copyright
Rossmaxx Aug 9, 2024
189ae65
more style
Rossmaxx Aug 9, 2024
9857493
code review
Rossmaxx Aug 11, 2024
cf2d996
further simplify random number math
Rossmaxx Aug 27, 2024
de57764
nuke unnecessary brackets
Rossmaxx Aug 27, 2024
f0e4d43
static cast
Rossmaxx Aug 27, 2024
af7deeb
removed static from lmms_math file
Rossmaxx Aug 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 25 additions & 116 deletions include/lmms_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@
#include <cmath>
#include <cstdint>

#include "lmms_basics.h"
#include "lmms_constants.h"
#include "lmmsconfig.h"
#include <cassert>

namespace lmms
{

#ifdef __INTEL_COMPILER

static inline float absFraction( const float _x )
{
return( _x - floorf( _x ) );
}

static inline float fraction( const float _x )
/*!
* @brief Returns the fractional part of a float, a value between -1.0f and 1.0f.
*
* fraction( 2.3) => 0.3
* fraction(-2.3) => -0.3
*
* Note that if the return value is used as a phase of an oscillator, that the oscillator must support
* negative phases.
*/
static inline float fraction(float x)
{
return( _x - floorf( _x ) - ( _x >= 0.0f ? 0.0 : 1.0 ) );
return x - static_cast<int>(x);
}

#else

/*!
* @brief Returns the wrapped fractional part of a float, a value between 0.0f and 1.0f.
*
Expand All @@ -61,68 +61,12 @@ static inline float fraction( const float _x )
* If the result is interpreted as a phase of an oscillator, it makes that negative phases are
* converted to positive phases.
*/
static inline float absFraction( const float _x )
{
return( _x - ( _x >= 0.0f ? static_cast<int>( _x ) :
static_cast<int>( _x ) - 1 ) );
}

/*!
* @brief Returns the fractional part of a float, a value between -1.0f and 1.0f.
*
* fraction( 2.3) => 0.3
* fraction(-2.3) => -0.3
*
* Note that if the return value is used as a phase of an oscillator, that the oscillator must support
* negative phases.
*/
static inline float fraction( const float _x )
static inline float absFraction(const float x)
{
return( _x - static_cast<int>( _x ) );
return x - std::floor(x);
}


#if 0
// SSE3-version
static inline float absFraction( float _x )
{
unsigned int tmp;
asm(
"fld %%st\n\t"
"fisttp %1\n\t"
"fild %1\n\t"
"ftst\n\t"
"sahf\n\t"
"jae 1f\n\t"
"fld1\n\t"
"fsubrp %%st, %%st(1)\n\t"
"1:\n\t"
"fsubrp %%st, %%st(1)"
: "+t"( _x ), "=m"( tmp )
:
: "st(1)", "cc" );
return( _x );
}

static inline float absFraction( float _x )
{
unsigned int tmp;
asm(
"fld %%st\n\t"
"fisttp %1\n\t"
"fild %1\n\t"
"fsubrp %%st, %%st(1)"
: "+t"( _x ), "=m"( tmp )
:
: "st(1)" );
return( _x );
}
#endif

#endif // __INTEL_COMPILER



constexpr int FAST_RAND_MAX = 32767;
static inline int fast_rand()
{
Expand Down Expand Up @@ -181,17 +125,14 @@ static inline double fastFma( double a, double b, double c )
#endif
}

// source: http://martin.ankerl.com/2007/10/04/optimized-pow-approximation-for-java-and-c-c/
static inline double fastPow( double a, double b )
//! Round `value` to `where` depending on step size
template<class T>
static void roundAt(T& value, const T& where, const T& stepSize)
{
union
if (std::abs(value - where) < typeInfo<float>::minEps() * std::abs(stepSize))
{
double d;
int32_t x[2];
} u = { a };
u.x[1] = static_cast<int32_t>( b * ( u.x[1] - 1072632447 ) + 1072632447 );
u.x[0] = 0;
return u.d;
value = where;
}
}

// sinc function
Expand Down Expand Up @@ -300,33 +241,6 @@ static inline float sqrt_neg( float val )
}


// fast approximation of square root
static inline float fastSqrt( float n )
{
union
{
int32_t i;
float f;
} u;
u.f = n;
u.i = ( u.i + ( 127 << 23 ) ) >> 1;
return u.f;
}

//! returns value furthest from zero
template<class T>
static inline T absMax( T a, T b )
{
return std::abs(a) > std::abs(b) ? a : b;
}

//! returns value nearest to zero
template<class T>
static inline T absMin( T a, T b )
{
return std::abs(a) < std::abs(b) ? a : b;
}

//! Returns the linear interpolation of the two values
template<class T, class F>
constexpr T lerp(T a, T b, F t)
Expand All @@ -338,23 +252,18 @@ constexpr T lerp(T a, T b, F t)
// @note Once we upgrade to C++20, we could probably use std::formatted_size
static inline int numDigitsAsInt(float f)
{
// use rounding:
// LcdSpinBox sometimes uses roundf(), sometimes cast rounding
// we use rounding to be on the "safe side"
const float rounded = roundf(f);
int asInt = static_cast<int>(rounded);
int asInt = static_cast<int>(std::round(f));
int digits = 1; // always at least 1
if(asInt < 0)
if (asInt < 0)
{
++digits;
asInt = -asInt;
}
// "asInt" is positive from now
int32_t power = 1;
for(int32_t i = 1; i<10; ++i)
int power = 1;
for (int i = 1; i < 10; ++i)
{
power *= 10;
if(static_cast<int32_t>(asInt) >= power) { ++digits; } // 2 digits for >=10, 3 for >=100
if (asInt >= power) { ++digits; } // 2 digits for >=10, 3 for >=100
else { break; }
}
return digits;
Expand Down
4 changes: 2 additions & 2 deletions plugins/Kicker/KickerOsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class KickerOsc
{
for( fpp_t frame = 0; frame < frames; ++frame )
{
const double gain = ( 1 - fastPow( ( m_counter < m_length ) ? m_counter / m_length : 1, m_env ) );
const double gain = 1 - std::pow((m_counter < m_length) ? m_counter / m_length : 1, m_env);
const sample_t s = ( Oscillator::sinSample( m_phase ) * ( 1 - m_noise ) ) + ( Oscillator::noiseSample( 0 ) * gain * gain * m_noise );
buf[frame][0] = s * gain;
buf[frame][1] = s * gain;
Expand All @@ -80,7 +80,7 @@ class KickerOsc
m_FX.nextSample( buf[frame][0], buf[frame][1] );
m_phase += m_freq / sampleRate;

const double change = ( m_counter < m_length ) ? ( ( m_startFreq - m_endFreq ) * ( 1 - fastPow( m_counter / m_length, m_slope ) ) ) : 0;
const double change = (m_counter < m_length) ? ((m_startFreq - m_endFreq) * (1 - std::pow(m_counter / m_length, m_slope))) : 0;
m_freq = m_endFreq + change;
++m_counter;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/Monstro/Monstro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ inline sample_t MonstroSynth::calcSlope( int slope, sample_t s )
{
if( m_parent->m_slope[slope] == 1.0f ) return s;
if( s == 0.0f ) return s;
return fastPow( s, m_parent->m_slope[slope] );
return std::pow(s, m_parent->m_slope[slope]);
}


Expand Down
13 changes: 0 additions & 13 deletions src/core/AutomatableModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,6 @@ float AutomatableModel::inverseScaledValue( float value ) const



//! @todo: this should be moved into a maths header
template<class T>
void roundAt( T& value, const T& where, const T& step_size )
{
if (std::abs(value - where)
< typeInfo<float>::minEps() * std::abs(step_size))
{
value = where;
}
}




template<class T>
void AutomatableModel::roundAt( T& value, const T& where ) const
Expand Down
Loading