diff options
author | Niels Dekker <N.Dekker@lumc.nl> | 2020-07-14 19:55:20 +0200 |
---|---|---|
committer | Niels Dekker <N.Dekker@lumc.nl> | 2020-07-14 19:55:20 +0200 |
commit | b11f817bcff04276f3024d6780f56a137968b81a (patch) | |
tree | 7eaf9f0e7d67ef477ca6c77313406abd3c507497 /Eigen | |
parent | 56b3e3f3f8ca9972ca390c8296fde363bdab271c (diff) |
Avoid undefined behavior by union type punning in float_to_bfloat16_rtne
Use `numext::as_uint`, instead of union based type punning, to avoid undefined behavior.
See also C++ Core Guidelines: "Don't use a union for type punning"
https://github.com/isocpp/CppCoreGuidelines/blob/v0.8/CppCoreGuidelines.md#c183-dont-use-a-union-for-type-punning
`numext::as_uint` was suggested by David Tellenbach
Diffstat (limited to 'Eigen')
-rw-r--r-- | Eigen/src/Core/arch/Default/BFloat16.h | 10 |
1 files changed, 1 insertions, 9 deletions
diff --git a/Eigen/src/Core/arch/Default/BFloat16.h b/Eigen/src/Core/arch/Default/BFloat16.h index 34a4f0ced..d96105923 100644 --- a/Eigen/src/Core/arch/Default/BFloat16.h +++ b/Eigen/src/Core/arch/Default/BFloat16.h @@ -326,19 +326,11 @@ EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __bfloat16_raw raw_uint16_to_bfloat16(unsi return h; } -union float32_bits { - unsigned int u; - float f; -}; - EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __bfloat16_raw float_to_bfloat16_rtne(float ff) { #if (defined(EIGEN_HAS_CUDA_BF16) && defined(EIGEN_HAS_HIP_BF16)) // Nothing to do here #else - unsigned int input; - float32_bits f; - f.f = ff; - input = f.u; + unsigned int input = numext::as_uint(ff); __bfloat16_raw output; if (Eigen::numext::isnan EIGEN_NOT_A_MACRO(ff)) { |