• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: int Main.round32(float) intrinsics_recognition (after)
20   /// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:MathRoundFloat
21   /// CHECK-DAG:                 Return [<<Result>>]
round32(float f)22   private static int round32(float f) {
23     return Math.round(f);
24   }
25 
26   /// CHECK-START: long Main.round64(double) intrinsics_recognition (after)
27   /// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect intrinsic:MathRoundDouble
28   /// CHECK-DAG:                 Return [<<Result>>]
round64(double d)29   private static long round64(double d) {
30     return Math.round(d);
31   }
32 
main(String args[])33   public static void main(String args[]) {
34     // A few obvious numbers.
35     expectEquals32(-2147483648, round32(Float.NEGATIVE_INFINITY));
36     expectEquals32(-2, round32(-1.51f));
37     expectEquals32(-1, round32(-1.2f));
38     expectEquals32(-1, round32(-1.0f));
39     expectEquals32(-1, round32(-0.51f));
40     expectEquals32(0, round32(-0.2f));
41     expectEquals32(0, round32(-0.0f));
42     expectEquals32(0, round32(+0.0f));
43     expectEquals32(0, round32(+0.2f));
44     expectEquals32(1, round32(+0.5f));
45     expectEquals32(1, round32(+1.0f));
46     expectEquals32(1, round32(+1.2f));
47     expectEquals32(2, round32(+1.5f));
48     expectEquals32(2147483647, round32(Float.POSITIVE_INFINITY));
49 
50     // Some others.
51     for (int i = -100; i <= 100; ++i) {
52       expectEquals32(i - 1, round32((float) i - 0.51f));
53       expectEquals32(i, round32((float) i));
54       expectEquals32(i + 1, round32((float) i + 0.5f));
55     }
56     for (float f = -1.5f; f <= -1.499f; f = Math.nextAfter(f, Float.POSITIVE_INFINITY)) {
57       expectEquals32(-1, round32(f));
58     }
59 
60     // Some harder.
61     float[] fvals = {
62       -16777215.5f,
63       -16777215.0f,
64       -0.4999f,
65       0.4999f,
66       16777215.0f,
67       16777215.5f
68     };
69     int[] ivals = {
70       -16777216,
71       -16777215,
72       0,
73       0,
74       16777215,
75       16777216
76     };
77     for (int i = 0; i < fvals.length; i++) {
78       expectEquals32(ivals[i], round32(fvals[i]));
79     }
80 
81     // A few NaN numbers.
82     float[] fnans = {
83       Float.intBitsToFloat(0x7f800001),
84       Float.intBitsToFloat(0x7fa00000),
85       Float.intBitsToFloat(0x7fc00000),
86       Float.intBitsToFloat(0x7fffffff),
87       Float.intBitsToFloat(0xff800001),
88       Float.intBitsToFloat(0xffa00000),
89       Float.intBitsToFloat(0xffc00000),
90       Float.intBitsToFloat(0xffffffff)
91     };
92     for (int i = 0; i < fnans.length; i++) {
93       expectEquals32(0, round32(fnans[i]));
94     }
95 
96     // A few obvious numbers.
97     expectEquals64(-9223372036854775808L, round64(Double.NEGATIVE_INFINITY));
98     expectEquals64(-2L, round64(-1.51d));
99     expectEquals64(-1L, round64(-1.2d));
100     expectEquals64(-1L, round64(-1.0d));
101     expectEquals64(-1L, round64(-0.51d));
102     expectEquals64(0L, round64(-0.2d));
103     expectEquals64(0L, round64(-0.0d));
104     expectEquals64(0L, round64(+0.0d));
105     expectEquals64(0L, round64(+0.2d));
106     expectEquals64(1L, round64(+0.5d));
107     expectEquals64(1L, round64(+1.0d));
108     expectEquals64(1L, round64(+1.2d));
109     expectEquals64(2L, round64(+1.5d));
110     expectEquals64(9223372036854775807L, round64(Double.POSITIVE_INFINITY));
111 
112     // Some others.
113     for (long l = -100; l <= 100; ++l) {
114       expectEquals64(l - 1, round64((double) l - 0.51d));
115       expectEquals64(l + 1, round64((double) l + 0.5d));
116       expectEquals64(l + 1, round64((double) l + 0.5d));
117     }
118     for (double d = -1.5d; d <= -1.49999999999d; d = Math.nextAfter(d, Double.POSITIVE_INFINITY)) {
119       expectEquals64(-1L, round64(d));
120     }
121 
122     // Some harder.
123     double[] dvals = {
124       -9007199254740991.5d,
125       -9007199254740991.0d,
126       -0.49999999999999994d,
127       0.49999999999999994d,
128       9007199254740991.0d,
129       9007199254740991.5d
130     };
131     long[] lvals = {
132       -9007199254740992L,
133       -9007199254740991L,
134       0L,
135       0L,
136       9007199254740991L,
137       9007199254740992L
138     };
139     for (int i = 0; i < dvals.length; i++) {
140       expectEquals64(lvals[i], round64(dvals[i]));
141     }
142 
143     // A few NaN numbers.
144     double[] dnans = {
145       Double.longBitsToDouble(0x7ff0000000000001L),
146       Double.longBitsToDouble(0x7ff4000000000000L),
147       Double.longBitsToDouble(0x7ff8000000000000L),
148       Double.longBitsToDouble(0x7fffffffffffffffL),
149       Double.longBitsToDouble(0xfff0000000000001L),
150       Double.longBitsToDouble(0xfff4000000000000L),
151       Double.longBitsToDouble(0xfff8000000000000L),
152       Double.longBitsToDouble(0xffffffffffffffffL)
153     };
154     for (int i = 0; i < dnans.length; i++) {
155       expectEquals64(0L, round64(dnans[i]));
156     }
157 
158     System.out.println("passed");
159   }
160 
expectEquals32(int expected, int result)161   private static void expectEquals32(int expected, int result) {
162     if (expected != result) {
163       throw new Error("Expected: " + expected + ", found: " + result);
164     }
165   }
166 
expectEquals64(long expected, long result)167   private static void expectEquals64(long expected, long result) {
168     if (expected != result) {
169       throw new Error("Expected: " + expected + ", found: " + result);
170     }
171   }
172 }
173