mirror of
https://github.com/intel/compute-runtime.git
synced 2025-12-19 06:24:51 +08:00
feature: LookupArray findGreaterEqual
Adding helpers for find first greater equal value in lookup array. Useful when finding minimum SLM encoding that satisfies requirements. Related-To: NEO-12747 Signed-off-by: Chodor, Jaroslaw <jaroslaw.chodor@intel.com>
This commit is contained in:
committed by
Compute-Runtime-Automation
parent
676334807f
commit
beedf709bc
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2022-2023 Intel Corporation
|
||||
* Copyright (C) 2022-2025 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
@@ -27,12 +27,27 @@ struct LookupArray {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
constexpr std::optional<ValueT> findGreaterEqual(const KeyT &keyToFind) const {
|
||||
for (auto &[key, value] : lookupArray) {
|
||||
if (key >= keyToFind) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
constexpr ValueT lookUp(const KeyT &keyToFind) const {
|
||||
auto value = find(keyToFind);
|
||||
UNRECOVERABLE_IF(false == value.has_value());
|
||||
return *value;
|
||||
}
|
||||
|
||||
constexpr ValueT lookUpGreaterEqual(const KeyT &keyToFind) const {
|
||||
auto value = findGreaterEqual(keyToFind);
|
||||
UNRECOVERABLE_IF(false == value.has_value());
|
||||
return *value;
|
||||
}
|
||||
|
||||
constexpr size_t size() const {
|
||||
return numElements;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2018-2023 Intel Corporation
|
||||
* Copyright (C) 2018-2025 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "shared/source/utilities/arrayref.h"
|
||||
#include "shared/source/utilities/idlist.h"
|
||||
#include "shared/source/utilities/iflist.h"
|
||||
#include "shared/source/utilities/lookup_array.h"
|
||||
#include "shared/source/utilities/range.h"
|
||||
#include "shared/source/utilities/stackvec.h"
|
||||
#include "shared/test/unit_test/utilities/containers_tests_helpers.h"
|
||||
@@ -1915,3 +1916,56 @@ TEST(ArrayRef, WhenClearedThenEmpty) {
|
||||
arrayRefU32.clear();
|
||||
EXPECT_TRUE(arrayRefU32.empty());
|
||||
}
|
||||
|
||||
TEST(LookupArrayFindGreaterEqual, WhenLookingForElementThenReturnFirstThatIsEqualOrGreater) {
|
||||
LookupArray<int, int, 3> arr({{{1, 10}, {4, 40}, {9, 90}}});
|
||||
auto res = arr.findGreaterEqual(0);
|
||||
ASSERT_TRUE(res.has_value());
|
||||
EXPECT_EQ(10, res.value());
|
||||
|
||||
res = arr.findGreaterEqual(1);
|
||||
ASSERT_TRUE(res.has_value());
|
||||
EXPECT_EQ(10, res.value());
|
||||
|
||||
res = arr.findGreaterEqual(2);
|
||||
ASSERT_TRUE(res.has_value());
|
||||
EXPECT_EQ(40, res.value());
|
||||
|
||||
res = arr.findGreaterEqual(3);
|
||||
ASSERT_TRUE(res.has_value());
|
||||
EXPECT_EQ(40, res.value());
|
||||
|
||||
res = arr.findGreaterEqual(4);
|
||||
ASSERT_TRUE(res.has_value());
|
||||
EXPECT_EQ(40, res.value());
|
||||
|
||||
res = arr.findGreaterEqual(9);
|
||||
ASSERT_TRUE(res.has_value());
|
||||
EXPECT_EQ(90, res.value());
|
||||
|
||||
res = arr.findGreaterEqual(10);
|
||||
EXPECT_FALSE(res.has_value());
|
||||
}
|
||||
|
||||
TEST(LookupArrayLookUpGreaterEqual, WhenLookingForElementThenReturnFirstThatIsEqualOrGreater) {
|
||||
LookupArray<int, int, 3> arr({{{1, 10}, {4, 40}, {9, 90}}});
|
||||
auto res = arr.lookUpGreaterEqual(0);
|
||||
EXPECT_EQ(10, res);
|
||||
|
||||
res = arr.lookUpGreaterEqual(1);
|
||||
EXPECT_EQ(10, res);
|
||||
|
||||
res = arr.lookUpGreaterEqual(2);
|
||||
EXPECT_EQ(40, res);
|
||||
|
||||
res = arr.lookUpGreaterEqual(3);
|
||||
EXPECT_EQ(40, res);
|
||||
|
||||
res = arr.lookUpGreaterEqual(4);
|
||||
EXPECT_EQ(40, res);
|
||||
|
||||
res = arr.lookUpGreaterEqual(9);
|
||||
EXPECT_EQ(90, res);
|
||||
|
||||
EXPECT_THROW(res = arr.lookUpGreaterEqual(10), std::exception);
|
||||
}
|
||||
Reference in New Issue
Block a user