Methods Summary |
---|
private void | checkedAddExponent(long offset)
long result = exponent + offset;
int expSign = Long.signum(exponent);
if (expSign * Long.signum(offset) > 0 && expSign * Long.signum(result) < 0) {
exponent = expSign * Long.MAX_VALUE;
} else {
exponent = result;
}
|
private int | countBitsLength(long value)
int leadingZeros = Long.numberOfLeadingZeros(value);
return Long.SIZE - leadingZeros;
|
private void | discardTrailingBits(long num)
long mask = ~(-1L << num);
abandonedNumber += (mantissa & mask);
mantissa >>= num;
|
private void | fitMantissaInDesiredWidth(int desiredWidth)
int bitLength = countBitsLength(mantissa);
if (bitLength > desiredWidth) {
discardTrailingBits(bitLength - desiredWidth);
} else {
mantissa <<= (desiredWidth - bitLength);
}
|
private java.lang.String | getNormalizedSignificand(java.lang.String strIntegerPart, java.lang.String strDecimalPart)
String significand = strIntegerPart + strDecimalPart;
significand = significand.replaceFirst("^0+", ""); //$NON-NLS-1$//$NON-NLS-2$
if (significand.length() == 0) {
significand = "0"; //$NON-NLS-1$
}
return significand;
|
private int | getOffset(java.lang.String strIntegerPart, java.lang.String strDecimalPart)
strIntegerPart = strIntegerPart.replaceFirst("^0+", ""); //$NON-NLS-1$ //$NON-NLS-2$
//If the Interger part is a nonzero number.
if (strIntegerPart.length() != 0) {
String leadingNumber = strIntegerPart.substring(0, 1);
return (strIntegerPart.length() - 1) * 4 + countBitsLength(Long.parseLong(leadingNumber,HEX_RADIX)) - 1;
}
//If the Interger part is a zero number.
int i;
for (i = 0; i < strDecimalPart.length() && strDecimalPart.charAt(i) == '0"; i++);
if (i == strDecimalPart.length()) {
return 0;
}
String leadingNumber=strDecimalPart.substring(i,i + 1);
return (-i - 1) * 4 + countBitsLength(Long.parseLong(leadingNumber, HEX_RADIX)) - 1;
|
private static java.lang.String[] | getSegmentsFromHexString(java.lang.String hexString)
Matcher matcher = PATTERN.matcher(hexString);
if (!matcher.matches()) {
throw new NumberFormatException();
}
String[] hexSegments = new String[3];
hexSegments[0] = matcher.group(1);
hexSegments[1] = matcher.group(2);
hexSegments[2] = matcher.group(3);
return hexSegments;
|
private long | parse(java.lang.String hexString)
String[] hexSegments = getSegmentsFromHexString(hexString);
String signStr = hexSegments[0];
String significantStr = hexSegments[1];
String exponentStr = hexSegments[2];
parseHexSign(signStr);
parseExponent(exponentStr);
parseMantissa(significantStr);
sign <<= (MANTISSA_WIDTH + EXPONENT_WIDTH);
exponent <<= MANTISSA_WIDTH;
return sign | exponent | mantissa;
|
public static double | parseDouble(java.lang.String hexString)
HexStringParser parser = new HexStringParser(DOUBLE_EXPONENT_WIDTH,
DOUBLE_MANTISSA_WIDTH);
long result = parser.parse(hexString);
return Double.longBitsToDouble(result);
|
private void | parseExponent(java.lang.String exponentStr)
char leadingChar = exponentStr.charAt(0);
int expSign = (leadingChar == '-" ? -1 : 1);
if (!Character.isDigit(leadingChar)) {
exponentStr = exponentStr.substring(1);
}
try {
exponent = expSign * Long.parseLong(exponentStr);
checkedAddExponent(EXPONENT_BASE);
} catch (NumberFormatException e) {
exponent = expSign * Long.MAX_VALUE;
}
|
public static float | parseFloat(java.lang.String hexString)
HexStringParser parser = new HexStringParser(FLOAT_EXPONENT_WIDTH,
FLOAT_MANTISSA_WIDTH);
int result = (int) parser.parse(hexString);
return Float.intBitsToFloat(result);
|
private void | parseHexSign(java.lang.String signStr)
this.sign = signStr.equals("-") ? 1 : 0; //$NON-NLS-1$
|
private void | parseMantissa(java.lang.String significantStr)
String[] strings = significantStr.split("\\."); //$NON-NLS-1$
String strIntegerPart = strings[0];
String strDecimalPart = strings.length > 1 ? strings[1] : ""; //$NON-NLS-1$
String significand = getNormalizedSignificand(strIntegerPart,strDecimalPart);
if (significand.equals("0")) { //$NON-NLS-1$
setZero();
return;
}
int offset = getOffset(strIntegerPart, strDecimalPart);
checkedAddExponent(offset);
if (exponent >= MAX_EXPONENT) {
setInfinite();
return;
}
if (exponent <= MIN_EXPONENT) {
setZero();
return;
}
if (significand.length() > MAX_SIGNIFICANT_LENGTH) {
abandonedNumber = significand.substring(MAX_SIGNIFICANT_LENGTH);
significand = significand.substring(0, MAX_SIGNIFICANT_LENGTH);
}
mantissa = Long.parseLong(significand, HEX_RADIX);
if (exponent >= 1) {
processNormalNumber();
} else{
processSubNormalNumber();
}
|
private void | processNormalNumber()
int desiredWidth = MANTISSA_WIDTH + 2;
fitMantissaInDesiredWidth(desiredWidth);
round();
mantissa = mantissa & MANTISSA_MASK;
|
private void | processSubNormalNumber()
int desiredWidth = MANTISSA_WIDTH + 1;
desiredWidth += (int)exponent;//lends bit from mantissa to exponent
exponent = 0;
fitMantissaInDesiredWidth(desiredWidth);
round();
mantissa = mantissa & MANTISSA_MASK;
|
private void | round()
String result = abandonedNumber.replaceAll("0+", ""); //$NON-NLS-1$ //$NON-NLS-2$
boolean moreThanZero = (result.length() > 0 ? true : false);
int lastDiscardedBit = (int) (mantissa & 1L);
mantissa >>= 1;
int tailBitInMantissa = (int) (mantissa & 1L);
if (lastDiscardedBit == 1 && (moreThanZero || tailBitInMantissa == 1)) {
int oldLength = countBitsLength(mantissa);
mantissa += 1L;
int newLength = countBitsLength(mantissa);
//Rounds up to exponent when whole bits of mantissa are one-bits.
if (oldLength >= MANTISSA_WIDTH && newLength > oldLength) {
checkedAddExponent(1);
}
}
|
private void | setInfinite()
exponent = MAX_EXPONENT;
mantissa = 0;
|
private void | setZero()
exponent = 0;
mantissa = 0;
|