提交 8eebd586 编写于 作者: D darcy

7128441: StrictMath performance improvement note shared with Math

Reviewed-by: darcy
Contributed-by: NMartin Desruisseaux <martin.desruisseaux@geomatys.fr>
上级 8ddac988
...@@ -818,8 +818,9 @@ public final class Math { ...@@ -818,8 +818,9 @@ public final class Math {
return (a >= b) ? a : b; return (a >= b) ? a : b;
} }
private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f); // Use raw bit-wise conversions on guaranteed non-NaN arguments.
private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d); private static long negativeZeroFloatBits = Float.floatToRawIntBits(-0.0f);
private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
/** /**
* Returns the greater of two {@code float} values. That is, * Returns the greater of two {@code float} values. That is,
...@@ -836,9 +837,12 @@ public final class Math { ...@@ -836,9 +837,12 @@ public final class Math {
* @return the larger of {@code a} and {@code b}. * @return the larger of {@code a} and {@code b}.
*/ */
public static float max(float a, float b) { public static float max(float a, float b) {
if (a != a) return a; // a is NaN if (a != a)
if ((a == 0.0f) && (b == 0.0f) return a; // a is NaN
&& (Float.floatToIntBits(a) == negativeZeroFloatBits)) { if ((a == 0.0f) &&
(b == 0.0f) &&
(Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b; return b;
} }
return (a >= b) ? a : b; return (a >= b) ? a : b;
...@@ -859,9 +863,12 @@ public final class Math { ...@@ -859,9 +863,12 @@ public final class Math {
* @return the larger of {@code a} and {@code b}. * @return the larger of {@code a} and {@code b}.
*/ */
public static double max(double a, double b) { public static double max(double a, double b) {
if (a != a) return a; // a is NaN if (a != a)
if ((a == 0.0d) && (b == 0.0d) return a; // a is NaN
&& (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) { if ((a == 0.0d) &&
(b == 0.0d) &&
(Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b; return b;
} }
return (a >= b) ? a : b; return (a >= b) ? a : b;
...@@ -910,9 +917,12 @@ public final class Math { ...@@ -910,9 +917,12 @@ public final class Math {
* @return the smaller of {@code a} and {@code b}. * @return the smaller of {@code a} and {@code b}.
*/ */
public static float min(float a, float b) { public static float min(float a, float b) {
if (a != a) return a; // a is NaN if (a != a)
if ((a == 0.0f) && (b == 0.0f) return a; // a is NaN
&& (Float.floatToIntBits(b) == negativeZeroFloatBits)) { if ((a == 0.0f) &&
(b == 0.0f) &&
(Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b; return b;
} }
return (a <= b) ? a : b; return (a <= b) ? a : b;
...@@ -933,9 +943,12 @@ public final class Math { ...@@ -933,9 +943,12 @@ public final class Math {
* @return the smaller of {@code a} and {@code b}. * @return the smaller of {@code a} and {@code b}.
*/ */
public static double min(double a, double b) { public static double min(double a, double b) {
if (a != a) return a; // a is NaN if (a != a)
if ((a == 0.0d) && (b == 0.0d) return a; // a is NaN
&& (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) { if ((a == 0.0d) &&
(b == 0.0d) &&
(Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b; return b;
} }
return (a <= b) ? a : b; return (a <= b) ? a : b;
......
...@@ -161,6 +161,8 @@ public final class StrictMath { ...@@ -161,6 +161,8 @@ public final class StrictMath {
* in radians. * in radians.
*/ */
public static strictfp double toRadians(double angdeg) { public static strictfp double toRadians(double angdeg) {
// Do not delegate to Math.toRadians(angdeg) because
// this method has the strictfp modifier.
return angdeg / 180.0 * PI; return angdeg / 180.0 * PI;
} }
...@@ -176,6 +178,8 @@ public final class StrictMath { ...@@ -176,6 +178,8 @@ public final class StrictMath {
* in degrees. * in degrees.
*/ */
public static strictfp double toDegrees(double angrad) { public static strictfp double toDegrees(double angrad) {
// Do not delegate to Math.toDegrees(angrad) because
// this method has the strictfp modifier.
return angrad * 180.0 / PI; return angrad * 180.0 / PI;
} }
...@@ -708,7 +712,7 @@ public final class StrictMath { ...@@ -708,7 +712,7 @@ public final class StrictMath {
* @return the absolute value of the argument. * @return the absolute value of the argument.
*/ */
public static int abs(int a) { public static int abs(int a) {
return (a < 0) ? -a : a; return Math.abs(a);
} }
/** /**
...@@ -725,7 +729,7 @@ public final class StrictMath { ...@@ -725,7 +729,7 @@ public final class StrictMath {
* @return the absolute value of the argument. * @return the absolute value of the argument.
*/ */
public static long abs(long a) { public static long abs(long a) {
return (a < 0) ? -a : a; return Math.abs(a);
} }
/** /**
...@@ -744,7 +748,7 @@ public final class StrictMath { ...@@ -744,7 +748,7 @@ public final class StrictMath {
* @return the absolute value of the argument. * @return the absolute value of the argument.
*/ */
public static float abs(float a) { public static float abs(float a) {
return (a <= 0.0F) ? 0.0F - a : a; return Math.abs(a);
} }
/** /**
...@@ -763,7 +767,7 @@ public final class StrictMath { ...@@ -763,7 +767,7 @@ public final class StrictMath {
* @return the absolute value of the argument. * @return the absolute value of the argument.
*/ */
public static double abs(double a) { public static double abs(double a) {
return (a <= 0.0D) ? 0.0D - a : a; return Math.abs(a);
} }
/** /**
...@@ -777,7 +781,7 @@ public final class StrictMath { ...@@ -777,7 +781,7 @@ public final class StrictMath {
* @return the larger of {@code a} and {@code b}. * @return the larger of {@code a} and {@code b}.
*/ */
public static int max(int a, int b) { public static int max(int a, int b) {
return (a >= b) ? a : b; return Math.max(a, b);
} }
/** /**
...@@ -791,13 +795,9 @@ public final class StrictMath { ...@@ -791,13 +795,9 @@ public final class StrictMath {
* @return the larger of {@code a} and {@code b}. * @return the larger of {@code a} and {@code b}.
*/ */
public static long max(long a, long b) { public static long max(long a, long b) {
return (a >= b) ? a : b; return Math.max(a, b);
} }
// Use raw bit-wise conversions on guaranteed non-NaN arguments.
private static long negativeZeroFloatBits = Float.floatToRawIntBits(-0.0f);
private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
/** /**
* Returns the greater of two {@code float} values. That is, * Returns the greater of two {@code float} values. That is,
* the result is the argument closer to positive infinity. If the * the result is the argument closer to positive infinity. If the
...@@ -813,15 +813,7 @@ public final class StrictMath { ...@@ -813,15 +813,7 @@ public final class StrictMath {
* @return the larger of {@code a} and {@code b}. * @return the larger of {@code a} and {@code b}.
*/ */
public static float max(float a, float b) { public static float max(float a, float b) {
if (a != a) return Math.max(a, b);
return a; // a is NaN
if ((a == 0.0f) &&
(b == 0.0f) &&
(Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b;
}
return (a >= b) ? a : b;
} }
/** /**
...@@ -839,15 +831,7 @@ public final class StrictMath { ...@@ -839,15 +831,7 @@ public final class StrictMath {
* @return the larger of {@code a} and {@code b}. * @return the larger of {@code a} and {@code b}.
*/ */
public static double max(double a, double b) { public static double max(double a, double b) {
if (a != a) return Math.max(a, b);
return a; // a is NaN
if ((a == 0.0d) &&
(b == 0.0d) &&
(Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b;
}
return (a >= b) ? a : b;
} }
/** /**
...@@ -861,7 +845,7 @@ public final class StrictMath { ...@@ -861,7 +845,7 @@ public final class StrictMath {
* @return the smaller of {@code a} and {@code b}. * @return the smaller of {@code a} and {@code b}.
*/ */
public static int min(int a, int b) { public static int min(int a, int b) {
return (a <= b) ? a : b; return Math.min(a, b);
} }
/** /**
...@@ -875,7 +859,7 @@ public final class StrictMath { ...@@ -875,7 +859,7 @@ public final class StrictMath {
* @return the smaller of {@code a} and {@code b}. * @return the smaller of {@code a} and {@code b}.
*/ */
public static long min(long a, long b) { public static long min(long a, long b) {
return (a <= b) ? a : b; return Math.min(a, b);
} }
/** /**
...@@ -893,15 +877,7 @@ public final class StrictMath { ...@@ -893,15 +877,7 @@ public final class StrictMath {
* @return the smaller of {@code a} and {@code b.} * @return the smaller of {@code a} and {@code b.}
*/ */
public static float min(float a, float b) { public static float min(float a, float b) {
if (a != a) return Math.min(a, b);
return a; // a is NaN
if ((a == 0.0f) &&
(b == 0.0f) &&
(Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b;
}
return (a <= b) ? a : b;
} }
/** /**
...@@ -919,15 +895,7 @@ public final class StrictMath { ...@@ -919,15 +895,7 @@ public final class StrictMath {
* @return the smaller of {@code a} and {@code b}. * @return the smaller of {@code a} and {@code b}.
*/ */
public static double min(double a, double b) { public static double min(double a, double b) {
if (a != a) return Math.min(a, b);
return a; // a is NaN
if ((a == 0.0d) &&
(b == 0.0d) &&
(Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b;
}
return (a <= b) ? a : b;
} }
/** /**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册