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