1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
14 import java
.nio
.ByteOrder
;
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
;
21 * A CTF float declaration.
23 * The declaration of a floating point basic data type.
26 * @author Matthew Khouzam
28 public final class FloatDeclaration
extends Declaration
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
34 private final int fMantissa
;
35 private final int fExponent
;
36 private final ByteOrder fByteOrder
;
37 private final long fAlignement
;
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
47 * The exponent size in bits
49 * The mantissa size in bits (+1 for sign) (see CTF spec)
53 * The alignment. Should be ≥ 1
55 public FloatDeclaration(int exponent
, int mantissa
, ByteOrder byteOrder
,
59 fByteOrder
= byteOrder
;
60 fAlignement
= Math
.max(alignment
, 1);
64 // ------------------------------------------------------------------------
65 // Getters/Setters/Predicates
66 // ------------------------------------------------------------------------
71 public int getMantissa() {
78 public int getExponent() {
83 * @return the byteOrder
85 public ByteOrder
getByteOrder() {
90 public long getAlignment() {
98 public int getMaximumSize() {
99 return fMantissa
+ fExponent
+ 1;
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
110 public FloatDefinition
createDefinition(IDefinitionScope definitionScope
,
111 String fieldName
, BitBuffer input
) throws CTFReaderException
{
113 double value
= read(input
);
114 return new FloatDefinition(this, definitionScope
, fieldName
, value
);
118 public String
toString() {
119 /* Only used for debugging */
120 return "[declaration] float[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$
123 private double read(BitBuffer input
) throws CTFReaderException
{
124 /* Offset the buffer position wrt the current alignment */
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
);
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
);
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
);
150 * Create a float from the raw value, Mathematicians beware.
153 * The raw value( up to 64 bits)
155 * number of bits in the mantissa
157 * number of bits in the exponent
159 private static double createFloat(long rawValue
, final int manBits
,
161 long manShift
= 1L << (manBits
);
162 long manMask
= manShift
- 1;
163 long expMask
= (1L << expBits
) - 1;
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
;