Skip to content

[SYCL] Add vec explicit conversion #17713

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

Merged
Merged
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
20 changes: 16 additions & 4 deletions sycl/include/sycl/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,23 @@ template <typename T> class GetOp {
//
// must go throw `v.x()` returning a swizzle, then its `operator==` returning
// vec<int, 1> and we want that code to compile.
template <typename Self> class ScalarConversionOperatorMixIn {
using T = typename from_incomplete<Self>::element_type;
template <typename Self> class ScalarConversionOperatorsMixIn {
using element_type = typename from_incomplete<Self>::element_type;

public:
operator T() const { return (*static_cast<const Self *>(this))[0]; }
operator element_type() const {
return (*static_cast<const Self *>(this))[0];
}

#if !__SYCL_USE_LIBSYCL8_VEC_IMPL
template <
typename T, typename = std::enable_if_t<!std::is_same_v<T, element_type>>,
typename =
std::void_t<decltype(static_cast<T>(std::declval<element_type>()))>>
explicit operator T() const {
return static_cast<T>((*static_cast<const Self *>(this))[0]);
}
#endif
};

template <typename T>
Expand Down Expand Up @@ -310,7 +322,7 @@ class __SYCL_EBO vec
: public detail::vec_arith<DataT, NumElements>,
public detail::ApplyIf<
NumElements == 1,
detail::ScalarConversionOperatorMixIn<vec<DataT, NumElements>>>,
detail::ScalarConversionOperatorsMixIn<vec<DataT, NumElements>>>,
public detail::NamedSwizzlesMixinBoth<vec<DataT, NumElements>>,
// Keep it last to simplify ABI layout test:
public detail::vec_base<DataT, NumElements> {
Expand Down
6 changes: 4 additions & 2 deletions sycl/test/basic_tests/vectors/cxx_conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ using sw_double_2 = decltype(std::declval<vec<double, 4>>().swizzle<1, 2>());

#if __INTEL_PREVIEW_BREAKING_CHANGES
#define EXCEPT_IN_PREVIEW !
#define PREVIEW_ONLY
#else
#define EXCEPT_IN_PREVIEW
#define PREVIEW_ONLY !
#endif

// clang-format off
Expand Down Expand Up @@ -128,8 +130,8 @@ static_assert( is_explicitly_convertible_to_v<vec<half, 1>, ha
static_assert( is_explicitly_convertible_to_v<vec<half, 1>, float>);
static_assert( is_explicitly_convertible_to_v<vec<half, 1>, double>);
#else
static_assert( !is_explicitly_convertible_to_v<vec<half, 1>, float>);
static_assert( !is_explicitly_convertible_to_v<vec<half, 1>, double>);
static_assert(PREVIEW_ONLY is_explicitly_convertible_to_v<vec<half, 1>, float>);
static_assert(PREVIEW_ONLY is_explicitly_convertible_to_v<vec<half, 1>, double>);
#endif

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