forked from nickbnf/glogg
-
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Check at startup for required cpu instructions (SSE2, SSSE3, POPCOUNT)
- Loading branch information
Showing
6 changed files
with
223 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) 2021 Anton Filimonov and other contributors | ||
* | ||
* This file is part of klogg. | ||
* | ||
* klogg is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* klogg is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with klogg. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef KLOGG_CPU_INFO_H | ||
#define KLOGG_CPU_INFO_H | ||
|
||
enum class CpuInstructions : unsigned { | ||
/// Indicates that no SIMD instructions are supported | ||
NONE = 0, | ||
SSE2 = 1 << 1, | ||
SSE3 = 1 << 2, | ||
SSSE3 = 1 << 3, | ||
SSE41 = 1 << 4, | ||
POPCNT = 1 << 5, | ||
AVX = 1 << 6, | ||
AVX2 = 1 << 7, | ||
}; | ||
|
||
inline CpuInstructions& operator|=( CpuInstructions& x, const CpuInstructions& y ) | ||
{ | ||
x = static_cast<CpuInstructions>( static_cast<unsigned>( x ) | static_cast<unsigned>( y ) ); | ||
return x; | ||
} | ||
|
||
inline CpuInstructions operator&( const CpuInstructions& x, const CpuInstructions& y ) | ||
{ | ||
return static_cast<CpuInstructions>( static_cast<unsigned>( x ) & static_cast<unsigned>( y ) ); | ||
} | ||
|
||
inline CpuInstructions operator~( const CpuInstructions& x ) | ||
{ | ||
return static_cast<CpuInstructions>( ~static_cast<unsigned>( x ) ); | ||
} | ||
|
||
inline bool hasRequiredInstructions( CpuInstructions current, CpuInstructions required ) | ||
{ | ||
if ( ( ~current & required ) == CpuInstructions::NONE ) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
CpuInstructions supportedCpuInstructions(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright (C) 2021 Anton Filimonov and other contributors | ||
* | ||
* This file is part of klogg. | ||
* | ||
* klogg is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* klogg is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with klogg. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "cpu_info.h" | ||
#include <QtGlobal> | ||
|
||
#if defined( Q_OS_WIN ) | ||
|
||
#include <array> | ||
#include <bitset> | ||
#include <vector> | ||
|
||
#include <intrin.h> | ||
|
||
CpuInstructions supportedCpuInstructions() | ||
{ | ||
CpuInstructions cpuInstructions = CpuInstructions::NONE; | ||
|
||
std::array<int, 4> cpui; | ||
|
||
// Calling __cpuid with 0x0 as the function_id argument | ||
// gets the number of the highest valid function ID. | ||
__cpuid( cpui.data(), 0 ); | ||
|
||
const auto nIds = cpui[ 0 ]; | ||
std::vector<std::array<int, 4>> data; | ||
|
||
for ( int i = 0; i <= nIds; ++i ) { | ||
__cpuidex( cpui.data(), i, 0 ); | ||
data.push_back( cpui ); | ||
} | ||
|
||
// load bitset with flags for function 0x00000001 | ||
if ( nIds >= 1 ) { | ||
std::bitset<32> f_1_ECX = data[ 1 ][ 2 ]; | ||
std::bitset<32> f_1_EDX = data[ 1 ][ 3 ]; | ||
|
||
if ( f_1_EDX[ 26 ] ) { | ||
cpuInstructions |= CpuInstructions::SSE2; | ||
} | ||
if ( f_1_ECX[ 0 ] ) { | ||
cpuInstructions |= CpuInstructions::SSE3; | ||
} | ||
if ( f_1_ECX[ 9 ] ) { | ||
cpuInstructions |= CpuInstructions::SSSE3; | ||
} | ||
if ( f_1_ECX[ 19 ] ) { | ||
cpuInstructions |= CpuInstructions::SSE41; | ||
} | ||
|
||
if ( f_1_ECX[ 23 ] ) { | ||
cpuInstructions |= CpuInstructions::POPCNT; | ||
} | ||
|
||
if ( f_1_ECX[ 28 ] ) { | ||
cpuInstructions |= CpuInstructions::AVX; | ||
} | ||
} | ||
|
||
// load bitset with flags for function 0x00000007 | ||
if ( nIds >= 7 ) { | ||
std::bitset<32> f_7_EBX = data[ 7 ][ 1 ]; | ||
|
||
if ( f_7_EBX[ 5 ] ) { | ||
cpuInstructions |= CpuInstructions::AVX2; | ||
} | ||
} | ||
|
||
return cpuInstructions; | ||
} | ||
#else | ||
CpuInstructions supportedCpuInstructions() | ||
{ | ||
CpuInstructions cpuInstructions = CpuInstructions::NONE; | ||
|
||
if ( __builtin_cpu_supports( "avx512f" ) ) { | ||
cpuInstructions |= CpuInstructions::SSE2; | ||
cpuInstructions |= CpuInstructions::SSE3; | ||
cpuInstructions |= CpuInstructions::SSSE3; | ||
cpuInstructions |= CpuInstructions::SSE41; | ||
cpuInstructions |= CpuInstructions::AVX; | ||
cpuInstructions |= CpuInstructions::AVX2; | ||
} | ||
else if ( __builtin_cpu_supports( "avx2" ) ) { | ||
cpuInstructions |= CpuInstructions::SSE2; | ||
cpuInstructions |= CpuInstructions::SSE3; | ||
cpuInstructions |= CpuInstructions::SSSE3; | ||
cpuInstructions |= CpuInstructions::SSE41; | ||
cpuInstructions |= CpuInstructions::AVX; | ||
cpuInstructions |= CpuInstructions::AVX2; | ||
} | ||
else if ( __builtin_cpu_supports( "avx" ) ) { | ||
cpuInstructions |= CpuInstructions::SSE2; | ||
cpuInstructions |= CpuInstructions::SSE3; | ||
cpuInstructions |= CpuInstructions::SSSE3; | ||
cpuInstructions |= CpuInstructions::SSE41; | ||
cpuInstructions |= CpuInstructions::AVX; | ||
} | ||
else if ( __builtin_cpu_supports( "sse4.1" ) ) { | ||
cpuInstructions |= CpuInstructions::SSE2; | ||
cpuInstructions |= CpuInstructions::SSE3; | ||
cpuInstructions |= CpuInstructions::SSSE3; | ||
cpuInstructions |= CpuInstructions::SSE41; | ||
} | ||
else if ( __builtin_cpu_supports( "ssse3" ) ) { | ||
cpuInstructions |= CpuInstructions::SSE2; | ||
cpuInstructions |= CpuInstructions::SSE3; | ||
cpuInstructions |= CpuInstructions::SSSE3; | ||
} | ||
else if ( __builtin_cpu_supports( "sse3" ) ) { | ||
cpuInstructions |= CpuInstructions::SSE3; | ||
cpuInstructions |= CpuInstructions::SSE2; | ||
} | ||
else if ( __builtin_cpu_supports( "sse2" ) ) { | ||
cpuInstructions |= CpuInstructions::SSE2; | ||
} | ||
if ( __builtin_cpu_supports( "popcnt" ) ) { | ||
cpuInstructions |= CpuInstructions::POPCNT; | ||
} | ||
return cpuInstructions; | ||
} | ||
|
||
#endif |