| 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 | * sve | ARM |
| 19 | * mips_dsp | mips |
| 20 | * mips_dspr2 | mips |
| 21 | * sse2 | x86 |
| 22 | * sse4_1 | x86 |
| 23 | * sse4_2 | x86 |
| 24 | * avx | x86 |
| 25 | * avx2 | x86 |
| 26 | * lsx | loongarch |
| 27 | * lasx | loongarch |
| 28 | * |
| 29 | * Code can use the following constructs to determine compiler support & status: |
| 30 | * - #if QT_COMPILER_USES(XXX) (e.g: #if QT_COMPILER_USES(neon) or QT_COMPILER_USES(sse4_1) |
| 31 | * If this test passes, then the compiler is already generating code using the |
| 32 | * given instruction set. The intrinsics for those instructions are |
| 33 | * #included and can be used without restriction or runtime check. |
| 34 | * |
| 35 | * Code that requires runtime detection and different code paths at runtime is |
| 36 | * currently not supported here, have a look at qsimd_p.h for support. |
| 37 | */ |
| 38 | |
| 39 | #define QT_COMPILER_USES(feature) (1/QT_COMPILER_USES_##feature == 1) |
| 40 | |
| 41 | #if defined(Q_PROCESSOR_ARM) && defined(__ARM_NEON) || defined(__ARM_NEON__) || defined(_M_ARM64) |
| 42 | # include <arm_neon.h> |
| 43 | # define QT_COMPILER_USES_neon 1 |
| 44 | #else |
| 45 | # define QT_COMPILER_USES_neon -1 |
| 46 | #endif |
| 47 | |
| 48 | // To avoid to many untestable fringe cases we so far only support 64bit LE in SVE code |
| 49 | // The test for QT_COMPILER_SUPPORTS_SVE ensures the intrinsics exists |
| 50 | #if defined(Q_PROCESSOR_ARM_64) && defined(__ARM_FEATURE_SVE) && defined(Q_LITTLE_ENDIAN) && defined(QT_COMPILER_SUPPORTS_SVE) |
| 51 | # include <arm_sve.h> |
| 52 | # define QT_COMPILER_USES_sve 1 |
| 53 | #else |
| 54 | # define QT_COMPILER_USES_sve -1 |
| 55 | #endif |
| 56 | |
| 57 | #if defined(Q_PROCESSOR_MIPS) && (defined(__MIPS_DSP__) || (defined(__mips_dsp) && defined(Q_PROCESSOR_MIPS_32))) |
| 58 | # define QT_COMPILER_USES_mips_dsp 1 |
| 59 | #else |
| 60 | # define QT_COMPILER_USES_mips_dsp -1 |
| 61 | #endif |
| 62 | |
| 63 | #if defined(Q_PROCESSOR_MIPS) && (defined(__MIPS_DSPR2__) || (defined(__mips_dspr2) && defined(Q_PROCESSOR_MIPS_32))) |
| 64 | # define QT_COMPILER_USES_mips_dspr2 1 |
| 65 | #else |
| 66 | # define QT_COMPILER_USES_mips_dspr2 -1 |
| 67 | #endif |
| 68 | |
| 69 | #if defined(Q_PROCESSOR_LOONGARCH) && defined(__loongarch_sx) |
| 70 | # include <lsxintrin.h> |
| 71 | # define QT_COMPILER_USES_lsx 1 |
| 72 | #else |
| 73 | # define QT_COMPILER_USES_lsx -1 |
| 74 | #endif |
| 75 | |
| 76 | #if defined(Q_PROCESSOR_LOONGARCH) && defined(__loongarch_asx) |
| 77 | # include <lasxintrin.h> |
| 78 | # define QT_COMPILER_USES_lasx 1 |
| 79 | #else |
| 80 | # define QT_COMPILER_USES_lasx -1 |
| 81 | #endif |
| 82 | |
| 83 | #if defined(Q_PROCESSOR_X86) && defined(Q_CC_MSVC) |
| 84 | // MSVC doesn't define __SSE2__, so do it ourselves |
| 85 | # if (defined(_M_X64) || _M_IX86_FP >= 2) && defined(QT_COMPILER_SUPPORTS_SSE2) |
| 86 | # define __SSE__ 1 |
| 87 | # define __SSE2__ 1 |
| 88 | # endif |
| 89 | # if (defined(_M_AVX) || defined(__AVX__)) |
| 90 | // Visual Studio defines __AVX__ when /arch:AVX is passed, but not the earlier macros |
| 91 | // See: https://msdn.microsoft.com/en-us/library/b0084kay.aspx |
| 92 | # define __SSE3__ 1 |
| 93 | # define __SSSE3__ 1 |
| 94 | # define __SSE4_1__ 1 |
| 95 | # define __SSE4_2__ 1 |
| 96 | # define __POPCNT__ 1 |
| 97 | # ifndef __AVX__ |
| 98 | # define __AVX__ 1 |
| 99 | # endif |
| 100 | # endif |
| 101 | # ifdef __SSE2__ |
| 102 | # define QT_VECTORCALL __vectorcall |
| 103 | # endif |
| 104 | # ifdef __AVX2__ |
| 105 | // MSVC defines __AVX2__ with /arch:AVX2 |
| 106 | # define __F16C__ 1 |
| 107 | # define __RDRND__ 1 |
| 108 | # define __FMA__ 1 |
| 109 | # define __BMI__ 1 |
| 110 | # define __BMI2__ 1 |
| 111 | # define __MOVBE__ 1 |
| 112 | # define __LZCNT__ 1 |
| 113 | # endif |
| 114 | // Starting with /arch:AVX512, MSVC defines all the macros |
| 115 | #endif |
| 116 | |
| 117 | #if defined(Q_PROCESSOR_X86) && defined(__SSE2__) |
| 118 | # include <immintrin.h> |
| 119 | # define QT_COMPILER_USES_sse2 1 |
| 120 | #else |
| 121 | # define QT_COMPILER_USES_sse2 -1 |
| 122 | #endif |
| 123 | |
| 124 | #if defined(Q_PROCESSOR_X86) && defined(__SSE3__) |
| 125 | # define QT_COMPILER_USES_sse3 1 |
| 126 | #else |
| 127 | # define QT_COMPILER_USES_sse3 -1 |
| 128 | #endif |
| 129 | |
| 130 | #if defined(Q_PROCESSOR_X86) && defined(__SSSE3__) |
| 131 | # define QT_COMPILER_USES_ssse3 1 |
| 132 | #else |
| 133 | # define QT_COMPILER_USES_ssse3 -1 |
| 134 | #endif |
| 135 | |
| 136 | #if defined(Q_PROCESSOR_X86) && defined(__SSE4_1__) |
| 137 | # define QT_COMPILER_USES_sse4_1 1 |
| 138 | #else |
| 139 | # define QT_COMPILER_USES_sse4_1 -1 |
| 140 | #endif |
| 141 | |
| 142 | #if defined(Q_PROCESSOR_X86) && defined(__SSE4_2__) |
| 143 | # define QT_COMPILER_USES_sse4_2 1 |
| 144 | #else |
| 145 | # define QT_COMPILER_USES_sse4_2 -1 |
| 146 | #endif |
| 147 | |
| 148 | #if defined(Q_PROCESSOR_X86) && defined(__AVX__) |
| 149 | # define QT_COMPILER_USES_avx 1 |
| 150 | #else |
| 151 | # define QT_COMPILER_USES_avx -1 |
| 152 | #endif |
| 153 | |
| 154 | #if defined(Q_PROCESSOR_X86) && defined(__AVX2__) |
| 155 | # define QT_COMPILER_USES_avx2 1 |
| 156 | #else |
| 157 | # define QT_COMPILER_USES_avx2 -1 |
| 158 | #endif |
| 159 | |
| 160 | #ifndef QT_VECTORCALL |
| 161 | #define QT_VECTORCALL |
| 162 | #endif |
| 163 | |
| 164 | QT_BEGIN_NAMESPACE |
| 165 | QT_END_NAMESPACE |
| 166 | |
| 167 | #endif // QSIMD_H |
| 168 | |