diff --git a/test/data_type/CMakeLists.txt b/test/data_type/CMakeLists.txt index 32d5464e8f..edb2befcbf 100644 --- a/test/data_type/CMakeLists.txt +++ b/test/data_type/CMakeLists.txt @@ -93,6 +93,10 @@ add_gtest_executable(test_custom_type test_custom_type.cpp) if(result EQUAL 0) target_link_libraries(test_custom_type PRIVATE utility) endif() +add_gtest_executable(test_vector_type test_vector_type.cpp) +if(result EQUAL 0) + target_link_libraries(test_vector_type PRIVATE utility) +endif() add_gtest_executable(test_type_convert_const type_convert_const.cpp) add_gtest_executable(test_bhalf test_bhalf.cpp) diff --git a/test/data_type/test_vector_type.cpp b/test/data_type/test_vector_type.cpp new file mode 100644 index 0000000000..f0149683dd --- /dev/null +++ b/test/data_type/test_vector_type.cpp @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2025, Advanced Micro Devices, Inc. All rights reserved. + +#include "gtest/gtest.h" +#include "ck/utility/dtype_vector.hpp" +#include "ck/utility/type_convert.hpp" + +using namespace ck; + +static constexpr auto I0 = Number<0>{}; +static constexpr auto I1 = Number<1>{}; +static constexpr auto I2 = Number<2>{}; +static constexpr auto I3 = Number<3>{}; + +TEST(vector_type, TestConstruction) +{ + using float_vector_1 = typename vector_type_maker::type; + using float_vector_2 = typename vector_type_maker::type; + using float_vector_4 = typename vector_type_maker::type; + + using bhalf_vector_1 = typename vector_type_maker::type; + using bhalf_vector_2 = typename vector_type_maker::type; + + // Verify sizes + EXPECT_EQ(sizeof(float_vector_1), sizeof(float) * 1); + EXPECT_EQ(sizeof(float_vector_2), sizeof(float) * 2); + EXPECT_EQ(sizeof(float_vector_4), sizeof(float) * 4); + EXPECT_EQ(sizeof(bhalf_vector_1), sizeof(ck::bhalf_t) * 1); + EXPECT_EQ(sizeof(bhalf_vector_2), sizeof(ck::bhalf_t) * 2); +} + +TEST(vector_type, TestModifyingContents) +{ + typename vector_type_maker::type dst_vector; + + float val_0 = 1.5f; + float val_1 = -2.25f; + + ck::bhalf_t bhalf_0 = ck::type_convert(val_0); + ck::bhalf_t bhalf_1 = ck::type_convert(val_1); + + dst_vector.template AsType()(I0) = bhalf_0; + dst_vector.template AsType()(I1) = bhalf_1; + + ck::bhalf_t stored_0 = dst_vector.template AsType()[I0]; + ck::bhalf_t stored_1 = dst_vector.template AsType()[I1]; + + float result_0 = ck::type_convert(stored_0); + float result_1 = ck::type_convert(stored_1); + + EXPECT_NEAR(val_0, result_0, 1e-3f); + EXPECT_NEAR(val_1, result_1, 1e-3f); +} + +TEST(vector_type, TestPointerToData) +{ + typename vector_type_maker::type float_vector; + + const float val_1 = 3.14f; + const float val_2 = -1.618f; + + const float val_3 = 2.718f; + const float val_4 = 0.577f; + + // Initialize the vector with some values + float_vector.template AsType()(I0) = val_1; + float_vector.template AsType()(I1) = val_2; + + // Get read-only pointer to data + const float* data_ptr = &(float_vector.template AsType()[I0]); + + // Verify the contents through pointer access + EXPECT_NEAR(data_ptr[0], val_1, 1e-3f); + EXPECT_NEAR(data_ptr[1], val_2, 1e-3f); + + // Get mutable pointer to data + float* mutable_data_ptr = &(float_vector.template AsType()(I0)); + mutable_data_ptr[0] = val_3; + mutable_data_ptr[1] = val_4; + + // Verify the contents after modification + EXPECT_NEAR(float_vector.template AsType()[I0], val_3, 1e-3f); + EXPECT_NEAR(float_vector.template AsType()[I1], val_4, 1e-3f); +} + +TEST(vector_type, TestVectorizedAccess) +{ + typename vector_type_maker::type float_vector; + + const float val_1 = 3.14f; + const float val_2 = -1.618f; + const float val_3 = 2.718f; + const float val_4 = 0.577f; + const float2_t val_12 = {val_1, val_2}; + const float2_t val_34 = {val_3, val_4}; + + // Via reference + float2_t& mutable_value = float_vector.template AsType()(I0); + mutable_value = val_12; + + // Directly via AsType + float_vector.template AsType()(I1) = val_34; + + // Verify the contents after modification + EXPECT_NEAR(float_vector.template AsType()[I0], val_1, 1e-3f); + EXPECT_NEAR(float_vector.template AsType()[I1], val_2, 1e-3f); + EXPECT_NEAR(float_vector.template AsType()[I2], val_3, 1e-3f); + EXPECT_NEAR(float_vector.template AsType()[I3], val_4, 1e-3f); +}