1 /*****************************************************************************
2 * checkasm.c: assembly check tool
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Laurent Aimar <fenrir@via.ecp.fr>
8 * Fiona Glaser <fiona@x264.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
23 *****************************************************************************/
30 #include "common/common.h"
31 #include "common/cpu.h"
33 /* buf1, buf2: initialised to random data and shouldn't write into them */
34 uint8_t * buf1, * buf2;
35 /* buf3, buf4: used to store output */
36 uint8_t * buf3, * buf4;
40 #define report( name ) { \
41 if( used_asm && !quiet ) \
42 fprintf( stderr, " - %-21s [%s]\n", name, ok ? "OK" : "FAILED" ); \
46 #define BENCH_RUNS 100 // tradeoff between accuracy and speed
47 #define BENCH_ALIGNS 16 // number of stack+heap data alignments (another accuracy vs speed tradeoff)
48 #define MAX_FUNCS 1000 // just has to be big enough to hold all the existing functions
49 #define MAX_CPUS 10 // number of different combinations of cpu flags
52 void *pointer; // just for detecting duplicates
60 bench_t vers[MAX_CPUS];
64 int bench_pattern_len = 0;
65 const char *bench_pattern = "";
67 static bench_func_t benchs[MAX_FUNCS];
69 static const char *pixel_names[10] = { "16x16", "16x8", "8x16", "8x8", "8x4", "4x8", "4x4", "4x2", "2x4", "2x2" };
70 static const char *intra_predict_16x16_names[7] = { "v", "h", "dc", "p", "dcl", "dct", "dc8" };
71 static const char *intra_predict_8x8c_names[7] = { "dc", "h", "v", "p", "dcl", "dct", "dc8" };
72 static const char *intra_predict_4x4_names[12] = { "v", "h", "dc", "ddl", "ddr", "vr", "hd", "vl", "hu", "dcl", "dct", "dc8" };
73 static const char **intra_predict_8x8_names = intra_predict_4x4_names;
75 #define set_func_name(...) snprintf( func_name, sizeof(func_name), __VA_ARGS__ )
77 static inline uint32_t read_time(void)
79 #if defined(__GNUC__) && (defined(ARCH_X86) || defined(ARCH_X86_64))
81 asm volatile( "rdtsc" :"=a"(a) ::"edx" );
88 static bench_t* get_bench( const char *name, int cpu )
91 for( i=0; benchs[i].name && strcmp(name, benchs[i].name); i++ )
92 assert( i < MAX_FUNCS );
94 benchs[i].name = strdup( name );
96 return &benchs[i].vers[0];
97 for( j=1; benchs[i].vers[j].cpu && benchs[i].vers[j].cpu != cpu; j++ )
98 assert( j < MAX_CPUS );
99 benchs[i].vers[j].cpu = cpu;
100 return &benchs[i].vers[j];
103 int cmp_nop( const void *a, const void *b )
105 return *(uint16_t*)a - *(uint16_t*)b;
108 int cmp_bench( const void *a, const void *b )
110 // asciibetical sort except preserving numbers
111 const char *sa = ((bench_func_t*)a)->name;
112 const char *sb = ((bench_func_t*)b)->name;
115 if( !*sa && !*sb ) return 0;
116 if( isdigit(*sa) && isdigit(*sb) && isdigit(sa[1]) != isdigit(sb[1]) )
117 return isdigit(sa[1]) - isdigit(sb[1]);
118 if( *sa != *sb ) return *sa - *sb;
122 static void print_bench(void)
124 uint16_t nops[10000] = {0};
125 int i, j, k, nfuncs, nop_time=0;
127 for( i=0; i<10000; i++ )
130 nops[i] = read_time() - t;
132 qsort( nops, 10000, sizeof(uint16_t), cmp_nop );
133 for( i=500; i<9500; i++ )
136 printf( "nop: %d\n", nop_time );
138 for( i=0; i<MAX_FUNCS && benchs[i].name; i++ );
140 qsort( benchs, nfuncs, sizeof(bench_func_t), cmp_bench );
141 for( i=0; i<nfuncs; i++ )
142 for( j=0; j<MAX_CPUS && (!j || benchs[i].vers[j].cpu); j++ )
144 bench_t *b = &benchs[i].vers[j];
145 if( !b->den ) continue;
146 for( k=0; k<j && benchs[i].vers[k].pointer != b->pointer; k++ );
148 printf( "%s_%s%s: %"PRId64"\n", benchs[i].name,
149 b->cpu&X264_CPU_PHADD_IS_FAST ? "phadd" :
150 b->cpu&X264_CPU_SSSE3 ? "ssse3" :
151 b->cpu&X264_CPU_SSE3 ? "sse3" :
152 /* print sse2slow only if there's also a sse2fast version of the same func */
153 b->cpu&X264_CPU_SSE2_IS_SLOW && j<MAX_CPUS && b[1].cpu&X264_CPU_SSE2_IS_FAST && !(b[1].cpu&X264_CPU_SSE3) ? "sse2slow" :
154 b->cpu&X264_CPU_SSE2 ? "sse2" :
155 b->cpu&X264_CPU_MMX ? "mmx" : "c",
156 b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
157 b->cpu&X264_CPU_CACHELINE_64 ? "_c64" : "",
158 ((int64_t)10*b->cycles/b->den - nop_time)/4 );
162 #if defined(ARCH_X86) || defined(ARCH_X86_64)
163 int x264_stack_pagealign( int (*func)(), int align );
165 #define x264_stack_pagealign( func, align ) func()
168 #define call_c1(func,...) func(__VA_ARGS__)
171 /* detect when callee-saved regs aren't saved.
172 * needs an explicit asm check because it only sometimes crashes in normal use. */
173 long x264_checkasm_call( long (*func)(), int *ok, ... );
174 #define call_a1(func,...) x264_checkasm_call((long(*)())func, &ok, __VA_ARGS__)
176 #define call_a1 call_c1
179 #define call_bench(func,cpu,...)\
180 if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
185 call_a1(func, __VA_ARGS__);\
186 for( ti=0; ti<(cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
188 uint32_t t = read_time();\
193 t = read_time() - t;\
194 if( t*tcount <= tsum*4 && ti > 0 )\
200 bench_t *b = get_bench( func_name, cpu );\
206 /* for most functions, run benchmark and correctness test at the same time.
207 * for those that modify their inputs, run the above macros separately */
208 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
209 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
210 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
211 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
214 static int check_pixel( int cpu_ref, int cpu_new )
216 x264_pixel_function_t pixel_c;
217 x264_pixel_function_t pixel_ref;
218 x264_pixel_function_t pixel_asm;
219 x264_predict_t predict_16x16[4+3];
220 x264_predict_t predict_8x8c[4+3];
221 x264_predict_t predict_4x4[9+3];
222 x264_predict8x8_t predict_8x8[9+3];
223 DECLARE_ALIGNED_16( uint8_t edge[33] );
224 uint16_t cost_mv[32];
225 int ret = 0, ok, used_asm;
228 x264_pixel_init( 0, &pixel_c );
229 x264_pixel_init( cpu_ref, &pixel_ref );
230 x264_pixel_init( cpu_new, &pixel_asm );
231 x264_predict_16x16_init( 0, predict_16x16 );
232 x264_predict_8x8c_init( 0, predict_8x8c );
233 x264_predict_8x8_init( 0, predict_8x8 );
234 x264_predict_4x4_init( 0, predict_4x4 );
235 x264_predict_8x8_filter( buf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
237 #define TEST_PIXEL( name, align ) \
238 for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
240 int res_c, res_asm; \
241 if( pixel_asm.name[i] != pixel_ref.name[i] ) \
243 set_func_name( "%s_%s", #name, pixel_names[i] ); \
244 for( j=0; j<64; j++ ) \
247 res_c = call_c( pixel_c.name[i], buf1, 16, buf2+j*!align, 64 ); \
248 res_asm = call_a( pixel_asm.name[i], buf1, 16, buf2+j*!align, 64 ); \
249 if( res_c != res_asm ) \
252 fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
258 report( "pixel " #name " :" );
260 TEST_PIXEL( sad, 0 );
261 TEST_PIXEL( ssd, 1 );
262 TEST_PIXEL( satd, 0 );
263 TEST_PIXEL( sa8d, 0 );
265 #define TEST_PIXEL_X( N ) \
266 for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
268 int res_c[4]={0}, res_asm[4]={0}; \
269 if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
271 set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
272 for( j=0; j<64; j++) \
274 uint8_t *pix2 = buf2+j; \
276 res_c[0] = pixel_c.sad[i]( buf1, 16, pix2, 64 ); \
277 res_c[1] = pixel_c.sad[i]( buf1, 16, pix2+6, 64 ); \
278 res_c[2] = pixel_c.sad[i]( buf1, 16, pix2+1, 64 ); \
281 res_c[3] = pixel_c.sad[i]( buf1, 16, pix2+10, 64 ); \
282 call_a( pixel_asm.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
285 call_a( pixel_asm.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
286 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
289 fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
290 i, res_c[0], res_c[1], res_c[2], res_c[3], \
291 res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
294 call_c2( pixel_c.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
296 call_c2( pixel_c.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
300 report( "pixel sad_x"#N" :" );
305 #define TEST_PIXEL_VAR( i ) \
306 if( pixel_asm.var[i] != pixel_ref.var[i] ) \
308 uint32_t res_c, res_asm; \
309 uint32_t sad_c, sad_asm; \
310 set_func_name( "%s_%s", "var", pixel_names[i] ); \
312 res_c = call_c( pixel_c.var[i], buf1, 16, &sad_c ); \
313 res_asm = call_a( pixel_asm.var[i], buf1, 16, &sad_asm ); \
314 if( (res_c != res_asm) || (sad_c != sad_asm) ) \
317 fprintf( stderr, "var[%d]: %d,%d != %d,%d [FAILED]\n", i, res_c, sad_c, res_asm, sad_asm ); \
321 ok = 1; used_asm = 0;
322 TEST_PIXEL_VAR( PIXEL_16x16 );
323 TEST_PIXEL_VAR( PIXEL_8x8 );
324 report( "pixel var :" );
326 #define TEST_INTRA_SATD( name, pred, satd, i8x8, ... ) \
327 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
329 int res_c[3], res_asm[3]; \
330 set_func_name( #name );\
332 memcpy( buf3, buf2, 1024 ); \
333 for( i=0; i<3; i++ ) \
335 pred[i]( buf3+40, ##__VA_ARGS__ ); \
336 res_c[i] = pixel_c.satd( buf1+40, 16, buf3+40, 32 ); \
338 call_a( pixel_asm.name, buf1+40, i8x8 ? edge : buf3+40, res_asm ); \
339 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
342 fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
343 res_c[0], res_c[1], res_c[2], \
344 res_asm[0], res_asm[1], res_asm[2] ); \
348 ok = 1; used_asm = 0;
349 TEST_INTRA_SATD( intra_satd_x3_16x16, predict_16x16, satd[PIXEL_16x16], 0 );
350 TEST_INTRA_SATD( intra_satd_x3_8x8c, predict_8x8c, satd[PIXEL_8x8], 0 );
351 TEST_INTRA_SATD( intra_satd_x3_4x4, predict_4x4, satd[PIXEL_4x4], 0 );
352 TEST_INTRA_SATD( intra_sa8d_x3_8x8, predict_8x8, sa8d[PIXEL_8x8], 1, edge );
353 report( "intra satd_x3 :" );
355 if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
356 pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
359 int sums[5][4] = {{0}};
362 res_c = x264_pixel_ssim_wxh( &pixel_c, buf1+2, 32, buf2+2, 32, 32, 28 );
363 res_a = x264_pixel_ssim_wxh( &pixel_asm, buf1+2, 32, buf2+2, 32, 32, 28 );
364 if( fabs(res_c - res_a) > 1e-6 )
367 fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
369 set_func_name( "ssim_core" );
370 call_c2( pixel_c.ssim_4x4x2_core, buf1+2, 32, buf2+2, 32, sums );
371 call_a2( pixel_asm.ssim_4x4x2_core, buf1+2, 32, buf2+2, 32, sums );
372 set_func_name( "ssim_end" );
373 call_c2( pixel_c.ssim_end4, sums, sums, 4 );
374 call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
378 ok = 1; used_asm = 0;
379 for( i=0; i<32; i++ )
381 for( i=0; i<100 && ok; i++ )
382 if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
384 DECLARE_ALIGNED_16( uint16_t sums[72] );
385 DECLARE_ALIGNED_16( int dc[4] );
386 int16_t mvs_a[32], mvs_c[32];
388 int thresh = rand() & 0x3fff;
389 set_func_name( "esa_ads" );
390 for( j=0; j<72; j++ )
391 sums[j] = rand() & 0x3fff;
393 dc[j] = rand() & 0x3fff;
395 mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
396 mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
397 if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
400 printf("c%d: ", i&3);
401 for(j=0; j<mvn_c; j++)
402 printf("%d ", mvs_c[j]);
403 printf("\na%d: ", i&3);
404 for(j=0; j<mvn_a; j++)
405 printf("%d ", mvs_a[j]);
409 report( "esa ads:" );
414 static int check_dct( int cpu_ref, int cpu_new )
416 x264_dct_function_t dct_c;
417 x264_dct_function_t dct_ref;
418 x264_dct_function_t dct_asm;
419 x264_quant_function_t qf;
420 int ret = 0, ok, used_asm, i, interlace;
421 DECLARE_ALIGNED_16( int16_t dct1[16][4][4] );
422 DECLARE_ALIGNED_16( int16_t dct2[16][4][4] );
423 DECLARE_ALIGNED_16( int16_t dct4[16][4][4] );
424 DECLARE_ALIGNED_16( int16_t dct8[4][8][8] );
428 x264_dct_init( 0, &dct_c );
429 x264_dct_init( cpu_ref, &dct_ref);
430 x264_dct_init( cpu_new, &dct_asm );
432 memset( h, 0, sizeof(*h) );
433 h->pps = h->pps_array;
434 x264_param_default( &h->param );
435 h->param.analyse.i_luma_deadzone[0] = 0;
436 h->param.analyse.i_luma_deadzone[1] = 0;
437 h->param.analyse.b_transform_8x8 = 1;
439 h->pps->scaling_list[i] = x264_cqm_flat16;
441 x264_quant_init( h, 0, &qf );
443 #define TEST_DCT( name, t1, t2, size ) \
444 if( dct_asm.name != dct_ref.name ) \
446 set_func_name( #name );\
448 call_c( dct_c.name, t1, buf1, buf2 ); \
449 call_a( dct_asm.name, t2, buf1, buf2 ); \
450 if( memcmp( t1, t2, size ) ) \
453 fprintf( stderr, #name " [FAILED]\n" ); \
456 ok = 1; used_asm = 0;
457 TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
458 TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
459 TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
460 report( "sub_dct4 :" );
462 ok = 1; used_asm = 0;
463 TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64*2 );
464 TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*2*4 );
465 report( "sub_dct8 :" );
468 // fdct and idct are denormalized by different factors, so quant/dequant
469 // is needed to force the coefs into the right range.
470 dct_c.sub16x16_dct( dct4, buf1, buf2 );
471 dct_c.sub16x16_dct8( dct8, buf1, buf2 );
472 for( i=0; i<16; i++ )
474 qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
475 qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
479 qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
480 qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
483 #define TEST_IDCT( name, src ) \
484 if( dct_asm.name != dct_ref.name ) \
486 set_func_name( #name );\
488 memcpy( buf3, buf1, 32*32 ); \
489 memcpy( buf4, buf1, 32*32 ); \
490 memcpy( dct1, src, 512 ); \
491 memcpy( dct2, src, 512 ); \
492 call_c1( dct_c.name, buf3, (void*)dct1 ); \
493 call_a1( dct_asm.name, buf4, (void*)dct2 ); \
494 if( memcmp( buf3, buf4, 32*32 ) ) \
497 fprintf( stderr, #name " [FAILED]\n" ); \
499 call_c2( dct_c.name, buf3, (void*)dct1 ); \
500 call_a2( dct_asm.name, buf4, (void*)dct2 ); \
502 ok = 1; used_asm = 0;
503 TEST_IDCT( add4x4_idct, dct4 );
504 TEST_IDCT( add8x8_idct, dct4 );
505 TEST_IDCT( add16x16_idct, dct4 );
506 report( "add_idct4 :" );
508 ok = 1; used_asm = 0;
509 TEST_IDCT( add8x8_idct8, dct8 );
510 TEST_IDCT( add16x16_idct8, dct8 );
511 report( "add_idct8 :" );
514 ok = 1; used_asm = 0;
515 if( dct_asm.dct4x4dc != dct_ref.dct4x4dc )
517 DECLARE_ALIGNED_16( int16_t dct1[4][4] ) = {{-12, 42, 23, 67},{2, 90, 89,56},{67,43,-76,91},{56,-78,-54,1}};
518 DECLARE_ALIGNED_16( int16_t dct2[4][4] ) = {{-12, 42, 23, 67},{2, 90, 89,56},{67,43,-76,91},{56,-78,-54,1}};
519 set_func_name( "dct4x4dc" );
521 call_c1( dct_c.dct4x4dc, dct1 );
522 call_a1( dct_asm.dct4x4dc, dct2 );
523 if( memcmp( dct1, dct2, 32 ) )
526 fprintf( stderr, " - dct4x4dc : [FAILED]\n" );
528 call_c2( dct_c.dct4x4dc, dct1 );
529 call_a2( dct_asm.dct4x4dc, dct2 );
531 if( dct_asm.idct4x4dc != dct_ref.idct4x4dc )
533 DECLARE_ALIGNED_16( int16_t dct1[4][4] ) = {{-12, 42, 23, 67},{2, 90, 89,56},{67,43,-76,91},{56,-78,-54,1}};
534 DECLARE_ALIGNED_16( int16_t dct2[4][4] ) = {{-12, 42, 23, 67},{2, 90, 89,56},{67,43,-76,91},{56,-78,-54,1}};
535 set_func_name( "idct4x4dc" );
537 call_c1( dct_c.idct4x4dc, dct1 );
538 call_a1( dct_asm.idct4x4dc, dct2 );
539 if( memcmp( dct1, dct2, 32 ) )
542 fprintf( stderr, " - idct4x4dc : [FAILED]\n" );
544 call_c2( dct_c.idct4x4dc, dct1 );
545 call_a2( dct_asm.idct4x4dc, dct2 );
547 report( "(i)dct4x4dc :" );
549 ok = 1; used_asm = 0;
550 if( dct_asm.dct2x2dc != dct_ref.dct2x2dc )
552 DECLARE_ALIGNED_16( int16_t dct1[2][2] ) = {{-12, 42},{2, 90}};
553 DECLARE_ALIGNED_16( int16_t dct2[2][2] ) = {{-12, 42},{2, 90}};
554 set_func_name( "dct2x2dc" );
556 call_c( dct_c.dct2x2dc, dct1 );
557 call_a( dct_asm.dct2x2dc, dct2 );
558 if( memcmp( dct1, dct2, 4*2 ) )
561 fprintf( stderr, " - dct2x2dc : [FAILED]\n" );
564 if( dct_asm.idct2x2dc != dct_ref.idct2x2dc )
566 DECLARE_ALIGNED_16( int16_t dct1[2][2] ) = {{-12, 42},{2, 90}};
567 DECLARE_ALIGNED_16( int16_t dct2[2][2] ) = {{-12, 42},{2, 90}};
568 set_func_name( "idct2x2dc" );
570 call_c( dct_c.idct2x2dc, dct1 );
571 call_a( dct_asm.idct2x2dc, dct2 );
572 if( memcmp( dct1, dct2, 4*2 ) )
575 fprintf( stderr, " - idct2x2dc : [FAILED]\n" );
578 report( "(i)dct2x2dc :" );
580 x264_zigzag_function_t zigzag_c;
581 x264_zigzag_function_t zigzag_ref;
582 x264_zigzag_function_t zigzag_asm;
584 DECLARE_ALIGNED_16( int16_t level1[64] );
585 DECLARE_ALIGNED_16( int16_t level2[64] );
587 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
588 if( zigzag_asm.name != zigzag_ref.name ) \
590 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
592 call_c( zigzag_c.name, t1, dct ); \
593 call_a( zigzag_asm.name, t2, dct ); \
594 if( memcmp( t1, t2, size*sizeof(int16_t) ) ) \
597 fprintf( stderr, #name " [FAILED]\n" ); \
601 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
602 if( zigzag_asm.name != zigzag_ref.name ) \
604 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
606 memcpy( buf3, buf1, 16*FDEC_STRIDE ); \
607 memcpy( buf4, buf1, 16*FDEC_STRIDE ); \
608 call_c1( zigzag_c.name, t1, buf2, buf3 ); \
609 call_a1( zigzag_asm.name, t2, buf2, buf4 ); \
610 if( memcmp( t1, t2, size*sizeof(int16_t) )|| memcmp( buf3, buf4, 16*FDEC_STRIDE ) ) \
613 fprintf( stderr, #name " [FAILED]\n" ); \
615 call_c2( zigzag_c.name, t1, buf2, buf3 ); \
616 call_a2( zigzag_asm.name, t2, buf2, buf4 ); \
620 x264_zigzag_init( 0, &zigzag_c, 0 );
621 x264_zigzag_init( cpu_ref, &zigzag_ref, 0 );
622 x264_zigzag_init( cpu_new, &zigzag_asm, 0 );
624 ok = 1; used_asm = 0;
625 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
626 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
627 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
628 report( "zigzag_frame :" );
631 x264_zigzag_init( 0, &zigzag_c, 1 );
632 x264_zigzag_init( cpu_ref, &zigzag_ref, 1 );
633 x264_zigzag_init( cpu_new, &zigzag_asm, 1 );
635 ok = 1; used_asm = 0;
636 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
637 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
638 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
639 report( "zigzag_field :" );
640 #undef TEST_ZIGZAG_SCAN
641 #undef TEST_ZIGZAG_SUB
646 static int check_mc( int cpu_ref, int cpu_new )
648 x264_mc_functions_t mc_c;
649 x264_mc_functions_t mc_ref;
650 x264_mc_functions_t mc_a;
651 x264_pixel_function_t pixel;
653 uint8_t *src = &buf1[2*32+2];
654 uint8_t *src2[4] = { &buf1[3*64+2], &buf1[5*64+2],
655 &buf1[7*64+2], &buf1[9*64+2] };
656 uint8_t *dst1 = buf3;
657 uint8_t *dst2 = buf4;
659 int dx, dy, i, j, k, w;
660 int ret = 0, ok, used_asm;
662 x264_mc_init( 0, &mc_c );
663 x264_mc_init( cpu_ref, &mc_ref );
664 x264_mc_init( cpu_new, &mc_a );
665 x264_pixel_init( 0, &pixel );
667 #define MC_TEST_LUMA( w, h ) \
668 if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
670 set_func_name( "mc_luma_%dx%d", w, h );\
672 memset(buf3, 0xCD, 1024); \
673 memset(buf4, 0xCD, 1024); \
674 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
675 call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h ); \
676 if( memcmp( buf3, buf4, 1024 ) ) \
678 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
682 if( mc_a.get_ref != mc_ref.get_ref ) \
684 uint8_t *ref = dst2; \
685 int ref_stride = 32; \
686 set_func_name( "get_ref_%dx%d", w, h );\
688 memset(buf3, 0xCD, 1024); \
689 memset(buf4, 0xCD, 1024); \
690 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
691 ref = (uint8_t*) call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h ); \
692 for( i=0; i<h; i++ ) \
693 if( memcmp( dst1+i*32, ref+i*ref_stride, w ) ) \
695 fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
701 #define MC_TEST_CHROMA( w, h ) \
702 if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
704 set_func_name( "mc_chroma_%dx%d", w, h );\
706 memset(buf3, 0xCD, 1024); \
707 memset(buf4, 0xCD, 1024); \
708 call_c( mc_c.mc_chroma, dst1, 16, src, 32, dx, dy, w, h ); \
709 call_a( mc_a.mc_chroma, dst2, 16, src, 32, dx, dy, w, h ); \
710 /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */\
711 for( j=0; j<h; j++ ) \
712 for( i=w; i<4; i++ ) \
713 dst2[i+j*16] = dst1[i+j*16]; \
714 if( memcmp( buf3, buf4, 1024 ) ) \
716 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
720 ok = 1; used_asm = 0;
721 for( dy = -8; dy < 8; dy++ )
722 for( dx = -128; dx < 128; dx++ )
724 if( rand()&15 ) continue; // running all of them is too slow
725 MC_TEST_LUMA( 20, 18 );
726 MC_TEST_LUMA( 16, 16 );
727 MC_TEST_LUMA( 16, 8 );
728 MC_TEST_LUMA( 12, 10 );
729 MC_TEST_LUMA( 8, 16 );
730 MC_TEST_LUMA( 8, 8 );
731 MC_TEST_LUMA( 8, 4 );
732 MC_TEST_LUMA( 4, 8 );
733 MC_TEST_LUMA( 4, 4 );
735 report( "mc luma :" );
737 ok = 1; used_asm = 0;
738 for( dy = -1; dy < 9; dy++ )
739 for( dx = -1; dx < 9; dx++ )
741 MC_TEST_CHROMA( 8, 8 );
742 MC_TEST_CHROMA( 8, 4 );
743 MC_TEST_CHROMA( 4, 8 );
744 MC_TEST_CHROMA( 4, 4 );
745 MC_TEST_CHROMA( 4, 2 );
746 MC_TEST_CHROMA( 2, 4 );
747 MC_TEST_CHROMA( 2, 2 );
749 report( "mc chroma :" );
751 #undef MC_TEST_CHROMA
753 #define MC_TEST_AVG( name, ... ) \
754 for( i = 0, ok = 1, used_asm = 0; i < 10; i++ ) \
756 memcpy( buf3, buf1, 1024 ); \
757 memcpy( buf4, buf1, 1024 ); \
758 if( mc_a.name[i] != mc_ref.name[i] ) \
760 set_func_name( "%s_%s", #name, pixel_names[i] );\
762 call_c1( mc_c.name[i], buf3, 32, buf2, 16, ##__VA_ARGS__ ); \
763 call_a1( mc_a.name[i], buf4, 32, buf2, 16, ##__VA_ARGS__ ); \
764 if( memcmp( buf3, buf4, 1024 ) ) \
767 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
769 call_c2( mc_c.name[i], buf3, 32, buf2, 16, ##__VA_ARGS__ ); \
770 call_a2( mc_a.name[i], buf4, 32, buf2, 16, ##__VA_ARGS__ ); \
774 report( "mc avg :" );
775 ok = 1; used_asm = 0;
776 for( w = -64; w <= 128 && ok; w++ )
777 MC_TEST_AVG( avg_weight, w );
778 report( "mc wpredb :" );
780 if( mc_a.hpel_filter != mc_ref.hpel_filter )
782 uint8_t *src = buf1+8+2*64;
783 uint8_t *dstc[3] = { buf3+8, buf3+8+16*64, buf3+8+32*64 };
784 uint8_t *dsta[3] = { buf4+8, buf4+8+16*64, buf4+8+32*64 };
785 set_func_name( "hpel_filter" );
786 ok = 1; used_asm = 1;
787 memset( buf3, 0, 4096 );
788 memset( buf4, 0, 4096 );
789 call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], src, 64, 48, 10 );
790 call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], src, 64, 48, 10 );
792 for( j=0; j<10; j++ )
793 //FIXME ideally the first pixels would match too, but they aren't actually used
794 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 ) )
797 fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
798 for( k=0; k<48; k++ )
799 printf("%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " ");
801 for( k=0; k<48; k++ )
802 printf("%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " ");
806 report( "hpel filter :" );
809 if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
811 uint8_t *dstc[4] = { buf3, buf3+1024, buf3+2048, buf3+3072 };
812 uint8_t *dsta[4] = { buf4, buf4+1024, buf4+2048, buf3+3072 };
813 set_func_name( "lowres_init" );
814 for( w=40; w<=48; w+=8 )
815 if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
817 int stride = (w+8)&~15;
819 call_c( mc_c.frame_init_lowres_core, buf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
820 call_a( mc_a.frame_init_lowres_core, buf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
824 if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w ) )
827 fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
829 printf( "%d ", dstc[j][k+i*stride] );
832 printf( "%d ", dsta[j][k+i*stride] );
838 report( "lowres init :" );
844 static int check_deblock( int cpu_ref, int cpu_new )
846 x264_deblock_function_t db_c;
847 x264_deblock_function_t db_ref;
848 x264_deblock_function_t db_a;
849 int ret = 0, ok = 1, used_asm = 0;
850 int alphas[36], betas[36];
854 x264_deblock_init( 0, &db_c );
855 x264_deblock_init( cpu_ref, &db_ref );
856 x264_deblock_init( cpu_new, &db_a );
858 /* not exactly the real values of a,b,tc but close enough */
860 for( i = 35; i >= 0; i-- )
864 tcs[i][0] = tcs[i][2] = (c+6)/10;
865 tcs[i][1] = tcs[i][3] = (c+9)/20;
870 #define TEST_DEBLOCK( name, align, ... ) \
871 for( i = 0; i < 36; i++ ) \
873 int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */\
874 for( j = 0; j < 1024; j++ ) \
875 /* two distributions of random to excersize different failure modes */\
876 buf3[j] = rand() & (i&1 ? 0xf : 0xff ); \
877 memcpy( buf4, buf3, 1024 ); \
878 if( db_a.name != db_ref.name ) \
880 set_func_name( #name );\
882 call_c1( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
883 call_a1( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
884 if( memcmp( buf3, buf4, 1024 ) ) \
887 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
890 call_c2( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
891 call_a2( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
895 TEST_DEBLOCK( deblock_h_luma, 0, tcs[i] );
896 TEST_DEBLOCK( deblock_v_luma, 1, tcs[i] );
897 TEST_DEBLOCK( deblock_h_chroma, 0, tcs[i] );
898 TEST_DEBLOCK( deblock_v_chroma, 1, tcs[i] );
899 TEST_DEBLOCK( deblock_h_luma_intra, 0 );
900 TEST_DEBLOCK( deblock_v_luma_intra, 1 );
901 TEST_DEBLOCK( deblock_h_chroma_intra, 0 );
902 TEST_DEBLOCK( deblock_v_chroma_intra, 1 );
904 report( "deblock :" );
909 static int check_quant( int cpu_ref, int cpu_new )
911 x264_quant_function_t qf_c;
912 x264_quant_function_t qf_ref;
913 x264_quant_function_t qf_a;
914 DECLARE_ALIGNED_16( int16_t dct1[64] );
915 DECLARE_ALIGNED_16( int16_t dct2[64] );
916 DECLARE_ALIGNED_16( uint8_t cqm_buf[64] );
917 int ret = 0, ok, used_asm;
918 int oks[2] = {1,1}, used_asms[2] = {0,0};
922 memset( h, 0, sizeof(*h) );
923 h->pps = h->pps_array;
924 x264_param_default( &h->param );
925 h->param.rc.i_qp_min = 26;
926 h->param.analyse.b_transform_8x8 = 1;
928 for( i_cqm = 0; i_cqm < 4; i_cqm++ )
932 for( i = 0; i < 6; i++ )
933 h->pps->scaling_list[i] = x264_cqm_flat16;
934 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
936 else if( i_cqm == 1 )
938 for( i = 0; i < 6; i++ )
939 h->pps->scaling_list[i] = x264_cqm_jvt[i];
940 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
945 for( i = 0; i < 64; i++ )
946 cqm_buf[i] = 10 + rand() % 246;
948 for( i = 0; i < 64; i++ )
950 for( i = 0; i < 6; i++ )
951 h->pps->scaling_list[i] = cqm_buf;
952 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
956 x264_quant_init( h, 0, &qf_c );
957 x264_quant_init( h, cpu_ref, &qf_ref );
958 x264_quant_init( h, cpu_new, &qf_a );
960 #define INIT_QUANT8() \
962 static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
964 for( y = 0; y < 8; y++ ) \
965 for( x = 0; x < 8; x++ ) \
967 unsigned int scale = (255*scale1d[y]*scale1d[x])/16; \
968 dct1[y*8+x] = dct2[y*8+x] = (rand()%(2*scale+1))-scale; \
972 #define INIT_QUANT4() \
974 static const int scale1d[4] = {4,6,4,6}; \
976 for( y = 0; y < 4; y++ ) \
977 for( x = 0; x < 4; x++ ) \
979 unsigned int scale = 255*scale1d[y]*scale1d[x]; \
980 dct1[y*4+x] = dct2[y*4+x] = (rand()%(2*scale+1))-scale; \
984 #define TEST_QUANT_DC( name, cqm ) \
985 if( qf_a.name != qf_ref.name ) \
987 set_func_name( #name ); \
989 for( qp = 51; qp > 0; qp-- ) \
991 for( i = 0; i < 16; i++ ) \
992 dct1[i] = dct2[i] = (rand() & 0x1fff) - 0xfff; \
993 call_c1( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
994 call_a1( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
995 if( memcmp( dct1, dct2, 16*2 ) ) \
998 fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1001 call_c2( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1002 call_a2( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1006 #define TEST_QUANT( qname, block, w ) \
1007 if( qf_a.qname != qf_ref.qname ) \
1009 set_func_name( #qname ); \
1011 for( qp = 51; qp > 0; qp-- ) \
1014 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1015 call_a1( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1016 if( memcmp( dct1, dct2, w*w*2 ) ) \
1019 fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1022 call_c2( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1023 call_a2( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1027 TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1028 TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1029 TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1030 TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1031 TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1032 TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1034 #define TEST_DEQUANT( qname, dqname, block, w ) \
1035 if( qf_a.dqname != qf_ref.dqname ) \
1037 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1039 for( qp = 51; qp > 0; qp-- ) \
1042 call_c( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1043 memcpy( dct2, dct1, w*w*2 ); \
1044 call_c1( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1045 call_a1( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1046 if( memcmp( dct1, dct2, w*w*2 ) ) \
1049 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1052 call_c2( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1053 call_a2( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1057 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1058 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1059 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1060 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1062 x264_cqm_delete( h );
1065 ok = oks[0]; used_asm = used_asms[0];
1066 report( "quant :" );
1068 ok = oks[1]; used_asm = used_asms[1];
1069 report( "dequant :" );
1072 if( qf_a.denoise_dct_core != qf_ref.denoise_dct_core )
1075 for( size = 16; size <= 64; size += 48 )
1077 set_func_name( "denoise_dct" );
1079 memcpy(dct1, buf1, size*2);
1080 memcpy(dct2, buf1, size*2);
1081 memcpy(buf3+256, buf3, 256);
1082 call_c1( qf_c.denoise_dct_core, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1083 call_a1( qf_a.denoise_dct_core, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1084 if( memcmp( dct1, dct2, size*2 ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1086 call_c2( qf_c.denoise_dct_core, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1087 call_a2( qf_a.denoise_dct_core, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1090 report( "denoise dct :" );
1095 static int check_intra( int cpu_ref, int cpu_new )
1097 int ret = 0, ok = 1, used_asm = 0;
1099 DECLARE_ALIGNED_16( uint8_t edge[33] );
1102 x264_predict_t predict_16x16[4+3];
1103 x264_predict_t predict_8x8c[4+3];
1104 x264_predict8x8_t predict_8x8[9+3];
1105 x264_predict_t predict_4x4[9+3];
1106 } ip_c, ip_ref, ip_a;
1108 x264_predict_16x16_init( 0, ip_c.predict_16x16 );
1109 x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
1110 x264_predict_8x8_init( 0, ip_c.predict_8x8 );
1111 x264_predict_4x4_init( 0, ip_c.predict_4x4 );
1113 x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
1114 x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
1115 x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8 );
1116 x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
1118 x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
1119 x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
1120 x264_predict_8x8_init( cpu_new, ip_a.predict_8x8 );
1121 x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
1123 x264_predict_8x8_filter( buf1+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
1125 #define INTRA_TEST( name, dir, w, ... ) \
1126 if( ip_a.name[dir] != ip_ref.name[dir] )\
1128 set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
1130 memcpy( buf3, buf1, 32*20 );\
1131 memcpy( buf4, buf1, 32*20 );\
1132 call_c( ip_c.name[dir], buf3+48, ##__VA_ARGS__ );\
1133 call_a( ip_a.name[dir], buf4+48, ##__VA_ARGS__ );\
1134 if( memcmp( buf3, buf4, 32*20 ) )\
1136 fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\
1139 for(k=-1; k<16; k++)\
1140 printf("%2x ", edge[16+k]);\
1142 for(j=0; j<w; j++){\
1143 printf("%2x ", edge[14-j]);\
1145 printf("%2x ", buf4[48+k+j*32]);\
1149 for(j=0; j<w; j++){\
1152 printf("%2x ", buf3[48+k+j*32]);\
1158 for( i = 0; i < 12; i++ )
1159 INTRA_TEST( predict_4x4, i, 4 );
1160 for( i = 0; i < 7; i++ )
1161 INTRA_TEST( predict_8x8c, i, 8 );
1162 for( i = 0; i < 7; i++ )
1163 INTRA_TEST( predict_16x16, i, 16 );
1164 for( i = 0; i < 12; i++ )
1165 INTRA_TEST( predict_8x8, i, 8, edge );
1167 report( "intra pred :" );
1171 #define DECL_CABAC(cpu) \
1172 static void run_cabac_##cpu( uint8_t *dst )\
1176 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1177 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1178 for( i=0; i<0x1000; i++ )\
1179 x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
1185 #define run_cabac_asm run_cabac_c
1188 static int check_cabac( int cpu_ref, int cpu_new )
1190 int ret = 0, ok, used_asm = 1;
1191 if( cpu_ref || run_cabac_c == run_cabac_asm)
1193 set_func_name( "cabac_encode_decision" );
1194 memcpy( buf4, buf3, 0x1000 );
1195 call_c( run_cabac_c, buf3 );
1196 call_a( run_cabac_asm, buf4 );
1197 ok = !memcmp( buf3, buf4, 0x1000 );
1198 report( "cabac :" );
1202 int check_all_funcs( int cpu_ref, int cpu_new )
1204 return check_pixel( cpu_ref, cpu_new )
1205 + check_dct( cpu_ref, cpu_new )
1206 + check_mc( cpu_ref, cpu_new )
1207 + check_intra( cpu_ref, cpu_new )
1208 + check_deblock( cpu_ref, cpu_new )
1209 + check_quant( cpu_ref, cpu_new )
1210 + check_cabac( cpu_ref, cpu_new );
1213 int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
1215 *cpu_ref = *cpu_new;
1217 if( *cpu_new & X264_CPU_SSE2_IS_FAST )
1218 *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
1220 fprintf( stderr, "x264: %s\n", name );
1221 return check_all_funcs( *cpu_ref, *cpu_new );
1224 int check_all_flags( void )
1227 int cpu0 = 0, cpu1 = 0;
1229 if( x264_cpu_detect() & X264_CPU_MMXEXT )
1231 ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMXEXT, "MMX" );
1232 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
1233 cpu1 &= ~X264_CPU_CACHELINE_64;
1235 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
1236 cpu1 &= ~X264_CPU_CACHELINE_32;
1239 if( x264_cpu_detect() & X264_CPU_SSE2 )
1241 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
1242 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
1243 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
1245 if( x264_cpu_detect() & X264_CPU_SSE3 )
1246 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
1247 if( x264_cpu_detect() & X264_CPU_SSSE3 )
1249 cpu1 &= ~X264_CPU_CACHELINE_64;
1250 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
1251 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
1252 ret |= add_flags( &cpu0, &cpu1, X264_CPU_PHADD_IS_FAST, "PHADD" );
1255 if( x264_cpu_detect() & X264_CPU_ALTIVEC )
1257 fprintf( stderr, "x264: ALTIVEC against C\n" );
1258 ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
1264 int main(int argc, char *argv[])
1269 if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
1271 #if !defined(ARCH_X86) && !defined(ARCH_X86_64)
1272 fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
1276 if( argv[1][7] == '=' )
1278 bench_pattern = argv[1]+8;
1279 bench_pattern_len = strlen(bench_pattern);
1285 i = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
1286 fprintf( stderr, "x264: using random seed %u\n", i );
1289 buf1 = x264_malloc( 0x3e00 + 16*BENCH_ALIGNS );
1290 buf2 = buf1 + 0xf00;
1291 buf3 = buf2 + 0xf00;
1292 buf4 = buf3 + 0x1000;
1293 for( i=0; i<0x1e00; i++ )
1294 buf1[i] = rand() & 0xFF;
1295 memset( buf1+0x1e00, 0, 0x2000 );
1297 /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
1299 for( i=0; i<BENCH_ALIGNS && !ret; i++ )
1301 buf2 = buf1 + 0xf00;
1302 buf3 = buf2 + 0xf00;
1303 buf4 = buf3 + 0x1000;
1304 ret |= x264_stack_pagealign( check_all_flags, i*16 );
1307 fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
1310 ret = check_all_flags();
1314 fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
1317 fprintf( stderr, "x264: All tests passed Yeah :)\n" );