Andrew Geissler | f103a7f | 2021-05-07 16:09:40 -0500 | [diff] [blame] | 1 | From 9cd614dd5481a4fdf552effac4820f51a10092c7 Mon Sep 17 00:00:00 2001 |
| 2 | From: =?UTF-8?q?Mika=20V=C3=A4in=C3=B6l=C3=A4?= |
| 3 | <33728696+mvainola@users.noreply.github.com> |
| 4 | Date: Wed, 7 Apr 2021 13:12:17 +0300 |
| 5 | Subject: [PATCH] Workaround for GCC 11 uninit variable warnings (#946) |
| 6 | |
| 7 | Building Amber with GCC 11.0.1 produces some uninitialized variable |
| 8 | warnings. This commit works around them by replacing |
| 9 | reinterpret_cast with memcpy when type punning unsigned integers to |
| 10 | floats. |
| 11 | |
| 12 | Upstream-Status: Backport [https://github.com/google/amber/commit/aa69a0ac23ea7f68dd32bbef210546a5d84c1734] |
| 13 | --- |
| 14 | src/float16_helper.cc | 22 ++++++++++++++++------ |
| 15 | 1 file changed, 16 insertions(+), 6 deletions(-) |
| 16 | |
| 17 | diff --git a/src/float16_helper.cc b/src/float16_helper.cc |
| 18 | index 617bd72..5cb35e7 100644 |
| 19 | --- a/src/float16_helper.cc |
| 20 | +++ b/src/float16_helper.cc |
| 21 | @@ -15,6 +15,7 @@ |
| 22 | #include "src/float16_helper.h" |
| 23 | |
| 24 | #include <cassert> |
| 25 | +#include <cstring> |
| 26 | |
| 27 | // Float10 |
| 28 | // | 9 8 7 6 5 | 4 3 2 1 0 | |
| 29 | @@ -75,8 +76,11 @@ float HexFloat16ToFloat(const uint8_t* value) { |
| 30 | } |
| 31 | |
| 32 | uint32_t hex = sign | exponent | mantissa; |
| 33 | - float* hex_float = reinterpret_cast<float*>(&hex); |
| 34 | - return *hex_float; |
| 35 | + float hex_float; |
| 36 | + static_assert((sizeof(uint32_t) == sizeof(float)), |
| 37 | + "sizeof(uint32_t) != sizeof(float)"); |
| 38 | + memcpy(&hex_float, &hex, sizeof(float)); |
| 39 | + return hex_float; |
| 40 | } |
| 41 | |
| 42 | // Convert float |value| whose size is 11 bits to 32 bits float |
| 43 | @@ -89,8 +93,11 @@ float HexFloat11ToFloat(const uint8_t* value) { |
| 44 | uint32_t mantissa = (static_cast<uint32_t>(value[0]) & 0x3f) << 17U; |
| 45 | |
| 46 | uint32_t hex = exponent | mantissa; |
| 47 | - float* hex_float = reinterpret_cast<float*>(&hex); |
| 48 | - return *hex_float; |
| 49 | + float hex_float; |
| 50 | + static_assert((sizeof(uint32_t) == sizeof(float)), |
| 51 | + "sizeof(uint32_t) != sizeof(float)"); |
| 52 | + memcpy(&hex_float, &hex, sizeof(float)); |
| 53 | + return hex_float; |
| 54 | } |
| 55 | |
| 56 | // Convert float |value| whose size is 10 bits to 32 bits float |
| 57 | @@ -103,8 +110,11 @@ float HexFloat10ToFloat(const uint8_t* value) { |
| 58 | uint32_t mantissa = (static_cast<uint32_t>(value[0]) & 0x1f) << 18U; |
| 59 | |
| 60 | uint32_t hex = exponent | mantissa; |
| 61 | - float* hex_float = reinterpret_cast<float*>(&hex); |
| 62 | - return *hex_float; |
| 63 | + float hex_float; |
| 64 | + static_assert((sizeof(uint32_t) == sizeof(float)), |
| 65 | + "sizeof(uint32_t) != sizeof(float)"); |
| 66 | + memcpy(&hex_float, &hex, sizeof(float)); |
| 67 | + return hex_float; |
| 68 | } |
| 69 | |
| 70 | } // namespace |
| 71 | -- |
| 72 | 2.31.1 |
| 73 | |