| 1 | // Copyright (C) 2020 The Qt Company Ltd. |
| 2 | // Copyright (C) 2022 Intel Corporation. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QSIMD_H |
| 6 | #define QSIMD_H |
| 7 | |
| 8 | #include <QtCore/qglobal.h> |
| 9 | |
| 10 | /* |
| 11 | * qconfig.h defines the QT_COMPILER_SUPPORTS_XXX macros. |
| 12 | * They mean the compiler supports the necessary flags and the headers |
| 13 | * for the x86 and ARM intrinsics. |
| 14 | * |
| 15 | * Supported instruction set extensions are: |
| 16 | * Flag | Arch |
| 17 | * neon | ARM |
| 18 | * mips_dsp | mips |
| 19 | * mips_dspr2 | mips |
| 20 | * sse2 | x86 |
| 21 | * sse4_1 | x86 |
| 22 | * sse4_2 | x86 |
| 23 | * avx | x86 |
| 24 | * |
| 25 | * Code can use the following constructs to determine compiler support & status: |
| 26 | * - #if QT_COMPILER_USES(XXX) (e.g: #if QT_COMPILER_USES(neon) or QT_COMPILER_USES(sse4_1) |
| 27 | * If this test passes, then the compiler is already generating code using the |
| 28 | * given instruction set. The intrinsics for those instructions are |
| 29 | * #included and can be used without restriction or runtime check. |
| 30 | * |
| 31 | * Code that requires runtime detection and different code paths at runtime is |
| 32 | * currently not supported here, have a look at qsimd_p.h for support. |
| 33 | */ |
| 34 | |
| 35 | #define QT_COMPILER_USES(feature) (1/QT_COMPILER_USES_##feature == 1) |
| 36 | |
| 37 | #if defined(Q_PROCESSOR_ARM) && defined(__ARM_NEON) || defined(__ARM_NEON__) || defined(_M_ARM64) |
| 38 | # include <arm_neon.h> |
| 39 | # define QT_COMPILER_USES_neon 1 |
| 40 | #else |
| 41 | # define QT_COMPILER_USES_neon -1 |
| 42 | #endif |
| 43 | |
| 44 | #if defined(Q_PROCESSOR_MIPS) && (defined(__MIPS_DSP__) || (defined(__mips_dsp) && defined(Q_PROCESSOR_MIPS_32))) |
| 45 | # define QT_COMPILER_USES_mips_dsp 1 |
| 46 | #else |
| 47 | # define QT_COMPILER_USES_mips_dsp -1 |
| 48 | #endif |
| 49 | |
| 50 | #if defined(Q_PROCESSOR_MIPS) && (defined(__MIPS_DSPR2__) || (defined(__mips_dspr2) && defined(Q_PROCESSOR_MIPS_32))) |
| 51 | # define QT_COMPILER_USES_mips_dspr2 1 |
| 52 | #else |
| 53 | # define QT_COMPILER_USES_mips_dspr2 -1 |
| 54 | #endif |
| 55 | |
| 56 | #if defined(Q_PROCESSOR_X86) && defined(Q_CC_MSVC) |
| 57 | // MSVC doesn't define __SSE2__, so do it ourselves |
| 58 | # if (defined(_M_X64) || _M_IX86_FP >= 2) && defined(QT_COMPILER_SUPPORTS_SSE2) |
| 59 | # define __SSE__ 1 |
| 60 | # define __SSE2__ 1 |
| 61 | # endif |
| 62 | # if (defined(_M_AVX) || defined(__AVX__)) |
| 63 | // Visual Studio defines __AVX__ when /arch:AVX is passed, but not the earlier macros |
| 64 | // See: https://msdn.microsoft.com/en-us/library/b0084kay.aspx |
| 65 | # define __SSE3__ 1 |
| 66 | # define __SSSE3__ 1 |
| 67 | # define __SSE4_1__ 1 |
| 68 | # define __SSE4_2__ 1 |
| 69 | # define __POPCNT__ 1 |
| 70 | # ifndef __AVX__ |
| 71 | # define __AVX__ 1 |
| 72 | # endif |
| 73 | # endif |
| 74 | # ifdef __SSE2__ |
| 75 | # define QT_VECTORCALL __vectorcall |
| 76 | # endif |
| 77 | # ifdef __AVX2__ |
| 78 | // MSVC defines __AVX2__ with /arch:AVX2 |
| 79 | # define __F16C__ 1 |
| 80 | # define __RDRND__ 1 |
| 81 | # define __FMA__ 1 |
| 82 | # define __BMI__ 1 |
| 83 | # define __BMI2__ 1 |
| 84 | # define __MOVBE__ 1 |
| 85 | # define __LZCNT__ 1 |
| 86 | # endif |
| 87 | // Starting with /arch:AVX512, MSVC defines all the macros |
| 88 | #endif |
| 89 | |
| 90 | #if defined(Q_PROCESSOR_X86) && defined(__SSE2__) |
| 91 | # include <immintrin.h> |
| 92 | # define QT_COMPILER_USES_sse2 1 |
| 93 | #else |
| 94 | # define QT_COMPILER_USES_sse2 -1 |
| 95 | #endif |
| 96 | |
| 97 | #if defined(Q_PROCESSOR_X86) && defined(__SSE3__) |
| 98 | # define QT_COMPILER_USES_sse3 1 |
| 99 | #else |
| 100 | # define QT_COMPILER_USES_sse3 -1 |
| 101 | #endif |
| 102 | |
| 103 | #if defined(Q_PROCESSOR_X86) && defined(__SSSE3__) |
| 104 | # define QT_COMPILER_USES_ssse3 1 |
| 105 | #else |
| 106 | # define QT_COMPILER_USES_ssse3 -1 |
| 107 | #endif |
| 108 | |
| 109 | #if defined(Q_PROCESSOR_X86) && defined(__SSE4_1__) |
| 110 | # define QT_COMPILER_USES_sse4_1 1 |
| 111 | #else |
| 112 | # define QT_COMPILER_USES_sse4_1 -1 |
| 113 | #endif |
| 114 | |
| 115 | #if defined(Q_PROCESSOR_X86) && defined(__SSE4_2__) |
| 116 | # define QT_COMPILER_USES_sse4_2 1 |
| 117 | #else |
| 118 | # define QT_COMPILER_USES_sse4_2 -1 |
| 119 | #endif |
| 120 | |
| 121 | #if defined(Q_PROCESSOR_X86) && defined(__AVX__) |
| 122 | # define QT_COMPILER_USES_avx 1 |
| 123 | #else |
| 124 | # define QT_COMPILER_USES_avx -1 |
| 125 | #endif |
| 126 | |
| 127 | #ifndef QT_VECTORCALL |
| 128 | #define QT_VECTORCALL |
| 129 | #endif |
| 130 | |
| 131 | QT_BEGIN_NAMESPACE |
| 132 | QT_END_NAMESPACE |
| 133 | |
| 134 | #endif // QSIMD_H |
| 135 | |