X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=org.eclipse.linuxtools.ctf.core%2Fsrc%2Forg%2Feclipse%2Flinuxtools%2Fctf%2Fcore%2Fevent%2Ftypes%2FFloatDeclaration.java;h=18d0f9c9c884f382ed0685f4d6e9191cb3b8bcde;hb=a4fa4e3606484778f4ea4ba3e42944c4c78430b2;hp=71ec3208d6bf99f75718b94196942a25756d01bf;hpb=46965b5eb18197cf2c3374f9e26c546e4b4f7b90;p=deliverable%2Ftracecompass.git diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDeclaration.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDeclaration.java index 71ec3208d6..18d0f9c9c8 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDeclaration.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/types/FloatDeclaration.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others + * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others * * All rights reserved. This program and the accompanying materials are made * available under the terms of the Eclipse Public License v1.0 which @@ -13,6 +13,10 @@ package org.eclipse.linuxtools.ctf.core.event.types; import java.nio.ByteOrder; +import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; +import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; + /** * A CTF float declaration. * @@ -21,16 +25,16 @@ import java.nio.ByteOrder; * @version 1.0 * @author Matthew Khouzam */ -public class FloatDeclaration implements IDeclaration { +public final class FloatDeclaration extends Declaration { // ------------------------------------------------------------------------ // Attributes // ------------------------------------------------------------------------ - private final int mant; - private final int exp; - private final ByteOrder byteOrder; - private final long alignment; + private final int fMantissa; + private final int fExponent; + private final ByteOrder fByteOrder; + private final long fAlignement; // ------------------------------------------------------------------------ // Constructors @@ -46,55 +50,68 @@ public class FloatDeclaration implements IDeclaration { * @param byteOrder * The byte order * @param alignment - * The alignment. Should be >= 1 + * The alignment. Should be ≥ 1 */ public FloatDeclaration(int exponent, int mantissa, ByteOrder byteOrder, long alignment) { - mant = mantissa; - exp = exponent; - this.byteOrder = byteOrder; - this.alignment = Math.max(alignment, 1); + fMantissa = mantissa; + fExponent = exponent; + fByteOrder = byteOrder; + fAlignement = Math.max(alignment, 1); } // ------------------------------------------------------------------------ - // Gettters/Setters/Predicates + // Getters/Setters/Predicates // ------------------------------------------------------------------------ /** * @return the mant */ public int getMantissa() { - return mant; + return fMantissa; } /** * @return the exp */ public int getExponent() { - return exp; + return fExponent; } /** * @return the byteOrder */ public ByteOrder getByteOrder() { - return byteOrder; + return fByteOrder; } @Override public long getAlignment() { - return alignment; + return fAlignement; + } + + /** + * @since 3.0 + */ + @Override + public int getMaximumSize() { + return fMantissa + fExponent + 1; } // ------------------------------------------------------------------------ // Operations // ------------------------------------------------------------------------ + /** + * @since 3.0 + */ @Override public FloatDefinition createDefinition(IDefinitionScope definitionScope, - String fieldName) { - return new FloatDefinition(this, definitionScope, fieldName); + String fieldName, BitBuffer input) throws CTFReaderException { + alignRead(input); + double value = read(input); + return new FloatDefinition(this, definitionScope, fieldName, value); } @Override @@ -102,4 +119,57 @@ public class FloatDeclaration implements IDeclaration { /* Only used for debugging */ return "[declaration] float[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ } + + 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; + } }