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 TestIsNan {
18 
19   /// CHECK-START: boolean TestIsNan.isNaN32(float) builder (after)
20   /// CHECK-DAG: <<Result:z\d+>> NotEqual
21   /// CHECK-DAG: Return [<<Result>>]
22   //
23   /// CHECK-START: boolean TestIsNan.isNaN32(float) builder (after)
24   /// CHECK-NOT: InvokeStaticOrDirect
isNaN32(float x)25   private static boolean isNaN32(float x) {
26     return Float.isNaN(x);
27   }
28 
29   /// CHECK-START: boolean TestIsNan.isNaN64(double) builder (after)
30   /// CHECK-DAG: <<Result:z\d+>> NotEqual
31   /// CHECK-DAG: Return [<<Result>>]
32   //
33   /// CHECK-START: boolean TestIsNan.isNaN64(double) builder (after)
34   /// CHECK-NOT: InvokeStaticOrDirect
isNaN64(double x)35   private static boolean isNaN64(double x) {
36     return Double.isNaN(x);
37   }
38 
main()39   public static void main() {
40     // A few distinct numbers.
41     expectFalse(isNaN32(Float.NEGATIVE_INFINITY));
42     expectFalse(isNaN32(-1.0f));
43     expectFalse(isNaN32(-0.0f));
44     expectFalse(isNaN32(0.0f));
45     expectFalse(isNaN32(1.0f));
46     expectFalse(isNaN32(Float.POSITIVE_INFINITY));
47 
48     // A few distinct subnormal numbers.
49     expectFalse(isNaN32(Float.intBitsToFloat(0x00400000)));
50     expectFalse(isNaN32(Float.intBitsToFloat(0x80400000)));
51     expectFalse(isNaN32(Float.intBitsToFloat(0x00000001)));
52     expectFalse(isNaN32(Float.intBitsToFloat(0x80000001)));
53 
54     // A few NaN numbers.
55     expectTrue(isNaN32(Float.NaN));
56     expectTrue(isNaN32(0.0f / 0.0f));
57     expectTrue(isNaN32((float)Math.sqrt(-1.0f)));
58     float[] fvals = {
59       Float.intBitsToFloat(0x7f800001),
60       Float.intBitsToFloat(0x7fa00000),
61       Float.intBitsToFloat(0x7fc00000),
62       Float.intBitsToFloat(0x7fffffff),
63       Float.intBitsToFloat(0xff800001),
64       Float.intBitsToFloat(0xffa00000),
65       Float.intBitsToFloat(0xffc00000),
66       Float.intBitsToFloat(0xffffffff)
67     };
68     for (int i = 0; i < fvals.length; i++) {
69       expectTrue(isNaN32(fvals[i]));
70     }
71 
72     // A few distinct numbers.
73     expectFalse(isNaN64(Double.NEGATIVE_INFINITY));
74     expectFalse(isNaN32(-1.0f));
75     expectFalse(isNaN64(-0.0d));
76     expectFalse(isNaN64(0.0d));
77     expectFalse(isNaN64(1.0d));
78     expectFalse(isNaN64(Double.POSITIVE_INFINITY));
79 
80     // A few distinct subnormal numbers.
81     expectFalse(isNaN64(Double.longBitsToDouble(0x0008000000000000l)));
82     expectFalse(isNaN64(Double.longBitsToDouble(0x8008000000000000l)));
83     expectFalse(isNaN64(Double.longBitsToDouble(0x0000000000000001l)));
84     expectFalse(isNaN64(Double.longBitsToDouble(0x8000000000000001l)));
85 
86     // A few NaN numbers.
87     expectTrue(isNaN64(Double.NaN));
88     expectTrue(isNaN64(0.0d / 0.0d));
89     expectTrue(isNaN64(Math.sqrt(-1.0d)));
90     double[] dvals = {
91       Double.longBitsToDouble(0x7ff0000000000001L),
92       Double.longBitsToDouble(0x7ff4000000000000L),
93       Double.longBitsToDouble(0x7ff8000000000000L),
94       Double.longBitsToDouble(0x7fffffffffffffffL),
95       Double.longBitsToDouble(0xfff0000000000001L),
96       Double.longBitsToDouble(0xfff4000000000000L),
97       Double.longBitsToDouble(0xfff8000000000000L),
98       Double.longBitsToDouble(0xffffffffffffffffL)
99     };
100     for (int i = 0; i < dvals.length; i++) {
101       expectTrue(isNaN64(dvals[i]));
102     }
103 
104     System.out.println("TestIsNan passed");
105   }
106 
expectTrue(boolean value)107   private static void expectTrue(boolean value) {
108     if (!value) {
109       throw new Error("Expected True");
110     }
111   }
112 
expectFalse(boolean value)113   private static void expectFalse(boolean value) {
114     if (value) {
115       throw new Error("Expected False");
116     }
117   }
118 }
119