michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: /// michael@0: /// Generic version of the x86 CPU extension detection routine. michael@0: /// michael@0: /// This file is for GNU & other non-Windows compilers, see 'cpu_detect_x86_win.cpp' michael@0: /// for the Microsoft compiler version. michael@0: /// michael@0: /// Author : Copyright (c) Olli Parviainen michael@0: /// Author e-mail : oparviai 'at' iki.fi michael@0: /// SoundTouch WWW: http://www.surina.net/soundtouch michael@0: /// michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // Last changed : $Date: 2014-01-07 12:24:28 -0600 (Tue, 07 Jan 2014) $ michael@0: // File revision : $Revision: 4 $ michael@0: // michael@0: // $Id: cpu_detect_x86.cpp 183 2014-01-07 18:24:28Z oparviai $ michael@0: // michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // License : michael@0: // michael@0: // SoundTouch audio processing library michael@0: // Copyright (c) Olli Parviainen michael@0: // michael@0: // This library is free software; you can redistribute it and/or michael@0: // modify it under the terms of the GNU Lesser General Public michael@0: // License as published by the Free Software Foundation; either michael@0: // version 2.1 of the License, or (at your option) any later version. michael@0: // michael@0: // This library is distributed in the hope that it will be useful, michael@0: // but WITHOUT ANY WARRANTY; without even the implied warranty of michael@0: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU michael@0: // Lesser General Public License for more details. michael@0: // michael@0: // You should have received a copy of the GNU Lesser General Public michael@0: // License along with this library; if not, write to the Free Software michael@0: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA michael@0: // michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "cpu_detect.h" michael@0: #include "STTypes.h" michael@0: michael@0: michael@0: #if defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS) michael@0: #if defined(__GNUC__) && defined(HAVE_CPUID_H) michael@0: // gcc and clang michael@0: #include "cpuid.h" michael@0: #elif defined(_M_IX86) michael@0: // windows non-gcc michael@0: #include michael@0: #endif michael@0: michael@0: #define bit_MMX (1 << 23) michael@0: #define bit_SSE (1 << 25) michael@0: #define bit_SSE2 (1 << 26) michael@0: #endif michael@0: michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // processor instructions extension detection routines michael@0: // michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // Flag variable indicating whick ISA extensions are disabled (for debugging) michael@0: static uint _dwDisabledISA = 0x00; // 0xffffffff; //<- use this to disable all extensions michael@0: michael@0: // Disables given set of instruction extensions. See SUPPORT_... defines. michael@0: void disableExtensions(uint dwDisableMask) michael@0: { michael@0: _dwDisabledISA = dwDisableMask; michael@0: } michael@0: michael@0: michael@0: michael@0: /// Checks which instruction set extensions are supported by the CPU. michael@0: uint detectCPUextensions(void) michael@0: { michael@0: /// If building for a 64bit system (no Itanium) and the user wants optimizations. michael@0: /// Return the OR of SUPPORT_{MMX,SSE,SSE2}. 11001 or 0x19. michael@0: /// Keep the _dwDisabledISA test (2 more operations, could be eliminated). michael@0: #if ((defined(__GNUC__) && defined(__x86_64__)) \ michael@0: || defined(_M_X64)) \ michael@0: && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS) michael@0: return 0x19 & ~_dwDisabledISA; michael@0: michael@0: /// If building for a 32bit system and the user wants optimizations. michael@0: /// Keep the _dwDisabledISA test (2 more operations, could be eliminated). michael@0: #elif ((defined(__GNUC__) && defined(__i386__)) \ michael@0: || defined(_M_IX86)) \ michael@0: && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS) michael@0: michael@0: if (_dwDisabledISA == 0xffffffff) return 0; michael@0: michael@0: uint res = 0; michael@0: michael@0: #if !defined(__GNUC__) michael@0: // Window / VS version of cpuid. Notice that Visual Studio 2005 or later required michael@0: // for __cpuid intrinsic support. michael@0: int reg[4] = {-1}; michael@0: michael@0: // Check if no cpuid support. michael@0: __cpuid(reg,0); michael@0: if ((unsigned int)reg[0] == 0) return 0; // always disable extensions. michael@0: michael@0: __cpuid(reg,1); michael@0: if ((unsigned int)reg[3] & bit_MMX) res = res | SUPPORT_MMX; michael@0: if ((unsigned int)reg[3] & bit_SSE) res = res | SUPPORT_SSE; michael@0: if ((unsigned int)reg[3] & bit_SSE2) res = res | SUPPORT_SSE2; michael@0: #elif defined(HAVE_CPUID_H) michael@0: // GCC version of cpuid. Requires GCC 4.3.0 or later for __cpuid intrinsic support. michael@0: uint eax, ebx, ecx, edx; // unsigned int is the standard type. uint is defined by the compiler and not guaranteed to be portable. michael@0: michael@0: // Check if no cpuid support. michael@0: if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx)) return 0; // always disable extensions. michael@0: michael@0: if (edx & bit_MMX) res = res | SUPPORT_MMX; michael@0: if (edx & bit_SSE) res = res | SUPPORT_SSE; michael@0: if (edx & bit_SSE2) res = res | SUPPORT_SSE2; michael@0: #else michael@0: // Compatible with GCC but no cpuid.h. michael@0: return 0; michael@0: #endif michael@0: michael@0: return res & ~_dwDisabledISA; michael@0: michael@0: #else michael@0: michael@0: /// One of these is true: michael@0: /// 1) We don't want optimizations. michael@0: /// 2) Using an unsupported compiler. michael@0: /// 3) Running on a non-x86 platform. michael@0: return 0; michael@0: michael@0: #endif michael@0: }