Fix bug when tracefile is not aligned. Now supports exotic architectures.
[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 value = readRawFloat32(input, mant , exp);
61 }
62 else if((exp + mant) == 64)
63 {
64 value = readRawFloat64(input, mant,exp);
65 }
66 else
67 {
68 value = Double.NaN;
69 }
70 }
71
72
73
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);
81 }
82
83 /**
84 * @param rawValue
85 * @param manBits
86 * @param expBits
87 */
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;
92
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;
97 ret /= manShift;
98 ret += 1.0;
99 ret *= expPow;
100 return ret;
101 }
102
103 private static double readRawFloat32(BitBuffer input, final int manBits,
104 final int expBits) {
105 long temp = input.getInt(32, false);
106 return createFloat(temp, manBits-1, expBits);
107 }
108
109 @Override
110 public String toString() {
111 return String.valueOf(value);
112 }
113 }
This page took 0.03332 seconds and 5 git commands to generate.