Matt Spinler | d7abf36 | 2018-01-18 12:40:02 -0600 | [diff] [blame] | 1 | /** |
| 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 Spinler | e710d18 | 2018-01-18 13:06:17 -0600 | [diff] [blame^] | 16 | #include <math.h> |
Matt Spinler | d7abf36 | 2018-01-18 12:40:02 -0600 | [diff] [blame] | 17 | #include "record_manager.hpp" |
| 18 | |
| 19 | namespace witherspoon |
| 20 | { |
| 21 | namespace power |
| 22 | { |
| 23 | namespace history |
| 24 | { |
| 25 | |
Matt Spinler | e710d18 | 2018-01-18 13:06:17 -0600 | [diff] [blame^] | 26 | int64_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 Spinler | d7abf36 | 2018-01-18 12:40:02 -0600 | [diff] [blame] | 51 | |
| 52 | } |
| 53 | } |
| 54 | } |