/*
* 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: int Main.round32(float) builder (after)
/// CHECK-DAG: <<Result:i\d+>> InvokeStaticOrDirect intrinsic:MathRoundFloat
/// CHECK-DAG: Return [<<Result>>]
private static int round32(float f) {
return Math.round(f);
}
/// CHECK-START: long Main.round64(double) builder (after)
/// CHECK-DAG: <<Result:j\d+>> InvokeStaticOrDirect intrinsic:MathRoundDouble
/// CHECK-DAG: Return [<<Result>>]
private static long round64(double d) {
return Math.round(d);
}
public static void main(String args[]) {
// A few obvious numbers.
expectEquals32(-2147483648 , round32(Float .NEGATIVE_INFINITY));
expectEquals32(-2 , round32(-1 .51 f));
expectEquals32(-1 , round32(-1 .2 f));
expectEquals32(-1 , round32(-1 .0 f));
expectEquals32(-1 , round32(-0 .5000001 f));
expectEquals32(0 , round32(-0 .5 f));
expectEquals32(0 , round32(-0 .2 f));
expectEquals32(0 , round32(-0 .0 f));
expectEquals32(0 , round32(+0 .0 f));
expectEquals32(0 , round32(+0 .2 f));
expectEquals32(1 , round32(+0 .5 f));
expectEquals32(1 , round32(+1 .0 f));
expectEquals32(1 , round32(+1 .2 f));
expectEquals32(2 , round32(+1 .5 f));
expectEquals32(2147483647 , round32(Float .POSITIVE_INFINITY));
// Near minint.
expectEquals32(-2147483648 , round32(Math.nextAfter(-2147483648 .0 f, Float .NEGATIVE_INFINITY)));
expectEquals32(-2147483648 , round32(-2147483648 .0 f));
expectEquals32(-2147483520 , round32(Math.nextAfter(-2147483648 .0 f, Float .POSITIVE_INFINITY)));
// Near maxint.
expectEquals32(2147483520 , round32(Math.nextAfter(2147483648 .0 f, Float .NEGATIVE_INFINITY)));
expectEquals32(2147483647 , round32(2147483648 .0 f));
expectEquals32(2147483647 , round32(Math.nextAfter(2147483648 .0 f, Float .POSITIVE_INFINITY)));
// Some others.
for (int i = -100 ; i <= 100 ; ++i) {
expectEquals32(i - 1 , round32((float ) i - 0 .51 f));
expectEquals32(i, round32((float ) i - 0 .5 f));
expectEquals32(i, round32((float ) i));
expectEquals32(i + 1 , round32((float ) i + 0 .5 f));
expectEquals32(i + 1 , round32((float ) i + 0 .51 f));
}
for (float f = -1 .5 f; f <= -1 .499 f; f = Math.nextAfter(f, Float .POSITIVE_INFINITY)) {
expectEquals32(-1 , round32(f));
}
// Some harder.
float [] fvals = {
-16777215 .5 f,
-16777215 .0 f,
-0 .49999998 f,
-0 .4999999701976776123046875 f,
0 .4999999701976776123046875 f,
0 .49999998 f,
16777215 .0 f,
16777215 .5 f
};
int [] ivals = {
-16777216 ,
-16777215 ,
0 ,
0 ,
0 ,
0 ,
16777215 ,
16777216
};
for (int i = 0 ; i < fvals.length; i++) {
expectEquals32(ivals[i], round32(fvals[i]));
}
// A few NaN numbers.
float [] fnans = {
Float .intBitsToFloat(0 x7f800001),
Float .intBitsToFloat(0 x7fa00000),
Float .intBitsToFloat(0 x7fc00000),
Float .intBitsToFloat(0 x7fffffff),
Float .intBitsToFloat(0 xff800001),
Float .intBitsToFloat(0 xffa00000),
Float .intBitsToFloat(0 xffc00000),
Float .intBitsToFloat(0 xffffffff)
};
for (int i = 0 ; i < fnans.length; i++) {
expectEquals32(0 , round32(fnans[i]));
}
// A few obvious numbers.
expectEquals64(-9223372036854775808 L, round64(Double .NEGATIVE_INFINITY));
expectEquals64(-2 L, round64(-1 .51 d));
expectEquals64(-1 L, round64(-1 .2 d));
expectEquals64(-1 L, round64(-1 .0 d));
expectEquals64(-1 L, round64(-0 .5000001 f));
expectEquals64(0 L, round64(-0 .5 d));
expectEquals64(0 L, round64(-0 .2 d));
expectEquals64(0 L, round64(-0 .0 d));
expectEquals64(0 L, round64(+0 .0 d));
expectEquals64(0 L, round64(+0 .2 d));
expectEquals64(1 L, round64(+0 .5 d));
expectEquals64(1 L, round64(+1 .0 d));
expectEquals64(1 L, round64(+1 .2 d));
expectEquals64(2 L, round64(+1 .5 d));
expectEquals64(9223372036854775807 L, round64(Double .POSITIVE_INFINITY));
// Near minlong.
expectEquals64(-9223372036854775808 L,
round64(Math.nextAfter(-9223372036854775808 .0 , Double .NEGATIVE_INFINITY)));
expectEquals64(-9223372036854775808 L, round64(-9223372036854775808 .0 ));
expectEquals64(-9223372036854774784 L,
round64(Math.nextAfter(-9223372036854775809 .0 , Double .POSITIVE_INFINITY)));
// Near maxlong.
expectEquals64(9223372036854774784 L,
round64(Math.nextAfter(9223372036854775808 .0 , Double .NEGATIVE_INFINITY)));
expectEquals64(9223372036854775807 L, round64(9223372036854775808 .0 ));
expectEquals64(9223372036854775807 L,
round64(Math.nextAfter(9223372036854775808 .0 , Double .POSITIVE_INFINITY)));
// Some others.
for (long l = -100 ; l <= 100 ; ++l) {
expectEquals64(l - 1 , round64((double ) l - 0 .51 d));
expectEquals64(l, round64((double ) l - 0 .5 d));
expectEquals64(l, round64((double ) l));
expectEquals64(l + 1 , round64((double ) l + 0 .5 d));
expectEquals64(l + 1 , round64((double ) l + 0 .51 d));
}
for (double d = -1 .5 d; d <= -1 .49999999999 d; d = Math.nextAfter(d, Double .POSITIVE_INFINITY)) {
expectEquals64(-1 L, round64(d));
}
// Some harder.
double [] dvals = {
-9007199254740991 .5 d,
-9007199254740991 .0 d,
-0 .49999999999999997 d,
-0 .49999999999999994 d,
0 .49999999999999994 d,
0 .49999999999999997 d,
9007199254740991 .0 d,
9007199254740991 .5 d
};
long [] lvals = {
-9007199254740992 L,
-9007199254740991 L,
0 L,
0 L,
0 L,
0 L,
9007199254740991 L,
9007199254740992 L
};
for (int i = 0 ; i < dvals.length; i++) {
expectEquals64(lvals[i], round64(dvals[i]));
}
// A few NaN numbers.
double [] dnans = {
Double .longBitsToDouble(0 x7ff0000000000001L),
Double .longBitsToDouble(0 x7ff4000000000000L),
Double .longBitsToDouble(0 x7ff8000000000000L),
Double .longBitsToDouble(0 x7fffffffffffffffL),
Double .longBitsToDouble(0 xfff0000000000001L),
Double .longBitsToDouble(0 xfff4000000000000L),
Double .longBitsToDouble(0 xfff8000000000000L),
Double .longBitsToDouble(0 xffffffffffffffffL)
};
for (int i = 0 ; i < dnans.length; i++) {
expectEquals64(0 L, round64(dnans[i]));
}
System.out.println("passed" );
}
private static void expectEquals32(int expected, int result) {
if (expected != result) {
throw new Error("Expected: " + expected + ", found: " + result);
}
}
private static void expectEquals64(long expected, long result) {
if (expected != result) {
throw new Error("Expected: " + expected + ", found: " + result);
}
}
}
Messung V0.5 in Prozent C=81 H=93 G=86
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-29)
¤
*© Formatika GbR, Deutschland