Add support for float fields in a string output.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / FloatDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
3 *
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
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.linuxtools.ctf.core.event.types;
13
14 import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
15
16 public class FloatDefinition extends Definition {
17 // ------------------------------------------------------------------------
18 // Attributes
19 // ------------------------------------------------------------------------
20
21 private final FloatDeclaration declaration;
22 private double value;
23
24 // ------------------------------------------------------------------------
25 // Contructors
26 // ------------------------------------------------------------------------
27
28 public FloatDefinition(FloatDeclaration declaration,
29 IDefinitionScope definitionScope, String fieldName) {
30 super(definitionScope, fieldName);
31 this.declaration = declaration;
32 }
33 // ------------------------------------------------------------------------
34 // Gettters/Setters/Predicates
35 // ------------------------------------------------------------------------
36
37 public double getValue() {
38 return value;
39 }
40
41 public void setValue(double val) {
42 value = val;
43 }
44
45 public FloatDeclaration getDeclaration() {
46 return declaration;
47 }
48
49 // ------------------------------------------------------------------------
50 // Operations
51 // ------------------------------------------------------------------------
52
53
54
55 @Override
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);
61 }
62 else if((exp + mant) == 64)
63 {
64 readRawFloat64(input, mant,exp);
65 }
66 else
67 {
68 value = Double.NaN;
69 }
70 }
71
72
73
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;
80
81
82 value = createFloat(temp, manBits, expBits);
83 }
84
85 /**
86 * @param rawValue
87 * @param manBits
88 * @param expBits
89 */
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;
94
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;
99 ret /= manShift;
100 ret += 1.0;
101 ret *= expPow;
102 return ret;
103 }
104
105 private void readRawFloat32(BitBuffer input, final int manBits,
106 final int expBits) {
107 long temp = input.getInt(32, false);
108
109 value = createFloat(temp, manBits, expBits);
110 }
111
112 @Override
113 public String toString() {
114 return String.valueOf(value);
115 }
116 }
This page took 0.031705 seconds and 5 git commands to generate.