Skip to content

Commit

Permalink
Reorder some multiplications for optimized temporary reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Dec 31, 2024
1 parent 6118ae7 commit f042579
Showing 1 changed file with 47 additions and 49 deletions.
96 changes: 47 additions & 49 deletions core/filters/nfc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

#include <algorithm>

#include "opthelpers.h"


/* Near-field control filters are the basis for handling the near-field effect.
* The near-field effect is a bass-boost present in the directional components
Expand Down Expand Up @@ -58,12 +56,12 @@ constexpr std::array B{

NfcFilter1 NfcFilterCreate1(const float w0, const float w1) noexcept
{
NfcFilter1 nfc{};
auto nfc = NfcFilter1{};

/* Calculate bass-cut coefficients. */
float r{0.5f * w1};
float b_00{B[1][0] * r};
float g_0{1.0f + b_00};
auto r = 0.5f * w1;
auto b_00 = B[1][0] * r;
auto g_0 = 1.0f + b_00;

nfc.base_gain = 1.0f / g_0;
nfc.a1 = 2.0f * b_00 / g_0;
Expand All @@ -81,9 +79,9 @@ NfcFilter1 NfcFilterCreate1(const float w0, const float w1) noexcept

void NfcFilterAdjust1(NfcFilter1 *nfc, const float w0) noexcept
{
const float r{0.5f * w0};
const float b_00{B[1][0] * r};
const float g_0{1.0f + b_00};
const auto r = 0.5f * w0;
const auto b_00 = B[1][0] * r;
const auto g_0 = 1.0f + b_00;

nfc->gain = nfc->base_gain * g_0;
nfc->b1 = 2.0f * b_00 / g_0;
Expand All @@ -92,13 +90,13 @@ void NfcFilterAdjust1(NfcFilter1 *nfc, const float w0) noexcept

NfcFilter2 NfcFilterCreate2(const float w0, const float w1) noexcept
{
NfcFilter2 nfc{};
auto nfc = NfcFilter2{};

/* Calculate bass-cut coefficients. */
float r{0.5f * w1};
float b_10{B[2][0] * r};
float b_11{B[2][1] * r * r};
float g_1{1.0f + b_10 + b_11};
auto r = 0.5f * w1;
auto b_10 = B[2][0] * r;
auto b_11 = B[2][1] * (r*r);
auto g_1 = 1.0f + b_10 + b_11;

nfc.base_gain = 1.0f / g_1;
nfc.a1 = (2.0f*b_10 + 4.0f*b_11) / g_1;
Expand All @@ -119,10 +117,10 @@ NfcFilter2 NfcFilterCreate2(const float w0, const float w1) noexcept

void NfcFilterAdjust2(NfcFilter2 *nfc, const float w0) noexcept
{
const float r{0.5f * w0};
const float b_10{B[2][0] * r};
const float b_11{B[2][1] * r * r};
const float g_1{1.0f + b_10 + b_11};
const auto r = 0.5f * w0;
const auto b_10 = B[2][0] * r;
const auto b_11 = B[2][1] * (r*r);
const auto g_1 = 1.0f + b_10 + b_11;

nfc->gain = nfc->base_gain * g_1;
nfc->b1 = (2.0f*b_10 + 4.0f*b_11) / g_1;
Expand All @@ -132,15 +130,15 @@ void NfcFilterAdjust2(NfcFilter2 *nfc, const float w0) noexcept

NfcFilter3 NfcFilterCreate3(const float w0, const float w1) noexcept
{
NfcFilter3 nfc{};
auto nfc = NfcFilter3{};

/* Calculate bass-cut coefficients. */
float r{0.5f * w1};
float b_10{B[3][0] * r};
float b_11{B[3][1] * r * r};
float b_00{B[3][2] * r};
float g_1{1.0f + b_10 + b_11};
float g_0{1.0f + b_00};
auto r = 0.5f * w1;
auto b_10 = B[3][0] * r;
auto b_11 = B[3][1] * (r*r);
auto b_00 = B[3][2] * r;
auto g_1 = 1.0f + b_10 + b_11;
auto g_0 = 1.0f + b_00;

nfc.base_gain = 1.0f / (g_1 * g_0);
nfc.a1 = (2.0f*b_10 + 4.0f*b_11) / g_1;
Expand All @@ -150,7 +148,7 @@ NfcFilter3 NfcFilterCreate3(const float w0, const float w1) noexcept
/* Calculate bass-boost coefficients. */
r = 0.5f * w0;
b_10 = B[3][0] * r;
b_11 = B[3][1] * r * r;
b_11 = B[3][1] * (r*r);
b_00 = B[3][2] * r;
g_1 = 1.0f + b_10 + b_11;
g_0 = 1.0f + b_00;
Expand All @@ -165,12 +163,12 @@ NfcFilter3 NfcFilterCreate3(const float w0, const float w1) noexcept

void NfcFilterAdjust3(NfcFilter3 *nfc, const float w0) noexcept
{
const float r{0.5f * w0};
const float b_10{B[3][0] * r};
const float b_11{B[3][1] * r * r};
const float b_00{B[3][2] * r};
const float g_1{1.0f + b_10 + b_11};
const float g_0{1.0f + b_00};
const auto r = 0.5f * w0;
const auto b_10 = B[3][0] * r;
const auto b_11 = B[3][1] * (r*r);
const auto b_00 = B[3][2] * r;
const auto g_1 = 1.0f + b_10 + b_11;
const auto g_0 = 1.0f + b_00;

nfc->gain = nfc->base_gain * (g_1 * g_0);
nfc->b1 = (2.0f*b_10 + 4.0f*b_11) / g_1;
Expand All @@ -181,16 +179,16 @@ void NfcFilterAdjust3(NfcFilter3 *nfc, const float w0) noexcept

NfcFilter4 NfcFilterCreate4(const float w0, const float w1) noexcept
{
NfcFilter4 nfc{};
auto nfc = NfcFilter4{};

/* Calculate bass-cut coefficients. */
float r{0.5f * w1};
float b_10{B[4][0] * r};
float b_11{B[4][1] * r * r};
float b_00{B[4][2] * r};
float b_01{B[4][3] * r * r};
float g_1{1.0f + b_10 + b_11};
float g_0{1.0f + b_00 + b_01};
auto r = 0.5f * w1;
auto b_10 = B[4][0] * r;
auto b_11 = B[4][1] * (r*r);
auto b_00 = B[4][2] * r;
auto b_01 = B[4][3] * (r*r);
auto g_1 = 1.0f + b_10 + b_11;
auto g_0 = 1.0f + b_00 + b_01;

nfc.base_gain = 1.0f / (g_1 * g_0);
nfc.a1 = (2.0f*b_10 + 4.0f*b_11) / g_1;
Expand All @@ -201,9 +199,9 @@ NfcFilter4 NfcFilterCreate4(const float w0, const float w1) noexcept
/* Calculate bass-boost coefficients. */
r = 0.5f * w0;
b_10 = B[4][0] * r;
b_11 = B[4][1] * r * r;
b_11 = B[4][1] * (r*r);
b_00 = B[4][2] * r;
b_01 = B[4][3] * r * r;
b_01 = B[4][3] * (r*r);
g_1 = 1.0f + b_10 + b_11;
g_0 = 1.0f + b_00 + b_01;

Expand All @@ -218,13 +216,13 @@ NfcFilter4 NfcFilterCreate4(const float w0, const float w1) noexcept

void NfcFilterAdjust4(NfcFilter4 *nfc, const float w0) noexcept
{
const float r{0.5f * w0};
const float b_10{B[4][0] * r};
const float b_11{B[4][1] * r * r};
const float b_00{B[4][2] * r};
const float b_01{B[4][3] * r * r};
const float g_1{1.0f + b_10 + b_11};
const float g_0{1.0f + b_00 + b_01};
const auto r = 0.5f * w0;
const auto b_10 = B[4][0] * r;
const auto b_11 = B[4][1] * (r*r);
const auto b_00 = B[4][2] * r;
const auto b_01 = B[4][3] * (r*r);
const auto g_1 = 1.0f + b_10 + b_11;
const auto g_0 = 1.0f + b_00 + b_01;

nfc->gain = nfc->base_gain * (g_1 * g_0);
nfc->b1 = (2.0f*b_10 + 4.0f*b_11) / g_1;
Expand Down

0 comments on commit f042579

Please sign in to comment.