Skip to content

[SYCL] Fix arithmetic unary operators for non-native vec #10750

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

Closed
Closed
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
32 changes: 20 additions & 12 deletions sycl/include/sycl/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1281,26 +1281,34 @@ template <typename Type, int NumElements> class vec {
// Use __SYCL_DEVICE_ONLY__ macro because cast to OpenCL vector type is defined
// by SYCL device compiler only.
#ifdef __SYCL_DEVICE_ONLY__
return vec{+m_Data};
#else
vec Ret{};
for (size_t I = 0; I < NumElements; ++I)
Ret.setValue(I, vec_data<DataT>::get(+vec_data<DataT>::get(getValue(I))));
return Ret;
if constexpr (NativeVec) {
return vec{+m_Data};
} else
#endif
{
vec Ret{};
for (size_t I = 0; I < NumElements; ++I)
Ret.setValue(I,
vec_data<DataT>::get(+vec_data<DataT>::get(getValue(I))));
return Ret;
}
}

vec operator-() const {
// Use __SYCL_DEVICE_ONLY__ macro because cast to OpenCL vector type is defined
// by SYCL device compiler only.
#ifdef __SYCL_DEVICE_ONLY__
return vec{-m_Data};
#else
vec Ret{};
for (size_t I = 0; I < NumElements; ++I)
Ret.setValue(I, vec_data<DataT>::get(-vec_data<DataT>::get(getValue(I))));
return Ret;
if constexpr (NativeVec) {
return vec{-m_Data};
} else
#endif
{
vec Ret{};
for (size_t I = 0; I < NumElements; ++I)
Ret.setValue(I,
vec_data<DataT>::get(-vec_data<DataT>::get(getValue(I))));
return Ret;
}
}

// OP is: &&, ||
Expand Down
16 changes: 16 additions & 0 deletions sycl/test/regression/vec_arith_unary_ops.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %clangxx -fsycl -fsyntax-only %s

// Tests that the arithmetic unary operators - and + on vectors that do not use
// native OpenCL vector implementations compile.

#include <sycl/sycl.hpp>

int main() {
sycl::queue Q;
Q.single_task([=]() {
sycl::vec<long, 16> V1{32};
auto V2 = -V1;
auto V3 = +V2;
});
return 0;
}