Line data Source code
1 : /*----------------------------------------------------------------------------*/ 2 : /* CP2K: A general program to perform molecular dynamics simulations */ 3 : /* Copyright 2000-2024 CP2K developers group <https://cp2k.org> */ 4 : /* */ 5 : /* SPDX-License-Identifier: GPL-2.0-or-later */ 6 : /*----------------------------------------------------------------------------*/ 7 : 8 : /* shared between C and Fortran */ 9 : #include "machine_cpuid.h" 10 : 11 : #if defined(__cplusplus) 12 : extern "C" { 13 : #endif 14 : 15 : /******************************************************************************* 16 : * \brief This routine determines the CPUID according to the given compiler 17 : * flags (expected to be similar to Fortran). Similar to other Fortran 18 : * compilers, "gfortran -E -dM -mavx - < /dev/null | grep AVX" defines a 19 : * variety of predefined macros (also similar to C). However, with a 20 : * Fortran translation unit only a subset of these definitions disappears 21 : * ("gfortran -E -dM -mavx my.F | grep AVX") 22 : * hence an implementation in C is used. 23 : ******************************************************************************/ 24 : int m_cpuid_static(void); /* avoid pedantic warning about missing prototype */ 25 4667 : int m_cpuid_static(void) { 26 : #if (defined(__x86_64__) && 0 != (__x86_64__)) || \ 27 : (defined(__amd64__) && 0 != (__amd64__)) || \ 28 : (defined(_M_X64) || defined(_M_AMD64)) || \ 29 : (defined(__i386__) && 0 != (__i386__)) || (defined(_M_IX86)) 30 : #if (__AVX512F__ && __AVX512CD__ && __AVX2__ && __FMA__ && __AVX__ && \ 31 : __SSE4_2__ && __SSE4_1__ && __SSE3__) 32 : return CP_MACHINE_X86_AVX512; 33 : #elif (__AVX2__ && __FMA__ && __AVX__ && __SSE4_2__ && __SSE4_1__ && __SSE3__) 34 4667 : return CP_MACHINE_X86_AVX2; 35 : #elif (__AVX__ && __SSE4_2__ && __SSE4_1__ && __SSE3__) 36 : return CP_MACHINE_X86_AVX; 37 : #elif (__SSE4_2__ && __SSE4_1__ && __SSE3__) 38 : return CP_MACHINE_X86_SSE4; 39 : #else 40 : return CP_MACHINE_CPU_GENERIC; 41 : #endif 42 : #elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) 43 : /* only care about SVE, embrace "thermometer approach" like for x86 */ 44 : #if (512 <= __ARM_FEATURE_SVE_BITS) 45 : return CP_MACHINE_ARM_SVE512; 46 : #elif (256 <= __ARM_FEATURE_SVE_BITS) 47 : return CP_MACHINE_ARM_SVE256; 48 : #elif (128 <= __ARM_FEATURE_SVE_BITS) 49 : return CP_MACHINE_ARM_SVE128; 50 : #else 51 : return CP_MACHINE_ARM_ARCH64; 52 : #endif 53 : #elif defined(__ARM_ARCH) 54 : return CP_MACHINE_CPU_GENERIC; 55 : #else 56 : return CP_MACHINE_UNKNOWN; 57 : #endif 58 : } 59 : 60 : #if defined(__cplusplus) 61 : } 62 : #endif