Improve Vec3

Signed-off-by: Kamil Kopryk <kamil.kopryk@intel.com>
Related-To: NEO-4692
This commit is contained in:
Kamil Kopryk 2021-06-09 13:03:43 +00:00 committed by Compute-Runtime-Automation
parent eafc0b08cd
commit 8af9ca71d3
2 changed files with 54 additions and 3 deletions

View File

@ -7,8 +7,16 @@
#pragma once
#include "shared/source/helpers/debug_helpers.h"
#include <cstddef>
#include <cstdint>
#include <type_traits>
template <typename T>
struct Vec3 {
static_assert(std::is_pod<T>::value);
Vec3(T x, T y, T z) : x(x), y(y), z(z) {}
Vec3(const Vec3 &v) : x(v.x), y(v.y), z(v.z) {}
Vec3(const T *arr) {
@ -43,6 +51,16 @@ struct Vec3 {
return !operator==(vec);
}
T &operator[](uint32_t i) {
UNRECOVERABLE_IF(i > 2);
return values[i];
}
T operator[](uint32_t i) const {
UNRECOVERABLE_IF(i > 2);
return values[i];
}
unsigned int getSimplifiedDim() const {
if (z > 1) {
return 3;
@ -56,7 +74,14 @@ struct Vec3 {
return 0;
}
T x;
T y;
T z;
union {
struct {
T x, y, z;
};
T values[3];
};
};
static_assert(offsetof(Vec3<size_t>, x) == offsetof(Vec3<size_t>, values[0]));
static_assert(offsetof(Vec3<size_t>, y) == offsetof(Vec3<size_t>, values[1]));
static_assert(offsetof(Vec3<size_t>, z) == offsetof(Vec3<size_t>, values[2]));

View File

@ -6,6 +6,7 @@
*/
#include "shared/source/helpers/basic_math.h"
#include "shared/source/helpers/vec.h"
#include "gtest/gtest.h"
@ -214,3 +215,28 @@ TEST(ffs, givenNonZeroThenReturnFirstSetBitIndex) {
EXPECT_EQ(16ULL, ffs((1ULL << 63ULL) | (1ULL << 32ULL) | (1ULL << 16ULL)));
EXPECT_EQ(63ULL, ffs(1ULL << 63ULL));
}
TEST(Vec3Tests, whenAccessingVec3ThenCorrectResultsAreReturned) {
Vec3<size_t> vec{1, 2, 3};
EXPECT_EQ(1u, vec[0]);
EXPECT_EQ(2u, vec[1]);
EXPECT_EQ(3u, vec[2]);
EXPECT_EQ(vec.x, vec[0]);
EXPECT_EQ(vec.y, vec[1]);
EXPECT_EQ(vec.z, vec[2]);
vec[0] = 10u;
vec[1] = 20u;
vec[2] = 30u;
EXPECT_EQ(10u, vec.x);
EXPECT_EQ(20u, vec.y);
EXPECT_EQ(30u, vec.z);
EXPECT_ANY_THROW(vec[3]);
const Vec3<size_t> vec2 = {1, 2, 3};
EXPECT_EQ(1u, vec2[0]);
EXPECT_EQ(2u, vec2[1]);
EXPECT_EQ(3u, vec2[2]);
EXPECT_ANY_THROW(vec2[3]);
}