]> git.sesse.net Git - x264/blob - common/cpu.c
be05806d061aa18fe3cb1b25ae7f113134feef15
[x264] / common / cpu.c
1 /*****************************************************************************
2  * cpu.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: cpu.c,v 1.1 2004/06/03 19:27:06 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <string.h>
25
26 #include "common.h"
27
28 #if defined(ARCH_X86) || defined(ARCH_X86_64)
29 extern int  x264_cpu_cpuid_test( void );
30 extern uint32_t  x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
31 extern void x264_emms( void );
32
33 uint32_t x264_cpu_detect( void )
34 {
35     uint32_t cpu = 0;
36
37     uint32_t eax, ebx, ecx, edx;
38     int      b_amd;
39
40
41     if( !x264_cpu_cpuid_test() )
42     {
43         /* No cpuid */
44         return 0;
45     }
46
47     x264_cpu_cpuid( 0, &eax, &ebx, &ecx, &edx);
48     if( eax == 0 )
49     {
50         return 0;
51     }
52     b_amd   = (ebx == 0x68747541) && (ecx == 0x444d4163) && (edx == 0x69746e65);
53
54     x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
55     if( (edx&0x00800000) == 0 )
56     {
57         /* No MMX */
58         return 0;
59     }
60     cpu = X264_CPU_MMX;
61     if( (edx&0x02000000) )
62     {
63         /* SSE - identical to AMD MMX extensions */
64         cpu |= X264_CPU_MMXEXT|X264_CPU_SSE;
65     }
66     if( (edx&0x04000000) )
67     {
68         /* Is it OK ? */
69         cpu |= X264_CPU_SSE2;
70     }
71
72     x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
73     if( eax < 0x80000001 )
74     {
75         /* no extended capabilities */
76         return cpu;
77     }
78
79     x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
80     if( edx&0x80000000 )
81     {
82         cpu |= X264_CPU_3DNOW;
83     }
84     if( b_amd && (edx&0x00400000) )
85     {
86         /* AMD MMX extensions */
87         cpu |= X264_CPU_MMXEXT;
88     }
89
90     return cpu;
91 }
92
93 void     x264_cpu_restore( uint32_t cpu )
94 {
95     if( cpu&(X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_3DNOW|X264_CPU_3DNOWEXT) )
96     {
97         x264_emms();
98     }
99 }
100
101 #elif defined( ARCH_PPC )
102
103 #ifdef SYS_MACOSX
104 #include <sys/sysctl.h>
105 uint32_t x264_cpu_detect( void )
106 {
107     /* Thank you VLC */
108     uint32_t cpu = 0;
109     int      selectors[2] = { CTL_HW, HW_VECTORUNIT };
110     int      has_altivec = 0;
111     size_t   length = sizeof( has_altivec );
112     int      error = sysctl( selectors, 2, &has_altivec, &length, NULL, 0 );
113
114     if( error == 0 && has_altivec != 0 )
115     {
116         cpu |= X264_CPU_ALTIVEC;
117     }
118
119     return cpu;
120 }
121
122 #elif defined( SYS_LINUX )
123 uint32_t x264_cpu_detect( void )
124 {
125     /* FIXME (Linux PPC) */
126     return X264_CPU_ALTIVEC;
127 }
128 #endif
129
130 void     x264_cpu_restore( uint32_t cpu )
131 {
132 }
133
134 #else
135
136 uint32_t x264_cpu_detect( void )
137 {
138     return 0;
139 }
140
141 void     x264_cpu_restore( uint32_t cpu )
142 {
143 }
144
145 #endif