Skip to content

[SYCL][ESIMD] Add conversion ctor and operator #3028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions sycl/include/CL/sycl/INTEL/esimd/esimd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ template <typename Ty, int N> class simd {
/// @{
/// Constructors.
constexpr simd() = default;
constexpr simd(const simd &other) { set(other.data()); }
constexpr simd(simd &&other) { set(other.data()); }
template <typename SrcTy> constexpr simd(const simd<SrcTy, N> &other) {
if constexpr (std::is_same<SrcTy, Ty>::value)
set(other.data());
else
set(__builtin_convertvector(other.data(), vector_type_t<Ty, N>));
}
template <typename SrcTy> constexpr simd(simd<SrcTy, N> &&other) {
if constexpr (std::is_same<SrcTy, Ty>::value)
set(other.data());
else
set(__builtin_convertvector(other.data(), vector_type_t<Ty, N>));
}
constexpr simd(const vector_type &Val) { set(Val); }

// TODO @rolandschulz
Expand Down Expand Up @@ -87,6 +97,7 @@ template <typename Ty, int N> class simd {
}
/// @}

/// conversion operator
operator const vector_type &() const & { return M_data; }
operator vector_type &() & { return M_data; }

Expand Down Expand Up @@ -117,12 +128,6 @@ template <typename Ty, int N> class simd {
set(Val2.data());
}

/// {@
/// Assignment operators.
constexpr simd &operator=(const simd &) & = default;
constexpr simd &operator=(simd &&) & = default;
/// @}

/// View this simd object in a different element type.
template <typename EltTy> auto format() & {
using TopRegionTy = compute_format_type_t<simd, EltTy>;
Expand Down Expand Up @@ -208,6 +213,7 @@ template <typename Ty, int N> class simd {
DEF_BINOP(-, -=)
DEF_BINOP(*, *=)
DEF_BINOP(/, /=)
DEF_BINOP(%, %=)

#undef DEF_BINOP

Expand Down
10 changes: 8 additions & 2 deletions sycl/include/CL/sycl/INTEL/esimd/esimd_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ template <typename BaseTy, typename RegionTy> class simd_view {
: M_base(Other.M_base), M_region(Other.M_region) {}
/// @}

/// Conversion to simd value type.
operator value_type() const { return read(); }
/// Conversion to simd type.
template <typename ToTy> operator simd<ToTy, length>() const {
if constexpr (std::is_same<element_type, ToTy>::value)
return read();
else
return convert<ToTy, element_type, length>(read());
}

/// @{
/// Assignment operators.
Expand Down Expand Up @@ -217,6 +222,7 @@ template <typename BaseTy, typename RegionTy> class simd_view {
DEF_BINOP(-, -=)
DEF_BINOP(*, *=)
DEF_BINOP(/, /=)
DEF_BINOP(%, %=)

#undef DEF_BINOP

Expand Down
15 changes: 11 additions & 4 deletions sycl/test/esimd/simd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ bool test_simd_ctors() __attribute__((sycl_device)) {
return v0[0] + v1[1] + v2[2] + v3[3] == 1 + 1 + 2 + 6;
}

void test_conversion() __attribute__((sycl_device)) {
simd<int, 32> v = 3;
simd<float, 32> f = v;
simd<char, 32> c = f;
simd<char, 16> c1 = f.select<16, 1>(0);
f = v + static_cast<simd<int, 32>>(c);
}

bool test_1d_select() __attribute__((sycl_device)) {
simd<int, 32> v = 0;
v.select<8, 1>(0) = 1;
Expand Down Expand Up @@ -63,6 +71,7 @@ bool test_simd_bin_ops() __attribute__((sycl_device)) {
simd<int, 8> v0 = 1;
simd<int, 8> v1 = 2;
v0 += v1;
v0 %= v1;
v0 = 2 - v0;
v0 -= v1;
v0 -= 2;
Expand Down Expand Up @@ -212,14 +221,12 @@ bool test_replicate2() __attribute__((sycl_device)) {
simd<int, 8> v0(0, 1);
auto v0_rep = v0.replicate<2, 4, 2>(1);

return v0_rep[0] == v0[1] && v0_rep[1] == v0[2] &&
v0_rep[2] == v0[5];
return v0_rep[0] == v0[1] && v0_rep[1] == v0[2] && v0_rep[2] == v0[5];
}

bool test_replicate3() __attribute__((sycl_device)) {
simd<int, 8> v0(0, 1);
auto v0_rep = v0.replicate<2, 4, 2, 2>(1);

return v0_rep[0] == v0[1] && v0_rep[1] == v0[3] &&
v0_rep[2] == v0[5];
return v0_rep[0] == v0[1] && v0_rep[1] == v0[3] && v0_rep[2] == v0[5];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a testcase for %=

are you going to create executable tests for new esimd functionality you add?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test added. Yes, I will modify some existing examples to use the new esimd functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kychendev @kbobrovs please review and merge timely

}
1 change: 1 addition & 0 deletions sycl/test/esimd/simd_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bool test_simd_view_bin_ops() __attribute__((sycl_device)) {
auto ref1 = v1.select<8, 2>(0);
ref0 += ref1;
ref0 += 2;
ref0 %= ref1;
ref0 -= ref1;
ref0 -= 2;
ref0 *= ref1;
Expand Down