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