1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8160425
  27  * @summary Test vectorization with a signalling NaN.
  28  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-OptimizeFill TestNaNVector
  29  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-OptimizeFill -XX:MaxVectorSize=4 TestNaNVector
  30  */
  31 public class TestNaNVector {
  32     private char[] array;
  33     private static final int LEN = 1024;
  34 
  35     public static void main(String args[]) {
  36         TestNaNVector test = new TestNaNVector();
  37         // Check double precision NaN
  38         for (int i = 0; i < 10_000; ++i) {
  39           test.vectorizeNaNDP();
  40         }
  41         System.out.println("Checking double precision Nan");
  42         test.checkResult(0xfff7);
  43 
  44         // Check single precision NaN
  45         for (int i = 0; i < 10_000; ++i) {
  46           test.vectorizeNaNSP();
  47         }
  48         System.out.println("Checking single precision Nan");
  49         test.checkResult(0xff80);
  50     }
  51 
  52     public TestNaNVector() {
  53         array = new char[LEN];
  54     }
  55 
  56     public void vectorizeNaNDP() {
  57         // This loop will be vectorized and the array store will be replaced by
  58         // a 64-bit vector store to four subsequent array elements. The vector
  59         // should look like this '0xfff7fff7fff7fff7' and is read from the constant
  60         // table. However, in floating point arithmetic this is a signalling NaN
  61         // which may be converted to a quiet NaN when processed by the x87 FPU.
  62         // If the signalling bit is set, the vector ends up in the constant table
  63         // as '0xfffffff7fff7fff7' which leads to an incorrect result.
  64         for (int i = 0; i < LEN; ++i) {
  65             array[i] = 0xfff7;
  66         }
  67     }
  68 
  69     public void vectorizeNaNSP() {
  70         // Same as above but with single precision
  71         for (int i = 0; i < LEN; ++i) {
  72             array[i] = 0xff80;
  73         }
  74     }
  75 
  76     public void checkResult(int expected) {
  77         for (int i = 0; i < LEN; ++i) {
  78             if (array[i] != expected) {
  79                 throw new RuntimeException("Invalid result: array[" + i + "] = " + (int)array[i] + " != " + expected);
  80             }
  81         }
  82     }
  83 }
  84