1 /*****************************************************************************
2 * checkasm.c: assembly check tool
3 *****************************************************************************
4 * Copyright (C) 2003-2012 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.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
29 #include "common/common.h"
30 #include "common/cpu.h"
32 // GCC doesn't align stack variables on ARM, so use .bss
35 #define ALIGNED_16( var ) DECLARE_ALIGNED( static var, 16 )
38 /* buf1, buf2: initialised to random data and shouldn't write into them */
40 /* buf3, buf4: used to store output */
42 /* pbuf1, pbuf2: initialised to random pixel data and shouldn't write into them. */
44 /* pbuf3, pbuf4: point to buf3, buf4, just for type convenience */
49 #define report( name ) { \
50 if( used_asm && !quiet ) \
51 fprintf( stderr, " - %-21s [%s]\n", name, ok ? "OK" : "FAILED" ); \
55 #define BENCH_RUNS 100 // tradeoff between accuracy and speed
56 #define BENCH_ALIGNS 16 // number of stack+heap data alignments (another accuracy vs speed tradeoff)
57 #define MAX_FUNCS 1000 // just has to be big enough to hold all the existing functions
58 #define MAX_CPUS 30 // number of different combinations of cpu flags
62 void *pointer; // just for detecting duplicates
71 bench_t vers[MAX_CPUS];
75 int bench_pattern_len = 0;
76 const char *bench_pattern = "";
78 static bench_func_t benchs[MAX_FUNCS];
80 static const char *pixel_names[12] = { "16x16", "16x8", "8x16", "8x8", "8x4", "4x8", "4x4", "4x16", "4x2", "2x8", "2x4", "2x2" };
81 static const char *intra_predict_16x16_names[7] = { "v", "h", "dc", "p", "dcl", "dct", "dc8" };
82 static const char *intra_predict_8x8c_names[7] = { "dc", "h", "v", "p", "dcl", "dct", "dc8" };
83 static const char *intra_predict_4x4_names[12] = { "v", "h", "dc", "ddl", "ddr", "vr", "hd", "vl", "hu", "dcl", "dct", "dc8" };
84 static const char **intra_predict_8x8_names = intra_predict_4x4_names;
85 static const char **intra_predict_8x16c_names = intra_predict_8x8c_names;
87 #define set_func_name(...) snprintf( func_name, sizeof(func_name), __VA_ARGS__ )
89 static inline uint32_t read_time(void)
92 #if HAVE_X86_INLINE_ASM
93 asm volatile( "rdtsc" :"=a"(a) ::"edx" );
95 asm volatile( "mftb %0" : "=r" (a) );
96 #elif ARCH_ARM // ARMv7 only
97 asm volatile( "mrc p15, 0, %0, c9, c13, 0" : "=r"(a) );
102 static bench_t* get_bench( const char *name, int cpu )
105 for( i = 0; benchs[i].name && strcmp(name, benchs[i].name); i++ )
106 assert( i < MAX_FUNCS );
107 if( !benchs[i].name )
108 benchs[i].name = strdup( name );
110 return &benchs[i].vers[0];
111 for( j = 1; benchs[i].vers[j].cpu && benchs[i].vers[j].cpu != cpu; j++ )
112 assert( j < MAX_CPUS );
113 benchs[i].vers[j].cpu = cpu;
114 return &benchs[i].vers[j];
117 static int cmp_nop( const void *a, const void *b )
119 return *(uint16_t*)a - *(uint16_t*)b;
122 static int cmp_bench( const void *a, const void *b )
124 // asciibetical sort except preserving numbers
125 const char *sa = ((bench_func_t*)a)->name;
126 const char *sb = ((bench_func_t*)b)->name;
131 if( isdigit( *sa ) && isdigit( *sb ) && isdigit( sa[1] ) != isdigit( sb[1] ) )
132 return isdigit( sa[1] ) - isdigit( sb[1] );
138 static void print_bench(void)
140 uint16_t nops[10000] = {0};
141 int nfuncs, nop_time=0;
143 for( int i = 0; i < 10000; i++ )
146 nops[i] = read_time() - t;
148 qsort( nops, 10000, sizeof(uint16_t), cmp_nop );
149 for( int i = 500; i < 9500; i++ )
152 printf( "nop: %d\n", nop_time );
154 for( nfuncs = 0; nfuncs < MAX_FUNCS && benchs[nfuncs].name; nfuncs++ );
155 qsort( benchs, nfuncs, sizeof(bench_func_t), cmp_bench );
156 for( int i = 0; i < nfuncs; i++ )
157 for( int j = 0; j < MAX_CPUS && (!j || benchs[i].vers[j].cpu); j++ )
160 bench_t *b = &benchs[i].vers[j];
163 for( k = 0; k < j && benchs[i].vers[k].pointer != b->pointer; k++ );
166 printf( "%s_%s%s: %"PRId64"\n", benchs[i].name,
167 b->cpu&X264_CPU_FMA4 ? "fma4" :
168 b->cpu&X264_CPU_XOP ? "xop" :
169 b->cpu&X264_CPU_AVX ? "avx" :
170 b->cpu&X264_CPU_SSE4 ? "sse4" :
171 b->cpu&X264_CPU_SSSE3 ? "ssse3" :
172 b->cpu&X264_CPU_SSE3 ? "sse3" :
173 /* print sse2slow only if there's also a sse2fast version of the same func */
174 b->cpu&X264_CPU_SSE2_IS_SLOW && j<MAX_CPUS-1 && b[1].cpu&X264_CPU_SSE2_IS_FAST && !(b[1].cpu&X264_CPU_SSE3) ? "sse2slow" :
175 b->cpu&X264_CPU_SSE2 ? "sse2" :
176 b->cpu&X264_CPU_MMX ? "mmx" :
177 b->cpu&X264_CPU_ALTIVEC ? "altivec" :
178 b->cpu&X264_CPU_NEON ? "neon" :
179 b->cpu&X264_CPU_ARMV6 ? "armv6" : "c",
180 b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
181 b->cpu&X264_CPU_CACHELINE_64 ? "_c64" :
182 b->cpu&X264_CPU_SHUFFLE_IS_FAST && !(b->cpu&X264_CPU_SSE4) ? "_fastshuffle" :
183 b->cpu&X264_CPU_SSE_MISALIGN ? "_misalign" :
184 b->cpu&X264_CPU_LZCNT ? "_lzcnt" :
185 b->cpu&X264_CPU_FAST_NEON_MRC ? "_fast_mrc" :
186 b->cpu&X264_CPU_SLOW_CTZ ? "_slow_ctz" :
187 b->cpu&X264_CPU_SLOW_ATOM ? "_slow_atom" : "",
188 ((int64_t)10*b->cycles/b->den - nop_time)/4 );
192 #if ARCH_X86 || ARCH_X86_64
193 int x264_stack_pagealign( int (*func)(), int align );
195 #define x264_stack_pagealign( func, align ) func()
198 #define call_c1(func,...) func(__VA_ARGS__)
200 #if ARCH_X86 || defined(_WIN64)
201 /* detect when callee-saved regs aren't saved.
202 * needs an explicit asm check because it only sometimes crashes in normal use. */
203 intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... );
204 #define call_a1(func,...) x264_checkasm_call((intptr_t(*)())func, &ok, __VA_ARGS__)
206 #define call_a1 call_c1
209 #define call_bench(func,cpu,...)\
210 if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
214 call_a1(func, __VA_ARGS__);\
215 for( int ti = 0; ti < (cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
217 uint32_t t = read_time();\
222 t = read_time() - t;\
223 if( t*tcount <= tsum*4 && ti > 0 )\
229 bench_t *b = get_bench( func_name, cpu );\
235 /* for most functions, run benchmark and correctness test at the same time.
236 * for those that modify their inputs, run the above macros separately */
237 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
238 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
239 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
240 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
243 static int check_pixel( int cpu_ref, int cpu_new )
245 x264_pixel_function_t pixel_c;
246 x264_pixel_function_t pixel_ref;
247 x264_pixel_function_t pixel_asm;
248 x264_predict_t predict_4x4[12];
249 x264_predict8x8_t predict_8x8[12];
250 x264_predict_8x8_filter_t predict_8x8_filter;
251 ALIGNED_16( pixel edge[36] );
252 uint16_t cost_mv[32];
253 int ret = 0, ok, used_asm;
255 x264_pixel_init( 0, &pixel_c );
256 x264_pixel_init( cpu_ref, &pixel_ref );
257 x264_pixel_init( cpu_new, &pixel_asm );
258 x264_predict_4x4_init( 0, predict_4x4 );
259 x264_predict_8x8_init( 0, predict_8x8, &predict_8x8_filter );
260 predict_8x8_filter( pbuf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
263 for( int i = 0; i < 256; i++ )
268 pbuf4[i] = -(z&1) & PIXEL_MAX;
269 pbuf3[i] = ~pbuf4[i] & PIXEL_MAX;
271 // random pattern made of maxed pixel differences, in case an intermediate value overflows
272 for( int i = 256; i < 0x1000; i++ )
274 pbuf4[i] = -(pbuf1[i&~0x88]&1) & PIXEL_MAX;
275 pbuf3[i] = ~(pbuf4[i]) & PIXEL_MAX;
278 #define TEST_PIXEL( name, align ) \
279 ok = 1, used_asm = 0; \
280 for( int i = 0; i < 8; i++ ) \
282 int res_c, res_asm; \
283 if( pixel_asm.name[i] != pixel_ref.name[i] ) \
285 set_func_name( "%s_%s", #name, pixel_names[i] ); \
287 for( int j = 0; j < 64; j++ ) \
289 res_c = call_c( pixel_c.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \
290 res_asm = call_a( pixel_asm.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \
291 if( res_c != res_asm ) \
294 fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
298 for( int j = 0; j < 0x1000 && ok; j += 256 ) \
300 res_c = pixel_c .name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
301 res_asm = pixel_asm.name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
302 if( res_c != res_asm ) \
305 fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
310 report( "pixel " #name " :" );
312 TEST_PIXEL( sad, 0 );
313 TEST_PIXEL( sad_aligned, 1 );
314 TEST_PIXEL( ssd, 1 );
315 TEST_PIXEL( satd, 0 );
316 TEST_PIXEL( sa8d, 1 );
318 #define TEST_PIXEL_X( N ) \
319 ok = 1; used_asm = 0; \
320 for( int i = 0; i < 7; i++ ) \
322 int res_c[4]={0}, res_asm[4]={0}; \
323 if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
325 set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
327 for( int j = 0; j < 64; j++ ) \
329 pixel *pix2 = pbuf2+j; \
330 res_c[0] = pixel_c.sad[i]( pbuf1, 16, pix2, 64 ); \
331 res_c[1] = pixel_c.sad[i]( pbuf1, 16, pix2+6, 64 ); \
332 res_c[2] = pixel_c.sad[i]( pbuf1, 16, pix2+1, 64 ); \
335 res_c[3] = pixel_c.sad[i]( pbuf1, 16, pix2+10, 64 ); \
336 call_a( pixel_asm.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
339 call_a( pixel_asm.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
340 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
343 fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
344 i, res_c[0], res_c[1], res_c[2], res_c[3], \
345 res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
348 call_c2( pixel_c.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
350 call_c2( pixel_c.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
354 report( "pixel sad_x"#N" :" );
359 #define TEST_PIXEL_VAR( i ) \
360 if( pixel_asm.var[i] != pixel_ref.var[i] ) \
362 set_func_name( "%s_%s", "var", pixel_names[i] ); \
364 /* abi-check wrapper can't return uint64_t, so separate it from return value check */ \
365 call_c1( pixel_c.var[i], pbuf1, 16 ); \
366 call_a1( pixel_asm.var[i], pbuf1, 16 ); \
367 uint64_t res_c = pixel_c.var[i]( pbuf1, 16 ); \
368 uint64_t res_asm = pixel_asm.var[i]( pbuf1, 16 ); \
369 if( res_c != res_asm ) \
372 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) ); \
374 call_c2( pixel_c.var[i], pbuf1, 16 ); \
375 call_a2( pixel_asm.var[i], pbuf1, 16 ); \
378 ok = 1; used_asm = 0;
379 TEST_PIXEL_VAR( PIXEL_16x16 );
380 TEST_PIXEL_VAR( PIXEL_8x16 );
381 TEST_PIXEL_VAR( PIXEL_8x8 );
382 report( "pixel var :" );
384 #define TEST_PIXEL_VAR2( i ) \
385 if( pixel_asm.var2[i] != pixel_ref.var2[i] ) \
387 int res_c, res_asm, ssd_c, ssd_asm; \
388 set_func_name( "%s_%s", "var2", pixel_names[i] ); \
390 res_c = call_c( pixel_c.var2[i], pbuf1, 16, pbuf2, 16, &ssd_c ); \
391 res_asm = call_a( pixel_asm.var2[i], pbuf1, 16, pbuf2, 16, &ssd_asm ); \
392 if( res_c != res_asm || ssd_c != ssd_asm ) \
395 fprintf( stderr, "var2[%d]: %d != %d or %d != %d [FAILED]\n", i, res_c, res_asm, ssd_c, ssd_asm ); \
399 ok = 1; used_asm = 0;
400 TEST_PIXEL_VAR2( PIXEL_8x16 );
401 TEST_PIXEL_VAR2( PIXEL_8x8 );
402 report( "pixel var2 :" );
404 ok = 1; used_asm = 0;
405 for( int i = 0; i < 4; i++ )
406 if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
408 set_func_name( "hadamard_ac_%s", pixel_names[i] );
410 for( int j = 0; j < 32; j++ )
412 pixel *pix = (j&16 ? pbuf1 : pbuf3) + (j&15)*256;
413 call_c1( pixel_c.hadamard_ac[i], pbuf1, 16 );
414 call_a1( pixel_asm.hadamard_ac[i], pbuf1, 16 );
415 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
416 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
420 fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
424 call_c2( pixel_c.hadamard_ac[i], pbuf1, 16 );
425 call_a2( pixel_asm.hadamard_ac[i], pbuf1, 16 );
427 report( "pixel hadamard_ac :" );
430 for( int i = 0; i < 32; i++ )
431 for( int j = 0; j < 16; j++ )
432 pbuf4[16*i+j] = -((i+j)&1) & PIXEL_MAX;
433 ok = 1; used_asm = 0;
434 if( pixel_asm.vsad != pixel_ref.vsad )
436 for( int h = 2; h <= 32; h += 2 )
439 set_func_name( "vsad" );
441 for( int j = 0; j < 2 && ok; j++ )
443 pixel *p = j ? pbuf4 : pbuf1;
444 res_c = call_c( pixel_c.vsad, p, 16, h );
445 res_asm = call_a( pixel_asm.vsad, p, 16, h );
446 if( res_c != res_asm )
449 fprintf( stderr, "vsad: height=%d, %d != %d\n", h, res_c, res_asm );
455 report( "pixel vsad :" );
457 #define TEST_INTRA_X3( name, i8x8, ... ) \
458 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
460 int res_c[3], res_asm[3]; \
461 set_func_name( #name ); \
463 call_c( pixel_c.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_c ); \
464 call_a( pixel_asm.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_asm ); \
465 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
468 fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
469 res_c[0], res_c[1], res_c[2], \
470 res_asm[0], res_asm[1], res_asm[2] ); \
474 #define TEST_INTRA_X9( name, cmp ) \
475 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
477 set_func_name( #name ); \
479 ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
480 for( int i=0; i<17; i++ ) \
481 bitcosts[i] = 9*(i!=8); \
482 memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
483 memcpy( pbuf4, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
484 for( int i=0; i<32; i++ ) \
486 pixel *fenc = pbuf1+48+i*12; \
487 pixel *fdec1 = pbuf3+48+i*12; \
488 pixel *fdec2 = pbuf4+48+i*12; \
489 int pred_mode = i%9; \
490 int res_c = INT_MAX; \
491 for( int j=0; j<9; j++ ) \
493 predict_4x4[j]( fdec1 ); \
494 int cost = pixel_c.cmp[PIXEL_4x4]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
495 if( cost < (uint16_t)res_c ) \
496 res_c = cost + (j<<16); \
498 predict_4x4[res_c>>16]( fdec1 ); \
499 int res_a = call_a( pixel_asm.name, fenc, fdec2, bitcosts+8-pred_mode ); \
500 if( res_c != res_a ) \
503 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
506 if( memcmp(fdec1, fdec2, 4*FDEC_STRIDE*sizeof(pixel)) ) \
509 fprintf( stderr, #name" [FAILED]\n" ); \
510 for( int j=0; j<16; j++ ) \
511 fprintf( stderr, "%02x ", fdec1[(j&3)+(j>>2)*FDEC_STRIDE] ); \
512 fprintf( stderr, "\n" ); \
513 for( int j=0; j<16; j++ ) \
514 fprintf( stderr, "%02x ", fdec2[(j&3)+(j>>2)*FDEC_STRIDE] ); \
515 fprintf( stderr, "\n" ); \
521 #define TEST_INTRA8_X9( name, cmp ) \
522 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
524 set_func_name( #name ); \
526 ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
527 ALIGNED_ARRAY_16( uint16_t, satds_c,[16] ); \
528 ALIGNED_ARRAY_16( uint16_t, satds_a,[16] ); \
529 memset( satds_c, 0, 16 * sizeof(*satds_c) ); \
530 memset( satds_a, 0, 16 * sizeof(*satds_a) ); \
531 for( int i=0; i<17; i++ ) \
532 bitcosts[i] = 9*(i!=8); \
533 for( int i=0; i<32; i++ ) \
535 pixel *fenc = pbuf1+48+i*12; \
536 pixel *fdec1 = pbuf3+48+i*12; \
537 pixel *fdec2 = pbuf4+48+i*12; \
538 int pred_mode = i%9; \
539 int res_c = INT_MAX; \
540 predict_8x8_filter( fdec1, edge, ALL_NEIGHBORS, ALL_NEIGHBORS ); \
541 for( int j=0; j<9; j++ ) \
543 predict_8x8[j]( fdec1, edge ); \
544 satds_c[j] = pixel_c.cmp[PIXEL_8x8]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
545 if( satds_c[j] < (uint16_t)res_c ) \
546 res_c = satds_c[j] + (j<<16); \
548 predict_8x8[res_c>>16]( fdec1, edge ); \
549 int res_a = call_a( pixel_asm.name, fenc, fdec2, edge, bitcosts+8-pred_mode, satds_a ); \
550 if( res_c != res_a || memcmp(satds_c, satds_a, sizeof(satds_c)) ) \
553 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
554 for( int j = 0; j < 9; j++ ) \
555 fprintf( stderr, "%5d ", satds_c[j]); \
556 fprintf( stderr, "\n" ); \
557 for( int j = 0; j < 9; j++ ) \
558 fprintf( stderr, "%5d ", satds_a[j]); \
559 fprintf( stderr, "\n" ); \
562 for( int j=0; j<8; j++ ) \
563 if( memcmp(fdec1+j*FDEC_STRIDE, fdec2+j*FDEC_STRIDE, 8*sizeof(pixel)) ) \
567 fprintf( stderr, #name" [FAILED]\n" ); \
568 for( int j=0; j<8; j++ ) \
570 for( int k=0; k<8; k++ ) \
571 fprintf( stderr, "%02x ", fdec1[k+j*FDEC_STRIDE] ); \
572 fprintf( stderr, "\n" ); \
574 fprintf( stderr, "\n" ); \
575 for( int j=0; j<8; j++ ) \
577 for( int k=0; k<8; k++ ) \
578 fprintf( stderr, "%02x ", fdec2[k+j*FDEC_STRIDE] ); \
579 fprintf( stderr, "\n" ); \
581 fprintf( stderr, "\n" ); \
587 memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) );
588 ok = 1; used_asm = 0;
589 TEST_INTRA_X3( intra_satd_x3_16x16, 0 );
590 TEST_INTRA_X3( intra_satd_x3_8x16c, 0 );
591 TEST_INTRA_X3( intra_satd_x3_8x8c, 0 );
592 TEST_INTRA_X3( intra_sa8d_x3_8x8, 1, edge );
593 TEST_INTRA_X3( intra_satd_x3_4x4, 0 );
594 report( "intra satd_x3 :" );
595 ok = 1; used_asm = 0;
596 TEST_INTRA_X3( intra_sad_x3_16x16, 0 );
597 TEST_INTRA_X3( intra_sad_x3_8x16c, 0 );
598 TEST_INTRA_X3( intra_sad_x3_8x8c, 0 );
599 TEST_INTRA_X3( intra_sad_x3_8x8, 1, edge );
600 TEST_INTRA_X3( intra_sad_x3_4x4, 0 );
601 report( "intra sad_x3 :" );
602 ok = 1; used_asm = 0;
603 TEST_INTRA_X9( intra_satd_x9_4x4, satd );
604 TEST_INTRA8_X9( intra_sa8d_x9_8x8, sa8d );
605 report( "intra satd_x9 :" );
606 ok = 1; used_asm = 0;
607 TEST_INTRA_X9( intra_sad_x9_4x4, sad );
608 TEST_INTRA8_X9( intra_sad_x9_8x8, sad );
609 report( "intra sad_x9 :" );
611 ok = 1; used_asm = 0;
612 if( pixel_asm.ssd_nv12_core != pixel_ref.ssd_nv12_core )
615 set_func_name( "ssd_nv12" );
616 uint64_t res_u_c, res_v_c, res_u_a, res_v_a;
617 pixel_c.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c );
618 pixel_asm.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a );
619 if( res_u_c != res_u_a || res_v_c != res_v_a )
622 fprintf( stderr, "ssd_nv12: %"PRIu64",%"PRIu64" != %"PRIu64",%"PRIu64"\n",
623 res_u_c, res_v_c, res_u_a, res_v_a );
625 call_c( pixel_c.ssd_nv12_core, pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c );
626 call_a( pixel_asm.ssd_nv12_core, pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a );
628 report( "ssd_nv12 :" );
630 if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
631 pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
635 ALIGNED_16( int sums[5][4] ) = {{0}};
638 res_c = x264_pixel_ssim_wxh( &pixel_c, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
639 res_a = x264_pixel_ssim_wxh( &pixel_asm, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
640 if( fabs( res_c - res_a ) > 1e-6 )
643 fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
645 set_func_name( "ssim_core" );
646 call_c2( pixel_c.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums );
647 call_a2( pixel_asm.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums );
648 set_func_name( "ssim_end" );
649 call_c2( pixel_c.ssim_end4, sums, sums, 4 );
650 call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
654 ok = 1; used_asm = 0;
655 for( int i = 0; i < 32; i++ )
657 for( int i = 0; i < 100 && ok; i++ )
658 if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
660 ALIGNED_16( uint16_t sums[72] );
661 ALIGNED_16( int dc[4] );
662 ALIGNED_16( int16_t mvs_a[32] );
663 ALIGNED_16( int16_t mvs_c[32] );
665 int thresh = rand() & 0x3fff;
666 set_func_name( "esa_ads" );
667 for( int j = 0; j < 72; j++ )
668 sums[j] = rand() & 0x3fff;
669 for( int j = 0; j < 4; j++ )
670 dc[j] = rand() & 0x3fff;
672 mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
673 mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
674 if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
677 printf( "c%d: ", i&3 );
678 for( int j = 0; j < mvn_c; j++ )
679 printf( "%d ", mvs_c[j] );
680 printf( "\na%d: ", i&3 );
681 for( int j = 0; j < mvn_a; j++ )
682 printf( "%d ", mvs_a[j] );
686 report( "esa ads:" );
691 static int check_dct( int cpu_ref, int cpu_new )
693 x264_dct_function_t dct_c;
694 x264_dct_function_t dct_ref;
695 x264_dct_function_t dct_asm;
696 x264_quant_function_t qf;
697 int ret = 0, ok, used_asm, interlace = 0;
698 ALIGNED_16( dctcoef dct1[16][16] );
699 ALIGNED_16( dctcoef dct2[16][16] );
700 ALIGNED_16( dctcoef dct4[16][16] );
701 ALIGNED_16( dctcoef dct8[4][64] );
702 ALIGNED_16( dctcoef dctdc[2][8] );
706 x264_dct_init( 0, &dct_c );
707 x264_dct_init( cpu_ref, &dct_ref);
708 x264_dct_init( cpu_new, &dct_asm );
710 memset( h, 0, sizeof(*h) );
711 x264_param_default( &h->param );
712 h->sps->i_chroma_format_idc = 1;
713 h->chroma_qp_table = i_chroma_qp_table + 12;
714 h->param.analyse.i_luma_deadzone[0] = 0;
715 h->param.analyse.i_luma_deadzone[1] = 0;
716 h->param.analyse.b_transform_8x8 = 1;
717 for( int i = 0; i < 6; i++ )
718 h->pps->scaling_list[i] = x264_cqm_flat16;
720 x264_quant_init( h, 0, &qf );
722 /* overflow test cases */
723 for( int i = 0; i < 5; i++ )
725 pixel *enc = &pbuf3[16*i*FENC_STRIDE];
726 pixel *dec = &pbuf4[16*i*FDEC_STRIDE];
728 for( int j = 0; j < 16; j++ )
730 int cond_a = (i < 2) ? 1 : ((j&3) == 0 || (j&3) == (i-1));
731 int cond_b = (i == 0) ? 1 : !cond_a;
732 enc[0] = enc[1] = enc[4] = enc[5] = enc[8] = enc[9] = enc[12] = enc[13] = cond_a ? PIXEL_MAX : 0;
733 enc[2] = enc[3] = enc[6] = enc[7] = enc[10] = enc[11] = enc[14] = enc[15] = cond_b ? PIXEL_MAX : 0;
735 for( int k = 0; k < 4; k++ )
736 dec[k] = PIXEL_MAX - enc[k];
743 #define TEST_DCT( name, t1, t2, size ) \
744 if( dct_asm.name != dct_ref.name ) \
746 set_func_name( #name ); \
748 pixel *enc = pbuf3; \
749 pixel *dec = pbuf4; \
750 for( int j = 0; j < 5; j++) \
752 call_c( dct_c.name, t1, &pbuf1[j*64], &pbuf2[j*64] ); \
753 call_a( dct_asm.name, t2, &pbuf1[j*64], &pbuf2[j*64] ); \
754 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
757 fprintf( stderr, #name " [FAILED]\n" ); \
758 for( int k = 0; k < size; k++ )\
759 printf( "%d ", ((dctcoef*)t1)[k] );\
761 for( int k = 0; k < size; k++ )\
762 printf( "%d ", ((dctcoef*)t2)[k] );\
766 call_c( dct_c.name, t1, enc, dec ); \
767 call_a( dct_asm.name, t2, enc, dec ); \
768 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
771 fprintf( stderr, #name " [FAILED] (overflow)\n" ); \
774 enc += 16*FENC_STRIDE; \
775 dec += 16*FDEC_STRIDE; \
778 ok = 1; used_asm = 0;
779 TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16 );
780 TEST_DCT( sub8x8_dct, dct1, dct2, 16*4 );
781 TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4 );
782 TEST_DCT( sub8x16_dct_dc, dctdc[0], dctdc[1], 8 );
783 TEST_DCT( sub16x16_dct, dct1, dct2, 16*16 );
784 report( "sub_dct4 :" );
786 ok = 1; used_asm = 0;
787 TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64 );
788 TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*4 );
789 report( "sub_dct8 :" );
792 // fdct and idct are denormalized by different factors, so quant/dequant
793 // is needed to force the coefs into the right range.
794 dct_c.sub16x16_dct( dct4, pbuf1, pbuf2 );
795 dct_c.sub16x16_dct8( dct8, pbuf1, pbuf2 );
796 for( int i = 0; i < 16; i++ )
798 qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
799 qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
801 for( int i = 0; i < 4; i++ )
803 qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
804 qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
806 x264_cqm_delete( h );
808 #define TEST_IDCT( name, src ) \
809 if( dct_asm.name != dct_ref.name ) \
811 set_func_name( #name ); \
813 memcpy( pbuf3, pbuf1, 32*32 * sizeof(pixel) ); \
814 memcpy( pbuf4, pbuf1, 32*32 * sizeof(pixel) ); \
815 memcpy( dct1, src, 256 * sizeof(dctcoef) ); \
816 memcpy( dct2, src, 256 * sizeof(dctcoef) ); \
817 call_c1( dct_c.name, pbuf3, (void*)dct1 ); \
818 call_a1( dct_asm.name, pbuf4, (void*)dct2 ); \
819 if( memcmp( pbuf3, pbuf4, 32*32 * sizeof(pixel) ) ) \
822 fprintf( stderr, #name " [FAILED]\n" ); \
824 call_c2( dct_c.name, pbuf3, (void*)dct1 ); \
825 call_a2( dct_asm.name, pbuf4, (void*)dct2 ); \
827 ok = 1; used_asm = 0;
828 TEST_IDCT( add4x4_idct, dct4 );
829 TEST_IDCT( add8x8_idct, dct4 );
830 TEST_IDCT( add8x8_idct_dc, dct4 );
831 TEST_IDCT( add16x16_idct, dct4 );
832 TEST_IDCT( add16x16_idct_dc, dct4 );
833 report( "add_idct4 :" );
835 ok = 1; used_asm = 0;
836 TEST_IDCT( add8x8_idct8, dct8 );
837 TEST_IDCT( add16x16_idct8, dct8 );
838 report( "add_idct8 :" );
841 #define TEST_DCTDC( name )\
842 ok = 1; used_asm = 0;\
843 if( dct_asm.name != dct_ref.name )\
845 set_func_name( #name );\
847 uint16_t *p = (uint16_t*)buf1;\
848 for( int i = 0; i < 16 && ok; i++ )\
850 for( int j = 0; j < 16; j++ )\
851 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
852 : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
853 : ((*p++)&0x1fff)-0x1000; /* general case */\
854 memcpy( dct2, dct1, 16 * sizeof(dctcoef) );\
855 call_c1( dct_c.name, dct1[0] );\
856 call_a1( dct_asm.name, dct2[0] );\
857 if( memcmp( dct1, dct2, 16 * sizeof(dctcoef) ) )\
860 call_c2( dct_c.name, dct1[0] );\
861 call_a2( dct_asm.name, dct2[0] );\
863 report( #name " :" );
865 TEST_DCTDC( dct4x4dc );
866 TEST_DCTDC( idct4x4dc );
869 #define TEST_DCTDC_CHROMA( name )\
870 ok = 1; used_asm = 0;\
871 if( dct_asm.name != dct_ref.name )\
873 set_func_name( #name );\
875 uint16_t *p = (uint16_t*)buf1;\
876 for( int i = 0; i < 16 && ok; i++ )\
878 for( int j = 0; j < 8; j++ )\
879 dct1[j][0] = !i ? (j^j>>1^j>>2)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
880 : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
881 : ((*p++)&0x1fff)-0x1000; /* general case */\
882 memcpy( dct2, dct1, 8*16 * sizeof(dctcoef) );\
883 call_c1( dct_c.name, dctdc[0], dct1 );\
884 call_a1( dct_asm.name, dctdc[1], dct2 );\
885 if( memcmp( dctdc[0], dctdc[1], 8 * sizeof(dctcoef) ) || memcmp( dct1, dct2, 8*16 * sizeof(dctcoef) ) )\
888 fprintf( stderr, #name " [FAILED]\n" ); \
891 call_c2( dct_c.name, dctdc[0], dct1 );\
892 call_a2( dct_asm.name, dctdc[1], dct2 );\
894 report( #name " :" );
896 TEST_DCTDC_CHROMA( dct2x4dc );
897 #undef TEST_DCTDC_CHROMA
899 x264_zigzag_function_t zigzag_c[2];
900 x264_zigzag_function_t zigzag_ref[2];
901 x264_zigzag_function_t zigzag_asm[2];
903 ALIGNED_16( dctcoef level1[64] );
904 ALIGNED_16( dctcoef level2[64] );
906 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
907 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
909 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
911 for( int i = 0; i < size*size; i++ ) \
913 call_c( zigzag_c[interlace].name, t1, dct ); \
914 call_a( zigzag_asm[interlace].name, t2, dct ); \
915 if( memcmp( t1, t2, size*size*sizeof(dctcoef) ) ) \
918 for( int i = 0; i < 2; i++ ) \
920 dctcoef *d = (dctcoef*)(i ? t2 : t1); \
921 for( int j = 0; j < size; j++ ) \
923 for( int k = 0; k < size; k++ ) \
924 fprintf( stderr, "%2d ", d[k+j*8] ); \
925 fprintf( stderr, "\n" ); \
927 fprintf( stderr, "\n" ); \
929 fprintf( stderr, #name " [FAILED]\n" ); \
933 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
934 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
937 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
939 memcpy( pbuf3, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
940 memcpy( pbuf4, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
941 nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
942 nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
943 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE*sizeof(pixel) ) || nz_c != nz_a ) \
946 fprintf( stderr, #name " [FAILED]\n" ); \
948 call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
949 call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
952 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
953 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
956 dctcoef dc_a, dc_c; \
957 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
959 for( int i = 0; i < 2; i++ ) \
961 memcpy( pbuf3, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
962 memcpy( pbuf4, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
963 for( int j = 0; j < 4; j++ ) \
965 memcpy( pbuf3 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
966 memcpy( pbuf4 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
968 nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
969 nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
970 if( memcmp( t1+1, t2+1, 15*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE * sizeof(pixel) ) || nz_c != nz_a || dc_c != dc_a ) \
973 fprintf( stderr, #name " [FAILED]\n" ); \
977 call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
978 call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
981 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
982 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
984 for( int j = 0; j < 100; j++ ) \
986 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
988 memcpy(dct, buf1, size*sizeof(dctcoef)); \
989 for( int i = 0; i < size; i++ ) \
990 dct[i] = rand()&0x1F ? 0 : dct[i]; \
991 memcpy(buf3, buf4, 10); \
992 call_c( zigzag_c[interlace].name, t1, dct, buf3 ); \
993 call_a( zigzag_asm[interlace].name, t2, dct, buf4 ); \
994 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( buf3, buf4, 10 ) ) \
1001 x264_zigzag_init( 0, &zigzag_c[0], &zigzag_c[1] );
1002 x264_zigzag_init( cpu_ref, &zigzag_ref[0], &zigzag_ref[1] );
1003 x264_zigzag_init( cpu_new, &zigzag_asm[0], &zigzag_asm[1] );
1005 ok = 1; used_asm = 0;
1006 TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct1[0], 64 );
1007 report( "zigzag_interleave :" );
1009 for( interlace = 0; interlace <= 1; interlace++ )
1011 ok = 1; used_asm = 0;
1012 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, dct1[0], 8 );
1013 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 4 );
1014 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
1015 TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
1016 report( interlace ? "zigzag_field :" : "zigzag_frame :" );
1018 #undef TEST_ZIGZAG_SCAN
1019 #undef TEST_ZIGZAG_SUB
1024 static int check_mc( int cpu_ref, int cpu_new )
1026 x264_mc_functions_t mc_c;
1027 x264_mc_functions_t mc_ref;
1028 x264_mc_functions_t mc_a;
1029 x264_pixel_function_t pixf;
1031 pixel *src = &(pbuf1)[2*64+2];
1032 pixel *src2[4] = { &(pbuf1)[3*64+2], &(pbuf1)[5*64+2],
1033 &(pbuf1)[7*64+2], &(pbuf1)[9*64+2] };
1034 pixel *dst1 = pbuf3;
1035 pixel *dst2 = pbuf4;
1037 int ret = 0, ok, used_asm;
1039 x264_mc_init( 0, &mc_c );
1040 x264_mc_init( cpu_ref, &mc_ref );
1041 x264_mc_init( cpu_new, &mc_a );
1042 x264_pixel_init( 0, &pixf );
1044 #define MC_TEST_LUMA( w, h ) \
1045 if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
1047 const x264_weight_t *weight = x264_weight_none; \
1048 set_func_name( "mc_luma_%dx%d", w, h ); \
1050 for( int i = 0; i < 1024; i++ ) \
1051 pbuf3[i] = pbuf4[i] = 0xCD; \
1052 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
1053 call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h, weight ); \
1054 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1056 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
1060 if( mc_a.get_ref != mc_ref.get_ref ) \
1062 pixel *ref = dst2; \
1063 int ref_stride = 32; \
1064 int w_checked = ( ( sizeof(pixel) == 2 && (w == 12 || w == 20)) ? w-2 : w ); \
1065 const x264_weight_t *weight = x264_weight_none; \
1066 set_func_name( "get_ref_%dx%d", w_checked, h ); \
1068 for( int i = 0; i < 1024; i++ ) \
1069 pbuf3[i] = pbuf4[i] = 0xCD; \
1070 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
1071 ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h, weight ); \
1072 for( int i = 0; i < h; i++ ) \
1073 if( memcmp( dst1+i*32, ref+i*ref_stride, w_checked * sizeof(pixel) ) ) \
1075 fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w_checked, h ); \
1081 #define MC_TEST_CHROMA( w, h ) \
1082 if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
1084 set_func_name( "mc_chroma_%dx%d", w, h ); \
1086 for( int i = 0; i < 1024; i++ ) \
1087 pbuf3[i] = pbuf4[i] = 0xCD; \
1088 call_c( mc_c.mc_chroma, dst1, dst1+8, 16, src, 64, dx, dy, w, h ); \
1089 call_a( mc_a.mc_chroma, dst2, dst2+8, 16, src, 64, dx, dy, w, h ); \
1090 /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \
1091 for( int j = 0; j < h; j++ ) \
1092 for( int i = w; i < 8; i++ ) \
1094 dst2[i+j*16+8] = dst1[i+j*16+8]; \
1095 dst2[i+j*16] = dst1[i+j*16]; \
1097 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1099 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
1103 ok = 1; used_asm = 0;
1104 for( int dy = -8; dy < 8; dy++ )
1105 for( int dx = -128; dx < 128; dx++ )
1107 if( rand()&15 ) continue; // running all of them is too slow
1108 MC_TEST_LUMA( 20, 18 );
1109 MC_TEST_LUMA( 16, 16 );
1110 MC_TEST_LUMA( 16, 8 );
1111 MC_TEST_LUMA( 12, 10 );
1112 MC_TEST_LUMA( 8, 16 );
1113 MC_TEST_LUMA( 8, 8 );
1114 MC_TEST_LUMA( 8, 4 );
1115 MC_TEST_LUMA( 4, 8 );
1116 MC_TEST_LUMA( 4, 4 );
1118 report( "mc luma :" );
1120 ok = 1; used_asm = 0;
1121 for( int dy = -1; dy < 9; dy++ )
1122 for( int dx = -128; dx < 128; dx++ )
1124 if( rand()&15 ) continue;
1125 MC_TEST_CHROMA( 8, 8 );
1126 MC_TEST_CHROMA( 8, 4 );
1127 MC_TEST_CHROMA( 4, 8 );
1128 MC_TEST_CHROMA( 4, 4 );
1129 MC_TEST_CHROMA( 4, 2 );
1130 MC_TEST_CHROMA( 2, 4 );
1131 MC_TEST_CHROMA( 2, 2 );
1133 report( "mc chroma :" );
1135 #undef MC_TEST_CHROMA
1137 #define MC_TEST_AVG( name, weight ) \
1139 for( int i = 0; i < 12; i++ ) \
1141 memcpy( pbuf3, pbuf1+320, 320 * sizeof(pixel) ); \
1142 memcpy( pbuf4, pbuf1+320, 320 * sizeof(pixel) ); \
1143 if( mc_a.name[i] != mc_ref.name[i] ) \
1145 set_func_name( "%s_%s", #name, pixel_names[i] ); \
1147 call_c1( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1148 call_a1( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1149 if( memcmp( pbuf3, pbuf4, 320 * sizeof(pixel) ) ) \
1152 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
1154 call_c2( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1155 call_a2( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1160 ok = 1, used_asm = 0;
1161 for( int w = -63; w <= 127 && ok; w++ )
1162 MC_TEST_AVG( avg, w );
1163 report( "mc wpredb :" );
1165 #define MC_TEST_WEIGHT( name, weight, aligned ) \
1166 int align_off = (aligned ? 0 : rand()%16); \
1167 for( int i = 1; i <= 5; i++ ) \
1169 ALIGNED_16( pixel buffC[640] ); \
1170 ALIGNED_16( pixel buffA[640] ); \
1171 int j = X264_MAX( i*4, 2 ); \
1172 memset( buffC, 0, 640 * sizeof(pixel) ); \
1173 memset( buffA, 0, 640 * sizeof(pixel) ); \
1176 /* w12 is the same as w16 in some cases */ \
1177 if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
1179 if( mc_a.name[i] != mc_ref.name[i] ) \
1181 set_func_name( "%s_w%d", #name, j ); \
1183 call_c1( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
1184 mc_a.weight_cache(&ha, &weight); \
1185 call_a1( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
1186 for( int k = 0; k < 16; k++ ) \
1187 if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \
1190 fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
1193 call_c2( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
1194 call_a2( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
1198 ok = 1; used_asm = 0;
1201 for( int s = 0; s <= 127 && ok; s++ )
1203 for( int o = -128; o <= 127 && ok; o++ )
1205 if( rand() & 2047 ) continue;
1206 for( int d = 0; d <= 7 && ok; d++ )
1210 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
1211 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
1216 report( "mc weight :" );
1218 ok = 1; used_asm = 0;
1219 for( int o = 0; o <= 127 && ok; o++ )
1222 if( rand() & 15 ) continue;
1223 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1224 MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
1226 report( "mc offsetadd :" );
1227 ok = 1; used_asm = 0;
1228 for( int o = -128; o < 0 && ok; o++ )
1231 if( rand() & 15 ) continue;
1232 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1233 MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
1235 report( "mc offsetsub :" );
1237 ok = 1; used_asm = 0;
1238 for( int height = 8; height <= 16; height += 8 )
1240 if( mc_a.store_interleave_chroma != mc_ref.store_interleave_chroma )
1242 set_func_name( "store_interleave_chroma" );
1244 memset( pbuf3, 0, 64*height );
1245 memset( pbuf4, 0, 64*height );
1246 call_c( mc_c.store_interleave_chroma, pbuf3, 64, pbuf1, pbuf1+16, height );
1247 call_a( mc_a.store_interleave_chroma, pbuf4, 64, pbuf1, pbuf1+16, height );
1248 if( memcmp( pbuf3, pbuf4, 64*height ) )
1251 fprintf( stderr, "store_interleave_chroma FAILED: h=%d\n", height );
1255 if( mc_a.load_deinterleave_chroma_fenc != mc_ref.load_deinterleave_chroma_fenc )
1257 set_func_name( "load_deinterleave_chroma_fenc" );
1259 call_c( mc_c.load_deinterleave_chroma_fenc, pbuf3, pbuf1, 64, height );
1260 call_a( mc_a.load_deinterleave_chroma_fenc, pbuf4, pbuf1, 64, height );
1261 if( memcmp( pbuf3, pbuf4, FENC_STRIDE*height ) )
1264 fprintf( stderr, "load_deinterleave_chroma_fenc FAILED: h=%d\n", height );
1268 if( mc_a.load_deinterleave_chroma_fdec != mc_ref.load_deinterleave_chroma_fdec )
1270 set_func_name( "load_deinterleave_chroma_fdec" );
1272 call_c( mc_c.load_deinterleave_chroma_fdec, pbuf3, pbuf1, 64, height );
1273 call_a( mc_a.load_deinterleave_chroma_fdec, pbuf4, pbuf1, 64, height );
1274 if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*height ) )
1277 fprintf( stderr, "load_deinterleave_chroma_fdec FAILED: h=%d\n", height );
1282 report( "store_interleave :" );
1285 int w, h, src_stride;
1286 } plane_specs[] = { {2,2,2}, {8,6,8}, {20,31,24}, {32,8,40}, {256,10,272}, {504,7,505}, {528,6,528}, {256,10,-256}, {263,9,-264}, {1904,1,0} };
1287 ok = 1; used_asm = 0;
1288 if( mc_a.plane_copy != mc_ref.plane_copy )
1290 set_func_name( "plane_copy" );
1292 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1294 int w = plane_specs[i].w;
1295 int h = plane_specs[i].h;
1296 int src_stride = plane_specs[i].src_stride;
1297 int dst_stride = (w + 127) & ~63;
1298 assert( dst_stride * h <= 0x1000 );
1299 pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1300 memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1301 memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1302 call_c( mc_c.plane_copy, pbuf3, dst_stride, src1, src_stride, w, h );
1303 call_a( mc_a.plane_copy, pbuf4, dst_stride, src1, src_stride, w, h );
1304 for( int y = 0; y < h; y++ )
1305 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(pixel) ) )
1308 fprintf( stderr, "plane_copy FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1314 if( mc_a.plane_copy_interleave != mc_ref.plane_copy_interleave )
1316 set_func_name( "plane_copy_interleave" );
1318 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1320 int w = (plane_specs[i].w + 1) >> 1;
1321 int h = plane_specs[i].h;
1322 int src_stride = (plane_specs[i].src_stride + 1) >> 1;
1323 int dst_stride = (2*w + 127) & ~63;
1324 assert( dst_stride * h <= 0x1000 );
1325 pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1326 memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1327 memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1328 call_c( mc_c.plane_copy_interleave, pbuf3, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1329 call_a( mc_a.plane_copy_interleave, pbuf4, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1330 for( int y = 0; y < h; y++ )
1331 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) )
1334 fprintf( stderr, "plane_copy_interleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1340 if( mc_a.plane_copy_deinterleave != mc_ref.plane_copy_deinterleave )
1342 set_func_name( "plane_copy_deinterleave" );
1344 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1346 int w = (plane_specs[i].w + 1) >> 1;
1347 int h = plane_specs[i].h;
1349 int src_stride = (2*w + 127) & ~63;
1350 int offv = (dst_stride*h + 31) & ~15;
1351 memset( pbuf3, 0, 0x1000 );
1352 memset( pbuf4, 0, 0x1000 );
1353 call_c( mc_c.plane_copy_deinterleave, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf1, src_stride, w, h );
1354 call_a( mc_a.plane_copy_deinterleave, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf1, src_stride, w, h );
1355 for( int y = 0; y < h; y++ )
1356 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w ) ||
1357 memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w ) )
1360 fprintf( stderr, "plane_copy_deinterleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1365 report( "plane_copy :" );
1367 if( mc_a.hpel_filter != mc_ref.hpel_filter )
1369 pixel *srchpel = pbuf1+8+2*64;
1370 pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
1371 pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
1372 void *tmp = pbuf3+49*64;
1373 set_func_name( "hpel_filter" );
1374 ok = 1; used_asm = 1;
1375 memset( pbuf3, 0, 4096 * sizeof(pixel) );
1376 memset( pbuf4, 0, 4096 * sizeof(pixel) );
1377 call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, 64, 48, 10, tmp );
1378 call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, 64, 48, 10, tmp );
1379 for( int i = 0; i < 3; i++ )
1380 for( int j = 0; j < 10; j++ )
1381 //FIXME ideally the first pixels would match too, but they aren't actually used
1382 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
1385 fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
1386 for( int k = 0; k < 48; k++ )
1387 printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
1389 for( int k = 0; k < 48; k++ )
1390 printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
1394 report( "hpel filter :" );
1397 if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
1399 pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
1400 pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1401 set_func_name( "lowres_init" );
1402 ok = 1; used_asm = 1;
1403 for( int w = 40; w <= 48; w += 8 )
1405 int stride = (w+8)&~15;
1406 call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
1407 call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
1408 for( int i = 0; i < 16; i++ )
1410 for( int j = 0; j < 4; j++ )
1411 if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w * sizeof(pixel) ) )
1414 fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1415 for( int k = 0; k < w; k++ )
1416 printf( "%d ", dstc[j][k+i*stride] );
1418 for( int k = 0; k < w; k++ )
1419 printf( "%d ", dsta[j][k+i*stride] );
1425 report( "lowres init :" );
1428 #define INTEGRAL_INIT( name, size, ... )\
1429 if( mc_a.name != mc_ref.name )\
1432 set_func_name( #name );\
1434 memcpy( buf3, buf1, size*2*stride );\
1435 memcpy( buf4, buf1, size*2*stride );\
1436 uint16_t *sum = (uint16_t*)buf3;\
1437 call_c1( mc_c.name, __VA_ARGS__ );\
1438 sum = (uint16_t*)buf4;\
1439 call_a1( mc_a.name, __VA_ARGS__ );\
1440 if( memcmp( buf3, buf4, (stride-8)*2 ) \
1441 || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
1443 call_c2( mc_c.name, __VA_ARGS__ );\
1444 call_a2( mc_a.name, __VA_ARGS__ );\
1446 ok = 1; used_asm = 0;
1447 INTEGRAL_INIT( integral_init4h, 2, sum+stride, pbuf2, stride );
1448 INTEGRAL_INIT( integral_init8h, 2, sum+stride, pbuf2, stride );
1449 INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
1450 INTEGRAL_INIT( integral_init8v, 9, sum, stride );
1451 report( "integral init :" );
1453 if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1455 ok = 1; used_asm = 1;
1457 for( int i = 0; i < 10; i++ )
1459 float fps_factor = (rand()&65535) / 256.;
1460 set_func_name( "mbtree_propagate" );
1461 int *dsta = (int*)buf3;
1462 int *dstc = dsta+400;
1463 uint16_t *prop = (uint16_t*)buf1;
1464 uint16_t *intra = (uint16_t*)buf4;
1465 uint16_t *inter = intra+128;
1466 uint16_t *qscale = inter+128;
1467 uint16_t *rnd = (uint16_t*)buf2;
1469 for( int j = 0; j < 100; j++ )
1471 intra[j] = *rnd++ & 0x7fff;
1472 intra[j] += !intra[j];
1473 inter[j] = *rnd++ & 0x7fff;
1474 qscale[j] = *rnd++ & 0x7fff;
1476 call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, &fps_factor, 100 );
1477 call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, &fps_factor, 100 );
1478 // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1480 for( int j = 0; j < 100 && ok; j++ )
1482 ok &= abs( dstc[j]-dsta[j] ) <= 1 || fabs( (double)dstc[j]/dsta[j]-1 ) < 1e-4;
1484 fprintf( stderr, "mbtree_propagate FAILED: %f !~= %f\n", (double)dstc[j], (double)dsta[j] );
1487 report( "mbtree propagate :" );
1490 if( mc_a.memcpy_aligned != mc_ref.memcpy_aligned )
1492 set_func_name( "memcpy_aligned" );
1493 ok = 1; used_asm = 1;
1494 for( int size = 16; size < 256; size += 16 )
1496 memset( buf4, 0xAA, size + 1 );
1497 call_c( mc_c.memcpy_aligned, buf3, buf1, size );
1498 call_a( mc_a.memcpy_aligned, buf4, buf1, size );
1499 if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1502 fprintf( stderr, "memcpy_aligned FAILED: size=%d\n", size );
1506 report( "memcpy aligned :" );
1509 if( mc_a.memzero_aligned != mc_ref.memzero_aligned )
1511 set_func_name( "memzero_aligned" );
1512 ok = 1; used_asm = 1;
1513 for( int size = 128; size < 1024; size += 128 )
1515 memset( buf4, 0xAA, size + 1 );
1516 call_c( mc_c.memzero_aligned, buf3, size );
1517 call_a( mc_a.memzero_aligned, buf4, size );
1518 if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1521 fprintf( stderr, "memzero_aligned FAILED: size=%d\n", size );
1525 report( "memzero aligned :" );
1531 static int check_deblock( int cpu_ref, int cpu_new )
1533 x264_deblock_function_t db_c;
1534 x264_deblock_function_t db_ref;
1535 x264_deblock_function_t db_a;
1536 int ret = 0, ok = 1, used_asm = 0;
1537 int alphas[36], betas[36];
1540 x264_deblock_init( 0, &db_c, 0 );
1541 x264_deblock_init( cpu_ref, &db_ref, 0 );
1542 x264_deblock_init( cpu_new, &db_a, 0 );
1544 /* not exactly the real values of a,b,tc but close enough */
1545 for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1547 alphas[i] = a << (BIT_DEPTH-8);
1548 betas[i] = (i+1)/2 << (BIT_DEPTH-8);
1549 tcs[i][0] = tcs[i][3] = (c+6)/10 << (BIT_DEPTH-8);
1550 tcs[i][1] = (c+7)/15 << (BIT_DEPTH-8);
1551 tcs[i][2] = (c+9)/20 << (BIT_DEPTH-8);
1556 #define TEST_DEBLOCK( name, align, ... ) \
1557 for( int i = 0; i < 36; i++ ) \
1559 int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1560 for( int j = 0; j < 1024; j++ ) \
1561 /* two distributions of random to excersize different failure modes */ \
1562 pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \
1563 memcpy( pbuf4, pbuf3, 1024 * sizeof(pixel) ); \
1564 if( db_a.name != db_ref.name ) \
1566 set_func_name( #name ); \
1568 call_c1( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1569 call_a1( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1570 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1573 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1576 call_c2( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1577 call_a2( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1581 TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1582 TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1583 TEST_DEBLOCK( deblock_h_chroma_420, 0, tcs[i] );
1584 TEST_DEBLOCK( deblock_h_chroma_422, 0, tcs[i] );
1585 TEST_DEBLOCK( deblock_chroma_420_mbaff, 0, tcs[i] );
1586 TEST_DEBLOCK( deblock_chroma_422_mbaff, 0, tcs[i] );
1587 TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1588 TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1589 TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1590 TEST_DEBLOCK( deblock_h_chroma_420_intra, 0 );
1591 TEST_DEBLOCK( deblock_h_chroma_422_intra, 0 );
1592 TEST_DEBLOCK( deblock_chroma_420_intra_mbaff, 0 );
1593 TEST_DEBLOCK( deblock_chroma_422_intra_mbaff, 0 );
1594 TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1596 if( db_a.deblock_strength != db_ref.deblock_strength )
1598 for( int i = 0; i < 100; i++ )
1600 ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1601 ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1602 ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] );
1603 ALIGNED_ARRAY_16( uint8_t, bs, [2],[2][8][4] );
1604 memset( bs, 99, sizeof(bs) );
1605 for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1606 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1607 for( int j = 0; j < 2; j++ )
1608 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1610 ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1611 for( int l = 0; l < 2; l++ )
1612 mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1614 set_func_name( "deblock_strength" );
1615 call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1616 call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1617 if( memcmp( bs[0], bs[1], sizeof(bs[0]) ) )
1620 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1621 for( int j = 0; j < 2; j++ )
1623 for( int k = 0; k < 2; k++ )
1624 for( int l = 0; l < 4; l++ )
1626 for( int m = 0; m < 4; m++ )
1627 printf("%d ",bs[j][k][l][m]);
1637 report( "deblock :" );
1642 static int check_quant( int cpu_ref, int cpu_new )
1644 x264_quant_function_t qf_c;
1645 x264_quant_function_t qf_ref;
1646 x264_quant_function_t qf_a;
1647 ALIGNED_16( dctcoef dct1[64] );
1648 ALIGNED_16( dctcoef dct2[64] );
1649 ALIGNED_16( dctcoef dct3[8][16] );
1650 ALIGNED_16( dctcoef dct4[8][16] );
1651 ALIGNED_16( uint8_t cqm_buf[64] );
1652 int ret = 0, ok, used_asm;
1653 int oks[3] = {1,1,1}, used_asms[3] = {0,0,0};
1656 memset( h, 0, sizeof(*h) );
1657 h->sps->i_chroma_format_idc = 1;
1658 x264_param_default( &h->param );
1659 h->chroma_qp_table = i_chroma_qp_table + 12;
1660 h->param.analyse.b_transform_8x8 = 1;
1662 for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1666 for( int i = 0; i < 6; i++ )
1667 h->pps->scaling_list[i] = x264_cqm_flat16;
1668 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1670 else if( i_cqm == 1 )
1672 for( int i = 0; i < 6; i++ )
1673 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1674 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1678 int max_scale = BIT_DEPTH < 10 ? 255 : 228;
1680 for( int i = 0; i < 64; i++ )
1681 cqm_buf[i] = 10 + rand() % (max_scale - 9);
1683 for( int i = 0; i < 64; i++ )
1685 for( int i = 0; i < 6; i++ )
1686 h->pps->scaling_list[i] = cqm_buf;
1687 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1690 h->param.rc.i_qp_min = 0;
1691 h->param.rc.i_qp_max = QP_MAX;
1693 x264_quant_init( h, 0, &qf_c );
1694 x264_quant_init( h, cpu_ref, &qf_ref );
1695 x264_quant_init( h, cpu_new, &qf_a );
1697 #define INIT_QUANT8(j) \
1699 static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1700 for( int i = 0; i < 64; i++ ) \
1702 unsigned int scale = (255*scale1d[i>>3]*scale1d[i&7])/16; \
1703 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1707 #define INIT_QUANT4(j) \
1709 static const int scale1d[4] = {4,6,4,6}; \
1710 for( int i = 0; i < 16; i++ ) \
1712 unsigned int scale = 255*scale1d[i>>2]*scale1d[i&3]; \
1713 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1717 #define TEST_QUANT_DC( name, cqm ) \
1718 if( qf_a.name != qf_ref.name ) \
1720 set_func_name( #name ); \
1722 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1724 for( int j = 0; j < 2; j++ ) \
1726 int result_c, result_a; \
1727 for( int i = 0; i < 16; i++ ) \
1728 dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1729 result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1730 result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1731 if( memcmp( dct1, dct2, 16*sizeof(dctcoef) ) || result_c != result_a ) \
1734 fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1737 call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1738 call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1743 #define TEST_QUANT( qname, block, w ) \
1744 if( qf_a.qname != qf_ref.qname ) \
1746 set_func_name( #qname ); \
1748 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1750 for( int j = 0; j < 2; j++ ) \
1753 int result_c = call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1754 int result_a = call_a1( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1755 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \
1758 fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1761 call_c2( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1762 call_a2( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1767 TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1768 TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1769 TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1770 TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1771 TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1772 TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1774 #define TEST_DEQUANT( qname, dqname, block, w ) \
1775 if( qf_a.dqname != qf_ref.dqname ) \
1777 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1779 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1782 qf_c.qname( dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1783 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1784 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1785 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1786 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1789 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1792 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1793 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1797 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1798 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1799 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1800 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1802 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1803 if( qf_a.dqname != qf_ref.dqname ) \
1805 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1807 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1809 for( int i = 0; i < 16; i++ ) \
1810 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16; \
1811 qf_c.qname( dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1812 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1813 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1814 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1815 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1818 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1820 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1821 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1825 TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1827 if( qf_a.idct_dequant_2x4_dc != qf_ref.idct_dequant_2x4_dc )
1829 set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1831 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1833 for( int i = 0; i < 8; i++ )
1834 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1835 qf_c.quant_2x2_dc( &dct1[0], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
1836 qf_c.quant_2x2_dc( &dct1[4], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
1837 call_c( qf_c.idct_dequant_2x4_dc, dct1, dct3, h->dequant4_mf[CQM_4IC], qp+3 );
1838 call_a( qf_a.idct_dequant_2x4_dc, dct1, dct4, h->dequant4_mf[CQM_4IC], qp+3 );
1839 for( int i = 0; i < 8; i++ )
1840 if( dct3[i][0] != dct4[i][0] )
1843 fprintf( stderr, "idct_dequant_2x4_dc (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1849 if( qf_a.idct_dequant_2x4_dconly != qf_ref.idct_dequant_2x4_dconly )
1851 set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1853 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1855 for( int i = 0; i < 8; i++ )
1856 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1857 qf_c.quant_2x2_dc( &dct1[0], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
1858 qf_c.quant_2x2_dc( &dct1[4], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
1859 memcpy( dct2, dct1, 8*sizeof(dctcoef) );
1860 call_c1( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1861 call_a1( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1862 if( memcmp( dct1, dct2, 8*sizeof(dctcoef) ) )
1865 fprintf( stderr, "idct_dequant_2x4_dconly (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1868 call_c2( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1869 call_a2( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1873 #define TEST_OPTIMIZE_CHROMA_DC( optname, size ) \
1874 if( qf_a.optname != qf_ref.optname ) \
1876 set_func_name( #optname ); \
1878 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1880 int qpdc = qp + (size == 8 ? 3 : 0); \
1881 int dmf = h->dequant4_mf[CQM_4IC][qpdc%6][0] << qpdc/6; \
1884 for( int i = 16; ; i <<= 1 ) \
1886 int res_c, res_asm; \
1887 int max = X264_MIN( i, PIXEL_MAX*16 ); \
1888 for( int j = 0; j < size; j++ ) \
1889 dct1[j] = rand()%(max*2+1) - max; \
1890 for( int j = 0; i <= size; j += 4 ) \
1891 qf_c.quant_2x2_dc( &dct1[j], h->quant4_mf[CQM_4IC][qpdc][0]>>1, h->quant4_bias[CQM_4IC][qpdc][0]>>1 ); \
1892 memcpy( dct2, dct1, size*sizeof(dctcoef) ); \
1893 res_c = call_c1( qf_c.optname, dct1, dmf ); \
1894 res_asm = call_a1( qf_a.optname, dct2, dmf ); \
1895 if( res_c != res_asm || memcmp( dct1, dct2, size*sizeof(dctcoef) ) ) \
1898 fprintf( stderr, #optname "(qp=%d, res_c=%d, res_asm=%d): [FAILED]\n", qp, res_c, res_asm ); \
1900 call_c2( qf_c.optname, dct1, dmf ); \
1901 call_a2( qf_a.optname, dct2, dmf ); \
1902 if( i >= PIXEL_MAX*16 ) \
1908 TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x2_dc, 4 );
1909 TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x4_dc, 8 );
1911 x264_cqm_delete( h );
1914 ok = oks[0]; used_asm = used_asms[0];
1915 report( "quant :" );
1917 ok = oks[1]; used_asm = used_asms[1];
1918 report( "dequant :" );
1920 ok = oks[2]; used_asm = used_asms[2];
1921 report( "optimize chroma dc :" );
1923 ok = 1; used_asm = 0;
1924 if( qf_a.denoise_dct != qf_ref.denoise_dct )
1927 for( int size = 16; size <= 64; size += 48 )
1929 set_func_name( "denoise_dct" );
1930 memcpy( dct1, buf1, size*sizeof(dctcoef) );
1931 memcpy( dct2, buf1, size*sizeof(dctcoef) );
1932 memcpy( buf3+256, buf3, 256 );
1933 call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
1934 call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
1935 if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1937 call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
1938 call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
1941 report( "denoise dct :" );
1943 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1944 if( qf_a.decname != qf_ref.decname ) \
1946 set_func_name( #decname ); \
1948 for( int i = 0; i < 100; i++ ) \
1950 static const int distrib[16] = {1,1,1,1,1,1,1,1,1,1,1,1,2,3,4};\
1951 static const int zerorate_lut[4] = {3,7,15,31};\
1952 int zero_rate = zerorate_lut[i&3];\
1953 for( int idx = 0; idx < w*w; idx++ ) \
1955 int sign = (rand()&1) ? -1 : 1; \
1956 int abs_level = distrib[rand()&15]; \
1957 if( abs_level == 4 ) abs_level = rand()&0x3fff; \
1958 int zero = !(rand()&zero_rate); \
1959 dct1[idx] = zero * abs_level * sign; \
1963 int result_c = call_c( qf_c.decname, dct1 ); \
1964 int result_a = call_a( qf_a.decname, dct1 ); \
1965 if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1968 fprintf( stderr, #decname ": [FAILED]\n" ); \
1974 ok = 1; used_asm = 0;
1975 TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1976 TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1977 TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1978 report( "decimate_score :" );
1980 #define TEST_LAST( last, lastname, size, ac ) \
1981 if( qf_a.last != qf_ref.last ) \
1983 set_func_name( #lastname ); \
1985 for( int i = 0; i < 100; i++ ) \
1988 int max = rand() & (size-1); \
1989 memset( dct1, 0, size*sizeof(dctcoef) ); \
1990 for( int idx = ac; idx < max; idx++ ) \
1991 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1994 int result_c = call_c( qf_c.last, dct1+ac ); \
1995 int result_a = call_a( qf_a.last, dct1+ac ); \
1996 if( result_c != result_a ) \
1999 fprintf( stderr, #lastname ": [FAILED]\n" ); \
2005 ok = 1; used_asm = 0;
2006 TEST_LAST( coeff_last4 , coeff_last4, 4, 0 );
2007 TEST_LAST( coeff_last8 , coeff_last8, 8, 0 );
2008 TEST_LAST( coeff_last[ DCT_LUMA_AC], coeff_last15, 16, 1 );
2009 TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 16, 0 );
2010 TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 64, 0 );
2011 report( "coeff_last :" );
2013 #define TEST_LEVELRUN( lastname, name, size, ac ) \
2014 if( qf_a.lastname != qf_ref.lastname ) \
2016 set_func_name( #name ); \
2018 for( int i = 0; i < 100; i++ ) \
2020 x264_run_level_t runlevel_c, runlevel_a; \
2022 int max = rand() & (size-1); \
2023 memset( dct1, 0, size*sizeof(dctcoef) ); \
2024 memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
2025 memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
2026 for( int idx = ac; idx < max; idx++ ) \
2027 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2030 int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
2031 int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
2032 if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
2033 runlevel_c.mask != runlevel_a.mask || \
2034 memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c) || \
2035 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
2038 fprintf( stderr, #name ": [FAILED]\n" ); \
2044 ok = 1; used_asm = 0;
2045 TEST_LEVELRUN( coeff_level_run4 , coeff_level_run4, 4, 0 );
2046 TEST_LEVELRUN( coeff_level_run8 , coeff_level_run8, 8, 0 );
2047 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_AC], coeff_level_run15, 16, 1 );
2048 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 16, 0 );
2049 report( "coeff_level_run :" );
2054 static int check_intra( int cpu_ref, int cpu_new )
2056 int ret = 0, ok = 1, used_asm = 0;
2057 ALIGNED_ARRAY_32( pixel, edge,[36] );
2058 ALIGNED_ARRAY_32( pixel, edge2,[36] );
2059 ALIGNED_16( pixel fdec[FDEC_STRIDE*20] );
2062 x264_predict_t predict_16x16[4+3];
2063 x264_predict_t predict_8x8c[4+3];
2064 x264_predict_t predict_8x16c[4+3];
2065 x264_predict8x8_t predict_8x8[9+3];
2066 x264_predict_t predict_4x4[9+3];
2067 x264_predict_8x8_filter_t predict_8x8_filter;
2068 } ip_c, ip_ref, ip_a;
2070 x264_predict_16x16_init( 0, ip_c.predict_16x16 );
2071 x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
2072 x264_predict_8x16c_init( 0, ip_c.predict_8x16c );
2073 x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
2074 x264_predict_4x4_init( 0, ip_c.predict_4x4 );
2076 x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
2077 x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
2078 x264_predict_8x16c_init( cpu_ref, ip_ref.predict_8x16c );
2079 x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
2080 x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
2082 x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
2083 x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
2084 x264_predict_8x16c_init( cpu_new, ip_a.predict_8x16c );
2085 x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
2086 x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
2088 memcpy( fdec, pbuf1, 32*20 * sizeof(pixel) );\
2090 ip_c.predict_8x8_filter( fdec+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
2092 #define INTRA_TEST( name, dir, w, h, align, bench, ... )\
2093 if( ip_a.name[dir] != ip_ref.name[dir] )\
2095 set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
2097 memcpy( pbuf3, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2098 memcpy( pbuf4, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2099 for( int a = 0; a < (do_bench ? 64/sizeof(pixel) : 1); a += align )\
2101 call_c##bench( ip_c.name[dir], pbuf3+48+a, ##__VA_ARGS__ );\
2102 call_a##bench( ip_a.name[dir], pbuf4+48+a, ##__VA_ARGS__ );\
2103 if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*20 * sizeof(pixel) ) )\
2105 fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\
2107 for( int k = -1; k < 16; k++ )\
2108 printf( "%2x ", edge[16+k] );\
2110 for( int j = 0; j < h; j++ )\
2112 printf( "%2x ", edge[14-j] );\
2113 for( int k = 0; k < w; k++ )\
2114 printf( "%2x ", pbuf4[48+k+j*FDEC_STRIDE] );\
2118 for( int j = 0; j < h; j++ )\
2121 for( int k = 0; k < w; k++ )\
2122 printf( "%2x ", pbuf3[48+k+j*FDEC_STRIDE] );\
2130 for( int i = 0; i < 12; i++ )
2131 INTRA_TEST( predict_4x4, i, 4, 4, 4, );
2132 for( int i = 0; i < 7; i++ )
2133 INTRA_TEST( predict_8x8c, i, 8, 8, 16, );
2134 for( int i = 0; i < 7; i++ )
2135 INTRA_TEST( predict_8x16c, i, 8, 16, 16, );
2136 for( int i = 0; i < 7; i++ )
2137 INTRA_TEST( predict_16x16, i, 16, 16, 16, );
2138 for( int i = 0; i < 12; i++ )
2139 INTRA_TEST( predict_8x8, i, 8, 8, 8, , edge );
2141 set_func_name("intra_predict_8x8_filter");
2142 if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
2145 for( int i = 0; i < 32; i++ )
2147 if( !(i&7) || ((i&MB_TOPRIGHT) && !(i&MB_TOP)) )
2149 int neighbor = (i&24)>>1;
2150 memset( edge, 0, 36*sizeof(pixel) );
2151 memset( edge2, 0, 36*sizeof(pixel) );
2152 call_c( ip_c.predict_8x8_filter, pbuf1+48, edge, neighbor, i&7 );
2153 call_a( ip_a.predict_8x8_filter, pbuf1+48, edge2, neighbor, i&7 );
2154 if( !(neighbor&MB_TOPLEFT) )
2155 edge[15] = edge2[15] = 0;
2156 if( memcmp( edge+7, edge2+7, (i&MB_TOPRIGHT ? 26 : i&MB_TOP ? 17 : 8) * sizeof(pixel) ) )
2158 fprintf( stderr, "predict_8x8_filter : [FAILED] %d %d\n", (i&24)>>1, i&7);
2164 #define EXTREMAL_PLANE( w, h ) \
2167 for( int j = 0; j < 7; j++ ) \
2168 max[j] = test ? rand()&PIXEL_MAX : PIXEL_MAX; \
2169 fdec[48-1-FDEC_STRIDE] = (i&1)*max[0]; \
2170 for( int j = 0; j < w/2; j++ ) \
2171 fdec[48+j-FDEC_STRIDE] = (!!(i&2))*max[1]; \
2172 for( int j = w/2; j < w-1; j++ ) \
2173 fdec[48+j-FDEC_STRIDE] = (!!(i&4))*max[2]; \
2174 fdec[48+(w-1)-FDEC_STRIDE] = (!!(i&8))*max[3]; \
2175 for( int j = 0; j < h/2; j++ ) \
2176 fdec[48+j*FDEC_STRIDE-1] = (!!(i&16))*max[4]; \
2177 for( int j = h/2; j < h-1; j++ ) \
2178 fdec[48+j*FDEC_STRIDE-1] = (!!(i&32))*max[5]; \
2179 fdec[48+(h-1)*FDEC_STRIDE-1] = (!!(i&64))*max[6]; \
2181 /* Extremal test case for planar prediction. */
2182 for( int test = 0; test < 100 && ok; test++ )
2183 for( int i = 0; i < 128 && ok; i++ )
2185 EXTREMAL_PLANE( 8, 8 );
2186 INTRA_TEST( predict_8x8c, I_PRED_CHROMA_P, 8, 8, 64, 1 );
2187 EXTREMAL_PLANE( 8, 16 );
2188 INTRA_TEST( predict_8x16c, I_PRED_CHROMA_P, 8, 16, 64, 1 );
2189 EXTREMAL_PLANE( 16, 16 );
2190 INTRA_TEST( predict_16x16, I_PRED_16x16_P, 16, 16, 64, 1 );
2192 report( "intra pred :" );
2196 #define DECL_CABAC(cpu) \
2197 static void run_cabac_decision_##cpu( x264_t *h, uint8_t *dst )\
2200 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2201 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2202 for( int i = 0; i < 0x1000; i++ )\
2203 x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
2205 static void run_cabac_bypass_##cpu( x264_t *h, uint8_t *dst )\
2208 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2209 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2210 for( int i = 0; i < 0x1000; i++ )\
2211 x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
2213 static void run_cabac_terminal_##cpu( x264_t *h, uint8_t *dst )\
2216 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2217 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2218 for( int i = 0; i < 0x1000; i++ )\
2219 x264_cabac_encode_terminal_##cpu( &cb );\
2225 #define run_cabac_decision_asm run_cabac_decision_c
2226 #define run_cabac_bypass_asm run_cabac_bypass_c
2227 #define run_cabac_terminal_asm run_cabac_terminal_c
2230 static int check_cabac( int cpu_ref, int cpu_new )
2232 int ret = 0, ok, used_asm = 1;
2234 h.sps->i_chroma_format_idc = 3;
2235 if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
2237 x264_cabac_init( &h );
2239 set_func_name( "cabac_encode_decision" );
2240 memcpy( buf4, buf3, 0x1000 );
2241 call_c( run_cabac_decision_c, &h, buf3 );
2242 call_a( run_cabac_decision_asm, &h, buf4 );
2243 ok = !memcmp( buf3, buf4, 0x1000 );
2244 report( "cabac decision:" );
2246 set_func_name( "cabac_encode_bypass" );
2247 memcpy( buf4, buf3, 0x1000 );
2248 call_c( run_cabac_bypass_c, &h, buf3 );
2249 call_a( run_cabac_bypass_asm, &h, buf4 );
2250 ok = !memcmp( buf3, buf4, 0x1000 );
2251 report( "cabac bypass:" );
2253 set_func_name( "cabac_encode_terminal" );
2254 memcpy( buf4, buf3, 0x1000 );
2255 call_c( run_cabac_terminal_c, &h, buf3 );
2256 call_a( run_cabac_terminal_asm, &h, buf4 );
2257 ok = !memcmp( buf3, buf4, 0x1000 );
2258 report( "cabac terminal:" );
2263 static int check_bitstream( int cpu_ref, int cpu_new )
2265 x264_bitstream_function_t bs_c;
2266 x264_bitstream_function_t bs_ref;
2267 x264_bitstream_function_t bs_a;
2269 int ret = 0, ok = 1, used_asm = 0;
2271 x264_bitstream_init( 0, &bs_c );
2272 x264_bitstream_init( cpu_ref, &bs_ref );
2273 x264_bitstream_init( cpu_new, &bs_a );
2274 if( bs_a.nal_escape != bs_ref.nal_escape )
2277 uint8_t *input = malloc(size+100);
2278 uint8_t *output1 = malloc(size*2);
2279 uint8_t *output2 = malloc(size*2);
2281 set_func_name( "nal_escape" );
2282 for( int i = 0; i < 100; i++ )
2284 /* Test corner-case sizes */
2285 int test_size = i < 10 ? i+1 : rand() & 0x3fff;
2286 /* Test 8 different probability distributions of zeros */
2287 for( int j = 0; j < test_size+32; j++ )
2288 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
2289 uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
2290 uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
2291 int size_c = end_c-output1;
2292 int size_a = end_a-output2;
2293 if( size_c != size_a || memcmp( output1, output2, size_c ) )
2295 fprintf( stderr, "nal_escape : [FAILED] %d %d\n", size_c, size_a );
2300 for( int j = 0; j < size+32; j++ )
2302 call_c2( bs_c.nal_escape, output1, input, input+size );
2303 call_a2( bs_a.nal_escape, output2, input, input+size );
2308 report( "nal escape:" );
2313 static int check_all_funcs( int cpu_ref, int cpu_new )
2315 return check_pixel( cpu_ref, cpu_new )
2316 + check_dct( cpu_ref, cpu_new )
2317 + check_mc( cpu_ref, cpu_new )
2318 + check_intra( cpu_ref, cpu_new )
2319 + check_deblock( cpu_ref, cpu_new )
2320 + check_quant( cpu_ref, cpu_new )
2321 + check_cabac( cpu_ref, cpu_new )
2322 + check_bitstream( cpu_ref, cpu_new );
2325 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
2327 *cpu_ref = *cpu_new;
2329 #if BROKEN_STACK_ALIGNMENT
2330 *cpu_new |= X264_CPU_STACK_MOD4;
2332 if( *cpu_new & X264_CPU_SSE2_IS_FAST )
2333 *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
2335 fprintf( stderr, "x264: %s\n", name );
2336 return check_all_funcs( *cpu_ref, *cpu_new );
2339 static int check_all_flags( void )
2342 int cpu0 = 0, cpu1 = 0;
2344 if( x264_cpu_detect() & X264_CPU_MMX2 )
2346 ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMX2, "MMX" );
2347 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
2348 cpu1 &= ~X264_CPU_CACHELINE_64;
2350 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
2351 cpu1 &= ~X264_CPU_CACHELINE_32;
2353 if( x264_cpu_detect() & X264_CPU_LZCNT )
2355 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
2356 cpu1 &= ~X264_CPU_LZCNT;
2358 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
2359 cpu1 &= ~X264_CPU_SLOW_CTZ;
2361 if( x264_cpu_detect() & X264_CPU_SSE2 )
2363 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
2364 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
2365 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
2366 cpu1 &= ~X264_CPU_CACHELINE_64;
2367 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSE2 FastShuffle" );
2368 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
2369 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
2370 cpu1 &= ~X264_CPU_SLOW_CTZ;
2371 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSE2 SlowAtom" );
2372 cpu1 &= ~X264_CPU_SLOW_ATOM;
2374 if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
2376 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
2377 cpu1 &= ~X264_CPU_SSE_MISALIGN;
2379 if( x264_cpu_detect() & X264_CPU_LZCNT )
2381 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
2382 cpu1 &= ~X264_CPU_LZCNT;
2384 if( x264_cpu_detect() & X264_CPU_SSE3 )
2386 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
2387 cpu1 &= ~X264_CPU_CACHELINE_64;
2389 if( x264_cpu_detect() & X264_CPU_SSSE3 )
2391 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
2392 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
2393 cpu1 &= ~X264_CPU_CACHELINE_64;
2394 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSSE3 FastShuffle" );
2395 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
2396 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
2397 cpu1 &= ~X264_CPU_SLOW_CTZ;
2398 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
2399 cpu1 &= ~X264_CPU_SLOW_ATOM;
2401 if( x264_cpu_detect() & X264_CPU_SSE4 )
2402 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4 | X264_CPU_SHUFFLE_IS_FAST, "SSE4" );
2403 if( x264_cpu_detect() & X264_CPU_AVX )
2404 ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX, "AVX" );
2405 if( x264_cpu_detect() & X264_CPU_XOP )
2406 ret |= add_flags( &cpu0, &cpu1, X264_CPU_XOP, "XOP" );
2407 if( x264_cpu_detect() & X264_CPU_FMA4 )
2408 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA4, "FMA4" );
2410 if( x264_cpu_detect() & X264_CPU_ALTIVEC )
2412 fprintf( stderr, "x264: ALTIVEC against C\n" );
2413 ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
2416 if( x264_cpu_detect() & X264_CPU_ARMV6 )
2417 ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
2418 if( x264_cpu_detect() & X264_CPU_NEON )
2419 ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
2420 if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC )
2421 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
2426 int main(int argc, char *argv[])
2430 if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
2432 #if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM
2433 fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
2437 if( argv[1][7] == '=' )
2439 bench_pattern = argv[1]+8;
2440 bench_pattern_len = strlen(bench_pattern);
2446 int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
2447 fprintf( stderr, "x264: using random seed %u\n", seed );
2450 buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 16*BENCH_ALIGNS );
2451 pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 16*BENCH_ALIGNS );
2452 if( !buf1 || !pbuf1 )
2454 fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
2457 #define INIT_POINTER_OFFSETS\
2458 buf2 = buf1 + 0xf00;\
2459 buf3 = buf2 + 0xf00;\
2460 buf4 = buf3 + 0x1000*sizeof(pixel);\
2461 pbuf2 = pbuf1 + 0xf00;\
2462 pbuf3 = (pixel*)buf3;\
2463 pbuf4 = (pixel*)buf4;
2464 INIT_POINTER_OFFSETS;
2465 for( int i = 0; i < 0x1e00; i++ )
2467 buf1[i] = rand() & 0xFF;
2468 pbuf1[i] = rand() & PIXEL_MAX;
2470 memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) );
2472 /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
2474 for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
2476 INIT_POINTER_OFFSETS;
2477 ret |= x264_stack_pagealign( check_all_flags, i*16 );
2481 fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
2484 ret = check_all_flags();
2488 fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
2491 fprintf( stderr, "x264: All tests passed Yeah :)\n" );