blob: 7492dd3c93a37a8593ae536922c43bbbfb4788ec [file] [log] [blame]
Matt Spinlerd7abf362018-01-18 12:40:02 -06001/**
2 * Copyright © 2017 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matt Spinlere710d182018-01-18 13:06:17 -060016#include <math.h>
Matt Spinlerd7abf362018-01-18 12:40:02 -060017#include "record_manager.hpp"
18
19namespace witherspoon
20{
21namespace power
22{
23namespace history
24{
25
Matt Spinlere710d182018-01-18 13:06:17 -060026int64_t RecordManager::linearToInteger(uint16_t data)
27{
28 //The exponent is the first 5 bits, followed by 11 bits of mantissa.
29 int8_t exponent = (data & 0xF800) >> 11;
30 int16_t mantissa = (data & 0x07FF);
31
32 //If exponent's MSB on, then it's negative.
33 //Convert from two's complement.
34 if (exponent & 0x10)
35 {
36 exponent = (~exponent) & 0x1F;
37 exponent = (exponent + 1) * -1;
38 }
39
40 //If mantissa's MSB on, then it's negative.
41 //Convert from two's complement.
42 if (mantissa & 0x400)
43 {
44 mantissa = (~mantissa) & 0x07FF;
45 mantissa = (mantissa + 1) * -1;
46 }
47
48 auto value = static_cast<float>(mantissa) * pow(2, exponent);
49 return value;
50}
Matt Spinlerd7abf362018-01-18 12:40:02 -060051
52}
53}
54}