aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMika Väinölä <33728696+mvainola@users.noreply.github.com>2021-04-07 13:12:17 +0300
committerGitHub <noreply@github.com>2021-04-07 11:12:17 +0100
commitaa69a0ac23ea7f68dd32bbef210546a5d84c1734 (patch)
treec2ded7dc3df088290ab8e45c1a9bc64ab4245014
parentdd917bb97dd1a2957ca3e5d2d75f463e5ac91caa (diff)
downloadamber-aa69a0ac23ea7f68dd32bbef210546a5d84c1734.tar.gz
Workaround for GCC 11 uninit variable warnings (#946)
Building Amber with GCC 11.0.1 produces some uninitialized variable warnings. This commit works around them by replacing reinterpret_cast with memcpy when type punning unsigned integers to floats.
-rw-r--r--src/float16_helper.cc22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/float16_helper.cc b/src/float16_helper.cc
index 617bd72..5cb35e7 100644
--- a/src/float16_helper.cc
+++ b/src/float16_helper.cc
@@ -15,6 +15,7 @@
#include "src/float16_helper.h"
#include <cassert>
+#include <cstring>
// Float10
// | 9 8 7 6 5 | 4 3 2 1 0 |
@@ -75,8 +76,11 @@ float HexFloat16ToFloat(const uint8_t* value) {
}
uint32_t hex = sign | exponent | mantissa;
- float* hex_float = reinterpret_cast<float*>(&hex);
- return *hex_float;
+ float hex_float;
+ static_assert((sizeof(uint32_t) == sizeof(float)),
+ "sizeof(uint32_t) != sizeof(float)");
+ memcpy(&hex_float, &hex, sizeof(float));
+ return hex_float;
}
// Convert float |value| whose size is 11 bits to 32 bits float
@@ -89,8 +93,11 @@ float HexFloat11ToFloat(const uint8_t* value) {
uint32_t mantissa = (static_cast<uint32_t>(value[0]) & 0x3f) << 17U;
uint32_t hex = exponent | mantissa;
- float* hex_float = reinterpret_cast<float*>(&hex);
- return *hex_float;
+ float hex_float;
+ static_assert((sizeof(uint32_t) == sizeof(float)),
+ "sizeof(uint32_t) != sizeof(float)");
+ memcpy(&hex_float, &hex, sizeof(float));
+ return hex_float;
}
// Convert float |value| whose size is 10 bits to 32 bits float
@@ -103,8 +110,11 @@ float HexFloat10ToFloat(const uint8_t* value) {
uint32_t mantissa = (static_cast<uint32_t>(value[0]) & 0x1f) << 18U;
uint32_t hex = exponent | mantissa;
- float* hex_float = reinterpret_cast<float*>(&hex);
- return *hex_float;
+ float hex_float;
+ static_assert((sizeof(uint32_t) == sizeof(float)),
+ "sizeof(uint32_t) != sizeof(float)");
+ memcpy(&hex_float, &hex, sizeof(float));
+ return hex_float;
}
} // namespace