1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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
;
17 * A CTF float definition.
19 * The definition of a floating point basic data type. It will take the data
20 * from a trace and store it (and make it fit) as a double.
23 * @author Matthew Khouzam
24 * @author Simon Marchi
26 public class FloatDefinition
extends Definition
{
27 // ------------------------------------------------------------------------
29 // ------------------------------------------------------------------------
31 private final FloatDeclaration declaration
;
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
42 * the parent declaration
43 * @param definitionScope
48 public FloatDefinition(FloatDeclaration declaration
,
49 IDefinitionScope definitionScope
, String fieldName
) {
50 super(definitionScope
, fieldName
);
51 this.declaration
= declaration
;
54 // ------------------------------------------------------------------------
55 // Getters/Setters/Predicates
56 // ------------------------------------------------------------------------
59 * The value of a float stored, fit into a double. This should be extended
60 * for exotic floats if this is necessary.
62 * @return the value of the float field fit into a double.
64 public double getValue() {
69 * Sets the value of the float
72 * the value of the float
74 public void setValue(double val
) {
79 public FloatDeclaration
getDeclaration() {
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
88 public void read(BitBuffer input
) {
89 /* Offset the buffer position wrt the current alignment */
90 alignRead(input
, this.declaration
);
91 final int exp
= declaration
.getExponent();
92 final int mant
= declaration
.getMantissa();
94 if ((exp
+ mant
) == 32) {
95 value
= readRawFloat32(input
, mant
, exp
);
96 } else if ((exp
+ mant
) == 64) {
97 value
= readRawFloat64(input
, mant
, exp
);
103 private static double readRawFloat64(BitBuffer input
, final int manBits
,
105 long low
= input
.getInt(32, false);
106 low
= low
& 0x00000000FFFFFFFFL
;
107 long high
= input
.getInt(32, false);
108 high
= high
& 0x00000000FFFFFFFFL
;
109 long temp
= (high
<< 32) | low
;
110 return createFloat(temp
, manBits
- 1, expBits
);
118 private static double createFloat(long rawValue
, final int manBits
,
120 long manShift
= 1L << (manBits
);
121 long manMask
= manShift
- 1;
122 long expMask
= (1L << expBits
) - 1;
124 int exp
= (int) ((rawValue
>> (manBits
)) & expMask
) + 1;
125 long man
= (rawValue
& manMask
);
126 final int offsetExponent
= exp
- (1 << (expBits
- 1));
127 double expPow
= Math
.pow(2.0, offsetExponent
);
128 double ret
= man
* 1.0f
;
135 private static double readRawFloat32(BitBuffer input
, final int manBits
,
137 long temp
= input
.getInt(32, false);
138 return createFloat(temp
, manBits
- 1, expBits
);
142 public String
toString() {
143 return String
.valueOf(value
);