+
+ private double read(BitBuffer input) throws CTFReaderException {
+ /* Offset the buffer position wrt the current alignment */
+ alignRead(input);
+ final int exp = getExponent();
+ final int mant = getMantissa();
+ double value = Double.NaN;
+ if ((exp + mant) == 32) {
+ value = readRawFloat32(input, mant, exp);
+ } else if ((exp + mant) == 64) {
+ value = readRawFloat64(input, mant, exp);
+ }
+ return value;
+ }
+
+ private static double readRawFloat32(BitBuffer input, final int manBits,
+ final int expBits) throws CTFReaderException {
+ long temp = input.get(32, false);
+ return createFloat(temp, manBits - 1, expBits);
+ }
+
+ private static double readRawFloat64(BitBuffer input, final int manBits,
+ final int expBits) throws CTFReaderException {
+ long temp = input.get(64, false);
+ return createFloat(temp, manBits - 1, expBits);
+ }
+
+ /**
+ * Create a float from the raw value, Mathematicians beware.
+ *
+ * @param rawValue
+ * The raw value( up to 64 bits)
+ * @param manBits
+ * number of bits in the mantissa
+ * @param expBits
+ * number of bits in the exponent
+ */
+ private static double createFloat(long rawValue, final int manBits,
+ final int expBits) {
+ long manShift = 1L << (manBits);
+ long manMask = manShift - 1;
+ long expMask = (1L << expBits) - 1;
+
+ int exp = (int) ((rawValue >> (manBits)) & expMask) + 1;
+ long man = (rawValue & manMask);
+ final int offsetExponent = exp - (1 << (expBits - 1));
+ double expPow = Math.pow(2.0, offsetExponent);
+ double ret = man * 1.0f;
+ ret /= manShift;
+ ret += 1.0;
+ ret *= expPow;
+ return ret;
+ }