1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 public class Main {
18 
19   /// CHECK-START: boolean Main.isNaN32(float) instruction_simplifier (before)
20   /// CHECK-DAG: <<Result:z\d+>> InvokeStaticOrDirect
21   /// CHECK-DAG: Return [<<Result>>]
22   //
23   /// CHECK-START: boolean Main.isNaN32(float) instruction_simplifier (after)
24   /// CHECK-DAG: <<Result:z\d+>> NotEqual
25   /// CHECK-DAG: Return [<<Result>>]
26   //
27   /// CHECK-START: boolean Main.isNaN32(float) instruction_simplifier (after)
28   /// CHECK-NOT: InvokeStaticOrDirect
isNaN32(float x)29   private static boolean isNaN32(float x) {
30     return Float.isNaN(x);
31   }
32 
33   /// CHECK-START: boolean Main.isNaN64(double) instruction_simplifier (before)
34   /// CHECK-DAG: <<Result:z\d+>> InvokeStaticOrDirect
35   /// CHECK-DAG: Return [<<Result>>]
36   //
37   /// CHECK-START: boolean Main.isNaN64(double) instruction_simplifier (after)
38   /// CHECK-DAG: <<Result:z\d+>> NotEqual
39   /// CHECK-DAG: Return [<<Result>>]
40   //
41   /// CHECK-START: boolean Main.isNaN64(double) instruction_simplifier (after)
42   /// CHECK-NOT: InvokeStaticOrDirect
isNaN64(double x)43   private static boolean isNaN64(double x) {
44     return Double.isNaN(x);
45   }
46 
main(String args[])47   public static void main(String args[]) {
48     // A few distinct numbers.
49     expectFalse(isNaN32(Float.NEGATIVE_INFINITY));
50     expectFalse(isNaN32(-1.0f));
51     expectFalse(isNaN32(-0.0f));
52     expectFalse(isNaN32(0.0f));
53     expectFalse(isNaN32(1.0f));
54     expectFalse(isNaN32(Float.POSITIVE_INFINITY));
55 
56     // A few distinct subnormal numbers.
57     expectFalse(isNaN32(Float.intBitsToFloat(0x00400000)));
58     expectFalse(isNaN32(Float.intBitsToFloat(0x80400000)));
59     expectFalse(isNaN32(Float.intBitsToFloat(0x00000001)));
60     expectFalse(isNaN32(Float.intBitsToFloat(0x80000001)));
61 
62     // A few NaN numbers.
63     expectTrue(isNaN32(Float.NaN));
64     expectTrue(isNaN32(0.0f / 0.0f));
65     expectTrue(isNaN32((float)Math.sqrt(-1.0f)));
66     float[] fvals = {
67       Float.intBitsToFloat(0x7f800001),
68       Float.intBitsToFloat(0x7fa00000),
69       Float.intBitsToFloat(0x7fc00000),
70       Float.intBitsToFloat(0x7fffffff),
71       Float.intBitsToFloat(0xff800001),
72       Float.intBitsToFloat(0xffa00000),
73       Float.intBitsToFloat(0xffc00000),
74       Float.intBitsToFloat(0xffffffff)
75     };
76     for (int i = 0; i < fvals.length; i++) {
77       expectTrue(isNaN32(fvals[i]));
78     }
79 
80     // A few distinct numbers.
81     expectFalse(isNaN64(Double.NEGATIVE_INFINITY));
82     expectFalse(isNaN32(-1.0f));
83     expectFalse(isNaN64(-0.0d));
84     expectFalse(isNaN64(0.0d));
85     expectFalse(isNaN64(1.0d));
86     expectFalse(isNaN64(Double.POSITIVE_INFINITY));
87 
88     // A few distinct subnormal numbers.
89     expectFalse(isNaN64(Double.longBitsToDouble(0x0008000000000000l)));
90     expectFalse(isNaN64(Double.longBitsToDouble(0x8008000000000000l)));
91     expectFalse(isNaN64(Double.longBitsToDouble(0x0000000000000001l)));
92     expectFalse(isNaN64(Double.longBitsToDouble(0x8000000000000001l)));
93 
94     // A few NaN numbers.
95     expectTrue(isNaN64(Double.NaN));
96     expectTrue(isNaN64(0.0d / 0.0d));
97     expectTrue(isNaN64(Math.sqrt(-1.0d)));
98     double[] dvals = {
99       Double.longBitsToDouble(0x7ff0000000000001L),
100       Double.longBitsToDouble(0x7ff4000000000000L),
101       Double.longBitsToDouble(0x7ff8000000000000L),
102       Double.longBitsToDouble(0x7fffffffffffffffL),
103       Double.longBitsToDouble(0xfff0000000000001L),
104       Double.longBitsToDouble(0xfff4000000000000L),
105       Double.longBitsToDouble(0xfff8000000000000L),
106       Double.longBitsToDouble(0xffffffffffffffffL)
107     };
108     for (int i = 0; i < dvals.length; i++) {
109       expectTrue(isNaN64(dvals[i]));
110     }
111 
112     System.out.println("passed");
113   }
114 
expectTrue(boolean value)115   private static void expectTrue(boolean value) {
116     if (!value) {
117       throw new Error("Expected True");
118     }
119   }
120 
expectFalse(boolean value)121   private static void expectFalse(boolean value) {
122     if (value) {
123       throw new Error("Expected False");
124     }
125   }
126 }
127