Improve javadoc for ctfAdapter in Tmf.Core
[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 /**
17 * Float definition
18 * @author Matthew Khouzam
19 *
20 */
21 public class FloatDefinition extends Definition {
22 // ------------------------------------------------------------------------
23 // Attributes
24 // ------------------------------------------------------------------------
25
26 private final FloatDeclaration declaration;
27 private double value;
28
29 // ------------------------------------------------------------------------
30 // Contructors
31 // ------------------------------------------------------------------------
32
33 /**
34 * Constructor
35 *
36 * @param declaration
37 * the parent declaration
38 * @param definitionScope
39 * the parent scope
40 * @param fieldName
41 * the field name
42 */
43 public FloatDefinition(FloatDeclaration declaration,
44 IDefinitionScope definitionScope, String fieldName) {
45 super(definitionScope, fieldName);
46 this.declaration = declaration;
47 }
48
49 // ------------------------------------------------------------------------
50 // Gettters/Setters/Predicates
51 // ------------------------------------------------------------------------
52
53 /**
54 * THe value of a float stored, fit into a double. This should be extended
55 * for exotic floats if this is necessary.
56 *
57 * @return the value of the float field fit into a double.
58 */
59 public double getValue() {
60 return value;
61 }
62
63 /**
64 * Sets the value of the float
65 *
66 * @param val
67 * the value of the float
68 */
69 public void setValue(double val) {
70 value = val;
71 }
72
73 @Override
74 public FloatDeclaration getDeclaration() {
75 return declaration;
76 }
77
78 // ------------------------------------------------------------------------
79 // Operations
80 // ------------------------------------------------------------------------
81
82 @Override
83 public void read(BitBuffer input) {
84 int exp = declaration.getExponent();
85 int mant = declaration.getMantissa();
86 if ((exp + mant) == 32) {
87 value = readRawFloat32(input, mant, exp);
88 } else if ((exp + mant) == 64) {
89 value = readRawFloat64(input, mant, exp);
90 } else {
91 value = Double.NaN;
92 }
93 }
94
95 private static double readRawFloat64(BitBuffer input, final int manBits,
96 final int expBits) {
97 long low = input.getInt(32, false);
98 low = low & 0x00000000FFFFFFFFL;
99 long high = input.getInt(32, false);
100 high = high & 0x00000000FFFFFFFFL;
101 long temp = (high << 32) | low;
102 return createFloat(temp, manBits - 1, expBits);
103 }
104
105 /**
106 * @param rawValue
107 * @param manBits
108 * @param expBits
109 */
110 private static double createFloat(long rawValue, final int manBits,
111 final int expBits) {
112 long manShift = 1L << (manBits);
113 long manMask = manShift - 1;
114 long expMask = (1L << expBits) - 1;
115
116 int exp = (int) ((rawValue >> (manBits)) & expMask) + 1;
117 long man = (rawValue & manMask);
118 double expPow = Math.pow(2.0, exp - (1 << (expBits - 1)));
119 double ret = man * 1.0f;
120 ret /= manShift;
121 ret += 1.0;
122 ret *= expPow;
123 return ret;
124 }
125
126 private static double readRawFloat32(BitBuffer input, final int manBits,
127 final int expBits) {
128 long temp = input.getInt(32, false);
129 return createFloat(temp, manBits - 1, expBits);
130 }
131
132 @Override
133 public String toString() {
134 return String.valueOf(value);
135 }
136 }
This page took 0.049626 seconds and 5 git commands to generate.