Add function to convert from linear format

The average and maximum power values are returned from the
power supply in Watts specified in linear format.  The values
must be converted from linear format to be useful.  The value
will be converted to an integer, keeping the units as Watts.

Linear format is a 16 bit value made up of 5 bits of exponent
followed by 11 bits of mantissa, where the value is:
   X = mantissa * 2^exponent

These values are specified as two's complement, and technically
may be negative, though there isn't really a reason for a power
supply to specify negative power values.  The code will support
them anyway for completeness, and to be helpful to future code
that may need to for sure.

Change-Id: I2cde529ed4355e173c8a3ecff2865f2c350ad61e
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/power-supply/record_manager.cpp b/power-supply/record_manager.cpp
index bfc2f17..7492dd3 100644
--- a/power-supply/record_manager.cpp
+++ b/power-supply/record_manager.cpp
@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include <math.h>
 #include "record_manager.hpp"
 
 namespace witherspoon
@@ -22,6 +23,31 @@
 namespace history
 {
 
+int64_t RecordManager::linearToInteger(uint16_t data)
+{
+    //The exponent is the first 5 bits, followed by 11 bits of mantissa.
+    int8_t exponent = (data & 0xF800) >> 11;
+    int16_t mantissa = (data & 0x07FF);
+
+    //If exponent's MSB on, then it's negative.
+    //Convert from two's complement.
+    if (exponent & 0x10)
+    {
+        exponent = (~exponent) & 0x1F;
+        exponent = (exponent + 1) * -1;
+    }
+
+    //If mantissa's MSB on, then it's negative.
+    //Convert from two's complement.
+    if (mantissa & 0x400)
+    {
+        mantissa = (~mantissa) & 0x07FF;
+        mantissa = (mantissa + 1) * -1;
+    }
+
+    auto value = static_cast<float>(mantissa) * pow(2, exponent);
+    return value;
+}
 
 }
 }
diff --git a/power-supply/record_manager.hpp b/power-supply/record_manager.hpp
index fe35db0..09b55f9 100644
--- a/power-supply/record_manager.hpp
+++ b/power-supply/record_manager.hpp
@@ -73,6 +73,19 @@
         }
 
         /**
+         * @brief Converts a Linear Format power number to an integer
+         *
+         * The PMBus spec describes a 2 byte Linear Format
+         * number that is composed of an exponent and mantissa
+         * in two's complement notation.
+         *
+         * Value = Mantissa * 2**Exponent
+         *
+         * @return int64_t the converted value
+         */
+        static int64_t linearToInteger(uint16_t data);
+
+        /**
          * @brief Returns the number of records
          *
          * @return size_t - the number of records