<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 --- - Unsigned Saturation + Truncation at -02 -mavx is giving incorrect runtime results"
   href="https://llvm.org/bugs/show_bug.cgi?id=31773">31773</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Unsigned Saturation + Truncation at -02 -mavx is giving incorrect runtime results
          </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>charles_li@playstation.sony.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>We discovered this bug on PlayStation 4. 
This bug is reproducible on other X86 targets with flag –mavx

This bug started with revision 291670.
$ svn log -r 291670
------------------------------------------------------------------------
r291670 | delena | 2017-01-11 04:59:32 -0800 (Wed, 11 Jan 2017) 
X86 CodeGen: Optimized pattern for truncate with unsigned saturation.
DAG patterns optimization: truncate + unsigned saturation supported by VPMOVUS*
instructions in AVX-512.
And VPACKUS* instructions on SEE* targets.
Differential Revision: <a href="https://reviews.llvm.org/D28216">https://reviews.llvm.org/D28216</a>
------------------------------------------------------------------------

Here is a test case.
$ cat t.cpp
/*******************************************************/
extern "C" int printf(const char * , ...);

union myType
{
  unsigned short ushort_array[32];
  unsigned char uchar_array[64];

  myType() {  // Constructor
    unsigned short val = 1;
    for( int i = 0; i < 32 ; i++ ) {
      ushort_array[i] = val;
      val = val * 7 % 0xFFFFu; // pseudo RNG
    }
  }
};


int main() {
  myType src;
  myType dest;

  for (int i = 0; i < 32; i++) { // saturate to 0xFF then truncate
    dest.uchar_array[i] = static_cast<unsigned char>(
     (src.ushort_array[i] < static_cast<unsigned short>(0xFFu)) ?
      src.ushort_array[i] : static_cast<unsigned short>(0xFFu));
  }

  printf("Index , Source , Target\n");
  for (int j = 0; j < 32; j++) {
    printf("%d , %hx , %hhx \n", j, src.ushort_array[j], dest.uchar_array[j]);
  }

  return 0;
}
/*******************************************************/

Compile this test at –O2 -mavx
$ clang t.cpp -O2 –mavx -o t.elf

Here is the runtime output
$ ./t.elf
Index , Source , Target
0 , 1 , 1
1 , 7 , 7
2 , 31 , 31
3 , 157 , ff
4 , 961 , ff
5 , 41a7 , ff
6 , cb92 , 0
7 , 9103 , 0
8 , f718 , 0
9 , c1ae , 0
10 , 4bc7 , ff
11 , 1273 , ff
12 , 8125 , 0
13 , 8806 , 0
14 , b82d , 0
15 , 940 , ff
16 , 40c0 , ff
17 , c541 , 0
18 , 64cc , ff
19 , c196 , 0
20 , 4b1f , ff
21 , ddb , ff
22 , 60fd , ff
23 , a6ed , 0
24 , 907f , 0
25 , f37c , 0
26 , a86a , 0
27 , 9aea , 0
28 , 3c6a , ff
29 , a6e7 , 0
30 , 9055 , 0
31 , f256 , 0

For Index 6-10, 12-14, 23-27, 29-31,
Target is "0" when it should be "ff".</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>