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