Skip to content

Commit

Permalink
[#586] Fix FloatUtil conversions to respect strict aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed Aug 8, 2024
1 parent 581feb6 commit 530028b
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions compiler/extensions/cpp/runtime/src/zserio/FloatUtil.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <cstring>

#include "zserio/FloatUtil.h"

namespace zserio
Expand Down Expand Up @@ -166,30 +168,34 @@ uint16_t convertFloatToUInt16(float float32)

float convertUInt32ToFloat(uint32_t float32Value)
{
const float* convertedFloat = reinterpret_cast<const float*>(&float32Value);
float convertedFloat = 0.0f;
std::memcpy(&convertedFloat, &float32Value, sizeof(uint32_t));

return *convertedFloat;
return convertedFloat;
}

uint32_t convertFloatToUInt32(float float32)
{
const uint32_t* float32ValuePtr = reinterpret_cast<const uint32_t*>(&float32);
uint32_t float32Value = 0;
std::memcpy(&float32Value, &float32, sizeof(float));

return *float32ValuePtr;
return float32Value;
}

double convertUInt64ToDouble(uint64_t float64Value)
{
const double* convertedDouble = reinterpret_cast<const double*>(&float64Value);
double convertedDouble = 0.0;
std::memcpy(&convertedDouble, &float64Value, sizeof(uint64_t));

return *convertedDouble;
return convertedDouble;
}

uint64_t convertDoubleToUInt64(double float64)
{
const uint64_t* float64ValuePtr = reinterpret_cast<const uint64_t*>(&float64);
uint64_t float64Value = 0;
std::memcpy(&float64Value, &float64, sizeof(double));

return *float64ValuePtr;
return float64Value;
}

} // namespace zserio

0 comments on commit 530028b

Please sign in to comment.