diff --git a/rocclr/platform/memory.cpp b/rocclr/platform/memory.cpp index a887a2c95a..886810525b 100644 --- a/rocclr/platform/memory.cpp +++ b/rocclr/platform/memory.cpp @@ -1300,6 +1300,15 @@ void Image::copyToBackingStore(void* initFrom) { } } +template static Out interpret_value(In in) { + static_assert(sizeof(In) == sizeof(Out)); + union { + In in_val; + Out out_val; + } u{in}; + return u.out_val; +} + static int round_to_even(float v) { // clamp overflow if (v >= -(float)std::numeric_limits::min()) { @@ -1311,8 +1320,8 @@ static int round_to_even(float v) { static const unsigned int magic[2] = {0x4b000000u, 0xcb000000u}; // round fractional values to integer value - if (fabsf(v) < *reinterpret_cast(&magic[0])) { - float magicVal = *reinterpret_cast(&magic[v < 0.0f]); + if (fabsf(v) < interpret_value(magic[0])) { + float magicVal = interpret_value(magic[v < 0.0f]); v += magicVal; v -= magicVal; } @@ -1337,21 +1346,21 @@ static uint16_t float2half_rtz(float f) { } int values[5] = {0x47800000, 0x33800000, 0x38800000, 0x4b800000, 0x7f800000}; // overflow - if (x >= *reinterpret_cast(&values[0])) { - if (x == *reinterpret_cast(&values[4])) { + if (x >= interpret_value(values[0])) { + if (x == interpret_value(values[4])) { return 0x7c00 | sign; } return 0x7bff | sign; } // underflow - if (x < *reinterpret_cast(&values[1])) { + if (x < interpret_value(values[1])) { return sign; // The halfway case can return 0x0001 or 0. 0 is even. } // half denormal - if (x < *reinterpret_cast(&values[2])) { - x *= *reinterpret_cast(&values[3]); + if (x < interpret_value(values[2])) { + x *= interpret_value(values[3]); return static_cast((int)x | sign); }