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 // GCC doesn't align stack variables on ARM, so use .bss
36 #define ALIGNED_16( var ) DECLARE_ALIGNED( static var, 16 )
39 /* buf1, buf2: initialised to random data and shouldn't write into them */
41 /* buf3, buf4: used to store output */
43 /* pbuf*: point to the same memory as above, just for type convenience */
44 pixel *pbuf1, *pbuf2, *pbuf3, *pbuf4;
48 #define report( name ) { \
49 if( used_asm && !quiet ) \
50 fprintf( stderr, " - %-21s [%s]\n", name, ok ? "OK" : "FAILED" ); \
54 #define BENCH_RUNS 100 // tradeoff between accuracy and speed
55 #define BENCH_ALIGNS 16 // number of stack+heap data alignments (another accuracy vs speed tradeoff)
56 #define MAX_FUNCS 1000 // just has to be big enough to hold all the existing functions
57 #define MAX_CPUS 10 // number of different combinations of cpu flags
60 void *pointer; // just for detecting duplicates
68 bench_t vers[MAX_CPUS];
72 int bench_pattern_len = 0;
73 const char *bench_pattern = "";
75 static bench_func_t benchs[MAX_FUNCS];
77 static const char *pixel_names[10] = { "16x16", "16x8", "8x16", "8x8", "8x4", "4x8", "4x4", "4x2", "2x4", "2x2" };
78 static const char *intra_predict_16x16_names[7] = { "v", "h", "dc", "p", "dcl", "dct", "dc8" };
79 static const char *intra_predict_8x8c_names[7] = { "dc", "h", "v", "p", "dcl", "dct", "dc8" };
80 static const char *intra_predict_4x4_names[12] = { "v", "h", "dc", "ddl", "ddr", "vr", "hd", "vl", "hu", "dcl", "dct", "dc8" };
81 static const char **intra_predict_8x8_names = intra_predict_4x4_names;
83 #define set_func_name(...) snprintf( func_name, sizeof(func_name), __VA_ARGS__ )
85 static inline uint32_t read_time(void)
88 #if defined(__GNUC__) && (ARCH_X86 || ARCH_X86_64)
89 asm volatile( "rdtsc" :"=a"(a) ::"edx" );
91 asm volatile( "mftb %0" : "=r" (a) );
92 #elif ARCH_ARM // ARMv7 only
93 asm volatile( "mrc p15, 0, %0, c9, c13, 0" : "=r"(a) );
98 static bench_t* get_bench( const char *name, int cpu )
101 for( i = 0; benchs[i].name && strcmp(name, benchs[i].name); i++ )
102 assert( i < MAX_FUNCS );
103 if( !benchs[i].name )
104 benchs[i].name = strdup( name );
106 return &benchs[i].vers[0];
107 for( j = 1; benchs[i].vers[j].cpu && benchs[i].vers[j].cpu != cpu; j++ )
108 assert( j < MAX_CPUS );
109 benchs[i].vers[j].cpu = cpu;
110 return &benchs[i].vers[j];
113 static int cmp_nop( const void *a, const void *b )
115 return *(uint16_t*)a - *(uint16_t*)b;
118 static int cmp_bench( const void *a, const void *b )
120 // asciibetical sort except preserving numbers
121 const char *sa = ((bench_func_t*)a)->name;
122 const char *sb = ((bench_func_t*)b)->name;
127 if( isdigit( *sa ) && isdigit( *sb ) && isdigit( sa[1] ) != isdigit( sb[1] ) )
128 return isdigit( sa[1] ) - isdigit( sb[1] );
134 static void print_bench(void)
136 uint16_t nops[10000] = {0};
137 int nfuncs, nop_time=0;
139 for( int i = 0; i < 10000; i++ )
142 nops[i] = read_time() - t;
144 qsort( nops, 10000, sizeof(uint16_t), cmp_nop );
145 for( int i = 500; i < 9500; i++ )
148 printf( "nop: %d\n", nop_time );
150 for( nfuncs = 0; nfuncs < MAX_FUNCS && benchs[nfuncs].name; nfuncs++ );
151 qsort( benchs, nfuncs, sizeof(bench_func_t), cmp_bench );
152 for( int i = 0; i < nfuncs; i++ )
153 for( int j = 0; j < MAX_CPUS && (!j || benchs[i].vers[j].cpu); j++ )
156 bench_t *b = &benchs[i].vers[j];
159 for( k = 0; k < j && benchs[i].vers[k].pointer != b->pointer; k++ );
162 printf( "%s_%s%s: %"PRId64"\n", benchs[i].name,
163 b->cpu&X264_CPU_SSE4 ? "sse4" :
164 b->cpu&X264_CPU_SHUFFLE_IS_FAST ? "fastshuffle" :
165 b->cpu&X264_CPU_SSSE3 ? "ssse3" :
166 b->cpu&X264_CPU_SSE3 ? "sse3" :
167 /* print sse2slow only if there's also a sse2fast version of the same func */
168 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" :
169 b->cpu&X264_CPU_SSE2 ? "sse2" :
170 b->cpu&X264_CPU_MMX ? "mmx" :
171 b->cpu&X264_CPU_ALTIVEC ? "altivec" :
172 b->cpu&X264_CPU_NEON ? "neon" :
173 b->cpu&X264_CPU_ARMV6 ? "armv6" : "c",
174 b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
175 b->cpu&X264_CPU_CACHELINE_64 ? "_c64" :
176 b->cpu&X264_CPU_SSE_MISALIGN ? "_misalign" :
177 b->cpu&X264_CPU_LZCNT ? "_lzcnt" :
178 b->cpu&X264_CPU_FAST_NEON_MRC ? "_fast_mrc" :
179 b->cpu&X264_CPU_SLOW_CTZ ? "_slow_ctz" :
180 b->cpu&X264_CPU_SLOW_ATOM ? "_slow_atom" : "",
181 ((int64_t)10*b->cycles/b->den - nop_time)/4 );
185 #if ARCH_X86 || ARCH_X86_64
186 int x264_stack_pagealign( int (*func)(), int align );
188 #define x264_stack_pagealign( func, align ) func()
191 #define call_c1(func,...) func(__VA_ARGS__)
193 #if ARCH_X86 || defined(_WIN64)
194 /* detect when callee-saved regs aren't saved.
195 * needs an explicit asm check because it only sometimes crashes in normal use. */
196 intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... );
197 #define call_a1(func,...) x264_checkasm_call((intptr_t(*)())func, &ok, __VA_ARGS__)
199 #define call_a1 call_c1
202 #define call_bench(func,cpu,...)\
203 if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
207 call_a1(func, __VA_ARGS__);\
208 for( int ti = 0; ti < (cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
210 uint32_t t = read_time();\
215 t = read_time() - t;\
216 if( t*tcount <= tsum*4 && ti > 0 )\
222 bench_t *b = get_bench( func_name, cpu );\
228 /* for most functions, run benchmark and correctness test at the same time.
229 * for those that modify their inputs, run the above macros separately */
230 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
231 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
232 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
233 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
236 static int check_pixel( int cpu_ref, int cpu_new )
238 x264_pixel_function_t pixel_c;
239 x264_pixel_function_t pixel_ref;
240 x264_pixel_function_t pixel_asm;
241 x264_predict8x8_t predict_8x8[9+3];
242 x264_predict_8x8_filter_t predict_8x8_filter;
243 ALIGNED_16( pixel edge[33] );
244 uint16_t cost_mv[32];
245 int ret = 0, ok, used_asm;
247 x264_pixel_init( 0, &pixel_c );
248 x264_pixel_init( cpu_ref, &pixel_ref );
249 x264_pixel_init( cpu_new, &pixel_asm );
250 x264_predict_8x8_init( 0, predict_8x8, &predict_8x8_filter );
251 predict_8x8_filter( pbuf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
254 for( int i = 0; i < 256; i++ )
259 buf3[i] = ~(buf4[i] = -(z&1));
261 // random pattern made of maxed pixel differences, in case an intermediate value overflows
262 for( int i = 256; i < 0x1000; i++ )
263 buf3[i] = ~(buf4[i] = -(buf1[i&~0x88]&1));
265 #define TEST_PIXEL( name, align ) \
266 ok = 1, used_asm = 0; \
267 for( int i = 0; i < 7; i++ ) \
269 int res_c, res_asm; \
270 if( pixel_asm.name[i] != pixel_ref.name[i] ) \
272 set_func_name( "%s_%s", #name, pixel_names[i] ); \
274 for( int j = 0; j < 64; j++ ) \
276 res_c = call_c( pixel_c.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \
277 res_asm = call_a( pixel_asm.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \
278 if( res_c != res_asm ) \
281 fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
285 for( int j = 0; j < 0x1000 && ok; j += 256 ) \
287 res_c = pixel_c .name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
288 res_asm = pixel_asm.name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
289 if( res_c != res_asm ) \
292 fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
297 report( "pixel " #name " :" );
299 TEST_PIXEL( sad, 0 );
300 TEST_PIXEL( sad_aligned, 1 );
301 TEST_PIXEL( ssd, 1 );
302 TEST_PIXEL( satd, 0 );
303 TEST_PIXEL( sa8d, 1 );
305 #define TEST_PIXEL_X( N ) \
306 ok = 1; used_asm = 0; \
307 for( int i = 0; i < 7; i++ ) \
309 int res_c[4]={0}, res_asm[4]={0}; \
310 if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
312 set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
314 for( int j = 0; j < 64; j++ ) \
316 pixel *pix2 = pbuf2+j; \
317 res_c[0] = pixel_c.sad[i]( pbuf1, 16, pix2, 64 ); \
318 res_c[1] = pixel_c.sad[i]( pbuf1, 16, pix2+6, 64 ); \
319 res_c[2] = pixel_c.sad[i]( pbuf1, 16, pix2+1, 64 ); \
322 res_c[3] = pixel_c.sad[i]( pbuf1, 16, pix2+10, 64 ); \
323 call_a( pixel_asm.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
326 call_a( pixel_asm.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
327 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
330 fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
331 i, res_c[0], res_c[1], res_c[2], res_c[3], \
332 res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
335 call_c2( pixel_c.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
337 call_c2( pixel_c.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
341 report( "pixel sad_x"#N" :" );
346 #define TEST_PIXEL_VAR( i ) \
347 if( pixel_asm.var[i] != pixel_ref.var[i] ) \
349 set_func_name( "%s_%s", "var", pixel_names[i] ); \
351 /* abi-check wrapper can't return uint64_t, so separate it from return value check */ \
352 call_c1( pixel_c.var[i], pbuf1, 16 ); \
353 call_a1( pixel_asm.var[i], pbuf1, 16 ); \
354 uint64_t res_c = pixel_c.var[i]( pbuf1, 16 ); \
355 uint64_t res_asm = pixel_asm.var[i]( pbuf1, 16 ); \
356 if( res_c != res_asm ) \
359 fprintf( stderr, "var[%d]: %d %d != %d %d [FAILED]\n", i, (int)res_c, (int)(res_c>>32), (int)res_asm, (int)(res_asm>>32) ); \
361 call_c2( pixel_c.var[i], pbuf1, 16 ); \
362 call_a2( pixel_asm.var[i], pbuf1, 16 ); \
365 ok = 1; used_asm = 0;
366 TEST_PIXEL_VAR( PIXEL_16x16 );
367 TEST_PIXEL_VAR( PIXEL_8x8 );
368 report( "pixel var :" );
370 ok = 1; used_asm = 0;
371 if( pixel_asm.var2_8x8 != pixel_ref.var2_8x8 )
373 int res_c, res_asm, ssd_c, ssd_asm;
374 set_func_name( "var2_8x8" );
376 res_c = call_c( pixel_c.var2_8x8, pbuf1, 16, pbuf2, 16, &ssd_c );
377 res_asm = call_a( pixel_asm.var2_8x8, pbuf1, 16, pbuf2, 16, &ssd_asm );
378 if( res_c != res_asm || ssd_c != ssd_asm )
381 fprintf( stderr, "var2_8x8: %d != %d or %d != %d [FAILED]\n", res_c, res_asm, ssd_c, ssd_asm );
385 report( "pixel var2 :" );
387 ok = 1; used_asm = 0;
388 for( int i = 0; i < 4; i++ )
389 if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
391 set_func_name( "hadamard_ac_%s", pixel_names[i] );
393 for( int j = 0; j < 32; j++ )
395 pixel *pix = (j&16 ? pbuf1 : pbuf3) + (j&15)*256;
396 call_c1( pixel_c.hadamard_ac[i], pbuf1, 16 );
397 call_a1( pixel_asm.hadamard_ac[i], pbuf1, 16 );
398 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
399 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
403 fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
407 call_c2( pixel_c.hadamard_ac[i], pbuf1, 16 );
408 call_a2( pixel_asm.hadamard_ac[i], pbuf1, 16 );
410 report( "pixel hadamard_ac :" );
412 #define TEST_INTRA_MBCMP( name, pred, satd, i8x8, ... ) \
413 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
415 int res_c[3], res_asm[3]; \
416 set_func_name( #name ); \
418 call_c( pixel_c.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_c ); \
419 call_a( pixel_asm.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_asm ); \
420 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
423 fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
424 res_c[0], res_c[1], res_c[2], \
425 res_asm[0], res_asm[1], res_asm[2] ); \
429 ok = 1; used_asm = 0;
430 TEST_INTRA_MBCMP( intra_satd_x3_16x16, predict_16x16, satd[PIXEL_16x16], 0 );
431 TEST_INTRA_MBCMP( intra_satd_x3_8x8c , predict_8x8c , satd[PIXEL_8x8] , 0 );
432 TEST_INTRA_MBCMP( intra_satd_x3_4x4 , predict_4x4 , satd[PIXEL_4x4] , 0 );
433 TEST_INTRA_MBCMP( intra_sa8d_x3_8x8 , predict_8x8 , sa8d[PIXEL_8x8] , 1, edge );
434 report( "intra satd_x3 :" );
435 TEST_INTRA_MBCMP( intra_sad_x3_16x16 , predict_16x16, sad [PIXEL_16x16], 0 );
436 TEST_INTRA_MBCMP( intra_sad_x3_8x8c , predict_8x8c , sad [PIXEL_8x8] , 0 );
437 TEST_INTRA_MBCMP( intra_sad_x3_8x8 , predict_8x8 , sad [PIXEL_8x8] , 1, edge );
438 TEST_INTRA_MBCMP( intra_sad_x3_4x4 , predict_4x4 , sad [PIXEL_4x4] , 0 );
439 report( "intra sad_x3 :" );
441 if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
442 pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
445 ALIGNED_16( int sums[5][4] ) = {{0}};
448 res_c = x264_pixel_ssim_wxh( &pixel_c, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3 );
449 res_a = x264_pixel_ssim_wxh( &pixel_asm, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3 );
450 if( fabs( res_c - res_a ) > 1e-6 )
453 fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
455 set_func_name( "ssim_core" );
456 call_c2( pixel_c.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums );
457 call_a2( pixel_asm.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums );
458 set_func_name( "ssim_end" );
459 call_c2( pixel_c.ssim_end4, sums, sums, 4 );
460 call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
464 ok = 1; used_asm = 0;
465 for( int i = 0; i < 32; i++ )
467 for( int i = 0; i < 100 && ok; i++ )
468 if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
470 ALIGNED_16( uint16_t sums[72] );
471 ALIGNED_16( int dc[4] );
472 int16_t mvs_a[32], mvs_c[32];
474 int thresh = rand() & 0x3fff;
475 set_func_name( "esa_ads" );
476 for( int j = 0; j < 72; j++ )
477 sums[j] = rand() & 0x3fff;
478 for( int j = 0; j < 4; j++ )
479 dc[j] = rand() & 0x3fff;
481 mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
482 mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
483 if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
486 printf( "c%d: ", i&3 );
487 for( int j = 0; j < mvn_c; j++ )
488 printf( "%d ", mvs_c[j] );
489 printf( "\na%d: ", i&3 );
490 for( int j = 0; j < mvn_a; j++ )
491 printf( "%d ", mvs_a[j] );
495 report( "esa ads:" );
500 static int check_dct( int cpu_ref, int cpu_new )
502 x264_dct_function_t dct_c;
503 x264_dct_function_t dct_ref;
504 x264_dct_function_t dct_asm;
505 x264_quant_function_t qf;
506 int ret = 0, ok, used_asm, interlace;
507 ALIGNED_16( dctcoef dct1[16][16] );
508 ALIGNED_16( dctcoef dct2[16][16] );
509 ALIGNED_16( dctcoef dct4[16][16] );
510 ALIGNED_16( dctcoef dct8[4][64] );
511 ALIGNED_8( dctcoef dctdc[2][4] );
515 x264_dct_init( 0, &dct_c );
516 x264_dct_init( cpu_ref, &dct_ref);
517 x264_dct_init( cpu_new, &dct_asm );
519 memset( h, 0, sizeof(*h) );
520 h->pps = h->pps_array;
521 x264_param_default( &h->param );
522 h->chroma_qp_table = i_chroma_qp_table + 12;
523 h->param.analyse.i_luma_deadzone[0] = 0;
524 h->param.analyse.i_luma_deadzone[1] = 0;
525 h->param.analyse.b_transform_8x8 = 1;
526 for( int i = 0; i < 6; i++ )
527 h->pps->scaling_list[i] = x264_cqm_flat16;
529 x264_quant_init( h, 0, &qf );
531 #define TEST_DCT( name, t1, t2, size ) \
532 if( dct_asm.name != dct_ref.name ) \
534 set_func_name( #name ); \
536 call_c( dct_c.name, t1, pbuf1, pbuf2 ); \
537 call_a( dct_asm.name, t2, pbuf1, pbuf2 ); \
538 if( memcmp( t1, t2, size ) ) \
541 fprintf( stderr, #name " [FAILED]\n" ); \
544 ok = 1; used_asm = 0;
545 TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
546 TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
547 TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4*2 );
548 TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
549 report( "sub_dct4 :" );
551 ok = 1; used_asm = 0;
552 TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64*2 );
553 TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*2*4 );
554 report( "sub_dct8 :" );
557 // fdct and idct are denormalized by different factors, so quant/dequant
558 // is needed to force the coefs into the right range.
559 dct_c.sub16x16_dct( dct4, pbuf1, pbuf2 );
560 dct_c.sub16x16_dct8( dct8, pbuf1, pbuf2 );
561 for( int i = 0; i < 16; i++ )
563 qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
564 qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
566 for( int i = 0; i < 4; i++ )
568 qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
569 qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
572 #define TEST_IDCT( name, src ) \
573 if( dct_asm.name != dct_ref.name ) \
575 set_func_name( #name ); \
577 memcpy( buf3, buf1, 32*32 * sizeof(pixel) ); \
578 memcpy( buf4, buf1, 32*32 * sizeof(pixel) ); \
579 memcpy( dct1, src, 512 * sizeof(pixel) ); \
580 memcpy( dct2, src, 512 * sizeof(pixel) ); \
581 call_c1( dct_c.name, pbuf3, (void*)dct1 ); \
582 call_a1( dct_asm.name, pbuf4, (void*)dct2 ); \
583 if( memcmp( buf3, buf4, 32*32 * sizeof(pixel) ) ) \
586 fprintf( stderr, #name " [FAILED]\n" ); \
588 call_c2( dct_c.name, pbuf3, (void*)dct1 ); \
589 call_a2( dct_asm.name, pbuf4, (void*)dct2 ); \
591 ok = 1; used_asm = 0;
592 TEST_IDCT( add4x4_idct, dct4 );
593 TEST_IDCT( add8x8_idct, dct4 );
594 TEST_IDCT( add8x8_idct_dc, dct4 );
595 TEST_IDCT( add16x16_idct, dct4 );
596 TEST_IDCT( add16x16_idct_dc, dct4 );
597 report( "add_idct4 :" );
599 ok = 1; used_asm = 0;
600 TEST_IDCT( add8x8_idct8, dct8 );
601 TEST_IDCT( add16x16_idct8, dct8 );
602 report( "add_idct8 :" );
605 #define TEST_DCTDC( name )\
606 ok = 1; used_asm = 0;\
607 if( dct_asm.name != dct_ref.name )\
609 set_func_name( #name );\
611 uint16_t *p = (uint16_t*)buf1;\
612 for( int i = 0; i < 16 && ok; i++ )\
614 for( int j = 0; j < 16; j++ )\
615 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? 4080 : -4080 /* max dc */\
616 : i<8 ? (*p++)&1 ? 4080 : -4080 /* max elements */\
617 : ((*p++)&0x1fff)-0x1000; /* general case */\
618 memcpy( dct2, dct1, 32 );\
619 call_c1( dct_c.name, dct1[0] );\
620 call_a1( dct_asm.name, dct2[0] );\
621 if( memcmp( dct1, dct2, 32 ) )\
624 call_c2( dct_c.name, dct1[0] );\
625 call_a2( dct_asm.name, dct2[0] );\
627 report( #name " :" );
629 TEST_DCTDC( dct4x4dc );
630 TEST_DCTDC( idct4x4dc );
633 x264_zigzag_function_t zigzag_c;
634 x264_zigzag_function_t zigzag_ref;
635 x264_zigzag_function_t zigzag_asm;
637 ALIGNED_16( dctcoef level1[64] );
638 ALIGNED_16( dctcoef level2[64] );
640 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
641 if( zigzag_asm.name != zigzag_ref.name ) \
643 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
645 memcpy(dct, buf1, size*sizeof(dctcoef)); \
646 call_c( zigzag_c.name, t1, dct ); \
647 call_a( zigzag_asm.name, t2, dct ); \
648 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
651 fprintf( stderr, #name " [FAILED]\n" ); \
655 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
656 if( zigzag_asm.name != zigzag_ref.name ) \
659 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
661 memcpy( buf3, buf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
662 memcpy( buf4, buf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
663 nz_c = call_c1( zigzag_c.name, t1, pbuf2, pbuf3 ); \
664 nz_a = call_a1( zigzag_asm.name, t2, pbuf2, pbuf4 ); \
665 if( memcmp( t1, t2, size*sizeof(dctcoef) )|| memcmp( buf3, buf4, 16*FDEC_STRIDE ) || nz_c != nz_a ) \
668 fprintf( stderr, #name " [FAILED]\n" ); \
670 call_c2( zigzag_c.name, t1, pbuf2, pbuf3 ); \
671 call_a2( zigzag_asm.name, t2, pbuf2, pbuf4 ); \
674 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
675 if( zigzag_asm.name != zigzag_ref.name ) \
678 dctcoef dc_a, dc_c; \
679 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
681 for( int i = 0; i < 2; i++ ) \
683 memcpy( buf3, buf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
684 memcpy( buf4, buf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
685 for( int j = 0; j < 4; j++ ) \
687 memcpy( pbuf3 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
688 memcpy( pbuf4 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
690 nz_c = call_c1( zigzag_c.name, t1, pbuf2, pbuf3, &dc_c ); \
691 nz_a = call_a1( zigzag_asm.name, t2, pbuf2, pbuf4, &dc_a ); \
692 if( memcmp( t1+1, t2+1, 15*sizeof(dctcoef) ) || memcmp( buf3, buf4, 16*FDEC_STRIDE * sizeof(pixel) ) || nz_c != nz_a || dc_c != dc_a ) \
695 fprintf( stderr, #name " [FAILED]\n" ); \
699 call_c2( zigzag_c.name, t1, pbuf2, pbuf3, &dc_c ); \
700 call_a2( zigzag_asm.name, t2, pbuf2, pbuf4, &dc_a ); \
703 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
704 if( zigzag_asm.name != zigzag_ref.name ) \
706 for( int j = 0; j < 100; j++ ) \
708 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
710 memcpy(dct, buf1, size*sizeof(dctcoef)); \
711 for( int i = 0; i < size; i++ ) \
712 dct[i] = rand()&0x1F ? 0 : dct[i]; \
713 memcpy(buf3, buf4, 10); \
714 call_c( zigzag_c.name, t1, dct, buf3 ); \
715 call_a( zigzag_asm.name, t2, dct, buf4 ); \
716 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( buf3, buf4, 10 ) ) \
724 x264_zigzag_init( 0, &zigzag_c, 0 );
725 x264_zigzag_init( cpu_ref, &zigzag_ref, 0 );
726 x264_zigzag_init( cpu_new, &zigzag_asm, 0 );
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_frame :" );
736 x264_zigzag_init( 0, &zigzag_c, 1 );
737 x264_zigzag_init( cpu_ref, &zigzag_ref, 1 );
738 x264_zigzag_init( cpu_new, &zigzag_asm, 1 );
740 ok = 1; used_asm = 0;
741 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
742 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
743 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
744 TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
745 report( "zigzag_field :" );
747 ok = 1; used_asm = 0;
748 TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct1[0], 64 );
749 report( "zigzag_interleave :" );
750 #undef TEST_ZIGZAG_SCAN
751 #undef TEST_ZIGZAG_SUB
756 static int check_mc( int cpu_ref, int cpu_new )
758 x264_mc_functions_t mc_c;
759 x264_mc_functions_t mc_ref;
760 x264_mc_functions_t mc_a;
761 x264_pixel_function_t pixf;
763 pixel *src = &(pbuf1)[2*64+2];
764 pixel *src2[4] = { &(pbuf1)[3*64+2], &(pbuf1)[5*64+2],
765 &(pbuf1)[7*64+2], &(pbuf1)[9*64+2] };
769 int ret = 0, ok, used_asm;
771 x264_mc_init( 0, &mc_c );
772 x264_mc_init( cpu_ref, &mc_ref );
773 x264_mc_init( cpu_new, &mc_a );
774 x264_pixel_init( 0, &pixf );
776 #define MC_TEST_LUMA( w, h ) \
777 if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
779 const x264_weight_t *weight = weight_none; \
780 set_func_name( "mc_luma_%dx%d", w, h ); \
782 memset( buf3, 0xCD, 1024 ); \
783 memset( buf4, 0xCD, 1024 ); \
784 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
785 call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h, weight ); \
786 if( memcmp( buf3, buf4, 1024 ) ) \
788 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
792 if( mc_a.get_ref != mc_ref.get_ref ) \
795 int ref_stride = 32; \
796 const x264_weight_t *weight = weight_none; \
797 set_func_name( "get_ref_%dx%d", w, h ); \
799 memset( buf3, 0xCD, 1024 ); \
800 memset( buf4, 0xCD, 1024 ); \
801 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
802 ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h, weight ); \
803 for( int i = 0; i < h; i++ ) \
804 if( memcmp( dst1+i*32, ref+i*ref_stride, w * sizeof(pixel) ) ) \
806 fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
812 #define MC_TEST_CHROMA( w, h ) \
813 if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
815 set_func_name( "mc_chroma_%dx%d", w, h ); \
817 memset( buf3, 0xCD, 1024 ); \
818 memset( buf4, 0xCD, 1024 ); \
819 call_c( mc_c.mc_chroma, dst1, 16, src, 64, dx, dy, w, h ); \
820 call_a( mc_a.mc_chroma, dst2, 16, src, 64, dx, dy, w, h ); \
821 /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \
822 for( int j = 0; j < h; j++ ) \
823 for( int i = w; i < 4; i++ ) \
824 dst2[i+j*16] = dst1[i+j*16]; \
825 if( memcmp( buf3, buf4, 1024 ) ) \
827 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
831 ok = 1; used_asm = 0;
832 for( int dy = -8; dy < 8; dy++ )
833 for( int dx = -128; dx < 128; dx++ )
835 if( rand()&15 ) continue; // running all of them is too slow
836 MC_TEST_LUMA( 20, 18 );
837 MC_TEST_LUMA( 16, 16 );
838 MC_TEST_LUMA( 16, 8 );
839 MC_TEST_LUMA( 12, 10 );
840 MC_TEST_LUMA( 8, 16 );
841 MC_TEST_LUMA( 8, 8 );
842 MC_TEST_LUMA( 8, 4 );
843 MC_TEST_LUMA( 4, 8 );
844 MC_TEST_LUMA( 4, 4 );
846 report( "mc luma :" );
848 ok = 1; used_asm = 0;
849 for( int dy = -1; dy < 9; dy++ )
850 for( int dx = -128; dx < 128; dx++ )
852 if( rand()&15 ) continue;
853 MC_TEST_CHROMA( 8, 8 );
854 MC_TEST_CHROMA( 8, 4 );
855 MC_TEST_CHROMA( 4, 8 );
856 MC_TEST_CHROMA( 4, 4 );
857 MC_TEST_CHROMA( 4, 2 );
858 MC_TEST_CHROMA( 2, 4 );
859 MC_TEST_CHROMA( 2, 2 );
861 report( "mc chroma :" );
863 #undef MC_TEST_CHROMA
865 #define MC_TEST_AVG( name, weight ) \
867 ok = 1, used_asm = 0; \
868 for( int i = 0; i < 10; i++ ) \
870 memcpy( buf3, pbuf1+320, 320 * sizeof(pixel) ); \
871 memcpy( buf4, pbuf1+320, 320 * sizeof(pixel) ); \
872 if( mc_a.name[i] != mc_ref.name[i] ) \
874 set_func_name( "%s_%s", #name, pixel_names[i] ); \
876 call_c1( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
877 call_a1( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
878 if( memcmp( buf3, buf4, 320 * sizeof(pixel) ) ) \
881 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
883 call_c2( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
884 call_a2( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
889 for( int w = -63; w <= 127 && ok; w++ )
890 MC_TEST_AVG( avg, w );
891 report( "mc wpredb :" );
893 #define MC_TEST_WEIGHT( name, weight, aligned ) \
894 int align_off = (aligned ? 0 : rand()%16); \
895 ok = 1, used_asm = 0; \
896 for( int i = 1; i <= 5; i++ ) \
898 ALIGNED_16( pixel buffC[640] ); \
899 ALIGNED_16( pixel buffA[640] ); \
900 int j = X264_MAX( i*4, 2 ); \
901 memset( buffC, 0, 640 * sizeof(pixel) ); \
902 memset( buffA, 0, 640 * sizeof(pixel) ); \
905 /* w12 is the same as w16 in some cases */ \
906 if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
908 if( mc_a.name[i] != mc_ref.name[i] ) \
910 set_func_name( "%s_w%d", #name, j ); \
912 call_c1( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
913 mc_a.weight_cache(&ha, &weight); \
914 call_a1( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
915 for( int k = 0; k < 16; k++ ) \
916 if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \
919 fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
922 call_c2( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
923 call_a2( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
927 ok = 1; used_asm = 0;
930 for( int s = 0; s <= 127 && ok; s++ )
932 for( int o = -128; o <= 127 && ok; o++ )
934 if( rand() & 2047 ) continue;
935 for( int d = 0; d <= 7 && ok; d++ )
939 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
940 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
945 report( "mc weight :" );
947 ok = 1; used_asm = 0;
948 for( int o = 0; o <= 127 && ok; o++ )
951 if( rand() & 15 ) continue;
952 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
953 MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
955 report( "mc offsetadd :" );
956 ok = 1; used_asm = 0;
957 for( int o = -128; o < 0 && ok; o++ )
960 if( rand() & 15 ) continue;
961 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
962 MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
964 report( "mc offsetsub :" );
966 if( mc_a.hpel_filter != mc_ref.hpel_filter )
968 pixel *srchpel = pbuf1+8+2*64;
969 pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
970 pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
971 void *tmp = pbuf3+49*64;
972 set_func_name( "hpel_filter" );
973 ok = 1; used_asm = 1;
974 memset( buf3, 0, 4096 * sizeof(pixel) );
975 memset( buf4, 0, 4096 * sizeof(pixel) );
976 call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, 64, 48, 10, tmp );
977 call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, 64, 48, 10, tmp );
978 for( int i = 0; i < 3; i++ )
979 for( int j = 0; j < 10; j++ )
980 //FIXME ideally the first pixels would match too, but they aren't actually used
981 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
984 fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
985 for( int k = 0; k < 48; k++ )
986 printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
988 for( int k = 0; k < 48; k++ )
989 printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
993 report( "hpel filter :" );
996 if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
998 pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
999 pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1000 set_func_name( "lowres_init" );
1001 ok = 1; used_asm = 1;
1002 for( int w = 40; w <= 48; w += 8 )
1004 int stride = (w+8)&~15;
1005 call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
1006 call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
1007 for( int i = 0; i < 16; i++ )
1009 for( int j = 0; j < 4; j++ )
1010 if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w * sizeof(pixel) ) )
1013 fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1014 for( int k = 0; k < w; k++ )
1015 printf( "%d ", dstc[j][k+i*stride] );
1017 for( int k = 0; k < w; k++ )
1018 printf( "%d ", dsta[j][k+i*stride] );
1024 report( "lowres init :" );
1027 #define INTEGRAL_INIT( name, size, ... )\
1028 if( mc_a.name != mc_ref.name )\
1031 set_func_name( #name );\
1033 memcpy( buf3, buf1, size*2*stride * sizeof(pixel) );\
1034 memcpy( buf4, buf1, size*2*stride * sizeof(pixel) );\
1035 uint16_t *sum = (uint16_t*)buf3;\
1036 call_c1( mc_c.name, __VA_ARGS__ );\
1037 sum = (uint16_t*)buf4;\
1038 call_a1( mc_a.name, __VA_ARGS__ );\
1039 if( memcmp( buf3, buf4, (stride-8)*2 * sizeof(pixel) )\
1040 || (size>9 && memcmp( pbuf3+18*stride, pbuf4+18*stride, (stride-8)*2 * sizeof(pixel) )))\
1042 call_c2( mc_c.name, __VA_ARGS__ );\
1043 call_a2( mc_a.name, __VA_ARGS__ );\
1045 ok = 1; used_asm = 0;
1046 INTEGRAL_INIT( integral_init4h, 2, sum+stride, pbuf2, stride );
1047 INTEGRAL_INIT( integral_init8h, 2, sum+stride, pbuf2, stride );
1048 INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
1049 INTEGRAL_INIT( integral_init8v, 9, sum, stride );
1050 report( "integral init :" );
1052 if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1054 ok = 1; used_asm = 1;
1055 set_func_name( "mbtree_propagate" );
1056 int *dsta = (int*)buf3;
1057 int *dstc = dsta+400;
1058 uint16_t *prop = (uint16_t*)buf1;
1059 uint16_t *intra = (uint16_t*)buf4;
1060 uint16_t *inter = intra+400;
1061 uint16_t *qscale = inter+400;
1062 uint16_t *rnd = (uint16_t*)buf2;
1064 for( int i = 0; i < 400; i++ )
1066 intra[i] = *rnd++ & 0x7fff;
1067 intra[i] += !intra[i];
1068 inter[i] = *rnd++ & 0x7fff;
1069 qscale[i] = *rnd++ & 0x7fff;
1071 call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, 400 );
1072 call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, 400 );
1073 // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1075 for( int i = 0; i < 400; i++ )
1076 ok &= abs( dstc[i]-dsta[i] ) <= 1 || fabs( (double)dstc[i]/dsta[i]-1 ) < 1e-6;
1077 report( "mbtree propagate :" );
1083 static int check_deblock( int cpu_ref, int cpu_new )
1085 x264_deblock_function_t db_c;
1086 x264_deblock_function_t db_ref;
1087 x264_deblock_function_t db_a;
1088 int ret = 0, ok = 1, used_asm = 0;
1089 int alphas[36], betas[36];
1092 x264_deblock_init( 0, &db_c );
1093 x264_deblock_init( cpu_ref, &db_ref );
1094 x264_deblock_init( cpu_new, &db_a );
1096 /* not exactly the real values of a,b,tc but close enough */
1097 for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1101 tcs[i][0] = tcs[i][3] = (c+6)/10;
1102 tcs[i][1] = (c+7)/15;
1103 tcs[i][2] = (c+9)/20;
1108 #define TEST_DEBLOCK( name, align, ... ) \
1109 for( int i = 0; i < 36; i++ ) \
1111 int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1112 for( int j = 0; j < 1024; j++ ) \
1113 /* two distributions of random to excersize different failure modes */ \
1114 buf3[j] = rand() & (i&1 ? 0xf : 0xff ); \
1115 memcpy( buf4, buf3, 1024 * sizeof(pixel) ); \
1116 if( db_a.name != db_ref.name ) \
1118 set_func_name( #name ); \
1120 call_c1( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1121 call_a1( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1122 if( memcmp( buf3, buf4, 1024 * sizeof(pixel) ) ) \
1125 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1128 call_c2( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1129 call_a2( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1133 TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1134 TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1135 TEST_DEBLOCK( deblock_chroma[0], 0, tcs[i] );
1136 TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1137 TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1138 TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1139 TEST_DEBLOCK( deblock_chroma_intra[0], 0 );
1140 TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1142 if( db_a.deblock_strength != db_ref.deblock_strength )
1144 for( int i = 0; i < 100; i++ )
1146 ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1147 ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1148 ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] );
1149 ALIGNED_ARRAY_16( uint8_t, bs, [2],[2][4][4] );
1150 for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1151 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1152 for( int j = 0; j < 2; j++ )
1153 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1155 ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1156 for( int l = 0; l < 2; l++ )
1157 mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1159 set_func_name( "deblock_strength" );
1160 call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1161 call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1162 if( memcmp( bs[0], bs[1], sizeof(bs[0]) ) )
1165 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1166 for( int j = 0; j < 2; j++ )
1168 for( int k = 0; k < 2; k++ )
1169 for( int l = 0; l < 4; l++ )
1171 for( int m = 0; m < 4; m++ )
1172 printf("%d ",bs[j][k][l][m]);
1182 report( "deblock :" );
1187 static int check_quant( int cpu_ref, int cpu_new )
1189 x264_quant_function_t qf_c;
1190 x264_quant_function_t qf_ref;
1191 x264_quant_function_t qf_a;
1192 ALIGNED_16( dctcoef dct1[64] );
1193 ALIGNED_16( dctcoef dct2[64] );
1194 ALIGNED_16( uint8_t cqm_buf[64] );
1195 int ret = 0, ok, used_asm;
1196 int oks[2] = {1,1}, used_asms[2] = {0,0};
1199 memset( h, 0, sizeof(*h) );
1200 h->pps = h->pps_array;
1201 x264_param_default( &h->param );
1202 h->chroma_qp_table = i_chroma_qp_table + 12;
1203 h->param.rc.i_qp_min = 26;
1204 h->param.analyse.b_transform_8x8 = 1;
1206 for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1210 for( int i = 0; i < 6; i++ )
1211 h->pps->scaling_list[i] = x264_cqm_flat16;
1212 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1214 else if( i_cqm == 1 )
1216 for( int i = 0; i < 6; i++ )
1217 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1218 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1223 for( int i = 0; i < 64; i++ )
1224 cqm_buf[i] = 10 + rand() % 246;
1226 for( int i = 0; i < 64; i++ )
1228 for( int i = 0; i < 6; i++ )
1229 h->pps->scaling_list[i] = cqm_buf;
1230 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1234 x264_quant_init( h, 0, &qf_c );
1235 x264_quant_init( h, cpu_ref, &qf_ref );
1236 x264_quant_init( h, cpu_new, &qf_a );
1238 #define INIT_QUANT8(j) \
1240 static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1241 for( int i = 0; i < 64; i++ ) \
1243 unsigned int scale = (255*scale1d[i>>3]*scale1d[i&7])/16; \
1244 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1248 #define INIT_QUANT4(j) \
1250 static const int scale1d[4] = {4,6,4,6}; \
1251 for( int i = 0; i < 16; i++ ) \
1253 unsigned int scale = 255*scale1d[i>>2]*scale1d[i&3]; \
1254 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1258 #define TEST_QUANT_DC( name, cqm ) \
1259 if( qf_a.name != qf_ref.name ) \
1261 set_func_name( #name ); \
1263 for( int qp = 51; qp > 0; qp-- ) \
1265 for( int j = 0; j < 2; j++ ) \
1267 int result_c, result_a; \
1268 for( int i = 0; i < 16; i++ ) \
1269 dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1270 result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1271 result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1272 if( memcmp( dct1, dct2, 16*2 ) || result_c != result_a ) \
1275 fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1278 call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1279 call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1284 #define TEST_QUANT( qname, block, w ) \
1285 if( qf_a.qname != qf_ref.qname ) \
1287 set_func_name( #qname ); \
1289 for( int qp = 51; qp > 0; qp-- ) \
1291 for( int j = 0; j < 2; j++ ) \
1294 int result_c = call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1295 int result_a = call_a1( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1296 if( memcmp( dct1, dct2, w*w*2 ) || result_c != result_a ) \
1299 fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1302 call_c2( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1303 call_a2( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1308 TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1309 TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1310 TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1311 TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1312 TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1313 TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1315 #define TEST_DEQUANT( qname, dqname, block, w ) \
1316 if( qf_a.dqname != qf_ref.dqname ) \
1318 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1320 for( int qp = 51; qp > 0; qp-- ) \
1323 call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1324 memcpy( dct2, dct1, w*w*2 ); \
1325 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1326 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1327 if( memcmp( dct1, dct2, w*w*2 ) ) \
1330 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1333 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1334 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1338 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1339 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1340 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1341 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1343 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1344 if( qf_a.dqname != qf_ref.dqname ) \
1346 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1348 for( int qp = 51; qp > 0; qp-- ) \
1350 for( int i = 0; i < 16; i++ ) \
1352 call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1353 memcpy( dct2, dct1, w*w*2 ); \
1354 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1355 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1356 if( memcmp( dct1, dct2, w*w*2 ) ) \
1359 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1361 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1362 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1366 TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1368 x264_cqm_delete( h );
1371 ok = oks[0]; used_asm = used_asms[0];
1372 report( "quant :" );
1374 ok = oks[1]; used_asm = used_asms[1];
1375 report( "dequant :" );
1377 ok = 1; used_asm = 0;
1378 if( qf_a.denoise_dct != qf_ref.denoise_dct )
1381 for( int size = 16; size <= 64; size += 48 )
1383 set_func_name( "denoise_dct" );
1384 memcpy( dct1, buf1, size*2 );
1385 memcpy( dct2, buf1, size*2 );
1386 memcpy( buf3+256, buf3, 256 );
1387 call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1388 call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1389 if( memcmp( dct1, dct2, size*2 ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1391 call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1392 call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1395 report( "denoise dct :" );
1397 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1398 if( qf_a.decname != qf_ref.decname ) \
1400 set_func_name( #decname ); \
1402 for( int i = 0; i < 100; i++ ) \
1404 for( int idx = 0; idx < w*w; idx++ ) \
1405 dct1[idx] = !(rand()&3) + (!(rand()&15))*(rand()&3); \
1408 int result_c = call_c( qf_c.decname, dct1 ); \
1409 int result_a = call_a( qf_a.decname, dct1 ); \
1410 if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1413 fprintf( stderr, #decname ": [FAILED]\n" ); \
1419 ok = 1; used_asm = 0;
1420 TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1421 TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1422 TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1423 report( "decimate_score :" );
1425 #define TEST_LAST( last, lastname, w, ac ) \
1426 if( qf_a.last != qf_ref.last ) \
1428 set_func_name( #lastname ); \
1430 for( int i = 0; i < 100; i++ ) \
1433 int max = rand() & (w*w-1); \
1434 memset( dct1, 0, w*w*2 ); \
1435 for( int idx = ac; idx < max; idx++ ) \
1436 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1439 int result_c = call_c( qf_c.last, dct1+ac ); \
1440 int result_a = call_a( qf_a.last, dct1+ac ); \
1441 if( result_c != result_a ) \
1444 fprintf( stderr, #lastname ": [FAILED]\n" ); \
1450 ok = 1; used_asm = 0;
1451 TEST_LAST( coeff_last[DCT_CHROMA_DC], coeff_last4, 2, 0 );
1452 TEST_LAST( coeff_last[ DCT_LUMA_AC], coeff_last15, 4, 1 );
1453 TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 4, 0 );
1454 TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 8, 0 );
1455 report( "coeff_last :" );
1457 #define TEST_LEVELRUN( lastname, name, w, ac ) \
1458 if( qf_a.lastname != qf_ref.lastname ) \
1460 set_func_name( #name ); \
1462 for( int i = 0; i < 100; i++ ) \
1464 x264_run_level_t runlevel_c, runlevel_a; \
1466 int max = rand() & (w*w-1); \
1467 memset( dct1, 0, w*w*2 ); \
1468 memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
1469 memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
1470 for( int idx = ac; idx < max; idx++ ) \
1471 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1474 int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
1475 int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
1476 if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
1477 memcmp(runlevel_c.level, runlevel_a.level, sizeof(int16_t)*result_c) || \
1478 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
1481 fprintf( stderr, #name ": [FAILED]\n" ); \
1487 ok = 1; used_asm = 0;
1488 TEST_LEVELRUN( coeff_level_run[DCT_CHROMA_DC], coeff_level_run4, 2, 0 );
1489 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_AC], coeff_level_run15, 4, 1 );
1490 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 4, 0 );
1491 report( "coeff_level_run :" );
1496 static int check_intra( int cpu_ref, int cpu_new )
1498 int ret = 0, ok = 1, used_asm = 0;
1499 ALIGNED_16( pixel edge[33] );
1500 ALIGNED_16( pixel edge2[33] );
1503 x264_predict_t predict_16x16[4+3];
1504 x264_predict_t predict_8x8c[4+3];
1505 x264_predict8x8_t predict_8x8[9+3];
1506 x264_predict_t predict_4x4[9+3];
1507 x264_predict_8x8_filter_t predict_8x8_filter;
1508 } ip_c, ip_ref, ip_a;
1510 x264_predict_16x16_init( 0, ip_c.predict_16x16 );
1511 x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
1512 x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
1513 x264_predict_4x4_init( 0, ip_c.predict_4x4 );
1515 x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
1516 x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
1517 x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
1518 x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
1520 x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
1521 x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
1522 x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
1523 x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
1525 ip_c.predict_8x8_filter( pbuf1+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
1527 #define INTRA_TEST( name, dir, w, ... )\
1528 if( ip_a.name[dir] != ip_ref.name[dir] )\
1530 set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
1532 memcpy( buf3, buf1, 32*20 * sizeof(pixel) );\
1533 memcpy( buf4, buf1, 32*20 * sizeof(pixel) );\
1534 call_c( ip_c.name[dir], pbuf3+48, ##__VA_ARGS__ );\
1535 call_a( ip_a.name[dir], pbuf4+48, ##__VA_ARGS__ );\
1536 if( memcmp( buf3, buf4, 32*20 * sizeof(pixel) ) )\
1538 fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\
1540 for( int k = -1; k < 16; k++ )\
1541 printf( "%2x ", edge[16+k] );\
1543 for( int j = 0; j < w; j++ )\
1545 printf( "%2x ", edge[14-j] );\
1546 for( int k = 0; k < w; k++ )\
1547 printf( "%2x ", buf4[48+k+j*32] );\
1551 for( int j = 0; j < w; j++ )\
1554 for( int k = 0; k < w; k++ )\
1555 printf( "%2x ", buf3[48+k+j*32] );\
1561 for( int i = 0; i < 12; i++ )
1562 INTRA_TEST( predict_4x4, i, 4 );
1563 for( int i = 0; i < 7; i++ )
1564 INTRA_TEST( predict_8x8c, i, 8 );
1565 for( int i = 0; i < 7; i++ )
1566 INTRA_TEST( predict_16x16, i, 16 );
1567 for( int i = 0; i < 12; i++ )
1568 INTRA_TEST( predict_8x8, i, 8, edge );
1570 set_func_name("intra_predict_8x8_filter");
1571 if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
1574 for( int i = 0; i < 32; i++ )
1576 memcpy( edge2, edge, 33 * sizeof(pixel) );
1577 call_c(ip_c.predict_8x8_filter, pbuf1+48, edge, (i&24)>>1, i&7);
1578 call_a(ip_a.predict_8x8_filter, pbuf1+48, edge2, (i&24)>>1, i&7);
1579 if( memcmp( edge, edge2, 33 * sizeof(pixel) ) )
1581 fprintf( stderr, "predict_8x8_filter : [FAILED] %d %d\n", (i&24)>>1, i&7);
1587 report( "intra pred :" );
1591 #define DECL_CABAC(cpu) \
1592 static void run_cabac_decision_##cpu( uint8_t *dst )\
1595 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1596 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1597 for( int i = 0; i < 0x1000; i++ )\
1598 x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
1600 static void run_cabac_bypass_##cpu( uint8_t *dst )\
1603 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1604 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1605 for( int i = 0; i < 0x1000; i++ )\
1606 x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
1608 static void run_cabac_terminal_##cpu( uint8_t *dst )\
1611 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1612 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1613 for( int i = 0; i < 0x1000; i++ )\
1614 x264_cabac_encode_terminal_##cpu( &cb );\
1620 #define run_cabac_decision_asm run_cabac_decision_c
1621 #define run_cabac_bypass_asm run_cabac_bypass_c
1622 #define run_cabac_terminal_asm run_cabac_terminal_c
1625 static int check_cabac( int cpu_ref, int cpu_new )
1627 int ret = 0, ok, used_asm = 1;
1628 if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
1631 set_func_name( "cabac_encode_decision" );
1632 memcpy( buf4, buf3, 0x1000 );
1633 call_c( run_cabac_decision_c, buf3 );
1634 call_a( run_cabac_decision_asm, buf4 );
1635 ok = !memcmp( buf3, buf4, 0x1000 );
1636 report( "cabac decision:" );
1638 set_func_name( "cabac_encode_bypass" );
1639 memcpy( buf4, buf3, 0x1000 );
1640 call_c( run_cabac_bypass_c, buf3 );
1641 call_a( run_cabac_bypass_asm, buf4 );
1642 ok = !memcmp( buf3, buf4, 0x1000 );
1643 report( "cabac bypass:" );
1645 set_func_name( "cabac_encode_terminal" );
1646 memcpy( buf4, buf3, 0x1000 );
1647 call_c( run_cabac_terminal_c, buf3 );
1648 call_a( run_cabac_terminal_asm, buf4 );
1649 ok = !memcmp( buf3, buf4, 0x1000 );
1650 report( "cabac terminal:" );
1655 static int check_bitstream( int cpu_ref, int cpu_new )
1657 x264_bitstream_function_t bs_c;
1658 x264_bitstream_function_t bs_ref;
1659 x264_bitstream_function_t bs_a;
1661 int ret = 0, ok = 1, used_asm = 0;
1663 x264_bitstream_init( 0, &bs_c );
1664 x264_bitstream_init( cpu_ref, &bs_ref );
1665 x264_bitstream_init( cpu_new, &bs_a );
1666 if( bs_a.nal_escape != bs_ref.nal_escape )
1669 uint8_t *input = malloc(size+100);
1670 uint8_t *output1 = malloc(size*2);
1671 uint8_t *output2 = malloc(size*2);
1673 set_func_name( "nal_escape" );
1674 for( int i = 0; i < 100; i++ )
1676 /* Test corner-case sizes */
1677 int test_size = i < 10 ? i+1 : rand() & 0x3fff;
1678 /* Test 8 different probability distributions of zeros */
1679 for( int j = 0; j < test_size; j++ )
1680 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
1681 uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
1682 uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
1683 int size_c = end_c-output1;
1684 int size_a = end_a-output2;
1685 if( size_c != size_a || memcmp( output1, output2, size_c ) )
1687 fprintf( stderr, "nal_escape : [FAILED] %d %d\n", size_c, size_a );
1692 for( int j = 0; j < size; j++ )
1694 call_c2( bs_c.nal_escape, output1, input, input+size );
1695 call_a2( bs_a.nal_escape, output2, input, input+size );
1700 report( "nal escape:" );
1705 static int check_all_funcs( int cpu_ref, int cpu_new )
1707 return check_pixel( cpu_ref, cpu_new )
1708 + check_dct( cpu_ref, cpu_new )
1709 + check_mc( cpu_ref, cpu_new )
1710 + check_intra( cpu_ref, cpu_new )
1711 + check_deblock( cpu_ref, cpu_new )
1712 + check_quant( cpu_ref, cpu_new )
1713 + check_cabac( cpu_ref, cpu_new )
1714 + check_bitstream( cpu_ref, cpu_new );
1717 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
1719 *cpu_ref = *cpu_new;
1721 if( *cpu_new & X264_CPU_SSE2_IS_FAST )
1722 *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
1724 fprintf( stderr, "x264: %s\n", name );
1725 return check_all_funcs( *cpu_ref, *cpu_new );
1728 static int check_all_flags( void )
1731 int cpu0 = 0, cpu1 = 0;
1733 if( x264_cpu_detect() & X264_CPU_MMXEXT )
1735 ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMXEXT, "MMX" );
1736 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
1737 cpu1 &= ~X264_CPU_CACHELINE_64;
1739 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
1740 cpu1 &= ~X264_CPU_CACHELINE_32;
1742 if( x264_cpu_detect() & X264_CPU_LZCNT )
1744 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
1745 cpu1 &= ~X264_CPU_LZCNT;
1747 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
1748 cpu1 &= ~X264_CPU_SLOW_CTZ;
1750 if( x264_cpu_detect() & X264_CPU_SSE2 )
1752 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
1753 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
1754 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
1755 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSE2 FastShuffle" );
1756 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
1757 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
1758 cpu1 &= ~X264_CPU_SLOW_CTZ;
1759 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSE2 SlowAtom" );
1760 cpu1 &= ~X264_CPU_SLOW_ATOM;
1762 if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
1764 cpu1 &= ~X264_CPU_CACHELINE_64;
1765 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
1766 cpu1 &= ~X264_CPU_SSE_MISALIGN;
1768 if( x264_cpu_detect() & X264_CPU_LZCNT )
1770 cpu1 &= ~X264_CPU_CACHELINE_64;
1771 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
1772 cpu1 &= ~X264_CPU_LZCNT;
1774 if( x264_cpu_detect() & X264_CPU_SSE3 )
1775 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
1776 if( x264_cpu_detect() & X264_CPU_SSSE3 )
1778 cpu1 &= ~X264_CPU_CACHELINE_64;
1779 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
1780 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
1781 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSSE3 FastShuffle" );
1782 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
1783 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
1784 cpu1 &= ~X264_CPU_SLOW_CTZ;
1785 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
1786 cpu1 &= ~X264_CPU_SLOW_ATOM;
1788 if( x264_cpu_detect() & X264_CPU_SSE4 )
1790 cpu1 &= ~X264_CPU_CACHELINE_64;
1791 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
1794 if( x264_cpu_detect() & X264_CPU_ALTIVEC )
1796 fprintf( stderr, "x264: ALTIVEC against C\n" );
1797 ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
1800 if( x264_cpu_detect() & X264_CPU_ARMV6 )
1801 ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
1802 if( x264_cpu_detect() & X264_CPU_NEON )
1803 ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
1804 if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC )
1805 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
1810 int main(int argc, char *argv[])
1814 if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
1816 #if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM
1817 fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
1821 if( argv[1][7] == '=' )
1823 bench_pattern = argv[1]+8;
1824 bench_pattern_len = strlen(bench_pattern);
1830 int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
1831 fprintf( stderr, "x264: using random seed %u\n", seed );
1834 buf1 = x264_malloc( 0x3e00 + 16*BENCH_ALIGNS );
1837 fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
1840 #define INIT_POINTER_OFFSETS\
1841 buf2 = buf1 + 0xf00;\
1842 buf3 = buf2 + 0xf00;\
1843 buf4 = buf3 + 0x1000;\
1844 pbuf1 = (pixel*)buf1;\
1845 pbuf2 = (pixel*)buf2;\
1846 pbuf3 = (pixel*)buf3;\
1847 pbuf4 = (pixel*)buf4;
1848 INIT_POINTER_OFFSETS;
1849 for( int i = 0; i < 0x1e00; i++ )
1850 buf1[i] = rand() & 0xFF;
1851 memset( buf1+0x1e00, 0, 0x2000 );
1853 /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
1855 for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
1857 INIT_POINTER_OFFSETS;
1858 ret |= x264_stack_pagealign( check_all_flags, i*16 );
1861 fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
1864 ret = check_all_flags();
1868 fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
1871 fprintf( stderr, "x264: All tests passed Yeah :)\n" );