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 2306 * 2307 * Number Round(x) 2308 * x0.00 x0. 2309 * x0.01 x0. 2332 * float value. There are two cases to consider: 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 jdk.testlibrary.DoubleUtils; 27 import jdk.testlibrary.FloatUtils; 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 > Double.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 <= Double.MAX_EXPONENT && // (Usually) normal result 2225 exponent >= Double.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)DoubleUtils.EXP_BIAS) << 2240 (DoubleUtils.SIGNIFICAND_WIDTH-1)) 2241 & DoubleUtils.EXP_BIT_MASK) | 2242 (DoubleUtils.SIGNIF_BIT_MASK & significand); 2243 2244 } else { // Subnormal or zero 2245 // (exponent < Double.MIN_EXPONENT) 2246 2247 if (exponent < (DoubleUtils.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 - DoubleUtils.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)(Double.MIN_EXPONENT -1) + // subnorm exp. 2286 (long)DoubleUtils.EXP_BIAS) << 2287 (DoubleUtils.SIGNIFICAND_WIDTH-1)) 2288 & DoubleUtils.EXP_BIT_MASK) | 2289 (DoubleUtils.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 2306 * 2307 * Number Round(x) 2308 * x0.00 x0. 2309 * x0.01 x0. 2332 * float value. There are two cases to consider: 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 >= FloatUtils.MIN_SUB_EXPONENT-1) && 2353 (exponent <= Float.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 |