Skip to content

Commit

Permalink
x86: Add detection of the AES and SHA New Instructions
Browse files Browse the repository at this point in the history
The AES instructions were first introduced with the Westmere shrink
(22nm) of the Nehalem architecture. The SHA instructions are still
pending on Intel architecture, but is available on AMD family 17h (gcc
argument -march=znver1).

Both features operate on SSE registers, so that's why the MSVC command-
line argument is the SSE2 one and the configure-time tests depend on
features.sse2.

The qmake feature names end in "ni" because "aes" and "sha" are too
simple and could clash with other uses. The QT_COMPILER_SUPPORTS_ macro
doesn't have the "NI" suffix because it has to match the GCC/Clang
predefined macro.

Change-Id: I445bb15619f6401494e8fffd149dbd1f862ff51c
Reviewed-by: Allan Sandfeld Jensen <[email protected]>
  • Loading branch information
thiagomacieira committed Feb 21, 2017
1 parent db1c855 commit 0f1b6ac
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 1 deletion.
49 changes: 49 additions & 0 deletions config.tests/common/aesni/aesni.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/****************************************************************************
**
** Copyright (C) 2017 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the config.tests 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 <wmmintrin.h>

int main(int, char**)
{
__m128i a = _mm_setzero_si128();
__m128i b = _mm_aesenc_si128(a, a);
__m128i c = _mm_aesdec_si128(a, b);
(void)c;
return 0;
}
3 changes: 3 additions & 0 deletions config.tests/common/aesni/aesni.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SOURCES = aesni.cpp
!defined(QMAKE_CFLAGS_AESNI, "var"): error("This compiler does not support AES New Instructions")
else: QMAKE_CXXFLAGS += $$QMAKE_CFLAGS_AESNI
50 changes: 50 additions & 0 deletions config.tests/common/shani/shani.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/****************************************************************************
**
** Copyright (C) 2017 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the config.tests 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 <immintrin.h>

int main(int, char**)
{
__m128i a = _mm_setzero_si128();
__m128i b = _mm_sha1rnds4_epu32(a, a, 0);
__m128i c = _mm_sha1msg1_epu32(a, b);
__m128i d = _mm_sha256msg2_epu32(b, c);
(void)d;
return 0;
}
3 changes: 3 additions & 0 deletions config.tests/common/shani/shani.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SOURCES = shani.cpp
!defined(QMAKE_CFLAGS_SHANI, "var"): error("This compiler does not support Secure Hash Algorithm extensions")
else: QMAKE_CXXFLAGS += $$QMAKE_CFLAGS_SHANI
26 changes: 26 additions & 0 deletions configure.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,21 @@
"type": "compile",
"test": "common/sse4_2"
},
"aesni": {
"label": "AES new instructions",
"type": "compile",
"test": "common/aesni"
},
"f16c": {
"label": "F16C instructions",
"type": "compile",
"test": "common/f16c"
},
"shani": {
"label": "SHA new instructions",
"type": "compile",
"test": "common/shani"
},
"avx": {
"label": "AVX instructions",
"type": "compile",
Expand Down Expand Up @@ -871,6 +881,22 @@
{ "type": "define", "name": "QT_COMPILER_SUPPORTS_AVX512VBMI", "value": 1 }
]
},
"aesni": {
"label": "aesni",
"condition": "features.sse2 && tests.aesni",
"output": [
"privateConfig",
{ "type": "define", "name": "QT_COMPILER_SUPPORTS_AES", "value": 1 }
]
},
"shani": {
"label": "shani",
"condition": "features.sse2 && tests.shani",
"output": [
"privateConfig",
{ "type": "define", "name": "QT_COMPILER_SUPPORTS_SHA", "value": 1 }
]
},
"mips_dsp": {
"label": "DSP",
"condition": "arch.mips && tests.mips_dsp",
Expand Down
2 changes: 2 additions & 0 deletions mkspecs/common/gcc-base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ QMAKE_CFLAGS_AVX512BW += -mavx512bw
QMAKE_CFLAGS_AVX512VL += -mavx512vl
QMAKE_CFLAGS_AVX512IFMA += -mavx512ifma
QMAKE_CFLAGS_AVX512VBMI += -mavx512vbmi
QMAKE_CFLAGS_AESNI += -maes
QMAKE_CFLAGS_SHANI += -msha
QMAKE_CFLAGS_NEON += -mfpu=neon
QMAKE_CFLAGS_MIPS_DSP += -mdsp
QMAKE_CFLAGS_MIPS_DSPR2 += -mdspr2
Expand Down
2 changes: 2 additions & 0 deletions mkspecs/common/msvc-desktop.conf
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ QMAKE_CFLAGS_SSE3 = $$QMAKE_CFLAGS_SSE2
QMAKE_CFLAGS_SSSE3 = $$QMAKE_CFLAGS_SSE2
QMAKE_CFLAGS_SSE4_1 = $$QMAKE_CFLAGS_SSE2
QMAKE_CFLAGS_SSE4_2 = $$QMAKE_CFLAGS_SSE2
QMAKE_CFLAGS_AESNI = $$QMAKE_CFLAGS_SSE2
QMAKE_CFLAGS_SHANI = $$QMAKE_CFLAGS_SSE2

QMAKE_CXX = $$QMAKE_CC
QMAKE_CXXFLAGS = $$QMAKE_CFLAGS
Expand Down
2 changes: 2 additions & 0 deletions mkspecs/common/qcc-base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ QMAKE_CFLAGS_SSE4_1 += -msse4.1
QMAKE_CFLAGS_SSE4_2 += -msse4.2
QMAKE_CFLAGS_AVX += -mavx
QMAKE_CFLAGS_AVX2 += -mavx2
QMAKE_CFLAGS_AESNI += -maes
QMAKE_CFLAGS_SHANI += -msha

QMAKE_CXXFLAGS += $$QMAKE_CFLAGS -lang-c++
QMAKE_CXXFLAGS_DEPS += $$QMAKE_CFLAGS_DEPS
Expand Down
2 changes: 2 additions & 0 deletions mkspecs/common/winrt_winphone/qmake.conf
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ QMAKE_CFLAGS_SSE4_1 = -arch:SSE2
QMAKE_CFLAGS_SSE4_2 = -arch:SSE2
QMAKE_CFLAGS_AVX = -arch:AVX
QMAKE_CFLAGS_AVX2 = -arch:AVX
QMAKE_CFLAGS_AESNI = -arch:SSE2
QMAKE_CFLAGS_SHANI = -arch:SSE2

QMAKE_CXX = $$QMAKE_CC
QMAKE_CXXFLAGS = $$QMAKE_CFLAGS
Expand Down
2 changes: 2 additions & 0 deletions mkspecs/features/simd.prf
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ addSimdCompiler(sse3)
addSimdCompiler(ssse3)
addSimdCompiler(sse4_1)
addSimdCompiler(sse4_2)
addSimdCompiler(aesni)
addSimdCompiler(shani)
addSimdCompiler(avx)
addSimdCompiler(avx2)
addSimdCompiler(avx512f)
Expand Down
2 changes: 2 additions & 0 deletions mkspecs/linux-icc/qmake.conf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ QMAKE_CFLAGS_AVX512PF += -xMIC-AVX512
QMAKE_CFLAGS_AVX512DQ += -xCORE-AVX512
QMAKE_CFLAGS_AVX512BW += -xCORE-AVX512
QMAKE_CFLAGS_AVX512VL += -xCORE-AVX512
QMAKE_CFLAGS_AESNI += -maes
QMAKE_CFLAGS_SHANI += -msha

QMAKE_CXX = icpc
QMAKE_CXXFLAGS = $$QMAKE_CFLAGS
Expand Down
2 changes: 2 additions & 0 deletions mkspecs/macx-icc/qmake.conf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ QMAKE_CFLAGS_AVX512PF += -xMIC-AVX512
QMAKE_CFLAGS_AVX512DQ += -xCORE-AVX512
QMAKE_CFLAGS_AVX512BW += -xCORE-AVX512
QMAKE_CFLAGS_AVX512VL += -xCORE-AVX512
QMAKE_CFLAGS_AESNI += -maes
QMAKE_CFLAGS_SHANI += -msha

QMAKE_CXX = icpc
QMAKE_CXXFLAGS = $$QMAKE_CFLAGS
Expand Down
2 changes: 2 additions & 0 deletions mkspecs/win32-g++/qmake.conf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ QMAKE_YACCFLAGS = -d
QMAKE_CFLAGS += -fno-keep-inline-dllexport
QMAKE_CFLAGS_WARN_ON += -Wextra
QMAKE_CFLAGS_SSE2 += -mstackrealign
QMAKE_CFLAGS_AESNI = -maes
QMAKE_CFLAGS_SHANI = -msha

QMAKE_CXX = $${CROSS_COMPILE}g++
QMAKE_CXXFLAGS_THREAD = $$QMAKE_CFLAGS_THREAD
Expand Down
2 changes: 2 additions & 0 deletions mkspecs/win32-icc/qmake.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ QMAKE_CFLAGS_AVX512PF += -QxMIC-AVX512
QMAKE_CFLAGS_AVX512DQ += -QxCORE-AVX512
QMAKE_CFLAGS_AVX512BW += -QxCORE-AVX512
QMAKE_CFLAGS_AVX512VL += -QxCORE-AVX512
QMAKE_CFLAGS_AESNI = -QxSSE2
QMAKE_CFLAGS_SHANI = -QxSSE4.2

QMAKE_CXX = $$QMAKE_CC
QMAKE_CXXFLAGS = $$QMAKE_CFLAGS /Zc:forScope
Expand Down
2 changes: 1 addition & 1 deletion src/corelib/tools/qsimd_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
# endif
#endif

#if defined(__AES__) || defined(__PCLMUL__)
#if defined(__AES__) || defined(__PCLMUL__) || (defined(QT_COMPILER_SUPPORTS_AES) && defined(QT_COMPILER_SUPPORTS_SIMD_ALWAYS))
# include <wmmintrin.h>
#endif

Expand Down

0 comments on commit 0f1b6ac

Please sign in to comment.