Skip to content

Commit

Permalink
[nutekt/minilogue-xd/prologue] Fixes and performance improvements for…
Browse files Browse the repository at this point in the history
… float_math.h
  • Loading branch information
Etienne Noreau-Hebert committed Jun 17, 2020
1 parent 8c02465 commit ab654af
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
9 changes: 5 additions & 4 deletions platform/minilogue-xd/inc/utils/float_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,15 @@ float fasterlog2f(float x) {
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fastlogf(float x) {
return 0.69314718f * fastlog2f(x);
return M_LN2 * fastlog2f(x);
}

/** "Fast" natural logarithm approximation, valid for positive x as precision allows.
* @note Adapted from Paul Mineiro's FastFloat
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fasterlogf(float x) {
return 0.69314718f * fasterlog2f(x);
return M_LN2 * fasterlog2f(x);
}

/** "Fast" power of 2 approximation, valid for x in [ -126, ... as precision allows.
Expand Down Expand Up @@ -789,7 +789,8 @@ float ampdbf(const float amp) {
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fasterampdbf(const float amp) {
return 20.f*fasterlog2f(amp)/fasterlog2f(10);
static const float c = 3.3219280948873626f; // 20.f / log2f(10);
return c*fasterlog2f(amp);
}

/** dB to ampltitude
Expand All @@ -803,7 +804,7 @@ float dbampf(const float db) {
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fasterdbampf(const float db) {
return 20.f*fasterpowf(10.f, 0.05f*db);
return fasterpowf(10.f, 0.05f*db);
}

/** @} */
Expand Down
9 changes: 5 additions & 4 deletions platform/nutekt-digital/inc/utils/float_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,15 @@ float fasterlog2f(float x) {
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fastlogf(float x) {
return 0.69314718f * fastlog2f(x);
return M_LN2 * fastlog2f(x);
}

/** "Fast" natural logarithm approximation, valid for positive x as precision allows.
* @note Adapted from Paul Mineiro's FastFloat
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fasterlogf(float x) {
return 0.69314718f * fasterlog2f(x);
return M_LN2 * fasterlog2f(x);
}

/** "Fast" power of 2 approximation, valid for x in [ -126, ... as precision allows.
Expand Down Expand Up @@ -789,7 +789,8 @@ float ampdbf(const float amp) {
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fasterampdbf(const float amp) {
return 20.f*fasterlog2f(amp)/fasterlog2f(10);
static const float c = 3.3219280948873626f; // 20.f / log2f(10);
return c*fasterlog2f(amp);
}

/** dB to ampltitude
Expand All @@ -803,7 +804,7 @@ float dbampf(const float db) {
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fasterdbampf(const float db) {
return 20.f*fasterpowf(10.f, 0.05f*db);
return fasterpowf(10.f, 0.05f*db);
}

/** @} */
Expand Down
9 changes: 5 additions & 4 deletions platform/prologue/inc/utils/float_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,15 @@ float fasterlog2f(float x) {
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fastlogf(float x) {
return 0.69314718f * fastlog2f(x);
return M_LN2 * fastlog2f(x);
}

/** "Fast" natural logarithm approximation, valid for positive x as precision allows.
* @note Adapted from Paul Mineiro's FastFloat
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fasterlogf(float x) {
return 0.69314718f * fasterlog2f(x);
return M_LN2 * fasterlog2f(x);
}

/** "Fast" power of 2 approximation, valid for x in [ -126, ... as precision allows.
Expand Down Expand Up @@ -789,7 +789,8 @@ float ampdbf(const float amp) {
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fasterampdbf(const float amp) {
return 20.f*fasterlog2f(amp)/fasterlog2f(10);
static const float c = 3.3219280948873626f; // 20.f / log2f(10);
return c*fasterlog2f(amp);
}

/** dB to ampltitude
Expand All @@ -803,7 +804,7 @@ float dbampf(const float db) {
*/
static inline __attribute__((optimize("Ofast"), always_inline))
float fasterdbampf(const float db) {
return 20.f*fasterpowf(10.f, 0.05f*db);
return fasterpowf(10.f, 0.05f*db);
}

/** @} */
Expand Down

0 comments on commit ab654af

Please sign in to comment.