[ctf] Fixes multiple coding style issues while reading ctf Types.
[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.ctf.core.event.io.BitBuffer;
15
16 /**
17 * A CTF float definition.
18 *
19 * The definition of a floating point basic data type. It will take the data
20 * from a trace and store it (and make it fit) as a double.
21 *
22 * @version 1.0
23 * @author Matthew Khouzam
24 * @author Simon Marchi
25 */
26 public class FloatDefinition extends Definition {
27 // ------------------------------------------------------------------------
28 // Attributes
29 // ------------------------------------------------------------------------
30
31 private final FloatDeclaration declaration;
32 private double value;
33
34 // ------------------------------------------------------------------------
35 // Constructors
36 // ------------------------------------------------------------------------
37
38 /**
39 * Constructor
40 *
41 * @param declaration
42 * the parent declaration
43 * @param definitionScope
44 * the parent scope
45 * @param fieldName
46 * the field name
47 */
48 public FloatDefinition(FloatDeclaration declaration,
49 IDefinitionScope definitionScope, String fieldName) {
50 super(definitionScope, fieldName);
51 this.declaration = declaration;
52 }
53
54 // ------------------------------------------------------------------------
55 // Getters/Setters/Predicates
56 // ------------------------------------------------------------------------
57
58 /**
59 * The value of a float stored, fit into a double. This should be extended
60 * for exotic floats if this is necessary.
61 *
62 * @return the value of the float field fit into a double.
63 */
64 public double getValue() {
65 return value;
66 }
67
68 /**
69 * Sets the value of the float
70 *
71 * @param val
72 * the value of the float
73 */
74 public void setValue(double val) {
75 value = val;
76 }
77
78 @Override
79 public FloatDeclaration getDeclaration() {
80 return declaration;
81 }
82
83 // ------------------------------------------------------------------------
84 // Operations
85 // ------------------------------------------------------------------------
86
87 @Override
88 public void read(BitBuffer input) {
89 /* Offset the buffer position wrt the current alignment */
90 alignRead(input, this.declaration);
91 final int exp = declaration.getExponent();
92 final int mant = declaration.getMantissa();
93
94 if ((exp + mant) == 32) {
95 value = readRawFloat32(input, mant, exp);
96 } else if ((exp + mant) == 64) {
97 value = readRawFloat64(input, mant, exp);
98 } else {
99 value = Double.NaN;
100 }
101 }
102
103 private static double readRawFloat64(BitBuffer input, final int manBits,
104 final int expBits) {
105 long low = input.getInt(32, false);
106 low = low & 0x00000000FFFFFFFFL;
107 long high = input.getInt(32, false);
108 high = high & 0x00000000FFFFFFFFL;
109 long temp = (high << 32) | low;
110 return createFloat(temp, manBits - 1, expBits);
111 }
112
113 /**
114 * @param rawValue
115 * @param manBits
116 * @param expBits
117 */
118 private static double createFloat(long rawValue, final int manBits,
119 final int expBits) {
120 long manShift = 1L << (manBits);
121 long manMask = manShift - 1;
122 long expMask = (1L << expBits) - 1;
123
124 int exp = (int) ((rawValue >> (manBits)) & expMask) + 1;
125 long man = (rawValue & manMask);
126 final int offsetExponent = exp - (1 << (expBits - 1));
127 double expPow = Math.pow(2.0, offsetExponent);
128 double ret = man * 1.0f;
129 ret /= manShift;
130 ret += 1.0;
131 ret *= expPow;
132 return ret;
133 }
134
135 private static double readRawFloat32(BitBuffer input, final int manBits,
136 final int expBits) {
137 long temp = input.getInt(32, false);
138 return createFloat(temp, manBits - 1, expBits);
139 }
140
141 @Override
142 public String toString() {
143 return String.valueOf(value);
144 }
145 }
This page took 0.033604 seconds and 5 git commands to generate.