Skip to content

Commit 8af9ca7

Browse files
Improve Vec3
Signed-off-by: Kamil Kopryk <[email protected]> Related-To: NEO-4692
1 parent eafc0b0 commit 8af9ca7

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

shared/source/helpers/vec.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77

88
#pragma once
99

10+
#include "shared/source/helpers/debug_helpers.h"
11+
12+
#include <cstddef>
13+
#include <cstdint>
14+
#include <type_traits>
15+
1016
template <typename T>
1117
struct Vec3 {
18+
static_assert(std::is_pod<T>::value);
19+
1220
Vec3(T x, T y, T z) : x(x), y(y), z(z) {}
1321
Vec3(const Vec3 &v) : x(v.x), y(v.y), z(v.z) {}
1422
Vec3(const T *arr) {
@@ -43,6 +51,16 @@ struct Vec3 {
4351
return !operator==(vec);
4452
}
4553

54+
T &operator[](uint32_t i) {
55+
UNRECOVERABLE_IF(i > 2);
56+
return values[i];
57+
}
58+
59+
T operator[](uint32_t i) const {
60+
UNRECOVERABLE_IF(i > 2);
61+
return values[i];
62+
}
63+
4664
unsigned int getSimplifiedDim() const {
4765
if (z > 1) {
4866
return 3;
@@ -56,7 +74,14 @@ struct Vec3 {
5674
return 0;
5775
}
5876

59-
T x;
60-
T y;
61-
T z;
77+
union {
78+
struct {
79+
T x, y, z;
80+
};
81+
T values[3];
82+
};
6283
};
84+
85+
static_assert(offsetof(Vec3<size_t>, x) == offsetof(Vec3<size_t>, values[0]));
86+
static_assert(offsetof(Vec3<size_t>, y) == offsetof(Vec3<size_t>, values[1]));
87+
static_assert(offsetof(Vec3<size_t>, z) == offsetof(Vec3<size_t>, values[2]));

shared/test/unit_test/helpers/basic_math_tests.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include "shared/source/helpers/basic_math.h"
9+
#include "shared/source/helpers/vec.h"
910

1011
#include "gtest/gtest.h"
1112

@@ -214,3 +215,28 @@ TEST(ffs, givenNonZeroThenReturnFirstSetBitIndex) {
214215
EXPECT_EQ(16ULL, ffs((1ULL << 63ULL) | (1ULL << 32ULL) | (1ULL << 16ULL)));
215216
EXPECT_EQ(63ULL, ffs(1ULL << 63ULL));
216217
}
218+
219+
TEST(Vec3Tests, whenAccessingVec3ThenCorrectResultsAreReturned) {
220+
Vec3<size_t> vec{1, 2, 3};
221+
222+
EXPECT_EQ(1u, vec[0]);
223+
EXPECT_EQ(2u, vec[1]);
224+
EXPECT_EQ(3u, vec[2]);
225+
EXPECT_EQ(vec.x, vec[0]);
226+
EXPECT_EQ(vec.y, vec[1]);
227+
EXPECT_EQ(vec.z, vec[2]);
228+
229+
vec[0] = 10u;
230+
vec[1] = 20u;
231+
vec[2] = 30u;
232+
EXPECT_EQ(10u, vec.x);
233+
EXPECT_EQ(20u, vec.y);
234+
EXPECT_EQ(30u, vec.z);
235+
EXPECT_ANY_THROW(vec[3]);
236+
237+
const Vec3<size_t> vec2 = {1, 2, 3};
238+
EXPECT_EQ(1u, vec2[0]);
239+
EXPECT_EQ(2u, vec2[1]);
240+
EXPECT_EQ(3u, vec2[2]);
241+
EXPECT_ANY_THROW(vec2[3]);
242+
}

0 commit comments

Comments
 (0)