mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-02-04 17:05:24 -05:00
Fast log2 implementation.
Big improvement in code size (2034 bytes!). Baseband FFT (idle) thread CPU cut in half.
This commit is contained in:
parent
cdd15fcf79
commit
5cd423bb20
@ -22,7 +22,6 @@
|
|||||||
#include "utility.hpp"
|
#include "utility.hpp"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
uint32_t gcd(const uint32_t u, const uint32_t v) {
|
uint32_t gcd(const uint32_t u, const uint32_t v) {
|
||||||
@ -60,6 +59,20 @@ uint32_t gcd(const uint32_t u, const uint32_t v) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static float fast_log2(const float val) {
|
||||||
|
// Thank you Stack Overflow!
|
||||||
|
// http://stackoverflow.com/questions/9411823/fast-log2float-x-implementation-c
|
||||||
|
union {
|
||||||
|
float val;
|
||||||
|
int32_t x;
|
||||||
|
} u = { val };
|
||||||
|
float log_2 = (((u.x >> 23) & 255) - 128);
|
||||||
|
u.x &= ~(255 << 23);
|
||||||
|
u.x += (127 << 23);
|
||||||
|
log_2 += ((-0.34484843f) * u.val + 2.02466578f) * u.val - 0.67487759f;
|
||||||
|
return log_2;
|
||||||
|
}
|
||||||
|
|
||||||
float complex16_mag_squared_to_dbv_norm(const float c16_mag_squared) {
|
float complex16_mag_squared_to_dbv_norm(const float c16_mag_squared) {
|
||||||
constexpr float input_component_max = 32768;
|
constexpr float input_component_max = 32768;
|
||||||
constexpr float mag2_max = (input_component_max * input_component_max) * 2;
|
constexpr float mag2_max = (input_component_max * input_component_max) * 2;
|
||||||
@ -68,7 +81,7 @@ float complex16_mag_squared_to_dbv_norm(const float c16_mag_squared) {
|
|||||||
constexpr float log2_log10_factor = std::log10(2.0f);
|
constexpr float log2_log10_factor = std::log10(2.0f);
|
||||||
constexpr float log10_dbv_factor = 20.0f;
|
constexpr float log10_dbv_factor = 20.0f;
|
||||||
constexpr float mag2_to_db_factor = log_mag2_mag_factor * log2_log10_factor * log10_dbv_factor;
|
constexpr float mag2_to_db_factor = log_mag2_mag_factor * log2_log10_factor * log10_dbv_factor;
|
||||||
return (std::log2(c16_mag_squared) - mag2_log2_max) * mag2_to_db_factor;
|
return (fast_log2(c16_mag_squared) - mag2_log2_max) * mag2_to_db_factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* GCD implementation derived from recursive implementation at
|
/* GCD implementation derived from recursive implementation at
|
||||||
|
Loading…
x
Reference in New Issue
Block a user