<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 - Wrong optimization of byte-by-byte copying of long doubles"
   href="https://bugs.llvm.org/show_bug.cgi?id=44188">44188</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Wrong optimization of byte-by-byte copying of long doubles
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>ch3root@openwall.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Source code:

----------------------------------------------------------------------
#include <stdio.h>

int main()
{
    long double x = 1, y = 0;

    unsigned char *px = (unsigned char *)&x;
    unsigned char *py = (unsigned char *)&y;

    py[9] = px[9];
    py[8] = px[8];
    py[7] = px[7];
    py[6] = px[6];
    py[5] = px[5];
    py[4] = px[4];
    py[3] = px[3];
    py[2] = px[2];
    py[1] = px[1];
    py[0] = px[0];

    printf("y = %Lf\n", y);
}
----------------------------------------------------------------------

Results:

----------------------------------------------------------------------
$ clang -std=c11 -Weverything test.c && ./a.out
y = 1.000000
$ clang -std=c11 -Weverything -O3 test.c && ./a.out
y = inf
----------------------------------------------------------------------

clang x86-64 version: clang version 10.0.0
(<a href="https://github.com/llvm/llvm-project.git">https://github.com/llvm/llvm-project.git</a>
3f4b70c79e686117c2754d2c0a5a44c8b6829e79)


As I understand it, optimizer tracks the progress of assignments as actual long
double values and trips on intermediate results:
- initial x is 0xK3FFF8000000000000000;
- initial y is 0xK00000000000000000000;
- after copying one byte (py[9] = px[9];) y should be 0xK3F000000000000000000.
This is an unnormal (zero) and it's somehow changed to 0xK7FFF0000000000000000
(pseudo-infinity);
- final y is 0xK7FFF8000000000000000 (+infinity).
The problem is not that normalization of y went wrong. The problem is that,
during copying, the representation of y has changed outside of control of the
program.

And now the funny part -- DR 260[1] kinda blessed this behavior:

"
Committee Response

Question 1:

Values may have any bit-pattern that validly represents them and the
implementation is free to move between alternate representations (for example,
it may normalize pointers, floating-point representations etc.). In the case of
an indeterminate value all bit-patterns are valid representations and the
actual bit-pattern may change without direct action of the program. 
"

[1] <a href="http://open-std.org/jtc1/sc22/wg14/www/docs/dr_260.htm">http://open-std.org/jtc1/sc22/wg14/www/docs/dr_260.htm</a>

But IMO this shows that DR 260 Q1 is wrong, not that clang is right.</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>