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