Skip to content

Commit

Permalink
Add Math::round_to_next_multiple() (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 authored Apr 18, 2024
1 parent 11e1d7b commit 8847c1d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/dsp/chowdsp_eq/EQ/chowdsp_EQProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void EQProcessor<FloatType, numBands, EQBandType>::prepare (const juce::dsp::Pro
bypasses[i].prepare (spec, onOffs[i], false);
}

const auto paddedChannelSize = Math::ceiling_divide (static_cast<size_t> (spec.maximumBlockSize), SIMDUtils::defaultSIMDAlignment) * SIMDUtils::defaultSIMDAlignment;
const auto paddedChannelSize = Math::round_to_next_multiple (static_cast<size_t> (spec.maximumBlockSize), SIMDUtils::defaultSIMDAlignment);
const auto requiredMemoryBytes = paddedChannelSize * sizeof (FloatType) * 3 // per-band smoothed values
+ paddedChannelSize * spec.numChannels * sizeof (FloatType) * 2 // per-band fade and bypass buffers
+ 32; // extra padding
Expand Down
9 changes: 9 additions & 0 deletions modules/dsp/chowdsp_math/Math/chowdsp_OtherMathOps.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ namespace Math
return (num + den - 1) / den;
}

/**
* Rounds a number up to the nearest multiple of the given multiplier.
*/
template <typename T>
constexpr T round_to_next_multiple (T value, T multiplier)
{
return ceiling_divide (value, multiplier) * multiplier;
}

/**
* Returns 1 if the input is positive, -1 if the input is negative,
* and 0 if the input is zero.
Expand Down
10 changes: 10 additions & 0 deletions tests/dsp_tests/chowdsp_math_test/OtherMathOpsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ TEST_CASE ("Other Math Ops Test", "[dsp][math][simd]")
REQUIRE_MESSAGE (chowdsp::Math::ceiling_divide (9, 4) == 3, "Ceiling divide 9 / 4 should equal 3");
}

SECTION ("Next Multiple Test")
{
STATIC_REQUIRE (chowdsp::Math::round_to_next_multiple (3, 4) == 4);
STATIC_REQUIRE (chowdsp::Math::round_to_next_multiple (4, 4) == 4);
STATIC_REQUIRE (chowdsp::Math::round_to_next_multiple (5, 4) == 8);
STATIC_REQUIRE (chowdsp::Math::round_to_next_multiple (7, 4) == 8);
STATIC_REQUIRE (chowdsp::Math::round_to_next_multiple (8, 4) == 8);
STATIC_REQUIRE (chowdsp::Math::round_to_next_multiple (9, 4) == 12);
}

SECTION ("Signum Test")
{
REQUIRE_MESSAGE (chowdsp::Math::sign (-1) == -1, "Signum of negative number is incorrect!");
Expand Down

0 comments on commit 8847c1d

Please sign in to comment.