blob: e3f1230beb9275b75d51bb96cfe98d9e35982a8c [file] [log] [blame]
Aart Bik2a6aad92016-02-25 11:32:32 -08001/*
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
17public class Main {
18
19 /// CHECK-START: int Main.f2int(float) instruction_simplifier (before)
20 /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:FloatFloatToIntBits
21 /// CHECK-DAG: Return [<<Result>>]
22 //
23 /// CHECK-START: int Main.f2int(float) instruction_simplifier (after)
24 /// CHECK-DAG: <<Raw:i\d+>> InvokeStaticOrDirect [<<Arg:f\d+>>] intrinsic:FloatFloatToRawIntBits
25 /// CHECK-DAG: <<Cond:z\d+>> NotEqual [<<Arg>>,<<Arg>>]
26 /// CHECK-DAG: <<Result:i\d+>> Select [<<Raw>>,{{i\d+}},<<Cond>>]
27 /// CHECK-DAG: Return [<<Result>>]
28 private static int f2int(float f) {
29 return Float.floatToIntBits(f);
30 }
31
32 /// CHECK-START: long Main.d2long(double) instruction_simplifier (before)
33 /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect intrinsic:DoubleDoubleToLongBits
34 /// CHECK-DAG: Return [<<Result>>]
35 //
36 /// CHECK-START: long Main.d2long(double) instruction_simplifier (after)
37 /// CHECK-DAG: <<Raw:j\d+>> InvokeStaticOrDirect [<<Arg:d\d+>>] intrinsic:DoubleDoubleToRawLongBits
38 /// CHECK-DAG: <<Cond:z\d+>> NotEqual [<<Arg>>,<<Arg>>]
39 /// CHECK-DAG: <<Result:j\d+>> Select [<<Raw>>,{{j\d+}},<<Cond>>]
40 /// CHECK-DAG: Return [<<Result>>]
41 private static long d2long(double d) {
42 return Double.doubleToLongBits(d);
43 }
44
45 public static void main(String args[]) {
46 // A few distinct numbers.
47 expectEquals32(0xff800000, f2int(Float.NEGATIVE_INFINITY));
48 expectEquals32(0xbf800000, f2int(-1.0f));
49 expectEquals32(0x80000000, f2int(-0.0f));
50 expectEquals32(0x00000000, f2int(+0.0f));
51 expectEquals32(0x3f800000, f2int(+1.0f));
52 expectEquals32(0x7f800000, f2int(Float.POSITIVE_INFINITY));
53
54 // A few others.
55 for (int i = 0; i <= 100; i++) {
56 expectEquals32(i, f2int(Float.intBitsToFloat(i)));
57 }
58
59 // A few NaN numbers.
60 float[] fvals = {
61 Float.intBitsToFloat(0x7f800001),
62 Float.intBitsToFloat(0x7fa00000),
63 Float.intBitsToFloat(0x7fc00000),
64 Float.intBitsToFloat(0x7fffffff),
65 Float.intBitsToFloat(0xff800001),
66 Float.intBitsToFloat(0xffa00000),
67 Float.intBitsToFloat(0xffc00000),
68 Float.intBitsToFloat(0xffffffff)
69 };
70 for (int i = 0; i < fvals.length; i++) {
71 expectEquals32(0x7fc00000, f2int(fvals[i]));
72 }
73
74 // A few distinct numbers.
75 expectEquals64(0xfff0000000000000L, d2long(Double.NEGATIVE_INFINITY));
76 expectEquals64(0xbff0000000000000L, d2long(-1.0d));
77 expectEquals64(0x8000000000000000L, d2long(-0.0d));
78 expectEquals64(0x0000000000000000L, d2long(+0.0d));
79 expectEquals64(0x3ff0000000000000L, d2long(+1.0d));
80 expectEquals64(0x7ff0000000000000L, d2long(Double.POSITIVE_INFINITY));
81
82 // A few others.
83 for (long l = 0; l <= 100; l++) {
84 expectEquals64(l, d2long(Double.longBitsToDouble(l)));
85 }
86
87 // A few NaN numbers.
88 double[] dvals = {
89 Double.longBitsToDouble(0x7ff0000000000001L),
90 Double.longBitsToDouble(0x7ff4000000000000L),
91 Double.longBitsToDouble(0x7ff8000000000000L),
92 Double.longBitsToDouble(0x7fffffffffffffffL),
93 Double.longBitsToDouble(0xfff0000000000001L),
94 Double.longBitsToDouble(0xfff4000000000000L),
95 Double.longBitsToDouble(0xfff8000000000000L),
96 Double.longBitsToDouble(0xffffffffffffffffL)
97 };
98 for (int i = 0; i < dvals.length; i++) {
99 expectEquals64(0x7ff8000000000000L, d2long(dvals[i]));
100 }
101
102 System.out.println("passed");
103 }
104
105 private static void expectEquals32(int expected, int result) {
106 if (expected != result) {
107 throw new Error("Expected: "
108 + Integer.toHexString(expected)
109 + ", found: "
110 + Integer.toHexString(result));
111 }
112 }
113
114 private static void expectEquals64(long expected, long result) {
115 if (expected != result) {
116 throw new Error("Expected: "
117 + Long.toHexString(expected)
118 + ", found: "
119 + Long.toHexString(result));
120 }
121 }
122}