Chris@42: /* Chris@42: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@42: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@42: * Chris@42: * This program is free software; you can redistribute it and/or modify Chris@42: * it under the terms of the GNU General Public License as published by Chris@42: * the Free Software Foundation; either version 2 of the License, or Chris@42: * (at your option) any later version. Chris@42: * Chris@42: * This program is distributed in the hope that it will be useful, Chris@42: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@42: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@42: * GNU General Public License for more details. Chris@42: * Chris@42: * You should have received a copy of the GNU General Public License Chris@42: * along with this program; if not, write to the Free Software Chris@42: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@42: * Chris@42: */ Chris@42: Chris@42: Chris@42: #ifdef _MSC_VER Chris@42: #ifndef inline Chris@42: #define inline __inline Chris@42: #endif Chris@42: #endif Chris@42: Chris@42: #ifdef _MSC_VER Chris@42: #include Chris@42: #if (_MSC_VER >= 1600) && !defined(__INTEL_COMPILER) Chris@42: #include Chris@42: #endif Chris@42: #endif Chris@42: Chris@42: /* cpuid version to get all registers. Donated by Erik Lindahl from Gromacs. */ Chris@42: static inline void Chris@42: cpuid_all(int level, int ecxval, int *eax, int *ebx, int *ecx, int *edx) Chris@42: { Chris@42: # ifdef _MSC_VER Chris@42: int CPUInfo[4]; Chris@42: Chris@42: #if (_MSC_VER > 1500) || (_MSC_VER == 1500 & _MSC_FULL_VER >= 150030729) Chris@42: /* MSVC 9.0 SP1 or later */ Chris@42: __cpuidex(CPUInfo, op, ecxval); Chris@42: #else Chris@42: __cpuid(CPUInfo, level); Chris@42: #endif Chris@42: *eax = CPUInfo[0]; Chris@42: *ebx = CPUInfo[1]; Chris@42: *ecx = CPUInfo[2]; Chris@42: *edx = CPUInfo[3]; Chris@42: Chris@42: # else Chris@42: /* Not MSVC */ Chris@42: *eax = level; Chris@42: *ecx = ecxval; Chris@42: *ebx = 0; Chris@42: *edx = 0; Chris@42: /* No need to save ebx if we are not in pic mode */ Chris@42: __asm__ ("cpuid \n\t" Chris@42: : "+a" (*eax), "+b" (*ebx), "+c" (*ecx), "+d" (*edx)); Chris@42: # endif Chris@42: } Chris@42: Chris@42: Chris@42: static inline int cpuid_ecx(int op) Chris@42: { Chris@42: # ifdef _MSC_VER Chris@42: # ifdef __INTEL_COMPILER Chris@42: int result; Chris@42: _asm { Chris@42: push rbx Chris@42: mov eax,op Chris@42: cpuid Chris@42: mov result,ecx Chris@42: pop rbx Chris@42: } Chris@42: return result; Chris@42: # else Chris@42: int cpu_info[4]; Chris@42: __cpuid(cpu_info,op); Chris@42: return cpu_info[2]; Chris@42: # endif Chris@42: # else Chris@42: int eax, ecx = 0, edx; Chris@42: Chris@42: __asm__("pushq %%rbx\n\tcpuid\n\tpopq %%rbx" Chris@42: : "=a" (eax), "+c" (ecx), "=d" (edx) Chris@42: : "a" (op)); Chris@42: return ecx; Chris@42: # endif Chris@42: } Chris@42: Chris@42: static inline int cpuid_ebx(int op) Chris@42: { Chris@42: # ifdef _MSC_VER Chris@42: # ifdef __INTEL_COMPILER Chris@42: int result; Chris@42: _asm { Chris@42: push rbx Chris@42: mov eax,op Chris@42: cpuid Chris@42: mov result,ebx Chris@42: pop rbx Chris@42: } Chris@42: return result; Chris@42: # else Chris@42: int cpu_info[4]; Chris@42: __cpuid(cpu_info,op); Chris@42: return cpu_info[1]; Chris@42: # endif Chris@42: # else Chris@42: int eax, ecx = 0, edx; Chris@42: Chris@42: __asm__("pushq %%rbx\n\tcpuid\nmov %%ebx,%%ecx\n\tpopq %%rbx" Chris@42: : "=a" (eax), "+c" (ecx), "=d" (edx) Chris@42: : "a" (op)); Chris@42: return ecx; Chris@42: # endif Chris@42: } Chris@42: Chris@42: static inline int xgetbv_eax(int op) Chris@42: { Chris@42: # ifdef _MSC_VER Chris@42: # ifdef __INTEL_COMPILER Chris@42: int veax, vedx; Chris@42: _asm { Chris@42: mov ecx,op Chris@42: xgetbv Chris@42: mov veax,eax Chris@42: mov vedx,edx Chris@42: } Chris@42: return veax; Chris@42: # else Chris@42: # if defined(_MSC_VER) && (_MSC_VER >= 1600) Chris@42: unsigned __int64 result; Chris@42: result = _xgetbv(op); Chris@42: return (int)result; Chris@42: # else Chris@42: # error "Need at least Visual Studio 10 SP1 for AVX support" Chris@42: # endif Chris@42: # endif Chris@42: # else Chris@42: int eax, edx; Chris@42: __asm__ (".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c" (op)); Chris@42: return eax; Chris@42: #endif Chris@42: }