Skip to content
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

Add explicit vec conversion #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion include/simsycl/sycl/vec.hh
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ template<typename T, typename DataT, int NumElements>
concept VecCompatible
= vec_like_num_elements<DataT, T>::value == 1 || vec_like_num_elements<DataT, T>::value == NumElements;

template<typename From, typename To>
concept implicitly_convertible = requires { std::is_convertible_v<From, To>; };
Comment on lines +72 to +73
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears unused?


template<typename From, typename To>
concept explicitly_convertible = requires { static_cast<To>(std::declval<From>()); };
Comment on lines +75 to +76
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: explicitly_convertible_to


template<int... Is>
struct no_repeat_indices;
Expand Down Expand Up @@ -261,6 +266,13 @@ class swizzled_vec {
return m_elems[indices[0]];
}

template<typename T>
explicit operator T() const
requires(num_elements == 1 && detail::explicitly_convertible<value_type, T>)
{
return m_elems[indices[0]];
}

static constexpr size_t byte_size() noexcept { return sycl::vec<value_type, num_elements>::byte_size(); }

static constexpr size_t size() noexcept { return num_elements; }
Expand Down Expand Up @@ -515,7 +527,7 @@ class alignas(detail::vec_alignment_v<DataT, NumElements>) vec {

vec() = default;

explicit constexpr vec(const DataT &arg) {
explicit(num_elements > 1) constexpr vec(const DataT &arg) {
for(int i = 0; i < NumElements; ++i) { m_elems[i] = arg; }
}

Expand Down Expand Up @@ -548,6 +560,13 @@ class alignas(detail::vec_alignment_v<DataT, NumElements>) vec {
return m_elems[0];
}

template<typename T>
explicit operator T() const
requires(NumElements == 1 && detail::explicitly_convertible<DataT, T>)
{
return m_elems[0];
}

static constexpr size_t byte_size() noexcept { return sizeof m_elems; }

static constexpr size_t size() noexcept { return NumElements; }
Expand Down
Loading