OpenJDK 11: Merging in jdk.internal.math

This is part of merging upstream changes from OpenJDK 11.28. This updates
classes listed below:

 * DoubleConsts.java
 * FDBigInteger.java
 * FloatConsts.java
 * FloatingDecimal.java
 * FormattedFloatingDecimal.java

Bug: 199067710
Test: m droid
Change-Id: Ic3d0b750ad9e922f51c0c399e23b5a1be5932c44
diff --git a/ojluni/src/main/java/jdk/internal/math/DoubleConsts.java b/ojluni/src/main/java/jdk/internal/math/DoubleConsts.java
index d952a1f..57e835b 100644
--- a/ojluni/src/main/java/jdk/internal/math/DoubleConsts.java
+++ b/ojluni/src/main/java/jdk/internal/math/DoubleConsts.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2016, 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
@@ -27,7 +27,7 @@
 
 /**
  * This class contains additional constants documenting limits of the
- * <code>double</code> type.
+ * {@code double} type.
  *
  * @author Joseph D. Darcy
  */
@@ -57,7 +57,7 @@
 
     /**
      * The number of logical bits in the significand of a
-     * <code>double</code> number, including the implicit bit.
+     * {@code double} number, including the implicit bit.
      */
     public static final int SIGNIFICAND_WIDTH   = 53;
 
@@ -76,33 +76,31 @@
     public static final int     MIN_EXPONENT    = -1022;
 
     /**
-     * The exponent the smallest positive <code>double</code>
-     * subnormal value would have if it could be normalized.  It is
-     * equal to the value returned by
-     * <code>FpUtils.ilogb(Double.MIN_VALUE)</code>.
+     * The exponent the smallest positive {@code double}
+     * subnormal value would have if it could be normalized..
      */
-    public static final int     MIN_SUB_EXPONENT = MIN_EXPONENT -
+    public static final int     MIN_SUB_EXPONENT = Double.MIN_EXPONENT -
                                                    (SIGNIFICAND_WIDTH - 1);
 
     /**
-     * Bias used in representing a <code>double</code> exponent.
+     * Bias used in representing a {@code double} exponent.
      */
     public static final int     EXP_BIAS        = 1023;
 
     /**
-     * Bit mask to isolate the sign bit of a <code>double</code>.
+     * Bit mask to isolate the sign bit of a {@code double}.
      */
     public static final long    SIGN_BIT_MASK   = 0x8000000000000000L;
 
     /**
      * Bit mask to isolate the exponent field of a
-     * <code>double</code>.
+     * {@code double}.
      */
     public static final long    EXP_BIT_MASK    = 0x7FF0000000000000L;
 
     /**
      * Bit mask to isolate the significand field of a
-     * <code>double</code>.
+     * {@code double}.
      */
     public static final long    SIGNIF_BIT_MASK = 0x000FFFFFFFFFFFFFL;
 
diff --git a/ojluni/src/main/java/jdk/internal/math/FDBigInteger.java b/ojluni/src/main/java/jdk/internal/math/FDBigInteger.java
index ea68529..ba3ce9b 100644
--- a/ojluni/src/main/java/jdk/internal/math/FDBigInteger.java
+++ b/ojluni/src/main/java/jdk/internal/math/FDBigInteger.java
@@ -144,7 +144,7 @@
     }
 
     // Constant for casting an int to a long via bitwise AND.
-    private final static long LONG_MASK = 0xffffffffL;
+    private static final long LONG_MASK = 0xffffffffL;
 
     //@ spec_public non_null;
     private int data[];  // value: data[0] is least significant
@@ -384,8 +384,8 @@
      * Retrieves the normalization bias of the <code>FDBigIntger</code>. The
      * normalization bias is a left shift such that after it the highest word
      * of the value will have the 4 highest bits equal to zero:
-     * <code>(highestWord & 0xf0000000) == 0</code>, but the next bit should be 1
-     * <code>(highestWord & 0x08000000) != 0</code>.
+     * {@code (highestWord & 0xf0000000) == 0}, but the next bit should be 1
+     * {@code (highestWord & 0x08000000) != 0}.
      *
      * @return The normalization bias.
      */
@@ -546,9 +546,9 @@
      * We assume that S has been normalized, as above, and that
      * "this" has been left-shifted accordingly.
      * Also assumed, of course, is that the result, q, can be expressed
-     * as an integer, 0 <= q < 10.
+     * as an integer, {@code 0 <= q < 10}.
      *
-     * @param The divisor of this <code>FDBigInteger</code>.
+     * @param S The divisor of this <code>FDBigInteger</code>.
      * @return <code>q = (int)(this / S)</code>.
      */
     /*@
@@ -685,7 +685,7 @@
      *
      * @param p5 The exponent of the power-of-five factor.
      * @param p2 The exponent of the power-of-two factor.
-     * @return
+     * @return The multiplication result.
      */
     /*@
      @ requires this.value() == 0 || p5 == 0 && p2 == 0;
@@ -931,11 +931,11 @@
     /**
      * Compares the parameter with this <code>FDBigInteger</code>. Returns an
      * integer accordingly as:
-     * <pre>
-     * >0: this > other
-     *  0: this == other
-     * <0: this < other
-     * </pre>
+     * <pre>{@code
+     * > 0: this > other
+     *   0: this == other
+     * < 0: this < other
+     * }</pre>
      *
      * @param other The <code>FDBigInteger</code> to compare.
      * @return A negative value, zero, or a positive value according to the
@@ -974,11 +974,11 @@
      * Compares this <code>FDBigInteger</code> with
      * <code>5<sup>p5</sup> * 2<sup>p2</sup></code>.
      * Returns an integer accordingly as:
-     * <pre>
-     * >0: this > other
-     *  0: this == other
-     * <0: this < other
-     * </pre>
+     * <pre>{@code
+     * > 0: this > other
+     *   0: this == other
+     * < 0: this < other
+     * }</pre>
      * @param p5 The exponent of the power-of-five factor.
      * @param p2 The exponent of the power-of-two factor.
      * @return A negative value, zero, or a positive value according to the
@@ -1011,11 +1011,11 @@
     /**
      * Compares this <code>FDBigInteger</code> with <code>x + y</code>. Returns a
      * value according to the comparison as:
-     * <pre>
+     * <pre>{@code
      * -1: this <  x + y
      *  0: this == x + y
      *  1: this >  x + y
-     * </pre>
+     * }</pre>
      * @param x The first addend of the sum to compare.
      * @param y The second addend of the sum to compare.
      * @return -1, 0, or 1 according to the result of the comparison.
diff --git a/ojluni/src/main/java/jdk/internal/math/FloatConsts.java b/ojluni/src/main/java/jdk/internal/math/FloatConsts.java
index d2873aa..73e0408 100644
--- a/ojluni/src/main/java/jdk/internal/math/FloatConsts.java
+++ b/ojluni/src/main/java/jdk/internal/math/FloatConsts.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2016, 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
@@ -27,7 +27,7 @@
 
 /**
  * This class contains additional constants documenting limits of the
- * <code>float</code> type.
+ * {@code float} type.
  *
  * @author Joseph D. Darcy
  */
@@ -53,7 +53,7 @@
 
     /**
      * The number of logical bits in the significand of a
-     * <code>float</code> number, including the implicit bit.
+     * {@code float} number, including the implicit bit.
      */
     public static final int SIGNIFICAND_WIDTH   = 24;
 
@@ -72,32 +72,31 @@
     public static final int     MIN_EXPONENT    = -126;
 
     /**
-     * The exponent the smallest positive <code>float</code> subnormal
-     * value would have if it could be normalized.  It is equal to the
-     * value returned by <code>FpUtils.ilogb(Float.MIN_VALUE)</code>.
+     * The exponent the smallest positive {@code float} subnormal
+     * value would have if it could be normalized.
      */
-    public static final int     MIN_SUB_EXPONENT = MIN_EXPONENT -
+    public static final int     MIN_SUB_EXPONENT = Float.MIN_EXPONENT -
                                                    (SIGNIFICAND_WIDTH - 1);
 
     /**
-     * Bias used in representing a <code>float</code> exponent.
+     * Bias used in representing a {@code float} exponent.
      */
     public static final int     EXP_BIAS        = 127;
 
     /**
-     * Bit mask to isolate the sign bit of a <code>float</code>.
+     * Bit mask to isolate the sign bit of a {@code float}.
      */
     public static final int     SIGN_BIT_MASK   = 0x80000000;
 
     /**
      * Bit mask to isolate the exponent field of a
-     * <code>float</code>.
+     * {@code float}.
      */
     public static final int     EXP_BIT_MASK    = 0x7F800000;
 
     /**
      * Bit mask to isolate the significand field of a
-     * <code>float</code>.
+     * {@code float}.
      */
     public static final int     SIGNIF_BIT_MASK = 0x007FFFFF;
 
diff --git a/ojluni/src/main/java/jdk/internal/math/FloatingDecimal.java b/ojluni/src/main/java/jdk/internal/math/FloatingDecimal.java
index 9a4f919..2f4ade0 100644
--- a/ojluni/src/main/java/jdk/internal/math/FloatingDecimal.java
+++ b/ojluni/src/main/java/jdk/internal/math/FloatingDecimal.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2016, 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
@@ -154,7 +154,7 @@
 
         /**
          * Indicates the sign of the value.
-         * @return <code>value < 0.0</code>.
+         * @return {@code value < 0.0}.
          */
         public boolean isNegative();
 
@@ -187,7 +187,7 @@
      * and infinite values.
      */
     private static class ExceptionalBinaryToASCIIBuffer implements BinaryToASCIIConverter {
-        final private String image;
+        private final String image;
         private boolean isNegative;
 
         public ExceptionalBinaryToASCIIBuffer(String image, boolean isNegative) {
@@ -1003,8 +1003,8 @@
      * A <code>ASCIIToBinaryConverter</code> container for a <code>double</code>.
      */
     static class PreparedASCIIToBinaryBuffer implements ASCIIToBinaryConverter {
-        final private double doubleVal;
-        final private float floatVal;
+        private final double doubleVal;
+        private final float floatVal;
 
         public PreparedASCIIToBinaryBuffer(double doubleVal, float floatVal) {
             this.doubleVal = doubleVal;
@@ -1992,21 +1992,32 @@
                         break expLoop; // stop parsing exponent.
                     }
                 }
-                int expLimit = BIG_DECIMAL_EXPONENT+nDigits+nTrailZero;
-                if ( expOverflow || ( expVal > expLimit ) ){
-                    //
-                    // The intent here is to end up with
-                    // infinity or zero, as appropriate.
-                    // The reason for yielding such a small decExponent,
-                    // rather than something intuitive such as
-                    // expSign*Integer.MAX_VALUE, is that this value
-                    // is subject to further manipulation in
-                    // doubleValue() and floatValue(), and I don't want
-                    // it to be able to cause overflow there!
-                    // (The only way we can get into trouble here is for
-                    // really outrageous nDigits+nTrailZero, such as 2 billion. )
-                    //
-                    decExp = expSign*expLimit;
+                int expLimit = BIG_DECIMAL_EXPONENT + nDigits + nTrailZero;
+                if (expOverflow || (expVal > expLimit)) {
+                    // There is still a chance that the exponent will be safe to
+                    // use: if it would eventually decrease due to a negative
+                    // decExp, and that number is below the limit.  We check for
+                    // that here.
+                    if (!expOverflow && (expSign == 1 && decExp < 0)
+                            && (expVal + decExp) < expLimit) {
+                        // Cannot overflow: adding a positive and negative number.
+                        decExp += expVal;
+                    } else {
+                        //
+                        // The intent here is to end up with
+                        // infinity or zero, as appropriate.
+                        // The reason for yielding such a small decExponent,
+                        // rather than something intuitive such as
+                        // expSign*Integer.MAX_VALUE, is that this value
+                        // is subject to further manipulation in
+                        // doubleValue() and floatValue(), and I don't want
+                        // it to be able to cause overflow there!
+                        // (The only way we can get into trouble here is for
+                        // really outrageous nDigits+nTrailZero, such as 2
+                        // billion.)
+                        //
+                        decExp = expSign * expLimit;
+                    }
                 } else {
                     // this should not overflow, since we tested
                     // for expVal > (MAX+N), where N >= abs(decExp)
@@ -2366,8 +2377,8 @@
 
                 // Float calculations
                 int floatBits = isNegative ? FloatConsts.SIGN_BIT_MASK : 0;
-                if (exponent >= FloatConsts.MIN_EXPONENT) {
-                    if (exponent > FloatConsts.MAX_EXPONENT) {
+                if (exponent >= Float.MIN_EXPONENT) {
+                    if (exponent > Float.MAX_EXPONENT) {
                         // Float.POSITIVE_INFINITY
                         floatBits |= FloatConsts.EXP_BIT_MASK;
                     } else {
@@ -2398,12 +2409,12 @@
                 float fValue = Float.intBitsToFloat(floatBits);
 
                 // Check for overflow and update exponent accordingly.
-                if (exponent > DoubleConsts.MAX_EXPONENT) {         // Infinite result
+                if (exponent > Double.MAX_EXPONENT) {         // Infinite result
                     // overflow to properly signed infinity
                     return isNegative ? A2BC_NEGATIVE_INFINITY : A2BC_POSITIVE_INFINITY;
                 } else {  // Finite return value
-                    if (exponent <= DoubleConsts.MAX_EXPONENT && // (Usually) normal result
-                            exponent >= DoubleConsts.MIN_EXPONENT) {
+                    if (exponent <= Double.MAX_EXPONENT && // (Usually) normal result
+                            exponent >= Double.MIN_EXPONENT) {
 
                         // The result returned in this block cannot be a
                         // zero or subnormal; however after the
@@ -2423,7 +2434,7 @@
                                 (DoubleConsts.SIGNIF_BIT_MASK & significand);
 
                     } else {  // Subnormal or zero
-                        // (exponent < DoubleConsts.MIN_EXPONENT)
+                        // (exponent < Double.MIN_EXPONENT)
 
                         if (exponent < (DoubleConsts.MIN_SUB_EXPONENT - 1)) {
                             // No way to round back to nonzero value
@@ -2463,7 +2474,7 @@
                             // Now, discard the bits
                             significand = significand >> bitsDiscarded;
 
-                            significand = ((((long) (DoubleConsts.MIN_EXPONENT - 1) + // subnorm exp.
+                            significand = ((((long) (Double.MIN_EXPONENT - 1) + // subnorm exp.
                                     (long) DoubleConsts.EXP_BIAS) <<
                                     (DoubleConsts.SIGNIFICAND_WIDTH - 1))
                                     & DoubleConsts.EXP_BIT_MASK) |
diff --git a/ojluni/src/main/java/jdk/internal/math/FormattedFloatingDecimal.java b/ojluni/src/main/java/jdk/internal/math/FormattedFloatingDecimal.java
index 0a560dc..88cd1fe 100644
--- a/ojluni/src/main/java/jdk/internal/math/FormattedFloatingDecimal.java
+++ b/ojluni/src/main/java/jdk/internal/math/FormattedFloatingDecimal.java
@@ -106,10 +106,28 @@
         return decExponentRounded - 1;
     }
 
+    /**
+     * Returns the mantissa as a {@code char[]}.  Note that the returned value
+     * is a reference to the internal {@code char[]} containing the mantissa,
+     * therefore code invoking this method should not pass the return value to
+     * external code but should in that case make a copy.
+     *
+     * @return a reference to the internal {@code char[]} representing the
+     *         mantissa.
+     */
     public char[] getMantissa(){
         return mantissa;
     }
 
+    /**
+     * Returns the exponent as a {@code char[]}.  Note that the returned value
+     * is a reference to the internal {@code char[]} containing the exponent,
+     * therefore code invoking this method should not pass the return value to
+     * external code but should in that case make a copy.
+     *
+     * @return a reference to the internal {@code char[]} representing the
+     *         exponent.
+     */
     public char[] getExponent(){
         return exponent;
     }