<html>
    <head>
      <base href="https://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 --- - sitofp conversion to half-precision float generates single-precision float without truncating"
   href="https://llvm.org/bugs/show_bug.cgi?id=26871">26871</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>sitofp conversion to half-precision float generates single-precision float without truncating
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </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>Common Code Generator Code
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>andres.noetzli@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following program converts -4095 (not precisely representable as
half-precision float) to a half-precision float, once at runtime and once at
compile time, and adds 1.0:

$ cat ../example.ll
define half @foo(i32 %x) {
  %r = sitofp i32 %x to half
  %rr = fadd half %r, 1.0
  ret half %rr
}

define i1 @main() {
  %x = sitofp i32 -4095 to half
  %xx = fadd half %x, 1.0
  %y = call half @foo(i32 -4095)
  %rr = fcmp oeq half %xx, %y
  ret i1 %rr
}

One would expect the result to be true (1) but the program returns false (0):

$ bin/llc -filetype=obj ../example.ll && clang ../example.o -o a.out &&
./a.out; echo $?
0

The reason seems to be that cvtsi2ssl is used to convert the integer to a
single-precision float but then the addition is done without truncating the
value to a half-precision float before extending it to a single-precision float
for the addition (Note: the problem seems to appear on multiple architectures
(x86, ARM, ...)):

$ bin/llc -o - ../example.ll
    .section    __TEXT,__text,regular,pure_instructions
    .macosx_version_min 10, 11
    .globl    _foo
    .p2align    4, 0x90
_foo:                                   ## @foo
    .cfi_startproc
## BB#0:
    pushq    %rax
Ltmp0:
    .cfi_def_cfa_offset 16
    cvtsi2ssl    %edi, %xmm0
    movss    %xmm0, 4(%rsp)          ## 4-byte Spill
    movl    $15360, %edi            ## imm = 0x3C00
    callq    ___extendhfsf2
    addss    4(%rsp), %xmm0          ## 4-byte Folded Reload
    popq    %rax
    retq
    .cfi_endproc

    .globl    _main
    .p2align    4, 0x90
_main:                                  ## @main
    .cfi_startproc
## BB#0:
    pushq    %rax
Ltmp1:
    .cfi_def_cfa_offset 16
    movl    $-4095, %edi            ## imm = 0xFFFFFFFFFFFFF001
    callq    _foo
    callq    ___truncsfhf2
    movzwl    %ax, %edi
    callq    ___extendhfsf2
    movss    %xmm0, 4(%rsp)          ## 4-byte Spill
    movl    $60416, %edi            ## imm = 0xEC00
    callq    ___extendhfsf2
    movss    4(%rsp), %xmm1          ## 4-byte Reload
                                        ## xmm1 = mem[0],zero,zero,zero
    cmpeqss    %xmm0, %xmm1
    movd    %xmm1, %eax
    andl    $1, %eax
    popq    %rcx
    retq
    .cfi_endproc


.subsections_via_symbols</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>