提交 a95d3a3a 编写于 作者: D darcy

7091682: Move sun.misc.FpUtils code into java.lang.Math

Reviewed-by: alanb
上级 a1cbf502
......@@ -283,7 +283,7 @@ public final class Double extends Number implements Comparable<Double> {
// Initialized to maximum size of output.
StringBuffer answer = new StringBuffer(24);
if (FpUtils.rawCopySign(1.0, d) == -1.0) // value is negative,
if (Math.copySign(1.0, d) == -1.0) // value is negative,
answer.append("-"); // so append sign info
answer.append("0x");
......@@ -322,7 +322,7 @@ public final class Double extends Number implements Comparable<Double> {
// E_min -1).
answer.append("p" + (subnormal ?
DoubleConsts.MIN_EXPONENT:
FpUtils.getExponent(d) ));
Math.getExponent(d) ));
}
return answer.toString();
}
......
......@@ -26,7 +26,6 @@
package java.lang;
import sun.misc.FloatingDecimal;
import sun.misc.FpUtils;
import sun.misc.FloatConsts;
import sun.misc.DoubleConsts;
......@@ -279,10 +278,10 @@ public final class Float extends Number implements Comparable<Float> {
// Adjust exponent to create subnormal double, then
// replace subnormal double exponent with subnormal float
// exponent
String s = Double.toHexString(FpUtils.scalb((double)f,
/* -1022+126 */
DoubleConsts.MIN_EXPONENT-
FloatConsts.MIN_EXPONENT));
String s = Double.toHexString(Math.scalb((double)f,
/* -1022+126 */
DoubleConsts.MIN_EXPONENT-
FloatConsts.MIN_EXPONENT));
return s.replaceFirst("p-1022$", "p-126");
}
else // double string will be the same as float string
......
......@@ -26,6 +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
......@@ -963,7 +965,31 @@ public final class Math {
* @since 1.5
*/
public static double ulp(double d) {
return sun.misc.FpUtils.ulp(d);
int exp = getExponent(d);
switch(exp) {
case DoubleConsts.MAX_EXPONENT+1: // NaN or infinity
return Math.abs(d);
case DoubleConsts.MIN_EXPONENT-1: // zero or subnormal
return Double.MIN_VALUE;
default:
assert exp <= DoubleConsts.MAX_EXPONENT && exp >= DoubleConsts.MIN_EXPONENT;
// ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1);
if (exp >= DoubleConsts.MIN_EXPONENT) {
return powerOfTwoD(exp);
}
else {
// return a subnormal result; left shift integer
// representation of Double.MIN_VALUE appropriate
// number of positions
return Double.longBitsToDouble(1L <<
(exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) ));
}
}
}
/**
......@@ -990,7 +1016,31 @@ public final class Math {
* @since 1.5
*/
public static float ulp(float f) {
return sun.misc.FpUtils.ulp(f);
int exp = getExponent(f);
switch(exp) {
case FloatConsts.MAX_EXPONENT+1: // NaN or infinity
return Math.abs(f);
case FloatConsts.MIN_EXPONENT-1: // zero or subnormal
return FloatConsts.MIN_VALUE;
default:
assert exp <= FloatConsts.MAX_EXPONENT && exp >= FloatConsts.MIN_EXPONENT;
// ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1);
if (exp >= FloatConsts.MIN_EXPONENT) {
return powerOfTwoF(exp);
}
else {
// return a subnormal result; left shift integer
// representation of FloatConsts.MIN_VALUE appropriate
// number of positions
return Float.intBitsToFloat(1 <<
(exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) ));
}
}
}
/**
......@@ -1011,7 +1061,7 @@ public final class Math {
* @since 1.5
*/
public static double signum(double d) {
return sun.misc.FpUtils.signum(d);
return (d == 0.0 || Double.isNaN(d))?d:copySign(1.0, d);
}
/**
......@@ -1032,7 +1082,7 @@ public final class Math {
* @since 1.5
*/
public static float signum(float f) {
return sun.misc.FpUtils.signum(f);
return (f == 0.0f || Float.isNaN(f))?f:copySign(1.0f, f);
}
/**
......@@ -1252,7 +1302,11 @@ public final class Math {
* @since 1.6
*/
public static double copySign(double magnitude, double sign) {
return sun.misc.FpUtils.rawCopySign(magnitude, sign);
return Double.longBitsToDouble((Double.doubleToRawLongBits(sign) &
(DoubleConsts.SIGN_BIT_MASK)) |
(Double.doubleToRawLongBits(magnitude) &
(DoubleConsts.EXP_BIT_MASK |
DoubleConsts.SIGNIF_BIT_MASK)));
}
/**
......@@ -1271,7 +1325,11 @@ public final class Math {
* @since 1.6
*/
public static float copySign(float magnitude, float sign) {
return sun.misc.FpUtils.rawCopySign(magnitude, sign);
return Float.intBitsToFloat((Float.floatToRawIntBits(sign) &
(FloatConsts.SIGN_BIT_MASK)) |
(Float.floatToRawIntBits(magnitude) &
(FloatConsts.EXP_BIT_MASK |
FloatConsts.SIGNIF_BIT_MASK)));
}
/**
......@@ -1289,7 +1347,13 @@ public final class Math {
* @since 1.6
*/
public static int getExponent(float f) {
return sun.misc.FpUtils.getExponent(f);
/*
* Bitwise convert f to integer, mask out exponent bits, shift
* to the right and then subtract out float's bias adjust to
* get true exponent value
*/
return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
(FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
/**
......@@ -1307,7 +1371,13 @@ public final class Math {
* @since 1.6
*/
public static int getExponent(double d) {
return sun.misc.FpUtils.getExponent(d);
/*
* Bitwise convert d to long, mask out exponent bits, shift
* to the right and then subtract out double's bias adjust to
* get true exponent value.
*/
return (int)(((Double.doubleToRawLongBits(d) & DoubleConsts.EXP_BIT_MASK) >>
(DoubleConsts.SIGNIFICAND_WIDTH - 1)) - DoubleConsts.EXP_BIAS);
}
/**
......@@ -1351,7 +1421,63 @@ public final class Math {
* @since 1.6
*/
public static double nextAfter(double start, double direction) {
return sun.misc.FpUtils.nextAfter(start, direction);
/*
* The cases:
*
* nextAfter(+infinity, 0) == MAX_VALUE
* nextAfter(+infinity, +infinity) == +infinity
* nextAfter(-infinity, 0) == -MAX_VALUE
* nextAfter(-infinity, -infinity) == -infinity
*
* are naturally handled without any additional testing
*/
// First check for NaN values
if (Double.isNaN(start) || Double.isNaN(direction)) {
// return a NaN derived from the input NaN(s)
return start + direction;
} else if (start == direction) {
return direction;
} else { // start > direction or start < direction
// Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
// then bitwise convert start to integer.
long transducer = Double.doubleToRawLongBits(start + 0.0d);
/*
* IEEE 754 floating-point numbers are lexicographically
* ordered if treated as signed- magnitude integers .
* Since Java's integers are two's complement,
* incrementing" the two's complement representation of a
* logically negative floating-point value *decrements*
* the signed-magnitude representation. Therefore, when
* the integer representation of a floating-point values
* is less than zero, the adjustment to the representation
* is in the opposite direction than would be expected at
* first .
*/
if (direction > start) { // Calculate next greater value
transducer = transducer + (transducer >= 0L ? 1L:-1L);
} else { // Calculate next lesser value
assert direction < start;
if (transducer > 0L)
--transducer;
else
if (transducer < 0L )
++transducer;
/*
* transducer==0, the result is -MIN_VALUE
*
* The transition from zero (implicitly
* positive) to the smallest negative
* signed magnitude value must be done
* explicitly.
*/
else
transducer = DoubleConsts.SIGN_BIT_MASK | 1L;
}
return Double.longBitsToDouble(transducer);
}
}
/**
......@@ -1394,7 +1520,63 @@ public final class Math {
* @since 1.6
*/
public static float nextAfter(float start, double direction) {
return sun.misc.FpUtils.nextAfter(start, direction);
/*
* The cases:
*
* nextAfter(+infinity, 0) == MAX_VALUE
* nextAfter(+infinity, +infinity) == +infinity
* nextAfter(-infinity, 0) == -MAX_VALUE
* nextAfter(-infinity, -infinity) == -infinity
*
* are naturally handled without any additional testing
*/
// First check for NaN values
if (Float.isNaN(start) || Double.isNaN(direction)) {
// return a NaN derived from the input NaN(s)
return start + (float)direction;
} else if (start == direction) {
return (float)direction;
} else { // start > direction or start < direction
// Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
// then bitwise convert start to integer.
int transducer = Float.floatToRawIntBits(start + 0.0f);
/*
* IEEE 754 floating-point numbers are lexicographically
* ordered if treated as signed- magnitude integers .
* Since Java's integers are two's complement,
* incrementing" the two's complement representation of a
* logically negative floating-point value *decrements*
* the signed-magnitude representation. Therefore, when
* the integer representation of a floating-point values
* is less than zero, the adjustment to the representation
* is in the opposite direction than would be expected at
* first.
*/
if (direction > start) {// Calculate next greater value
transducer = transducer + (transducer >= 0 ? 1:-1);
} else { // Calculate next lesser value
assert direction < start;
if (transducer > 0)
--transducer;
else
if (transducer < 0 )
++transducer;
/*
* transducer==0, the result is -MIN_VALUE
*
* The transition from zero (implicitly
* positive) to the smallest negative
* signed magnitude value must be done
* explicitly.
*/
else
transducer = FloatConsts.SIGN_BIT_MASK | 1;
}
return Float.intBitsToFloat(transducer);
}
}
/**
......@@ -1423,7 +1605,13 @@ public final class Math {
* @since 1.6
*/
public static double nextUp(double d) {
return sun.misc.FpUtils.nextUp(d);
if( Double.isNaN(d) || d == Double.POSITIVE_INFINITY)
return d;
else {
d += 0.0d;
return Double.longBitsToDouble(Double.doubleToRawLongBits(d) +
((d >= 0.0d)?+1L:-1L));
}
}
/**
......@@ -1452,7 +1640,13 @@ public final class Math {
* @since 1.6
*/
public static float nextUp(float f) {
return sun.misc.FpUtils.nextUp(f);
if( Float.isNaN(f) || f == FloatConsts.POSITIVE_INFINITY)
return f;
else {
f += 0.0f;
return Float.intBitsToFloat(Float.floatToRawIntBits(f) +
((f >= 0.0f)?+1:-1));
}
}
......@@ -1487,7 +1681,80 @@ public final class Math {
* @since 1.6
*/
public static double scalb(double d, int scaleFactor) {
return sun.misc.FpUtils.scalb(d, scaleFactor);
/*
* This method does not need to be declared strictfp to
* compute the same correct result on all platforms. When
* scaling up, it does not matter what order the
* multiply-store operations are done; the result will be
* finite or overflow regardless of the operation ordering.
* However, to get the correct result when scaling down, a
* particular ordering must be used.
*
* When scaling down, the multiply-store operations are
* sequenced so that it is not possible for two consecutive
* multiply-stores to return subnormal results. If one
* multiply-store result is subnormal, the next multiply will
* round it away to zero. This is done by first multiplying
* by 2 ^ (scaleFactor % n) and then multiplying several
* times by by 2^n as needed where n is the exponent of number
* that is a covenient power of two. In this way, at most one
* real rounding error occurs. If the double value set is
* being used exclusively, the rounding will occur on a
* multiply. If the double-extended-exponent value set is
* being used, the products will (perhaps) be exact but the
* stores to d are guaranteed to round to the double value
* set.
*
* It is _not_ a valid implementation to first multiply d by
* 2^MIN_EXPONENT and then by 2 ^ (scaleFactor %
* MIN_EXPONENT) since even in a strictfp program double
* rounding on underflow could occur; e.g. if the scaleFactor
* argument was (MIN_EXPONENT - n) and the exponent of d was a
* little less than -(MIN_EXPONENT - n), meaning the final
* result would be subnormal.
*
* Since exact reproducibility of this method can be achieved
* without any undue performance burden, there is no
* compelling reason to allow double rounding on underflow in
* scalb.
*/
// magnitude of a power of two so large that scaling a finite
// nonzero value by it would be guaranteed to over or
// underflow; due to rounding, scaling down takes takes an
// additional power of two which is reflected here
final int MAX_SCALE = DoubleConsts.MAX_EXPONENT + -DoubleConsts.MIN_EXPONENT +
DoubleConsts.SIGNIFICAND_WIDTH + 1;
int exp_adjust = 0;
int scale_increment = 0;
double exp_delta = Double.NaN;
// Make sure scaling factor is in a reasonable range
if(scaleFactor < 0) {
scaleFactor = Math.max(scaleFactor, -MAX_SCALE);
scale_increment = -512;
exp_delta = twoToTheDoubleScaleDown;
}
else {
scaleFactor = Math.min(scaleFactor, MAX_SCALE);
scale_increment = 512;
exp_delta = twoToTheDoubleScaleUp;
}
// Calculate (scaleFactor % +/-512), 512 = 2^9, using
// technique from "Hacker's Delight" section 10-2.
int t = (scaleFactor >> 9-1) >>> 32 - 9;
exp_adjust = ((scaleFactor + t) & (512 -1)) - t;
d *= powerOfTwoD(exp_adjust);
scaleFactor -= exp_adjust;
while(scaleFactor != 0) {
d *= exp_delta;
scaleFactor -= scale_increment;
}
return d;
}
/**
......@@ -1521,6 +1788,49 @@ public final class Math {
* @since 1.6
*/
public static float scalb(float f, int scaleFactor) {
return sun.misc.FpUtils.scalb(f, scaleFactor);
// magnitude of a power of two so large that scaling a finite
// nonzero value by it would be guaranteed to over or
// underflow; due to rounding, scaling down takes takes an
// additional power of two which is reflected here
final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
FloatConsts.SIGNIFICAND_WIDTH + 1;
// Make sure scaling factor is in a reasonable range
scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);
/*
* Since + MAX_SCALE for float fits well within the double
* exponent range and + float -> double conversion is exact
* the multiplication below will be exact. Therefore, the
* rounding that occurs when the double product is cast to
* float will be the correctly rounded float result. Since
* all operations other than the final multiply will be exact,
* it is not necessary to declare this method strictfp.
*/
return (float)((double)f*powerOfTwoD(scaleFactor));
}
// Constants used in scalb
static double twoToTheDoubleScaleUp = powerOfTwoD(512);
static double twoToTheDoubleScaleDown = powerOfTwoD(-512);
/**
* Returns a floating-point power of two in the normal range.
*/
static double powerOfTwoD(int n) {
assert(n >= DoubleConsts.MIN_EXPONENT && n <= DoubleConsts.MAX_EXPONENT);
return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
(DoubleConsts.SIGNIFICAND_WIDTH-1))
& DoubleConsts.EXP_BIT_MASK);
}
/**
* Returns a floating-point power of two in the normal range.
*/
public static float powerOfTwoF(int n) {
assert(n >= FloatConsts.MIN_EXPONENT && n <= FloatConsts.MAX_EXPONENT);
return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
(FloatConsts.SIGNIFICAND_WIDTH-1))
& FloatConsts.EXP_BIT_MASK);
}
}
......@@ -25,7 +25,6 @@
package java.lang;
import java.util.Random;
import sun.misc.FpUtils;
import sun.misc.DoubleConsts;
/**
......@@ -428,7 +427,7 @@ public final class StrictMath {
* 1.0, which is exact too.
*/
double twoToThe52 = (double)(1L << 52); // 2^52
double sign = FpUtils.rawCopySign(1.0, a); // preserve sign info
double sign = Math.copySign(1.0, a); // preserve sign info
a = Math.abs(a);
if (a < twoToThe52) { // E_min <= ilogb(a) <= 51
......@@ -955,7 +954,7 @@ public final class StrictMath {
* @since 1.5
*/
public static double ulp(double d) {
return sun.misc.FpUtils.ulp(d);
return Math.ulp(d);
}
/**
......@@ -982,7 +981,7 @@ public final class StrictMath {
* @since 1.5
*/
public static float ulp(float f) {
return sun.misc.FpUtils.ulp(f);
return Math.ulp(f);
}
/**
......@@ -1003,7 +1002,7 @@ public final class StrictMath {
* @since 1.5
*/
public static double signum(double d) {
return sun.misc.FpUtils.signum(d);
return Math.signum(d);
}
/**
......@@ -1024,7 +1023,7 @@ public final class StrictMath {
* @since 1.5
*/
public static float signum(float f) {
return sun.misc.FpUtils.signum(f);
return Math.signum(f);
}
/**
......@@ -1202,7 +1201,7 @@ public final class StrictMath {
* @since 1.6
*/
public static double copySign(double magnitude, double sign) {
return sun.misc.FpUtils.copySign(magnitude, sign);
return Math.copySign(magnitude, (Double.isNaN(sign)?1.0d:sign));
}
/**
......@@ -1218,7 +1217,7 @@ public final class StrictMath {
* @since 1.6
*/
public static float copySign(float magnitude, float sign) {
return sun.misc.FpUtils.copySign(magnitude, sign);
return Math.copySign(magnitude, (Float.isNaN(sign)?1.0f:sign));
}
/**
* Returns the unbiased exponent used in the representation of a
......@@ -1234,7 +1233,7 @@ public final class StrictMath {
* @since 1.6
*/
public static int getExponent(float f) {
return sun.misc.FpUtils.getExponent(f);
return Math.getExponent(f);
}
/**
......@@ -1251,7 +1250,7 @@ public final class StrictMath {
* @since 1.6
*/
public static int getExponent(double d) {
return sun.misc.FpUtils.getExponent(d);
return Math.getExponent(d);
}
/**
......@@ -1294,7 +1293,7 @@ public final class StrictMath {
* @since 1.6
*/
public static double nextAfter(double start, double direction) {
return sun.misc.FpUtils.nextAfter(start, direction);
return Math.nextAfter(start, direction);
}
/**
......@@ -1336,7 +1335,7 @@ public final class StrictMath {
* @since 1.6
*/
public static float nextAfter(float start, double direction) {
return sun.misc.FpUtils.nextAfter(start, direction);
return Math.nextAfter(start, direction);
}
/**
......@@ -1365,7 +1364,7 @@ public final class StrictMath {
* @since 1.6
*/
public static double nextUp(double d) {
return sun.misc.FpUtils.nextUp(d);
return Math.nextUp(d);
}
/**
......@@ -1394,10 +1393,9 @@ public final class StrictMath {
* @since 1.6
*/
public static float nextUp(float f) {
return sun.misc.FpUtils.nextUp(f);
return Math.nextUp(f);
}
/**
* Return {@code d} &times;
* 2<sup>{@code scaleFactor}</sup> rounded as if performed
......@@ -1429,7 +1427,7 @@ public final class StrictMath {
* @since 1.6
*/
public static double scalb(double d, int scaleFactor) {
return sun.misc.FpUtils.scalb(d, scaleFactor);
return Math.scalb(d, scaleFactor);
}
/**
......@@ -1463,6 +1461,6 @@ public final class StrictMath {
* @since 1.6
*/
public static float scalb(float f, int scaleFactor) {
return sun.misc.FpUtils.scalb(f, scaleFactor);
return Math.scalb(f, scaleFactor);
}
}
......@@ -3423,18 +3423,18 @@ public final class Formatter implements Closeable, Flushable {
else {
assert(prec >= 1 && prec <= 12);
int exponent = FpUtils.getExponent(d);
int exponent = Math.getExponent(d);
boolean subnormal
= (exponent == DoubleConsts.MIN_EXPONENT - 1);
// If this is subnormal input so normalize (could be faster to
// do as integer operation).
if (subnormal) {
scaleUp = FpUtils.scalb(1.0, 54);
scaleUp = Math.scalb(1.0, 54);
d *= scaleUp;
// Calculate the exponent. This is not just exponent + 54
// since the former is not the normalized exponent.
exponent = FpUtils.getExponent(d);
exponent = Math.getExponent(d);
assert exponent >= DoubleConsts.MIN_EXPONENT &&
exponent <= DoubleConsts.MAX_EXPONENT: exponent;
}
......
......@@ -25,7 +25,6 @@
package sun.misc;
import sun.misc.FpUtils;
import sun.misc.DoubleConsts;
import sun.misc.FloatConsts;
import java.util.regex.*;
......@@ -2297,9 +2296,9 @@ public class FloatingDecimal{
significand++;
}
FloatingDecimal fd = new FloatingDecimal(FpUtils.rawCopySign(
Double.longBitsToDouble(significand),
sign));
FloatingDecimal fd = new FloatingDecimal(Math.copySign(
Double.longBitsToDouble(significand),
sign));
/*
* Set roundingDir variable field of fd properly so
......
......@@ -25,7 +25,6 @@
package sun.misc;
import sun.misc.FpUtils;
import sun.misc.DoubleConsts;
import sun.misc.FloatConsts;
import java.util.regex.*;
......
......@@ -29,7 +29,6 @@
*/
import java.util.regex.*;
import sun.misc.FpUtils;
import sun.misc.DoubleConsts;
public class ToHexString {
......
......@@ -95,14 +95,14 @@ public class CubeRootTests {
// Test cbrt(2^(3n)) = 2^n.
for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) {
failures += testCubeRootCase(FpUtils.scalb(1.0, 3*i),
FpUtils.scalb(1.0, i) );
failures += testCubeRootCase(Math.scalb(1.0, 3*i),
Math.scalb(1.0, i) );
}
// Test cbrt(2^(-3n)) = 2^-n.
for(int i = -1; i >= FpUtils.ilogb(Double.MIN_VALUE)/3; i--) {
failures += testCubeRootCase(FpUtils.scalb(1.0, 3*i),
FpUtils.scalb(1.0, i) );
for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) {
failures += testCubeRootCase(Math.scalb(1.0, 3*i),
Math.scalb(1.0, i) );
}
// Test random perfect cubes. Create double values with
......@@ -110,10 +110,10 @@ public class CubeRootTests {
// significant bits in the significand set; 17*3 = 51, which
// is less than the number of bits in a double's significand.
long exponentBits1 =
Double.doubleToLongBits(FpUtils.scalb(1.0, 55)) &
Double.doubleToLongBits(Math.scalb(1.0, 55)) &
DoubleConsts.EXP_BIT_MASK;
long exponentBits2=
Double.doubleToLongBits(FpUtils.scalb(1.0, -55)) &
Double.doubleToLongBits(Math.scalb(1.0, -55)) &
DoubleConsts.EXP_BIT_MASK;
for(int i = 0; i < 100; i++) {
// Take 16 bits since the 17th bit is implicit in the
......@@ -177,16 +177,16 @@ public class CubeRootTests {
err = d - StrictMath.pow(y1, 3);
if (err != 0.0) {
if(FpUtils.isNaN(err)) {
if(Double.isNaN(err)) {
failures++;
System.err.println("Encountered unexpected NaN value: d = " + d +
"\tcbrt(d) = " + y1);
} else {
if (err < 0.0) {
err_adjacent = StrictMath.pow(FpUtils.nextUp(y1), 3) - d;
err_adjacent = StrictMath.pow(Math.nextUp(y1), 3) - d;
}
else { // (err > 0.0)
err_adjacent = StrictMath.pow(FpUtils.nextAfter(y1,0.0), 3) - d;
err_adjacent = StrictMath.pow(Math.nextAfter(y1,0.0), 3) - d;
}
if (Math.abs(err) > Math.abs(err_adjacent)) {
......@@ -200,16 +200,16 @@ public class CubeRootTests {
err = d - StrictMath.pow(y2, 3);
if (err != 0.0) {
if(FpUtils.isNaN(err)) {
if(Double.isNaN(err)) {
failures++;
System.err.println("Encountered unexpected NaN value: d = " + d +
"\tcbrt(d) = " + y2);
} else {
if (err < 0.0) {
err_adjacent = StrictMath.pow(FpUtils.nextUp(y2), 3) - d;
err_adjacent = StrictMath.pow(Math.nextUp(y2), 3) - d;
}
else { // (err > 0.0)
err_adjacent = StrictMath.pow(FpUtils.nextAfter(y2,0.0), 3) - d;
err_adjacent = StrictMath.pow(Math.nextAfter(y2,0.0), 3) - d;
}
if (Math.abs(err) > Math.abs(err_adjacent)) {
......@@ -242,13 +242,13 @@ public class CubeRootTests {
// Test near cbrt(2^(3n)) = 2^n.
for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) {
double pc = FpUtils.scalb(1.0, 3*i);
double pc = Math.scalb(1.0, 3*i);
pcNeighbors[2] = pc;
pcNeighbors[1] = FpUtils.nextDown(pc);
pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
pcNeighbors[3] = FpUtils.nextUp(pc);
pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]);
pcNeighbors[3] = Math.nextUp(pc);
pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
for(int j = 0; j < pcNeighbors.length; j++) {
pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]);
......@@ -280,14 +280,14 @@ public class CubeRootTests {
}
// Test near cbrt(2^(-3n)) = 2^-n.
for(int i = -1; i >= FpUtils.ilogb(Double.MIN_VALUE)/3; i--) {
double pc = FpUtils.scalb(1.0, 3*i);
for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) {
double pc = Math.scalb(1.0, 3*i);
pcNeighbors[2] = pc;
pcNeighbors[1] = FpUtils.nextDown(pc);
pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
pcNeighbors[3] = FpUtils.nextUp(pc);
pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]);
pcNeighbors[3] = Math.nextUp(pc);
pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
for(int j = 0; j < pcNeighbors.length; j++) {
pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]);
......
......@@ -82,7 +82,7 @@ public class Expm1Tests {
// For |x| < 2^-54 expm1(x) ~= x
for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
double d = FpUtils.scalb(2, i);
double d = Math.scalb(2, i);
failures += testExpm1Case(d, d);
failures += testExpm1Case(-d, -d);
}
......@@ -101,7 +101,7 @@ public class Expm1Tests {
// For x > 710, expm1(x) should be infinity
for(int i = 10; i <= DoubleConsts.MAX_EXPONENT; i++) {
double d = FpUtils.scalb(2, i);
double d = Math.scalb(2, i);
failures += testExpm1Case(d, infinityD);
}
......@@ -118,7 +118,7 @@ public class Expm1Tests {
}
for(int i = 7; i <= DoubleConsts.MAX_EXPONENT; i++) {
double d = -FpUtils.scalb(2, i);
double d = -Math.scalb(2, i);
failures += testExpm1CaseWithUlpDiff(d, -1.0, 1, reachedLimit);
}
......@@ -145,8 +145,8 @@ public class Expm1Tests {
pcNeighbors[2] = pc;
pcNeighbors[1] = FpUtils.nextDown(pc);
pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
pcNeighbors[3] = FpUtils.nextUp(pc);
pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]);
pcNeighbors[3] = Math.nextUp(pc);
pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
for(int j = 0; j < pcNeighbors.length; j++) {
pcNeighborsExpm1[j] = Math.expm1(pcNeighbors[j]);
......
......@@ -266,7 +266,7 @@ public class HyperbolicTests {
// double significand.
for(int i = DoubleConsts.MIN_SUB_EXPONENT; i < -27; i++) {
double d = FpUtils.scalb(2.0, i);
double d = Math.scalb(2.0, i);
// Result and expected are the same.
failures += testSinhCaseWithUlpDiff(d, d, 2.5);
......@@ -344,7 +344,7 @@ public class HyperbolicTests {
// sinh(x) overflows for values greater than 710; in
// particular, it overflows for all 2^i, i > 10.
for(int i = 10; i <= DoubleConsts.MAX_EXPONENT; i++) {
double d = FpUtils.scalb(2.0, i);
double d = Math.scalb(2.0, i);
// Result and expected are the same.
failures += testSinhCaseWithUlpDiff(d,
......@@ -625,7 +625,7 @@ public class HyperbolicTests {
// rounded.
for(int i = DoubleConsts.MIN_SUB_EXPONENT; i < -27; i++) {
double d = FpUtils.scalb(2.0, i);
double d = Math.scalb(2.0, i);
// Result and expected are the same.
failures += testCoshCaseWithUlpDiff(d, 1.0, 2.5);
......@@ -703,7 +703,7 @@ public class HyperbolicTests {
// cosh(x) overflows for values greater than 710; in
// particular, it overflows for all 2^i, i > 10.
for(int i = 10; i <= DoubleConsts.MAX_EXPONENT; i++) {
double d = FpUtils.scalb(2.0, i);
double d = Math.scalb(2.0, i);
// Result and expected are the same.
failures += testCoshCaseWithUlpDiff(d,
......@@ -984,7 +984,7 @@ public class HyperbolicTests {
// double significand.
for(int i = DoubleConsts.MIN_SUB_EXPONENT; i < -27; i++) {
double d = FpUtils.scalb(2.0, i);
double d = Math.scalb(2.0, i);
// Result and expected are the same.
failures += testTanhCaseWithUlpDiff(d, d, 2.5);
......@@ -998,7 +998,7 @@ public class HyperbolicTests {
}
for(int i = 5; i <= DoubleConsts.MAX_EXPONENT; i++) {
double d = FpUtils.scalb(2.0, i);
double d = Math.scalb(2.0, i);
failures += testTanhCaseWithUlpDiff(d, 1.0, 2.5);
}
......
......@@ -90,7 +90,7 @@ public class HypotTests {
for(int i = DoubleConsts.MIN_SUB_EXPONENT;
i <= DoubleConsts.MAX_EXPONENT;
i++) {
double input = FpUtils.scalb(2, i);
double input = Math.scalb(2, i);
failures += testHypotCase(input, 0.0, input);
}
......@@ -126,7 +126,7 @@ public class HypotTests {
for(int i = 0; i < 1000; i++) {
double d = rand.nextDouble();
// Scale d to have an exponent equal to MAX_EXPONENT -15
d = FpUtils.scalb(d, DoubleConsts.MAX_EXPONENT
d = Math.scalb(d, DoubleConsts.MAX_EXPONENT
-15 - FpUtils.ilogb(d));
for(int j = 0; j <= 13; j += 1) {
failures += testHypotCase(3*d, 4*d, 5*d, 2.5);
......@@ -153,13 +153,13 @@ public class HypotTests {
for(int i = -18; i <= 18; i++) {
double pc = FpUtils.scalb(1.0, i);
double pc = Math.scalb(1.0, i);
pcNeighbors[2] = pc;
pcNeighbors[1] = FpUtils.nextDown(pc);
pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
pcNeighbors[3] = FpUtils.nextUp(pc);
pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]);
pcNeighbors[3] = Math.nextUp(pc);
pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
for(int j = 0; j < pcNeighbors.length; j++) {
pcNeighborsHypot[j] = Math.hypot(2.0, pcNeighbors[j]);
......
......@@ -177,7 +177,7 @@ public class IeeeRecommendedTests {
}
if (i > FloatConsts.MIN_EXPONENT) {
float po2minus = FpUtils.nextAfter(po2,
float po2minus = Math.nextAfter(po2,
Float.NEGATIVE_INFINITY);
failures += testGetExponentCase(po2minus, i-1);
}
......@@ -205,7 +205,7 @@ public class IeeeRecommendedTests {
// Test largest value in next smaller binade
if (i >= 3) {// (i == 1) would test 0.0;
// (i == 2) would just retest MIN_VALUE
testGetExponentCase(FpUtils.nextAfter(top, 0.0f),
testGetExponentCase(Math.nextAfter(top, 0.0f),
FloatConsts.MIN_EXPONENT - 1);
if( i >= 10) {
......@@ -284,7 +284,7 @@ public class IeeeRecommendedTests {
}
if (i > DoubleConsts.MIN_EXPONENT) {
double po2minus = FpUtils.nextAfter(po2,
double po2minus = Math.nextAfter(po2,
Double.NEGATIVE_INFINITY);
failures += testGetExponentCase(po2minus, i-1);
}
......@@ -312,7 +312,7 @@ public class IeeeRecommendedTests {
// Test largest value in next smaller binade
if (i >= 3) {// (i == 1) would test 0.0;
// (i == 2) would just retest MIN_VALUE
testGetExponentCase(FpUtils.nextAfter(top, 0.0),
testGetExponentCase(Math.nextAfter(top, 0.0),
DoubleConsts.MIN_EXPONENT - 1);
if( i >= 10) {
......@@ -1061,7 +1061,7 @@ public class IeeeRecommendedTests {
float value = someTestCases[i];
failures+=testScalbCase(value,
scaleFactor,
FpUtils.copySign( (scaleFactor>0?infinityF:0.0f), value) );
Math.copySign( (scaleFactor>0?infinityF:0.0f), value) );
}
}
}
......@@ -1095,7 +1095,7 @@ public class IeeeRecommendedTests {
failures+=testScalbCase(value,
scaleFactor,
(FpUtils.ilogb(value) +j > FloatConsts.MAX_EXPONENT ) ?
FpUtils.copySign(infinityF, value) : // overflow
Math.copySign(infinityF, value) : // overflow
// calculate right answer
twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) );
scale*=2.0f;
......@@ -1268,7 +1268,7 @@ public class IeeeRecommendedTests {
double value = someTestCases[i];
failures+=testScalbCase(value,
scaleFactor,
FpUtils.copySign( (scaleFactor>0?infinityD:0.0), value) );
Math.copySign( (scaleFactor>0?infinityD:0.0), value) );
}
}
}
......@@ -1302,7 +1302,7 @@ public class IeeeRecommendedTests {
failures+=testScalbCase(value,
scaleFactor,
(FpUtils.ilogb(value) +j > DoubleConsts.MAX_EXPONENT ) ?
FpUtils.copySign(infinityD, value) : // overflow
Math.copySign(infinityD, value) : // overflow
// calculate right answer
twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) );
scale*=2.0;
......@@ -1423,7 +1423,7 @@ public class IeeeRecommendedTests {
// Create power of two
float po2 = powerOfTwoF(i);
expected = FpUtils.scalb(1.0f, i - (FloatConsts.SIGNIFICAND_WIDTH-1));
expected = Math.scalb(1.0f, i - (FloatConsts.SIGNIFICAND_WIDTH-1));
failures += testUlpCase(po2, expected);
......@@ -1443,7 +1443,7 @@ public class IeeeRecommendedTests {
}
if (i > FloatConsts.MIN_EXPONENT) {
float po2minus = FpUtils.nextAfter(po2,
float po2minus = Math.nextAfter(po2,
Float.NEGATIVE_INFINITY);
failures += testUlpCase(po2minus, expected/2.0f);
}
......@@ -1470,7 +1470,7 @@ public class IeeeRecommendedTests {
// Test largest value in next smaller binade
if (i >= 3) {// (i == 1) would test 0.0;
// (i == 2) would just retest MIN_VALUE
testUlpCase(FpUtils.nextAfter(top, 0.0f),
testUlpCase(Math.nextAfter(top, 0.0f),
Float.MIN_VALUE);
if( i >= 10) {
......@@ -1528,7 +1528,7 @@ public class IeeeRecommendedTests {
// Create power of two
double po2 = powerOfTwoD(i);
expected = FpUtils.scalb(1.0, i - (DoubleConsts.SIGNIFICAND_WIDTH-1));
expected = Math.scalb(1.0, i - (DoubleConsts.SIGNIFICAND_WIDTH-1));
failures += testUlpCase(po2, expected);
......@@ -1548,7 +1548,7 @@ public class IeeeRecommendedTests {
}
if (i > DoubleConsts.MIN_EXPONENT) {
double po2minus = FpUtils.nextAfter(po2,
double po2minus = Math.nextAfter(po2,
Double.NEGATIVE_INFINITY);
failures += testUlpCase(po2minus, expected/2.0f);
}
......@@ -1575,7 +1575,7 @@ public class IeeeRecommendedTests {
// Test largest value in next smaller binade
if (i >= 3) {// (i == 1) would test 0.0;
// (i == 2) would just retest MIN_VALUE
testUlpCase(FpUtils.nextAfter(top, 0.0f),
testUlpCase(Math.nextAfter(top, 0.0f),
Double.MIN_VALUE);
if( i >= 10) {
......
......@@ -153,12 +153,12 @@ public class Log10Tests {
for(int i = 0; i < half; i++) {
if (i == 0) {
input[half] = 1.0;
up = FpUtils.nextUp(1.0);
up = Math.nextUp(1.0);
down = FpUtils.nextDown(1.0);
} else {
input[half + i] = up;
input[half - i] = down;
up = FpUtils.nextUp(up);
up = Math.nextUp(up);
down = FpUtils.nextDown(down);
}
}
......
......@@ -88,14 +88,14 @@ public class Log1pTests {
// For |x| < 2^-54 log1p(x) ~= x
for(int i = DoubleConsts.MIN_SUB_EXPONENT; i <= -54; i++) {
double d = FpUtils.scalb(2, i);
double d = Math.scalb(2, i);
failures += testLog1pCase(d, d);
failures += testLog1pCase(-d, -d);
}
// For x > 2^53 log1p(x) ~= log(x)
for(int i = 53; i <= DoubleConsts.MAX_EXPONENT; i++) {
double d = FpUtils.scalb(2, i);
double d = Math.scalb(2, i);
failures += testLog1pCaseWithUlpDiff(d, StrictMath.log(d), 2.001);
}
......@@ -105,7 +105,7 @@ public class Log1pTests {
for(int i = 0; i < 1000; i++) {
double d = rand.nextDouble();
d = FpUtils.scalb(d, -53 - FpUtils.ilogb(d));
d = Math.scalb(d, -53 - FpUtils.ilogb(d));
for(int j = -53; j <= 52; j++) {
failures += testLog1pCaseWithUlpDiff(d, hp15cLogp(d), 5);
......@@ -137,8 +137,8 @@ public class Log1pTests {
pcNeighbors[2] = pc;
pcNeighbors[1] = FpUtils.nextDown(pc);
pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
pcNeighbors[3] = FpUtils.nextUp(pc);
pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]);
pcNeighbors[3] = Math.nextUp(pc);
pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
for(int j = 0; j < pcNeighbors.length; j++) {
pcNeighborsLog1p[j] = Math.log1p(pcNeighbors[j]);
......
......@@ -48,7 +48,7 @@ public class Rint {
public static void main(String args[]) {
int failures = 0;
double twoToThe52 = FpUtils.scalb(1.0, 52); // 2^52
double twoToThe52 = Math.scalb(1.0, 52); // 2^52
double [][] testCases = {
{0.0, 0.0},
......@@ -60,16 +60,16 @@ public class Rint {
{FpUtils.nextDown(0.5), 0.0},
{ 0.5, 0.0},
{ FpUtils.nextUp(0.5), 1.0},
{ Math.nextUp(0.5), 1.0},
{0.7, 1.0},
{FpUtils.nextDown(1.0), 1.0},
{ 1.0, 1.0},
{ FpUtils.nextUp(1.0), 1.0},
{ Math.nextUp(1.0), 1.0},
{FpUtils.nextDown(1.5), 1.0},
{ 1.5, 2.0},
{ FpUtils.nextUp(1.5), 2.0},
{ Math.nextUp(1.5), 2.0},
{4.2, 4.0},
{4.5, 4.0},
......@@ -81,10 +81,10 @@ public class Rint {
{150000.75, 150001.0},
{300000.5, 300000.0},
{FpUtils.nextUp(300000.5), 300001.0},
{Math.nextUp(300000.5), 300001.0},
{FpUtils.nextDown(300000.75), 300001.0},
{300000.75, 300001.0},
{FpUtils.nextUp(300000.75), 300001.0},
{Math.nextUp(300000.75), 300001.0},
{300000.99, 300001.0},
{262144.75, 262145.0}, //(2^18 ) + 0.75
{499998.75, 499999.0},
......@@ -93,7 +93,7 @@ public class Rint {
{FpUtils.nextDown(twoToThe52), twoToThe52},
{twoToThe52, twoToThe52},
{FpUtils.nextUp(twoToThe52), FpUtils.nextUp(twoToThe52)},
{Math.nextUp(twoToThe52), Math.nextUp(twoToThe52)},
{Double.MAX_VALUE, Double.MAX_VALUE},
{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册
新手
引导
客服 返回
顶部