bf16_to_float: Fix strict aliasing violation
* This function was accessing a uint32_t with a pointer to float which
violates the strict aliasing rules. With GCC 11, this leads to a
-Wuninitialized warning, which causes the build to fail.
* This change uses memcpy instead of type punning which fixes the strict
aliasing violation, thereby fixing the build with GCC 11.
* Resolves MLCE-420
Change-Id: I9fda5f1e68ac68490b244bd40380910adae00bf3
Signed-off-by: Pablo Marquez Tello <pablo.tello@arm.com>
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/5318
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: Manuel Bottini <manuel.bottini@arm.com>
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
diff --git a/support/Bfloat16.h b/support/Bfloat16.h
index d57d8ce..173f2d1 100644
--- a/support/Bfloat16.h
+++ b/support/Bfloat16.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2020 Arm Limited.
+ * Copyright (c) 2020-2021 Arm Limited.
*
* SPDX-License-Identifier: MIT
*
@@ -25,6 +25,7 @@
#define ARM_COMPUTE_BFLOAT16_H
#include <cstdint>
+#include <cstring>
namespace arm_compute
{
@@ -70,9 +71,9 @@
inline float bf16_to_float(const uint16_t &v)
{
const uint32_t lv = (v << 16);
- const float *fp = reinterpret_cast<const float *>(&lv);
-
- return *fp;
+ float fp;
+ memcpy(&fp, &lv, sizeof(lv));
+ return fp;
}
}
@@ -137,4 +138,4 @@
uint16_t value;
};
} // namespace arm_compute
-#endif /* ARM_COMPUTE_BFLOAT16_H */
\ No newline at end of file
+#endif /* ARM_COMPUTE_BFLOAT16_H */