Skip to content

[SYCL] Add vec assignment from scalar and more vec modulus overloads #9031

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 5 commits into from
Apr 28, 2023
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
26 changes: 26 additions & 0 deletions sycl/include/sycl/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,16 @@ class SwizzleOp {
return *this;
}

template <int IdxNum = getNumElements(),
EnableIfMultipleIndexes<IdxNum, bool> = true>
SwizzleOp &operator=(const DataT &Rhs) {
std::array<int, IdxNum> Idxs{Indexes...};
for (auto Idx : Idxs) {
m_Vector->setValue(Idx, Rhs);
}
return *this;
}

template <int IdxNum = getNumElements(), typename = EnableIfOneIndex<IdxNum>>
SwizzleOp &operator=(DataT &&Rhs) {
std::array<int, IdxNum> Idxs{Indexes...};
Expand Down Expand Up @@ -1679,6 +1689,21 @@ class SwizzleOp {
Rhs);
}

template <typename T, typename = EnableIfScalarType<T>>
NewLHOp<GetScalarOp<T>, std::modulus, Indexes...>
operator%(const T &Rhs) const {
return NewLHOp<GetScalarOp<T>, std::modulus, Indexes...>(
m_Vector, *this, GetScalarOp<T>(Rhs));
}

template <typename RhsOperation,
typename = EnableIfNoScalarType<RhsOperation>>
NewLHOp<RhsOperation, std::modulus, Indexes...>
operator%(const RhsOperation &Rhs) const {
return NewLHOp<RhsOperation, std::modulus, Indexes...>(m_Vector, *this,
Rhs);
}

template <typename T, typename = EnableIfScalarType<T>>
NewLHOp<GetScalarOp<T>, std::bit_and, Indexes...>
operator&(const T &Rhs) const {
Expand Down Expand Up @@ -2051,6 +2076,7 @@ __SYCL_BINOP(+)
__SYCL_BINOP(-)
__SYCL_BINOP(*)
__SYCL_BINOP(/)
__SYCL_BINOP(%)
__SYCL_BINOP(&)
__SYCL_BINOP(|)
__SYCL_BINOP(^)
Expand Down
34 changes: 34 additions & 0 deletions sycl/test/basic_tests/vectors/vectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ template <typename From> void check_convert_from() {
check_signed_unsigned_convert_to<From, double>();
}

template <typename T, typename OpT> void check_ops(OpT op, T c1, T c2) {
auto check = [&](sycl::vec<T, 2> vres) {
assert(op(c1, c2) == vres[0]);
assert(op(c1, c2) == vres[1]);
};

sycl::vec<T, 2> v1(c1);
sycl::vec<T, 2> v2(c2);
check(op(v1.template swizzle<0, 1>(), v2.template swizzle<0, 1>()));
check(op(v1.template swizzle<0, 1>(), v2));
check(op(v1.template swizzle<0, 1>(), c2));
check(op(c1, v2.template swizzle<0, 1>()));
check(op(c1, v2));
check(op(v1, v2.template swizzle<0, 1>()));
check(op(v1, v2));
check(op(v1, c2));

sycl::vec<T, 2> v3 = {c1, c2};
sycl::vec<T, 2> v4 = op(v3, v3.template swizzle<1, 0>());
assert(v4[0] == op(c1, c2) && v4[1] == op(c2, c1));
sycl::vec<T, 2> v5 = op(v3.template swizzle<1, 1>(), v3);
assert(v5[0] == op(c2, c1) && v5[1] == op(c2, c2));
sycl::vec<T, 2> v6 =
op(v3.template swizzle<1, 1>(), v3.template swizzle<0, 0>());
assert(v6[0] == op(c2, c1) && v6[1] == op(c2, c1));
}

int main() {
sycl::int4 a = {1, 2, 3, 4};
const sycl::int4 b = {10, 20, 30, 40};
Expand Down Expand Up @@ -91,6 +118,11 @@ int main() {
assert(static_cast<float>(b_vec.y()) == static_cast<float>(0.5));
assert(static_cast<float>(b_vec.z()) == static_cast<float>(0.5));
assert(static_cast<float>(b_vec.w()) == static_cast<float>(0.5));
b_vec.swizzle<0, 1, 2, 3>() = 0.6;
assert(static_cast<float>(b_vec.x()) == static_cast<float>(0.6));
assert(static_cast<float>(b_vec.y()) == static_cast<float>(0.6));
assert(static_cast<float>(b_vec.z()) == static_cast<float>(0.6));
assert(static_cast<float>(b_vec.w()) == static_cast<float>(0.6));

// Check that vector with 'unsigned long long' elements has enough bits to
// store value.
Expand Down Expand Up @@ -131,5 +163,7 @@ int main() {
check_convert_from<double>();
check_convert_from<bool>();

check_ops<int>(std::modulus(), 6, 3);

return 0;
}