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
;
34 // ------------------------------------------------------------------------
35 // Gettters/Setters/Predicates
36 // ------------------------------------------------------------------------
38 public double getValue() {
42 public void setValue(double val
) {
46 public FloatDeclaration
getDeclaration() {
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
55 public void read(BitBuffer input
) {
56 int exp
= declaration
.getExponent();
57 int mant
= declaration
.getMantissa();
58 if ((exp
+ mant
) == 32) {
59 value
= readRawFloat32(input
, mant
, exp
);
60 } else if ((exp
+ mant
) == 64) {
61 value
= readRawFloat64(input
, mant
, exp
);
67 private static double readRawFloat64(BitBuffer input
, final int manBits
,
69 long low
= input
.getInt(32, false);
70 low
= low
& 0x00000000FFFFFFFFL
;
71 long high
= input
.getInt(32, false);
72 high
= high
& 0x00000000FFFFFFFFL
;
73 long temp
= (high
<< 32) | low
;
74 return createFloat(temp
, manBits
- 1, expBits
);
82 private static double createFloat(long rawValue
, final int manBits
,
84 long manShift
= 1L << (manBits
);
85 long manMask
= manShift
- 1;
86 long expMask
= (1L << expBits
) - 1;
88 int exp
= (int) ((rawValue
>> (manBits
)) & expMask
) + 1;
89 long man
= (rawValue
& manMask
);
90 double expPow
= Math
.pow(2.0, exp
- (1 << (expBits
- 1)));
91 double ret
= man
* 1.0f
;
98 private static double readRawFloat32(BitBuffer input
, final int manBits
,
100 long temp
= input
.getInt(32, false);
101 return createFloat(temp
, manBits
- 1, expBits
);
105 public String
toString() {
106 return String
.valueOf(value
);