<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 - Clang/LLVM optimizes division and modulo worse than MSVC"
   href="https://bugs.llvm.org/show_bug.cgi?id=37983">37983</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang/LLVM optimizes division and modulo worse than MSVC
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

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

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>sfinae@hotmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=20488" name="attach_20488" title="Test case">attachment 20488</a> <a href="attachment.cgi?id=20488&action=edit" title="Test case">[details]</a></span>
Test case

This affects the Ryu algorithm for printing floating-point numbers
(<a href="https://github.com/ulfjack/ryu">https://github.com/ulfjack/ryu</a> ) and therefore affects C++17 floating-point
std::to_chars(). This is possibly the same bug as
<a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Division followed by modulo generates longer machine code than vice versa"
   href="show_bug.cgi?id=23106">https://bugs.llvm.org/show_bug.cgi?id=23106</a> "Division followed by modulo
generates longer machine code than vice versa".

I observe that MSVC's codegen is unaffected by USE_MODULO, while Clang/LLVM
generates more assembly code (which is slower when profiled in the real
algorithm) for USE_MODULO.

C:\Temp\TESTING_X64>cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.15.26504 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

usage: cl [ option... ] filename... [ /link linkoption... ]

C:\Temp\TESTING_X64>clang-cl -m64 -v
clang version 6.0.0 (tags/RELEASE_600/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: S:\msvc\src\vctools\NonShip\ClangLLVM\bin

C:\Temp\TESTING_X64>type meow.cpp
unsigned long long ryu(unsigned long long vp, unsigned long long vm) {
    bool vmIsTrailingZeros = true;

    while (vp / 10 > vm / 10) {
#ifdef USE_MODULO
        vmIsTrailingZeros &= vm % 10 == 0;
#else
        // The compiler does not realize that vm % 10 can be computed from vm /
10
        // as vm - (vm / 10) * 10.
        vmIsTrailingZeros &= vm - (vm / 10) * 10 == 0; // vm % 10 == 0;
#endif
        vp /= 10;
        vm /= 10;
    }

    return vmIsTrailingZeros ? vp : vm;
}

C:\Temp\TESTING_X64>cl /EHsc /nologo /W4 /MT /O2 /c /FAsc
/Famsvc_workaround.cod meow.cpp
meow.cpp

C:\Temp\TESTING_X64>cl /EHsc /nologo /W4 /MT /O2 /c /FAsc /Famsvc_modulo.cod
/DUSE_MODULO meow.cpp
meow.cpp

C:\Temp\TESTING_X64>clang-cl -m64 /EHsc /nologo /W4 /MT /O2 /c /FA
/Faclang_workaround.asm meow.cpp

C:\Temp\TESTING_X64>clang-cl -m64 /EHsc /nologo /W4 /MT /O2 /c /FA
/Faclang_modulo.asm /DUSE_MODULO meow.cpp

C:\Temp\TESTING_X64></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>