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__) && (defined(ARCH_X86) || defined(ARCH_X86_64))
89 asm volatile( "rdtsc" :"=a"(a) ::"edx" );
90 #elif defined(ARCH_PPC)
91 asm volatile( "mftb %0" : "=r" (a) );
92 #elif defined(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 defined(ARCH_X86) || defined(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 defined(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_predict_t predict_16x16[4+3];
242 x264_predict_t predict_8x8c[4+3];
243 x264_predict_t predict_4x4[9+3];
244 x264_predict8x8_t predict_8x8[9+3];
245 x264_predict_8x8_filter_t predict_8x8_filter;
246 ALIGNED_16( pixel edge[33] );
247 uint16_t cost_mv[32];
248 int ret = 0, ok, used_asm;
250 x264_pixel_init( 0, &pixel_c );
251 x264_pixel_init( cpu_ref, &pixel_ref );
252 x264_pixel_init( cpu_new, &pixel_asm );
253 x264_predict_16x16_init( 0, predict_16x16 );
254 x264_predict_8x8c_init( 0, predict_8x8c );
255 x264_predict_8x8_init( 0, predict_8x8, &predict_8x8_filter );
256 x264_predict_4x4_init( 0, predict_4x4 );
257 predict_8x8_filter( pbuf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
260 for( int i = 0; i < 256; i++ )
265 buf3[i] = ~(buf4[i] = -(z&1));
267 // random pattern made of maxed pixel differences, in case an intermediate value overflows
268 for( int i = 256; i < 0x1000; i++ )
269 buf3[i] = ~(buf4[i] = -(buf1[i&~0x88]&1));
271 #define TEST_PIXEL( name, align ) \
272 ok = 1, used_asm = 0; \
273 for( int i = 0; i < 7; i++ ) \
275 int res_c, res_asm; \
276 if( pixel_asm.name[i] != pixel_ref.name[i] ) \
278 set_func_name( "%s_%s", #name, pixel_names[i] ); \
280 for( int j = 0; j < 64; j++ ) \
282 res_c = call_c( pixel_c.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \
283 res_asm = call_a( pixel_asm.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \
284 if( res_c != res_asm ) \
287 fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
291 for( int j = 0; j < 0x1000 && ok; j += 256 ) \
293 res_c = pixel_c .name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
294 res_asm = pixel_asm.name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
295 if( res_c != res_asm ) \
298 fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
303 report( "pixel " #name " :" );
305 TEST_PIXEL( sad, 0 );
306 TEST_PIXEL( sad_aligned, 1 );
307 TEST_PIXEL( ssd, 1 );
308 TEST_PIXEL( satd, 0 );
309 TEST_PIXEL( sa8d, 1 );
311 #define TEST_PIXEL_X( N ) \
312 ok = 1; used_asm = 0; \
313 for( int i = 0; i < 7; i++ ) \
315 int res_c[4]={0}, res_asm[4]={0}; \
316 if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
318 set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
320 for( int j = 0; j < 64; j++ ) \
322 pixel *pix2 = pbuf2+j; \
323 res_c[0] = pixel_c.sad[i]( pbuf1, 16, pix2, 64 ); \
324 res_c[1] = pixel_c.sad[i]( pbuf1, 16, pix2+6, 64 ); \
325 res_c[2] = pixel_c.sad[i]( pbuf1, 16, pix2+1, 64 ); \
328 res_c[3] = pixel_c.sad[i]( pbuf1, 16, pix2+10, 64 ); \
329 call_a( pixel_asm.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
332 call_a( pixel_asm.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
333 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
336 fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
337 i, res_c[0], res_c[1], res_c[2], res_c[3], \
338 res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
341 call_c2( pixel_c.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
343 call_c2( pixel_c.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
347 report( "pixel sad_x"#N" :" );
352 #define TEST_PIXEL_VAR( i ) \
353 if( pixel_asm.var[i] != pixel_ref.var[i] ) \
355 set_func_name( "%s_%s", "var", pixel_names[i] ); \
357 /* abi-check wrapper can't return uint64_t, so separate it from return value check */ \
358 call_c1( pixel_c.var[i], pbuf1, 16 ); \
359 call_a1( pixel_asm.var[i], pbuf1, 16 ); \
360 uint64_t res_c = pixel_c.var[i]( pbuf1, 16 ); \
361 uint64_t res_asm = pixel_asm.var[i]( pbuf1, 16 ); \
362 if( res_c != res_asm ) \
365 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) ); \
367 call_c2( pixel_c.var[i], pbuf1, 16 ); \
368 call_a2( pixel_asm.var[i], pbuf1, 16 ); \
371 ok = 1; used_asm = 0;
372 TEST_PIXEL_VAR( PIXEL_16x16 );
373 TEST_PIXEL_VAR( PIXEL_8x8 );
374 report( "pixel var :" );
376 ok = 1; used_asm = 0;
377 if( pixel_asm.var2_8x8 != pixel_ref.var2_8x8 )
379 int res_c, res_asm, ssd_c, ssd_asm;
380 set_func_name( "var2_8x8" );
382 res_c = call_c( pixel_c.var2_8x8, pbuf1, 16, pbuf2, 16, &ssd_c );
383 res_asm = call_a( pixel_asm.var2_8x8, pbuf1, 16, pbuf2, 16, &ssd_asm );
384 if( res_c != res_asm || ssd_c != ssd_asm )
387 fprintf( stderr, "var2_8x8: %d != %d or %d != %d [FAILED]\n", res_c, res_asm, ssd_c, ssd_asm );
391 report( "pixel var2 :" );
393 ok = 1; used_asm = 0;
394 for( int i = 0; i < 4; i++ )
395 if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
397 set_func_name( "hadamard_ac_%s", pixel_names[i] );
399 for( int j = 0; j < 32; j++ )
401 pixel *pix = (j&16 ? pbuf1 : pbuf3) + (j&15)*256;
402 call_c1( pixel_c.hadamard_ac[i], pbuf1, 16 );
403 call_a1( pixel_asm.hadamard_ac[i], pbuf1, 16 );
404 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
405 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
409 fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
413 call_c2( pixel_c.hadamard_ac[i], pbuf1, 16 );
414 call_a2( pixel_asm.hadamard_ac[i], pbuf1, 16 );
416 report( "pixel hadamard_ac :" );
418 #define TEST_INTRA_MBCMP( name, pred, satd, i8x8, ... ) \
419 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
421 int res_c[3], res_asm[3]; \
422 set_func_name( #name ); \
424 memcpy( buf3, buf2, 1024 * sizeof(pixel) ); \
425 for( int i = 0; i < 3; i++ ) \
427 pred[i]( pbuf3+48, ##__VA_ARGS__ ); \
428 res_c[i] = pixel_c.satd( pbuf1+48, 16, pbuf3+48, 32 ); \
430 call_a( pixel_asm.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_asm ); \
431 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
434 fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
435 res_c[0], res_c[1], res_c[2], \
436 res_asm[0], res_asm[1], res_asm[2] ); \
440 ok = 1; used_asm = 0;
441 TEST_INTRA_MBCMP( intra_satd_x3_16x16, predict_16x16, satd[PIXEL_16x16], 0 );
442 TEST_INTRA_MBCMP( intra_satd_x3_8x8c , predict_8x8c , satd[PIXEL_8x8] , 0 );
443 TEST_INTRA_MBCMP( intra_satd_x3_4x4 , predict_4x4 , satd[PIXEL_4x4] , 0 );
444 TEST_INTRA_MBCMP( intra_sa8d_x3_8x8 , predict_8x8 , sa8d[PIXEL_8x8] , 1, edge );
445 report( "intra satd_x3 :" );
446 TEST_INTRA_MBCMP( intra_sad_x3_16x16 , predict_16x16, sad [PIXEL_16x16], 0 );
447 TEST_INTRA_MBCMP( intra_sad_x3_8x8c , predict_8x8c , sad [PIXEL_8x8] , 0 );
448 TEST_INTRA_MBCMP( intra_sad_x3_8x8 , predict_8x8 , sad [PIXEL_8x8] , 1, edge );
449 TEST_INTRA_MBCMP( intra_sad_x3_4x4 , predict_4x4 , sad [PIXEL_4x4] , 0 );
450 report( "intra sad_x3 :" );
452 if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
453 pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
456 ALIGNED_16( int sums[5][4] ) = {{0}};
459 res_c = x264_pixel_ssim_wxh( &pixel_c, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3 );
460 res_a = x264_pixel_ssim_wxh( &pixel_asm, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3 );
461 if( fabs( res_c - res_a ) > 1e-6 )
464 fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
466 set_func_name( "ssim_core" );
467 call_c2( pixel_c.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums );
468 call_a2( pixel_asm.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums );
469 set_func_name( "ssim_end" );
470 call_c2( pixel_c.ssim_end4, sums, sums, 4 );
471 call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
475 ok = 1; used_asm = 0;
476 for( int i = 0; i < 32; i++ )
478 for( int i = 0; i < 100 && ok; i++ )
479 if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
481 ALIGNED_16( uint16_t sums[72] );
482 ALIGNED_16( int dc[4] );
483 int16_t mvs_a[32], mvs_c[32];
485 int thresh = rand() & 0x3fff;
486 set_func_name( "esa_ads" );
487 for( int j = 0; j < 72; j++ )
488 sums[j] = rand() & 0x3fff;
489 for( int j = 0; j < 4; j++ )
490 dc[j] = rand() & 0x3fff;
492 mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
493 mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
494 if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
497 printf( "c%d: ", i&3 );
498 for( int j = 0; j < mvn_c; j++ )
499 printf( "%d ", mvs_c[j] );
500 printf( "\na%d: ", i&3 );
501 for( int j = 0; j < mvn_a; j++ )
502 printf( "%d ", mvs_a[j] );
506 report( "esa ads:" );
511 static int check_dct( int cpu_ref, int cpu_new )
513 x264_dct_function_t dct_c;
514 x264_dct_function_t dct_ref;
515 x264_dct_function_t dct_asm;
516 x264_quant_function_t qf;
517 int ret = 0, ok, used_asm, interlace;
518 ALIGNED_16( dctcoef dct1[16][16] );
519 ALIGNED_16( dctcoef dct2[16][16] );
520 ALIGNED_16( dctcoef dct4[16][16] );
521 ALIGNED_16( dctcoef dct8[4][64] );
522 ALIGNED_8( dctcoef dctdc[2][4] );
526 x264_dct_init( 0, &dct_c );
527 x264_dct_init( cpu_ref, &dct_ref);
528 x264_dct_init( cpu_new, &dct_asm );
530 memset( h, 0, sizeof(*h) );
531 h->pps = h->pps_array;
532 x264_param_default( &h->param );
533 h->chroma_qp_table = i_chroma_qp_table + 12;
534 h->param.analyse.i_luma_deadzone[0] = 0;
535 h->param.analyse.i_luma_deadzone[1] = 0;
536 h->param.analyse.b_transform_8x8 = 1;
537 for( int i = 0; i < 6; i++ )
538 h->pps->scaling_list[i] = x264_cqm_flat16;
540 x264_quant_init( h, 0, &qf );
542 #define TEST_DCT( name, t1, t2, size ) \
543 if( dct_asm.name != dct_ref.name ) \
545 set_func_name( #name ); \
547 call_c( dct_c.name, t1, pbuf1, pbuf2 ); \
548 call_a( dct_asm.name, t2, pbuf1, pbuf2 ); \
549 if( memcmp( t1, t2, size ) ) \
552 fprintf( stderr, #name " [FAILED]\n" ); \
555 ok = 1; used_asm = 0;
556 TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16*2 );
557 TEST_DCT( sub8x8_dct, dct1, dct2, 16*2*4 );
558 TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4*2 );
559 TEST_DCT( sub16x16_dct, dct1, dct2, 16*2*16 );
560 report( "sub_dct4 :" );
562 ok = 1; used_asm = 0;
563 TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64*2 );
564 TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*2*4 );
565 report( "sub_dct8 :" );
568 // fdct and idct are denormalized by different factors, so quant/dequant
569 // is needed to force the coefs into the right range.
570 dct_c.sub16x16_dct( dct4, pbuf1, pbuf2 );
571 dct_c.sub16x16_dct8( dct8, pbuf1, pbuf2 );
572 for( int i = 0; i < 16; i++ )
574 qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
575 qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
577 for( int i = 0; i < 4; i++ )
579 qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
580 qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
583 #define TEST_IDCT( name, src ) \
584 if( dct_asm.name != dct_ref.name ) \
586 set_func_name( #name ); \
588 memcpy( buf3, buf1, 32*32 * sizeof(pixel) ); \
589 memcpy( buf4, buf1, 32*32 * sizeof(pixel) ); \
590 memcpy( dct1, src, 512 * sizeof(pixel) ); \
591 memcpy( dct2, src, 512 * sizeof(pixel) ); \
592 call_c1( dct_c.name, pbuf3, (void*)dct1 ); \
593 call_a1( dct_asm.name, pbuf4, (void*)dct2 ); \
594 if( memcmp( buf3, buf4, 32*32 * sizeof(pixel) ) ) \
597 fprintf( stderr, #name " [FAILED]\n" ); \
599 call_c2( dct_c.name, pbuf3, (void*)dct1 ); \
600 call_a2( dct_asm.name, pbuf4, (void*)dct2 ); \
602 ok = 1; used_asm = 0;
603 TEST_IDCT( add4x4_idct, dct4 );
604 TEST_IDCT( add8x8_idct, dct4 );
605 TEST_IDCT( add8x8_idct_dc, dct4 );
606 TEST_IDCT( add16x16_idct, dct4 );
607 TEST_IDCT( add16x16_idct_dc, dct4 );
608 report( "add_idct4 :" );
610 ok = 1; used_asm = 0;
611 TEST_IDCT( add8x8_idct8, dct8 );
612 TEST_IDCT( add16x16_idct8, dct8 );
613 report( "add_idct8 :" );
616 #define TEST_DCTDC( name )\
617 ok = 1; used_asm = 0;\
618 if( dct_asm.name != dct_ref.name )\
620 set_func_name( #name );\
622 uint16_t *p = (uint16_t*)buf1;\
623 for( int i = 0; i < 16 && ok; i++ )\
625 for( int j = 0; j < 16; j++ )\
626 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? 4080 : -4080 /* max dc */\
627 : i<8 ? (*p++)&1 ? 4080 : -4080 /* max elements */\
628 : ((*p++)&0x1fff)-0x1000; /* general case */\
629 memcpy( dct2, dct1, 32 );\
630 call_c1( dct_c.name, dct1[0] );\
631 call_a1( dct_asm.name, dct2[0] );\
632 if( memcmp( dct1, dct2, 32 ) )\
635 call_c2( dct_c.name, dct1[0] );\
636 call_a2( dct_asm.name, dct2[0] );\
638 report( #name " :" );
640 TEST_DCTDC( dct4x4dc );
641 TEST_DCTDC( idct4x4dc );
644 x264_zigzag_function_t zigzag_c;
645 x264_zigzag_function_t zigzag_ref;
646 x264_zigzag_function_t zigzag_asm;
648 ALIGNED_16( dctcoef level1[64] );
649 ALIGNED_16( dctcoef level2[64] );
651 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
652 if( zigzag_asm.name != zigzag_ref.name ) \
654 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
656 memcpy(dct, buf1, size*sizeof(dctcoef)); \
657 call_c( zigzag_c.name, t1, dct ); \
658 call_a( zigzag_asm.name, t2, dct ); \
659 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
662 fprintf( stderr, #name " [FAILED]\n" ); \
666 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
667 if( zigzag_asm.name != zigzag_ref.name ) \
670 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
672 memcpy( buf3, buf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
673 memcpy( buf4, buf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
674 nz_c = call_c1( zigzag_c.name, t1, pbuf2, pbuf3 ); \
675 nz_a = call_a1( zigzag_asm.name, t2, pbuf2, pbuf4 ); \
676 if( memcmp( t1, t2, size*sizeof(dctcoef) )|| memcmp( buf3, buf4, 16*FDEC_STRIDE ) || nz_c != nz_a ) \
679 fprintf( stderr, #name " [FAILED]\n" ); \
681 call_c2( zigzag_c.name, t1, pbuf2, pbuf3 ); \
682 call_a2( zigzag_asm.name, t2, pbuf2, pbuf4 ); \
685 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
686 if( zigzag_asm.name != zigzag_ref.name ) \
689 dctcoef dc_a, dc_c; \
690 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
692 for( int i = 0; i < 2; i++ ) \
694 memcpy( buf3, buf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
695 memcpy( buf4, buf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
696 for( int j = 0; j < 4; j++ ) \
698 memcpy( pbuf3 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
699 memcpy( pbuf4 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
701 nz_c = call_c1( zigzag_c.name, t1, pbuf2, pbuf3, &dc_c ); \
702 nz_a = call_a1( zigzag_asm.name, t2, pbuf2, pbuf4, &dc_a ); \
703 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 ) \
706 fprintf( stderr, #name " [FAILED]\n" ); \
710 call_c2( zigzag_c.name, t1, pbuf2, pbuf3, &dc_c ); \
711 call_a2( zigzag_asm.name, t2, pbuf2, pbuf4, &dc_a ); \
714 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
715 if( zigzag_asm.name != zigzag_ref.name ) \
717 for( int j = 0; j < 100; j++ ) \
719 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
721 memcpy(dct, buf1, size*sizeof(dctcoef)); \
722 for( int i = 0; i < size; i++ ) \
723 dct[i] = rand()&0x1F ? 0 : dct[i]; \
724 memcpy(buf3, buf4, 10); \
725 call_c( zigzag_c.name, t1, dct, buf3 ); \
726 call_a( zigzag_asm.name, t2, dct, buf4 ); \
727 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( buf3, buf4, 10 ) ) \
735 x264_zigzag_init( 0, &zigzag_c, 0 );
736 x264_zigzag_init( cpu_ref, &zigzag_ref, 0 );
737 x264_zigzag_init( cpu_new, &zigzag_asm, 0 );
739 ok = 1; used_asm = 0;
740 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
741 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
742 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
743 TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
744 report( "zigzag_frame :" );
747 x264_zigzag_init( 0, &zigzag_c, 1 );
748 x264_zigzag_init( cpu_ref, &zigzag_ref, 1 );
749 x264_zigzag_init( cpu_new, &zigzag_asm, 1 );
751 ok = 1; used_asm = 0;
752 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
753 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
754 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
755 TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
756 report( "zigzag_field :" );
758 ok = 1; used_asm = 0;
759 TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct1[0], 64 );
760 report( "zigzag_interleave :" );
761 #undef TEST_ZIGZAG_SCAN
762 #undef TEST_ZIGZAG_SUB
767 static int check_mc( int cpu_ref, int cpu_new )
769 x264_mc_functions_t mc_c;
770 x264_mc_functions_t mc_ref;
771 x264_mc_functions_t mc_a;
772 x264_pixel_function_t pixf;
774 pixel *src = &(pbuf1)[2*64+2];
775 pixel *src2[4] = { &(pbuf1)[3*64+2], &(pbuf1)[5*64+2],
776 &(pbuf1)[7*64+2], &(pbuf1)[9*64+2] };
780 int ret = 0, ok, used_asm;
782 x264_mc_init( 0, &mc_c );
783 x264_mc_init( cpu_ref, &mc_ref );
784 x264_mc_init( cpu_new, &mc_a );
785 x264_pixel_init( 0, &pixf );
787 #define MC_TEST_LUMA( w, h ) \
788 if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
790 const x264_weight_t *weight = weight_none; \
791 set_func_name( "mc_luma_%dx%d", w, h ); \
793 memset( buf3, 0xCD, 1024 ); \
794 memset( buf4, 0xCD, 1024 ); \
795 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
796 call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h, weight ); \
797 if( memcmp( buf3, buf4, 1024 ) ) \
799 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
803 if( mc_a.get_ref != mc_ref.get_ref ) \
806 int ref_stride = 32; \
807 const x264_weight_t *weight = weight_none; \
808 set_func_name( "get_ref_%dx%d", w, h ); \
810 memset( buf3, 0xCD, 1024 ); \
811 memset( buf4, 0xCD, 1024 ); \
812 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
813 ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h, weight ); \
814 for( int i = 0; i < h; i++ ) \
815 if( memcmp( dst1+i*32, ref+i*ref_stride, w * sizeof(pixel) ) ) \
817 fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
823 #define MC_TEST_CHROMA( w, h ) \
824 if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
826 set_func_name( "mc_chroma_%dx%d", w, h ); \
828 memset( buf3, 0xCD, 1024 ); \
829 memset( buf4, 0xCD, 1024 ); \
830 call_c( mc_c.mc_chroma, dst1, 16, src, 64, dx, dy, w, h ); \
831 call_a( mc_a.mc_chroma, dst2, 16, src, 64, dx, dy, w, h ); \
832 /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \
833 for( int j = 0; j < h; j++ ) \
834 for( int i = w; i < 4; i++ ) \
835 dst2[i+j*16] = dst1[i+j*16]; \
836 if( memcmp( buf3, buf4, 1024 ) ) \
838 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
842 ok = 1; used_asm = 0;
843 for( int dy = -8; dy < 8; dy++ )
844 for( int dx = -128; dx < 128; dx++ )
846 if( rand()&15 ) continue; // running all of them is too slow
847 MC_TEST_LUMA( 20, 18 );
848 MC_TEST_LUMA( 16, 16 );
849 MC_TEST_LUMA( 16, 8 );
850 MC_TEST_LUMA( 12, 10 );
851 MC_TEST_LUMA( 8, 16 );
852 MC_TEST_LUMA( 8, 8 );
853 MC_TEST_LUMA( 8, 4 );
854 MC_TEST_LUMA( 4, 8 );
855 MC_TEST_LUMA( 4, 4 );
857 report( "mc luma :" );
859 ok = 1; used_asm = 0;
860 for( int dy = -1; dy < 9; dy++ )
861 for( int dx = -128; dx < 128; dx++ )
863 if( rand()&15 ) continue;
864 MC_TEST_CHROMA( 8, 8 );
865 MC_TEST_CHROMA( 8, 4 );
866 MC_TEST_CHROMA( 4, 8 );
867 MC_TEST_CHROMA( 4, 4 );
868 MC_TEST_CHROMA( 4, 2 );
869 MC_TEST_CHROMA( 2, 4 );
870 MC_TEST_CHROMA( 2, 2 );
872 report( "mc chroma :" );
874 #undef MC_TEST_CHROMA
876 #define MC_TEST_AVG( name, weight ) \
878 ok = 1, used_asm = 0; \
879 for( int i = 0; i < 10; i++ ) \
881 memcpy( buf3, pbuf1+320, 320 * sizeof(pixel) ); \
882 memcpy( buf4, pbuf1+320, 320 * sizeof(pixel) ); \
883 if( mc_a.name[i] != mc_ref.name[i] ) \
885 set_func_name( "%s_%s", #name, pixel_names[i] ); \
887 call_c1( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
888 call_a1( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
889 if( memcmp( buf3, buf4, 320 * sizeof(pixel) ) ) \
892 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
894 call_c2( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
895 call_a2( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
900 for( int w = -63; w <= 127 && ok; w++ )
901 MC_TEST_AVG( avg, w );
902 report( "mc wpredb :" );
904 #define MC_TEST_WEIGHT( name, weight, aligned ) \
905 int align_off = (aligned ? 0 : rand()%16); \
906 ok = 1, used_asm = 0; \
907 for( int i = 1; i <= 5; i++ ) \
909 ALIGNED_16( pixel buffC[640] ); \
910 ALIGNED_16( pixel buffA[640] ); \
911 int j = X264_MAX( i*4, 2 ); \
912 memset( buffC, 0, 640 * sizeof(pixel) ); \
913 memset( buffA, 0, 640 * sizeof(pixel) ); \
916 /* w12 is the same as w16 in some cases */ \
917 if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
919 if( mc_a.name[i] != mc_ref.name[i] ) \
921 set_func_name( "%s_w%d", #name, j ); \
923 call_c1( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
924 mc_a.weight_cache(&ha, &weight); \
925 call_a1( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
926 for( int k = 0; k < 16; k++ ) \
927 if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \
930 fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
933 call_c2( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
934 call_a2( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
938 ok = 1; used_asm = 0;
941 for( int s = 0; s <= 127 && ok; s++ )
943 for( int o = -128; o <= 127 && ok; o++ )
945 if( rand() & 2047 ) continue;
946 for( int d = 0; d <= 7 && ok; d++ )
950 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
951 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
956 report( "mc weight :" );
958 ok = 1; used_asm = 0;
959 for( int o = 0; o <= 127 && ok; o++ )
962 if( rand() & 15 ) continue;
963 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
964 MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
966 report( "mc offsetadd :" );
967 ok = 1; used_asm = 0;
968 for( int o = -128; o < 0 && ok; o++ )
971 if( rand() & 15 ) continue;
972 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
973 MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
975 report( "mc offsetsub :" );
977 if( mc_a.hpel_filter != mc_ref.hpel_filter )
979 pixel *srchpel = pbuf1+8+2*64;
980 pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
981 pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
982 void *tmp = pbuf3+49*64;
983 set_func_name( "hpel_filter" );
984 ok = 1; used_asm = 1;
985 memset( buf3, 0, 4096 * sizeof(pixel) );
986 memset( buf4, 0, 4096 * sizeof(pixel) );
987 call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, 64, 48, 10, tmp );
988 call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, 64, 48, 10, tmp );
989 for( int i = 0; i < 3; i++ )
990 for( int j = 0; j < 10; j++ )
991 //FIXME ideally the first pixels would match too, but they aren't actually used
992 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
995 fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
996 for( int k = 0; k < 48; k++ )
997 printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
999 for( int k = 0; k < 48; k++ )
1000 printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
1004 report( "hpel filter :" );
1007 if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
1009 pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
1010 pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1011 set_func_name( "lowres_init" );
1012 ok = 1; used_asm = 1;
1013 for( int w = 40; w <= 48; w += 8 )
1015 int stride = (w+8)&~15;
1016 call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
1017 call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
1018 for( int i = 0; i < 16; i++ )
1020 for( int j = 0; j < 4; j++ )
1021 if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w * sizeof(pixel) ) )
1024 fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1025 for( int k = 0; k < w; k++ )
1026 printf( "%d ", dstc[j][k+i*stride] );
1028 for( int k = 0; k < w; k++ )
1029 printf( "%d ", dsta[j][k+i*stride] );
1035 report( "lowres init :" );
1038 #define INTEGRAL_INIT( name, size, ... )\
1039 if( mc_a.name != mc_ref.name )\
1042 set_func_name( #name );\
1044 memcpy( buf3, buf1, size*2*stride * sizeof(pixel) );\
1045 memcpy( buf4, buf1, size*2*stride * sizeof(pixel) );\
1046 uint16_t *sum = (uint16_t*)buf3;\
1047 call_c1( mc_c.name, __VA_ARGS__ );\
1048 sum = (uint16_t*)buf4;\
1049 call_a1( mc_a.name, __VA_ARGS__ );\
1050 if( memcmp( buf3, buf4, (stride-8)*2 * sizeof(pixel) )\
1051 || (size>9 && memcmp( pbuf3+18*stride, pbuf4+18*stride, (stride-8)*2 * sizeof(pixel) )))\
1053 call_c2( mc_c.name, __VA_ARGS__ );\
1054 call_a2( mc_a.name, __VA_ARGS__ );\
1056 ok = 1; used_asm = 0;
1057 INTEGRAL_INIT( integral_init4h, 2, sum+stride, pbuf2, stride );
1058 INTEGRAL_INIT( integral_init8h, 2, sum+stride, pbuf2, stride );
1059 INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
1060 INTEGRAL_INIT( integral_init8v, 9, sum, stride );
1061 report( "integral init :" );
1063 if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1065 ok = 1; used_asm = 1;
1066 set_func_name( "mbtree_propagate" );
1067 int *dsta = (int*)buf3;
1068 int *dstc = dsta+400;
1069 uint16_t *prop = (uint16_t*)buf1;
1070 uint16_t *intra = (uint16_t*)buf4;
1071 uint16_t *inter = intra+400;
1072 uint16_t *qscale = inter+400;
1073 uint16_t *rnd = (uint16_t*)buf2;
1075 for( int i = 0; i < 400; i++ )
1077 intra[i] = *rnd++ & 0x7fff;
1078 intra[i] += !intra[i];
1079 inter[i] = *rnd++ & 0x7fff;
1080 qscale[i] = *rnd++ & 0x7fff;
1082 call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, 400 );
1083 call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, 400 );
1084 // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1086 for( int i = 0; i < 400; i++ )
1087 ok &= abs( dstc[i]-dsta[i] ) <= (abs( dstc[i])>512 ) || fabs( (double)dstc[i]/dsta[i]-1 ) < 1e-6;
1088 report( "mbtree propagate :" );
1094 static int check_deblock( int cpu_ref, int cpu_new )
1096 x264_deblock_function_t db_c;
1097 x264_deblock_function_t db_ref;
1098 x264_deblock_function_t db_a;
1099 int ret = 0, ok = 1, used_asm = 0;
1100 int alphas[36], betas[36];
1103 x264_deblock_init( 0, &db_c );
1104 x264_deblock_init( cpu_ref, &db_ref );
1105 x264_deblock_init( cpu_new, &db_a );
1107 /* not exactly the real values of a,b,tc but close enough */
1108 for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1112 tcs[i][0] = tcs[i][3] = (c+6)/10;
1113 tcs[i][1] = (c+7)/15;
1114 tcs[i][2] = (c+9)/20;
1119 #define TEST_DEBLOCK( name, align, ... ) \
1120 for( int i = 0; i < 36; i++ ) \
1122 int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1123 for( int j = 0; j < 1024; j++ ) \
1124 /* two distributions of random to excersize different failure modes */ \
1125 buf3[j] = rand() & (i&1 ? 0xf : 0xff ); \
1126 memcpy( buf4, buf3, 1024 * sizeof(pixel) ); \
1127 if( db_a.name != db_ref.name ) \
1129 set_func_name( #name ); \
1131 call_c1( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1132 call_a1( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1133 if( memcmp( buf3, buf4, 1024 * sizeof(pixel) ) ) \
1136 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1139 call_c2( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1140 call_a2( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1144 TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1145 TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1146 TEST_DEBLOCK( deblock_chroma[0], 0, tcs[i] );
1147 TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1148 TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1149 TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1150 TEST_DEBLOCK( deblock_chroma_intra[0], 0 );
1151 TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1153 if( db_a.deblock_strength != db_ref.deblock_strength )
1155 for( int i = 0; i < 100; i++ )
1157 ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1158 ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1159 ALIGNED_ARRAY_16( int16_t, mv, [2][X264_SCAN8_LUMA_SIZE][2] );
1160 ALIGNED_ARRAY_16( uint8_t, bs, [2][2][4][4] );
1161 for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1162 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1163 for( int j = 0; j < 2; j++ )
1164 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1166 ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1167 for( int l = 0; l < 2; l++ )
1168 mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1170 set_func_name( "deblock_strength" );
1171 call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1172 call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1173 if( memcmp( bs[0], bs[1], sizeof(bs[0]) ) )
1176 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1177 for( int j = 0; j < 2; j++ )
1179 for( int k = 0; k < 2; k++ )
1180 for( int l = 0; l < 4; l++ )
1182 for( int m = 0; m < 4; m++ )
1183 printf("%d ",bs[j][k][l][m]);
1193 report( "deblock :" );
1198 static int check_quant( int cpu_ref, int cpu_new )
1200 x264_quant_function_t qf_c;
1201 x264_quant_function_t qf_ref;
1202 x264_quant_function_t qf_a;
1203 ALIGNED_16( dctcoef dct1[64] );
1204 ALIGNED_16( dctcoef dct2[64] );
1205 ALIGNED_16( uint8_t cqm_buf[64] );
1206 int ret = 0, ok, used_asm;
1207 int oks[2] = {1,1}, used_asms[2] = {0,0};
1210 memset( h, 0, sizeof(*h) );
1211 h->pps = h->pps_array;
1212 x264_param_default( &h->param );
1213 h->chroma_qp_table = i_chroma_qp_table + 12;
1214 h->param.rc.i_qp_min = 26;
1215 h->param.analyse.b_transform_8x8 = 1;
1217 for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1221 for( int i = 0; i < 6; i++ )
1222 h->pps->scaling_list[i] = x264_cqm_flat16;
1223 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1225 else if( i_cqm == 1 )
1227 for( int i = 0; i < 6; i++ )
1228 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1229 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1234 for( int i = 0; i < 64; i++ )
1235 cqm_buf[i] = 10 + rand() % 246;
1237 for( int i = 0; i < 64; i++ )
1239 for( int i = 0; i < 6; i++ )
1240 h->pps->scaling_list[i] = cqm_buf;
1241 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1245 x264_quant_init( h, 0, &qf_c );
1246 x264_quant_init( h, cpu_ref, &qf_ref );
1247 x264_quant_init( h, cpu_new, &qf_a );
1249 #define INIT_QUANT8(j) \
1251 static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1252 for( int i = 0; i < 64; i++ ) \
1254 unsigned int scale = (255*scale1d[i>>3]*scale1d[i&7])/16; \
1255 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1259 #define INIT_QUANT4(j) \
1261 static const int scale1d[4] = {4,6,4,6}; \
1262 for( int i = 0; i < 16; i++ ) \
1264 unsigned int scale = 255*scale1d[i>>2]*scale1d[i&3]; \
1265 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1269 #define TEST_QUANT_DC( name, cqm ) \
1270 if( qf_a.name != qf_ref.name ) \
1272 set_func_name( #name ); \
1274 for( int qp = 51; qp > 0; qp-- ) \
1276 for( int j = 0; j < 2; j++ ) \
1278 int result_c, result_a; \
1279 for( int i = 0; i < 16; i++ ) \
1280 dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1281 result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1282 result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1283 if( memcmp( dct1, dct2, 16*2 ) || result_c != result_a ) \
1286 fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1289 call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1290 call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1295 #define TEST_QUANT( qname, block, w ) \
1296 if( qf_a.qname != qf_ref.qname ) \
1298 set_func_name( #qname ); \
1300 for( int qp = 51; qp > 0; qp-- ) \
1302 for( int j = 0; j < 2; j++ ) \
1305 int result_c = call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1306 int result_a = call_a1( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1307 if( memcmp( dct1, dct2, w*w*2 ) || result_c != result_a ) \
1310 fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1313 call_c2( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1314 call_a2( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1319 TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1320 TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1321 TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1322 TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1323 TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1324 TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1326 #define TEST_DEQUANT( qname, dqname, block, w ) \
1327 if( qf_a.dqname != qf_ref.dqname ) \
1329 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1331 for( int qp = 51; qp > 0; qp-- ) \
1334 call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1335 memcpy( dct2, dct1, w*w*2 ); \
1336 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1337 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1338 if( memcmp( dct1, dct2, w*w*2 ) ) \
1341 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1344 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1345 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1349 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1350 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1351 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1352 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1354 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1355 if( qf_a.dqname != qf_ref.dqname ) \
1357 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1359 for( int qp = 51; qp > 0; qp-- ) \
1361 for( int i = 0; i < 16; i++ ) \
1363 call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1364 memcpy( dct2, dct1, w*w*2 ); \
1365 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1366 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1367 if( memcmp( dct1, dct2, w*w*2 ) ) \
1370 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1372 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1373 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1377 TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1379 x264_cqm_delete( h );
1382 ok = oks[0]; used_asm = used_asms[0];
1383 report( "quant :" );
1385 ok = oks[1]; used_asm = used_asms[1];
1386 report( "dequant :" );
1388 ok = 1; used_asm = 0;
1389 if( qf_a.denoise_dct != qf_ref.denoise_dct )
1392 for( int size = 16; size <= 64; size += 48 )
1394 set_func_name( "denoise_dct" );
1395 memcpy( dct1, buf1, size*2 );
1396 memcpy( dct2, buf1, size*2 );
1397 memcpy( buf3+256, buf3, 256 );
1398 call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1399 call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1400 if( memcmp( dct1, dct2, size*2 ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1402 call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1403 call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1406 report( "denoise dct :" );
1408 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1409 if( qf_a.decname != qf_ref.decname ) \
1411 set_func_name( #decname ); \
1413 for( int i = 0; i < 100; i++ ) \
1415 for( int idx = 0; idx < w*w; idx++ ) \
1416 dct1[idx] = !(rand()&3) + (!(rand()&15))*(rand()&3); \
1419 int result_c = call_c( qf_c.decname, dct1 ); \
1420 int result_a = call_a( qf_a.decname, dct1 ); \
1421 if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1424 fprintf( stderr, #decname ": [FAILED]\n" ); \
1430 ok = 1; used_asm = 0;
1431 TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1432 TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1433 TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1434 report( "decimate_score :" );
1436 #define TEST_LAST( last, lastname, w, ac ) \
1437 if( qf_a.last != qf_ref.last ) \
1439 set_func_name( #lastname ); \
1441 for( int i = 0; i < 100; i++ ) \
1444 int max = rand() & (w*w-1); \
1445 memset( dct1, 0, w*w*2 ); \
1446 for( int idx = ac; idx < max; idx++ ) \
1447 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1450 int result_c = call_c( qf_c.last, dct1+ac ); \
1451 int result_a = call_a( qf_a.last, dct1+ac ); \
1452 if( result_c != result_a ) \
1455 fprintf( stderr, #lastname ": [FAILED]\n" ); \
1461 ok = 1; used_asm = 0;
1462 TEST_LAST( coeff_last[DCT_CHROMA_DC], coeff_last4, 2, 0 );
1463 TEST_LAST( coeff_last[ DCT_LUMA_AC], coeff_last15, 4, 1 );
1464 TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 4, 0 );
1465 TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 8, 0 );
1466 report( "coeff_last :" );
1468 #define TEST_LEVELRUN( lastname, name, w, ac ) \
1469 if( qf_a.lastname != qf_ref.lastname ) \
1471 set_func_name( #name ); \
1473 for( int i = 0; i < 100; i++ ) \
1475 x264_run_level_t runlevel_c, runlevel_a; \
1477 int max = rand() & (w*w-1); \
1478 memset( dct1, 0, w*w*2 ); \
1479 memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
1480 memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
1481 for( int idx = ac; idx < max; idx++ ) \
1482 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1485 int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
1486 int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
1487 if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
1488 memcmp(runlevel_c.level, runlevel_a.level, sizeof(int16_t)*result_c) || \
1489 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
1492 fprintf( stderr, #name ": [FAILED]\n" ); \
1498 ok = 1; used_asm = 0;
1499 TEST_LEVELRUN( coeff_level_run[DCT_CHROMA_DC], coeff_level_run4, 2, 0 );
1500 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_AC], coeff_level_run15, 4, 1 );
1501 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 4, 0 );
1502 report( "coeff_level_run :" );
1507 static int check_intra( int cpu_ref, int cpu_new )
1509 int ret = 0, ok = 1, used_asm = 0;
1510 ALIGNED_16( pixel edge[33] );
1511 ALIGNED_16( pixel edge2[33] );
1514 x264_predict_t predict_16x16[4+3];
1515 x264_predict_t predict_8x8c[4+3];
1516 x264_predict8x8_t predict_8x8[9+3];
1517 x264_predict_t predict_4x4[9+3];
1518 x264_predict_8x8_filter_t predict_8x8_filter;
1519 } ip_c, ip_ref, ip_a;
1521 x264_predict_16x16_init( 0, ip_c.predict_16x16 );
1522 x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
1523 x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
1524 x264_predict_4x4_init( 0, ip_c.predict_4x4 );
1526 x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
1527 x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
1528 x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
1529 x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
1531 x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
1532 x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
1533 x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
1534 x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
1536 ip_c.predict_8x8_filter( pbuf1+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
1538 #define INTRA_TEST( name, dir, w, ... )\
1539 if( ip_a.name[dir] != ip_ref.name[dir] )\
1541 set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
1543 memcpy( buf3, buf1, 32*20 * sizeof(pixel) );\
1544 memcpy( buf4, buf1, 32*20 * sizeof(pixel) );\
1545 call_c( ip_c.name[dir], pbuf3+48, ##__VA_ARGS__ );\
1546 call_a( ip_a.name[dir], pbuf4+48, ##__VA_ARGS__ );\
1547 if( memcmp( buf3, buf4, 32*20 * sizeof(pixel) ) )\
1549 fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\
1551 for( int k = -1; k < 16; k++ )\
1552 printf( "%2x ", edge[16+k] );\
1554 for( int j = 0; j < w; j++ )\
1556 printf( "%2x ", edge[14-j] );\
1557 for( int k = 0; k < w; k++ )\
1558 printf( "%2x ", buf4[48+k+j*32] );\
1562 for( int j = 0; j < w; j++ )\
1565 for( int k = 0; k < w; k++ )\
1566 printf( "%2x ", buf3[48+k+j*32] );\
1572 for( int i = 0; i < 12; i++ )
1573 INTRA_TEST( predict_4x4, i, 4 );
1574 for( int i = 0; i < 7; i++ )
1575 INTRA_TEST( predict_8x8c, i, 8 );
1576 for( int i = 0; i < 7; i++ )
1577 INTRA_TEST( predict_16x16, i, 16 );
1578 for( int i = 0; i < 12; i++ )
1579 INTRA_TEST( predict_8x8, i, 8, edge );
1581 set_func_name("intra_predict_8x8_filter");
1582 if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
1585 for( int i = 0; i < 32; i++ )
1587 memcpy( edge2, edge, 33 * sizeof(pixel) );
1588 call_c(ip_c.predict_8x8_filter, pbuf1+48, edge, (i&24)>>1, i&7);
1589 call_a(ip_a.predict_8x8_filter, pbuf1+48, edge2, (i&24)>>1, i&7);
1590 if( memcmp( edge, edge2, 33 * sizeof(pixel) ) )
1592 fprintf( stderr, "predict_8x8_filter : [FAILED] %d %d\n", (i&24)>>1, i&7);
1598 report( "intra pred :" );
1602 #define DECL_CABAC(cpu) \
1603 static void run_cabac_decision_##cpu( uint8_t *dst )\
1606 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1607 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1608 for( int i = 0; i < 0x1000; i++ )\
1609 x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
1611 static void run_cabac_bypass_##cpu( uint8_t *dst )\
1614 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1615 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1616 for( int i = 0; i < 0x1000; i++ )\
1617 x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
1619 static void run_cabac_terminal_##cpu( uint8_t *dst )\
1622 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1623 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1624 for( int i = 0; i < 0x1000; i++ )\
1625 x264_cabac_encode_terminal_##cpu( &cb );\
1631 #define run_cabac_decision_asm run_cabac_decision_c
1632 #define run_cabac_bypass_asm run_cabac_bypass_c
1633 #define run_cabac_terminal_asm run_cabac_terminal_c
1636 static int check_cabac( int cpu_ref, int cpu_new )
1638 int ret = 0, ok, used_asm = 1;
1639 if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
1642 set_func_name( "cabac_encode_decision" );
1643 memcpy( buf4, buf3, 0x1000 );
1644 call_c( run_cabac_decision_c, buf3 );
1645 call_a( run_cabac_decision_asm, buf4 );
1646 ok = !memcmp( buf3, buf4, 0x1000 );
1647 report( "cabac decision:" );
1649 set_func_name( "cabac_encode_bypass" );
1650 memcpy( buf4, buf3, 0x1000 );
1651 call_c( run_cabac_bypass_c, buf3 );
1652 call_a( run_cabac_bypass_asm, buf4 );
1653 ok = !memcmp( buf3, buf4, 0x1000 );
1654 report( "cabac bypass:" );
1656 set_func_name( "cabac_encode_terminal" );
1657 memcpy( buf4, buf3, 0x1000 );
1658 call_c( run_cabac_terminal_c, buf3 );
1659 call_a( run_cabac_terminal_asm, buf4 );
1660 ok = !memcmp( buf3, buf4, 0x1000 );
1661 report( "cabac terminal:" );
1666 static int check_bitstream( int cpu_ref, int cpu_new )
1668 x264_bitstream_function_t bs_c;
1669 x264_bitstream_function_t bs_ref;
1670 x264_bitstream_function_t bs_a;
1672 int ret = 0, ok = 1, used_asm = 0;
1674 x264_bitstream_init( 0, &bs_c );
1675 x264_bitstream_init( cpu_ref, &bs_ref );
1676 x264_bitstream_init( cpu_new, &bs_a );
1677 if( bs_a.nal_escape != bs_ref.nal_escape )
1680 uint8_t *input = malloc(size+100);
1681 uint8_t *output1 = malloc(size*2);
1682 uint8_t *output2 = malloc(size*2);
1684 set_func_name( "nal_escape" );
1685 for( int i = 0; i < 100; i++ )
1687 /* Test corner-case sizes */
1688 int test_size = i < 10 ? i+1 : rand() & 0x3fff;
1689 /* Test 8 different probability distributions of zeros */
1690 for( int j = 0; j < test_size; j++ )
1691 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
1692 uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
1693 uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
1694 int size_c = end_c-output1;
1695 int size_a = end_a-output2;
1696 if( size_c != size_a || memcmp( output1, output2, size_c ) )
1698 fprintf( stderr, "nal_escape : [FAILED] %d %d\n", size_c, size_a );
1703 for( int j = 0; j < size; j++ )
1705 call_c2( bs_c.nal_escape, output1, input, input+size );
1706 call_a2( bs_a.nal_escape, output2, input, input+size );
1711 report( "nal escape:" );
1716 static int check_all_funcs( int cpu_ref, int cpu_new )
1718 return check_pixel( cpu_ref, cpu_new )
1719 + check_dct( cpu_ref, cpu_new )
1720 + check_mc( cpu_ref, cpu_new )
1721 + check_intra( cpu_ref, cpu_new )
1722 + check_deblock( cpu_ref, cpu_new )
1723 + check_quant( cpu_ref, cpu_new )
1724 + check_cabac( cpu_ref, cpu_new )
1725 + check_bitstream( cpu_ref, cpu_new );
1728 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
1730 *cpu_ref = *cpu_new;
1732 if( *cpu_new & X264_CPU_SSE2_IS_FAST )
1733 *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
1735 fprintf( stderr, "x264: %s\n", name );
1736 return check_all_funcs( *cpu_ref, *cpu_new );
1739 static int check_all_flags( void )
1742 int cpu0 = 0, cpu1 = 0;
1744 if( x264_cpu_detect() & X264_CPU_MMXEXT )
1746 ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMXEXT, "MMX" );
1747 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
1748 cpu1 &= ~X264_CPU_CACHELINE_64;
1750 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
1751 cpu1 &= ~X264_CPU_CACHELINE_32;
1753 if( x264_cpu_detect() & X264_CPU_LZCNT )
1755 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
1756 cpu1 &= ~X264_CPU_LZCNT;
1758 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
1759 cpu1 &= ~X264_CPU_SLOW_CTZ;
1761 if( x264_cpu_detect() & X264_CPU_SSE2 )
1763 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
1764 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
1765 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
1766 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSE2 FastShuffle" );
1767 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
1768 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
1769 cpu1 &= ~X264_CPU_SLOW_CTZ;
1770 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSE2 SlowAtom" );
1771 cpu1 &= ~X264_CPU_SLOW_ATOM;
1773 if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
1775 cpu1 &= ~X264_CPU_CACHELINE_64;
1776 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
1777 cpu1 &= ~X264_CPU_SSE_MISALIGN;
1779 if( x264_cpu_detect() & X264_CPU_LZCNT )
1781 cpu1 &= ~X264_CPU_CACHELINE_64;
1782 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
1783 cpu1 &= ~X264_CPU_LZCNT;
1785 if( x264_cpu_detect() & X264_CPU_SSE3 )
1786 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
1787 if( x264_cpu_detect() & X264_CPU_SSSE3 )
1789 cpu1 &= ~X264_CPU_CACHELINE_64;
1790 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
1791 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
1792 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSSE3 FastShuffle" );
1793 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
1794 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
1795 cpu1 &= ~X264_CPU_SLOW_CTZ;
1796 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
1797 cpu1 &= ~X264_CPU_SLOW_ATOM;
1799 if( x264_cpu_detect() & X264_CPU_SSE4 )
1801 cpu1 &= ~X264_CPU_CACHELINE_64;
1802 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
1804 #elif defined(ARCH_PPC)
1805 if( x264_cpu_detect() & X264_CPU_ALTIVEC )
1807 fprintf( stderr, "x264: ALTIVEC against C\n" );
1808 ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
1810 #elif defined(ARCH_ARM)
1811 if( x264_cpu_detect() & X264_CPU_ARMV6 )
1812 ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
1813 if( x264_cpu_detect() & X264_CPU_NEON )
1814 ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
1815 if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC )
1816 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
1821 int main(int argc, char *argv[])
1825 if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
1827 #if !defined(ARCH_X86) && !defined(ARCH_X86_64) && !defined(ARCH_PPC) && !defined(ARCH_ARM)
1828 fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
1832 if( argv[1][7] == '=' )
1834 bench_pattern = argv[1]+8;
1835 bench_pattern_len = strlen(bench_pattern);
1841 int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
1842 fprintf( stderr, "x264: using random seed %u\n", seed );
1845 buf1 = x264_malloc( 0x3e00 + 16*BENCH_ALIGNS );
1848 fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
1851 #define INIT_POINTER_OFFSETS\
1852 buf2 = buf1 + 0xf00;\
1853 buf3 = buf2 + 0xf00;\
1854 buf4 = buf3 + 0x1000;\
1855 pbuf1 = (pixel*)buf1;\
1856 pbuf2 = (pixel*)buf2;\
1857 pbuf3 = (pixel*)buf3;\
1858 pbuf4 = (pixel*)buf4;
1859 INIT_POINTER_OFFSETS;
1860 for( int i = 0; i < 0x1e00; i++ )
1861 buf1[i] = rand() & 0xFF;
1862 memset( buf1+0x1e00, 0, 0x2000 );
1864 /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
1866 for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
1868 INIT_POINTER_OFFSETS;
1869 ret |= x264_stack_pagealign( check_all_flags, i*16 );
1872 fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
1875 ret = check_all_flags();
1879 fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
1882 fprintf( stderr, "x264: All tests passed Yeah :)\n" );