diff options
Diffstat (limited to 'src/corelib/global/qendian.h')
-rw-r--r-- | src/corelib/global/qendian.h | 135 |
1 files changed, 61 insertions, 74 deletions
diff --git a/src/corelib/global/qendian.h b/src/corelib/global/qendian.h index a50ade4aa4..8c3b5e4374 100644 --- a/src/corelib/global/qendian.h +++ b/src/corelib/global/qendian.h @@ -1,49 +1,19 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2021 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QENDIAN_H #define QENDIAN_H +#if 0 +#pragma qt_class(QtEndian) +#endif + #include <QtCore/qfloat16.h> #include <QtCore/qglobal.h> +#include <limits> + // include stdlib.h and hope that it defines __GLIBC__ for glibc-based systems #include <stdlib.h> #include <string.h> @@ -89,7 +59,7 @@ template <typename T> Q_ALWAYS_INLINE T qFromUnaligned(const void *src) // These definitions are written so that they are recognized by most compilers // as bswap and replaced with single instruction builtins if available. -inline Q_DECL_CONSTEXPR quint64 qbswap_helper(quint64 source) +inline constexpr quint64 qbswap_helper(quint64 source) { return 0 | ((source & Q_UINT64_C(0x00000000000000ff)) << 56) @@ -102,7 +72,7 @@ inline Q_DECL_CONSTEXPR quint64 qbswap_helper(quint64 source) | ((source & Q_UINT64_C(0xff00000000000000)) >> 56); } -inline Q_DECL_CONSTEXPR quint32 qbswap_helper(quint32 source) +inline constexpr quint32 qbswap_helper(quint32 source) { return 0 | ((source & 0x000000ff) << 24) @@ -111,30 +81,47 @@ inline Q_DECL_CONSTEXPR quint32 qbswap_helper(quint32 source) | ((source & 0xff000000) >> 24); } -inline Q_DECL_CONSTEXPR quint16 qbswap_helper(quint16 source) +inline constexpr quint16 qbswap_helper(quint16 source) { return quint16( 0 | ((source & 0x00ff) << 8) | ((source & 0xff00) >> 8) ); } -inline Q_DECL_CONSTEXPR quint8 qbswap_helper(quint8 source) +inline constexpr quint8 qbswap_helper(quint8 source) { return source; } /* * T qbswap(T source). - * Changes the byte order of a value from big endian to little endian or vice versa. + * Changes the byte order of a value from big-endian to little-endian or vice versa. * This function can be used if you are not concerned about alignment issues, * and it is therefore a bit more convenient and in most cases more efficient. */ template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>> -inline Q_DECL_CONSTEXPR T qbswap(T source) +inline constexpr T qbswap(T source) { return T(qbswap_helper(typename QIntegerForSizeof<T>::Unsigned(source))); } +#ifdef QT_SUPPORTS_INT128 +// extra definitions for q(u)int128, in case std::is_integral_v<~~> == false +inline constexpr quint128 qbswap(quint128 source) +{ + quint128 result = {}; + result = qbswap_helper(quint64(source)); + result <<= 64; + result |= qbswap_helper(quint64(source >> 64)); + return result; +} + +inline constexpr qint128 qbswap(qint128 source) +{ + return qint128(qbswap(quint128(source))); +} +#endif + // floating specializations template<typename Float> Float qbswapFloatHelper(Float source) @@ -162,7 +149,7 @@ inline double qbswap(double source) /* * qbswap(const T src, const void *dest); - * Changes the byte order of \a src from big endian to little endian or vice versa + * Changes the byte order of \a src from big-endian to little-endian or vice versa * and stores the result in \a dest. * There is no alignment requirements for \a dest. */ @@ -182,13 +169,13 @@ template<> Q_CORE_EXPORT void *qbswap<8>(const void *source, qsizetype count, vo #if Q_BYTE_ORDER == Q_BIG_ENDIAN -template <typename T> inline Q_DECL_CONSTEXPR T qToBigEndian(T source) +template <typename T> inline constexpr T qToBigEndian(T source) { return source; } -template <typename T> inline Q_DECL_CONSTEXPR T qFromBigEndian(T source) +template <typename T> inline constexpr T qFromBigEndian(T source) { return source; } -template <typename T> inline Q_DECL_CONSTEXPR T qToLittleEndian(T source) +template <typename T> inline constexpr T qToLittleEndian(T source) { return qbswap(source); } -template <typename T> inline Q_DECL_CONSTEXPR T qFromLittleEndian(T source) +template <typename T> inline constexpr T qFromLittleEndian(T source) { return qbswap(source); } template <typename T> inline void qToBigEndian(T src, void *dest) { qToUnaligned<T>(src, dest); } @@ -205,13 +192,13 @@ template <typename T> inline void qFromLittleEndian(const void *source, qsizetyp { qbswap<sizeof(T)>(source, count, dest); } #else // Q_LITTLE_ENDIAN -template <typename T> inline Q_DECL_CONSTEXPR T qToBigEndian(T source) +template <typename T> inline constexpr T qToBigEndian(T source) { return qbswap(source); } -template <typename T> inline Q_DECL_CONSTEXPR T qFromBigEndian(T source) +template <typename T> inline constexpr T qFromBigEndian(T source) { return qbswap(source); } -template <typename T> inline Q_DECL_CONSTEXPR T qToLittleEndian(T source) +template <typename T> inline constexpr T qToLittleEndian(T source) { return source; } -template <typename T> inline Q_DECL_CONSTEXPR T qFromLittleEndian(T source) +template <typename T> inline constexpr T qFromLittleEndian(T source) { return source; } template <typename T> inline void qToBigEndian(T src, void *dest) { qbswap<T>(src, dest); } @@ -265,7 +252,7 @@ class QSpecialInteger T val; public: QSpecialInteger() = default; - explicit Q_DECL_CONSTEXPR QSpecialInteger(T i) : val(S::toSpecial(i)) {} + explicit constexpr QSpecialInteger(T i) : val(S::toSpecial(i)) {} QSpecialInteger &operator =(T i) { val = S::toSpecial(i); return *this; } operator T() const { return S::fromSpecial(val); } @@ -310,33 +297,33 @@ public: return pre; } - static Q_DECL_CONSTEXPR QSpecialInteger max() - { return QSpecialInteger(std::numeric_limits<T>::max()); } - static Q_DECL_CONSTEXPR QSpecialInteger min() - { return QSpecialInteger(std::numeric_limits<T>::min()); } + static constexpr QSpecialInteger max() + { return QSpecialInteger((std::numeric_limits<T>::max)()); } + static constexpr QSpecialInteger min() + { return QSpecialInteger((std::numeric_limits<T>::min)()); } }; template<typename T> class QLittleEndianStorageType { public: typedef T StorageType; - static Q_DECL_CONSTEXPR T toSpecial(T source) { return qToLittleEndian(source); } - static Q_DECL_CONSTEXPR T fromSpecial(T source) { return qFromLittleEndian(source); } + static constexpr T toSpecial(T source) { return qToLittleEndian(source); } + static constexpr T fromSpecial(T source) { return qFromLittleEndian(source); } }; template<typename T> class QBigEndianStorageType { public: typedef T StorageType; - static Q_DECL_CONSTEXPR T toSpecial(T source) { return qToBigEndian(source); } - static Q_DECL_CONSTEXPR T fromSpecial(T source) { return qFromBigEndian(source); } + static constexpr T toSpecial(T source) { return qToBigEndian(source); } + static constexpr T fromSpecial(T source) { return qFromBigEndian(source); } }; -#ifdef Q_CLANG_QDOC +#ifdef Q_QDOC template<typename T> class QLEInteger { public: - explicit Q_DECL_CONSTEXPR QLEInteger(T i); + explicit constexpr QLEInteger(T i); QLEInteger &operator =(T i); operator T() const; bool operator ==(QLEInteger i) const; @@ -353,17 +340,17 @@ public: QLEInteger &operator ^=(T i); QLEInteger &operator ++(); QLEInteger &operator --(); - QLEInteger &operator ++(int); - QLEInteger &operator --(int); + QLEInteger operator ++(int); + QLEInteger operator --(int); - static Q_DECL_CONSTEXPR QLEInteger max(); - static Q_DECL_CONSTEXPR QLEInteger min(); + static constexpr QLEInteger max(); + static constexpr QLEInteger min(); }; template<typename T> class QBEInteger { public: - explicit Q_DECL_CONSTEXPR QBEInteger(T i); + explicit constexpr QBEInteger(T i); QBEInteger &operator =(T i); operator T() const; bool operator ==(QBEInteger i) const; @@ -380,11 +367,11 @@ public: QBEInteger &operator ^=(T i); QBEInteger &operator ++(); QBEInteger &operator --(); - QBEInteger &operator ++(int); - QBEInteger &operator --(int); + QBEInteger operator ++(int); + QBEInteger operator --(int); - static Q_DECL_CONSTEXPR QBEInteger max(); - static Q_DECL_CONSTEXPR QBEInteger min(); + static constexpr QBEInteger max(); + static constexpr QBEInteger min(); }; #else |