1 |
//////////////////////////////////////////////////////////////////////////////// |
2 |
/// |
3 |
/// Generic version of the x86 CPU extension detection routine. |
4 |
/// |
5 |
/// This file is for GNU & other non-Windows compilers, see 'cpu_detect_x86_win.cpp' |
6 |
/// for the Microsoft compiler version. |
7 |
/// |
8 |
/// Author : Copyright (c) Olli Parviainen |
9 |
/// Author e-mail : oparviai 'at' iki.fi |
10 |
/// SoundTouch WWW: http://www.surina.net/soundtouch |
11 |
/// |
12 |
//////////////////////////////////////////////////////////////////////////////// |
13 |
// |
14 |
// Last changed : $Date: 2009-02-25 19:13:51 +0200 (Wed, 25 Feb 2009) $ |
15 |
// File revision : $Revision: 4 $ |
16 |
// |
17 |
// $Id: cpu_detect_x86_gcc.cpp 67 2009-02-25 17:13:51Z oparviai $ |
18 |
// |
19 |
//////////////////////////////////////////////////////////////////////////////// |
20 |
// |
21 |
// License : |
22 |
// |
23 |
// SoundTouch audio processing library |
24 |
// Copyright (c) Olli Parviainen |
25 |
// |
26 |
// This library is free software; you can redistribute it and/or |
27 |
// modify it under the terms of the GNU Lesser General Public |
28 |
// License as published by the Free Software Foundation; either |
29 |
// version 2.1 of the License, or (at your option) any later version. |
30 |
// |
31 |
// This library is distributed in the hope that it will be useful, |
32 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
33 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
34 |
// Lesser General Public License for more details. |
35 |
// |
36 |
// You should have received a copy of the GNU Lesser General Public |
37 |
// License along with this library; if not, write to the Free Software |
38 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
39 |
// |
40 |
//////////////////////////////////////////////////////////////////////////////// |
41 |
|
42 |
#include <stdexcept> |
43 |
#include <string> |
44 |
#include "cpu_detect.h" |
45 |
#include "STTypes.h" |
46 |
|
47 |
using namespace std; |
48 |
|
49 |
#include <stdio.h> |
50 |
|
51 |
////////////////////////////////////////////////////////////////////////////// |
52 |
// |
53 |
// processor instructions extension detection routines |
54 |
// |
55 |
////////////////////////////////////////////////////////////////////////////// |
56 |
|
57 |
// Flag variable indicating whick ISA extensions are disabled (for debugging) |
58 |
static uint _dwDisabledISA = 0x00; // 0xffffffff; //<- use this to disable all extensions |
59 |
|
60 |
// Disables given set of instruction extensions. See SUPPORT_... defines. |
61 |
void disableExtensions(uint dwDisableMask) |
62 |
{ |
63 |
_dwDisabledISA = dwDisableMask; |
64 |
} |
65 |
|
66 |
|
67 |
|
68 |
/// Checks which instruction set extensions are supported by the CPU. |
69 |
uint detectCPUextensions(void) |
70 |
{ |
71 |
#if (!(ALLOW_X86_OPTIMIZATIONS) || !(__GNUC__)) |
72 |
|
73 |
return 0; // always disable extensions on non-x86 platforms. |
74 |
|
75 |
#else |
76 |
uint res = 0; |
77 |
|
78 |
if (_dwDisabledISA == 0xffffffff) return 0; |
79 |
|
80 |
asm volatile( |
81 |
"\n\txor %%esi, %%esi" // clear %%esi = result register |
82 |
// check if 'cpuid' instructions is available by toggling eflags bit 21 |
83 |
|
84 |
"\n\tpushf" // save eflags to stack |
85 |
"\n\tmovl (%%esp), %%eax" // load eax from stack (with eflags) |
86 |
"\n\tmovl %%eax, %%ecx" // save the original eflags values to ecx |
87 |
"\n\txor $0x00200000, %%eax" // toggle bit 21 |
88 |
"\n\tmovl %%eax, (%%esp)" // store toggled eflags to stack |
89 |
"\n\tpopf" // load eflags from stack |
90 |
"\n\tpushf" // save updated eflags to stack |
91 |
"\n\tmovl (%%esp), %%eax" // load eax from stack |
92 |
"\n\tpopf" // pop stack to restore esp |
93 |
"\n\txor %%edx, %%edx" // clear edx for defaulting no mmx |
94 |
"\n\tcmp %%ecx, %%eax" // compare to original eflags values |
95 |
"\n\tjz end" // jumps to 'end' if cpuid not present |
96 |
// cpuid instruction available, test for presence of mmx instructions |
97 |
|
98 |
"\n\tmovl $1, %%eax" |
99 |
"\n\tcpuid" |
100 |
"\n\ttest $0x00800000, %%edx" |
101 |
"\n\tjz end" // branch if MMX not available |
102 |
|
103 |
"\n\tor $0x01, %%esi" // otherwise add MMX support bit |
104 |
|
105 |
"\n\ttest $0x02000000, %%edx" |
106 |
"\n\tjz test3DNow" // branch if SSE not available |
107 |
|
108 |
"\n\tor $0x08, %%esi" // otherwise add SSE support bit |
109 |
|
110 |
"\n\ttest3DNow:" |
111 |
// test for precense of AMD extensions |
112 |
"\n\tmov $0x80000000, %%eax" |
113 |
"\n\tcpuid" |
114 |
"\n\tcmp $0x80000000, %%eax" |
115 |
"\n\tjbe end" // branch if no AMD extensions detected |
116 |
|
117 |
// test for precense of 3DNow! extension |
118 |
"\n\tmov $0x80000001, %%eax" |
119 |
"\n\tcpuid" |
120 |
"\n\ttest $0x80000000, %%edx" |
121 |
"\n\tjz end" // branch if 3DNow! not detected |
122 |
|
123 |
"\n\tor $0x02, %%esi" // otherwise add 3DNow support bit |
124 |
|
125 |
"\n\tend:" |
126 |
|
127 |
"\n\tmov %%esi, %0" |
128 |
|
129 |
: "=r" (res) |
130 |
: /* no inputs */ |
131 |
: "%edx", "%eax", "%ecx", "%esi" ); |
132 |
|
133 |
return res & ~_dwDisabledISA; |
134 |
#endif |
135 |
} |