Initial revision
[libav.git] / libavcodec / i386 / cputest.c
1 /* Cpu detection code, extracted from mmx.h ((c)1997-99 by H. Dietz
2 and R. Fisher). Converted to C and improved by Gerard Lantau */
3
4 #include <stdlib.h>
5 #include "../dsputil.h"
6
7 #define cpuid(index,eax,ebx,ecx,edx) \
8 asm ("cpuid" \
9 : "=a" (eax), "=b" (ebx), \
10 "=c" (ecx), "=d" (edx) \
11 : "a" (index) \
12 : "cc")
13
14 /* Function to test if multimedia instructions are supported... */
15 int mm_support(void)
16 {
17 int rval;
18 int eax, ebx, ecx, edx;
19
20
21 __asm__ __volatile__ (
22 /* See if CPUID instruction is supported ... */
23 /* ... Get copies of EFLAGS into eax and ecx */
24 "pushf\n\t"
25 "popl %0\n\t"
26 "movl %0, %1\n\t"
27
28 /* ... Toggle the ID bit in one copy and store */
29 /* to the EFLAGS reg */
30 "xorl $0x200000, %0\n\t"
31 "push %0\n\t"
32 "popf\n\t"
33
34 /* ... Get the (hopefully modified) EFLAGS */
35 "pushf\n\t"
36 "popl %0\n\t"
37 : "=a" (eax), "=c" (ecx)
38 :
39 : "cc"
40 );
41
42 if (eax == ecx)
43 return 0; /* CPUID not supported */
44
45 cpuid(0, eax, ebx, ecx, edx);
46
47 if (ebx == 0x756e6547 &&
48 edx == 0x49656e69 &&
49 ecx == 0x6c65746e) {
50
51 /* intel */
52 inteltest:
53 cpuid(1, eax, ebx, ecx, edx);
54 if ((edx & 0x00800000) == 0)
55 return 0;
56 rval = MM_MMX;
57 if (edx & 0x02000000)
58 rval |= MM_MMXEXT | MM_SSE;
59 if (edx & 0x04000000)
60 rval |= MM_SSE2;
61 return rval;
62 } else if (ebx == 0x68747541 &&
63 edx == 0x69746e65 &&
64 ecx == 0x444d4163) {
65 /* AMD */
66 cpuid(0x80000000, eax, ebx, ecx, edx);
67 if ((unsigned)eax < 0x80000001)
68 goto inteltest;
69 cpuid(0x80000001, eax, ebx, ecx, edx);
70 if ((edx & 0x00800000) == 0)
71 return 0;
72 rval = MM_MMX;
73 if (edx & 0x80000000)
74 rval |= MM_3DNOW;
75 if (edx & 0x00400000)
76 rval |= MM_MMXEXT;
77 return rval;
78 } else if (ebx == 0x69727943 &&
79 edx == 0x736e4978 &&
80 ecx == 0x64616574) {
81 /* Cyrix Section */
82 /* See if extended CPUID level 80000001 is supported */
83 /* The value of CPUID/80000001 for the 6x86MX is undefined
84 according to the Cyrix CPU Detection Guide (Preliminary
85 Rev. 1.01 table 1), so we'll check the value of eax for
86 CPUID/0 to see if standard CPUID level 2 is supported.
87 According to the table, the only CPU which supports level
88 2 is also the only one which supports extended CPUID levels.
89 */
90 if (eax != 2)
91 goto inteltest;
92 cpuid(0x80000001, eax, ebx, ecx, edx);
93 if ((eax & 0x00800000) == 0)
94 return 0;
95 rval = MM_MMX;
96 if (eax & 0x01000000)
97 rval |= MM_MMXEXT;
98 return rval;
99 } else {
100 return 0;
101 }
102 }