~ubuntu-branches/ubuntu/saucy/x264/saucy-updates

« back to all changes in this revision

Viewing changes to common/cpu.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2010-07-04 16:23:51 UTC
  • mto: (12.1.1 sid) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: james.westby@ubuntu.com-20100704162351-4r9pyu2l13du91l5
Tags: upstream-0.98.1653+git88b90d9
ImportĀ upstreamĀ versionĀ 0.98.1653+git88b90d9

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include "common.h"
27
27
#include "cpu.h"
28
28
 
29
 
#if defined(HAVE_PTHREAD) && defined(SYS_LINUX)
 
29
#if HAVE_PTHREAD && SYS_LINUX
30
30
#include <sched.h>
31
31
#endif
32
 
#ifdef SYS_BEOS
 
32
#if SYS_BEOS
33
33
#include <kernel/OS.h>
34
34
#endif
35
 
#if defined(SYS_MACOSX) || defined(SYS_FREEBSD)
 
35
#if SYS_MACOSX || SYS_FREEBSD
36
36
#include <sys/types.h>
37
37
#include <sys/sysctl.h>
38
38
#endif
39
 
#ifdef SYS_OPENBSD
 
39
#if SYS_OPENBSD
40
40
#include <sys/param.h>
41
41
#include <sys/sysctl.h>
42
42
#include <machine/cpu.h>
64
64
    {"ARMv6", X264_CPU_ARMV6},
65
65
    {"NEON",  X264_CPU_NEON},
66
66
    {"Fast_NEON_MRC",  X264_CPU_FAST_NEON_MRC},
 
67
    {"SlowCTZ", X264_CPU_SLOW_CTZ},
 
68
    {"SlowAtom", X264_CPU_SLOW_ATOM},
67
69
    {"", 0},
68
70
};
69
71
 
70
 
#if (defined(ARCH_PPC) && defined(SYS_LINUX)) || (defined(ARCH_ARM) && !defined(HAVE_NEON))
 
72
#if (ARCH_PPC && SYS_LINUX) || (ARCH_ARM && !HAVE_NEON)
71
73
#include <signal.h>
72
74
#include <setjmp.h>
73
75
static sigjmp_buf jmpbuf;
86
88
}
87
89
#endif
88
90
 
89
 
#ifdef HAVE_MMX
90
 
extern int  x264_cpu_cpuid_test( void );
91
 
extern uint32_t  x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
 
91
#if HAVE_MMX
 
92
int x264_cpu_cpuid_test( void );
 
93
uint32_t x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
92
94
 
93
95
uint32_t x264_cpu_detect( void )
94
96
{
98
100
    int max_extended_cap;
99
101
    int cache;
100
102
 
101
 
#ifndef ARCH_X86_64
 
103
#if !ARCH_X86_64
102
104
    if( !x264_cpu_cpuid_test() )
103
105
        return 0;
104
106
#endif
135
137
 
136
138
    if( !strcmp((char*)vendor, "AuthenticAMD") && max_extended_cap >= 0x80000001 )
137
139
    {
 
140
        cpu |= X264_CPU_SLOW_CTZ;
138
141
        x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
139
142
        if( edx&0x00400000 )
140
143
            cpu |= X264_CPU_MMXEXT;
145
148
                cpu |= X264_CPU_SSE2_IS_FAST;
146
149
                cpu |= X264_CPU_LZCNT;
147
150
                cpu |= X264_CPU_SHUFFLE_IS_FAST;
 
151
                cpu &= ~X264_CPU_SLOW_CTZ;
148
152
            }
149
153
            else
150
154
                cpu |= X264_CPU_SSE2_IS_SLOW;
159
163
 
160
164
    if( !strcmp((char*)vendor, "GenuineIntel") )
161
165
    {
162
 
        int family, model, stepping;
163
166
        x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
164
 
        family = ((eax>>8)&0xf) + ((eax>>20)&0xff);
165
 
        model  = ((eax>>4)&0xf) + ((eax>>12)&0xf0);
166
 
        stepping = eax&0xf;
 
167
        int family = ((eax>>8)&0xf) + ((eax>>20)&0xff);
 
168
        int model  = ((eax>>4)&0xf) + ((eax>>12)&0xf0);
167
169
        /* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and 6/14 (core1 "yonah")
168
170
         * theoretically support sse2, but it's significantly slower than mmx for
169
171
         * almost all of x264's functions, so let's just pretend they don't. */
170
 
        if( family==6 && (model==9 || model==13 || model==14) )
 
172
        if( family == 6 && (model == 9 || model == 13 || model == 14) )
171
173
        {
172
174
            cpu &= ~(X264_CPU_SSE2|X264_CPU_SSE3);
173
175
            assert(!(cpu&(X264_CPU_SSSE3|X264_CPU_SSE4)));
174
176
        }
 
177
        /* Detect Atom CPU */
 
178
        if( family == 6 && model == 28 )
 
179
        {
 
180
            cpu |= X264_CPU_SLOW_ATOM;
 
181
            cpu |= X264_CPU_SLOW_CTZ;
 
182
        }
175
183
    }
176
184
 
177
185
    if( (!strcmp((char*)vendor, "GenuineIntel") || !strcmp((char*)vendor, "CyrixInstead")) && !(cpu&X264_CPU_SSE42))
188
196
        {
189
197
            // Cache and TLB Information
190
198
            static const char cache32_ids[] = { 0x0a, 0x0c, 0x41, 0x42, 0x43, 0x44, 0x45, 0x82, 0x83, 0x84, 0x85, 0 };
191
 
            static const char cache64_ids[] = { 0x22, 0x23, 0x25, 0x29, 0x2c, 0x46, 0x47, 0x49, 0x60, 0x66, 0x67, 0x68, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7c, 0x7f, 0x86, 0x87, 0 };
 
199
            static const char cache64_ids[] = { 0x22, 0x23, 0x25, 0x29, 0x2c, 0x46, 0x47, 0x49, 0x60, 0x66, 0x67,
 
200
                                                0x68, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7c, 0x7f, 0x86, 0x87, 0 };
192
201
            uint32_t buf[4];
193
 
            int max, i=0, j;
 
202
            int max, i = 0;
194
203
            do {
195
204
                x264_cpu_cpuid( 2, buf+0, buf+1, buf+2, buf+3 );
196
205
                max = buf[0]&0xff;
197
206
                buf[0] &= ~0xff;
198
 
                for(j=0; j<4; j++)
 
207
                for( int j = 0; j < 4; j++ )
199
208
                    if( !(buf[j]>>31) )
200
209
                        while( buf[j] )
201
210
                        {
213
222
        else if( cache == 64 )
214
223
            cpu |= X264_CPU_CACHELINE_64;
215
224
        else
216
 
            fprintf( stderr, "x264 [warning]: unable to determine cacheline size\n" );
 
225
            x264_log( NULL, X264_LOG_WARNING, "unable to determine cacheline size\n" );
217
226
    }
218
227
 
219
 
#ifdef BROKEN_STACK_ALIGNMENT
 
228
#if BROKEN_STACK_ALIGNMENT
220
229
    cpu |= X264_CPU_STACK_MOD4;
221
230
#endif
222
231
 
223
232
    return cpu;
224
233
}
225
234
 
226
 
#elif defined( ARCH_PPC )
 
235
#elif ARCH_PPC
227
236
 
228
 
#if defined(SYS_MACOSX) || defined(SYS_OPENBSD)
 
237
#if SYS_MACOSX || SYS_OPENBSD
229
238
#include <sys/sysctl.h>
230
239
uint32_t x264_cpu_detect( void )
231
240
{
232
241
    /* Thank you VLC */
233
242
    uint32_t cpu = 0;
234
 
#ifdef SYS_OPENBSD
 
243
#if SYS_OPENBSD
235
244
    int      selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
236
245
#else
237
246
    int      selectors[2] = { CTL_HW, HW_VECTORUNIT };
241
250
    int      error = sysctl( selectors, 2, &has_altivec, &length, NULL, 0 );
242
251
 
243
252
    if( error == 0 && has_altivec != 0 )
244
 
    {
245
253
        cpu |= X264_CPU_ALTIVEC;
246
 
    }
247
254
 
248
255
    return cpu;
249
256
}
250
257
 
251
 
#elif defined( SYS_LINUX )
 
258
#elif SYS_LINUX
252
259
 
253
260
uint32_t x264_cpu_detect( void )
254
261
{
255
 
    static void (* oldsig)( int );
 
262
    static void (*oldsig)( int );
256
263
 
257
264
    oldsig = signal( SIGILL, sigill_handler );
258
265
    if( sigsetjmp( jmpbuf, 1 ) )
274
281
}
275
282
#endif
276
283
 
277
 
#elif defined( ARCH_ARM )
 
284
#elif ARCH_ARM
278
285
 
279
286
void x264_cpu_neon_test();
280
287
int x264_cpu_fast_neon_mrc_test();
282
289
uint32_t x264_cpu_detect( void )
283
290
{
284
291
    int flags = 0;
285
 
#ifdef HAVE_ARMV6
 
292
#if HAVE_ARMV6
286
293
    flags |= X264_CPU_ARMV6;
287
294
 
288
295
    // don't do this hack if compiled with -mfpu=neon
289
 
#ifndef HAVE_NEON
 
296
#if !HAVE_NEON
290
297
    static void (* oldsig)( int );
291
298
    oldsig = signal( SIGILL, sigill_handler );
292
299
    if( sigsetjmp( jmpbuf, 1 ) )
325
332
 
326
333
#endif
327
334
 
328
 
#ifndef HAVE_MMX
329
 
void x264_emms( void )
330
 
{
331
 
}
332
 
#endif
333
 
 
334
 
 
335
335
int x264_cpu_num_processors( void )
336
336
{
337
 
#if !defined(HAVE_PTHREAD)
 
337
#if !HAVE_PTHREAD
338
338
    return 1;
339
339
 
340
340
#elif defined(_WIN32)
341
341
    return pthread_num_processors_np();
342
342
 
343
 
#elif defined(SYS_LINUX)
 
343
#elif SYS_LINUX
344
344
    unsigned int bit;
345
345
    int np;
346
346
    cpu_set_t p_aff;
350
350
        np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
351
351
    return np;
352
352
 
353
 
#elif defined(SYS_BEOS)
 
353
#elif SYS_BEOS
354
354
    system_info info;
355
355
    get_system_info( &info );
356
356
    return info.cpu_count;
357
357
 
358
 
#elif defined(SYS_MACOSX) || defined(SYS_FREEBSD) || defined(SYS_OPENBSD)
 
358
#elif SYS_MACOSX || SYS_FREEBSD || SYS_OPENBSD
359
359
    int numberOfCPUs;
360
360
    size_t length = sizeof( numberOfCPUs );
361
 
#ifdef SYS_OPENBSD
 
361
#if SYS_OPENBSD
362
362
    int mib[2] = { CTL_HW, HW_NCPU };
363
363
    if( sysctl(mib, 2, &numberOfCPUs, &length, NULL, 0) )
364
364
#else