Skip to content

Commit 7daf0c0

Browse files
alexeyvoronov-intelbader
authored andcommitted
[SYCL] Add unary plus and minus for SYCL vec class
Signed-off-by: Alexey Voronov <alexey.voronov@intel.com>
1 parent 489118c commit 7daf0c0

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

sycl/include/CL/sycl/types.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,32 @@ template <typename Type, int NumElements> class vec {
853853
#endif
854854
}
855855

856+
vec operator+() const {
857+
// Use __SYCL_DEVICE_ONLY__ macro because cast to OpenCL vector type is defined
858+
// by SYCL device compiler only.
859+
#ifdef __SYCL_DEVICE_ONLY__
860+
return vec{+m_Data};
861+
#else
862+
vec Ret;
863+
for (size_t I = 0; I < NumElements; ++I)
864+
Ret.setValue(I, +getValue(I));
865+
return Ret;
866+
#endif
867+
}
868+
869+
vec operator-() const {
870+
// Use __SYCL_DEVICE_ONLY__ macro because cast to OpenCL vector type is defined
871+
// by SYCL device compiler only.
872+
#ifdef __SYCL_DEVICE_ONLY__
873+
return vec{-m_Data};
874+
#else
875+
vec Ret;
876+
for (size_t I = 0; I < NumElements; ++I)
877+
Ret.setValue(I, -getValue(I));
878+
return Ret;
879+
#endif
880+
}
881+
856882
// OP is: &&, ||
857883
// vec<RET, NumElements> operatorOP(const vec<DataT, NumElements> &Rhs) const;
858884
// vec<RET, NumElements> operatorOP(const DataT &Rhs) const;
@@ -1185,6 +1211,16 @@ class SwizzleOp {
11851211
return !Tmp;
11861212
}
11871213

1214+
vec_t operator+() {
1215+
vec_t Tmp = *this;
1216+
return +Tmp;
1217+
}
1218+
1219+
vec_t operator-() {
1220+
vec_t Tmp = *this;
1221+
return -Tmp;
1222+
}
1223+
11881224
template <int IdxNum = getNumElements(),
11891225
typename = EnableIfMultipleIndexes<IdxNum>>
11901226
SwizzleOp &operator=(const vec<DataT, IdxNum> &Rhs) {

sycl/test/basic_tests/vec_op.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: env SYCL_DEVICE_TYPE=HOST %t.out
3+
// RUN: %CPU_RUN_PLACEHOLDER %t.out
4+
// RUN: %GPU_RUN_PLACEHOLDER %t.out
5+
// RUN: %ACC_RUN_PLACEHOLDER %t.out
6+
//==------------ vec_op.cpp - SYCL vec operations basic test ---------------==//
7+
//
8+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9+
// See https://llvm.org/LICENSE.txt for license information.
10+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11+
//
12+
//===----------------------------------------------------------------------===//
13+
#define SYCL_SIMPLE_SWIZZLES
14+
15+
#include <CL/sycl.hpp>
16+
17+
#include <cassert>
18+
19+
namespace s = cl::sycl;
20+
21+
template <typename> class test;
22+
23+
template <typename T> void testUnaryOp(const s::vec<T, 2> V) {
24+
s::buffer<int, 1> Buf(s::range<1>(4));
25+
s::queue Queue;
26+
Queue.submit([&](s::handler &Cgh) {
27+
auto Acc = Buf.get_access<s::access::mode::write>(Cgh);
28+
Cgh.single_task<test<T>>([=]() {
29+
Acc[0] = s::all(+V == s::vec<T, 2>{0.0, 1.0});
30+
Acc[1] = s::all(-V == s::vec<T, 2>{-0.0, -1});
31+
Acc[2] = s::all(+V.yx() == s::vec<T, 2>{1.0, 0.0});
32+
Acc[3] = s::all(-V.yx() == s::vec<T, 2>{-1.0, -0.0});
33+
});
34+
});
35+
auto Acc = Buf.get_access<s::access::mode::read>();
36+
assert(Acc[0] == true);
37+
assert(Acc[1] == true);
38+
assert(Acc[2] == true);
39+
assert(Acc[3] == true);
40+
}
41+
42+
int main() {
43+
testUnaryOp(s::int2{0, 1});
44+
testUnaryOp(s::float2{0.f, 1.f});
45+
testUnaryOp(s::half2{0.0, 1.0});
46+
return 0;
47+
}

0 commit comments

Comments
 (0)