mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-06-21 08:53:17 +08:00
Fix alias violation in BFloat16
reinterpret_cast between unrelated types is undefined behavior and leads to misoptimizations on some platforms. Use the safer (and faster) version via bit_cast
This commit is contained in:
parent
4d622be118
commit
b5eaa42695
@ -253,12 +253,7 @@ EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __bfloat16_raw truncate_to_bfloat16(const
|
|||||||
output.value = std::signbit(v) ? 0xFFC0: 0x7FC0;
|
output.value = std::signbit(v) ? 0xFFC0: 0x7FC0;
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
const uint16_t* p = reinterpret_cast<const uint16_t*>(&v);
|
output.value = static_cast<numext::uint16_t>(numext::bit_cast<numext::uint32_t>(v) >> 16);
|
||||||
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
||||||
output.value = p[0];
|
|
||||||
#else
|
|
||||||
output.value = p[1];
|
|
||||||
#endif
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -464,14 +459,7 @@ EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __bfloat16_raw float_to_bfloat16_rtne<true
|
|||||||
}
|
}
|
||||||
|
|
||||||
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float bfloat16_to_float(__bfloat16_raw h) {
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float bfloat16_to_float(__bfloat16_raw h) {
|
||||||
float result = 0;
|
return numext::bit_cast<float>(static_cast<numext::uint32_t>(h.value) << 16);
|
||||||
unsigned short* q = reinterpret_cast<unsigned short*>(&result);
|
|
||||||
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
||||||
q[0] = h.value;
|
|
||||||
#else
|
|
||||||
q[1] = h.value;
|
|
||||||
#endif
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
// --- standard functions ---
|
// --- standard functions ---
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user