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" );
83 #elif defined(ARCH_PPC)
85 asm volatile( "mftb %0" : "=r" (a) );
92 static bench_t* get_bench( const char *name, int cpu )
95 for( i=0; benchs[i].name && strcmp(name, benchs[i].name); i++ )
96 assert( i < MAX_FUNCS );
98 benchs[i].name = strdup( name );
100 return &benchs[i].vers[0];
101 for( j=1; benchs[i].vers[j].cpu && benchs[i].vers[j].cpu != cpu; j++ )
102 assert( j < MAX_CPUS );
103 benchs[i].vers[j].cpu = cpu;
104 return &benchs[i].vers[j];
107 static int cmp_nop( const void *a, const void *b )
109 return *(uint16_t*)a - *(uint16_t*)b;
112 static int cmp_bench( const void *a, const void *b )
114 // asciibetical sort except preserving numbers
115 const char *sa = ((bench_func_t*)a)->name;
116 const char *sb = ((bench_func_t*)b)->name;
119 if( !*sa && !*sb ) return 0;
120 if( isdigit(*sa) && isdigit(*sb) && isdigit(sa[1]) != isdigit(sb[1]) )
121 return isdigit(sa[1]) - isdigit(sb[1]);
122 if( *sa != *sb ) return *sa - *sb;
126 static void print_bench(void)
128 uint16_t nops[10000] = {0};
129 int i, j, k, nfuncs, nop_time=0;
131 for( i=0; i<10000; i++ )
134 nops[i] = read_time() - t;
136 qsort( nops, 10000, sizeof(uint16_t), cmp_nop );
137 for( i=500; i<9500; i++ )
140 printf( "nop: %d\n", nop_time );
142 for( i=0; i<MAX_FUNCS && benchs[i].name; i++ );
144 qsort( benchs, nfuncs, sizeof(bench_func_t), cmp_bench );
145 for( i=0; i<nfuncs; i++ )
146 for( j=0; j<MAX_CPUS && (!j || benchs[i].vers[j].cpu); j++ )
148 bench_t *b = &benchs[i].vers[j];
149 if( !b->den ) continue;
150 for( k=0; k<j && benchs[i].vers[k].pointer != b->pointer; k++ );
152 printf( "%s_%s%s: %"PRId64"\n", benchs[i].name,
153 b->cpu&X264_CPU_SSE4 ? "sse4" :
154 b->cpu&X264_CPU_PHADD_IS_FAST ? "phadd" :
155 b->cpu&X264_CPU_SSSE3 ? "ssse3" :
156 b->cpu&X264_CPU_SSE3 ? "sse3" :
157 /* print sse2slow only if there's also a sse2fast version of the same func */
158 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" :
159 b->cpu&X264_CPU_SSE2 ? "sse2" :
160 b->cpu&X264_CPU_MMX ? "mmx" :
161 b->cpu&X264_CPU_ALTIVEC ? "altivec" : "c",
162 b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
163 b->cpu&X264_CPU_CACHELINE_64 ? "_c64" :
164 b->cpu&X264_CPU_SSE_MISALIGN ? "_misalign" :
165 b->cpu&X264_CPU_LZCNT ? "_lzcnt" : "",
166 ((int64_t)10*b->cycles/b->den - nop_time)/4 );
170 #if defined(ARCH_X86) || defined(ARCH_X86_64)
171 int x264_stack_pagealign( int (*func)(), int align );
173 #define x264_stack_pagealign( func, align ) func()
176 #define call_c1(func,...) func(__VA_ARGS__)
179 /* detect when callee-saved regs aren't saved.
180 * needs an explicit asm check because it only sometimes crashes in normal use. */
181 long x264_checkasm_call( long (*func)(), int *ok, ... );
182 #define call_a1(func,...) x264_checkasm_call((long(*)())func, &ok, __VA_ARGS__)
184 #define call_a1 call_c1
187 #define call_bench(func,cpu,...)\
188 if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
193 call_a1(func, __VA_ARGS__);\
194 for( ti=0; ti<(cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
196 uint32_t t = read_time();\
201 t = read_time() - t;\
202 if( t*tcount <= tsum*4 && ti > 0 )\
208 bench_t *b = get_bench( func_name, cpu );\
214 /* for most functions, run benchmark and correctness test at the same time.
215 * for those that modify their inputs, run the above macros separately */
216 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
217 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
218 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
219 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
222 static int check_pixel( int cpu_ref, int cpu_new )
224 x264_pixel_function_t pixel_c;
225 x264_pixel_function_t pixel_ref;
226 x264_pixel_function_t pixel_asm;
227 x264_predict_t predict_16x16[4+3];
228 x264_predict_t predict_8x8c[4+3];
229 x264_predict_t predict_4x4[9+3];
230 x264_predict8x8_t predict_8x8[9+3];
231 DECLARE_ALIGNED_16( uint8_t edge[33] );
232 uint16_t cost_mv[32];
233 int ret = 0, ok, used_asm;
236 x264_pixel_init( 0, &pixel_c );
237 x264_pixel_init( cpu_ref, &pixel_ref );
238 x264_pixel_init( cpu_new, &pixel_asm );
239 x264_predict_16x16_init( 0, predict_16x16 );
240 x264_predict_8x8c_init( 0, predict_8x8c );
241 x264_predict_8x8_init( 0, predict_8x8 );
242 x264_predict_4x4_init( 0, predict_4x4 );
243 x264_predict_8x8_filter( buf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
246 for( i=0; i<256; i++ )
251 buf3[i] = ~(buf4[i] = -(z&1));
253 // random pattern made of maxed pixel differences, in case an intermediate value overflows
254 for( ; i<0x1000; i++ )
255 buf3[i] = ~(buf4[i] = -(buf1[i&~0x88]&1));
257 #define TEST_PIXEL( name, align ) \
258 for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
260 int res_c, res_asm; \
261 if( pixel_asm.name[i] != pixel_ref.name[i] ) \
263 set_func_name( "%s_%s", #name, pixel_names[i] ); \
265 for( j=0; j<64; j++ ) \
267 res_c = call_c( pixel_c.name[i], buf1, 16, buf2+j*!align, 64 ); \
268 res_asm = call_a( pixel_asm.name[i], buf1, 16, buf2+j*!align, 64 ); \
269 if( res_c != res_asm ) \
272 fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
276 for( j=0; j<0x1000 && ok; j+=256 ) \
278 res_c = pixel_c .name[i]( buf3+j, 16, buf4+j, 16 ); \
279 res_asm = pixel_asm.name[i]( buf3+j, 16, buf4+j, 16 ); \
280 if( res_c != res_asm ) \
283 fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
288 report( "pixel " #name " :" );
290 TEST_PIXEL( sad, 0 );
291 TEST_PIXEL( sad_aligned, 1 );
292 TEST_PIXEL( ssd, 1 );
293 TEST_PIXEL( satd, 0 );
294 TEST_PIXEL( sa8d, 0 );
296 #define TEST_PIXEL_X( N ) \
297 for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
299 int res_c[4]={0}, res_asm[4]={0}; \
300 if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
302 set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
304 for( j=0; j<64; j++) \
306 uint8_t *pix2 = buf2+j; \
307 res_c[0] = pixel_c.sad[i]( buf1, 16, pix2, 64 ); \
308 res_c[1] = pixel_c.sad[i]( buf1, 16, pix2+6, 64 ); \
309 res_c[2] = pixel_c.sad[i]( buf1, 16, pix2+1, 64 ); \
312 res_c[3] = pixel_c.sad[i]( buf1, 16, pix2+10, 64 ); \
313 call_a( pixel_asm.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
316 call_a( pixel_asm.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
317 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
320 fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
321 i, res_c[0], res_c[1], res_c[2], res_c[3], \
322 res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
325 call_c2( pixel_c.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
327 call_c2( pixel_c.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
331 report( "pixel sad_x"#N" :" );
336 #define TEST_PIXEL_VAR( i ) \
337 if( pixel_asm.var[i] != pixel_ref.var[i] ) \
339 int res_c, res_asm; \
340 set_func_name( "%s_%s", "var", pixel_names[i] ); \
342 res_c = call_c( pixel_c.var[i], buf1, 16 ); \
343 res_asm = call_a( pixel_asm.var[i], buf1, 16 ); \
344 if( res_c != res_asm ) \
347 fprintf( stderr, "var[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
351 ok = 1; used_asm = 0;
352 TEST_PIXEL_VAR( PIXEL_16x16 );
353 TEST_PIXEL_VAR( PIXEL_8x8 );
354 report( "pixel var :" );
356 for( i=0, ok=1, used_asm=0; i<4; i++ )
357 if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
359 set_func_name( "hadamard_ac_%s", pixel_names[i] );
361 for( j=0; j<32; j++ )
363 uint8_t *pix = (j&16 ? buf1 : buf3) + (j&15)*256;
364 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
365 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
369 fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
373 call_c2( pixel_c.hadamard_ac[i], buf1, 16 );
374 call_a2( pixel_asm.hadamard_ac[i], buf1, 16 );
376 report( "pixel hadamard_ac :" );
378 #define TEST_INTRA_MBCMP( name, pred, satd, i8x8, ... ) \
379 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
381 int res_c[3], res_asm[3]; \
382 set_func_name( #name );\
384 memcpy( buf3, buf2, 1024 ); \
385 for( i=0; i<3; i++ ) \
387 pred[i]( buf3+48, ##__VA_ARGS__ ); \
388 res_c[i] = pixel_c.satd( buf1+48, 16, buf3+48, 32 ); \
390 call_a( pixel_asm.name, buf1+48, i8x8 ? edge : buf3+48, res_asm ); \
391 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
394 fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
395 res_c[0], res_c[1], res_c[2], \
396 res_asm[0], res_asm[1], res_asm[2] ); \
400 ok = 1; used_asm = 0;
401 TEST_INTRA_MBCMP( intra_satd_x3_16x16, predict_16x16, satd[PIXEL_16x16], 0 );
402 TEST_INTRA_MBCMP( intra_satd_x3_8x8c , predict_8x8c , satd[PIXEL_8x8] , 0 );
403 TEST_INTRA_MBCMP( intra_satd_x3_4x4 , predict_4x4 , satd[PIXEL_4x4] , 0 );
404 TEST_INTRA_MBCMP( intra_sa8d_x3_8x8 , predict_8x8 , sa8d[PIXEL_8x8] , 1, edge );
405 report( "intra satd_x3 :" );
406 TEST_INTRA_MBCMP( intra_sad_x3_16x16 , predict_16x16, sad [PIXEL_16x16], 0 );
407 report( "intra sad_x3 :" );
409 if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
410 pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
413 int sums[5][4] = {{0}};
416 res_c = x264_pixel_ssim_wxh( &pixel_c, buf1+2, 32, buf2+2, 32, 32, 28, buf3 );
417 res_a = x264_pixel_ssim_wxh( &pixel_asm, buf1+2, 32, buf2+2, 32, 32, 28, buf3 );
418 if( fabs(res_c - res_a) > 1e-6 )
421 fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
423 set_func_name( "ssim_core" );
424 call_c2( pixel_c.ssim_4x4x2_core, buf1+2, 32, buf2+2, 32, sums );
425 call_a2( pixel_asm.ssim_4x4x2_core, buf1+2, 32, buf2+2, 32, sums );
426 set_func_name( "ssim_end" );
427 call_c2( pixel_c.ssim_end4, sums, sums, 4 );
428 call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
432 ok = 1; used_asm = 0;
433 for( i=0; i<32; i++ )
435 for( i=0; i<100 && ok; i++ )
436 if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
438 DECLARE_ALIGNED_16( uint16_t sums[72] );
439 DECLARE_ALIGNED_16( int dc[4] );
440 int16_t mvs_a[32], mvs_c[32];
442 int thresh = rand() & 0x3fff;
443 set_func_name( "esa_ads" );
444 for( j=0; j<72; j++ )
445 sums[j] = rand() & 0x3fff;
447 dc[j] = rand() & 0x3fff;
449 mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
450 mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
451 if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
454 printf("c%d: ", i&3);
455 for(j=0; j<mvn_c; j++)
456 printf("%d ", mvs_c[j]);
457 printf("\na%d: ", i&3);
458 for(j=0; j<mvn_a; j++)
459 printf("%d ", mvs_a[j]);
463 report( "esa ads:" );
468 static int check_dct( int cpu_ref, int cpu_new )
470 x264_dct_function_t dct_c;
471 x264_dct_function_t dct_ref;
472 x264_dct_function_t dct_asm;
473 x264_quant_function_t qf;
474 int ret = 0, ok, used_asm, i, j, interlace;
475 DECLARE_ALIGNED_16( int16_t dct1[16][4][4] );
476 DECLARE_ALIGNED_16( int16_t dct2[16][4][4] );
477 DECLARE_ALIGNED_16( int16_t dct4[16][4][4] );
478 DECLARE_ALIGNED_16( int16_t dct8[4][8][8] );
482 x264_dct_init( 0, &dct_c );
483 x264_dct_init( cpu_ref, &dct_ref);
484 x264_dct_init( cpu_new, &dct_asm );
486 memset( h, 0, sizeof(*h) );
487 h->pps = h->pps_array;
488 x264_param_default( &h->param );
489 h->param.analyse.i_luma_deadzone[0] = 0;
490 h->param.analyse.i_luma_deadzone[1] = 0;
491 h->param.analyse.b_transform_8x8 = 1;
493 h->pps->scaling_list[i] = x264_cqm_flat16;
495 x264_quant_init( h, 0, &qf );
497 #define TEST_DCT( name, t1, t2, size ) \
498 if( dct_asm.name != dct_ref.name ) \
500 set_func_name( #name );\
502 call_c( dct_c.name, t1, buf1, buf2 ); \
503 call_a( dct_asm.name, t2, buf1, buf2 ); \
504 if( memcmp( t1, t2, size ) ) \
507 fprintf( stderr, #name " [FAILED]\n" ); \
510 ok = 1; used_asm = 0;
511 TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
512 TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
513 TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
514 report( "sub_dct4 :" );
516 ok = 1; used_asm = 0;
517 TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64*2 );
518 TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*2*4 );
519 report( "sub_dct8 :" );
522 // fdct and idct are denormalized by different factors, so quant/dequant
523 // is needed to force the coefs into the right range.
524 dct_c.sub16x16_dct( dct4, buf1, buf2 );
525 dct_c.sub16x16_dct8( dct8, buf1, buf2 );
526 for( i=0; i<16; i++ )
528 qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
529 qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
533 qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
534 qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
537 #define TEST_IDCT( name, src ) \
538 if( dct_asm.name != dct_ref.name ) \
540 set_func_name( #name );\
542 memcpy( buf3, buf1, 32*32 ); \
543 memcpy( buf4, buf1, 32*32 ); \
544 memcpy( dct1, src, 512 ); \
545 memcpy( dct2, src, 512 ); \
546 call_c1( dct_c.name, buf3, (void*)dct1 ); \
547 call_a1( dct_asm.name, buf4, (void*)dct2 ); \
548 if( memcmp( buf3, buf4, 32*32 ) ) \
551 fprintf( stderr, #name " [FAILED]\n" ); \
553 call_c2( dct_c.name, buf3, (void*)dct1 ); \
554 call_a2( dct_asm.name, buf4, (void*)dct2 ); \
556 ok = 1; used_asm = 0;
557 TEST_IDCT( add4x4_idct, dct4 );
558 TEST_IDCT( add8x8_idct, dct4 );
559 TEST_IDCT( add8x8_idct_dc, dct4 );
560 TEST_IDCT( add16x16_idct, dct4 );
561 TEST_IDCT( add16x16_idct_dc, dct4 );
562 report( "add_idct4 :" );
564 ok = 1; used_asm = 0;
565 TEST_IDCT( add8x8_idct8, dct8 );
566 TEST_IDCT( add16x16_idct8, dct8 );
567 report( "add_idct8 :" );
570 #define TEST_DCTDC( name )\
571 ok = 1; used_asm = 0;\
572 if( dct_asm.name != dct_ref.name )\
574 set_func_name( #name );\
576 uint16_t *p = (uint16_t*)buf1;\
577 for( i=0; i<16 && ok; i++ )\
579 for( j=0; j<16; j++ )\
580 dct1[0][0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? 4080 : -4080 /* max dc */\
581 : i<8 ? (*p++)&1 ? 4080 : -4080 /* max elements */\
582 : ((*p++)&0x1fff)-0x1000; /* general case */\
583 memcpy( dct2, dct1, 32 );\
584 call_c1( dct_c.name, dct1[0] );\
585 call_a1( dct_asm.name, dct2[0] );\
586 if( memcmp( dct1, dct2, 32 ) )\
589 call_c2( dct_c.name, dct1[0] );\
590 call_a2( dct_asm.name, dct2[0] );\
592 report( #name " :" );
594 TEST_DCTDC( dct4x4dc );
595 TEST_DCTDC( idct4x4dc );
598 x264_zigzag_function_t zigzag_c;
599 x264_zigzag_function_t zigzag_ref;
600 x264_zigzag_function_t zigzag_asm;
602 DECLARE_ALIGNED_16( int16_t level1[64] );
603 DECLARE_ALIGNED_16( int16_t level2[64] );
605 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
606 if( zigzag_asm.name != zigzag_ref.name ) \
608 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
610 memcpy(dct, buf1, size*sizeof(int16_t));\
611 call_c( zigzag_c.name, t1, dct ); \
612 call_a( zigzag_asm.name, t2, dct ); \
613 if( memcmp( t1, t2, size*sizeof(int16_t) ) ) \
616 fprintf( stderr, #name " [FAILED]\n" ); \
620 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
621 if( zigzag_asm.name != zigzag_ref.name ) \
623 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
625 memcpy( buf3, buf1, 16*FDEC_STRIDE ); \
626 memcpy( buf4, buf1, 16*FDEC_STRIDE ); \
627 call_c1( zigzag_c.name, t1, buf2, buf3 ); \
628 call_a1( zigzag_asm.name, t2, buf2, buf4 ); \
629 if( memcmp( t1, t2, size*sizeof(int16_t) )|| memcmp( buf3, buf4, 16*FDEC_STRIDE ) ) \
632 fprintf( stderr, #name " [FAILED]\n" ); \
634 call_c2( zigzag_c.name, t1, buf2, buf3 ); \
635 call_a2( zigzag_asm.name, t2, buf2, buf4 ); \
638 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
639 if( zigzag_asm.name != zigzag_ref.name ) \
641 for( j=0; j<100; j++ ) \
643 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
645 memcpy(dct, buf1, size*sizeof(int16_t));\
646 for( i=0; i<size; i++ ) \
647 dct[i] = rand()&0x1F ? 0 : dct[i]; \
648 memcpy(buf3, buf4, 10*sizeof(uint8_t)); \
649 call_c( zigzag_c.name, t1, dct, buf3 ); \
650 call_a( zigzag_asm.name, t2, dct, buf4 ); \
651 if( memcmp( t1, t2, size*sizeof(int16_t) ) || memcmp( buf3, buf4, 10*sizeof(uint8_t) ) ) \
659 x264_zigzag_init( 0, &zigzag_c, 0 );
660 x264_zigzag_init( cpu_ref, &zigzag_ref, 0 );
661 x264_zigzag_init( cpu_new, &zigzag_asm, 0 );
663 ok = 1; used_asm = 0;
664 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
665 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
666 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
667 report( "zigzag_frame :" );
670 x264_zigzag_init( 0, &zigzag_c, 1 );
671 x264_zigzag_init( cpu_ref, &zigzag_ref, 1 );
672 x264_zigzag_init( cpu_new, &zigzag_asm, 1 );
674 ok = 1; used_asm = 0;
675 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
676 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
677 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
678 report( "zigzag_field :" );
680 ok = 1; used_asm = 0;
681 TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct1[0][0], 64 );
682 report( "zigzag_interleave :" );
683 #undef TEST_ZIGZAG_SCAN
684 #undef TEST_ZIGZAG_SUB
689 static int check_mc( int cpu_ref, int cpu_new )
691 x264_mc_functions_t mc_c;
692 x264_mc_functions_t mc_ref;
693 x264_mc_functions_t mc_a;
694 x264_pixel_function_t pixel;
696 uint8_t *src = &buf1[2*32+2];
697 uint8_t *src2[4] = { &buf1[3*64+2], &buf1[5*64+2],
698 &buf1[7*64+2], &buf1[9*64+2] };
699 uint8_t *dst1 = buf3;
700 uint8_t *dst2 = buf4;
702 int dx, dy, i, j, k, w;
703 int ret = 0, ok, used_asm;
705 x264_mc_init( 0, &mc_c );
706 x264_mc_init( cpu_ref, &mc_ref );
707 x264_mc_init( cpu_new, &mc_a );
708 x264_pixel_init( 0, &pixel );
710 #define MC_TEST_LUMA( w, h ) \
711 if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
713 set_func_name( "mc_luma_%dx%d", w, h );\
715 memset(buf3, 0xCD, 1024); \
716 memset(buf4, 0xCD, 1024); \
717 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
718 call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h ); \
719 if( memcmp( buf3, buf4, 1024 ) ) \
721 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
725 if( mc_a.get_ref != mc_ref.get_ref ) \
727 uint8_t *ref = dst2; \
728 int ref_stride = 32; \
729 set_func_name( "get_ref_%dx%d", w, h );\
731 memset(buf3, 0xCD, 1024); \
732 memset(buf4, 0xCD, 1024); \
733 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
734 ref = (uint8_t*) call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h ); \
735 for( i=0; i<h; i++ ) \
736 if( memcmp( dst1+i*32, ref+i*ref_stride, w ) ) \
738 fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
744 #define MC_TEST_CHROMA( w, h ) \
745 if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
747 set_func_name( "mc_chroma_%dx%d", w, h );\
749 memset(buf3, 0xCD, 1024); \
750 memset(buf4, 0xCD, 1024); \
751 call_c( mc_c.mc_chroma, dst1, 16, src, 32, dx, dy, w, h ); \
752 call_a( mc_a.mc_chroma, dst2, 16, src, 32, dx, dy, w, h ); \
753 /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */\
754 for( j=0; j<h; j++ ) \
755 for( i=w; i<4; i++ ) \
756 dst2[i+j*16] = dst1[i+j*16]; \
757 if( memcmp( buf3, buf4, 1024 ) ) \
759 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
763 ok = 1; used_asm = 0;
764 for( dy = -8; dy < 8; dy++ )
765 for( dx = -128; dx < 128; dx++ )
767 if( rand()&15 ) continue; // running all of them is too slow
768 MC_TEST_LUMA( 20, 18 );
769 MC_TEST_LUMA( 16, 16 );
770 MC_TEST_LUMA( 16, 8 );
771 MC_TEST_LUMA( 12, 10 );
772 MC_TEST_LUMA( 8, 16 );
773 MC_TEST_LUMA( 8, 8 );
774 MC_TEST_LUMA( 8, 4 );
775 MC_TEST_LUMA( 4, 8 );
776 MC_TEST_LUMA( 4, 4 );
778 report( "mc luma :" );
780 ok = 1; used_asm = 0;
781 for( dy = -1; dy < 9; dy++ )
782 for( dx = -1; dx < 9; dx++ )
784 MC_TEST_CHROMA( 8, 8 );
785 MC_TEST_CHROMA( 8, 4 );
786 MC_TEST_CHROMA( 4, 8 );
787 MC_TEST_CHROMA( 4, 4 );
788 MC_TEST_CHROMA( 4, 2 );
789 MC_TEST_CHROMA( 2, 4 );
790 MC_TEST_CHROMA( 2, 2 );
792 report( "mc chroma :" );
794 #undef MC_TEST_CHROMA
796 #define MC_TEST_AVG( name, weight ) \
797 for( i = 0, ok = 1, used_asm = 0; i < 10; i++ ) \
799 memcpy( buf3, buf1+320, 320 ); \
800 memcpy( buf4, buf1+320, 320 ); \
801 if( mc_a.name[i] != mc_ref.name[i] ) \
803 set_func_name( "%s_%s", #name, pixel_names[i] );\
805 call_c1( mc_c.name[i], buf3, 16, buf2+1, 16, buf1+18, 16, weight ); \
806 call_a1( mc_a.name[i], buf4, 16, buf2+1, 16, buf1+18, 16, weight ); \
807 if( memcmp( buf3, buf4, 320 ) ) \
810 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
812 call_c2( mc_c.name[i], buf3, 16, buf2+1, 16, buf1+18, 16, weight ); \
813 call_a2( mc_a.name[i], buf4, 16, buf2+1, 16, buf1+18, 16, weight ); \
816 ok = 1; used_asm = 0;
817 for( w = -63; w <= 127 && ok; w++ )
818 MC_TEST_AVG( avg, w );
819 report( "mc wpredb :" );
821 if( mc_a.hpel_filter != mc_ref.hpel_filter )
823 uint8_t *src = buf1+8+2*64;
824 uint8_t *dstc[3] = { buf3+8, buf3+8+16*64, buf3+8+32*64 };
825 uint8_t *dsta[3] = { buf4+8, buf4+8+16*64, buf4+8+32*64 };
826 void *tmp = buf3+49*64;
827 set_func_name( "hpel_filter" );
828 ok = 1; used_asm = 1;
829 memset( buf3, 0, 4096 );
830 memset( buf4, 0, 4096 );
831 call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], src, 64, 48, 10, tmp );
832 call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], src, 64, 48, 10, tmp );
834 for( j=0; j<10; j++ )
835 //FIXME ideally the first pixels would match too, but they aren't actually used
836 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 ) )
839 fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
840 for( k=0; k<48; k++ )
841 printf("%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " ");
843 for( k=0; k<48; k++ )
844 printf("%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " ");
848 report( "hpel filter :" );
851 if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
853 uint8_t *dstc[4] = { buf3, buf3+1024, buf3+2048, buf3+3072 };
854 uint8_t *dsta[4] = { buf4, buf4+1024, buf4+2048, buf3+3072 };
855 set_func_name( "lowres_init" );
856 ok = 1; used_asm = 1;
857 for( w=40; w<=48; w+=8 )
859 int stride = (w+8)&~15;
860 call_c( mc_c.frame_init_lowres_core, buf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
861 call_a( mc_a.frame_init_lowres_core, buf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
865 if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w ) )
868 fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
870 printf( "%d ", dstc[j][k+i*stride] );
873 printf( "%d ", dsta[j][k+i*stride] );
879 report( "lowres init :" );
882 #define INTEGRAL_INIT( name, size, ... )\
883 if( mc_a.name != mc_ref.name )\
886 set_func_name( #name );\
888 memcpy( buf3, buf1, size*2*stride );\
889 memcpy( buf4, buf1, size*2*stride );\
890 uint16_t *sum = (uint16_t*)buf3;\
891 call_c1( mc_c.name, __VA_ARGS__ );\
892 sum = (uint16_t*)buf4;\
893 call_a1( mc_a.name, __VA_ARGS__ );\
894 if( memcmp( buf3, buf4, (stride-8)*2 )\
895 || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
897 call_c2( mc_c.name, __VA_ARGS__ );\
898 call_a2( mc_a.name, __VA_ARGS__ );\
900 ok = 1; used_asm = 0;
901 INTEGRAL_INIT( integral_init4h, 2, sum+stride, buf2, stride );
902 INTEGRAL_INIT( integral_init8h, 2, sum+stride, buf2, stride );
903 INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
904 INTEGRAL_INIT( integral_init8v, 9, sum, stride );
905 report( "integral init :" );
910 static int check_deblock( int cpu_ref, int cpu_new )
912 x264_deblock_function_t db_c;
913 x264_deblock_function_t db_ref;
914 x264_deblock_function_t db_a;
915 int ret = 0, ok = 1, used_asm = 0;
916 int alphas[36], betas[36];
920 x264_deblock_init( 0, &db_c );
921 x264_deblock_init( cpu_ref, &db_ref );
922 x264_deblock_init( cpu_new, &db_a );
924 /* not exactly the real values of a,b,tc but close enough */
926 for( i = 35; i >= 0; i-- )
930 tcs[i][0] = tcs[i][2] = (c+6)/10;
931 tcs[i][1] = tcs[i][3] = (c+9)/20;
936 #define TEST_DEBLOCK( name, align, ... ) \
937 for( i = 0; i < 36; i++ ) \
939 int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */\
940 for( j = 0; j < 1024; j++ ) \
941 /* two distributions of random to excersize different failure modes */\
942 buf3[j] = rand() & (i&1 ? 0xf : 0xff ); \
943 memcpy( buf4, buf3, 1024 ); \
944 if( db_a.name != db_ref.name ) \
946 set_func_name( #name );\
948 call_c1( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
949 call_a1( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
950 if( memcmp( buf3, buf4, 1024 ) ) \
953 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
956 call_c2( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
957 call_a2( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
961 TEST_DEBLOCK( deblock_h_luma, 0, tcs[i] );
962 TEST_DEBLOCK( deblock_v_luma, 1, tcs[i] );
963 TEST_DEBLOCK( deblock_h_chroma, 0, tcs[i] );
964 TEST_DEBLOCK( deblock_v_chroma, 1, tcs[i] );
965 TEST_DEBLOCK( deblock_h_luma_intra, 0 );
966 TEST_DEBLOCK( deblock_v_luma_intra, 1 );
967 TEST_DEBLOCK( deblock_h_chroma_intra, 0 );
968 TEST_DEBLOCK( deblock_v_chroma_intra, 1 );
970 report( "deblock :" );
975 static int check_quant( int cpu_ref, int cpu_new )
977 x264_quant_function_t qf_c;
978 x264_quant_function_t qf_ref;
979 x264_quant_function_t qf_a;
980 DECLARE_ALIGNED_16( int16_t dct1[64] );
981 DECLARE_ALIGNED_16( int16_t dct2[64] );
982 DECLARE_ALIGNED_16( uint8_t cqm_buf[64] );
983 int ret = 0, ok, used_asm;
984 int oks[2] = {1,1}, used_asms[2] = {0,0};
988 memset( h, 0, sizeof(*h) );
989 h->pps = h->pps_array;
990 x264_param_default( &h->param );
991 h->param.rc.i_qp_min = 26;
992 h->param.analyse.b_transform_8x8 = 1;
994 for( i_cqm = 0; i_cqm < 4; i_cqm++ )
998 for( i = 0; i < 6; i++ )
999 h->pps->scaling_list[i] = x264_cqm_flat16;
1000 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1002 else if( i_cqm == 1 )
1004 for( i = 0; i < 6; i++ )
1005 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1006 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1011 for( i = 0; i < 64; i++ )
1012 cqm_buf[i] = 10 + rand() % 246;
1014 for( i = 0; i < 64; i++ )
1016 for( i = 0; i < 6; i++ )
1017 h->pps->scaling_list[i] = cqm_buf;
1018 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1022 x264_quant_init( h, 0, &qf_c );
1023 x264_quant_init( h, cpu_ref, &qf_ref );
1024 x264_quant_init( h, cpu_new, &qf_a );
1026 #define INIT_QUANT8() \
1028 static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1030 for( y = 0; y < 8; y++ ) \
1031 for( x = 0; x < 8; x++ ) \
1033 unsigned int scale = (255*scale1d[y]*scale1d[x])/16; \
1034 dct1[y*8+x] = dct2[y*8+x] = j ? (rand()%(2*scale+1))-scale : 0; \
1038 #define INIT_QUANT4() \
1040 static const int scale1d[4] = {4,6,4,6}; \
1042 for( y = 0; y < 4; y++ ) \
1043 for( x = 0; x < 4; x++ ) \
1045 unsigned int scale = 255*scale1d[y]*scale1d[x]; \
1046 dct1[y*4+x] = dct2[y*4+x] = j ? (rand()%(2*scale+1))-scale : 0; \
1050 #define TEST_QUANT_DC( name, cqm ) \
1051 if( qf_a.name != qf_ref.name ) \
1053 set_func_name( #name ); \
1055 for( qp = 51; qp > 0; qp-- ) \
1057 for( j = 0; j < 2; j++ ) \
1059 int result_c, result_a; \
1060 for( i = 0; i < 16; i++ ) \
1061 dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1062 result_c = call_c1( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1063 result_a = call_a1( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1064 if( memcmp( dct1, dct2, 16*2 ) || result_c != result_a ) \
1067 fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1070 call_c2( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1071 call_a2( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1076 #define TEST_QUANT( qname, block, w ) \
1077 if( qf_a.qname != qf_ref.qname ) \
1079 set_func_name( #qname ); \
1081 for( qp = 51; qp > 0; qp-- ) \
1083 for( j = 0; j < 2; j++ ) \
1085 int result_c, result_a; \
1087 result_c = call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1088 result_a = call_a1( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1089 if( memcmp( dct1, dct2, w*w*2 ) || result_c != result_a ) \
1092 fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1095 call_c2( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1096 call_a2( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1101 TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1102 TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1103 TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1104 TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1105 TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1106 TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1108 #define TEST_DEQUANT( qname, dqname, block, w ) \
1109 if( qf_a.dqname != qf_ref.dqname ) \
1111 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1114 for( qp = 51; qp > 0; qp-- ) \
1117 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1118 memcpy( dct2, dct1, w*w*2 ); \
1119 call_c1( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1120 call_a1( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1121 if( memcmp( dct1, dct2, w*w*2 ) ) \
1124 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1127 call_c2( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1128 call_a2( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1132 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1133 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1134 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1135 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1137 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1138 if( qf_a.dqname != qf_ref.dqname ) \
1140 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1142 for( qp = 51; qp > 0; qp-- ) \
1144 for( i = 0; i < 16; i++ ) \
1146 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1147 memcpy( dct2, dct1, w*w*2 ); \
1148 call_c1( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1149 call_a1( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1150 if( memcmp( dct1, dct2, w*w*2 ) ) \
1153 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1155 call_c2( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1156 call_a2( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1160 TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1162 x264_cqm_delete( h );
1165 ok = oks[0]; used_asm = used_asms[0];
1166 report( "quant :" );
1168 ok = oks[1]; used_asm = used_asms[1];
1169 report( "dequant :" );
1171 ok = 1; used_asm = 0;
1172 if( qf_a.denoise_dct != qf_ref.denoise_dct )
1176 for( size = 16; size <= 64; size += 48 )
1178 set_func_name( "denoise_dct" );
1179 memcpy(dct1, buf1, size*2);
1180 memcpy(dct2, buf1, size*2);
1181 memcpy(buf3+256, buf3, 256);
1182 call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1183 call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1184 if( memcmp( dct1, dct2, size*2 ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1186 call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1187 call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1190 report( "denoise dct :" );
1192 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1193 if( qf_a.decname != qf_ref.decname ) \
1195 set_func_name( #decname ); \
1197 for( i = 0; i < 100; i++ ) \
1199 int result_c, result_a, idx; \
1200 for( idx = 0; idx < w*w; idx++ ) \
1201 dct1[idx] = !(rand()&3) + (!(rand()&15))*(rand()&3); \
1204 result_c = call_c( qf_c.decname, (void*)dct1 ); \
1205 result_a = call_a( qf_a.decname, (void*)dct1 ); \
1206 if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1209 fprintf( stderr, #decname ": [FAILED]\n" ); \
1215 ok = 1; used_asm = 0;
1216 TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1217 TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1218 TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1219 report( "decimate_score :" );
1221 #define TEST_LAST( last, lastname, w, ac ) \
1222 if( qf_a.last != qf_ref.last ) \
1224 set_func_name( #lastname ); \
1226 for( i = 0; i < 100; i++ ) \
1228 int result_c, result_a, idx, nnz=0; \
1229 int max = rand() & (w*w-1); \
1230 memset( dct1, 0, w*w*2 ); \
1231 for( idx = ac; idx < max; idx++ ) \
1232 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1235 result_c = call_c( qf_c.last, (void*)(dct1+ac) ); \
1236 result_a = call_a( qf_a.last, (void*)(dct1+ac) ); \
1237 if( result_c != result_a ) \
1240 fprintf( stderr, #lastname ": [FAILED]\n" ); \
1246 ok = 1; used_asm = 0;
1247 TEST_LAST( coeff_last[DCT_CHROMA_DC], coeff_last4, 2, 0 );
1248 TEST_LAST( coeff_last[ DCT_LUMA_AC], coeff_last15, 4, 1 );
1249 TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 4, 0 );
1250 TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 8, 0 );
1251 report( "coeff_last :" );
1253 #define TEST_LEVELRUN( lastname, name, w, ac ) \
1254 if( qf_a.lastname != qf_ref.lastname ) \
1256 set_func_name( #name ); \
1258 for( i = 0; i < 100; i++ ) \
1260 x264_run_level_t runlevel_c, runlevel_a; \
1261 int result_c, result_a, idx, nnz=0; \
1262 int max = rand() & (w*w-1); \
1263 memset( dct1, 0, w*w*2 ); \
1264 memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
1265 memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
1266 for( idx = ac; idx < max; idx++ ) \
1267 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1270 result_c = call_c( qf_c.lastname, (void*)(dct1+ac), &runlevel_c ); \
1271 result_a = call_a( qf_a.lastname, (void*)(dct1+ac), &runlevel_a ); \
1272 if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
1273 memcmp(runlevel_c.level, runlevel_a.level, sizeof(int16_t)*result_c) || \
1274 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
1277 fprintf( stderr, #name ": [FAILED]\n" ); \
1283 ok = 1; used_asm = 0;
1284 TEST_LEVELRUN( coeff_level_run[DCT_CHROMA_DC], coeff_level_run4, 2, 0 );
1285 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_AC], coeff_level_run15, 4, 1 );
1286 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 4, 0 );
1287 report( "coeff_level_run :" );
1292 static int check_intra( int cpu_ref, int cpu_new )
1294 int ret = 0, ok = 1, used_asm = 0;
1296 DECLARE_ALIGNED_16( uint8_t edge[33] );
1299 x264_predict_t predict_16x16[4+3];
1300 x264_predict_t predict_8x8c[4+3];
1301 x264_predict8x8_t predict_8x8[9+3];
1302 x264_predict_t predict_4x4[9+3];
1303 } ip_c, ip_ref, ip_a;
1305 x264_predict_16x16_init( 0, ip_c.predict_16x16 );
1306 x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
1307 x264_predict_8x8_init( 0, ip_c.predict_8x8 );
1308 x264_predict_4x4_init( 0, ip_c.predict_4x4 );
1310 x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
1311 x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
1312 x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8 );
1313 x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
1315 x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
1316 x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
1317 x264_predict_8x8_init( cpu_new, ip_a.predict_8x8 );
1318 x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
1320 x264_predict_8x8_filter( buf1+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
1322 #define INTRA_TEST( name, dir, w, ... ) \
1323 if( ip_a.name[dir] != ip_ref.name[dir] )\
1325 set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
1327 memcpy( buf3, buf1, 32*20 );\
1328 memcpy( buf4, buf1, 32*20 );\
1329 call_c( ip_c.name[dir], buf3+48, ##__VA_ARGS__ );\
1330 call_a( ip_a.name[dir], buf4+48, ##__VA_ARGS__ );\
1331 if( memcmp( buf3, buf4, 32*20 ) )\
1333 fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\
1336 for(k=-1; k<16; k++)\
1337 printf("%2x ", edge[16+k]);\
1339 for(j=0; j<w; j++){\
1340 printf("%2x ", edge[14-j]);\
1342 printf("%2x ", buf4[48+k+j*32]);\
1346 for(j=0; j<w; j++){\
1349 printf("%2x ", buf3[48+k+j*32]);\
1355 for( i = 0; i < 12; i++ )
1356 INTRA_TEST( predict_4x4, i, 4 );
1357 for( i = 0; i < 7; i++ )
1358 INTRA_TEST( predict_8x8c, i, 8 );
1359 for( i = 0; i < 7; i++ )
1360 INTRA_TEST( predict_16x16, i, 16 );
1361 for( i = 0; i < 12; i++ )
1362 INTRA_TEST( predict_8x8, i, 8, edge );
1364 report( "intra pred :" );
1368 #define DECL_CABAC(cpu) \
1369 static void run_cabac_##cpu( uint8_t *dst )\
1373 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1374 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1375 for( i=0; i<0x1000; i++ )\
1376 x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
1382 #define run_cabac_asm run_cabac_c
1385 static int check_cabac( int cpu_ref, int cpu_new )
1387 int ret = 0, ok, used_asm = 1;
1388 if( cpu_ref || run_cabac_c == run_cabac_asm)
1390 set_func_name( "cabac_encode_decision" );
1391 memcpy( buf4, buf3, 0x1000 );
1392 call_c( run_cabac_c, buf3 );
1393 call_a( run_cabac_asm, buf4 );
1394 ok = !memcmp( buf3, buf4, 0x1000 );
1395 report( "cabac :" );
1399 static int check_all_funcs( int cpu_ref, int cpu_new )
1401 return check_pixel( cpu_ref, cpu_new )
1402 + check_dct( cpu_ref, cpu_new )
1403 + check_mc( cpu_ref, cpu_new )
1404 + check_intra( cpu_ref, cpu_new )
1405 + check_deblock( cpu_ref, cpu_new )
1406 + check_quant( cpu_ref, cpu_new )
1407 + check_cabac( cpu_ref, cpu_new );
1410 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
1412 *cpu_ref = *cpu_new;
1414 if( *cpu_new & X264_CPU_SSE2_IS_FAST )
1415 *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
1417 fprintf( stderr, "x264: %s\n", name );
1418 return check_all_funcs( *cpu_ref, *cpu_new );
1421 static int check_all_flags( void )
1424 int cpu0 = 0, cpu1 = 0;
1426 if( x264_cpu_detect() & X264_CPU_MMXEXT )
1428 ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMXEXT, "MMX" );
1429 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
1430 cpu1 &= ~X264_CPU_CACHELINE_64;
1432 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
1433 cpu1 &= ~X264_CPU_CACHELINE_32;
1435 if( x264_cpu_detect() & X264_CPU_LZCNT )
1437 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
1438 cpu1 &= ~X264_CPU_LZCNT;
1441 if( x264_cpu_detect() & X264_CPU_SSE2 )
1443 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
1444 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
1445 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
1447 if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
1449 cpu1 &= ~X264_CPU_CACHELINE_64;
1450 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
1451 cpu1 &= ~X264_CPU_SSE_MISALIGN;
1453 if( x264_cpu_detect() & X264_CPU_LZCNT )
1455 cpu1 &= ~X264_CPU_CACHELINE_64;
1456 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
1457 cpu1 &= ~X264_CPU_LZCNT;
1459 if( x264_cpu_detect() & X264_CPU_SSE3 )
1460 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
1461 if( x264_cpu_detect() & X264_CPU_SSSE3 )
1463 cpu1 &= ~X264_CPU_CACHELINE_64;
1464 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
1465 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
1466 ret |= add_flags( &cpu0, &cpu1, X264_CPU_PHADD_IS_FAST, "PHADD" );
1468 if( x264_cpu_detect() & X264_CPU_SSE4 )
1470 cpu1 &= ~X264_CPU_CACHELINE_64;
1471 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
1474 if( x264_cpu_detect() & X264_CPU_ALTIVEC )
1476 fprintf( stderr, "x264: ALTIVEC against C\n" );
1477 ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
1483 int main(int argc, char *argv[])
1488 if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
1490 #if !defined(ARCH_X86) && !defined(ARCH_X86_64) && !defined(ARCH_PPC)
1491 fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
1495 if( argv[1][7] == '=' )
1497 bench_pattern = argv[1]+8;
1498 bench_pattern_len = strlen(bench_pattern);
1504 i = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
1505 fprintf( stderr, "x264: using random seed %u\n", i );
1508 buf1 = x264_malloc( 0x3e00 + 16*BENCH_ALIGNS );
1509 buf2 = buf1 + 0xf00;
1510 buf3 = buf2 + 0xf00;
1511 buf4 = buf3 + 0x1000;
1512 for( i=0; i<0x1e00; i++ )
1513 buf1[i] = rand() & 0xFF;
1514 memset( buf1+0x1e00, 0, 0x2000 );
1516 /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
1518 for( i=0; i<BENCH_ALIGNS && !ret; i++ )
1520 buf2 = buf1 + 0xf00;
1521 buf3 = buf2 + 0xf00;
1522 buf4 = buf3 + 0x1000;
1523 ret |= x264_stack_pagealign( check_all_flags, i*16 );
1526 fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
1529 ret = check_all_flags();
1533 fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
1536 fprintf( stderr, "x264: All tests passed Yeah :)\n" );