diff options
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/corelib.pro | 2 | ||||
-rw-r--r-- | src/corelib/global/global.pri | 2 | ||||
-rw-r--r-- | src/corelib/global/qcompilerdetection.h | 29 | ||||
-rw-r--r-- | src/corelib/global/qfloat16.cpp | 85 | ||||
-rw-r--r-- | src/corelib/global/qfloat16.h | 3 | ||||
-rw-r--r-- | src/corelib/global/qfloat16_f16c.c | 87 | ||||
-rw-r--r-- | src/corelib/global/qglobal.h | 62 | ||||
-rw-r--r-- | src/corelib/tools/tools.pri | 1 |
8 files changed, 243 insertions, 28 deletions
diff --git a/src/corelib/corelib.pro b/src/corelib/corelib.pro index 1a7a463c2d..a0d41396dd 100644 --- a/src/corelib/corelib.pro +++ b/src/corelib/corelib.pro @@ -11,7 +11,7 @@ DEFINES += $$MODULE_DEFINES DEFINES += QT_NO_USING_NAMESPACE QT_NO_FOREACH win32-msvc*|win32-icc:QMAKE_LFLAGS += /BASE:0x67000000 -CONFIG += optimize_full +CONFIG += simd optimize_full QMAKE_DOCS = $$PWD/doc/qtcore.qdocconf diff --git a/src/corelib/global/global.pri b/src/corelib/global/global.pri index e1ca8828f9..f4f4a75536 100644 --- a/src/corelib/global/global.pri +++ b/src/corelib/global/global.pri @@ -39,6 +39,8 @@ SOURCES += \ global/qrandom.cpp \ global/qhooks.cpp +F16C_SOURCES += global/qfloat16_f16c.c + VERSIONTAGGING_SOURCES = global/qversiontagging.cpp darwin: SOURCES += global/qoperatingsystemversion_darwin.mm diff --git a/src/corelib/global/qcompilerdetection.h b/src/corelib/global/qcompilerdetection.h index 231ac2c9b0..50cf0889c7 100644 --- a/src/corelib/global/qcompilerdetection.h +++ b/src/corelib/global/qcompilerdetection.h @@ -651,6 +651,12 @@ # undef Q_COMPILER_CONSTEXPR # endif # endif +# elif defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L s +// C11 features supported. Only tested with ICC 17 and up. +# define Q_COMPILER_STATIC_ASSERT +# if __has_include(<threads.h>) +# define Q_COMPILER_THREAD_LOCAL +# endif # endif #endif @@ -803,6 +809,17 @@ # endif # endif +# if defined(__STDC_VERSION__) +# if __has_feature(c_static_assert) +# define Q_COMPILER_STATIC_ASSERT +# endif +# if __has_feature(c_thread_local) && __has_include(<threads.h>) +# if !defined(__FreeBSD__) /* FreeBSD clang fails on __cxa_thread_atexit */ +# define Q_COMPILER_THREAD_LOCAL +# endif +# endif +# endif + # if defined(__has_warning) # if __has_warning("-Wunused-private-field") # define Q_DECL_UNUSED_MEMBER Q_DECL_UNUSED @@ -898,6 +915,18 @@ # define Q_COMPILER_RETURN_TYPE_DEDUCTION # endif # endif +# if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L +# if Q_CC_GNU >= 407 + /* C11 features supported in GCC 4.7: */ +# define Q_COMPILER_STATIC_ASSERT +# endif +# if Q_CC_GNU >= 409 + /* C11 features supported in GCC 4.9: */ +# if __has_include(<threads.h>) +# define Q_COMPILER_THREAD_LOCAL +# endif +# endif +# endif #endif #if defined(Q_CC_MSVC) diff --git a/src/corelib/global/qfloat16.cpp b/src/corelib/global/qfloat16.cpp index 89edfc8787..4c9b831469 100644 --- a/src/corelib/global/qfloat16.cpp +++ b/src/corelib/global/qfloat16.cpp @@ -38,6 +38,7 @@ ****************************************************************************/ #include "qfloat16_p.h" +#include "private/qsimd_p.h" QT_BEGIN_NAMESPACE @@ -113,4 +114,88 @@ Q_REQUIRED_RESULT bool qIsFinite(qfloat16 f) Q_DECL_NOTHROW { return qt_is_finit exactness is stronger the smaller the numbers are. */ +#if QT_COMPILER_SUPPORTS(F16C) +static inline bool hasFastF16() +{ + // All processors with F16C also support AVX, but YMM registers + // might not be supported by the OS, or they might be disabled. + return qCpuHasFeature(F16C) && qCpuHasFeature(AVX); +} + +extern "C" { +extern void qFloatToFloat16_fast(quint16 *out, const float *in, qssize_t len) Q_DECL_NOTHROW; +extern void qFloatFromFloat16_fast(float *out, const quint16 *in, qssize_t len) Q_DECL_NOTHROW; +} + +#elif defined(__ARM_FP16_FORMAT_IEEE) && defined(__ARM_NEON__) +static inline bool hasFastF16() +{ + return true; +} + +static void qFloatToFloat16_fast(quint16 *out, const float *in, qssize_t len) Q_DECL_NOTHROW +{ + __fp16 *out_f16 = reinterpret_cast<__fp16 *>(out); + qssize_t i = 0; + for (; i < len - 3; i += 4) + vst1_f16(out_f16 + i, vcvt_f16_f32(vld1q_f32(in + i))); + SIMD_EPILOGUE(i, len, 3) + out_f16[i] = __fp16(in[i]); +} + +static void qFloatFromFloat16_fast(float *out, const quint16 *in, qssize_t len) Q_DECL_NOTHROW +{ + const __fp16 *in_f16 = reinterpret_cast<const __fp16 *>(in); + qssize_t i = 0; + for (; i < len - 3; i += 4) + vst1q_f32(out + i, vcvt_f32_f16(vld1_f16(in_f16 + i))); + SIMD_EPILOGUE(i, len, 3) + out[i] = float(in_f16[i]); +} +#else +static inline bool hasFastF16() +{ + return false; +} + +static void qFloatToFloat16_fast(quint16 *, const float *, qssize_t) Q_DECL_NOTHROW +{ + Q_UNREACHABLE(); +} + +static void qFloatFromFloat16_fast(float *, const quint16 *, qssize_t) Q_DECL_NOTHROW +{ + Q_UNREACHABLE(); +} +#endif +/*! + \since 5.11 + + Converts \a len floats from \a in to qfloat16 and stores them in \a out. + Both \a in and \a out must have \a len allocated entries. +*/ +Q_CORE_EXPORT void qFloatToFloat16(qfloat16 *out, const float *in, qssize_t len) Q_DECL_NOTHROW +{ + if (hasFastF16()) + return qFloatToFloat16_fast(reinterpret_cast<quint16 *>(out), in, len); + + for (qssize_t i = 0; i < len; ++i) + out[i] = qfloat16(in[i]); +} + +/*! + \since 5.11 + + Converts \a len qfloat16 from \a in to floats and stores them in \a out. + Both \a in and \a out must have \a len allocated entries. +*/ +Q_CORE_EXPORT void qFloatFromFloat16(float *out, const qfloat16 *in, qssize_t len) Q_DECL_NOTHROW +{ + if (hasFastF16()) + return qFloatFromFloat16_fast(out, reinterpret_cast<const quint16 *>(in), len); + + for (qssize_t i = 0; i < len; ++i) + out[i] = float(in[i]); +} + QT_END_NAMESPACE diff --git a/src/corelib/global/qfloat16.h b/src/corelib/global/qfloat16.h index a36852fc22..6b5c550b53 100644 --- a/src/corelib/global/qfloat16.h +++ b/src/corelib/global/qfloat16.h @@ -79,6 +79,9 @@ private: Q_DECLARE_TYPEINFO(qfloat16, Q_PRIMITIVE_TYPE); +Q_CORE_EXPORT void qFloatToFloat16(qfloat16 *, const float *, qssize_t length) Q_DECL_NOTHROW; +Q_CORE_EXPORT void qFloatFromFloat16(float *, const qfloat16 *, qssize_t length) Q_DECL_NOTHROW; + Q_REQUIRED_RESULT Q_CORE_EXPORT bool qIsInf(qfloat16 f) Q_DECL_NOTHROW; // complements qnumeric.h Q_REQUIRED_RESULT Q_CORE_EXPORT bool qIsNaN(qfloat16 f) Q_DECL_NOTHROW; // complements qnumeric.h Q_REQUIRED_RESULT Q_CORE_EXPORT bool qIsFinite(qfloat16 f) Q_DECL_NOTHROW; // complements qnumeric.h diff --git a/src/corelib/global/qfloat16_f16c.c b/src/corelib/global/qfloat16_f16c.c new file mode 100644 index 0000000000..ffd35f8ebc --- /dev/null +++ b/src/corelib/global/qfloat16_f16c.c @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** 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$ +** +****************************************************************************/ + +#include "private/qsimd_p.h" + +// The x86 F16C instructions operate on AVX registers, so AVX support is +// required. We don't need to check for __F16C__ because we this file wouldn't +// have been compiled if the support was missing in the first place, and not +// all compilers define it. Technically, we didn't need to check for __AVX__ +// either. +#if !defined(__AVX__) +# error "AVX support required" +#endif + +#ifdef __cplusplus +QT_BEGIN_NAMESPACE +extern "C" { +#endif + +void qFloatToFloat16_fast(quint16 *out, const float *in, qssize_t len) Q_DECL_NOTHROW +{ + qssize_t i = 0; + for (; i < len - 7; i += 8) + _mm_storeu_si128((__m128i *)(out + i), _mm256_cvtps_ph(_mm256_loadu_ps(in + i), 0)); + if (i < len - 3) { + _mm_storel_epi64((__m128i *)(out + i), _mm_cvtps_ph(_mm_loadu_ps(in + i), 0)); + i += 4; + } + // Inlining "qfloat16::qfloat16(float f)": + SIMD_EPILOGUE(i, len, 3) + out[i] = _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(in[i]), 0), 0); +} + +void qFloatFromFloat16_fast(float *out, const quint16 *in, qssize_t len) Q_DECL_NOTHROW +{ + qssize_t i = 0; + for (; i < len - 7; i += 8) + _mm256_storeu_ps(out + i, _mm256_cvtph_ps(_mm_loadu_si128((const __m128i *)(in + i)))); + if (i < len - 3) { + _mm_storeu_ps(out + i, _mm_cvtph_ps(_mm_loadl_epi64((const __m128i *)(in + i)))); + i += 4; + } + // Inlining "qfloat16::operator float()": + SIMD_EPILOGUE(i, len, 3) + out[i] = _mm_cvtss_f32(_mm_cvtph_ps(_mm_cvtsi32_si128(in[i]))); +} + +#ifdef __cplusplus +} // extern "C" +QT_END_NAMESPACE +#endif diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index e49bace002..203c9a9da4 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -47,6 +47,7 @@ # include <utility> #endif #ifndef __ASSEMBLER__ +# include <assert.h> # include <stddef.h> #endif @@ -105,6 +106,32 @@ # define Q_OF_MACH_O #endif +/* + Avoid "unused parameter" warnings +*/ +#define Q_UNUSED(x) (void)x; + +#if defined(__cplusplus) && defined(Q_COMPILER_STATIC_ASSERT) +# define Q_STATIC_ASSERT(Condition) static_assert(bool(Condition), #Condition) +# define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message) +#elif defined(Q_COMPILER_STATIC_ASSERT) +// C11 mode - using the _S version in case <assert.h> doesn't do the right thing +# define Q_STATIC_ASSERT(Condition) _Static_assert(!!(Condition), #Condition) +# define Q_STATIC_ASSERT_X(Condition, Message) _Static_assert(!!(Condition), Message) +#else +// C89 & C99 version +# define Q_STATIC_ASSERT_PRIVATE_JOIN(A, B) Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B) +# define Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B) A ## B +# ifdef __COUNTER__ +# define Q_STATIC_ASSERT(Condition) \ + typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) [(Condition) ? 1 : -1]; +# else +# define Q_STATIC_ASSERT(Condition) \ + typedef char Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __LINE__) [(Condition) ? 1 : -1]; +# endif /* __COUNTER__ */ +# define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition) +#endif + #ifdef __cplusplus #include <algorithm> @@ -219,6 +246,15 @@ typedef unsigned long long quint64; /* 64 bit unsigned */ typedef qint64 qlonglong; typedef quint64 qulonglong; +#ifndef __cplusplus +// In C++ mode, we define below using QIntegerForSize template +Q_STATIC_ASSERT_X(sizeof(ptrdiff_t) == sizeof(size_t), "Weird ptrdiff_t and size_t definitions"); +typedef ptrdiff_t qptrdiff; +typedef ptrdiff_t qssize_t; +typedef ptrdiff_t qintptr; +typedef size_t quintptr; +#endif + /* Useful type definitions for Qt */ @@ -688,11 +724,6 @@ Q_CORE_EXPORT Q_DECL_CONST_FUNCTION bool qSharedBuild() Q_DECL_NOTHROW; #endif /* - Avoid "unused parameter" warnings -*/ -#define Q_UNUSED(x) (void)x; - -/* Debugging and error handling */ @@ -750,27 +781,6 @@ Q_CORE_EXPORT void qt_assert_x(const char *where, const char *what, const char * # endif #endif - -#ifdef Q_COMPILER_STATIC_ASSERT -#define Q_STATIC_ASSERT(Condition) static_assert(bool(Condition), #Condition) -#define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message) -#else -// Intentionally undefined -template <bool Test> class QStaticAssertFailure; -template <> class QStaticAssertFailure<true> {}; - -#define Q_STATIC_ASSERT_PRIVATE_JOIN(A, B) Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B) -#define Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B) A ## B -#ifdef __COUNTER__ -#define Q_STATIC_ASSERT(Condition) \ - enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) = sizeof(QStaticAssertFailure<!!(Condition)>)} -#else -#define Q_STATIC_ASSERT(Condition) \ - enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __LINE__) = sizeof(QStaticAssertFailure<!!(Condition)>)} -#endif /* __COUNTER__ */ -#define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition) -#endif - Q_NORETURN Q_CORE_EXPORT void qt_check_pointer(const char *, int) Q_DECL_NOTHROW; Q_CORE_EXPORT void qBadAlloc(); diff --git a/src/corelib/tools/tools.pri b/src/corelib/tools/tools.pri index 2c609098ea..56b30279bd 100644 --- a/src/corelib/tools/tools.pri +++ b/src/corelib/tools/tools.pri @@ -223,4 +223,3 @@ TR_EXCLUDE += ../3rdparty/* # MIPS DSP MIPS_DSP_ASM += tools/qstring_mips_dsp_asm.S MIPS_DSP_HEADERS += ../gui/painting/qt_mips_asm_dsp_p.h -CONFIG += simd |