<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </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 --- - better 387 code for float x == (int)x"
   href="http://llvm.org/bugs/show_bug.cgi?id=20086">20086</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>better 387 code for float x == (int)x
          </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>Linux
          </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>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>jay.foad@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I get this with clang 3.5.0 (trunk) at r211342:

$ clang -mno-sse -O3 -S -o - -x c - <<<"int f(float x) { return x == (int)x; }"
...
f:
    flds    8(%rsp)
    fnstcw    -10(%rsp)
    movw    -10(%rsp), %ax
    movw    $3199, -10(%rsp)        # imm = 0xC7F
    fldcw    -10(%rsp)
    movw    %ax, -10(%rsp)
    fistl    -8(%rsp)
    fldcw    -10(%rsp)
    movl    -8(%rsp), %eax
    movl    %eax, -4(%rsp)
    fildl    -4(%rsp)
    fucompi    %st(1)
    fstp    %st(0)
    setnp    %al
    sete    %cl
    andb    %al, %cl
    movzbl    %cl, %eax
    retq

All the fnstcw and fldcw stuff is unnecessary, because when we convert x to
int, it doesn't matter what rounding mode we use; all that matters is whether
it is already an exact int value or not.

I think the x == (int)x pattern is fairly common, and it would be nice if clang
generated better 387 code for it.

On the corresponding GCC bug:
<a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61563">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61563</a>
Joseph pointed out that for other combinations of floating point and integer
types (e.g. double and int) it's not completely safe to use a different
rounding mode, because it might affect whether the conversion to int raises an
"invalid" exception.</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>