Fix test failures for FastMath.pow by preventing JIT branch optimization.
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/a675ca78 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/a675ca78 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/a675ca78 Branch: refs/heads/h10-builds Commit: a675ca7802065f459a96bdbd593d8dc491180cbf Parents: 51cb31e Author: Thomas Neidhart <thomas.neidh...@gmail.com> Authored: Thu May 7 21:35:50 2015 +0200 Committer: Thomas Neidhart <thomas.neidh...@gmail.com> Committed: Thu May 7 21:35:50 2015 +0200 ---------------------------------------------------------------------- src/changes/changes.xml | 4 ++ .../org/apache/commons/math4/util/FastMath.java | 39 +++++--------------- 2 files changed, 13 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/a675ca78/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 19f4046..3219f41 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,10 @@ If the output is not quite correct, check for invisible trailing spaces! </release> <release version="4.0" date="XXXX-XX-XX" description=""> + <action dev="tn" type="fix"> <!-- backported to 3.6 --> + Fix potential branching errors in "FastMath#pow(double, double)" when + passing special values, i.e. infinity, due to erroneous JIT optimization. + </action> <action dev="luc" type="fix" issue="MATH-1118" > <!-- backported to 3.6 --> Fixed equals/hashcode contract failure for Dfp. </action> http://git-wip-us.apache.org/repos/asf/commons-math/blob/a675ca78/src/main/java/org/apache/commons/math4/util/FastMath.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/util/FastMath.java b/src/main/java/org/apache/commons/math4/util/FastMath.java index bdb420e..57793bd 100644 --- a/src/main/java/org/apache/commons/math4/util/FastMath.java +++ b/src/main/java/org/apache/commons/math4/util/FastMath.java @@ -1462,13 +1462,11 @@ public class FastMath { if (y == 0.0) { return 1.0; - } - - if (x != x) { // X is NaN + } else if (x != x) { // X is NaN return x; - } - - if (x == 0) { + } else if (y != y) { // y is NaN + return y; + } else if (x == 0) { long bits = Double.doubleToRawLongBits(x); if ((bits & 0x8000000000000000L) != 0) { // -zero @@ -1491,20 +1489,13 @@ public class FastMath { } return Double.NaN; - } - - if (x == Double.POSITIVE_INFINITY) { - if (y != y) { // y is NaN - return y; - } + } else if (x == Double.POSITIVE_INFINITY) { if (y < 0.0) { return 0.0; } else { return Double.POSITIVE_INFINITY; } - } - - if (y == Double.POSITIVE_INFINITY) { + } else if (y == Double.POSITIVE_INFINITY) { if (x * x == 1.0) { return Double.NaN; } @@ -1514,13 +1505,7 @@ public class FastMath { } else { return 0.0; } - } - - if (x == Double.NEGATIVE_INFINITY) { - if (y != y) { // y is NaN - return y; - } - + } else if (x == Double.NEGATIVE_INFINITY) { if (y < 0) { long yi = (long) y; if (y == yi && (yi & 1) == 1) { @@ -1538,10 +1523,7 @@ public class FastMath { return Double.POSITIVE_INFINITY; } - } - - if (y == Double.NEGATIVE_INFINITY) { - + } else if (y == Double.NEGATIVE_INFINITY) { if (x * x == 1.0) { return Double.NaN; } @@ -1551,10 +1533,7 @@ public class FastMath { } else { return 0.0; } - } - - /* Handle special case x<0 */ - if (x < 0) { + } else if (x < 0) { // Handle special case x<0 // y is an even integer in this case if (y >= TWO_POWER_53 || y <= -TWO_POWER_53) { return pow(-x, y);