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