Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:24:33

0001 // Copyright (C) 2022 The Qt Company Ltd.
0002 // Copyright (C) 2022 Intel Corporation.
0003 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0004 
0005 #ifndef QTYPES_H
0006 #define QTYPES_H
0007 
0008 #include <QtCore/qprocessordetection.h>
0009 #include <QtCore/qsystemdetection.h>
0010 #include <QtCore/qtconfigmacros.h>
0011 #include <QtCore/qassert.h>
0012 
0013 #ifdef __cplusplus
0014 #  include <cstddef>
0015 #  if defined(_HAS_STD_BYTE) && _HAS_STD_BYTE == 0
0016 #    error "Qt requires std::byte, but _HAS_STD_BYTE has been set to 0"
0017 #  endif
0018 #  include <cstdint>
0019 #  if defined(__STDCPP_FLOAT16_T__) && __has_include(<stdfloat>)
0020 // P1467 implementation - https://wg21.link/p1467
0021 #    include <stdfloat>
0022 #  endif // defined(__STDCPP_FLOAT16_T__) && __has_include(<stdfloat>)
0023 #  include <type_traits>
0024 #else
0025 #  include <assert.h>
0026 #endif
0027 
0028 #if 0
0029 #pragma qt_class(QtTypes)
0030 #pragma qt_class(QIntegerForSize)
0031 #pragma qt_sync_stop_processing
0032 #endif
0033 
0034 /*
0035    Useful type definitions for Qt
0036 */
0037 typedef unsigned char uchar;
0038 typedef unsigned short ushort;
0039 typedef unsigned int uint;
0040 typedef unsigned long ulong;
0041 
0042 QT_BEGIN_NAMESPACE
0043 
0044 /*
0045    Size-dependent types (architecture-dependent byte order)
0046 
0047    Make sure to update QMetaType when changing these typedefs
0048 */
0049 
0050 typedef signed char qint8;         /* 8 bit signed */
0051 typedef unsigned char quint8;      /* 8 bit unsigned */
0052 typedef short qint16;              /* 16 bit signed */
0053 typedef unsigned short quint16;    /* 16 bit unsigned */
0054 typedef int qint32;                /* 32 bit signed */
0055 typedef unsigned int quint32;      /* 32 bit unsigned */
0056 // Unlike LL / ULL in C++, for historical reasons, we force the
0057 // result to be of the requested type.
0058 #ifdef __cplusplus
0059 #  define Q_INT64_C(c) static_cast<long long>(c ## LL)     /* signed 64 bit constant */
0060 #  define Q_UINT64_C(c) static_cast<unsigned long long>(c ## ULL) /* unsigned 64 bit constant */
0061 #else
0062 #  define Q_INT64_C(c) ((long long)(c ## LL))               /* signed 64 bit constant */
0063 #  define Q_UINT64_C(c) ((unsigned long long)(c ## ULL))    /* unsigned 64 bit constant */
0064 #endif
0065 typedef long long qint64;           /* 64 bit signed */
0066 typedef unsigned long long quint64; /* 64 bit unsigned */
0067 
0068 typedef qint64 qlonglong;
0069 typedef quint64 qulonglong;
0070 
0071 #ifdef Q_QDOC // QDoc always needs to see the typedefs
0072 #  define QT_SUPPORTS_INT128 16
0073 #elif defined(QT_COMPILER_SUPPORTS_INT128) && !defined(QT_NO_INT128)
0074 #  define QT_SUPPORTS_INT128 QT_COMPILER_SUPPORTS_INT128
0075 #  if defined(__GLIBCXX__) && defined(__STRICT_ANSI__) // -ansi/-std=c++NN instead of gnu++NN
0076 #    undef QT_SUPPORTS_INT128                          // breaks <type_traits> on libstdc++
0077 #  endif
0078 #  if defined(__clang__) && defined(_MSVC_STL_VERSION) // Clang with MSVC's STL
0079 #    undef QT_SUPPORTS_INT128                          // MSVC's STL doesn't support int128
0080 #  endif
0081 #else
0082 #  undef QT_SUPPORTS_INT128
0083 #endif
0084 
0085 #if defined(QT_SUPPORTS_INT128)
0086 __extension__ typedef __int128_t qint128;
0087 __extension__ typedef __uint128_t quint128;
0088 
0089 #ifdef __cplusplus
0090 static_assert(std::is_signed_v<qint128>,
0091               "Qt requires <type_traits> and <limits> to work for q(u)int128.");
0092 #endif
0093 
0094 // limits:
0095 #  ifdef __cplusplus /* need to avoid c-style-casts in C++ mode */
0096 #    define QT_C_STYLE_CAST(type, x) static_cast<type>(x)
0097 #  else /* but C doesn't have constructor-style casts */
0098 #    define QT_C_STYLE_CAST(type, x) ((type)(x))
0099 #  endif
0100 #  ifndef Q_UINT128_MAX /* allow qcompilerdetection.h/user override */
0101 #    define Q_UINT128_MAX QT_C_STYLE_CAST(quint128, -1)
0102 #  endif
0103 #  define Q_INT128_MAX QT_C_STYLE_CAST(qint128, Q_UINT128_MAX / 2)
0104 #  define Q_INT128_MIN (-Q_INT128_MAX - 1)
0105 
0106 #  ifdef __cplusplus
0107     namespace QtPrivate::NumberLiterals {
0108     namespace detail {
0109         template <quint128 accu, int base>
0110         constexpr quint128 construct() { return accu; }
0111 
0112         template <quint128 accu, int base, char C, char...Cs>
0113         constexpr quint128 construct()
0114         {
0115             if constexpr (C != '\'') { // ignore digit separators
0116                 const int digitValue = '0' <= C && C <= '9' ? C - '0'      :
0117                                        'a' <= C && C <= 'z' ? C - 'a' + 10 :
0118                                        'A' <= C && C <= 'Z' ? C - 'A' + 10 :
0119                                        /* else */        -1 ;
0120                 static_assert(digitValue >= 0 && digitValue < base,
0121                               "Invalid character");
0122                 // accu * base + digitValue <= MAX, but without overflow:
0123                 static_assert(accu <= (Q_UINT128_MAX - digitValue) / base,
0124                               "Overflow occurred");
0125                 return construct<accu * base + digitValue, base, Cs...>();
0126             } else {
0127                 return construct<accu, base, Cs...>();
0128             }
0129         }
0130 
0131         template <char C, char...Cs>
0132         constexpr quint128 parse0xb()
0133         {
0134             constexpr quint128 accu = 0;
0135             if constexpr (C == 'x' || C == 'X')
0136                 return construct<accu, 16,   Cs...>(); // base 16, skip 'x'
0137             else if constexpr (C == 'b' || C == 'B')
0138                 return construct<accu, 2,    Cs...>(); // base 2, skip 'b'
0139             else
0140                 return construct<accu, 8, C, Cs...>(); // base 8, include C
0141         }
0142 
0143         template <char...Cs>
0144         constexpr quint128 parse0()
0145         {
0146             if constexpr (sizeof...(Cs) == 0) // this was just a literal 0
0147                 return 0;
0148             else
0149                 return parse0xb<Cs...>();
0150         }
0151 
0152         template <char C, char...Cs>
0153         constexpr quint128 parse()
0154         {
0155             if constexpr (C == '0')
0156                 return parse0<Cs...>(); // base 2, 8, or 16 (or just a literal 0), skip '0'
0157             else
0158                 return construct<0, 10, C, Cs...>(); // initial accu 0, base 10, include C
0159         }
0160     } // namespace detail
0161     template <char...Cs>
0162     constexpr quint128 operator""_quint128() noexcept
0163     { return QtPrivate::NumberLiterals::detail::parse<Cs...>(); }
0164     template <char...Cs>
0165     constexpr qint128 operator""_qint128() noexcept
0166     { return qint128(QtPrivate::NumberLiterals::detail::parse<Cs...>()); }
0167 
0168     #ifndef Q_UINT128_C // allow qcompilerdetection.h/user override
0169     #  define Q_UINT128_C(c) ([]{ using namespace QtPrivate::NumberLiterals; return c ## _quint128; }())
0170     #endif
0171     #ifndef Q_INT128_C // allow qcompilerdetection.h/user override
0172     #  define Q_INT128_C(c)  ([]{ using namespace QtPrivate::NumberLiterals; return c ## _qint128;  }())
0173     #endif
0174 
0175     } // namespace QtPrivate::NumberLiterals
0176 #  endif // __cplusplus
0177 #endif // QT_SUPPORTS_INT128
0178 
0179 #ifndef __cplusplus
0180 // In C++ mode, we define below using QIntegerForSize template
0181 static_assert(sizeof(ptrdiff_t) == sizeof(size_t), "Weird ptrdiff_t and size_t definitions");
0182 typedef ptrdiff_t qptrdiff;
0183 typedef ptrdiff_t qsizetype;
0184 typedef ptrdiff_t qintptr;
0185 typedef size_t quintptr;
0186 
0187 #define PRIdQPTRDIFF "td"
0188 #define PRIiQPTRDIFF "ti"
0189 
0190 #define PRIdQSIZETYPE "td"
0191 #define PRIiQSIZETYPE "ti"
0192 
0193 #define PRIdQINTPTR "td"
0194 #define PRIiQINTPTR "ti"
0195 
0196 #define PRIuQUINTPTR "zu"
0197 #define PRIoQUINTPTR "zo"
0198 #define PRIxQUINTPTR "zx"
0199 #define PRIXQUINTPTR "zX"
0200 #endif
0201 
0202 #if defined(QT_COORD_TYPE)
0203 typedef QT_COORD_TYPE qreal;
0204 #else
0205 typedef double qreal;
0206 #endif
0207 
0208 #if defined(__cplusplus)
0209 /*
0210   quintptr and qptrdiff are guaranteed to be the same size as a pointer, i.e.
0211 
0212       sizeof(void *) == sizeof(quintptr)
0213       && sizeof(void *) == sizeof(qptrdiff)
0214 
0215   While size_t and qsizetype are not guaranteed to be the same size as a pointer,
0216   they usually are and we do check for that in qtypes.cpp, just to be sure.
0217 */
0218 template <int> struct QIntegerForSize;
0219 template <>    struct QIntegerForSize<1> { typedef quint8  Unsigned; typedef qint8  Signed; };
0220 template <>    struct QIntegerForSize<2> { typedef quint16 Unsigned; typedef qint16 Signed; };
0221 template <>    struct QIntegerForSize<4> { typedef quint32 Unsigned; typedef qint32 Signed; };
0222 template <>    struct QIntegerForSize<8> { typedef quint64 Unsigned; typedef qint64 Signed; };
0223 #if defined(QT_SUPPORTS_INT128)
0224 template <>    struct QIntegerForSize<16> { typedef quint128 Unsigned; typedef qint128 Signed; };
0225 #endif
0226 template <class T> struct QIntegerForSizeof: QIntegerForSize<sizeof(T)> { };
0227 typedef QIntegerForSize<Q_PROCESSOR_WORDSIZE>::Signed qregisterint;
0228 typedef QIntegerForSize<Q_PROCESSOR_WORDSIZE>::Unsigned qregisteruint;
0229 typedef QIntegerForSizeof<void *>::Unsigned quintptr;
0230 typedef QIntegerForSizeof<void *>::Signed qptrdiff;
0231 typedef qptrdiff qintptr;
0232 using qsizetype = QIntegerForSizeof<std::size_t>::Signed;
0233 
0234 // These custom definitions are necessary as we're not defining our
0235 // datatypes in terms of the language ones, but in terms of integer
0236 // types that have the sime size. For instance, on a 32-bit platform,
0237 // qptrdiff is int, while ptrdiff_t may be aliased to long; therefore
0238 // using %td to print a qptrdiff would be wrong (and raise -Wformat
0239 // warnings), although both int and long have same bit size on that
0240 // platform.
0241 //
0242 // We know that sizeof(size_t) == sizeof(void *) == sizeof(qptrdiff).
0243 #if SIZE_MAX == 0xffffffffULL
0244 #define PRIuQUINTPTR "u"
0245 #define PRIoQUINTPTR "o"
0246 #define PRIxQUINTPTR "x"
0247 #define PRIXQUINTPTR "X"
0248 
0249 #define PRIdQPTRDIFF "d"
0250 #define PRIiQPTRDIFF "i"
0251 
0252 #define PRIdQINTPTR "d"
0253 #define PRIiQINTPTR "i"
0254 
0255 #define PRIdQSIZETYPE "d"
0256 #define PRIiQSIZETYPE "i"
0257 #elif SIZE_MAX == 0xffffffffffffffffULL
0258 #define PRIuQUINTPTR "llu"
0259 #define PRIoQUINTPTR "llo"
0260 #define PRIxQUINTPTR "llx"
0261 #define PRIXQUINTPTR "llX"
0262 
0263 #define PRIdQPTRDIFF "lld"
0264 #define PRIiQPTRDIFF "lli"
0265 
0266 #define PRIdQINTPTR "lld"
0267 #define PRIiQINTPTR "lli"
0268 
0269 #define PRIdQSIZETYPE "lld"
0270 #define PRIiQSIZETYPE "lli"
0271 #else
0272 #error Unsupported platform (unknown value for SIZE_MAX)
0273 #endif
0274 
0275 // Define a native float16 type
0276 namespace QtPrivate {
0277 #if defined(__STDCPP_FLOAT16_T__)
0278 #  define QFLOAT16_IS_NATIVE        1
0279 using NativeFloat16Type = std::float16_t;
0280 #elif defined(Q_CC_CLANG) && defined(__FLT16_MAX__) && 0
0281 // disabled due to https://github.com/llvm/llvm-project/issues/56963
0282 #  define QFLOAT16_IS_NATIVE        1
0283 using NativeFloat16Type = decltype(__FLT16_MAX__);
0284 #elif defined(Q_CC_GNU_ONLY) && defined(__FLT16_MAX__) && defined(__ARM_FP16_FORMAT_IEEE)
0285 #  define QFLOAT16_IS_NATIVE        1
0286 using NativeFloat16Type = __fp16;
0287 #elif defined(Q_CC_GNU_ONLY) && defined(__FLT16_MAX__) && defined(__SSE2__)
0288 #  define QFLOAT16_IS_NATIVE        1
0289 using NativeFloat16Type = _Float16;
0290 #else
0291 #  define QFLOAT16_IS_NATIVE        0
0292 using NativeFloat16Type = void;
0293 #endif
0294 } // QtPrivate
0295 
0296 #endif // __cplusplus
0297 
0298 QT_END_NAMESPACE
0299 
0300 #endif // QTYPES_H