Audacity 3.2.0
Functions
MathApprox.h File Reference
#include <cstdint>
Include dependency graph for MathApprox.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

constexpr float FastLog2 (float x)
 Approximates the base-2 logarithm of a float to two decimal places, adapted from https://stackoverflow.com/a/28730362. More...
 

Function Documentation

◆ FastLog2()

constexpr float FastLog2 ( float  x)
constexpr

Approximates the base-2 logarithm of a float to two decimal places, adapted from https://stackoverflow.com/a/28730362.


Audacity: A Digital Audio Editor

MathApprox.h

Matthieu Hodgkinson

Accuracy claim is demonstated in MathApproxTest.cpp, and coincides with @Paul-Licameli's analysis (copied from conversation): "If x == 1.0f, then log2 before the += correction actually becomes -1 &ndash; because a zero exponent of two is represented by 127 in the eight exponent bits, but one more was subtracted. > u.x by that point has been normalized to the range [1.0f, 2.0f), by changing the exponent, multiplying it by an integer power of 2. > So the correction must add 1 &ndash; so that log2(1) comes out (approximately) zero &ndash; but then, add a quadratic polynomial in u.x, a*x*x + b*x + c, where a is -0.3358287811f, b is 2, c is -1.65871759316667f (the magic number as written, minus 1) > Evaluate that at x = 1, and you don't get exactly 0 ! You get a little off. It's correct to only two decimal places. Curious why they chose it that way."

Definition at line 32 of file MathApprox.h.

33{
34 static_assert(sizeof(float) == sizeof(int32_t));
35 union
36 {
37 float val;
38 int32_t x;
39 } u = { x };
40 auto log_2 = (float)(((u.x >> 23) & 255) - 128);
41 u.x &= ~(255 << 23);
42 u.x += 127 << 23;
43 log_2 += ((-0.3358287811f) * u.val + 2.0f) * u.val - 0.65871759316667f;
44 return log_2;
45}

Referenced by FormantShifter::Process(), and TEST_CASE().

Here is the caller graph for this function: