<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 - [SIMD opt of checksum func] Smart, but could be smarter"
   href="https://bugs.llvm.org/show_bug.cgi?id=42674">42674</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[SIMD opt of checksum func] Smart, but could be smarter
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>bisqwit@iki.fi
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>#include <string.h>
    unsigned char calculate_checksum(const void* ptr)
    {
        unsigned char bytes[16], result = 0;
        memcpy(bytes, ptr, 16); // Endianess does not matter.
        for(unsigned n=0; n<16; ++n) result += bytes[n];
        return result;
    }

For this code, Clang (generates the following SIMD code, which is not too bad.

        vmovdqu xmm0, xmmword ptr [rdi]
        vmovdqa xmmword ptr [rsp - 24], xmm0
        vpshufd xmm1, xmm0, 78          # xmm1 = xmm0[2,3,0,1]
        vpaddb  xmm0, xmm0, xmm1
        vpshufd xmm1, xmm0, 229         # xmm1 = xmm0[1,1,2,3]
        vpaddb  xmm0, xmm0, xmm1
        vpsrld  xmm1, xmm0, 16
        vpaddb  xmm0, xmm0, xmm1
        vpsrlw  xmm1, xmm0, 8
        vpaddb  xmm0, xmm0, xmm1
        vpextrb eax, xmm0, 0
        ret

But here is what ICC (version 18 or newer) generates for it.

        vmovups   xmm0, XMMWORD PTR [rdi]                       #5.9
        vpxor     xmm2, xmm2, xmm2                              #4.41
        vpaddb    xmm0, xmm2, xmm0                              #4.41
        vpsrldq   xmm1, xmm0, 8                                 #4.41
        vpaddb    xmm3, xmm0, xmm1                              #4.41
        vpsadbw   xmm4, xmm2, xmm3                              #4.41
        vmovd     eax, xmm4                                     #4.41
        movsx     rax, al                                       #4.41
        ret                                                     #7.16

Hence the title of this report: Smart, but could be smarter.

For the record, both GCC and MSVC generates almost identical code as Clang for
this testcase. They use VPSRLDQ instead of VPSHUFD rthough. Compiler Explorer
link: <a href="https://godbolt.org/z/hA4J94">https://godbolt.org/z/hA4J94</a></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>