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 $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
36 #if defined(ARCH_X86) || defined(ARCH_X86_64)
37 extern int x264_cpu_cpuid_test( void );
38 extern uint32_t x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
39 extern void x264_emms( void );
41 uint32_t x264_cpu_detect( void )
45 uint32_t eax, ebx, ecx, edx;
49 if( !x264_cpu_cpuid_test() )
55 x264_cpu_cpuid( 0, &eax, &ebx, &ecx, &edx);
60 b_amd = (ebx == 0x68747541) && (ecx == 0x444d4163) && (edx == 0x69746e65);
62 x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
63 if( (edx&0x00800000) == 0 )
69 if( (edx&0x02000000) )
71 /* SSE - identical to AMD MMX extensions */
72 cpu |= X264_CPU_MMXEXT|X264_CPU_SSE;
74 if( (edx&0x04000000) )
80 x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
81 if( eax < 0x80000001 )
83 /* no extended capabilities */
87 x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
90 cpu |= X264_CPU_3DNOW;
92 if( b_amd && (edx&0x00400000) )
94 /* AMD MMX extensions */
95 cpu |= X264_CPU_MMXEXT;
101 void x264_cpu_restore( uint32_t cpu )
103 if( cpu&(X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_3DNOW|X264_CPU_3DNOWEXT) )
112 * XXX: adapted from libmpeg2 */
114 #define cpuid(op,eax,ebx,ecx,edx) \
115 __asm__ ("push %%ebx\n\t" \
117 "movl %%ebx,%1\n\t" \
127 uint32_t x264_cpu_detect( void )
131 uint32_t eax, ebx, ecx, edx;
135 /* Test if cpuid is supported */
141 "xorl $0x200000,%0\n"
147 : "=r" (eax), "=r" (ebx) : : "cc");
155 cpuid( 0, eax, ebx, ecx, edx);
160 b_amd = (ebx == 0x68747541) && (ecx == 0x444d4163) && (edx == 0x69746e65);
162 cpuid( 1, eax, ebx, ecx, edx );
163 if( (edx&0x00800000) == 0 )
169 if( (edx&0x02000000) )
171 /* SSE - identical to AMD MMX extensions */
172 cpu |= X264_CPU_MMXEXT|X264_CPU_SSE;
174 if( (edx&0x04000000) )
177 cpu |= X264_CPU_SSE2;
180 cpuid( 0x80000000, eax, ebx, ecx, edx );
181 if( eax < 0x80000001 )
183 /* no extended capabilities */
187 cpuid( 0x80000001, eax, ebx, ecx, edx );
190 cpu |= X264_CPU_3DNOW;
192 if( b_amd && (edx&0x00400000) )
194 /* AMD MMX extensions */
195 cpu |= X264_CPU_MMXEXT;
202 #elif defined( ARCH_PPC )
205 #include <sys/sysctl.h>
206 uint32_t x264_cpu_detect( void )
210 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
212 size_t length = sizeof( has_altivec );
213 int error = sysctl( selectors, 2, &has_altivec, &length, NULL, 0 );
215 if( error == 0 && has_altivec != 0 )
217 cpu |= X264_CPU_ALTIVEC;
223 #elif defined( SYS_LINUX )
224 uint32_t x264_cpu_detect( void )
226 /* FIXME (Linux PPC) */
227 return X264_CPU_ALTIVEC;
231 void x264_cpu_restore( uint32_t cpu )
237 uint32_t x264_cpu_detect( void )
242 void x264_cpu_restore( uint32_t cpu )