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
;
16 public class FloatDefinition
extends Definition
{
17 // ------------------------------------------------------------------------
19 // ------------------------------------------------------------------------
21 private final FloatDeclaration declaration
;
24 // ------------------------------------------------------------------------
26 // ------------------------------------------------------------------------
28 public FloatDefinition(FloatDeclaration declaration
,
29 IDefinitionScope definitionScope
, String fieldName
) {
30 super(definitionScope
, fieldName
);
31 this.declaration
= declaration
;
33 // ------------------------------------------------------------------------
34 // Gettters/Setters/Predicates
35 // ------------------------------------------------------------------------
37 public double getValue() {
41 public void setValue(double val
) {
45 public FloatDeclaration
getDeclaration() {
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
56 public void read(BitBuffer input
) {
57 int exp
= declaration
.getExponent();
58 int mant
= declaration
.getMantissa();
59 if( (exp
+ mant
) == 32 ){
60 readRawFloat32(input
, mant
, exp
);
62 else if((exp
+ mant
) == 64)
64 readRawFloat64(input
, mant
,exp
);
74 private void readRawFloat64(BitBuffer input
, final int manBits
, final int expBits
) {
75 long low
= input
.getInt(32, false);
76 low
= low
& 0x00000000FFFFFFFFL
;
77 long high
= input
.getInt(32, false);
78 high
= high
& 0x00000000FFFFFFFFL
;
79 long temp
= (high
<< 32) | low
;
82 value
= createFloat(temp
, manBits
, expBits
);
90 private static double createFloat(long rawValue
, final int manBits
, final int expBits
) {
91 long manShift
= 1L << manBits
;
92 long manMask
= manShift
-1;
93 long expMask
= (1L << expBits
) -1;
95 int exp
=(int)( rawValue
>> manBits
);
96 long man
= (rawValue
& manMask
);
97 double expPow
= Math
.pow(2.0, exp
-(1 << (expBits
-1)));
98 double ret
= man
* 1.0f
;
105 private void readRawFloat32(BitBuffer input
, final int manBits
,
107 long temp
= input
.getInt(32, false);
109 value
= createFloat(temp
, manBits
, expBits
);
113 public String
toString() {
114 return String
.valueOf(value
);