2009-02-15 05:15:39 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///
|
2010-03-18 20:04:51 +00:00
|
|
|
/// Generic version of the x86 CPU extension detection routine.
|
2009-02-15 05:15:39 +00:00
|
|
|
///
|
2010-04-25 00:31:27 +00:00
|
|
|
/// This file is for GNU & other non-Windows compilers, see 'cpu_detect_x86_win.cpp'
|
2010-03-18 20:04:51 +00:00
|
|
|
/// for the Microsoft compiler version.
|
2009-02-15 05:15:39 +00:00
|
|
|
///
|
|
|
|
/// Author : Copyright (c) Olli Parviainen
|
|
|
|
/// Author e-mail : oparviai 'at' iki.fi
|
|
|
|
/// SoundTouch WWW: http://www.surina.net/soundtouch
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2010-03-18 20:04:51 +00:00
|
|
|
// Last changed : $Date: 2009-02-25 19:13:51 +0200 (Wed, 25 Feb 2009) $
|
|
|
|
// File revision : $Revision: 4 $
|
2009-02-15 05:15:39 +00:00
|
|
|
//
|
2010-03-18 20:04:51 +00:00
|
|
|
// $Id: cpu_detect_x86_gcc.cpp 67 2009-02-25 17:13:51Z oparviai $
|
2009-02-15 05:15:39 +00:00
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// License :
|
|
|
|
//
|
|
|
|
// SoundTouch audio processing library
|
|
|
|
// Copyright (c) Olli Parviainen
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include "cpu_detect.h"
|
2010-03-18 20:04:51 +00:00
|
|
|
#include "STTypes.h"
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2010-03-18 20:04:51 +00:00
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// processor instructions extension detection routines
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Flag variable indicating whick ISA extensions are disabled (for debugging)
|
|
|
|
static uint _dwDisabledISA = 0x00; // 0xffffffff; //<- use this to disable all extensions
|
|
|
|
|
|
|
|
// Disables given set of instruction extensions. See SUPPORT_... defines.
|
|
|
|
void disableExtensions(uint dwDisableMask)
|
|
|
|
{
|
|
|
|
_dwDisabledISA = dwDisableMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Checks which instruction set extensions are supported by the CPU.
|
|
|
|
uint detectCPUextensions(void)
|
|
|
|
{
|
2010-03-18 20:04:51 +00:00
|
|
|
#if (!(ALLOW_X86_OPTIMIZATIONS) || !(__GNUC__))
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
return 0; // always disable extensions on non-x86 platforms.
|
2010-03-18 20:04:51 +00:00
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
#else
|
|
|
|
uint res = 0;
|
|
|
|
|
|
|
|
if (_dwDisabledISA == 0xffffffff) return 0;
|
|
|
|
|
|
|
|
asm volatile(
|
|
|
|
"\n\txor %%esi, %%esi" // clear %%esi = result register
|
|
|
|
// check if 'cpuid' instructions is available by toggling eflags bit 21
|
|
|
|
|
|
|
|
"\n\tpushf" // save eflags to stack
|
2010-03-18 20:04:51 +00:00
|
|
|
"\n\tmovl (%%esp), %%eax" // load eax from stack (with eflags)
|
2009-02-15 05:15:39 +00:00
|
|
|
"\n\tmovl %%eax, %%ecx" // save the original eflags values to ecx
|
|
|
|
"\n\txor $0x00200000, %%eax" // toggle bit 21
|
2010-03-18 20:04:51 +00:00
|
|
|
"\n\tmovl %%eax, (%%esp)" // store toggled eflags to stack
|
2009-02-15 05:15:39 +00:00
|
|
|
"\n\tpopf" // load eflags from stack
|
|
|
|
"\n\tpushf" // save updated eflags to stack
|
2010-03-18 20:04:51 +00:00
|
|
|
"\n\tmovl (%%esp), %%eax" // load eax from stack
|
|
|
|
"\n\tpopf" // pop stack to restore esp
|
2009-02-15 05:15:39 +00:00
|
|
|
"\n\txor %%edx, %%edx" // clear edx for defaulting no mmx
|
|
|
|
"\n\tcmp %%ecx, %%eax" // compare to original eflags values
|
|
|
|
"\n\tjz end" // jumps to 'end' if cpuid not present
|
|
|
|
// cpuid instruction available, test for presence of mmx instructions
|
|
|
|
|
|
|
|
"\n\tmovl $1, %%eax"
|
|
|
|
"\n\tcpuid"
|
|
|
|
"\n\ttest $0x00800000, %%edx"
|
|
|
|
"\n\tjz end" // branch if MMX not available
|
|
|
|
|
|
|
|
"\n\tor $0x01, %%esi" // otherwise add MMX support bit
|
|
|
|
|
|
|
|
"\n\ttest $0x02000000, %%edx"
|
|
|
|
"\n\tjz test3DNow" // branch if SSE not available
|
|
|
|
|
|
|
|
"\n\tor $0x08, %%esi" // otherwise add SSE support bit
|
|
|
|
|
|
|
|
"\n\ttest3DNow:"
|
|
|
|
// test for precense of AMD extensions
|
|
|
|
"\n\tmov $0x80000000, %%eax"
|
|
|
|
"\n\tcpuid"
|
|
|
|
"\n\tcmp $0x80000000, %%eax"
|
|
|
|
"\n\tjbe end" // branch if no AMD extensions detected
|
|
|
|
|
|
|
|
// test for precense of 3DNow! extension
|
|
|
|
"\n\tmov $0x80000001, %%eax"
|
|
|
|
"\n\tcpuid"
|
|
|
|
"\n\ttest $0x80000000, %%edx"
|
|
|
|
"\n\tjz end" // branch if 3DNow! not detected
|
|
|
|
|
|
|
|
"\n\tor $0x02, %%esi" // otherwise add 3DNow support bit
|
|
|
|
|
|
|
|
"\n\tend:"
|
|
|
|
|
|
|
|
"\n\tmov %%esi, %0"
|
|
|
|
|
|
|
|
: "=r" (res)
|
|
|
|
: /* no inputs */
|
|
|
|
: "%edx", "%eax", "%ecx", "%esi" );
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
return res & ~_dwDisabledISA;
|
|
|
|
#endif
|
|
|
|
}
|