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_SHUFFLE_IS_FAST ? "fastshuffle" :
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__)
178 #if defined(ARCH_X86) || defined(_WIN64)
179 /* detect when callee-saved regs aren't saved.
180 * needs an explicit asm check because it only sometimes crashes in normal use. */
181 intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... );
182 #define call_a1(func,...) x264_checkasm_call((intptr_t(*)())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 x264_predict_8x8_filter_t predict_8x8_filter;
232 DECLARE_ALIGNED_16( uint8_t edge[33] );
233 uint16_t cost_mv[32];
234 int ret = 0, ok, used_asm;
237 x264_pixel_init( 0, &pixel_c );
238 x264_pixel_init( cpu_ref, &pixel_ref );
239 x264_pixel_init( cpu_new, &pixel_asm );
240 x264_predict_16x16_init( 0, predict_16x16 );
241 x264_predict_8x8c_init( 0, predict_8x8c );
242 x264_predict_8x8_init( 0, predict_8x8, &predict_8x8_filter );
243 x264_predict_4x4_init( 0, predict_4x4 );
244 predict_8x8_filter( buf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
247 for( i=0; i<256; i++ )
252 buf3[i] = ~(buf4[i] = -(z&1));
254 // random pattern made of maxed pixel differences, in case an intermediate value overflows
255 for( ; i<0x1000; i++ )
256 buf3[i] = ~(buf4[i] = -(buf1[i&~0x88]&1));
258 #define TEST_PIXEL( name, align ) \
259 for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
261 int res_c, res_asm; \
262 if( pixel_asm.name[i] != pixel_ref.name[i] ) \
264 set_func_name( "%s_%s", #name, pixel_names[i] ); \
266 for( j=0; j<64; j++ ) \
268 res_c = call_c( pixel_c.name[i], buf1, 16, buf2+j*!align, 64 ); \
269 res_asm = call_a( pixel_asm.name[i], buf1, 16, buf2+j*!align, 64 ); \
270 if( res_c != res_asm ) \
273 fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
277 for( j=0; j<0x1000 && ok; j+=256 ) \
279 res_c = pixel_c .name[i]( buf3+j, 16, buf4+j, 16 ); \
280 res_asm = pixel_asm.name[i]( buf3+j, 16, buf4+j, 16 ); \
281 if( res_c != res_asm ) \
284 fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
289 report( "pixel " #name " :" );
291 TEST_PIXEL( sad, 0 );
292 TEST_PIXEL( sad_aligned, 1 );
293 TEST_PIXEL( ssd, 1 );
294 TEST_PIXEL( satd, 0 );
295 TEST_PIXEL( sa8d, 1 );
297 #define TEST_PIXEL_X( N ) \
298 for( i = 0, ok = 1, used_asm = 0; i < 7; i++ ) \
300 int res_c[4]={0}, res_asm[4]={0}; \
301 if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
303 set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
305 for( j=0; j<64; j++) \
307 uint8_t *pix2 = buf2+j; \
308 res_c[0] = pixel_c.sad[i]( buf1, 16, pix2, 64 ); \
309 res_c[1] = pixel_c.sad[i]( buf1, 16, pix2+6, 64 ); \
310 res_c[2] = pixel_c.sad[i]( buf1, 16, pix2+1, 64 ); \
313 res_c[3] = pixel_c.sad[i]( buf1, 16, pix2+10, 64 ); \
314 call_a( pixel_asm.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
317 call_a( pixel_asm.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
318 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
321 fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
322 i, res_c[0], res_c[1], res_c[2], res_c[3], \
323 res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
326 call_c2( pixel_c.sad_x4[i], buf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
328 call_c2( pixel_c.sad_x3[i], buf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
332 report( "pixel sad_x"#N" :" );
337 #define TEST_PIXEL_VAR( i ) \
338 if( pixel_asm.var[i] != pixel_ref.var[i] ) \
340 int res_c, res_asm; \
341 set_func_name( "%s_%s", "var", pixel_names[i] ); \
343 res_c = call_c( pixel_c.var[i], buf1, 16 ); \
344 res_asm = call_a( pixel_asm.var[i], buf1, 16 ); \
345 if( res_c != res_asm ) \
348 fprintf( stderr, "var[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
352 ok = 1; used_asm = 0;
353 TEST_PIXEL_VAR( PIXEL_16x16 );
354 TEST_PIXEL_VAR( PIXEL_8x8 );
355 report( "pixel var :" );
357 ok = 1; used_asm = 0;
358 if( pixel_asm.var2_8x8 != pixel_ref.var2_8x8 )
360 int res_c, res_asm, ssd_c, ssd_asm;
361 set_func_name( "var2_8x8" );
363 res_c = call_c( pixel_c.var2_8x8, buf1, 16, buf2, 16, &ssd_c );
364 res_asm = call_a( pixel_asm.var2_8x8, buf1, 16, buf2, 16, &ssd_asm );
365 if( res_c != res_asm || ssd_c != ssd_asm )
368 fprintf( stderr, "var[%d]: %d != %d or %d != %d [FAILED]\n", i, res_c, res_asm, ssd_c, ssd_asm );
372 report( "pixel var2 :" );
374 for( i=0, ok=1, used_asm=0; i<4; i++ )
375 if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
377 set_func_name( "hadamard_ac_%s", pixel_names[i] );
379 for( j=0; j<32; j++ )
381 uint8_t *pix = (j&16 ? buf1 : buf3) + (j&15)*256;
382 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
383 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
387 fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
391 call_c2( pixel_c.hadamard_ac[i], buf1, 16 );
392 call_a2( pixel_asm.hadamard_ac[i], buf1, 16 );
394 report( "pixel hadamard_ac :" );
396 #define TEST_INTRA_MBCMP( name, pred, satd, i8x8, ... ) \
397 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
399 int res_c[3], res_asm[3]; \
400 set_func_name( #name );\
402 memcpy( buf3, buf2, 1024 ); \
403 for( i=0; i<3; i++ ) \
405 pred[i]( buf3+48, ##__VA_ARGS__ ); \
406 res_c[i] = pixel_c.satd( buf1+48, 16, buf3+48, 32 ); \
408 call_a( pixel_asm.name, buf1+48, i8x8 ? edge : buf3+48, res_asm ); \
409 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
412 fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
413 res_c[0], res_c[1], res_c[2], \
414 res_asm[0], res_asm[1], res_asm[2] ); \
418 ok = 1; used_asm = 0;
419 TEST_INTRA_MBCMP( intra_satd_x3_16x16, predict_16x16, satd[PIXEL_16x16], 0 );
420 TEST_INTRA_MBCMP( intra_satd_x3_8x8c , predict_8x8c , satd[PIXEL_8x8] , 0 );
421 TEST_INTRA_MBCMP( intra_satd_x3_4x4 , predict_4x4 , satd[PIXEL_4x4] , 0 );
422 TEST_INTRA_MBCMP( intra_sa8d_x3_8x8 , predict_8x8 , sa8d[PIXEL_8x8] , 1, edge );
423 report( "intra satd_x3 :" );
424 TEST_INTRA_MBCMP( intra_sad_x3_16x16 , predict_16x16, sad [PIXEL_16x16], 0 );
425 TEST_INTRA_MBCMP( intra_sad_x3_8x8c , predict_8x8c , sad [PIXEL_8x8] , 0 );
426 TEST_INTRA_MBCMP( intra_sad_x3_8x8 , predict_8x8 , sad [PIXEL_8x8] , 1, edge );
427 TEST_INTRA_MBCMP( intra_sad_x3_4x4 , predict_4x4 , sad [PIXEL_4x4] , 0 );
428 report( "intra sad_x3 :" );
430 if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
431 pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
434 DECLARE_ALIGNED_16( int sums[5][4] ) = {{0}};
437 res_c = x264_pixel_ssim_wxh( &pixel_c, buf1+2, 32, buf2+2, 32, 32, 28, buf3 );
438 res_a = x264_pixel_ssim_wxh( &pixel_asm, buf1+2, 32, buf2+2, 32, 32, 28, buf3 );
439 if( fabs(res_c - res_a) > 1e-6 )
442 fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
444 set_func_name( "ssim_core" );
445 call_c2( pixel_c.ssim_4x4x2_core, buf1+2, 32, buf2+2, 32, sums );
446 call_a2( pixel_asm.ssim_4x4x2_core, buf1+2, 32, buf2+2, 32, sums );
447 set_func_name( "ssim_end" );
448 call_c2( pixel_c.ssim_end4, sums, sums, 4 );
449 call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
453 ok = 1; used_asm = 0;
454 for( i=0; i<32; i++ )
456 for( i=0; i<100 && ok; i++ )
457 if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
459 DECLARE_ALIGNED_16( uint16_t sums[72] );
460 DECLARE_ALIGNED_16( int dc[4] );
461 int16_t mvs_a[32], mvs_c[32];
463 int thresh = rand() & 0x3fff;
464 set_func_name( "esa_ads" );
465 for( j=0; j<72; j++ )
466 sums[j] = rand() & 0x3fff;
468 dc[j] = rand() & 0x3fff;
470 mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
471 mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
472 if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
475 printf("c%d: ", i&3);
476 for(j=0; j<mvn_c; j++)
477 printf("%d ", mvs_c[j]);
478 printf("\na%d: ", i&3);
479 for(j=0; j<mvn_a; j++)
480 printf("%d ", mvs_a[j]);
484 report( "esa ads:" );
489 static int check_dct( int cpu_ref, int cpu_new )
491 x264_dct_function_t dct_c;
492 x264_dct_function_t dct_ref;
493 x264_dct_function_t dct_asm;
494 x264_quant_function_t qf;
495 int ret = 0, ok, used_asm, i, j, interlace;
496 DECLARE_ALIGNED_16( int16_t dct1[16][4][4] );
497 DECLARE_ALIGNED_16( int16_t dct2[16][4][4] );
498 DECLARE_ALIGNED_16( int16_t dct4[16][4][4] );
499 DECLARE_ALIGNED_16( int16_t dct8[4][8][8] );
500 DECLARE_ALIGNED_8( int16_t dctdc[2][2][2] );
504 x264_dct_init( 0, &dct_c );
505 x264_dct_init( cpu_ref, &dct_ref);
506 x264_dct_init( cpu_new, &dct_asm );
508 memset( h, 0, sizeof(*h) );
509 h->pps = h->pps_array;
510 x264_param_default( &h->param );
511 h->param.analyse.i_luma_deadzone[0] = 0;
512 h->param.analyse.i_luma_deadzone[1] = 0;
513 h->param.analyse.b_transform_8x8 = 1;
515 h->pps->scaling_list[i] = x264_cqm_flat16;
517 x264_quant_init( h, 0, &qf );
519 #define TEST_DCT( name, t1, t2, size ) \
520 if( dct_asm.name != dct_ref.name ) \
522 set_func_name( #name );\
524 call_c( dct_c.name, t1, buf1, buf2 ); \
525 call_a( dct_asm.name, t2, buf1, buf2 ); \
526 if( memcmp( t1, t2, size ) ) \
529 fprintf( stderr, #name " [FAILED]\n" ); \
532 ok = 1; used_asm = 0;
533 TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
534 TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
535 TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4*2 );
536 TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
537 report( "sub_dct4 :" );
539 ok = 1; used_asm = 0;
540 TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64*2 );
541 TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*2*4 );
542 report( "sub_dct8 :" );
545 // fdct and idct are denormalized by different factors, so quant/dequant
546 // is needed to force the coefs into the right range.
547 dct_c.sub16x16_dct( dct4, buf1, buf2 );
548 dct_c.sub16x16_dct8( dct8, buf1, buf2 );
549 for( i=0; i<16; i++ )
551 qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
552 qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
556 qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
557 qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
560 #define TEST_IDCT( name, src ) \
561 if( dct_asm.name != dct_ref.name ) \
563 set_func_name( #name );\
565 memcpy( buf3, buf1, 32*32 ); \
566 memcpy( buf4, buf1, 32*32 ); \
567 memcpy( dct1, src, 512 ); \
568 memcpy( dct2, src, 512 ); \
569 call_c1( dct_c.name, buf3, (void*)dct1 ); \
570 call_a1( dct_asm.name, buf4, (void*)dct2 ); \
571 if( memcmp( buf3, buf4, 32*32 ) ) \
574 fprintf( stderr, #name " [FAILED]\n" ); \
576 call_c2( dct_c.name, buf3, (void*)dct1 ); \
577 call_a2( dct_asm.name, buf4, (void*)dct2 ); \
579 ok = 1; used_asm = 0;
580 TEST_IDCT( add4x4_idct, dct4 );
581 TEST_IDCT( add8x8_idct, dct4 );
582 TEST_IDCT( add8x8_idct_dc, dct4 );
583 TEST_IDCT( add16x16_idct, dct4 );
584 TEST_IDCT( add16x16_idct_dc, dct4 );
585 report( "add_idct4 :" );
587 ok = 1; used_asm = 0;
588 TEST_IDCT( add8x8_idct8, dct8 );
589 TEST_IDCT( add16x16_idct8, dct8 );
590 report( "add_idct8 :" );
593 #define TEST_DCTDC( name )\
594 ok = 1; used_asm = 0;\
595 if( dct_asm.name != dct_ref.name )\
597 set_func_name( #name );\
599 uint16_t *p = (uint16_t*)buf1;\
600 for( i=0; i<16 && ok; i++ )\
602 for( j=0; j<16; j++ )\
603 dct1[0][0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? 4080 : -4080 /* max dc */\
604 : i<8 ? (*p++)&1 ? 4080 : -4080 /* max elements */\
605 : ((*p++)&0x1fff)-0x1000; /* general case */\
606 memcpy( dct2, dct1, 32 );\
607 call_c1( dct_c.name, dct1[0] );\
608 call_a1( dct_asm.name, dct2[0] );\
609 if( memcmp( dct1, dct2, 32 ) )\
612 call_c2( dct_c.name, dct1[0] );\
613 call_a2( dct_asm.name, dct2[0] );\
615 report( #name " :" );
617 TEST_DCTDC( dct4x4dc );
618 TEST_DCTDC( idct4x4dc );
621 x264_zigzag_function_t zigzag_c;
622 x264_zigzag_function_t zigzag_ref;
623 x264_zigzag_function_t zigzag_asm;
625 DECLARE_ALIGNED_16( int16_t level1[64] );
626 DECLARE_ALIGNED_16( int16_t level2[64] );
628 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
629 if( zigzag_asm.name != zigzag_ref.name ) \
631 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
633 memcpy(dct, buf1, size*sizeof(int16_t));\
634 call_c( zigzag_c.name, t1, dct ); \
635 call_a( zigzag_asm.name, t2, dct ); \
636 if( memcmp( t1, t2, size*sizeof(int16_t) ) ) \
639 fprintf( stderr, #name " [FAILED]\n" ); \
643 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
644 if( zigzag_asm.name != zigzag_ref.name ) \
647 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
649 memcpy( buf3, buf1, 16*FDEC_STRIDE ); \
650 memcpy( buf4, buf1, 16*FDEC_STRIDE ); \
651 nz_c = call_c1( zigzag_c.name, t1, buf2, buf3 ); \
652 nz_a = call_a1( zigzag_asm.name, t2, buf2, buf4 ); \
653 if( memcmp( t1, t2, size*sizeof(int16_t) )|| memcmp( buf3, buf4, 16*FDEC_STRIDE ) || nz_c != nz_a ) \
656 fprintf( stderr, #name " [FAILED]\n" ); \
658 call_c2( zigzag_c.name, t1, buf2, buf3 ); \
659 call_a2( zigzag_asm.name, t2, buf2, buf4 ); \
662 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
663 if( zigzag_asm.name != zigzag_ref.name ) \
666 int16_t dc_a, dc_c; \
667 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
669 for( i = 0; i < 2; i++ ) \
671 memcpy( buf3, buf2, 16*FDEC_STRIDE ); \
672 memcpy( buf4, buf2, 16*FDEC_STRIDE ); \
673 for( j = 0; j < 4; j++ ) \
675 memcpy( buf3 + j*FDEC_STRIDE, (i?buf1:buf2) + j*FENC_STRIDE, 4 ); \
676 memcpy( buf4 + j*FDEC_STRIDE, (i?buf1:buf2) + j*FENC_STRIDE, 4 ); \
678 nz_c = call_c1( zigzag_c.name, t1, buf2, buf3, &dc_c ); \
679 nz_a = call_a1( zigzag_asm.name, t2, buf2, buf4, &dc_a ); \
680 if( memcmp( t1+1, t2+1, 15*sizeof(int16_t) ) || memcmp( buf3, buf4, 16*FDEC_STRIDE ) || nz_c != nz_a || dc_c != dc_a ) \
683 fprintf( stderr, #name " [FAILED]\n" ); \
687 call_c2( zigzag_c.name, t1, buf2, buf3, &dc_c ); \
688 call_a2( zigzag_asm.name, t2, buf2, buf4, &dc_a ); \
691 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
692 if( zigzag_asm.name != zigzag_ref.name ) \
694 for( j=0; j<100; j++ ) \
696 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" );\
698 memcpy(dct, buf1, size*sizeof(int16_t));\
699 for( i=0; i<size; i++ ) \
700 dct[i] = rand()&0x1F ? 0 : dct[i]; \
701 memcpy(buf3, buf4, 10*sizeof(uint8_t)); \
702 call_c( zigzag_c.name, t1, dct, buf3 ); \
703 call_a( zigzag_asm.name, t2, dct, buf4 ); \
704 if( memcmp( t1, t2, size*sizeof(int16_t) ) || memcmp( buf3, buf4, 10*sizeof(uint8_t) ) ) \
712 x264_zigzag_init( 0, &zigzag_c, 0 );
713 x264_zigzag_init( cpu_ref, &zigzag_ref, 0 );
714 x264_zigzag_init( cpu_new, &zigzag_asm, 0 );
716 ok = 1; used_asm = 0;
717 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
718 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
719 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
720 TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
721 report( "zigzag_frame :" );
724 x264_zigzag_init( 0, &zigzag_c, 1 );
725 x264_zigzag_init( cpu_ref, &zigzag_ref, 1 );
726 x264_zigzag_init( cpu_new, &zigzag_asm, 1 );
728 ok = 1; used_asm = 0;
729 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
730 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
731 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
732 TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
733 report( "zigzag_field :" );
735 ok = 1; used_asm = 0;
736 TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct1[0][0], 64 );
737 report( "zigzag_interleave :" );
738 #undef TEST_ZIGZAG_SCAN
739 #undef TEST_ZIGZAG_SUB
744 static int check_mc( int cpu_ref, int cpu_new )
746 x264_mc_functions_t mc_c;
747 x264_mc_functions_t mc_ref;
748 x264_mc_functions_t mc_a;
749 x264_pixel_function_t pixel;
751 uint8_t *src = &buf1[2*32+2];
752 uint8_t *src2[4] = { &buf1[3*64+2], &buf1[5*64+2],
753 &buf1[7*64+2], &buf1[9*64+2] };
754 uint8_t *dst1 = buf3;
755 uint8_t *dst2 = buf4;
757 int dx, dy, i, j, k, w;
758 int ret = 0, ok, used_asm;
760 x264_mc_init( 0, &mc_c );
761 x264_mc_init( cpu_ref, &mc_ref );
762 x264_mc_init( cpu_new, &mc_a );
763 x264_pixel_init( 0, &pixel );
765 #define MC_TEST_LUMA( w, h ) \
766 if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
768 set_func_name( "mc_luma_%dx%d", w, h );\
770 memset(buf3, 0xCD, 1024); \
771 memset(buf4, 0xCD, 1024); \
772 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
773 call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h ); \
774 if( memcmp( buf3, buf4, 1024 ) ) \
776 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
780 if( mc_a.get_ref != mc_ref.get_ref ) \
782 uint8_t *ref = dst2; \
783 int ref_stride = 32; \
784 set_func_name( "get_ref_%dx%d", w, h );\
786 memset(buf3, 0xCD, 1024); \
787 memset(buf4, 0xCD, 1024); \
788 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h ); \
789 ref = (uint8_t*) call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h ); \
790 for( i=0; i<h; i++ ) \
791 if( memcmp( dst1+i*32, ref+i*ref_stride, w ) ) \
793 fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
799 #define MC_TEST_CHROMA( w, h ) \
800 if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
802 set_func_name( "mc_chroma_%dx%d", w, h );\
804 memset(buf3, 0xCD, 1024); \
805 memset(buf4, 0xCD, 1024); \
806 call_c( mc_c.mc_chroma, dst1, 16, src, 64, dx, dy, w, h ); \
807 call_a( mc_a.mc_chroma, dst2, 16, src, 64, dx, dy, w, h ); \
808 /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */\
809 for( j=0; j<h; j++ ) \
810 for( i=w; i<4; i++ ) \
811 dst2[i+j*16] = dst1[i+j*16]; \
812 if( memcmp( buf3, buf4, 1024 ) ) \
814 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
818 ok = 1; used_asm = 0;
819 for( dy = -8; dy < 8; dy++ )
820 for( dx = -128; dx < 128; dx++ )
822 if( rand()&15 ) continue; // running all of them is too slow
823 MC_TEST_LUMA( 20, 18 );
824 MC_TEST_LUMA( 16, 16 );
825 MC_TEST_LUMA( 16, 8 );
826 MC_TEST_LUMA( 12, 10 );
827 MC_TEST_LUMA( 8, 16 );
828 MC_TEST_LUMA( 8, 8 );
829 MC_TEST_LUMA( 8, 4 );
830 MC_TEST_LUMA( 4, 8 );
831 MC_TEST_LUMA( 4, 4 );
833 report( "mc luma :" );
835 ok = 1; used_asm = 0;
836 for( dy = -1; dy < 9; dy++ )
837 for( dx = -128; dx < 128; dx++ )
839 if( rand()&15 ) continue;
840 MC_TEST_CHROMA( 8, 8 );
841 MC_TEST_CHROMA( 8, 4 );
842 MC_TEST_CHROMA( 4, 8 );
843 MC_TEST_CHROMA( 4, 4 );
844 MC_TEST_CHROMA( 4, 2 );
845 MC_TEST_CHROMA( 2, 4 );
846 MC_TEST_CHROMA( 2, 2 );
848 report( "mc chroma :" );
850 #undef MC_TEST_CHROMA
852 #define MC_TEST_AVG( name, weight ) \
853 for( i = 0, ok = 1, used_asm = 0; i < 10; i++ ) \
855 memcpy( buf3, buf1+320, 320 ); \
856 memcpy( buf4, buf1+320, 320 ); \
857 if( mc_a.name[i] != mc_ref.name[i] ) \
859 set_func_name( "%s_%s", #name, pixel_names[i] );\
861 call_c1( mc_c.name[i], buf3, 16, buf2+1, 16, buf1+18, 16, weight ); \
862 call_a1( mc_a.name[i], buf4, 16, buf2+1, 16, buf1+18, 16, weight ); \
863 if( memcmp( buf3, buf4, 320 ) ) \
866 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
868 call_c2( mc_c.name[i], buf3, 16, buf2+1, 16, buf1+18, 16, weight ); \
869 call_a2( mc_a.name[i], buf4, 16, buf2+1, 16, buf1+18, 16, weight ); \
872 ok = 1; used_asm = 0;
873 for( w = -63; w <= 127 && ok; w++ )
874 MC_TEST_AVG( avg, w );
875 report( "mc wpredb :" );
877 if( mc_a.hpel_filter != mc_ref.hpel_filter )
879 uint8_t *src = buf1+8+2*64;
880 uint8_t *dstc[3] = { buf3+8, buf3+8+16*64, buf3+8+32*64 };
881 uint8_t *dsta[3] = { buf4+8, buf4+8+16*64, buf4+8+32*64 };
882 void *tmp = buf3+49*64;
883 set_func_name( "hpel_filter" );
884 ok = 1; used_asm = 1;
885 memset( buf3, 0, 4096 );
886 memset( buf4, 0, 4096 );
887 call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], src, 64, 48, 10, tmp );
888 call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], src, 64, 48, 10, tmp );
890 for( j=0; j<10; j++ )
891 //FIXME ideally the first pixels would match too, but they aren't actually used
892 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 ) )
895 fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
896 for( k=0; k<48; k++ )
897 printf("%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " ");
899 for( k=0; k<48; k++ )
900 printf("%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " ");
904 report( "hpel filter :" );
907 if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
909 uint8_t *dstc[4] = { buf3, buf3+1024, buf3+2048, buf3+3072 };
910 uint8_t *dsta[4] = { buf4, buf4+1024, buf4+2048, buf4+3072 };
911 set_func_name( "lowres_init" );
912 ok = 1; used_asm = 1;
913 for( w=40; w<=48; w+=8 )
915 int stride = (w+8)&~15;
916 call_c( mc_c.frame_init_lowres_core, buf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
917 call_a( mc_a.frame_init_lowres_core, buf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
921 if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w ) )
924 fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
926 printf( "%d ", dstc[j][k+i*stride] );
929 printf( "%d ", dsta[j][k+i*stride] );
935 report( "lowres init :" );
938 #define INTEGRAL_INIT( name, size, ... )\
939 if( mc_a.name != mc_ref.name )\
942 set_func_name( #name );\
944 memcpy( buf3, buf1, size*2*stride );\
945 memcpy( buf4, buf1, size*2*stride );\
946 uint16_t *sum = (uint16_t*)buf3;\
947 call_c1( mc_c.name, __VA_ARGS__ );\
948 sum = (uint16_t*)buf4;\
949 call_a1( mc_a.name, __VA_ARGS__ );\
950 if( memcmp( buf3, buf4, (stride-8)*2 )\
951 || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
953 call_c2( mc_c.name, __VA_ARGS__ );\
954 call_a2( mc_a.name, __VA_ARGS__ );\
956 ok = 1; used_asm = 0;
957 INTEGRAL_INIT( integral_init4h, 2, sum+stride, buf2, stride );
958 INTEGRAL_INIT( integral_init8h, 2, sum+stride, buf2, stride );
959 INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
960 INTEGRAL_INIT( integral_init8v, 9, sum, stride );
961 report( "integral init :" );
966 static int check_deblock( int cpu_ref, int cpu_new )
968 x264_deblock_function_t db_c;
969 x264_deblock_function_t db_ref;
970 x264_deblock_function_t db_a;
971 int ret = 0, ok = 1, used_asm = 0;
972 int alphas[36], betas[36];
976 x264_deblock_init( 0, &db_c );
977 x264_deblock_init( cpu_ref, &db_ref );
978 x264_deblock_init( cpu_new, &db_a );
980 /* not exactly the real values of a,b,tc but close enough */
982 for( i = 35; i >= 0; i-- )
986 tcs[i][0] = tcs[i][2] = (c+6)/10;
987 tcs[i][1] = tcs[i][3] = (c+9)/20;
992 #define TEST_DEBLOCK( name, align, ... ) \
993 for( i = 0; i < 36; i++ ) \
995 int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */\
996 for( j = 0; j < 1024; j++ ) \
997 /* two distributions of random to excersize different failure modes */\
998 buf3[j] = rand() & (i&1 ? 0xf : 0xff ); \
999 memcpy( buf4, buf3, 1024 ); \
1000 if( db_a.name != db_ref.name ) \
1002 set_func_name( #name );\
1004 call_c1( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1005 call_a1( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1006 if( memcmp( buf3, buf4, 1024 ) ) \
1009 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1012 call_c2( db_c.name, buf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1013 call_a2( db_a.name, buf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1017 TEST_DEBLOCK( deblock_h_luma, 0, tcs[i] );
1018 TEST_DEBLOCK( deblock_v_luma, 1, tcs[i] );
1019 TEST_DEBLOCK( deblock_h_chroma, 0, tcs[i] );
1020 TEST_DEBLOCK( deblock_v_chroma, 1, tcs[i] );
1021 TEST_DEBLOCK( deblock_h_luma_intra, 0 );
1022 TEST_DEBLOCK( deblock_v_luma_intra, 1 );
1023 TEST_DEBLOCK( deblock_h_chroma_intra, 0 );
1024 TEST_DEBLOCK( deblock_v_chroma_intra, 1 );
1026 report( "deblock :" );
1031 static int check_quant( int cpu_ref, int cpu_new )
1033 x264_quant_function_t qf_c;
1034 x264_quant_function_t qf_ref;
1035 x264_quant_function_t qf_a;
1036 DECLARE_ALIGNED_16( int16_t dct1[64] );
1037 DECLARE_ALIGNED_16( int16_t dct2[64] );
1038 DECLARE_ALIGNED_16( uint8_t cqm_buf[64] );
1039 int ret = 0, ok, used_asm;
1040 int oks[2] = {1,1}, used_asms[2] = {0,0};
1041 int i, j, i_cqm, qp;
1044 memset( h, 0, sizeof(*h) );
1045 h->pps = h->pps_array;
1046 x264_param_default( &h->param );
1047 h->param.rc.i_qp_min = 26;
1048 h->param.analyse.b_transform_8x8 = 1;
1050 for( i_cqm = 0; i_cqm < 4; i_cqm++ )
1054 for( i = 0; i < 6; i++ )
1055 h->pps->scaling_list[i] = x264_cqm_flat16;
1056 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1058 else if( i_cqm == 1 )
1060 for( i = 0; i < 6; i++ )
1061 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1062 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1067 for( i = 0; i < 64; i++ )
1068 cqm_buf[i] = 10 + rand() % 246;
1070 for( i = 0; i < 64; i++ )
1072 for( i = 0; i < 6; i++ )
1073 h->pps->scaling_list[i] = cqm_buf;
1074 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1078 x264_quant_init( h, 0, &qf_c );
1079 x264_quant_init( h, cpu_ref, &qf_ref );
1080 x264_quant_init( h, cpu_new, &qf_a );
1082 #define INIT_QUANT8() \
1084 static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1086 for( y = 0; y < 8; y++ ) \
1087 for( x = 0; x < 8; x++ ) \
1089 unsigned int scale = (255*scale1d[y]*scale1d[x])/16; \
1090 dct1[y*8+x] = dct2[y*8+x] = j ? (rand()%(2*scale+1))-scale : 0; \
1094 #define INIT_QUANT4() \
1096 static const int scale1d[4] = {4,6,4,6}; \
1098 for( y = 0; y < 4; y++ ) \
1099 for( x = 0; x < 4; x++ ) \
1101 unsigned int scale = 255*scale1d[y]*scale1d[x]; \
1102 dct1[y*4+x] = dct2[y*4+x] = j ? (rand()%(2*scale+1))-scale : 0; \
1106 #define TEST_QUANT_DC( name, cqm ) \
1107 if( qf_a.name != qf_ref.name ) \
1109 set_func_name( #name ); \
1111 for( qp = 51; qp > 0; qp-- ) \
1113 for( j = 0; j < 2; j++ ) \
1115 int result_c, result_a; \
1116 for( i = 0; i < 16; i++ ) \
1117 dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1118 result_c = call_c1( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1119 result_a = call_a1( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1120 if( memcmp( dct1, dct2, 16*2 ) || result_c != result_a ) \
1123 fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1126 call_c2( qf_c.name, (void*)dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1127 call_a2( qf_a.name, (void*)dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1132 #define TEST_QUANT( qname, block, w ) \
1133 if( qf_a.qname != qf_ref.qname ) \
1135 set_func_name( #qname ); \
1137 for( qp = 51; qp > 0; qp-- ) \
1139 for( j = 0; j < 2; j++ ) \
1141 int result_c, result_a; \
1143 result_c = call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1144 result_a = call_a1( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1145 if( memcmp( dct1, dct2, w*w*2 ) || result_c != result_a ) \
1148 fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1151 call_c2( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1152 call_a2( qf_a.qname, (void*)dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1157 TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1158 TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1159 TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1160 TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1161 TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1162 TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1164 #define TEST_DEQUANT( qname, dqname, block, w ) \
1165 if( qf_a.dqname != qf_ref.dqname ) \
1167 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1170 for( qp = 51; qp > 0; qp-- ) \
1173 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1174 memcpy( dct2, dct1, w*w*2 ); \
1175 call_c1( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1176 call_a1( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1177 if( memcmp( dct1, dct2, w*w*2 ) ) \
1180 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1183 call_c2( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1184 call_a2( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1188 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1189 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1190 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1191 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1193 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1194 if( qf_a.dqname != qf_ref.dqname ) \
1196 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1198 for( qp = 51; qp > 0; qp-- ) \
1200 for( i = 0; i < 16; i++ ) \
1202 call_c1( qf_c.qname, (void*)dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1203 memcpy( dct2, dct1, w*w*2 ); \
1204 call_c1( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1205 call_a1( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1206 if( memcmp( dct1, dct2, w*w*2 ) ) \
1209 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1211 call_c2( qf_c.dqname, (void*)dct1, h->dequant##w##_mf[block], qp ); \
1212 call_a2( qf_a.dqname, (void*)dct2, h->dequant##w##_mf[block], qp ); \
1216 TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1218 x264_cqm_delete( h );
1221 ok = oks[0]; used_asm = used_asms[0];
1222 report( "quant :" );
1224 ok = oks[1]; used_asm = used_asms[1];
1225 report( "dequant :" );
1227 ok = 1; used_asm = 0;
1228 if( qf_a.denoise_dct != qf_ref.denoise_dct )
1232 for( size = 16; size <= 64; size += 48 )
1234 set_func_name( "denoise_dct" );
1235 memcpy(dct1, buf1, size*2);
1236 memcpy(dct2, buf1, size*2);
1237 memcpy(buf3+256, buf3, 256);
1238 call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1239 call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1240 if( memcmp( dct1, dct2, size*2 ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1242 call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1243 call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1246 report( "denoise dct :" );
1248 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1249 if( qf_a.decname != qf_ref.decname ) \
1251 set_func_name( #decname ); \
1253 for( i = 0; i < 100; i++ ) \
1255 int result_c, result_a, idx; \
1256 for( idx = 0; idx < w*w; idx++ ) \
1257 dct1[idx] = !(rand()&3) + (!(rand()&15))*(rand()&3); \
1260 result_c = call_c( qf_c.decname, (void*)dct1 ); \
1261 result_a = call_a( qf_a.decname, (void*)dct1 ); \
1262 if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1265 fprintf( stderr, #decname ": [FAILED]\n" ); \
1271 ok = 1; used_asm = 0;
1272 TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1273 TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1274 TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1275 report( "decimate_score :" );
1277 #define TEST_LAST( last, lastname, w, ac ) \
1278 if( qf_a.last != qf_ref.last ) \
1280 set_func_name( #lastname ); \
1282 for( i = 0; i < 100; i++ ) \
1284 int result_c, result_a, idx, nnz=0; \
1285 int max = rand() & (w*w-1); \
1286 memset( dct1, 0, w*w*2 ); \
1287 for( idx = ac; idx < max; idx++ ) \
1288 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1291 result_c = call_c( qf_c.last, (void*)(dct1+ac) ); \
1292 result_a = call_a( qf_a.last, (void*)(dct1+ac) ); \
1293 if( result_c != result_a ) \
1296 fprintf( stderr, #lastname ": [FAILED]\n" ); \
1302 ok = 1; used_asm = 0;
1303 TEST_LAST( coeff_last[DCT_CHROMA_DC], coeff_last4, 2, 0 );
1304 TEST_LAST( coeff_last[ DCT_LUMA_AC], coeff_last15, 4, 1 );
1305 TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 4, 0 );
1306 TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 8, 0 );
1307 report( "coeff_last :" );
1309 #define TEST_LEVELRUN( lastname, name, w, ac ) \
1310 if( qf_a.lastname != qf_ref.lastname ) \
1312 set_func_name( #name ); \
1314 for( i = 0; i < 100; i++ ) \
1316 x264_run_level_t runlevel_c, runlevel_a; \
1317 int result_c, result_a, idx, nnz=0; \
1318 int max = rand() & (w*w-1); \
1319 memset( dct1, 0, w*w*2 ); \
1320 memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
1321 memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
1322 for( idx = ac; idx < max; idx++ ) \
1323 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1326 result_c = call_c( qf_c.lastname, (void*)(dct1+ac), &runlevel_c ); \
1327 result_a = call_a( qf_a.lastname, (void*)(dct1+ac), &runlevel_a ); \
1328 if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
1329 memcmp(runlevel_c.level, runlevel_a.level, sizeof(int16_t)*result_c) || \
1330 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
1333 fprintf( stderr, #name ": [FAILED]\n" ); \
1339 ok = 1; used_asm = 0;
1340 TEST_LEVELRUN( coeff_level_run[DCT_CHROMA_DC], coeff_level_run4, 2, 0 );
1341 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_AC], coeff_level_run15, 4, 1 );
1342 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 4, 0 );
1343 report( "coeff_level_run :" );
1348 static int check_intra( int cpu_ref, int cpu_new )
1350 int ret = 0, ok = 1, used_asm = 0;
1352 DECLARE_ALIGNED_16( uint8_t edge[33] );
1353 DECLARE_ALIGNED_16( uint8_t edge2[33] );
1356 x264_predict_t predict_16x16[4+3];
1357 x264_predict_t predict_8x8c[4+3];
1358 x264_predict8x8_t predict_8x8[9+3];
1359 x264_predict_t predict_4x4[9+3];
1360 x264_predict_8x8_filter_t predict_8x8_filter;
1361 } ip_c, ip_ref, ip_a;
1363 x264_predict_16x16_init( 0, ip_c.predict_16x16 );
1364 x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
1365 x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
1366 x264_predict_4x4_init( 0, ip_c.predict_4x4 );
1368 x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
1369 x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
1370 x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
1371 x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
1373 x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
1374 x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
1375 x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
1376 x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
1378 ip_c.predict_8x8_filter( buf1+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
1380 #define INTRA_TEST( name, dir, w, ... ) \
1381 if( ip_a.name[dir] != ip_ref.name[dir] )\
1383 set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
1385 memcpy( buf3, buf1, 32*20 );\
1386 memcpy( buf4, buf1, 32*20 );\
1387 call_c( ip_c.name[dir], buf3+48, ##__VA_ARGS__ );\
1388 call_a( ip_a.name[dir], buf4+48, ##__VA_ARGS__ );\
1389 if( memcmp( buf3, buf4, 32*20 ) )\
1391 fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\
1394 for(k=-1; k<16; k++)\
1395 printf("%2x ", edge[16+k]);\
1397 for(j=0; j<w; j++){\
1398 printf("%2x ", edge[14-j]);\
1400 printf("%2x ", buf4[48+k+j*32]);\
1404 for(j=0; j<w; j++){\
1407 printf("%2x ", buf3[48+k+j*32]);\
1413 for( i = 0; i < 12; i++ )
1414 INTRA_TEST( predict_4x4, i, 4 );
1415 for( i = 0; i < 7; i++ )
1416 INTRA_TEST( predict_8x8c, i, 8 );
1417 for( i = 0; i < 7; i++ )
1418 INTRA_TEST( predict_16x16, i, 16 );
1419 for( i = 0; i < 12; i++ )
1420 INTRA_TEST( predict_8x8, i, 8, edge );
1422 set_func_name("intra_predict_8x8_filter");
1423 if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
1426 for( i = 0; i < 32; i++ )
1428 memcpy( edge2, edge, 33 );
1429 call_c(ip_c.predict_8x8_filter, buf1+48, edge, (i&24)>>1, i&7);
1430 call_a(ip_a.predict_8x8_filter, buf1+48, edge2, (i&24)>>1, i&7);
1431 if( memcmp( edge, edge2, 33 ) )
1433 fprintf( stderr, "predict_8x8_filter : [FAILED] %d %d\n", (i&24)>>1, i&7);
1439 report( "intra pred :" );
1443 #define DECL_CABAC(cpu) \
1444 static void run_cabac_##cpu( uint8_t *dst )\
1448 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1449 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1450 for( i=0; i<0x1000; i++ )\
1451 x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
1457 #define run_cabac_asm run_cabac_c
1460 static int check_cabac( int cpu_ref, int cpu_new )
1462 int ret = 0, ok, used_asm = 1;
1463 if( cpu_ref || run_cabac_c == run_cabac_asm)
1465 set_func_name( "cabac_encode_decision" );
1466 memcpy( buf4, buf3, 0x1000 );
1467 call_c( run_cabac_c, buf3 );
1468 call_a( run_cabac_asm, buf4 );
1469 ok = !memcmp( buf3, buf4, 0x1000 );
1470 report( "cabac :" );
1474 static int check_all_funcs( int cpu_ref, int cpu_new )
1476 return check_pixel( cpu_ref, cpu_new )
1477 + check_dct( cpu_ref, cpu_new )
1478 + check_mc( cpu_ref, cpu_new )
1479 + check_intra( cpu_ref, cpu_new )
1480 + check_deblock( cpu_ref, cpu_new )
1481 + check_quant( cpu_ref, cpu_new )
1482 + check_cabac( cpu_ref, cpu_new );
1485 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
1487 *cpu_ref = *cpu_new;
1489 if( *cpu_new & X264_CPU_SSE2_IS_FAST )
1490 *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
1492 fprintf( stderr, "x264: %s\n", name );
1493 return check_all_funcs( *cpu_ref, *cpu_new );
1496 static int check_all_flags( void )
1499 int cpu0 = 0, cpu1 = 0;
1501 if( x264_cpu_detect() & X264_CPU_MMXEXT )
1503 ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMXEXT, "MMX" );
1504 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
1505 cpu1 &= ~X264_CPU_CACHELINE_64;
1507 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
1508 cpu1 &= ~X264_CPU_CACHELINE_32;
1510 if( x264_cpu_detect() & X264_CPU_LZCNT )
1512 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
1513 cpu1 &= ~X264_CPU_LZCNT;
1516 if( x264_cpu_detect() & X264_CPU_SSE2 )
1518 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
1519 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
1520 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
1521 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSE2 FastShuffle" );
1522 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
1524 if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
1526 cpu1 &= ~X264_CPU_CACHELINE_64;
1527 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
1528 cpu1 &= ~X264_CPU_SSE_MISALIGN;
1530 if( x264_cpu_detect() & X264_CPU_LZCNT )
1532 cpu1 &= ~X264_CPU_CACHELINE_64;
1533 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
1534 cpu1 &= ~X264_CPU_LZCNT;
1536 if( x264_cpu_detect() & X264_CPU_SSE3 )
1537 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
1538 if( x264_cpu_detect() & X264_CPU_SSSE3 )
1540 cpu1 &= ~X264_CPU_CACHELINE_64;
1541 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
1542 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
1543 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSSE3 FastShuffle" );
1544 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
1546 if( x264_cpu_detect() & X264_CPU_SSE4 )
1548 cpu1 &= ~X264_CPU_CACHELINE_64;
1549 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
1552 if( x264_cpu_detect() & X264_CPU_ALTIVEC )
1554 fprintf( stderr, "x264: ALTIVEC against C\n" );
1555 ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
1561 int main(int argc, char *argv[])
1566 if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
1568 #if !defined(ARCH_X86) && !defined(ARCH_X86_64) && !defined(ARCH_PPC)
1569 fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
1573 if( argv[1][7] == '=' )
1575 bench_pattern = argv[1]+8;
1576 bench_pattern_len = strlen(bench_pattern);
1582 i = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
1583 fprintf( stderr, "x264: using random seed %u\n", i );
1586 buf1 = x264_malloc( 0x3e00 + 16*BENCH_ALIGNS );
1589 fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
1592 buf2 = buf1 + 0xf00;
1593 buf3 = buf2 + 0xf00;
1594 buf4 = buf3 + 0x1000;
1595 for( i=0; i<0x1e00; i++ )
1596 buf1[i] = rand() & 0xFF;
1597 memset( buf1+0x1e00, 0, 0x2000 );
1599 /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
1601 for( i=0; i<BENCH_ALIGNS && !ret; i++ )
1603 buf2 = buf1 + 0xf00;
1604 buf3 = buf2 + 0xf00;
1605 buf4 = buf3 + 0x1000;
1606 ret |= x264_stack_pagealign( check_all_flags, i*16 );
1609 fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
1612 ret = check_all_flags();
1616 fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
1619 fprintf( stderr, "x264: All tests passed Yeah :)\n" );