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 value
= readRawFloat32(input
, mant
, exp
);
62 else if((exp
+ mant
) == 64)
64 value
= readRawFloat64(input
, mant
,exp
);
74 private static double 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
;
80 return createFloat(temp
, manBits
-1, expBits
);
88 private static double createFloat(long rawValue
, final int manBits
, final int expBits
) {
89 long manShift
= 1L << (manBits
);
90 long manMask
= manShift
- 1;
91 long expMask
= (1L << expBits
) - 1;
93 int exp
= (int) ((rawValue
>> (manBits
))&expMask
)+1;
94 long man
= (rawValue
& manMask
);
95 double expPow
= Math
.pow(2.0, exp
- (1 << (expBits
- 1)));
96 double ret
= man
* 1.0f
;
103 private static double readRawFloat32(BitBuffer input
, final int manBits
,
105 long temp
= input
.getInt(32, false);
106 return createFloat(temp
, manBits
-1, expBits
);
110 public String
toString() {
111 return String
.valueOf(value
);