18d0f9c9c884f382ed0685f4d6e9191cb3b8bcde
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / FloatDeclaration.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 java.nio.ByteOrder;
15
16 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
17 import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
18 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
19
20 /**
21 * A CTF float declaration.
22 *
23 * The declaration of a floating point basic data type.
24 *
25 * @version 1.0
26 * @author Matthew Khouzam
27 */
28 public final class FloatDeclaration extends Declaration {
29
30 // ------------------------------------------------------------------------
31 // Attributes
32 // ------------------------------------------------------------------------
33
34 private final int fMantissa;
35 private final int fExponent;
36 private final ByteOrder fByteOrder;
37 private final long fAlignement;
38
39 // ------------------------------------------------------------------------
40 // Constructors
41 // ------------------------------------------------------------------------
42
43 /**
44 * Constructor
45 *
46 * @param exponent
47 * The exponent size in bits
48 * @param mantissa
49 * The mantissa size in bits (+1 for sign) (see CTF spec)
50 * @param byteOrder
51 * The byte order
52 * @param alignment
53 * The alignment. Should be ≥ 1
54 */
55 public FloatDeclaration(int exponent, int mantissa, ByteOrder byteOrder,
56 long alignment) {
57 fMantissa = mantissa;
58 fExponent = exponent;
59 fByteOrder = byteOrder;
60 fAlignement = Math.max(alignment, 1);
61
62 }
63
64 // ------------------------------------------------------------------------
65 // Getters/Setters/Predicates
66 // ------------------------------------------------------------------------
67
68 /**
69 * @return the mant
70 */
71 public int getMantissa() {
72 return fMantissa;
73 }
74
75 /**
76 * @return the exp
77 */
78 public int getExponent() {
79 return fExponent;
80 }
81
82 /**
83 * @return the byteOrder
84 */
85 public ByteOrder getByteOrder() {
86 return fByteOrder;
87 }
88
89 @Override
90 public long getAlignment() {
91 return fAlignement;
92 }
93
94 /**
95 * @since 3.0
96 */
97 @Override
98 public int getMaximumSize() {
99 return fMantissa + fExponent + 1;
100 }
101
102 // ------------------------------------------------------------------------
103 // Operations
104 // ------------------------------------------------------------------------
105
106 /**
107 * @since 3.0
108 */
109 @Override
110 public FloatDefinition createDefinition(IDefinitionScope definitionScope,
111 String fieldName, BitBuffer input) throws CTFReaderException {
112 alignRead(input);
113 double value = read(input);
114 return new FloatDefinition(this, definitionScope, fieldName, value);
115 }
116
117 @Override
118 public String toString() {
119 /* Only used for debugging */
120 return "[declaration] float[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
121 }
122
123 private double read(BitBuffer input) throws CTFReaderException {
124 /* Offset the buffer position wrt the current alignment */
125 alignRead(input);
126 final int exp = getExponent();
127 final int mant = getMantissa();
128 double value = Double.NaN;
129 if ((exp + mant) == 32) {
130 value = readRawFloat32(input, mant, exp);
131 } else if ((exp + mant) == 64) {
132 value = readRawFloat64(input, mant, exp);
133 }
134 return value;
135 }
136
137 private static double readRawFloat32(BitBuffer input, final int manBits,
138 final int expBits) throws CTFReaderException {
139 long temp = input.get(32, false);
140 return createFloat(temp, manBits - 1, expBits);
141 }
142
143 private static double readRawFloat64(BitBuffer input, final int manBits,
144 final int expBits) throws CTFReaderException {
145 long temp = input.get(64, false);
146 return createFloat(temp, manBits - 1, expBits);
147 }
148
149 /**
150 * Create a float from the raw value, Mathematicians beware.
151 *
152 * @param rawValue
153 * The raw value( up to 64 bits)
154 * @param manBits
155 * number of bits in the mantissa
156 * @param expBits
157 * number of bits in the exponent
158 */
159 private static double createFloat(long rawValue, final int manBits,
160 final int expBits) {
161 long manShift = 1L << (manBits);
162 long manMask = manShift - 1;
163 long expMask = (1L << expBits) - 1;
164
165 int exp = (int) ((rawValue >> (manBits)) & expMask) + 1;
166 long man = (rawValue & manMask);
167 final int offsetExponent = exp - (1 << (expBits - 1));
168 double expPow = Math.pow(2.0, offsetExponent);
169 double ret = man * 1.0f;
170 ret /= manShift;
171 ret += 1.0;
172 ret *= expPow;
173 return ret;
174 }
175 }
This page took 0.034887 seconds and 5 git commands to generate.