avoid memcpy() in endian templates for performance reasons

only difference from the GCC version being sign-ness:
https://github.com/gcc-mirror/gcc/blob/master/libgcc/memcpy.c

with the potential loop-unrolling optimization from the compiler since the
size of the type should be known

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2021-06-02 03:52:14 +03:00
parent cc77f4a544
commit 3d99f5f82a

View file

@ -24,8 +24,6 @@
#include <QtCore/qglobal.h>
#include <string.h>
QT_BEGIN_NAMESPACE
/*
@ -36,6 +34,11 @@ inline void qbswap_helper(const uchar *src, uchar *dest, int size)
for (int i = 0; i < size ; ++i) dest[i] = src[size - 1 - i];
}
inline void qbcopy_helper(const uchar *src, uchar *dest, int size)
{
for (int i = 0; i < size ; ++i) dest[i] = src[i];
}
/*
* qbswap(const T src, const uchar *dest);
* Changes the byte order of \a src from big endian to little endian or vice versa
@ -188,7 +191,7 @@ template <typename T> inline T qToLittleEndian(const T source)
template <typename T> inline T qFromLittleEndian(const T source)
{ return qbswap<T>(source); }
template <typename T> inline void qToBigEndian(const T src, uchar *dest)
{ ::memcpy(dest, &src, sizeof(T)); }
{ qbcopy_helper(reinterpret_cast<const uchar *>(&src), dest, sizeof(T)); }
template <typename T> inline void qToLittleEndian(const T src, uchar *dest)
{ qbswap<T>(src, dest); }
#else // Q_LITTLE_ENDIAN
@ -204,7 +207,7 @@ template <typename T> inline T qFromLittleEndian(const T source)
template <typename T> inline void qToBigEndian(const T src, uchar *dest)
{ qbswap<T>(src, dest); }
template <typename T> inline void qToLittleEndian(const T src, uchar *dest)
{ ::memcpy(dest, &src, sizeof(T)); }
{ qbcopy_helper(reinterpret_cast<const uchar *>(&src), dest, sizeof(T)); }
#endif // Q_BYTE_ORDER == Q_BIG_ENDIAN