Skip to content

Commit ff48612

Browse files
authored
[SYCL] Fix operator|| and operator&& in vec (#11955)
Adds a special case logical operator implementation for device code using `std::array`. Fixes #11903. --------- Signed-off-by: Michael Aziz <michael.aziz@intel.com>
1 parent 2a3e1ab commit ff48612

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

sycl/include/sycl/types.hpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,11 +1075,24 @@ template <typename Type, int NumElements> class vec {
10751075
#ifdef __SYCL_DEVICE_ONLY__
10761076
#define __SYCL_RELLOGOP(RELLOGOP) \
10771077
vec<rel_t, NumElements> operator RELLOGOP(const vec & Rhs) const { \
1078-
auto Ret = \
1079-
vec<rel_t, NumElements>((typename vec<rel_t, NumElements>::vector_t)( \
1080-
m_Data RELLOGOP Rhs.m_Data)); \
1081-
if (NumElements == 1) /*Scalar 0/1 logic was applied, invert*/ \
1082-
Ret *= -1; \
1078+
vec<rel_t, NumElements> Ret{}; \
1079+
/* This special case is needed since there are no standard operator|| */ \
1080+
/* or operator&& functions for std::array. */ \
1081+
if constexpr (IsUsingArrayOnDevice && \
1082+
(std::string_view(#RELLOGOP) == "||" || \
1083+
std::string_view(#RELLOGOP) == "&&")) { \
1084+
for (size_t I = 0; I < NumElements; ++I) { \
1085+
Ret.setValue(I, \
1086+
-(vec_data<DataT>::get(getValue(I)) \
1087+
RELLOGOP vec_data<DataT>::get(Rhs.getValue(I)))); \
1088+
} \
1089+
} else { \
1090+
Ret = vec<rel_t, NumElements>( \
1091+
(typename vec<rel_t, NumElements>::vector_t)( \
1092+
m_Data RELLOGOP Rhs.m_Data)); \
1093+
if (NumElements == 1) /*Scalar 0/1 logic was applied, invert*/ \
1094+
Ret *= -1; \
1095+
} \
10831096
return Ret; \
10841097
} \
10851098
template <typename T> \
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %{build} -o %t.out
2+
// RUN: %{run} %t.out
3+
// RUN: %if preview-breaking-changes-supported %{ %clangxx -fsycl -fpreview-breaking-changes && ./%t.out %}
4+
5+
#include <sycl/sycl.hpp>
6+
7+
int main() {
8+
sycl::queue q;
9+
q.single_task([=]() {
10+
auto testVec1 = sycl::vec<double, 16>(static_cast<double>(1));
11+
auto testVec2 = sycl::vec<double, 16>(static_cast<double>(2));
12+
sycl::vec<std::int64_t, 16> resVec1 = testVec1 || testVec2;
13+
sycl::vec<std::int64_t, 16> resVec2 = testVec1 && testVec2;
14+
});
15+
}

0 commit comments

Comments
 (0)