media/libsoundtouch/src/cpu_detect_x86.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 ////////////////////////////////////////////////////////////////////////////////
michael@0 2 ///
michael@0 3 /// Generic version of the x86 CPU extension detection routine.
michael@0 4 ///
michael@0 5 /// This file is for GNU & other non-Windows compilers, see 'cpu_detect_x86_win.cpp'
michael@0 6 /// for the Microsoft compiler version.
michael@0 7 ///
michael@0 8 /// Author : Copyright (c) Olli Parviainen
michael@0 9 /// Author e-mail : oparviai 'at' iki.fi
michael@0 10 /// SoundTouch WWW: http://www.surina.net/soundtouch
michael@0 11 ///
michael@0 12 ////////////////////////////////////////////////////////////////////////////////
michael@0 13 //
michael@0 14 // Last changed : $Date: 2014-01-07 12:24:28 -0600 (Tue, 07 Jan 2014) $
michael@0 15 // File revision : $Revision: 4 $
michael@0 16 //
michael@0 17 // $Id: cpu_detect_x86.cpp 183 2014-01-07 18:24:28Z oparviai $
michael@0 18 //
michael@0 19 ////////////////////////////////////////////////////////////////////////////////
michael@0 20 //
michael@0 21 // License :
michael@0 22 //
michael@0 23 // SoundTouch audio processing library
michael@0 24 // Copyright (c) Olli Parviainen
michael@0 25 //
michael@0 26 // This library is free software; you can redistribute it and/or
michael@0 27 // modify it under the terms of the GNU Lesser General Public
michael@0 28 // License as published by the Free Software Foundation; either
michael@0 29 // version 2.1 of the License, or (at your option) any later version.
michael@0 30 //
michael@0 31 // This library is distributed in the hope that it will be useful,
michael@0 32 // but WITHOUT ANY WARRANTY; without even the implied warranty of
michael@0 33 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
michael@0 34 // Lesser General Public License for more details.
michael@0 35 //
michael@0 36 // You should have received a copy of the GNU Lesser General Public
michael@0 37 // License along with this library; if not, write to the Free Software
michael@0 38 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
michael@0 39 //
michael@0 40 ////////////////////////////////////////////////////////////////////////////////
michael@0 41
michael@0 42 #include "cpu_detect.h"
michael@0 43 #include "STTypes.h"
michael@0 44
michael@0 45
michael@0 46 #if defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS)
michael@0 47 #if defined(__GNUC__) && defined(HAVE_CPUID_H)
michael@0 48 // gcc and clang
michael@0 49 #include "cpuid.h"
michael@0 50 #elif defined(_M_IX86)
michael@0 51 // windows non-gcc
michael@0 52 #include <intrin.h>
michael@0 53 #endif
michael@0 54
michael@0 55 #define bit_MMX (1 << 23)
michael@0 56 #define bit_SSE (1 << 25)
michael@0 57 #define bit_SSE2 (1 << 26)
michael@0 58 #endif
michael@0 59
michael@0 60
michael@0 61 //////////////////////////////////////////////////////////////////////////////
michael@0 62 //
michael@0 63 // processor instructions extension detection routines
michael@0 64 //
michael@0 65 //////////////////////////////////////////////////////////////////////////////
michael@0 66
michael@0 67 // Flag variable indicating whick ISA extensions are disabled (for debugging)
michael@0 68 static uint _dwDisabledISA = 0x00; // 0xffffffff; //<- use this to disable all extensions
michael@0 69
michael@0 70 // Disables given set of instruction extensions. See SUPPORT_... defines.
michael@0 71 void disableExtensions(uint dwDisableMask)
michael@0 72 {
michael@0 73 _dwDisabledISA = dwDisableMask;
michael@0 74 }
michael@0 75
michael@0 76
michael@0 77
michael@0 78 /// Checks which instruction set extensions are supported by the CPU.
michael@0 79 uint detectCPUextensions(void)
michael@0 80 {
michael@0 81 /// If building for a 64bit system (no Itanium) and the user wants optimizations.
michael@0 82 /// Return the OR of SUPPORT_{MMX,SSE,SSE2}. 11001 or 0x19.
michael@0 83 /// Keep the _dwDisabledISA test (2 more operations, could be eliminated).
michael@0 84 #if ((defined(__GNUC__) && defined(__x86_64__)) \
michael@0 85 || defined(_M_X64)) \
michael@0 86 && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS)
michael@0 87 return 0x19 & ~_dwDisabledISA;
michael@0 88
michael@0 89 /// If building for a 32bit system and the user wants optimizations.
michael@0 90 /// Keep the _dwDisabledISA test (2 more operations, could be eliminated).
michael@0 91 #elif ((defined(__GNUC__) && defined(__i386__)) \
michael@0 92 || defined(_M_IX86)) \
michael@0 93 && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS)
michael@0 94
michael@0 95 if (_dwDisabledISA == 0xffffffff) return 0;
michael@0 96
michael@0 97 uint res = 0;
michael@0 98
michael@0 99 #if !defined(__GNUC__)
michael@0 100 // Window / VS version of cpuid. Notice that Visual Studio 2005 or later required
michael@0 101 // for __cpuid intrinsic support.
michael@0 102 int reg[4] = {-1};
michael@0 103
michael@0 104 // Check if no cpuid support.
michael@0 105 __cpuid(reg,0);
michael@0 106 if ((unsigned int)reg[0] == 0) return 0; // always disable extensions.
michael@0 107
michael@0 108 __cpuid(reg,1);
michael@0 109 if ((unsigned int)reg[3] & bit_MMX) res = res | SUPPORT_MMX;
michael@0 110 if ((unsigned int)reg[3] & bit_SSE) res = res | SUPPORT_SSE;
michael@0 111 if ((unsigned int)reg[3] & bit_SSE2) res = res | SUPPORT_SSE2;
michael@0 112 #elif defined(HAVE_CPUID_H)
michael@0 113 // GCC version of cpuid. Requires GCC 4.3.0 or later for __cpuid intrinsic support.
michael@0 114 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 115
michael@0 116 // Check if no cpuid support.
michael@0 117 if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx)) return 0; // always disable extensions.
michael@0 118
michael@0 119 if (edx & bit_MMX) res = res | SUPPORT_MMX;
michael@0 120 if (edx & bit_SSE) res = res | SUPPORT_SSE;
michael@0 121 if (edx & bit_SSE2) res = res | SUPPORT_SSE2;
michael@0 122 #else
michael@0 123 // Compatible with GCC but no cpuid.h.
michael@0 124 return 0;
michael@0 125 #endif
michael@0 126
michael@0 127 return res & ~_dwDisabledISA;
michael@0 128
michael@0 129 #else
michael@0 130
michael@0 131 /// One of these is true:
michael@0 132 /// 1) We don't want optimizations.
michael@0 133 /// 2) Using an unsupported compiler.
michael@0 134 /// 3) Running on a non-x86 platform.
michael@0 135 return 0;
michael@0 136
michael@0 137 #endif
michael@0 138 }

mercurial