Skip to content

Commit

Permalink
Tests for Max/Min
Browse files Browse the repository at this point in the history
  • Loading branch information
Epixu committed Jun 25, 2024
1 parent f1aaea1 commit 9082599
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
105 changes: 105 additions & 0 deletions test/TestMax.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
///
/// Langulus::SIMD
/// Copyright (c) 2019 Dimo Markov <[email protected]>
/// Part of the Langulus framework, see https://langulus.com
///
/// SPDX-License-Identifier: MIT
///
#include "Common.hpp"


template<class LHS, class RHS, class OUT> LANGULUS(INLINED)
void ControlMax(const LHS& lhs, const RHS& rhs, OUT& out) noexcept {
out = lhs > rhs ? lhs : rhs;
}

template<class LHS, class RHS, size_t C, class OUT> LANGULUS(INLINED)
void ControlMax(const Vector<LHS, C>& lhsArray, const Vector<RHS, C>& rhsArray, Vector<OUT, C>& out) noexcept {
auto r = out.mArray;
auto lhs = lhsArray.mArray;
auto rhs = rhsArray.mArray;
const auto lhsEnd = lhs + C;
while (lhs != lhsEnd)
ControlMax(*lhs++, *rhs++, *r++);
}

TEMPLATE_TEST_CASE("Max", "[max]"
, NUMBERS_ALL()
, VECTORS_ALL(1)
, VECTORS_ALL(2)
, VECTORS_ALL(3)
, VECTORS_ALL(4)
, VECTORS_ALL(5)
, VECTORS_ALL(8)
, VECTORS_ALL(9)
, VECTORS_ALL(16)
, VECTORS_ALL(17)
, VECTORS_ALL(32)
, VECTORS_ALL(33)
) {
using T = TestType;

GIVEN("max(x, y) = r") {
T x, y;
T r, rCheck;

if constexpr (not CT::Vector<T>) {
InitOne(x, 1);
InitOne(y, -5);
}

WHEN("Max is applied") {
ControlMax(x, y, rCheck);
SIMD::Max(x, y, r);

REQUIRE(r == rCheck);

#ifdef LANGULUS_STD_BENCHMARK
BENCHMARK_ADVANCED("Max (control)") (timer meter) {
some<T> nx(meter.runs());
if constexpr (not CT::Vector<T>) {
for (auto& i : nx)
InitOne(i, 1);
}

some<T> ny(meter.runs());
if constexpr (not CT::Vector<T>) {
for (auto& i : ny)
InitOne(i, 1);
}

some<T> nr(meter.runs());
meter.measure([&](int i) {
ControlMax(nx[i], ny[i], nr[i]);
});
};

BENCHMARK_ADVANCED("Max (SIMD)") (timer meter) {
some<T> nx(meter.runs());
if constexpr (not CT::Vector<T>) {
for (auto& i : nx)
InitOne(i, 1);
}

some<T> ny(meter.runs());
if constexpr (not CT::Vector<T>) {
for (auto& i : ny)
InitOne(i, 1);
}

some<T> nr(meter.runs());
meter.measure([&](int i) {
SIMD::Max(nx[i], ny[i], nr[i]);
});
};
#endif
}

WHEN("Max in reverse") {
ControlMax(y, x, rCheck);
SIMD::Max(y, x, r);

REQUIRE(r == rCheck);
}
}
}
105 changes: 105 additions & 0 deletions test/TestMin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
///
/// Langulus::SIMD
/// Copyright (c) 2019 Dimo Markov <[email protected]>
/// Part of the Langulus framework, see https://langulus.com
///
/// SPDX-License-Identifier: MIT
///
#include "Common.hpp"


template<class LHS, class RHS, class OUT> LANGULUS(INLINED)
void ControlMin(const LHS& lhs, const RHS& rhs, OUT& out) noexcept {
out = lhs < rhs ? lhs : rhs;
}

template<class LHS, class RHS, size_t C, class OUT> LANGULUS(INLINED)
void ControlMin(const Vector<LHS, C>& lhsArray, const Vector<RHS, C>& rhsArray, Vector<OUT, C>& out) noexcept {
auto r = out.mArray;
auto lhs = lhsArray.mArray;
auto rhs = rhsArray.mArray;
const auto lhsEnd = lhs + C;
while (lhs != lhsEnd)
ControlMin(*lhs++, *rhs++, *r++);
}

TEMPLATE_TEST_CASE("Min", "[min]"
, NUMBERS_ALL()
, VECTORS_ALL(1)
, VECTORS_ALL(2)
, VECTORS_ALL(3)
, VECTORS_ALL(4)
, VECTORS_ALL(5)
, VECTORS_ALL(8)
, VECTORS_ALL(9)
, VECTORS_ALL(16)
, VECTORS_ALL(17)
, VECTORS_ALL(32)
, VECTORS_ALL(33)
) {
using T = TestType;

GIVEN("min(x, y) = r") {
T x, y;
T r, rCheck;

if constexpr (not CT::Vector<T>) {
InitOne(x, 1);
InitOne(y, -5);
}

WHEN("Min is applied") {
ControlMin(x, y, rCheck);
SIMD::Min(x, y, r);

REQUIRE(r == rCheck);

#ifdef LANGULUS_STD_BENCHMARK
BENCHMARK_ADVANCED("Min (control)") (timer meter) {
some<T> nx(meter.runs());
if constexpr (not CT::Vector<T>) {
for (auto& i : nx)
InitOne(i, 1);
}

some<T> ny(meter.runs());
if constexpr (not CT::Vector<T>) {
for (auto& i : ny)
InitOne(i, 1);
}

some<T> nr(meter.runs());
meter.measure([&](int i) {
ControlMin(nx[i], ny[i], nr[i]);
});
};

BENCHMARK_ADVANCED("Min (SIMD)") (timer meter) {
some<T> nx(meter.runs());
if constexpr (not CT::Vector<T>) {
for (auto& i : nx)
InitOne(i, 1);
}

some<T> ny(meter.runs());
if constexpr (not CT::Vector<T>) {
for (auto& i : ny)
InitOne(i, 1);
}

some<T> nr(meter.runs());
meter.measure([&](int i) {
SIMD::Min(nx[i], ny[i], nr[i]);
});
};
#endif
}

WHEN("Min in reverse") {
ControlMin(y, x, rCheck);
SIMD::Min(y, x, r);

REQUIRE(r == rCheck);
}
}
}

0 comments on commit 9082599

Please sign in to comment.