Commit 58d1924b authored by Po-Yen, Chen's avatar Po-Yen, Chen
Browse files

Extract common logic out as helper method

parent 65a317de
...@@ -40,24 +40,34 @@ struct get_carrier<3> ...@@ -40,24 +40,34 @@ struct get_carrier<3>
std::array<std::byte, 3> bytes; std::array<std::byte, 3> bytes;
static_assert(sizeof(bytes) <= sizeof(value_type)); static_assert(sizeof(bytes) <= sizeof(value_type));
public: // replacement of host std::copy_n()
__device__ carrier(value_type value) noexcept template <typename InputIterator, typename Size, typename OutputIterator>
__device__ static OutputIterator copy_n(InputIterator from, Size size, OutputIterator to)
{ {
auto from = reinterpret_cast<const std::byte*>(&value); if(0 < size)
for(auto to = bytes.begin(); to != bytes.end(); ++to, ++from)
{ {
*to = *from; *to = *from;
++to;
for(Size count = 1; count < size; ++count)
{
*to = *++from;
++to;
}
} }
return to;
}
public:
__device__ carrier(value_type value) noexcept
{
copy_n(reinterpret_cast<const std::byte*>(&value), bytes.size(), bytes.begin());
} }
// method to trigger template substitution failure // method to trigger template substitution failure
__device__ carrier& operator=(const carrier& other) noexcept __device__ carrier& operator=(const carrier& other) noexcept
{ {
auto from = other.bytes.begin(); copy_n(other.bytes.begin(), bytes.size(), bytes.begin());
for(auto to = bytes.begin(); to != bytes.end(); ++to, ++from)
{
*to = *from;
}
return *this; return *this;
} }
...@@ -66,11 +76,7 @@ struct get_carrier<3> ...@@ -66,11 +76,7 @@ struct get_carrier<3>
{ {
std::byte result[sizeof(value_type)]; std::byte result[sizeof(value_type)];
auto to = result; copy_n(bytes.begin(), bytes.size(), result);
for(auto from = bytes.begin(); from != bytes.end(); ++to, ++from)
{
*to = *from;
}
return *reinterpret_cast<const value_type*>(result); return *reinterpret_cast<const value_type*>(result);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment