<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 - Conversion from uint64_t to double produces wrong result for rounding downward"
   href="https://bugs.llvm.org/show_bug.cgi?id=47393">47393</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Conversion from uint64_t to double produces wrong result for rounding downward
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>chfast@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When floating-point rounding direction is set to "towards negative infinity"
(FE_DOWNWARD) and conversion from uint64_t(0) to double is performed the result
is -0.0 instead of +0.0.

Architecture: x86_64
Clang version: reproducible in all known versions, including trunk
Options: happens for all combinations of -O0, -O2, -frounding-math,
-fsigned-zeros.

Code (-lm is needed)

enum { FE_DOWNWARD = 0x400 };

extern int fesetround(int rounding_direction);

__attribute__((noinline))
double ui64_to_f64(unsigned long x)
{
    return (double)x;
}

int main()
{
    fesetround(FE_DOWNWARD);
    double z = ui64_to_f64(0);
    return __builtin_signbit(z) != 0;  // expected 0
}

Can be played with at <a href="https://godbolt.org/z/jvPGqW">https://godbolt.org/z/jvPGqW</a>.

I believe the main issue is in LLVM.

Without -frounding-math the Clang uses uitofp instruction. The reference says
this should be fine in this context as "it is rounded using the default
rounding mode".

With -frounding-math the 
call double @llvm.experimental.constrained.uitofp.f64.i64(i64 %3, metadata
!"round.dynamic", metadata !"fpexcept.ignore")
is used, but the result is wrong nevertheless.</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>