1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
14 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
15 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
18 * A CTF float definition.
20 * The definition of a floating point basic data type. It will take the data
21 * from a trace and store it (and make it fit) as a double.
24 * @author Matthew Khouzam
25 * @author Simon Marchi
27 public class FloatDefinition
extends Definition
{
28 // ------------------------------------------------------------------------
30 // ------------------------------------------------------------------------
32 private final FloatDeclaration declaration
;
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
43 * the parent declaration
44 * @param definitionScope
49 public FloatDefinition(FloatDeclaration declaration
,
50 IDefinitionScope definitionScope
, String fieldName
) {
51 super(definitionScope
, fieldName
);
52 this.declaration
= declaration
;
55 // ------------------------------------------------------------------------
56 // Getters/Setters/Predicates
57 // ------------------------------------------------------------------------
60 * The value of a float stored, fit into a double. This should be extended
61 * for exotic floats if this is necessary.
63 * @return the value of the float field fit into a double.
65 public double getValue() {
70 * Sets the value of the float
73 * the value of the float
75 public void setValue(double val
) {
80 public FloatDeclaration
getDeclaration() {
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
89 public void read(BitBuffer input
) throws CTFReaderException
{
90 /* Offset the buffer position wrt the current alignment */
91 alignRead(input
, this.declaration
);
92 final int exp
= declaration
.getExponent();
93 final int mant
= declaration
.getMantissa();
95 if ((exp
+ mant
) == 32) {
96 value
= readRawFloat32(input
, mant
, exp
);
97 } else if ((exp
+ mant
) == 64) {
98 value
= readRawFloat64(input
, mant
, exp
);
104 private static double readRawFloat64(BitBuffer input
, final int manBits
,
105 final int expBits
) throws CTFReaderException
{
106 long temp
= input
.get(64, false);
107 return createFloat(temp
, manBits
- 1, expBits
);
115 private static double createFloat(long rawValue
, final int manBits
,
117 long manShift
= 1L << (manBits
);
118 long manMask
= manShift
- 1;
119 long expMask
= (1L << expBits
) - 1;
121 int exp
= (int) ((rawValue
>> (manBits
)) & expMask
) + 1;
122 long man
= (rawValue
& manMask
);
123 final int offsetExponent
= exp
- (1 << (expBits
- 1));
124 double expPow
= Math
.pow(2.0, offsetExponent
);
125 double ret
= man
* 1.0f
;
132 private static double readRawFloat32(BitBuffer input
, final int manBits
,
133 final int expBits
) throws CTFReaderException
{
134 long temp
= input
.get(32, false);
135 return createFloat(temp
, manBits
- 1, expBits
);
139 public String
toString() {
140 return String
.valueOf(value
);