From 95476bfcf64aa9cb43775ebfe3410ce9565de4d5 Mon Sep 17 00:00:00 2001 From: Ivan Komissarov Date: Mon, 22 Oct 2018 19:08:57 +0200 Subject: qendian: Fix float conversions Since Qt 5.10, qTo/FromBig/LittleEndian stopped working. It may be confusing, but big endian floats do exist, so not to break old code, we should support them. Change-Id: I21cdbc7f48ec030ce3d82f1cd1aad212f0fe5dd0 Reviewed-by: Thiago Macieira --- src/corelib/global/qendian.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src') diff --git a/src/corelib/global/qendian.h b/src/corelib/global/qendian.h index 1cc8a823d9..0e67a1ab8e 100644 --- a/src/corelib/global/qendian.h +++ b/src/corelib/global/qendian.h @@ -41,6 +41,7 @@ #ifndef QENDIAN_H #define QENDIAN_H +#include #include // include stdlib.h and hope that it defines __GLIBC__ for glibc-based systems @@ -151,6 +152,31 @@ template <> inline Q_DECL_CONSTEXPR qint8 qbswap(qint8 source) return source; } +// floating specializations +template +Float qbswapFloatHelper(Float source) +{ + // memcpy call in qFromUnaligned is recognized by optimizer as a correct way of type prunning + auto temp = qFromUnaligned::Unsigned>(&source); + temp = qbswap(temp); + return qFromUnaligned(&temp); +} + +template <> inline qfloat16 qbswap(qfloat16 source) +{ + return qbswapFloatHelper(source); +} + +template <> inline float qbswap(float source) +{ + return qbswapFloatHelper(source); +} + +template <> inline double qbswap(double source) +{ + return qbswapFloatHelper(source); +} + /* * qbswap(const T src, const void *dest); * Changes the byte order of \a src from big endian to little endian or vice versa -- cgit v1.2.3