Audacity 3.2.0
MathApprox.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 MathApprox.h
7
8 Matthieu Hodgkinson
9
10**********************************************************************/
11#pragma once
12
13#include <cstdint>
14
32constexpr float FastLog2(float x)
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}
constexpr float FastLog2(float x)
Approximates the base-2 logarithm of a float to two decimal places, adapted from https://stackoverflo...
Definition: MathApprox.h:32