1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libsoundtouch/src/cpu_detect_x86.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,138 @@ 1.4 +//////////////////////////////////////////////////////////////////////////////// 1.5 +/// 1.6 +/// Generic version of the x86 CPU extension detection routine. 1.7 +/// 1.8 +/// This file is for GNU & other non-Windows compilers, see 'cpu_detect_x86_win.cpp' 1.9 +/// for the Microsoft compiler version. 1.10 +/// 1.11 +/// Author : Copyright (c) Olli Parviainen 1.12 +/// Author e-mail : oparviai 'at' iki.fi 1.13 +/// SoundTouch WWW: http://www.surina.net/soundtouch 1.14 +/// 1.15 +//////////////////////////////////////////////////////////////////////////////// 1.16 +// 1.17 +// Last changed : $Date: 2014-01-07 12:24:28 -0600 (Tue, 07 Jan 2014) $ 1.18 +// File revision : $Revision: 4 $ 1.19 +// 1.20 +// $Id: cpu_detect_x86.cpp 183 2014-01-07 18:24:28Z oparviai $ 1.21 +// 1.22 +//////////////////////////////////////////////////////////////////////////////// 1.23 +// 1.24 +// License : 1.25 +// 1.26 +// SoundTouch audio processing library 1.27 +// Copyright (c) Olli Parviainen 1.28 +// 1.29 +// This library is free software; you can redistribute it and/or 1.30 +// modify it under the terms of the GNU Lesser General Public 1.31 +// License as published by the Free Software Foundation; either 1.32 +// version 2.1 of the License, or (at your option) any later version. 1.33 +// 1.34 +// This library is distributed in the hope that it will be useful, 1.35 +// but WITHOUT ANY WARRANTY; without even the implied warranty of 1.36 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1.37 +// Lesser General Public License for more details. 1.38 +// 1.39 +// You should have received a copy of the GNU Lesser General Public 1.40 +// License along with this library; if not, write to the Free Software 1.41 +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 1.42 +// 1.43 +//////////////////////////////////////////////////////////////////////////////// 1.44 + 1.45 +#include "cpu_detect.h" 1.46 +#include "STTypes.h" 1.47 + 1.48 + 1.49 +#if defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS) 1.50 + #if defined(__GNUC__) && defined(HAVE_CPUID_H) 1.51 + // gcc and clang 1.52 + #include "cpuid.h" 1.53 + #elif defined(_M_IX86) 1.54 + // windows non-gcc 1.55 + #include <intrin.h> 1.56 + #endif 1.57 + 1.58 + #define bit_MMX (1 << 23) 1.59 + #define bit_SSE (1 << 25) 1.60 + #define bit_SSE2 (1 << 26) 1.61 +#endif 1.62 + 1.63 + 1.64 +////////////////////////////////////////////////////////////////////////////// 1.65 +// 1.66 +// processor instructions extension detection routines 1.67 +// 1.68 +////////////////////////////////////////////////////////////////////////////// 1.69 + 1.70 +// Flag variable indicating whick ISA extensions are disabled (for debugging) 1.71 +static uint _dwDisabledISA = 0x00; // 0xffffffff; //<- use this to disable all extensions 1.72 + 1.73 +// Disables given set of instruction extensions. See SUPPORT_... defines. 1.74 +void disableExtensions(uint dwDisableMask) 1.75 +{ 1.76 + _dwDisabledISA = dwDisableMask; 1.77 +} 1.78 + 1.79 + 1.80 + 1.81 +/// Checks which instruction set extensions are supported by the CPU. 1.82 +uint detectCPUextensions(void) 1.83 +{ 1.84 +/// If building for a 64bit system (no Itanium) and the user wants optimizations. 1.85 +/// Return the OR of SUPPORT_{MMX,SSE,SSE2}. 11001 or 0x19. 1.86 +/// Keep the _dwDisabledISA test (2 more operations, could be eliminated). 1.87 +#if ((defined(__GNUC__) && defined(__x86_64__)) \ 1.88 + || defined(_M_X64)) \ 1.89 + && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS) 1.90 + return 0x19 & ~_dwDisabledISA; 1.91 + 1.92 +/// If building for a 32bit system and the user wants optimizations. 1.93 +/// Keep the _dwDisabledISA test (2 more operations, could be eliminated). 1.94 +#elif ((defined(__GNUC__) && defined(__i386__)) \ 1.95 + || defined(_M_IX86)) \ 1.96 + && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS) 1.97 + 1.98 + if (_dwDisabledISA == 0xffffffff) return 0; 1.99 + 1.100 + uint res = 0; 1.101 + 1.102 +#if !defined(__GNUC__) 1.103 + // Window / VS version of cpuid. Notice that Visual Studio 2005 or later required 1.104 + // for __cpuid intrinsic support. 1.105 + int reg[4] = {-1}; 1.106 + 1.107 + // Check if no cpuid support. 1.108 + __cpuid(reg,0); 1.109 + if ((unsigned int)reg[0] == 0) return 0; // always disable extensions. 1.110 + 1.111 + __cpuid(reg,1); 1.112 + if ((unsigned int)reg[3] & bit_MMX) res = res | SUPPORT_MMX; 1.113 + if ((unsigned int)reg[3] & bit_SSE) res = res | SUPPORT_SSE; 1.114 + if ((unsigned int)reg[3] & bit_SSE2) res = res | SUPPORT_SSE2; 1.115 +#elif defined(HAVE_CPUID_H) 1.116 + // GCC version of cpuid. Requires GCC 4.3.0 or later for __cpuid intrinsic support. 1.117 + uint eax, ebx, ecx, edx; // unsigned int is the standard type. uint is defined by the compiler and not guaranteed to be portable. 1.118 + 1.119 + // Check if no cpuid support. 1.120 + if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx)) return 0; // always disable extensions. 1.121 + 1.122 + if (edx & bit_MMX) res = res | SUPPORT_MMX; 1.123 + if (edx & bit_SSE) res = res | SUPPORT_SSE; 1.124 + if (edx & bit_SSE2) res = res | SUPPORT_SSE2; 1.125 +#else 1.126 + // Compatible with GCC but no cpuid.h. 1.127 + return 0; 1.128 +#endif 1.129 + 1.130 + return res & ~_dwDisabledISA; 1.131 + 1.132 +#else 1.133 + 1.134 +/// One of these is true: 1.135 +/// 1) We don't want optimizations. 1.136 +/// 2) Using an unsupported compiler. 1.137 +/// 3) Running on a non-x86 platform. 1.138 + return 0; 1.139 + 1.140 +#endif 1.141 +}