summaryrefslogtreecommitdiffstats
path: root/library/cpp/dot_product/ut/triwaydotproduct_ut.cpp
blob: 521e2884ee5d962a36d51a73815bc6d50e2ccecb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <library/cpp/dot_product/dot_product_avx2.h>
#include <library/cpp/dot_product/dot_product_simple.h>
#include <library/cpp/dot_product/dot_product_sse.h>

#include <library/cpp/testing/unittest/registar.h>

#include <util/system/cpu_id.h>
#include <util/generic/vector.h>

using TriWayFunc = TTriWayDotProduct<float> (*)
        (const float* lhs, const float* rhs, size_t length, bool computeRR) noexcept;

namespace {

void TestTriWay(size_t length, TriWayFunc func) {
    const float lhsMultiplier = 0.25f;
    const float rhsMultiplier = -0.5f;

    TVector<float> lhs(length);
    TVector<float> rhs(length);
    for (size_t i = 0; i < length; ++i) {
        lhs[i] = static_cast<float>(i) * lhsMultiplier;
        rhs[i] = static_cast<float>(length - i) * rhsMultiplier;
    }

    const bool computeRRVariants[] = {false, true};
    for (const bool computeRR : computeRRVariants) {
        const auto expected = TriWayDotProductSimple(lhs.data(), rhs.data(), length, computeRR);
        const auto actual = func(lhs.data(), rhs.data(), length, computeRR);

        UNIT_ASSERT_DOUBLES_EQUAL(expected.LL, actual.LL, std::abs(0.00001 * expected.LL));
        UNIT_ASSERT_DOUBLES_EQUAL(expected.LR, actual.LR, std::abs(0.00001 * expected.LR));
        UNIT_ASSERT_DOUBLES_EQUAL(expected.RR, actual.RR, std::abs(0.00001 * expected.RR));
    }
}

} // anonymous namespace

Y_UNIT_TEST_SUITE(TriWayDotProductTest) {

Y_UNIT_TEST(TestTriWayAvx2Maybe) {
    // depending on compiler flags and machine it might:
    // * test AVX2 version as desired
    // * test SSE version
    // * test nothing
    // but that's fine
    if (!NX86::HaveAVX2() || !NX86::HaveFMA()) {
        return;
    }

    for (size_t length = 640; length < 656; ++length) {
        TestTriWay(length, TriWayDotProductAvx2);
    }
}

Y_UNIT_TEST(TestTriWaySse) {
    if (!NX86::HaveSSE2()) {
        return;
    }

    for (size_t length = 640; length < 656; ++length) {
        TestTriWay(length, TriWayDotProductSse);
    }
}

}