Skip to content

Commit 8ee2aaa

Browse files
[SYCL] Make swizzle::operator vec available for 1-elem swizzles (#17870)
KhronosGroup/SYCL-Docs#800
1 parent 0809666 commit 8ee2aaa

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

sycl/include/sycl/vector.hpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,13 @@ template <typename Self> class ConversionToVecMixin {
309309

310310
public:
311311
operator vec_ty() const {
312-
vec_ty res{*static_cast<const Self *>(this)};
313-
return res;
312+
auto &self = *static_cast<const Self *>(this);
313+
if constexpr (vec_ty::size() == 1)
314+
// Avoid recursion by explicitly going through `vec(const DataT &)` ctor.
315+
return vec_ty{static_cast<typename vec_ty::element_type>(self)};
316+
else
317+
// Uses `vec`'s variadic ctor.
318+
return vec_ty{self};
314319
}
315320
};
316321

@@ -398,9 +403,8 @@ class __SYCL_EBO Swizzle
398403
public ApplyIf<sizeof...(Indexes) == 1,
399404
ScalarConversionOperatorsMixIn<
400405
Swizzle<IsConstVec, DataT, VecSize, Indexes...>>>,
401-
public ApplyIf<sizeof...(Indexes) != 1,
402-
ConversionToVecMixin<
403-
Swizzle<IsConstVec, DataT, VecSize, Indexes...>>>,
406+
public ConversionToVecMixin<
407+
Swizzle<IsConstVec, DataT, VecSize, Indexes...>>,
404408
public NamedSwizzlesMixinBoth<
405409
Swizzle<IsConstVec, DataT, VecSize, Indexes...>> {
406410
using Base = SwizzleBase<Swizzle<IsConstVec, DataT, VecSize, Indexes...>>;

sycl/test/basic_tests/vectors/cxx_conversions.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ using sw_double_2 = decltype(std::declval<vec<double, 4>>().swizzle<1, 2>());
5353
static_assert( std::is_invocable_v<decltype(f_half_v1), half>);
5454
static_assert(EXCEPT_IN_PREVIEW std::is_invocable_v<decltype(f_half_v1), float>);
5555
static_assert(EXCEPT_IN_PREVIEW std::is_invocable_v<decltype(f_half_v1), double>);
56-
static_assert(EXCEPT_IN_PREVIEW std::is_invocable_v<decltype(f_half_v1), sw_half_1>);
56+
static_assert( std::is_invocable_v<decltype(f_half_v1), sw_half_1>);
5757
static_assert(EXCEPT_IN_PREVIEW std::is_invocable_v<decltype(f_half_v1), sw_float_1>);
5858
static_assert(EXCEPT_IN_PREVIEW std::is_invocable_v<decltype(f_half_v1), sw_double_1>);
5959
static_assert( std::is_invocable_v<decltype(f_half_v1), vec<half, 1>>);
@@ -64,7 +64,7 @@ static_assert(EXCEPT_IN_PREVIEW std::is_invocable_v<decltype(f_float_v1), half>)
6464
static_assert( std::is_invocable_v<decltype(f_float_v1), float>);
6565
static_assert( std::is_invocable_v<decltype(f_float_v1), double>);
6666
static_assert(EXCEPT_IN_PREVIEW std::is_invocable_v<decltype(f_float_v1), sw_half_1>);
67-
static_assert(EXCEPT_IN_PREVIEW std::is_invocable_v<decltype(f_float_v1), sw_float_1>);
67+
static_assert( std::is_invocable_v<decltype(f_float_v1), sw_float_1>);
6868
static_assert(EXCEPT_IN_PREVIEW std::is_invocable_v<decltype(f_float_v1), sw_double_1>);
6969
static_assert(EXCEPT_IN_PREVIEW std::is_invocable_v<decltype(f_float_v1), vec<half, 1>>);
7070
static_assert( std::is_invocable_v<decltype(f_float_v1), vec<float, 1>>);

0 commit comments

Comments
 (0)