<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 - std::clamp generates suboptimal assembly for primitive types"
   href="https://bugs.llvm.org/show_bug.cgi?id=49909">49909</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>std::clamp generates suboptimal assembly for primitive types
          </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>Windows NT
          </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>vittorio.romeo@outlook.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>`std::clamp` generates poor assembly compared to hand-written counterpart for
primitive types like `float`, even with `-Ofast -ffast-math`:

stdclamp(float, float, float):                         # @stdclamp(float,
float, float)
        movss   dword ptr [rsp - 4], xmm0
        movss   dword ptr [rsp - 8], xmm1
        movss   dword ptr [rsp - 12], xmm2
        ucomiss xmm2, xmm0
        lea     rax, [rsp - 12]
        lea     rcx, [rsp - 4]
        cmovb   rcx, rax
        ucomiss xmm0, xmm1
        lea     rax, [rsp - 8]
        cmovae  rax, rcx
        movss   xmm0, dword ptr [rax]           # xmm0 = mem[0],zero,zero,zero
        ret

myclamp(float, float, float):                          # @myclamp(float, float,
float)
        maxss   xmm0, xmm1
        minss   xmm0, xmm2
        ret

Live example:
<a href="https://godbolt.org/z/5oxvocevK">https://godbolt.org/z/5oxvocevK</a>

More information on:
<a href="https://secret.club/2021/04/09/std-clamp.html">https://secret.club/2021/04/09/std-clamp.html</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>