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
.internal
.ctf
.core
.event
.io
.BitBuffer
;
18 * @author Matthew Khouzam
21 public class FloatDefinition
extends Definition
{
22 // ------------------------------------------------------------------------
24 // ------------------------------------------------------------------------
26 private final FloatDeclaration declaration
;
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
37 * the parent declaration
38 * @param definitionScope
43 public FloatDefinition(FloatDeclaration declaration
,
44 IDefinitionScope definitionScope
, String fieldName
) {
45 super(definitionScope
, fieldName
);
46 this.declaration
= declaration
;
49 // ------------------------------------------------------------------------
50 // Gettters/Setters/Predicates
51 // ------------------------------------------------------------------------
54 * THe value of a float stored, fit into a double. This should be extended
55 * for exotic floats if this is necessary.
57 * @return the value of the float field fit into a double.
59 public double getValue() {
64 * Sets the value of the float
67 * the value of the float
69 public void setValue(double val
) {
74 public FloatDeclaration
getDeclaration() {
78 // ------------------------------------------------------------------------
80 // ------------------------------------------------------------------------
83 public void read(BitBuffer input
) {
84 int exp
= declaration
.getExponent();
85 int mant
= declaration
.getMantissa();
86 if ((exp
+ mant
) == 32) {
87 value
= readRawFloat32(input
, mant
, exp
);
88 } else if ((exp
+ mant
) == 64) {
89 value
= readRawFloat64(input
, mant
, exp
);
95 private static double readRawFloat64(BitBuffer input
, final int manBits
,
97 long low
= input
.getInt(32, false);
98 low
= low
& 0x00000000FFFFFFFFL
;
99 long high
= input
.getInt(32, false);
100 high
= high
& 0x00000000FFFFFFFFL
;
101 long temp
= (high
<< 32) | low
;
102 return createFloat(temp
, manBits
- 1, expBits
);
110 private static double createFloat(long rawValue
, final int manBits
,
112 long manShift
= 1L << (manBits
);
113 long manMask
= manShift
- 1;
114 long expMask
= (1L << expBits
) - 1;
116 int exp
= (int) ((rawValue
>> (manBits
)) & expMask
) + 1;
117 long man
= (rawValue
& manMask
);
118 double expPow
= Math
.pow(2.0, exp
- (1 << (expBits
- 1)));
119 double ret
= man
* 1.0f
;
126 private static double readRawFloat32(BitBuffer input
, final int manBits
,
128 long temp
= input
.getInt(32, false);
129 return createFloat(temp
, manBits
- 1, expBits
);
133 public String
toString() {
134 return String
.valueOf(value
);