Merge "Fix Math.round bug for large integral arguments"
diff --git a/ojluni/src/main/java/java/lang/Math.java b/ojluni/src/main/java/java/lang/Math.java
index aea7651..0e36ab7 100755
--- a/ojluni/src/main/java/java/lang/Math.java
+++ b/ojluni/src/main/java/java/lang/Math.java
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014 The Android Open Source Project
- * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,8 @@
package java.lang;
import java.util.Random;
-
+import sun.misc.FloatConsts;
+import sun.misc.DoubleConsts;
/**
* The class {@code Math} contains methods for performing basic
@@ -591,7 +592,7 @@
/**
* Returns the closest {@code int} to the argument, with ties
- * rounding up.
+ * rounding to positive infinity.
*
* <p>
* Special cases:
@@ -610,15 +611,37 @@
* @see java.lang.Integer#MIN_VALUE
*/
public static int round(float a) {
- if (a != 0x1.fffffep-2f) // greatest float value less than 0.5
- return (int)floor(a + 0.5f);
- else
- return 0;
+ int intBits = Float.floatToRawIntBits(a);
+ int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
+ >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
+ int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
+ + FloatConsts.EXP_BIAS) - biasedExp;
+ if ((shift & -32) == 0) { // shift >= 0 && shift < 32
+ // a is a finite number such that pow(2,-32) <= ulp(a) < 1
+ int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
+ | (FloatConsts.SIGNIF_BIT_MASK + 1));
+ if (intBits < 0) {
+ r = -r;
+ }
+ // In the comments below each Java expression evaluates to the value
+ // the corresponding mathematical expression:
+ // (r) evaluates to a / ulp(a)
+ // (r >> shift) evaluates to floor(a * 2)
+ // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
+ // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
+ return ((r >> shift) + 1) >> 1;
+ } else {
+ // a is either
+ // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
+ // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
+ // - an infinity or NaN
+ return (int) a;
+ }
}
/**
* Returns the closest {@code long} to the argument, with ties
- * rounding up.
+ * rounding to positive infinity.
*
* <p>Special cases:
* <ul><li>If the argument is NaN, the result is 0.
@@ -637,10 +660,32 @@
* @see java.lang.Long#MIN_VALUE
*/
public static long round(double a) {
- if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
- return (long)floor(a + 0.5d);
- else
- return 0;
+ long longBits = Double.doubleToRawLongBits(a);
+ long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
+ >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
+ long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
+ + DoubleConsts.EXP_BIAS) - biasedExp;
+ if ((shift & -64) == 0) { // shift >= 0 && shift < 64
+ // a is a finite number such that pow(2,-64) <= ulp(a) < 1
+ long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
+ | (DoubleConsts.SIGNIF_BIT_MASK + 1));
+ if (longBits < 0) {
+ r = -r;
+ }
+ // In the comments below each Java expression evaluates to the value
+ // the corresponding mathematical expression:
+ // (r) evaluates to a / ulp(a)
+ // (r >> shift) evaluates to floor(a * 2)
+ // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
+ // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
+ return ((r >> shift) + 1) >> 1;
+ } else {
+ // a is either
+ // - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
+ // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
+ // - an infinity or NaN
+ return (long) a;
+ }
}
private static class NoImagePreloadHolder {
diff --git a/ojluni/src/main/java/java/lang/StrictMath.java b/ojluni/src/main/java/java/lang/StrictMath.java
index b829a81..23978ea 100755
--- a/ojluni/src/main/java/java/lang/StrictMath.java
+++ b/ojluni/src/main/java/java/lang/StrictMath.java
@@ -614,7 +614,7 @@
/**
* Returns the closest {@code int} to the argument, with ties
- * rounding up.
+ * rounding to positive infinity.
*
* <p>Special cases:
* <ul><li>If the argument is NaN, the result is 0.
@@ -637,7 +637,7 @@
/**
* Returns the closest {@code long} to the argument, with ties
- * rounding up.
+ * rounding to positive infinity.
*
* <p>Special cases:
* <ul><li>If the argument is NaN, the result is 0.