[llvm] r340507 - Fix undefined behavior in r340457
Scott Linder via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 22 19:51:09 PDT 2018
Author: scott.linder
Date: Wed Aug 22 19:51:09 2018
New Revision: 340507
URL: http://llvm.org/viewvc/llvm-project?rev=340507&view=rev
Log:
Fix undefined behavior in r340457
Modified:
llvm/trunk/lib/BinaryFormat/MsgPackWriter.cpp
Modified: llvm/trunk/lib/BinaryFormat/MsgPackWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/BinaryFormat/MsgPackWriter.cpp?rev=340507&r1=340506&r2=340507&view=diff
==============================================================================
--- llvm/trunk/lib/BinaryFormat/MsgPackWriter.cpp (original)
+++ llvm/trunk/lib/BinaryFormat/MsgPackWriter.cpp Wed Aug 22 19:51:09 2018
@@ -88,10 +88,11 @@ void Writer::write(uint64_t u) {
void Writer::write(double d) {
// If no loss of precision, encode as a Float32.
- float f = static_cast<float>(d);
- if (static_cast<double>(f) == d) {
+ double a = std::fabs(d);
+ if (a >= std::numeric_limits<float>::min() &&
+ a <= std::numeric_limits<float>::max()) {
EW.write(FirstByte::Float32);
- EW.write(f);
+ EW.write(static_cast<float>(d));
} else {
EW.write(FirstByte::Float64);
EW.write(d);
More information about the llvm-commits
mailing list