Statistics
| Branch: | Revision:

ffmpeg / libavcodec / i386 / cputest.c @ e42a152b

History | View | Annotate | Download (3.98 KB)

1 de6d9b64 Fabrice Bellard
/* Cpu detection code, extracted from mmx.h ((c)1997-99 by H. Dietz
2 ff4ec49e Fabrice Bellard
   and R. Fisher). Converted to C and improved by Fabrice Bellard */
3 de6d9b64 Fabrice Bellard
4
#include <stdlib.h>
5
#include "../dsputil.h"
6
7 053dea12 Aurelien Jacobs
#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 1d20b11a Nick Kurshev
/* ebx saving is necessary for PIC. gcc seems unable to see it alone */
16
#define cpuid(index,eax,ebx,ecx,edx)\
17 c10e9f70 Nick Kurshev
    __asm __volatile\
18 053dea12 Aurelien Jacobs
        ("mov %%"REG_b", %%"REG_S"\n\t"\
19 1d20b11a Nick Kurshev
         "cpuid\n\t"\
20 053dea12 Aurelien Jacobs
         "xchg %%"REG_b", %%"REG_S\
21 1d20b11a Nick Kurshev
         : "=a" (eax), "=S" (ebx),\
22
           "=c" (ecx), "=d" (edx)\
23 c10e9f70 Nick Kurshev
         : "0" (index));
24 de6d9b64 Fabrice Bellard
25
/* Function to test if multimedia instructions are supported...  */
26
int mm_support(void)
27
{
28 e7ddb0cf Matthieu Castet
    int rval = 0;
29 de6d9b64 Fabrice Bellard
    int eax, ebx, ecx, edx;
30 e42a152b Michael Niedermayer
    int max_std_level, max_ext_level, std_caps=0, ext_caps=0;
31 8b4d077f Aurelien Jacobs
    long a, c;
32 de6d9b64 Fabrice Bellard
    
33
    __asm__ __volatile__ (
34
                          /* See if CPUID instruction is supported ... */
35
                          /* ... Get copies of EFLAGS into eax and ecx */
36
                          "pushf\n\t"
37 053dea12 Aurelien Jacobs
                          "pop %0\n\t"
38 8b4d077f Aurelien Jacobs
                          "mov %0, %1\n\t"
39 de6d9b64 Fabrice Bellard
                          
40
                          /* ... Toggle the ID bit in one copy and store */
41
                          /*     to the EFLAGS reg */
42 8b4d077f Aurelien Jacobs
                          "xor $0x200000, %0\n\t"
43 de6d9b64 Fabrice Bellard
                          "push %0\n\t"
44
                          "popf\n\t"
45
                          
46
                          /* ... Get the (hopefully modified) EFLAGS */
47
                          "pushf\n\t"
48 053dea12 Aurelien Jacobs
                          "pop %0\n\t"
49 8b4d077f Aurelien Jacobs
                          : "=a" (a), "=c" (c)
50 de6d9b64 Fabrice Bellard
                          :
51
                          : "cc" 
52
                          );
53
    
54 8b4d077f Aurelien Jacobs
    if (a == c)
55 de6d9b64 Fabrice Bellard
        return 0; /* CPUID not supported */
56
57 e42a152b Michael Niedermayer
    cpuid(0, max_std_level, ebx, ecx, edx);
58
59
    if(max_std_level >= 1){
60
        cpuid(1, eax, ebx, ecx, std_caps);
61
        if (std_caps & (1<<23))
62
            rval |= MM_MMX;
63
        if (std_caps & (1<<25)) 
64 de6d9b64 Fabrice Bellard
            rval |= MM_MMXEXT | MM_SSE;
65 e42a152b Michael Niedermayer
        if (std_caps & (1<<26)) 
66 de6d9b64 Fabrice Bellard
            rval |= MM_SSE2;
67 e42a152b Michael Niedermayer
    }
68
69
    cpuid(0x80000000, max_ext_level, ebx, ecx, edx);
70
71
    if(max_ext_level >= 0x80000001){
72
        cpuid(0x80000001, eax, ebx, ecx, ext_caps);
73
        if (ext_caps & (1<<31))
74
            rval |= MM_3DNOW;
75
        if (ext_caps & (1<<30))
76
            rval |= MM_3DNOWEXT;
77
        if (ext_caps & (1<<23))
78
            rval |= MM_MMX;
79
    }
80
81
    cpuid(0, eax, ebx, ecx, edx);
82
    if (       ebx == 0x68747541 &&
83 de6d9b64 Fabrice Bellard
               edx == 0x69746e65 &&
84
               ecx == 0x444d4163) {
85
        /* AMD */
86 e42a152b Michael Niedermayer
        if(ext_caps & (1<<22))
87 de6d9b64 Fabrice Bellard
            rval |= MM_MMXEXT;
88 57fc2576 Francisco Javier Cabello Torres
    } else if (ebx == 0x746e6543 &&
89
               edx == 0x48727561 &&
90
               ecx == 0x736c7561) {  /*  "CentaurHauls" */
91
        /* VIA C3 */
92 e42a152b Michael Niedermayer
        if(ext_caps & (1<<24))
93 7ca413b4 Francisco Javier Cabello Torres
          rval |= MM_MMXEXT;
94 de6d9b64 Fabrice Bellard
    } else if (ebx == 0x69727943 &&
95
               edx == 0x736e4978 &&
96
               ecx == 0x64616574) {
97
        /* Cyrix Section */
98
        /* See if extended CPUID level 80000001 is supported */
99
        /* The value of CPUID/80000001 for the 6x86MX is undefined
100
           according to the Cyrix CPU Detection Guide (Preliminary
101
           Rev. 1.01 table 1), so we'll check the value of eax for
102
           CPUID/0 to see if standard CPUID level 2 is supported.
103
           According to the table, the only CPU which supports level
104
           2 is also the only one which supports extended CPUID levels.
105
        */
106 e42a152b Michael Niedermayer
        if (eax < 2) 
107
            return rval;
108
        if (ext_caps & (1<<24))
109 de6d9b64 Fabrice Bellard
            rval |= MM_MMXEXT;
110
    }
111 e42a152b Michael Niedermayer
#if 0
112
    av_log(NULL, AV_LOG_DEBUG, "%s%s%s%s%s%s\n", 
113
        (rval&MM_MMX) ? "MMX ":"", 
114
        (rval&MM_MMXEXT) ? "MMX2 ":"", 
115
        (rval&MM_SSE) ? "SSE ":"", 
116
        (rval&MM_SSE2) ? "SSE2 ":"", 
117
        (rval&MM_3DNOW) ? "3DNow ":"", 
118
        (rval&MM_3DNOWEXT) ? "3DNowExt ":"");
119
#endif
120
    return rval;
121 de6d9b64 Fabrice Bellard
}
122 c10e9f70 Nick Kurshev
123
#ifdef __TEST__
124
int main ( void )
125
{
126
  int mm_flags;
127
  mm_flags = mm_support();
128 6206f8c9 Michael Niedermayer
  printf("mm_support = 0x%08X\n",mm_flags);
129 c10e9f70 Nick Kurshev
  return 0;
130
}
131
#endif