From a5212fd2d64174cb4a3ed7a73c6450f5c8286f3e Mon Sep 17 00:00:00 2001 From: paulfd Date: Fri, 6 Sep 2019 22:15:19 +0200 Subject: [PATCH] Added a copy helper --- benchmarks/CMakeLists.txt | 3 +++ external/Catch2 | 2 +- external/abseil-cpp | 2 +- sfizz/Config.h | 1 + sfizz/SIMDDummy.cpp | 6 ++++++ sfizz/SIMDHelpers.h | 23 ++++++++++++++++++++++- sfizz/SIMDSSE.cpp | 22 ++++++++++++++++++++++ tests/SIMDHelpersT.cpp | 30 ++++++++++++++++++++++++++++++ 8 files changed, 86 insertions(+), 3 deletions(-) diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt index 7b5a929c..4619ee3f 100644 --- a/benchmarks/CMakeLists.txt +++ b/benchmarks/CMakeLists.txt @@ -57,6 +57,9 @@ target_link_libraries(bm_ADSR benchmark absl::span absl::algorithm) add_executable(bm_add BM_add.cpp ${SFIZZ_SIMD_SOURCES}) target_link_libraries(bm_add benchmark absl::span absl::algorithm) +add_executable(bm_copy BM_copy.cpp ${SFIZZ_SIMD_SOURCES}) +target_link_libraries(bm_copy benchmark absl::span absl::algorithm) + add_custom_target(sfizz_benchmarks) add_dependencies(sfizz_benchmarks bm_opf_high_vs_low diff --git a/external/Catch2 b/external/Catch2 index ce42deb7..b77ab74b 160000 --- a/external/Catch2 +++ b/external/Catch2 @@ -1 +1 @@ -Subproject commit ce42deb72fab2be85a862f559984580c24cb76c4 +Subproject commit b77ab74b723cbd55ee12883562e22c7e467dbcb1 diff --git a/external/abseil-cpp b/external/abseil-cpp index 83c1d65c..325fd7b0 160000 --- a/external/abseil-cpp +++ b/external/abseil-cpp @@ -1 +1 @@ -Subproject commit 83c1d65c90a92aa49632b9ac5a793214bb0768bc +Subproject commit 325fd7b042ff4ec34f7dd32e602cd81ad0e24b22 diff --git a/sfizz/Config.h b/sfizz/Config.h index 6329f1cd..1bb304d7 100644 --- a/sfizz/Config.h +++ b/sfizz/Config.h @@ -54,4 +54,5 @@ namespace SIMDConfig { constexpr bool linearRamp { false }; constexpr bool multiplicativeRamp { true }; constexpr bool add { false }; + constexpr bool copy { false }; } \ No newline at end of file diff --git a/sfizz/SIMDDummy.cpp b/sfizz/SIMDDummy.cpp index 979b2ae7..1df98f6c 100644 --- a/sfizz/SIMDDummy.cpp +++ b/sfizz/SIMDDummy.cpp @@ -106,4 +106,10 @@ template <> void add(absl::Span input, absl::Span output) noexcept { add(input, output); +} + +template <> +void copy(absl::Span input, absl::Span output) noexcept +{ + copy(input, output); } \ No newline at end of file diff --git a/sfizz/SIMDHelpers.h b/sfizz/SIMDHelpers.h index 88205ce3..1e9cb4ab 100644 --- a/sfizz/SIMDHelpers.h +++ b/sfizz/SIMDHelpers.h @@ -325,4 +325,25 @@ void add(absl::Span input, absl::Span output) noexcept } template <> -void add(absl::Span input, absl::Span output) noexcept; \ No newline at end of file +void add(absl::Span input, absl::Span output) noexcept; + + +template +void snippetCopy(const T*& input, T*& output) +{ + *output++ = *input++; +} + +template +void copy(absl::Span input, absl::Span output) noexcept +{ + ASSERT(output.size() >= input.size()); + auto* in = input.begin(); + auto* out = output.begin(); + auto* sentinel = out + min(input.size(), output.size()); + while (out < sentinel) + snippetCopy(in, out); +} + +template <> +void copy(absl::Span input, absl::Span output) noexcept; \ No newline at end of file diff --git a/sfizz/SIMDSSE.cpp b/sfizz/SIMDSSE.cpp index 1eaf054f..75124d24 100644 --- a/sfizz/SIMDSSE.cpp +++ b/sfizz/SIMDSSE.cpp @@ -494,4 +494,26 @@ void add(absl::Span input, absl::Span output) n while (out < sentinel) snippetAdd(in, out); +} + +template <> +void copy(absl::Span input, absl::Span output) noexcept +{ + ASSERT(output.size() >= input.size()); + auto* in = input.begin(); + auto* out = output.begin(); + auto* sentinel = out + min(input.size(), output.size()); + const auto* lastAligned = prevAligned(sentinel); + + while (unaligned(in, out) && out < lastAligned) + snippetCopy(in, out); + + while (out < lastAligned) { + _mm_store_ps(out, _mm_load_ps(in)); + out += TypeAlignment; + in += TypeAlignment; + } + + while (out < sentinel) + snippetCopy(in, out); } \ No newline at end of file diff --git a/tests/SIMDHelpersT.cpp b/tests/SIMDHelpersT.cpp index dbf691e6..235ab117 100644 --- a/tests/SIMDHelpersT.cpp +++ b/tests/SIMDHelpersT.cpp @@ -622,3 +622,33 @@ TEST_CASE("[Helpers] Add (SIMD vs scalar)") add(input, absl::MakeSpan(outputSIMD)); REQUIRE(approxEqual(outputScalar, outputSIMD)); } + +TEST_CASE("[Helpers] copy") +{ + std::array input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }; + std::array output { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }; + copy(input, absl::MakeSpan(output)); + REQUIRE(output == input); +} + +TEST_CASE("[Helpers] copy (SIMD)") +{ + std::array input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }; + std::array output { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f }; + copy(input, absl::MakeSpan(output)); + REQUIRE(output == input); +} + +TEST_CASE("[Helpers] copy (SIMD vs scalar)") +{ + std::vector input(bigBufferSize); + std::vector outputScalar(bigBufferSize); + std::vector outputSIMD(bigBufferSize); + absl::c_iota(input, 0.0); + absl::c_fill(outputScalar, 0.0); + absl::c_fill(outputSIMD, 0.0); + + add(input, absl::MakeSpan(outputScalar)); + add(input, absl::MakeSpan(outputSIMD)); + REQUIRE(approxEqual(outputScalar, outputSIMD)); +}