Skip to content

Commit 1d0e1eb

Browse files
[SYCL] Add vec explicit conversion (#17713)
Implements KhronosGroup/SYCL-Docs#669.
1 parent 4150bdf commit 1d0e1eb

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

sycl/include/sycl/vector.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,23 @@ template <typename T> class GetOp {
125125
//
126126
// must go throw `v.x()` returning a swizzle, then its `operator==` returning
127127
// vec<int, 1> and we want that code to compile.
128-
template <typename Self> class ScalarConversionOperatorMixIn {
129-
using T = typename from_incomplete<Self>::element_type;
128+
template <typename Self> class ScalarConversionOperatorsMixIn {
129+
using element_type = typename from_incomplete<Self>::element_type;
130130

131131
public:
132-
operator T() const { return (*static_cast<const Self *>(this))[0]; }
132+
operator element_type() const {
133+
return (*static_cast<const Self *>(this))[0];
134+
}
135+
136+
#if !__SYCL_USE_LIBSYCL8_VEC_IMPL
137+
template <
138+
typename T, typename = std::enable_if_t<!std::is_same_v<T, element_type>>,
139+
typename =
140+
std::void_t<decltype(static_cast<T>(std::declval<element_type>()))>>
141+
explicit operator T() const {
142+
return static_cast<T>((*static_cast<const Self *>(this))[0]);
143+
}
144+
#endif
133145
};
134146

135147
template <typename T>
@@ -310,7 +322,7 @@ class __SYCL_EBO vec
310322
: public detail::vec_arith<DataT, NumElements>,
311323
public detail::ApplyIf<
312324
NumElements == 1,
313-
detail::ScalarConversionOperatorMixIn<vec<DataT, NumElements>>>,
325+
detail::ScalarConversionOperatorsMixIn<vec<DataT, NumElements>>>,
314326
public detail::NamedSwizzlesMixinBoth<vec<DataT, NumElements>>,
315327
// Keep it last to simplify ABI layout test:
316328
public detail::vec_base<DataT, NumElements> {

sycl/test/basic_tests/vectors/cxx_conversions.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ using sw_double_2 = decltype(std::declval<vec<double, 4>>().swizzle<1, 2>());
3939

4040
#if __INTEL_PREVIEW_BREAKING_CHANGES
4141
#define EXCEPT_IN_PREVIEW !
42+
#define PREVIEW_ONLY
4243
#else
4344
#define EXCEPT_IN_PREVIEW
45+
#define PREVIEW_ONLY !
4446
#endif
4547

4648
// clang-format off
@@ -128,8 +130,8 @@ static_assert( is_explicitly_convertible_to_v<vec<half, 1>, ha
128130
static_assert( is_explicitly_convertible_to_v<vec<half, 1>, float>);
129131
static_assert( is_explicitly_convertible_to_v<vec<half, 1>, double>);
130132
#else
131-
static_assert( !is_explicitly_convertible_to_v<vec<half, 1>, float>);
132-
static_assert( !is_explicitly_convertible_to_v<vec<half, 1>, double>);
133+
static_assert(PREVIEW_ONLY is_explicitly_convertible_to_v<vec<half, 1>, float>);
134+
static_assert(PREVIEW_ONLY is_explicitly_convertible_to_v<vec<half, 1>, double>);
133135
#endif
134136

135137
static_assert( is_explicitly_convertible_to_v<vec<float, 1>, half>);

0 commit comments

Comments
 (0)