Clean up code.
[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.internal.ctf.core.event.io.BitBuffer;
15
16 public class FloatDefinition extends Definition {
17 // ------------------------------------------------------------------------
18 // Attributes
19 // ------------------------------------------------------------------------
20
21 private final FloatDeclaration declaration;
22 private double value;
23
24 // ------------------------------------------------------------------------
25 // Contructors
26 // ------------------------------------------------------------------------
27
28 public FloatDefinition(FloatDeclaration declaration,
29 IDefinitionScope definitionScope, String fieldName) {
30 super(definitionScope, fieldName);
31 this.declaration = declaration;
32 }
33
34 // ------------------------------------------------------------------------
35 // Gettters/Setters/Predicates
36 // ------------------------------------------------------------------------
37
38 public double getValue() {
39 return value;
40 }
41
42 public void setValue(double val) {
43 value = val;
44 }
45
46 public FloatDeclaration getDeclaration() {
47 return declaration;
48 }
49
50 // ------------------------------------------------------------------------
51 // Operations
52 // ------------------------------------------------------------------------
53
54 @Override
55 public void read(BitBuffer input) {
56 int exp = declaration.getExponent();
57 int mant = declaration.getMantissa();
58 if ((exp + mant) == 32) {
59 value = readRawFloat32(input, mant, exp);
60 } else if ((exp + mant) == 64) {
61 value = readRawFloat64(input, mant, exp);
62 } else {
63 value = Double.NaN;
64 }
65 }
66
67 private static double readRawFloat64(BitBuffer input, final int manBits,
68 final int expBits) {
69 long low = input.getInt(32, false);
70 low = low & 0x00000000FFFFFFFFL;
71 long high = input.getInt(32, false);
72 high = high & 0x00000000FFFFFFFFL;
73 long temp = (high << 32) | low;
74 return createFloat(temp, manBits - 1, expBits);
75 }
76
77 /**
78 * @param rawValue
79 * @param manBits
80 * @param expBits
81 */
82 private static double createFloat(long rawValue, final int manBits,
83 final int expBits) {
84 long manShift = 1L << (manBits);
85 long manMask = manShift - 1;
86 long expMask = (1L << expBits) - 1;
87
88 int exp = (int) ((rawValue >> (manBits)) & expMask) + 1;
89 long man = (rawValue & manMask);
90 double expPow = Math.pow(2.0, exp - (1 << (expBits - 1)));
91 double ret = man * 1.0f;
92 ret /= manShift;
93 ret += 1.0;
94 ret *= expPow;
95 return ret;
96 }
97
98 private static double readRawFloat32(BitBuffer input, final int manBits,
99 final int expBits) {
100 long temp = input.getInt(32, false);
101 return createFloat(temp, manBits - 1, expBits);
102 }
103
104 @Override
105 public String toString() {
106 return String.valueOf(value);
107 }
108 }
This page took 0.036995 seconds and 6 git commands to generate.