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 *****************************************************************************/
24 #if defined(HAVE_PTHREAD) && defined(SYS_LINUX)
29 #include <kernel/OS.h>
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
41 extern int x264_cpu_cpuid_test( void );
42 extern uint32_t x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
43 extern void x264_emms( void );
45 uint32_t x264_cpu_detect( void )
49 uint32_t eax, ebx, ecx, edx;
52 if( !x264_cpu_cpuid_test() )
58 x264_cpu_cpuid( 0, &eax, &ebx, &ecx, &edx);
63 b_amd = (ebx == 0x68747541) && (ecx == 0x444d4163) && (edx == 0x69746e65);
65 x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
66 if( (edx&0x00800000) == 0 )
72 if( (edx&0x02000000) )
74 /* SSE - identical to AMD MMX extensions */
75 cpu |= X264_CPU_MMXEXT|X264_CPU_SSE;
77 if( (edx&0x04000000) )
83 if( (ecx&0x00000001) )
87 if( (ecx&0x00000200) )
89 cpu |= X264_CPU_SSSE3;
93 x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
94 if( eax < 0x80000001 )
96 /* no extended capabilities */
100 x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
103 cpu |= X264_CPU_3DNOW;
105 if( b_amd && (edx&0x00400000) )
107 /* AMD MMX extensions */
108 cpu |= X264_CPU_MMXEXT;
114 void x264_cpu_restore( uint32_t cpu )
116 if( cpu&(X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_3DNOW|X264_CPU_3DNOWEXT) )
122 #elif defined( ARCH_PPC )
125 #include <sys/sysctl.h>
126 uint32_t x264_cpu_detect( void )
130 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
132 size_t length = sizeof( has_altivec );
133 int error = sysctl( selectors, 2, &has_altivec, &length, NULL, 0 );
135 if( error == 0 && has_altivec != 0 )
137 cpu |= X264_CPU_ALTIVEC;
143 #elif defined( SYS_LINUX )
146 static sigjmp_buf jmpbuf;
147 static volatile sig_atomic_t canjump = 0;
149 static void sigill_handler( int sig )
153 signal( sig, SIG_DFL );
158 siglongjmp( jmpbuf, 1 );
161 uint32_t x264_cpu_detect( void )
163 static void (* oldsig)( int );
165 oldsig = signal( SIGILL, sigill_handler );
166 if( sigsetjmp( jmpbuf, 1 ) )
168 signal( SIGILL, oldsig );
173 asm volatile( "mtspr 256, %0\n\t"
179 signal( SIGILL, oldsig );
181 return X264_CPU_ALTIVEC;
185 void x264_cpu_restore( uint32_t cpu )
191 uint32_t x264_cpu_detect( void )
196 void x264_cpu_restore( uint32_t cpu )
203 int x264_cpu_num_processors( void )
205 #if !defined(HAVE_PTHREAD)
209 return pthread_num_processors_np();
211 #elif defined(SYS_LINUX)
215 memset( &p_aff, 0, sizeof(p_aff) );
216 sched_getaffinity( 0, sizeof(p_aff), &p_aff );
217 for( np = 0, bit = 0; bit < sizeof(p_aff); bit++ )
218 np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
221 #elif defined(SYS_BEOS)
223 get_system_info( &info );
224 return info.cpu_count;
226 #elif defined(SYS_MACOSX)
228 size_t length = sizeof( numberOfCPUs );
229 if( sysctlbyname("hw.ncpu", &numberOfCPUs, &length, NULL, 0) )