6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 //package sun.misc;
25
26 import sun.misc.DoubleConsts;
27 import sun.misc.FloatConsts;
28 import java.util.regex.*;
29
30 public class OldFloatingDecimalForTest{
31 boolean isExceptional;
32 boolean isNegative;
33 int decExponent;
34 char digits[];
35 int nDigits;
36 int bigIntExp;
37 int bigIntNBits;
38 boolean mustSetRoundDir = false;
39 boolean fromHex = false;
40 int roundDir = 0; // set by doubleValue
41
42 /*
43 * The fields below provides additional information about the result of
44 * the binary to decimal digits conversion done in dtoa() and roundup()
45 * methods. They are changed if needed by those two methods.
46 */
47
2200
2201 // Round is set; sticky might be set.
2202
2203 // For the sticky bit, it suffices to check the
2204 // current digit and test for any nonzero digits in
2205 // the remaining unprocessed input.
2206 i++;
2207 while(i < signifLength && !sticky) {
2208 currentDigit = getHexDigit(significandString,i);
2209 sticky = sticky || (currentDigit != 0);
2210 i++;
2211 }
2212
2213 }
2214 // else all of string was seen, round and sticky are
2215 // correct as false.
2216
2217
2218 // Check for overflow and update exponent accordingly.
2219
2220 if (exponent > DoubleConsts.MAX_EXPONENT) { // Infinite result
2221 // overflow to properly signed infinity
2222 return new OldFloatingDecimalForTest(sign * Double.POSITIVE_INFINITY);
2223 } else { // Finite return value
2224 if (exponent <= DoubleConsts.MAX_EXPONENT && // (Usually) normal result
2225 exponent >= DoubleConsts.MIN_EXPONENT) {
2226
2227 // The result returned in this block cannot be a
2228 // zero or subnormal; however after the
2229 // significand is adjusted from rounding, we could
2230 // still overflow in infinity.
2231
2232 // AND exponent bits into significand; if the
2233 // significand is incremented and overflows from
2234 // rounding, this combination will update the
2235 // exponent correctly, even in the case of
2236 // Double.MAX_VALUE overflowing to infinity.
2237
2238 significand = (( (exponent +
2239 (long)DoubleConsts.EXP_BIAS) <<
2240 (DoubleConsts.SIGNIFICAND_WIDTH-1))
2241 & DoubleConsts.EXP_BIT_MASK) |
2242 (DoubleConsts.SIGNIF_BIT_MASK & significand);
2243
2244 } else { // Subnormal or zero
2245 // (exponent < DoubleConsts.MIN_EXPONENT)
2246
2247 if (exponent < (DoubleConsts.MIN_SUB_EXPONENT -1 )) {
2248 // No way to round back to nonzero value
2249 // regardless of significand if the exponent is
2250 // less than -1075.
2251 return new OldFloatingDecimalForTest(sign * 0.0);
2252 } else { // -1075 <= exponent <= MIN_EXPONENT -1 = -1023
2253 /*
2254 * Find bit position to round to; recompute
2255 * round and sticky bits, and shift
2256 * significand right appropriately.
2257 */
2258
2259 sticky = sticky || round;
2260 round = false;
2261
2262 // Number of bits of significand to preserve is
2263 // exponent - abs_min_exp +1
2264 // check:
2265 // -1075 +1074 + 1 = 0
2266 // -1023 +1074 + 1 = 52
2267
2268 int bitsDiscarded = 53 -
2269 ((int)exponent - DoubleConsts.MIN_SUB_EXPONENT + 1);
2270 assert bitsDiscarded >= 1 && bitsDiscarded <= 53;
2271
2272 // What to do here:
2273 // First, isolate the new round bit
2274 round = (significand & (1L << (bitsDiscarded -1))) != 0L;
2275 if (bitsDiscarded > 1) {
2276 // create mask to update sticky bits; low
2277 // order bitsDiscarded bits should be 1
2278 long mask = ~((~0L) << (bitsDiscarded -1));
2279 sticky = sticky || ((significand & mask) != 0L ) ;
2280 }
2281
2282 // Now, discard the bits
2283 significand = significand >> bitsDiscarded;
2284
2285 significand = (( ((long)(DoubleConsts.MIN_EXPONENT -1) + // subnorm exp.
2286 (long)DoubleConsts.EXP_BIAS) <<
2287 (DoubleConsts.SIGNIFICAND_WIDTH-1))
2288 & DoubleConsts.EXP_BIT_MASK) |
2289 (DoubleConsts.SIGNIF_BIT_MASK & significand);
2290 }
2291 }
2292
2293 // The significand variable now contains the currently
2294 // appropriate exponent bits too.
2295
2296 /*
2297 * Determine if significand should be incremented;
2298 * making this determination depends on the least
2299 * significant bit and the round and sticky bits.
2300 *
2301 * Round to nearest even rounding table, adapted from
2302 * table 4.7 in "Computer Arithmetic" by IsraelKoren.
2303 * The digit to the left of the "decimal" point is the
2304 * least significant bit, the digits to the right of
2305 * the point are the round and sticky bits
2333 *
2334 * 1. rounding to double discards sticky bit
2335 * information that would change the result of a float
2336 * rounding (near halfway case between two floats)
2337 *
2338 * 2. rounding to double rounds up when rounding up
2339 * would not occur when rounding to float.
2340 *
2341 * For former case only needs to be considered when
2342 * the bits rounded away when casting to float are all
2343 * zero; otherwise, float round bit is properly set
2344 * and sticky will already be true.
2345 *
2346 * The lower exponent bound for the code below is the
2347 * minimum (normalized) subnormal exponent - 1 since a
2348 * value with that exponent can round up to the
2349 * minimum subnormal value and the sticky bit
2350 * information must be preserved (i.e. case 1).
2351 */
2352 if ((exponent >= FloatConsts.MIN_SUB_EXPONENT-1) &&
2353 (exponent <= FloatConsts.MAX_EXPONENT ) ){
2354 // Outside above exponent range, the float value
2355 // will be zero or infinity.
2356
2357 /*
2358 * If the low-order 28 bits of a rounded double
2359 * significand are 0, the double could be a
2360 * half-way case for a rounding to float. If the
2361 * double value is a half-way case, the double
2362 * significand may have to be modified to round
2363 * the the right float value (see the stickyRound
2364 * method). If the rounding to double has lost
2365 * what would be float sticky bit information, the
2366 * double significand must be incremented. If the
2367 * double value's significand was itself
2368 * incremented, the float value may end up too
2369 * large so the increment should be undone.
2370 */
2371 if ((significand & 0xfffffffL) == 0x0L) {
2372 // For negative values, the sign of the
2373 // roundDir is the same as for positive values
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 //package sun.misc;
25
26 import java.util.regex.*;
27
28 public class OldFloatingDecimalForTest{
29 boolean isExceptional;
30 boolean isNegative;
31 int decExponent;
32 char digits[];
33 int nDigits;
34 int bigIntExp;
35 int bigIntNBits;
36 boolean mustSetRoundDir = false;
37 boolean fromHex = false;
38 int roundDir = 0; // set by doubleValue
39
40 /*
41 * The fields below provides additional information about the result of
42 * the binary to decimal digits conversion done in dtoa() and roundup()
43 * methods. They are changed if needed by those two methods.
44 */
45
2198
2199 // Round is set; sticky might be set.
2200
2201 // For the sticky bit, it suffices to check the
2202 // current digit and test for any nonzero digits in
2203 // the remaining unprocessed input.
2204 i++;
2205 while(i < signifLength && !sticky) {
2206 currentDigit = getHexDigit(significandString,i);
2207 sticky = sticky || (currentDigit != 0);
2208 i++;
2209 }
2210
2211 }
2212 // else all of string was seen, round and sticky are
2213 // correct as false.
2214
2215
2216 // Check for overflow and update exponent accordingly.
2217
2218 if (exponent > Double.MAX_EXPONENT) { // Infinite result
2219 // overflow to properly signed infinity
2220 return new OldFloatingDecimalForTest(sign * Double.POSITIVE_INFINITY);
2221 } else { // Finite return value
2222 if (exponent <= Double.MAX_EXPONENT && // (Usually) normal result
2223 exponent >= Double.MIN_EXPONENT) {
2224
2225 // The result returned in this block cannot be a
2226 // zero or subnormal; however after the
2227 // significand is adjusted from rounding, we could
2228 // still overflow in infinity.
2229
2230 // AND exponent bits into significand; if the
2231 // significand is incremented and overflows from
2232 // rounding, this combination will update the
2233 // exponent correctly, even in the case of
2234 // Double.MAX_VALUE overflowing to infinity.
2235
2236 significand = (( (exponent +
2237 (long)DoubleConsts.EXP_BIAS) <<
2238 (DoubleConsts.SIGNIFICAND_WIDTH-1))
2239 & DoubleConsts.EXP_BIT_MASK) |
2240 (DoubleConsts.SIGNIF_BIT_MASK & significand);
2241
2242 } else { // Subnormal or zero
2243 // (exponent < Double.MIN_EXPONENT)
2244
2245 if (exponent < (DoubleConsts.MIN_SUB_EXPONENT -1 )) {
2246 // No way to round back to nonzero value
2247 // regardless of significand if the exponent is
2248 // less than -1075.
2249 return new OldFloatingDecimalForTest(sign * 0.0);
2250 } else { // -1075 <= exponent <= MIN_EXPONENT -1 = -1023
2251 /*
2252 * Find bit position to round to; recompute
2253 * round and sticky bits, and shift
2254 * significand right appropriately.
2255 */
2256
2257 sticky = sticky || round;
2258 round = false;
2259
2260 // Number of bits of significand to preserve is
2261 // exponent - abs_min_exp +1
2262 // check:
2263 // -1075 +1074 + 1 = 0
2264 // -1023 +1074 + 1 = 52
2265
2266 int bitsDiscarded = 53 -
2267 ((int)exponent - DoubleConsts.MIN_SUB_EXPONENT + 1);
2268 assert bitsDiscarded >= 1 && bitsDiscarded <= 53;
2269
2270 // What to do here:
2271 // First, isolate the new round bit
2272 round = (significand & (1L << (bitsDiscarded -1))) != 0L;
2273 if (bitsDiscarded > 1) {
2274 // create mask to update sticky bits; low
2275 // order bitsDiscarded bits should be 1
2276 long mask = ~((~0L) << (bitsDiscarded -1));
2277 sticky = sticky || ((significand & mask) != 0L ) ;
2278 }
2279
2280 // Now, discard the bits
2281 significand = significand >> bitsDiscarded;
2282
2283 significand = (( ((long)(Double.MIN_EXPONENT -1) + // subnorm exp.
2284 (long)DoubleConsts.EXP_BIAS) <<
2285 (DoubleConsts.SIGNIFICAND_WIDTH-1))
2286 & DoubleConsts.EXP_BIT_MASK) |
2287 (DoubleConsts.SIGNIF_BIT_MASK & significand);
2288 }
2289 }
2290
2291 // The significand variable now contains the currently
2292 // appropriate exponent bits too.
2293
2294 /*
2295 * Determine if significand should be incremented;
2296 * making this determination depends on the least
2297 * significant bit and the round and sticky bits.
2298 *
2299 * Round to nearest even rounding table, adapted from
2300 * table 4.7 in "Computer Arithmetic" by IsraelKoren.
2301 * The digit to the left of the "decimal" point is the
2302 * least significant bit, the digits to the right of
2303 * the point are the round and sticky bits
2331 *
2332 * 1. rounding to double discards sticky bit
2333 * information that would change the result of a float
2334 * rounding (near halfway case between two floats)
2335 *
2336 * 2. rounding to double rounds up when rounding up
2337 * would not occur when rounding to float.
2338 *
2339 * For former case only needs to be considered when
2340 * the bits rounded away when casting to float are all
2341 * zero; otherwise, float round bit is properly set
2342 * and sticky will already be true.
2343 *
2344 * The lower exponent bound for the code below is the
2345 * minimum (normalized) subnormal exponent - 1 since a
2346 * value with that exponent can round up to the
2347 * minimum subnormal value and the sticky bit
2348 * information must be preserved (i.e. case 1).
2349 */
2350 if ((exponent >= FloatConsts.MIN_SUB_EXPONENT-1) &&
2351 (exponent <= Float.MAX_EXPONENT ) ){
2352 // Outside above exponent range, the float value
2353 // will be zero or infinity.
2354
2355 /*
2356 * If the low-order 28 bits of a rounded double
2357 * significand are 0, the double could be a
2358 * half-way case for a rounding to float. If the
2359 * double value is a half-way case, the double
2360 * significand may have to be modified to round
2361 * the the right float value (see the stickyRound
2362 * method). If the rounding to double has lost
2363 * what would be float sticky bit information, the
2364 * double significand must be incremented. If the
2365 * double value's significand was itself
2366 * incremented, the float value may end up too
2367 * large so the increment should be undone.
2368 */
2369 if ((significand & 0xfffffffL) == 0x0L) {
2370 // For negative values, the sign of the
2371 // roundDir is the same as for positive values
|