<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 - Floating point to integer conversion for Java-like languages"
   href="https://bugs.llvm.org/show_bug.cgi?id=33467">33467</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Floating point to integer conversion for Java-like languages
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>serguei.katkov@azul.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>There are languages like Java where the floating pointer conversion to integer
is strictly defined in case fp value does not fit int range.

Specifically Java semantic can be represented by the following C++ function:
int test1(float f) {
  if (f != f) // NaN
    return 0;
  if (f > (float)std::numeric_limits<int>::max())
    return std::numeric_limits<int>::max();
  if (f < (float)std::numeric_limits<int>::min())
    return std::numeric_limits<int>::min();
  return (int)f;
}

Currently this results in the following X86 assembler code:
0000000000000000 <_Z5test1f>:
   0:   0f 2e c0                ucomiss %xmm0,%xmm0
   3:   7a 25                   jp     2a <_Z5test1f+0x2a>
   5:   b8 ff ff ff 7f          mov    $0x7fffffff,%eax
   a:   0f 2e 05 00 00 00 00    ucomiss 0x0(%rip),%xmm0        # 11
<_Z5test1f+0x11>
  11:   77 16                   ja     29 <_Z5test1f+0x29>
  13:   b8 00 00 00 80          mov    $0x80000000,%eax
  18:   f3 0f 10 0d 00 00 00    movss  0x0(%rip),%xmm1        # 20
<_Z5test1f+0x20>
  1f:   00
  20:   0f 2e c8                ucomiss %xmm0,%xmm1
  23:   77 04                   ja     29 <_Z5test1f+0x29>
  25:   f3 0f 2c c0             cvttss2si %xmm0,%eax
  29:   c3                      retq
  2a:   31 c0                   xor    %eax,%eax
  2c:   c3                      retq

So to do a fp conversion to int we should execute 3 checks for conversion
really happens while all checks actually are for corner cases when fp value is
NaN or does not fit int range.

However X86 instruction cvttss2si has an additional property. It returns
INT_MIN in case fp value does not fit the int range.

So the better generated code would be something like this:
0000000000000000 <_Z4testf>:
   0:   f3 0f 2c c0             cvttss2si %xmm0,%eax
   4:   3d 00 00 00 80          cmp    $0x80000000,%eax
   9:   75 17                   jne    22 <_Z4testf+0x22>
   b:   0f 2e c0                ucomiss %xmm0,%xmm0
   e:   7a 13                   jp     23 <_Z4testf+0x23>
  10:   b8 00 00 00 80          mov    $0x80000000,%eax
  15:   0f 57 c9                xorps  %xmm1,%xmm1
  18:   0f 2e c1                ucomiss %xmm1,%xmm0
  1b:   76 05                   jbe    22 <_Z4testf+0x22>
  1d:   b8 ff ff ff 7f          mov    $0x7fffffff,%eax
  22:   c3                      retq
  23:   31 c0                   xor    %eax,%eax
  25:   c3                      retq
where we need only one check to generate the most common case.
Or even better if we could put the fast pass right after first conversion.

It would be nice if X86 backend could catch this pattern to utilize the
addition property of conversion instruction.</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>