sfizz/tests/SIMDHelpersT.cpp

775 lines
30 KiB
C++
Raw Normal View History

2019-08-30 00:49:58 +02:00
// Copyright (c) 2019, Paul Ferrand
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2019-08-30 15:28:30 +02:00
#include "SIMDHelpers.h"
2019-08-25 14:01:03 +02:00
#include "catch2/catch.hpp"
#include <absl/algorithm/container.h>
#include <absl/types/span.h>
2019-08-21 01:00:07 +02:00
#include <algorithm>
2019-08-25 14:01:03 +02:00
#include <array>
2019-08-22 10:43:33 +02:00
#include <iostream>
2019-08-21 01:00:07 +02:00
using namespace Catch::literals;
constexpr int smallBufferSize { 3 };
constexpr int bigBufferSize { 4095 };
constexpr int medBufferSize { 127 };
constexpr double fillValue { 1.3 };
2019-08-28 19:36:16 +02:00
template <class Type>
inline bool approxEqualMargin(absl::Span<const Type> lhs, absl::Span<const Type> rhs, Type eps = 1e-3)
{
if (lhs.size() != rhs.size())
return false;
for (size_t i = 0; i < rhs.size(); ++i)
if (rhs[i] != Approx(lhs[i]).margin(eps)) {
std::cerr << lhs[i] << " != " << rhs[i] << " at index " << i << '\n';
return false;
}
return true;
}
2019-08-25 14:01:03 +02:00
template <class Type>
inline bool approxEqual(absl::Span<const Type> lhs, absl::Span<const Type> rhs, Type eps = 1e-3)
2019-08-22 01:54:43 +02:00
{
if (lhs.size() != rhs.size())
return false;
for (size_t i = 0; i < rhs.size(); ++i)
2019-08-25 14:01:03 +02:00
if (rhs[i] != Approx(lhs[i]).epsilon(eps)) {
2019-08-22 01:54:43 +02:00
std::cerr << lhs[i] << " != " << rhs[i] << " at index " << i << '\n';
return false;
}
return true;
}
2019-08-21 01:00:07 +02:00
TEST_CASE("[Helpers] fill() - Manual buffer")
{
2019-08-25 14:01:03 +02:00
std::vector<float> buffer(5);
2019-08-21 01:00:07 +02:00
std::vector<float> expected { fillValue, fillValue, fillValue, fillValue, fillValue };
fill<float, false>(absl::MakeSpan(buffer), fillValue);
2019-08-21 01:00:07 +02:00
REQUIRE(buffer == expected);
}
TEST_CASE("[Helpers] fill() - Small buffer")
{
2019-08-25 14:01:03 +02:00
std::vector<float> buffer(smallBufferSize);
std::vector<float> expected(smallBufferSize);
2019-08-21 01:00:07 +02:00
std::fill(expected.begin(), expected.end(), fillValue);
fill<float, false>(absl::MakeSpan(buffer), fillValue);
2019-08-21 01:00:07 +02:00
REQUIRE(buffer == expected);
}
TEST_CASE("[Helpers] fill() - Big buffer")
{
2019-08-25 14:01:03 +02:00
std::vector<float> buffer(bigBufferSize);
std::vector<float> expected(bigBufferSize);
2019-08-21 01:00:07 +02:00
std::fill(expected.begin(), expected.end(), fillValue);
fill<float, false>(absl::MakeSpan(buffer), fillValue);
2019-08-21 01:00:07 +02:00
REQUIRE(buffer == expected);
}
TEST_CASE("[Helpers] fill() - Small buffer -- SIMD")
{
2019-08-25 14:01:03 +02:00
std::vector<float> buffer(smallBufferSize);
std::vector<float> expected(smallBufferSize);
2019-08-21 01:00:07 +02:00
std::fill(expected.begin(), expected.end(), fillValue);
fill<float, true>(absl::MakeSpan(buffer), fillValue);
2019-08-21 01:00:07 +02:00
REQUIRE(buffer == expected);
}
TEST_CASE("[Helpers] fill() - Big buffer -- SIMD")
{
2019-08-25 14:01:03 +02:00
std::vector<float> buffer(bigBufferSize);
std::vector<float> expected(bigBufferSize);
2019-08-21 01:00:07 +02:00
std::fill(expected.begin(), expected.end(), fillValue);
fill<float, true>(absl::MakeSpan(buffer), fillValue);
2019-08-21 01:00:07 +02:00
REQUIRE(buffer == expected);
}
TEST_CASE("[Helpers] fill() - Small buffer -- doubles")
{
2019-08-25 14:01:03 +02:00
std::vector<double> buffer(smallBufferSize);
std::vector<double> expected(smallBufferSize);
2019-08-21 01:00:07 +02:00
std::fill(expected.begin(), expected.end(), fillValue);
fill<double, false>(absl::MakeSpan(buffer), fillValue);
2019-08-21 01:00:07 +02:00
REQUIRE(buffer == expected);
}
TEST_CASE("[Helpers] fill() - Big buffer -- doubles")
{
2019-08-25 14:01:03 +02:00
std::vector<double> buffer(bigBufferSize);
std::vector<double> expected(bigBufferSize);
2019-08-21 01:00:07 +02:00
std::fill(expected.begin(), expected.end(), fillValue);
fill<double, false>(absl::MakeSpan(buffer), fillValue);
2019-08-21 01:00:07 +02:00
REQUIRE(buffer == expected);
}
TEST_CASE("[Helpers] Interleaved read")
{
2019-08-25 14:01:03 +02:00
std::array<float, 16> input { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f, 3.0f, 13.0f, 4.0f, 14.0f, 5.0f, 15.0f, 6.0f, 16.0f, 7.0f, 17.0f };
std::array<float, 16> expected { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f };
std::array<float, 8> leftOutput;
std::array<float, 8> rightOutput;
readInterleaved<float, false>(input, absl::MakeSpan(leftOutput), absl::MakeSpan(rightOutput));
std::array<float, 16> real;
2019-08-21 01:00:07 +02:00
auto realIdx = 0;
2019-08-25 14:01:03 +02:00
for (auto value : leftOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
for (auto value : rightOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
REQUIRE(real == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Interleaved read unaligned end")
{
2019-08-25 14:01:03 +02:00
std::array<float, 20> input { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f, 3.0f, 13.0f, 4.0f, 14.0f, 5.0f, 15.0f, 6.0f, 16.0f, 7.0f, 17.0f, 8.0f, 18.0f, 9.0f, 19.0f };
std::array<float, 20> expected { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f };
std::array<float, 10> leftOutput;
std::array<float, 10> rightOutput;
readInterleaved<float, false>(input, absl::MakeSpan(leftOutput), absl::MakeSpan(rightOutput));
std::array<float, 20> real;
2019-08-21 01:00:07 +02:00
auto realIdx = 0;
2019-08-25 14:01:03 +02:00
for (auto value : leftOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
for (auto value : rightOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
REQUIRE(real == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Small interleaved read unaligned end")
{
2019-08-25 14:01:03 +02:00
std::array<float, 6> input { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f };
std::array<float, 6> expected { 0.0f, 1.0f, 2.0f, 10.0f, 11.0f, 12.0f };
std::array<float, 3> leftOutput;
std::array<float, 3> rightOutput;
readInterleaved<float, false>(input, absl::MakeSpan(leftOutput), absl::MakeSpan(rightOutput));
std::array<float, 6> real;
2019-08-21 01:00:07 +02:00
auto realIdx = 0;
2019-08-25 14:01:03 +02:00
for (auto value : leftOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
for (auto value : rightOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
REQUIRE(real == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Interleaved read -- SIMD")
{
2019-08-25 14:01:03 +02:00
std::array<float, 16> input = { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f, 3.0f, 13.0f, 4.0f, 14.0f, 5.0f, 15.0f, 6.0f, 16.0f, 7.0f, 17.0f };
2019-08-21 01:00:07 +02:00
std::array<float, 16> expected = { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f };
std::array<float, 8> leftOutput;
std::array<float, 8> rightOutput;
readInterleaved<float, true>(input, absl::MakeSpan(leftOutput), absl::MakeSpan(rightOutput));
std::array<float, 16> real;
2019-08-21 01:00:07 +02:00
auto realIdx = 0;
2019-08-25 14:01:03 +02:00
for (auto value : leftOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
for (auto value : rightOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
REQUIRE(real == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Interleaved read unaligned end -- SIMD")
{
2019-08-25 14:01:03 +02:00
std::array<float, 20> input = { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f, 3.0f, 13.0f, 4.0f, 14.0f, 5.0f, 15.0f, 6.0f, 16.0f, 7.0f, 17.0f, 8.0f, 18.0f, 9.0f, 19.0f };
std::array<float, 20> expected = { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f };
std::array<float, 10> leftOutput;
std::array<float, 10> rightOutput;
readInterleaved<float, true>(input, absl::MakeSpan(leftOutput), absl::MakeSpan(rightOutput));
std::array<float, 20> real;
2019-08-21 01:00:07 +02:00
auto realIdx = 0;
2019-08-25 14:01:03 +02:00
for (auto value : leftOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
for (auto value : rightOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
REQUIRE(real == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Small interleaved read unaligned end -- SIMD")
{
2019-08-25 14:01:03 +02:00
std::array<float, 6> input { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f };
std::array<float, 6> expected { 0.0f, 1.0f, 2.0f, 10.0f, 11.0f, 12.0f };
std::array<float, 3> leftOutput;
std::array<float, 3> rightOutput;
readInterleaved<float, true>(input, absl::MakeSpan(leftOutput), absl::MakeSpan(rightOutput));
std::array<float, 6> real;
2019-08-21 01:00:07 +02:00
auto realIdx = 0;
2019-08-25 14:01:03 +02:00
for (auto value : leftOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
for (auto value : rightOutput)
2019-08-21 01:00:07 +02:00
real[realIdx++] = value;
2019-08-25 14:01:03 +02:00
REQUIRE(real == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Interleaved read SIMD vs Scalar")
{
std::array<float, medBufferSize * 2> input;
std::array<float, medBufferSize> leftOutputScalar;
std::array<float, medBufferSize> rightOutputScalar;
std::array<float, medBufferSize> leftOutputSIMD;
std::array<float, medBufferSize> rightOutputSIMD;
std::iota(input.begin(), input.end(), 0.0f);
readInterleaved<float, false>(input, absl::MakeSpan(leftOutputScalar), absl::MakeSpan(rightOutputScalar));
readInterleaved<float, true>(input, absl::MakeSpan(leftOutputSIMD), absl::MakeSpan(rightOutputSIMD));
2019-08-25 14:01:03 +02:00
REQUIRE(leftOutputScalar == leftOutputSIMD);
REQUIRE(rightOutputScalar == rightOutputSIMD);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Interleaved write")
{
2019-08-25 14:01:03 +02:00
std::array<float, 8> leftInput {
0.0f,
1.0f,
2.0f,
3.0f,
4.0f,
5.0f,
6.0f,
7.0f,
};
2019-08-21 01:00:07 +02:00
std::array<float, 8> rightInput { 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f };
std::array<float, 16> output;
2019-08-25 14:01:03 +02:00
std::array<float, 16> expected { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f, 3.0f, 13.0f, 4.0f, 14.0f, 5.0f, 15.0f, 6.0f, 16.0f, 7.0f, 17.0f };
writeInterleaved<float, false>(leftInput, rightInput, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Interleaved write unaligned end")
{
2019-08-25 14:01:03 +02:00
std::array<float, 10> leftInput { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f };
2019-08-21 01:00:07 +02:00
std::array<float, 10> rightInput { 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f };
std::array<float, 20> output;
2019-08-25 14:01:03 +02:00
std::array<float, 20> expected { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f, 3.0f, 13.0f, 4.0f, 14.0f, 5.0f, 15.0f, 6.0f, 16.0f, 7.0f, 17.0f, 8.0f, 18.0f, 9.0f, 19.0f };
writeInterleaved<float, false>(leftInput, rightInput, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Small interleaved write unaligned end")
{
2019-08-25 14:01:03 +02:00
std::array<float, 3> leftInput { 0.0f, 1.0f, 2.0f };
2019-08-21 01:00:07 +02:00
std::array<float, 3> rightInput { 10.0f, 11.0f, 12.0f };
std::array<float, 6> output;
2019-08-25 14:01:03 +02:00
std::array<float, 6> expected { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f };
writeInterleaved<float, false>(leftInput, rightInput, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Interleaved write -- SIMD")
{
2019-08-25 14:01:03 +02:00
std::array<float, 8> leftInput {
0.0f,
1.0f,
2.0f,
3.0f,
4.0f,
5.0f,
6.0f,
7.0f,
};
2019-08-21 01:00:07 +02:00
std::array<float, 8> rightInput { 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f };
std::array<float, 16> output;
2019-08-25 14:01:03 +02:00
std::array<float, 16> expected { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f, 3.0f, 13.0f, 4.0f, 14.0f, 5.0f, 15.0f, 6.0f, 16.0f, 7.0f, 17.0f };
writeInterleaved<float, true>(leftInput, rightInput, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Interleaved write unaligned end -- SIMD")
{
2019-08-25 14:01:03 +02:00
std::array<float, 10> leftInput { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f };
2019-08-21 01:00:07 +02:00
std::array<float, 10> rightInput { 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f, 17.0f, 18.0f, 19.0f };
std::array<float, 20> output;
2019-08-25 14:01:03 +02:00
std::array<float, 20> expected = { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f, 3.0f, 13.0f, 4.0f, 14.0f, 5.0f, 15.0f, 6.0f, 16.0f, 7.0f, 17.0f, 8.0f, 18.0f, 9.0f, 19.0f };
writeInterleaved<float, true>(leftInput, rightInput, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Small interleaved write unaligned end -- SIMD")
{
2019-08-25 14:01:03 +02:00
std::array<float, 3> leftInput { 0.0f, 1.0f, 2.0f };
2019-08-21 01:00:07 +02:00
std::array<float, 3> rightInput { 10.0f, 11.0f, 12.0f };
std::array<float, 6> output;
2019-08-25 14:01:03 +02:00
std::array<float, 6> expected { 0.0f, 10.0f, 1.0f, 11.0f, 2.0f, 12.0f };
writeInterleaved<float, true>(leftInput, rightInput, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-21 01:00:07 +02:00
}
TEST_CASE("[Helpers] Interleaved write SIMD vs Scalar")
{
std::array<float, medBufferSize> leftInput;
std::array<float, medBufferSize> rightInput;
std::array<float, medBufferSize * 2> outputScalar;
std::array<float, medBufferSize * 2> outputSIMD;
std::iota(leftInput.begin(), leftInput.end(), 0.0f);
std::iota(rightInput.begin(), rightInput.end(), medBufferSize);
writeInterleaved<float, false>(leftInput, rightInput, absl::MakeSpan(outputScalar));
writeInterleaved<float, true>(leftInput, rightInput, absl::MakeSpan(outputSIMD));
2019-08-25 14:01:03 +02:00
REQUIRE(outputScalar == outputSIMD);
2019-08-21 23:00:06 +02:00
}
TEST_CASE("[Helpers] Gain, single")
{
std::array<float, 5> input { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> output { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
std::array<float, 5> expected { fillValue, fillValue, fillValue, fillValue, fillValue };
applyGain<float, false>(fillValue, input, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-21 23:00:06 +02:00
}
TEST_CASE("[Helpers] Gain, single and inplace")
{
std::array<float, 5> buffer { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> expected { fillValue, fillValue, fillValue, fillValue, fillValue };
applyGain<float, false>(fillValue, buffer, absl::MakeSpan(buffer));
2019-08-25 14:01:03 +02:00
REQUIRE(buffer == expected);
2019-08-21 23:00:06 +02:00
}
TEST_CASE("[Helpers] Gain, spans")
{
std::array<float, 5> input { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> gain { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> output { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
std::array<float, 5> expected { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
applyGain<float, false>(gain, input, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-21 23:00:06 +02:00
}
TEST_CASE("[Helpers] Gain, spans and inplace")
{
std::array<float, 5> buffer { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> gain { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> expected { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
applyGain<float, false>(gain, buffer, absl::MakeSpan(buffer));
2019-08-25 14:01:03 +02:00
REQUIRE(buffer == expected);
2019-08-21 23:00:06 +02:00
}
TEST_CASE("[Helpers] Gain, single (SIMD)")
{
std::array<float, 5> input { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> output { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
std::array<float, 5> expected { fillValue, fillValue, fillValue, fillValue, fillValue };
applyGain<float, true>(fillValue, input, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-21 23:00:06 +02:00
}
TEST_CASE("[Helpers] Gain, single and inplace (SIMD)")
{
std::array<float, 5> buffer { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> expected { fillValue, fillValue, fillValue, fillValue, fillValue };
applyGain<float, true>(fillValue, buffer, absl::MakeSpan(buffer));
2019-08-25 14:01:03 +02:00
REQUIRE(buffer == expected);
2019-08-21 23:00:06 +02:00
}
TEST_CASE("[Helpers] Gain, spans (SIMD)")
{
std::array<float, 5> input { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> gain { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> output { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
std::array<float, 5> expected { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
applyGain<float, true>(gain, input, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-21 23:00:06 +02:00
}
TEST_CASE("[Helpers] Gain, spans and inplace (SIMD)")
{
std::array<float, 5> buffer { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> gain { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> expected { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
applyGain<float, true>(gain, buffer, absl::MakeSpan(buffer));
2019-08-25 14:01:03 +02:00
REQUIRE(buffer == expected);
2019-08-22 01:54:43 +02:00
}
TEST_CASE("[Helpers] SFZ looping index")
{
2019-08-25 14:01:03 +02:00
std::array<float, 6> jumps { 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f }; // 1.1 2.3 3.6 5.0 6.5 8.1
2019-08-22 01:54:43 +02:00
std::array<int, 6> indices;
std::array<float, 6> leftCoeffs;
std::array<float, 6> rightCoeffs;
std::array<int, 6> expectedIndices { 2, 3, 4, 1, 2, 4 };
std::array<float, 6> expectedLeft { 0.9f, 0.7f, 0.4f, 1.0f, 0.5f, 0.9f };
std::array<float, 6> expectedRight { 0.1f, 0.3f, 0.6f, 0.0f, 0.5f, 0.1f };
loopingSFZIndex<float, false>(jumps, absl::MakeSpan(leftCoeffs), absl::MakeSpan(rightCoeffs), absl::MakeSpan(indices), 1.0f, 6, 1);
2019-08-25 14:01:03 +02:00
REQUIRE(indices == expectedIndices);
REQUIRE(approxEqual<float>(leftCoeffs, expectedLeft));
REQUIRE(approxEqual<float>(rightCoeffs, expectedRight));
2019-08-22 01:54:43 +02:00
}
TEST_CASE("[Helpers] SFZ looping index (SIMD)")
{
2019-08-25 14:01:03 +02:00
std::array<float, 6> jumps { 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f }; // 1.1 2.3 3.6 5.0 6.5 8.1
2019-08-22 01:54:43 +02:00
std::array<int, 6> indices;
std::array<float, 6> leftCoeffs;
std::array<float, 6> rightCoeffs;
std::array<int, 6> expectedIndices { 2, 3, 4, 1, 2, 4 };
std::array<float, 6> expectedLeft { 0.9f, 0.7f, 0.4f, 1.0f, 0.5f, 0.9f };
std::array<float, 6> expectedRight { 0.1f, 0.3f, 0.6f, 0.0f, 0.5f, 0.1f };
loopingSFZIndex<float, true>(jumps, absl::MakeSpan(leftCoeffs), absl::MakeSpan(rightCoeffs), absl::MakeSpan(indices), 1.0f, 6, 1);
2019-08-25 14:01:03 +02:00
REQUIRE(indices == expectedIndices);
REQUIRE(approxEqual<float>(leftCoeffs, expectedLeft));
REQUIRE(approxEqual<float>(rightCoeffs, expectedRight));
2019-08-22 01:54:43 +02:00
}
// TEST_CASE("[Helpers] SFZ looping index (SIMD vs Scalar)")
// {
// std::vector<float> jumps(bigBufferSize);
// absl::c_fill(jumps, fillValue);
// std::vector<int> indices(bigBufferSize);
// std::vector<float> leftCoeffs(bigBufferSize);
// std::vector<float> rightCoeffs(bigBufferSize);
2019-08-25 14:01:03 +02:00
2019-08-22 01:54:43 +02:00
// std::vector<int> indicesSIMD(bigBufferSize);
// std::vector<float> leftCoeffsSIMD(bigBufferSize);
// std::vector<float> rightCoeffsSIMD(bigBufferSize);
// loopingSFZIndex<float, false>(jumps, absl::MakeSpan(leftCoeffs), absl::MakeSpan(rightCoeffs), absl::MakeSpan(indices), 1.0f, medBufferSize, 1);
// loopingSFZIndex<float, true>(jumps, absl::MakeSpan(leftCoeffsSIMD), absl::MakeSpan(rightCoeffsSIMD), absl::MakeSpan(indicesSIMD), 1.0f, medBufferSize, 1);
2019-08-28 19:36:16 +02:00
// for (int i = 0; i < bigBufferSize; ++i)
// REQUIRE( ((static_cast<float>(indices[i]) + rightCoeffs[i] == Approx(static_cast<float>(indicesSIMD[i]) + rightCoeffsSIMD[i]).margin(1e-2))
// || (static_cast<float>(indices[i]) + rightCoeffs[i] == Approx(static_cast<float>(indicesSIMD[i]) + rightCoeffsSIMD[i] - static_cast<float>(medBufferSize)).margin(2e-2))) );
2019-08-22 12:05:28 +02:00
// }
2019-08-28 19:36:16 +02:00
TEST_CASE("[Helpers] SFZ saturating index")
{
std::array<float, 6> jumps { 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f }; // 1.1 2.3 3.6 5.0 6.5 8.1
std::array<int, 6> indices;
std::array<float, 6> leftCoeffs;
std::array<float, 6> rightCoeffs;
std::array<int, 6> expectedIndices { 2, 3, 4, 5, 5, 5 };
std::array<float, 6> expectedLeft { 0.9f, 0.7f, 0.4f, 0.0f, 0.0f, 0.0f };
std::array<float, 6> expectedRight { 0.1f, 0.3f, 0.6f, 1.0f, 1.0f, 1.0f };
saturatingSFZIndex<float, false>(jumps, absl::MakeSpan(leftCoeffs), absl::MakeSpan(rightCoeffs), absl::MakeSpan(indices), 1.0f, 6);
REQUIRE(indices == expectedIndices);
REQUIRE(approxEqual<float>(leftCoeffs, expectedLeft));
REQUIRE(approxEqual<float>(rightCoeffs, expectedRight));
}
TEST_CASE("[Helpers] SFZ saturating index (SIMD)")
{
std::array<float, 6> jumps { 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f }; // 1.1 2.3 3.6 5.0 6.5 8.1
std::array<int, 6> indices;
std::array<float, 6> leftCoeffs;
std::array<float, 6> rightCoeffs;
std::array<int, 6> expectedIndices { 2, 3, 4, 5, 5, 5 };
std::array<float, 6> expectedLeft { 0.9f, 0.7f, 0.4f, 0.0f, 0.0f, 0.0f };
std::array<float, 6> expectedRight { 0.1f, 0.3f, 0.6f, 1.0f, 1.0f, 1.0f };
saturatingSFZIndex<float, true>(jumps, absl::MakeSpan(leftCoeffs), absl::MakeSpan(rightCoeffs), absl::MakeSpan(indices), 1.0f, 6);
REQUIRE(indices == expectedIndices);
REQUIRE(approxEqualMargin<float>(leftCoeffs, expectedLeft));
REQUIRE(approxEqualMargin<float>(rightCoeffs, expectedRight));
}
TEST_CASE("[Helpers] SFZ saturating index (SIMD vs Scalar)")
{
std::vector<float> jumps(medBufferSize);
absl::c_fill(jumps, fillValue);
std::vector<int> indices(medBufferSize);
std::vector<float> leftCoeffs(medBufferSize);
std::vector<float> rightCoeffs(medBufferSize);
std::vector<int> indicesSIMD(medBufferSize);
std::vector<float> leftCoeffsSIMD(medBufferSize);
std::vector<float> rightCoeffsSIMD(medBufferSize);
saturatingSFZIndex<float, false>(jumps, absl::MakeSpan(leftCoeffs), absl::MakeSpan(rightCoeffs), absl::MakeSpan(indices), 1.0f, 78);
saturatingSFZIndex<float, true>(jumps, absl::MakeSpan(leftCoeffsSIMD), absl::MakeSpan(rightCoeffsSIMD), absl::MakeSpan(indicesSIMD), 1.0f, 78);
for (int i = 0; i < medBufferSize; ++i)
REQUIRE( static_cast<float>(indices[i]) + rightCoeffs[i] == Approx(static_cast<float>(indicesSIMD[i]) + rightCoeffsSIMD[i]));
}
2019-08-22 12:05:28 +02:00
TEST_CASE("[Helpers] Linear Ramp")
{
const float start { 0.0f };
const float v { fillValue };
std::array<float, 6> output;
2019-08-25 14:01:03 +02:00
std::array<float, 6> expected { v, v + v, v + v + v, v + v + v + v, v + v + v + v + v, v + v + v + v + v + v };
2019-08-22 12:05:28 +02:00
linearRamp<float, false>(absl::MakeSpan(output), start, v);
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-22 12:05:28 +02:00
}
TEST_CASE("[Helpers] Linear Ramp (SIMD)")
{
const float start { 0.0f };
const float v { fillValue };
std::array<float, 6> output;
2019-08-25 14:01:03 +02:00
std::array<float, 6> expected { v, v + v, v + v + v, v + v + v + v, v + v + v + v + v, v + v + v + v + v + v };
2019-08-22 12:05:28 +02:00
linearRamp<float, true>(absl::MakeSpan(output), start, v);
2019-09-21 01:43:04 +02:00
REQUIRE(approxEqual<float>(output, expected));
2019-08-22 12:05:28 +02:00
}
TEST_CASE("[Helpers] Linear Ramp (SIMD vs scalar)")
{
const float start { 0.0f };
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
linearRamp<float, false>(absl::MakeSpan(outputScalar), start, fillValue);
linearRamp<float, true>(absl::MakeSpan(outputSIMD), start, fillValue);
2019-08-25 14:01:03 +02:00
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
2019-08-22 12:05:28 +02:00
}
TEST_CASE("[Helpers] Linear Ramp unaligned (SIMD vs scalar)")
{
const float start { 0.0f };
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
linearRamp<float, false>(absl::MakeSpan(outputScalar).subspan(1), start, fillValue);
linearRamp<float, true>(absl::MakeSpan(outputSIMD).subspan(1), start, fillValue);
2019-08-25 14:01:03 +02:00
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
2019-08-22 12:05:28 +02:00
}
TEST_CASE("[Helpers] Multiplicative Ramp")
{
const float start { 1.0f };
const float v { fillValue };
std::array<float, 6> output;
2019-08-25 14:01:03 +02:00
std::array<float, 6> expected { v, v * v, v * v * v, v * v * v * v, v * v * v * v * v, v * v * v * v * v * v };
2019-08-22 12:05:28 +02:00
multiplicativeRamp<float, false>(absl::MakeSpan(output), start, v);
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-22 12:05:28 +02:00
}
TEST_CASE("[Helpers] Multiplicative Ramp (SIMD)")
{
const float start { 1.0f };
const float v { fillValue };
std::array<float, 6> output;
2019-08-25 14:01:03 +02:00
std::array<float, 6> expected { v, v * v, v * v * v, v * v * v * v, v * v * v * v * v, v * v * v * v * v * v };
2019-08-22 12:05:28 +02:00
multiplicativeRamp<float, true>(absl::MakeSpan(output), start, v);
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-22 12:05:28 +02:00
}
TEST_CASE("[Helpers] Multiplicative Ramp (SIMD vs scalar)")
{
const float start { 1.0f };
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
multiplicativeRamp<float, false>(absl::MakeSpan(outputScalar), start, fillValue);
multiplicativeRamp<float, true>(absl::MakeSpan(outputSIMD), start, fillValue);
2019-08-25 14:01:03 +02:00
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
2019-08-22 12:05:28 +02:00
}
TEST_CASE("[Helpers] Multiplicative Ramp unaligned (SIMD vs scalar)")
{
const float start { 1.0f };
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
multiplicativeRamp<float, false>(absl::MakeSpan(outputScalar).subspan(1), start, fillValue);
multiplicativeRamp<float, true>(absl::MakeSpan(outputSIMD).subspan(1), start, fillValue);
2019-08-25 14:01:03 +02:00
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
2019-08-23 01:27:39 +02:00
}
TEST_CASE("[Helpers] Add")
{
std::array<float, 5> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> output { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> expected { 2.0, 3.0, 4.0, 5.0, 6.0 };
add<float, false>(input, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-23 01:27:39 +02:00
}
TEST_CASE("[Helpers] Add (SIMD)")
{
std::array<float, 5> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> output { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> expected { 2.0, 3.0, 4.0, 5.0, 6.0 };
add<float, true>(input, absl::MakeSpan(output));
2019-08-25 14:01:03 +02:00
REQUIRE(output == expected);
2019-08-23 01:27:39 +02:00
}
TEST_CASE("[Helpers] Add (SIMD vs scalar)")
{
std::vector<float> input(bigBufferSize);
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
absl::c_iota(input, 0.0);
absl::c_fill(outputScalar, 0.0);
absl::c_fill(outputSIMD, 0.0);
add<float, false>(input, absl::MakeSpan(outputScalar));
add<float, true>(input, absl::MakeSpan(outputSIMD));
2019-08-25 14:01:03 +02:00
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
2019-09-07 15:55:35 +02:00
}
TEST_CASE("[Helpers] Subtract")
{
std::array<float, 5> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> output { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> expected { 0.0, -1.0, -2.0, -3.0, -4.0 };
subtract<float, false>(input, absl::MakeSpan(output));
REQUIRE(output == expected);
}
2019-09-15 23:04:01 +02:00
TEST_CASE("[Helpers] Subtract 2")
{
std::array<float, 5> output { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> expected { 0.0, 1.0, 2.0, 3.0, 4.0 };
subtract<float, false>(1.0f, absl::MakeSpan(output));
REQUIRE(output == expected);
}
2019-09-07 15:55:35 +02:00
TEST_CASE("[Helpers] Subtract (SIMD)")
{
std::array<float, 5> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> output { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> expected { 0.0, -1.0, -2.0, -3.0, -4.0 };
subtract<float, true>(input, absl::MakeSpan(output));
REQUIRE(output == expected);
}
TEST_CASE("[Helpers] Subtract (SIMD vs scalar)")
{
std::vector<float> input(bigBufferSize);
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
absl::c_iota(input, 0.0);
absl::c_fill(outputScalar, 0.0);
absl::c_fill(outputSIMD, 0.0);
subtract<float, false>(input, absl::MakeSpan(outputScalar));
subtract<float, true>(input, absl::MakeSpan(outputSIMD));
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
2019-08-23 01:27:39 +02:00
}
2019-09-06 22:15:19 +02:00
2019-09-15 23:04:01 +02:00
TEST_CASE("[Helpers] Subtract 2 (SIMD vs scalar)")
{
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
absl::c_iota(outputScalar, 0.0);
absl::c_iota(outputSIMD, 0.0);
subtract<float, false>(1.2f, absl::MakeSpan(outputScalar));
subtract<float, true>(1.2f, absl::MakeSpan(outputSIMD));
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
}
2019-09-06 22:15:19 +02:00
TEST_CASE("[Helpers] copy")
{
std::array<float, 5> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> output { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
copy<float, false>(input, absl::MakeSpan(output));
REQUIRE(output == input);
}
TEST_CASE("[Helpers] copy (SIMD)")
{
std::array<float, 5> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> output { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
copy<float, true>(input, absl::MakeSpan(output));
REQUIRE(output == input);
}
TEST_CASE("[Helpers] copy (SIMD vs scalar)")
{
std::vector<float> input(bigBufferSize);
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
absl::c_iota(input, 0.0);
absl::c_fill(outputScalar, 0.0);
absl::c_fill(outputSIMD, 0.0);
add<float, false>(input, absl::MakeSpan(outputScalar));
add<float, true>(input, absl::MakeSpan(outputSIMD));
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
}
TEST_CASE("[Helpers] Mean")
{
std::array<float, 10> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f };
REQUIRE(mean<float, false>(input) == 5.5f);
REQUIRE(mean<float, true>(input) == 5.5f);
}
TEST_CASE("[Helpers] Mean (SIMD vs scalar)")
{
std::vector<float> input(bigBufferSize);
absl::c_iota(input, 0.0);
REQUIRE(mean<float, false>(input) == Approx(mean<float, true>(input)).margin(0.001));
}
TEST_CASE("[Helpers] Mean Squared")
{
std::array<float, 10> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f };
REQUIRE(meanSquared<float, false>(input) == 38.5f);
REQUIRE(meanSquared<float, true>(input) == 38.5f);
}
TEST_CASE("[Helpers] Mean Squared (SIMD vs scalar)")
{
std::vector<float> input(medBufferSize);
absl::c_iota(input, 0.0);
REQUIRE(meanSquared<float, false>(input) == meanSquared<float, true>(input));
2019-09-14 09:17:02 +02:00
}
TEST_CASE("[Helpers] Cumulative sum ")
{
std::array<float, 6> input { 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f }; // 1.1 2.3 3.6 5.0 6.5 8.1
std::array<float, 6> output;
std::array<float, 6> expected { 1.1f, 2.3f, 3.6f, 5.0f, 6.5f, 8.1f };
cumsum<float, false>(input, absl::MakeSpan(output));
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[Helpers] Cumulative sum (SIMD vs Scalar)")
{
std::vector<float> input(bigBufferSize);
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
linearRamp<float>(absl::MakeSpan(input), 0.0, 0.1);
cumsum<float, false>(input, absl::MakeSpan(outputScalar));
cumsum<float, true>(input, absl::MakeSpan(outputSIMD));
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
}
TEST_CASE("[Helpers] Diff ")
{
std::array<float, 6> input { 1.1f, 2.3f, 3.6f, 5.0f, 6.5f, 8.1f };
std::array<float, 6> output;
std::array<float, 6> expected { 1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f };
diff<float, false>(input, absl::MakeSpan(output));
REQUIRE(approxEqual<float>(output, expected));
}
TEST_CASE("[Helpers] Diff (SIMD vs Scalar)")
{
std::vector<float> input(bigBufferSize);
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
linearRamp<float>(absl::MakeSpan(input), 0.0, 0.1);
diff<float, false>(input, absl::MakeSpan(outputScalar));
diff<float, true>(input, absl::MakeSpan(outputSIMD));
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
}