Statistics
| Branch: | Revision:

ffmpeg / libavcodec / i386 / cputest.c @ e7ddb0cf

History | View | Annotate | Download (4.55 KB)

1
/* Cpu detection code, extracted from mmx.h ((c)1997-99 by H. Dietz
2
   and R. Fisher). Converted to C and improved by Fabrice Bellard */
3

    
4
#include <stdlib.h>
5
#include "../dsputil.h"
6

    
7
#ifdef ARCH_X86_64
8
#  define REG_b "rbx"
9
#  define REG_S "rsi"
10
#else
11
#  define REG_b "ebx"
12
#  define REG_S "esi"
13
#endif
14

    
15
/* ebx saving is necessary for PIC. gcc seems unable to see it alone */
16
#define cpuid(index,eax,ebx,ecx,edx)\
17
    __asm __volatile\
18
        ("mov %%"REG_b", %%"REG_S"\n\t"\
19
         "cpuid\n\t"\
20
         "xchg %%"REG_b", %%"REG_S\
21
         : "=a" (eax), "=S" (ebx),\
22
           "=c" (ecx), "=d" (edx)\
23
         : "0" (index));
24

    
25
/* Function to test if multimedia instructions are supported...  */
26
int mm_support(void)
27
{
28
    int rval = 0;
29
    int eax, ebx, ecx, edx;
30
    long a, c;
31
    
32
    __asm__ __volatile__ (
33
                          /* See if CPUID instruction is supported ... */
34
                          /* ... Get copies of EFLAGS into eax and ecx */
35
                          "pushf\n\t"
36
                          "pop %0\n\t"
37
                          "mov %0, %1\n\t"
38
                          
39
                          /* ... Toggle the ID bit in one copy and store */
40
                          /*     to the EFLAGS reg */
41
                          "xor $0x200000, %0\n\t"
42
                          "push %0\n\t"
43
                          "popf\n\t"
44
                          
45
                          /* ... Get the (hopefully modified) EFLAGS */
46
                          "pushf\n\t"
47
                          "pop %0\n\t"
48
                          : "=a" (a), "=c" (c)
49
                          :
50
                          : "cc" 
51
                          );
52
    
53
    if (a == c)
54
        return 0; /* CPUID not supported */
55
    
56
    cpuid(0, eax, ebx, ecx, edx);
57

    
58
    if (ebx == 0x756e6547 &&
59
        edx == 0x49656e69 &&
60
        ecx == 0x6c65746e) {
61
        
62
        /* intel */
63
    inteltest:
64
        cpuid(1, eax, ebx, ecx, edx);
65
        if ((edx & 0x00800000) == 0)
66
            return 0;
67
        rval |= MM_MMX;
68
        if (edx & 0x02000000) 
69
            rval |= MM_MMXEXT | MM_SSE;
70
        if (edx & 0x04000000) 
71
            rval |= MM_SSE2;
72
        return rval;
73
    } else if (ebx == 0x68747541 &&
74
               edx == 0x69746e65 &&
75
               ecx == 0x444d4163) {
76
        /* AMD */
77
        cpuid(0x80000000, eax, ebx, ecx, edx);
78
        if ((unsigned)eax < 0x80000001)
79
            goto inteltest;
80
        cpuid(0x80000001, eax, ebx, ecx, edx);
81
        if ((edx & 0x00800000) == 0)
82
            return 0;
83
        rval = MM_MMX;
84
        if (edx & 0x80000000)
85
            rval |= MM_3DNOW;
86
        if (edx & 0x00400000)
87
            rval |= MM_MMXEXT;
88
        goto inteltest;
89
    } else if (ebx == 0x746e6543 &&
90
               edx == 0x48727561 &&
91
               ecx == 0x736c7561) {  /*  "CentaurHauls" */
92
        /* VIA C3 */
93
        cpuid(0x80000000, eax, ebx, ecx, edx);
94
        if ((unsigned)eax < 0x80000001)
95
            goto inteltest;        
96
        cpuid(0x80000001, eax, ebx, ecx, edx);
97
        rval = 0;      
98
        if( edx & ( 1 << 31) )
99
          rval |= MM_3DNOW;
100
        if( edx & ( 1 << 23) )
101
          rval |= MM_MMX;
102
        if( edx & ( 1 << 24) )
103
          rval |= MM_MMXEXT;
104
        if(rval==0)
105
            goto inteltest;
106
        return rval;
107
    } else if (ebx == 0x69727943 &&
108
               edx == 0x736e4978 &&
109
               ecx == 0x64616574) {
110
        /* Cyrix Section */
111
        /* See if extended CPUID level 80000001 is supported */
112
        /* The value of CPUID/80000001 for the 6x86MX is undefined
113
           according to the Cyrix CPU Detection Guide (Preliminary
114
           Rev. 1.01 table 1), so we'll check the value of eax for
115
           CPUID/0 to see if standard CPUID level 2 is supported.
116
           According to the table, the only CPU which supports level
117
           2 is also the only one which supports extended CPUID levels.
118
        */
119
        if (eax != 2) 
120
            goto inteltest;
121
        cpuid(0x80000001, eax, ebx, ecx, edx);
122
        if ((eax & 0x00800000) == 0)
123
            return 0;
124
        rval = MM_MMX;
125
        if (eax & 0x01000000)
126
            rval |= MM_MMXEXT;
127
        return rval;
128
    } else if (ebx == 0x756e6547 &&
129
               edx == 0x54656e69 &&
130
               ecx == 0x3638784d) {
131
        /* Tranmeta Crusoe */
132
        cpuid(0x80000000, eax, ebx, ecx, edx);
133
        if ((unsigned)eax < 0x80000001)
134
            return 0;
135
        cpuid(0x80000001, eax, ebx, ecx, edx);
136
        if ((edx & 0x00800000) == 0)
137
            return 0;
138
        return MM_MMX;
139
    } else {
140
        return 0;
141
    }
142
}
143

    
144
#ifdef __TEST__
145
int main ( void )
146
{
147
  int mm_flags;
148
  mm_flags = mm_support();
149
  printf("mm_support = 0x%08X\n",mm_flags);
150
  return 0;
151
}
152
#endif