一、前言
关于ecj源码包中的org.eclipse.jdt.internal.compiler.util.FloatUtil浮点数工具类,对字符数组进行浮点数float、双精度double类型转换等。
二、源码说明
package org.eclipse.jdt.internal.compiler.util;@b@@b@public class FloatUtil@b@{@b@ private static final int DOUBLE_FRACTION_WIDTH = 52;@b@ private static final int DOUBLE_PRECISION = 53;@b@ private static final int MAX_DOUBLE_EXPONENT = 1023;@b@ private static final int MIN_NORMALIZED_DOUBLE_EXPONENT = -1022;@b@ private static final int MIN_UNNORMALIZED_DOUBLE_EXPONENT = -1075;@b@ private static final int DOUBLE_EXPONENT_BIAS = 1023;@b@ private static final int DOUBLE_EXPONENT_SHIFT = 52;@b@ private static final int SINGLE_FRACTION_WIDTH = 23;@b@ private static final int SINGLE_PRECISION = 24;@b@ private static final int MAX_SINGLE_EXPONENT = 127;@b@ private static final int MIN_NORMALIZED_SINGLE_EXPONENT = -126;@b@ private static final int MIN_UNNORMALIZED_SINGLE_EXPONENT = -150;@b@ private static final int SINGLE_EXPONENT_BIAS = 127;@b@ private static final int SINGLE_EXPONENT_SHIFT = 23;@b@@b@ public static float valueOfHexFloatLiteral(char[] source)@b@ {@b@ long bits = convertHexFloatingPointLiteralToBits(source);@b@ return Float.intBitsToFloat((int)bits);@b@ }@b@@b@ public static double valueOfHexDoubleLiteral(char[] source)@b@ {@b@ long bits = convertHexFloatingPointLiteralToBits(source);@b@ return Double.longBitsToDouble(bits);@b@ }@b@@b@ private static long convertHexFloatingPointLiteralToBits(char[] source)@b@ {@b@ long fraction;@b@ int extraBits;@b@ long lowBit;@b@ long biasedExponent;@b@ int length = source.length;@b@ long mantissa = 0L;@b@@b@ int next = 0;@b@ char nextChar = source[next];@b@ nextChar = source[next];@b@ if (nextChar == '0')@b@ ++next;@b@ else@b@ throw new NumberFormatException();@b@@b@ nextChar = source[next];@b@ if ((nextChar == 'X') || (nextChar == 'x'))@b@ ++next;@b@ else {@b@ throw new NumberFormatException();@b@ }@b@@b@ int binaryPointPosition = -1;@b@ while (true) {@b@ while (true) { nextChar = source[next];@b@ switch (nextChar) {@b@ case '0':@b@ ++next;@b@ case '.':@b@ case '/': } }@b@ binaryPointPosition = next;@b@ ++next;@b@ }@b@@b@ int mantissaBits = 0;@b@ int leadingDigitPosition = -1;@b@ while (true) { int hexdigit;@b@ while (true) { nextChar = source[next];@b@@b@ switch (nextChar) { case '0':@b@ case '1':@b@ case '2':@b@ case '3':@b@ case '4':@b@ case '5':@b@ case '6':@b@ case '7':@b@ case '8':@b@ case '9':@b@ hexdigit = nextChar - '0';@b@ break;@b@ case 'a':@b@ case 'b':@b@ case 'c':@b@ case 'd':@b@ case 'e':@b@ case 'f':@b@ hexdigit = nextChar - 'a' + 10;@b@ break;@b@ case 'A':@b@ case 'B':@b@ case 'C':@b@ case 'D':@b@ case 'E':@b@ case 'F':@b@ hexdigit = nextChar - 'A' + 10;@b@ break;@b@ case '.':@b@ binaryPointPosition = next;@b@ ++next;@b@ case '/':@b@ case ':':@b@ case ';':@b@ case '<':@b@ case '=':@b@ case '>':@b@ case '?':@b@ case '@':@b@ case 'G':@b@ case 'H':@b@ case 'I':@b@ case 'J':@b@ case 'K':@b@ case 'L':@b@ case 'M':@b@ case 'N':@b@ case 'O':@b@ case 'P':@b@ case 'Q':@b@ case 'R':@b@ case 'S':@b@ case 'T':@b@ case 'U':@b@ case 'V':@b@ case 'W':@b@ case 'X':@b@ case 'Y':@b@ case 'Z':@b@ case '[':@b@ case '\\':@b@ case ']':@b@ case '^':@b@ case '_':@b@ case ''': } } if (binaryPointPosition >= 0)@b@ break;@b@ binaryPointPosition = next;@b@@b@ break;@b@@b@ if (mantissaBits == 0)@b@ {@b@ leadingDigitPosition = next;@b@ mantissa = hexdigit;@b@ mantissaBits = 4;@b@ } else if (mantissaBits < 60)@b@ {@b@ mantissa <<= 4;@b@ mantissa |= hexdigit;@b@ mantissaBits += 4;@b@ }@b@@b@ ++next;@b@ }@b@@b@ nextChar = source[next];@b@ if ((nextChar == 'P') || (nextChar == 'p'))@b@ ++next;@b@ else {@b@ throw new NumberFormatException();@b@ }@b@@b@ int exponent = 0;@b@ int exponentSign = 1;@b@ while (next < length) {@b@ nextChar = source[next];@b@ switch (nextChar)@b@ {@b@ case '+':@b@ exponentSign = 1;@b@ ++next;@b@ break;@b@ case '-':@b@ exponentSign = -1;@b@ ++next;@b@ break;@b@ case '0':@b@ case '1':@b@ case '2':@b@ case '3':@b@ case '4':@b@ case '5':@b@ case '6':@b@ case '7':@b@ case '8':@b@ case '9':@b@ int digit = nextChar - '0';@b@ exponent = exponent * 10 + digit;@b@ ++next;@b@ break;@b@ case ',':@b@ case '.':@b@ case '/':@b@ default:@b@ break;@b@ }@b@@b@ }@b@@b@ boolean doublePrecision = true;@b@ if (next < length) {@b@ nextChar = source[next];@b@ switch (nextChar)@b@ {@b@ case 'F':@b@ case 'f':@b@ doublePrecision = false;@b@ ++next;@b@ break;@b@ case 'D':@b@ case 'd':@b@ doublePrecision = true;@b@ ++next;@b@ break;@b@ default:@b@ throw new NumberFormatException();@b@ }@b@@b@ }@b@@b@ if (mantissa == 0L) {@b@ return 0L;@b@ }@b@@b@ int scaleFactorCompensation = 0;@b@ long top = mantissa >>> mantissaBits - 4;@b@ if ((top & 0x8) == 0L) {@b@ --mantissaBits;@b@ ++scaleFactorCompensation;@b@ if ((top & 0x4) == 0L) {@b@ --mantissaBits;@b@ ++scaleFactorCompensation;@b@ if ((top & 0x2) == 0L) {@b@ --mantissaBits;@b@ ++scaleFactorCompensation;@b@ }@b@ }@b@@b@ }@b@@b@ long result = 0L;@b@ if (doublePrecision)@b@ {@b@ if (mantissaBits > 53)@b@ {@b@ extraBits = mantissaBits - 53;@b@@b@ fraction = mantissa >>> extraBits - 1;@b@ lowBit = fraction & 1L;@b@ fraction += lowBit;@b@ fraction >>>= 1;@b@ if ((fraction & 0x0) != 0L) {@b@ fraction >>>= 1;@b@ --scaleFactorCompensation;@b@ }@b@ }@b@ else {@b@ fraction = mantissa << 53 - mantissaBits;@b@ }@b@@b@ scaleFactor = 0;@b@ if (mantissaBits > 0)@b@ if (leadingDigitPosition < binaryPointPosition)@b@ {@b@ scaleFactor = 4 * (binaryPointPosition - leadingDigitPosition);@b@@b@ scaleFactor -= scaleFactorCompensation;@b@ }@b@ else {@b@ scaleFactor = -4 * @b@ (leadingDigitPosition - binaryPointPosition - 1);@b@@b@ scaleFactor -= scaleFactorCompensation;@b@ }@b@@b@@b@ e = exponentSign * exponent + scaleFactor;@b@ if (e - 1 > 1023)@b@ {@b@ result = Double.doubleToLongBits((1.0D / 0.0D));@b@ } else if (e - 1 >= -1022)@b@ {@b@ biasedExponent = e - 1 + 1023;@b@ result = fraction & 0xFFFFFFFF;@b@ result |= biasedExponent << 52;@b@ } else if (e - 1 > -1075)@b@ {@b@ biasedExponent = 0L;@b@ result = fraction >>> -1022 - e + 1;@b@ result |= biasedExponent << 52;@b@ }@b@ else {@b@ result = Double.doubleToLongBits((0.0D / 0.0D));@b@ }@b@ return result;@b@ }@b@@b@ if (mantissaBits > 24)@b@ {@b@ scaleFactor = mantissaBits - 24;@b@@b@ fraction = mantissa >>> scaleFactor - 1;@b@ e = fraction & 1L;@b@ fraction += e;@b@ fraction >>>= 1;@b@ if ((fraction & 0x1000000) != 0L) {@b@ fraction >>>= 1;@b@ --scaleFactorCompensation;@b@ }@b@ }@b@ else {@b@ fraction = mantissa << 24 - mantissaBits;@b@ }@b@@b@ int scaleFactor = 0;@b@ if (mantissaBits > 0)@b@ if (leadingDigitPosition < binaryPointPosition)@b@ {@b@ scaleFactor = 4 * (binaryPointPosition - leadingDigitPosition);@b@@b@ scaleFactor -= scaleFactorCompensation;@b@ }@b@ else {@b@ scaleFactor = -4 * @b@ (leadingDigitPosition - binaryPointPosition - 1);@b@@b@ scaleFactor -= scaleFactorCompensation;@b@ }@b@@b@@b@ int e = exponentSign * exponent + scaleFactor;@b@ if (e - 1 > 127)@b@ {@b@ result = Float.floatToIntBits((1.0F / 1.0F));@b@ } else if (e - 1 >= -126)@b@ {@b@ biasedExponent = e - 1 + 127;@b@ result = fraction & 0xFF7FFFFF;@b@ result |= biasedExponent << 23;@b@ } else if (e - 1 > -150)@b@ {@b@ biasedExponent = 0L;@b@ result = fraction >>> -126 - e + 1;@b@ result |= biasedExponent << 23;@b@ }@b@ else {@b@ result = Float.floatToIntBits((0.0F / 0.0F));@b@ }@b@ return result;@b@ }@b@}