<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Same issue with GCC. char and char8_t does not talk with each other with memcpy. std::copy std::copy_n, std::fill, std::fill_n, std::uninitialized_copy std::uninitialized_copy_n, std::fill, std::uninitialized_fill_n fails to convert to memxxx functions"
   href="https://bugs.llvm.org/show_bug.cgi?id=45104">45104</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Same issue with GCC. char and char8_t does not talk with each other with memcpy. std::copy std::copy_n, std::fill, std::fill_n, std::uninitialized_copy std::uninitialized_copy_n, std::fill, std::uninitialized_fill_n fails to convert to memxxx functions
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>euloanty@live.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre><a href="https://godbolt.org/z/dBEAS">https://godbolt.org/z/dBEAS</a>-

Miss an optimization point.

Same issue with GCC bug 93923

<a href="https://godbolt.org/z/SPktTz">https://godbolt.org/z/SPktTz</a>

All these functions should generate exactly the same assembly but they do not.
GCC does not treat char and char8_t the same because libstdc++ does not do this
check. I did my native manually fix and it works. (does not do mul_overflow
something)

//g++ -S copy.cc -Ofast -std=c++2a
#include<cstring>
#include<algorithm>
#include<array>
#include<concepts>
#include<iterator>

auto copy_char8_t_array(char* out,std::array<char8_t,2> const& bits)
{
        return std::copy_n(bits.data(),bits.size(),out);
}


auto memcpy_char8_t_array(char* out,std::array<char8_t,2> const& bits)
{
        std::memcpy(out,bits.data(),bits.size());
        return bits.size();
}


auto copy_char_array(char* out,std::array<char,2> const& bits)
{
        return std::copy_n(bits.data(),bits.size(),out);
}


auto memcpy_char_array(char* out,std::array<char,2> const& bits)
{
        std::memcpy(out,bits.data(),bits.size());
        return bits.size();
}

auto copy_char_array_chars(char8_t* out,std::array<char8_t,2> const& bits)
{
        return std::copy_n(bits.data(),bits.size(),out);
}


auto memcpy_char_array_array_chars(char8_t* out,std::array<char8_t,2> const&
bits)
{
        std::memcpy(out,bits.data(),bits.size());
        return bits.size();
}


auto copy_char_array(char8_t* out,std::array<char,2> const& bits)
{
        return std::copy_n(bits.data(),bits.size(),out);
}


auto memcpy_char_array(char8_t* out,std::array<char,2> const& bits)
{
        std::memcpy(out,bits.data(),bits.size());
        return bits.size();
}
template<std::input_iterator input_iter,std::input_iterator output_iter>
inline constexpr output_iter my_copy_n(input_iter first,std::size_t
count,output_iter result)
{
        if constexpr(std::contiguous_iterator<input_iter>&&
                std::contiguous_iterator<output_iter>&&
                std::is_trivially_copyable_v<typename
std::iterator_traits<input_iter>::value_type>&&
                std::is_trivially_copyable_v<typename
std::iterator_traits<output_iter>::value_type>)
        {
                if constexpr(sizeof(std::is_trivially_copyable_v<typename
std::iterator_traits<input_iter>::value_type>)
                ==sizeof(std::is_trivially_copyable_v<typename
std::iterator_traits<output_iter>::value_type>))
                {
                        memcpy(std::to_address(result),std::to_address(first),
                                sizeof(typename
std::iterator_traits<input_iter>::value_type)*count);
                        return result+count;
                }
        }
        return std::copy_n(first,count,result);
}

auto my_copy_char_array(char8_t* out,std::array<char,2> const& bits)
{
        return my_copy_n(bits.data(),bits.size(),out);
}
auto my_copy_char_array(char* out,std::array<char8_t,2> const& bits)
{
        return my_copy_n(bits.data(),bits.size(),out);
}


auto uninit_copy_char_array(char8_t* out,std::array<char,2> const& bits)
{
        return std::uninitialized_copy_n(bits.data(),bits.size(),out);
}




_Z29memcpy_char_array_array_charsPDuRKSt5arrayIDuLm2EE:
        movzwl  (%rsi), %eax
        movw    %ax, (%rdi)
        movl    $2, %eax
        ret
std::copy_n generates more assembly than it should
_Z15copy_char_arrayPDuRKSt5arrayIcLm2EE:
        movzbl  (%rsi), %eax
        movb    %al, (%rdi)
        movzbl  1(%rsi), %eax
        movb    %al, 1(%rdi)
        leaq    2(%rdi), %rax
        ret</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>