Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / inference_engine / precision_utils.cpp
index 9988693..b1d43ec 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2018 Intel Corporation
+// Copyright (C) 2018-2019 Intel Corporation
 // SPDX-License-Identifier: Apache-2.0
 //
 
@@ -41,13 +41,20 @@ INFERENCE_ENGINE_API_CPP(void) f32tof16Arrays(short *dst,
 
 // small helper function to represent uint32_t value as float32
 inline float asfloat(uint32_t v) {
-    return *reinterpret_cast<float *>(&v);
+    // Both type-punning casts and unions are UB per C++ spec
+    // But compilers usually only break code with casts
+    union {
+        float f;
+        uint32_t i;
+    };
+    i = v;
+    return f;
 }
 
 // Function to convert F32 into F16
 INFERENCE_ENGINE_API_CPP(float) f16tof32(ie_fp16 x) {
     // this is storage for output result
-    uint32_t u = x;
+    uint32_t u = static_cast<uint32_t>(x);
 
     // get sign in 32bit format
     uint32_t s = ((u & 0x8000) << 16);
@@ -65,8 +72,23 @@ INFERENCE_ENGINE_API_CPP(float) f16tof32(ie_fp16 x) {
         u <<= (23 - 10);
         u |= EXP_MASK_F32;
         u |= s;
-    } else if ((x & EXP_MASK_F16) == 0) {  // check for zero and denormals. both are converted to zero
-        u = s;
+    } else if ((u & EXP_MASK_F16) == 0) {  // check for zero and denormals.
+        uint16_t h_sig = (u & 0x03ffu);
+        if (h_sig == 0) {
+            /* Signed zero */
+            u = s;
+        } else {
+            /* Subnormal */
+            uint16_t h_exp = (u & EXP_MASK_F16);
+            h_sig <<= 1;
+            while ((h_sig & 0x0400u) == 0) {
+                h_sig <<= 1;
+                h_exp++;
+            }
+            uint32_t f_exp = (static_cast<uint32_t>(127 - 15 - h_exp)) << 23;
+            uint32_t f_sig = (static_cast<uint32_t>(h_sig & 0x03ffu)) << 13;
+            u = s + f_exp + f_sig;
+        }
     } else {
         // abs
         u = (u & 0x7FFF);
@@ -82,7 +104,7 @@ INFERENCE_ENGINE_API_CPP(float) f16tof32(ie_fp16 x) {
     }
 
     // finaly represent result as float and return
-    return *reinterpret_cast<float *>(&u);
+    return asfloat(u);
 }
 
 // This function convert f32 to f16 with rounding to nearest value to minimize error