1 /*****************************************************************************
2 * checkasm.c: assembly check tool
3 *****************************************************************************
4 * Copyright (C) 2003-2015 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( "lfence \n"
95 : "=a"(a) :: "edx", "memory" );
97 asm volatile( "mftb %0" : "=r"(a) :: "memory" );
98 #elif ARCH_ARM // ARMv7 only
99 asm volatile( "mrc p15, 0, %0, c9, c13, 0" : "=r"(a) :: "memory" );
102 asm volatile( "mrs %0, pmccntr_el0" : "=r"(b) :: "memory" );
105 asm volatile( "rdhwr %0, $2" : "=r"(a) :: "memory" );
110 static bench_t* get_bench( const char *name, int cpu )
113 for( i = 0; benchs[i].name && strcmp(name, benchs[i].name); i++ )
114 assert( i < MAX_FUNCS );
115 if( !benchs[i].name )
116 benchs[i].name = strdup( name );
118 return &benchs[i].vers[0];
119 for( j = 1; benchs[i].vers[j].cpu && benchs[i].vers[j].cpu != cpu; j++ )
120 assert( j < MAX_CPUS );
121 benchs[i].vers[j].cpu = cpu;
122 return &benchs[i].vers[j];
125 static int cmp_nop( const void *a, const void *b )
127 return *(uint16_t*)a - *(uint16_t*)b;
130 static int cmp_bench( const void *a, const void *b )
132 // asciibetical sort except preserving numbers
133 const char *sa = ((bench_func_t*)a)->name;
134 const char *sb = ((bench_func_t*)b)->name;
139 if( isdigit( *sa ) && isdigit( *sb ) && isdigit( sa[1] ) != isdigit( sb[1] ) )
140 return isdigit( sa[1] ) - isdigit( sb[1] );
146 static void print_bench(void)
148 uint16_t nops[10000];
149 int nfuncs, nop_time=0;
151 for( int i = 0; i < 10000; i++ )
153 uint32_t t = read_time();
154 nops[i] = read_time() - t;
156 qsort( nops, 10000, sizeof(uint16_t), cmp_nop );
157 for( int i = 500; i < 9500; i++ )
160 printf( "nop: %d\n", nop_time );
162 for( nfuncs = 0; nfuncs < MAX_FUNCS && benchs[nfuncs].name; nfuncs++ );
163 qsort( benchs, nfuncs, sizeof(bench_func_t), cmp_bench );
164 for( int i = 0; i < nfuncs; i++ )
165 for( int j = 0; j < MAX_CPUS && (!j || benchs[i].vers[j].cpu); j++ )
168 bench_t *b = &benchs[i].vers[j];
171 for( k = 0; k < j && benchs[i].vers[k].pointer != b->pointer; k++ );
174 printf( "%s_%s%s: %"PRId64"\n", benchs[i].name,
176 b->cpu&X264_CPU_AVX2 ? "avx2" :
177 b->cpu&X264_CPU_FMA3 ? "fma3" :
178 b->cpu&X264_CPU_FMA4 ? "fma4" :
179 b->cpu&X264_CPU_XOP ? "xop" :
180 b->cpu&X264_CPU_AVX ? "avx" :
181 b->cpu&X264_CPU_SSE42 ? "sse42" :
182 b->cpu&X264_CPU_SSE4 ? "sse4" :
183 b->cpu&X264_CPU_SSSE3 ? "ssse3" :
184 b->cpu&X264_CPU_SSE3 ? "sse3" :
185 /* print sse2slow only if there's also a sse2fast version of the same func */
186 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" :
187 b->cpu&X264_CPU_SSE2 ? "sse2" :
188 b->cpu&X264_CPU_SSE ? "sse" :
189 b->cpu&X264_CPU_MMX ? "mmx" :
191 b->cpu&X264_CPU_ALTIVEC ? "altivec" :
193 b->cpu&X264_CPU_NEON ? "neon" :
194 b->cpu&X264_CPU_ARMV6 ? "armv6" :
196 b->cpu&X264_CPU_NEON ? "neon" :
197 b->cpu&X264_CPU_ARMV8 ? "armv8" :
199 b->cpu&X264_CPU_MSA ? "msa" :
203 b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
204 b->cpu&X264_CPU_SLOW_ATOM && b->cpu&X264_CPU_CACHELINE_64 ? "_c64_atom" :
205 b->cpu&X264_CPU_CACHELINE_64 ? "_c64" :
206 b->cpu&X264_CPU_SLOW_SHUFFLE ? "_slowshuffle" :
207 b->cpu&X264_CPU_LZCNT ? "_lzcnt" :
208 b->cpu&X264_CPU_BMI2 ? "_bmi2" :
209 b->cpu&X264_CPU_BMI1 ? "_bmi1" :
210 b->cpu&X264_CPU_SLOW_CTZ ? "_slow_ctz" :
211 b->cpu&X264_CPU_SLOW_ATOM ? "_atom" :
213 b->cpu&X264_CPU_FAST_NEON_MRC ? "_fast_mrc" :
216 (int64_t)(10*b->cycles/b->den - nop_time)/4 );
220 #if ARCH_X86 || ARCH_X86_64
221 int x264_stack_pagealign( int (*func)(), int align );
223 /* detect when callee-saved regs aren't saved
224 * needs an explicit asm check because it only sometimes crashes in normal use. */
225 intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... );
227 #define x264_stack_pagealign( func, align ) func()
230 #define call_c1(func,...) func(__VA_ARGS__)
233 /* Evil hack: detect incorrect assumptions that 32-bit ints are zero-extended to 64-bit.
234 * This is done by clobbering the stack with junk around the stack pointer and calling the
235 * assembly function through x264_checkasm_call with added dummy arguments which forces all
236 * real arguments to be passed on the stack and not in registers. For 32-bit argument the
237 * upper half of the 64-bit register location on the stack will now contain junk. Note that
238 * this is dependant on compiler behaviour and that interrupts etc. at the wrong time may
239 * overwrite the junk written to the stack so there's no guarantee that it will always
240 * detect all functions that assumes zero-extension.
242 void x264_checkasm_stack_clobber( uint64_t clobber, ... );
243 #define call_a1(func,...) ({ \
244 uint64_t r = (rand() & 0xffff) * 0x0001000100010001ULL; \
245 x264_checkasm_stack_clobber( r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r,r ); /* max_args+6 */ \
246 x264_checkasm_call(( intptr_t(*)())func, &ok, 0, 0, 0, 0, __VA_ARGS__ ); })
248 #define call_a1(func,...) x264_checkasm_call( (intptr_t(*)())func, &ok, __VA_ARGS__ )
250 #define call_a1 call_c1
253 #define call_bench(func,cpu,...)\
254 if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
258 call_a1(func, __VA_ARGS__);\
259 for( int ti = 0; ti < (cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
261 uint32_t t = read_time();\
266 t = read_time() - t;\
267 if( (uint64_t)t*tcount <= tsum*4 && ti > 0 )\
273 bench_t *b = get_bench( func_name, cpu );\
279 /* for most functions, run benchmark and correctness test at the same time.
280 * for those that modify their inputs, run the above macros separately */
281 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
282 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
283 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
284 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
287 static int check_pixel( int cpu_ref, int cpu_new )
289 x264_pixel_function_t pixel_c;
290 x264_pixel_function_t pixel_ref;
291 x264_pixel_function_t pixel_asm;
292 x264_predict_t predict_4x4[12];
293 x264_predict8x8_t predict_8x8[12];
294 x264_predict_8x8_filter_t predict_8x8_filter;
295 ALIGNED_16( pixel edge[36] );
296 uint16_t cost_mv[32];
297 int ret = 0, ok, used_asm;
299 x264_pixel_init( 0, &pixel_c );
300 x264_pixel_init( cpu_ref, &pixel_ref );
301 x264_pixel_init( cpu_new, &pixel_asm );
302 x264_predict_4x4_init( 0, predict_4x4 );
303 x264_predict_8x8_init( 0, predict_8x8, &predict_8x8_filter );
304 predict_8x8_filter( pbuf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
307 for( int i = 0; i < 256; i++ )
312 pbuf4[i] = -(z&1) & PIXEL_MAX;
313 pbuf3[i] = ~pbuf4[i] & PIXEL_MAX;
315 // random pattern made of maxed pixel differences, in case an intermediate value overflows
316 for( int i = 256; i < 0x1000; i++ )
318 pbuf4[i] = -(pbuf1[i&~0x88]&1) & PIXEL_MAX;
319 pbuf3[i] = ~(pbuf4[i]) & PIXEL_MAX;
322 #define TEST_PIXEL( name, align ) \
323 ok = 1, used_asm = 0; \
324 for( int i = 0; i < ARRAY_ELEMS(pixel_c.name); i++ ) \
326 int res_c, res_asm; \
327 if( pixel_asm.name[i] != pixel_ref.name[i] ) \
329 set_func_name( "%s_%s", #name, pixel_names[i] ); \
331 for( int j = 0; j < 64; j++ ) \
333 res_c = call_c( pixel_c.name[i], pbuf1, (intptr_t)16, pbuf2+j*!align, (intptr_t)64 ); \
334 res_asm = call_a( pixel_asm.name[i], pbuf1, (intptr_t)16, pbuf2+j*!align, (intptr_t)64 ); \
335 if( res_c != res_asm ) \
338 fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
342 for( int j = 0; j < 0x1000 && ok; j += 256 ) \
344 res_c = pixel_c .name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
345 res_asm = pixel_asm.name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
346 if( res_c != res_asm ) \
349 fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
354 report( "pixel " #name " :" );
356 TEST_PIXEL( sad, 0 );
357 TEST_PIXEL( sad_aligned, 1 );
358 TEST_PIXEL( ssd, 1 );
359 TEST_PIXEL( satd, 0 );
360 TEST_PIXEL( sa8d, 1 );
362 ok = 1, used_asm = 0;
363 if( pixel_asm.sa8d_satd[PIXEL_16x16] != pixel_ref.sa8d_satd[PIXEL_16x16] )
365 set_func_name( "sa8d_satd_%s", pixel_names[PIXEL_16x16] );
367 for( int j = 0; j < 64; j++ )
369 uint32_t cost8_c = pixel_c.sa8d[PIXEL_16x16]( pbuf1, 16, pbuf2, 64 );
370 uint32_t cost4_c = pixel_c.satd[PIXEL_16x16]( pbuf1, 16, pbuf2, 64 );
371 uint64_t res_a = call_a( pixel_asm.sa8d_satd[PIXEL_16x16], pbuf1, (intptr_t)16, pbuf2, (intptr_t)64 );
372 uint32_t cost8_a = res_a;
373 uint32_t cost4_a = res_a >> 32;
374 if( cost8_a != cost8_c || cost4_a != cost4_c )
377 fprintf( stderr, "sa8d_satd [%d]: (%d,%d) != (%d,%d) [FAILED]\n", PIXEL_16x16,
378 cost8_c, cost4_c, cost8_a, cost4_a );
382 for( int j = 0; j < 0x1000 && ok; j += 256 ) \
384 uint32_t cost8_c = pixel_c.sa8d[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 );
385 uint32_t cost4_c = pixel_c.satd[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 );
386 uint64_t res_a = pixel_asm.sa8d_satd[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 );
387 uint32_t cost8_a = res_a;
388 uint32_t cost4_a = res_a >> 32;
389 if( cost8_a != cost8_c || cost4_a != cost4_c )
392 fprintf( stderr, "sa8d_satd [%d]: overflow (%d,%d) != (%d,%d) [FAILED]\n", PIXEL_16x16,
393 cost8_c, cost4_c, cost8_a, cost4_a );
397 report( "pixel sa8d_satd :" );
399 #define TEST_PIXEL_X( N ) \
400 ok = 1; used_asm = 0; \
401 for( int i = 0; i < 7; i++ ) \
403 ALIGNED_16( int res_c[4] ) = {0}; \
404 ALIGNED_16( int res_asm[4] ) = {0}; \
405 if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
407 set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
409 for( int j = 0; j < 64; j++ ) \
411 pixel *pix2 = pbuf2+j; \
412 res_c[0] = pixel_c.sad[i]( pbuf1, 16, pix2, 64 ); \
413 res_c[1] = pixel_c.sad[i]( pbuf1, 16, pix2+6, 64 ); \
414 res_c[2] = pixel_c.sad[i]( pbuf1, 16, pix2+1, 64 ); \
417 res_c[3] = pixel_c.sad[i]( pbuf1, 16, pix2+10, 64 ); \
418 call_a( pixel_asm.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, (intptr_t)64, res_asm ); \
421 call_a( pixel_asm.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, (intptr_t)64, res_asm ); \
422 if( memcmp(res_c, res_asm, N*sizeof(int)) ) \
425 fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
426 i, res_c[0], res_c[1], res_c[2], res_c[3], \
427 res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
430 call_c2( pixel_c.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, (intptr_t)64, res_asm ); \
432 call_c2( pixel_c.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, (intptr_t)64, res_asm ); \
436 report( "pixel sad_x"#N" :" );
441 #define TEST_PIXEL_VAR( i ) \
442 if( pixel_asm.var[i] != pixel_ref.var[i] ) \
444 set_func_name( "%s_%s", "var", pixel_names[i] ); \
446 /* abi-check wrapper can't return uint64_t, so separate it from return value check */ \
447 call_c1( pixel_c.var[i], pbuf1, 16 ); \
448 call_a1( pixel_asm.var[i], pbuf1, (intptr_t)16 ); \
449 uint64_t res_c = pixel_c.var[i]( pbuf1, 16 ); \
450 uint64_t res_asm = pixel_asm.var[i]( pbuf1, 16 ); \
451 if( res_c != res_asm ) \
454 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) ); \
456 call_c2( pixel_c.var[i], pbuf1, (intptr_t)16 ); \
457 call_a2( pixel_asm.var[i], pbuf1, (intptr_t)16 ); \
460 ok = 1; used_asm = 0;
461 TEST_PIXEL_VAR( PIXEL_16x16 );
462 TEST_PIXEL_VAR( PIXEL_8x16 );
463 TEST_PIXEL_VAR( PIXEL_8x8 );
464 report( "pixel var :" );
466 #define TEST_PIXEL_VAR2( i ) \
467 if( pixel_asm.var2[i] != pixel_ref.var2[i] ) \
469 int res_c, res_asm, ssd_c, ssd_asm; \
470 set_func_name( "%s_%s", "var2", pixel_names[i] ); \
472 res_c = call_c( pixel_c.var2[i], pbuf1, (intptr_t)16, pbuf2, (intptr_t)16, &ssd_c ); \
473 res_asm = call_a( pixel_asm.var2[i], pbuf1, (intptr_t)16, pbuf2, (intptr_t)16, &ssd_asm ); \
474 if( res_c != res_asm || ssd_c != ssd_asm ) \
477 fprintf( stderr, "var2[%d]: %d != %d or %d != %d [FAILED]\n", i, res_c, res_asm, ssd_c, ssd_asm ); \
481 ok = 1; used_asm = 0;
482 TEST_PIXEL_VAR2( PIXEL_8x16 );
483 TEST_PIXEL_VAR2( PIXEL_8x8 );
484 report( "pixel var2 :" );
486 ok = 1; used_asm = 0;
487 for( int i = 0; i < 4; i++ )
488 if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
490 set_func_name( "hadamard_ac_%s", pixel_names[i] );
492 for( int j = 0; j < 32; j++ )
494 pixel *pix = (j&16 ? pbuf1 : pbuf3) + (j&15)*256;
495 call_c1( pixel_c.hadamard_ac[i], pbuf1, (intptr_t)16 );
496 call_a1( pixel_asm.hadamard_ac[i], pbuf1, (intptr_t)16 );
497 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
498 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
502 fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
506 call_c2( pixel_c.hadamard_ac[i], pbuf1, (intptr_t)16 );
507 call_a2( pixel_asm.hadamard_ac[i], pbuf1, (intptr_t)16 );
509 report( "pixel hadamard_ac :" );
512 for( int i = 0; i < 32; i++ )
513 for( int j = 0; j < 16; j++ )
514 pbuf4[16*i+j] = -((i+j)&1) & PIXEL_MAX;
515 ok = 1; used_asm = 0;
516 if( pixel_asm.vsad != pixel_ref.vsad )
518 for( int h = 2; h <= 32; h += 2 )
521 set_func_name( "vsad" );
523 for( int j = 0; j < 2 && ok; j++ )
525 pixel *p = j ? pbuf4 : pbuf1;
526 res_c = call_c( pixel_c.vsad, p, (intptr_t)16, h );
527 res_asm = call_a( pixel_asm.vsad, p, (intptr_t)16, h );
528 if( res_c != res_asm )
531 fprintf( stderr, "vsad: height=%d, %d != %d\n", h, res_c, res_asm );
537 report( "pixel vsad :" );
539 ok = 1; used_asm = 0;
540 if( pixel_asm.asd8 != pixel_ref.asd8 )
542 set_func_name( "asd8" );
544 int res_c = call_c( pixel_c.asd8, pbuf1, (intptr_t)8, pbuf2, (intptr_t)8, 16 );
545 int res_a = call_a( pixel_asm.asd8, pbuf1, (intptr_t)8, pbuf2, (intptr_t)8, 16 );
549 fprintf( stderr, "asd: %d != %d\n", res_c, res_a );
552 report( "pixel asd :" );
554 #define TEST_INTRA_X3( name, i8x8, ... ) \
555 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
557 ALIGNED_16( int res_c[3] ); \
558 ALIGNED_16( int res_asm[3] ); \
559 set_func_name( #name ); \
561 call_c( pixel_c.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_c ); \
562 call_a( pixel_asm.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_asm ); \
563 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
566 fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
567 res_c[0], res_c[1], res_c[2], \
568 res_asm[0], res_asm[1], res_asm[2] ); \
572 #define TEST_INTRA_X9( name, cmp ) \
573 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
575 set_func_name( #name ); \
577 ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
578 for( int i=0; i<17; i++ ) \
579 bitcosts[i] = 9*(i!=8); \
580 memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
581 memcpy( pbuf4, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
582 for( int i=0; i<32; i++ ) \
584 pixel *fenc = pbuf1+48+i*12; \
585 pixel *fdec1 = pbuf3+48+i*12; \
586 pixel *fdec2 = pbuf4+48+i*12; \
587 int pred_mode = i%9; \
588 int res_c = INT_MAX; \
589 for( int j=0; j<9; j++ ) \
591 predict_4x4[j]( fdec1 ); \
592 int cost = pixel_c.cmp[PIXEL_4x4]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
593 if( cost < (uint16_t)res_c ) \
594 res_c = cost + (j<<16); \
596 predict_4x4[res_c>>16]( fdec1 ); \
597 int res_a = call_a( pixel_asm.name, fenc, fdec2, bitcosts+8-pred_mode ); \
598 if( res_c != res_a ) \
601 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
604 if( memcmp(fdec1, fdec2, 4*FDEC_STRIDE*sizeof(pixel)) ) \
607 fprintf( stderr, #name" [FAILED]\n" ); \
608 for( int j=0; j<16; j++ ) \
609 fprintf( stderr, "%02x ", fdec1[(j&3)+(j>>2)*FDEC_STRIDE] ); \
610 fprintf( stderr, "\n" ); \
611 for( int j=0; j<16; j++ ) \
612 fprintf( stderr, "%02x ", fdec2[(j&3)+(j>>2)*FDEC_STRIDE] ); \
613 fprintf( stderr, "\n" ); \
619 #define TEST_INTRA8_X9( name, cmp ) \
620 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
622 set_func_name( #name ); \
624 ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
625 ALIGNED_ARRAY_16( uint16_t, satds_c,[16] ); \
626 ALIGNED_ARRAY_16( uint16_t, satds_a,[16] ); \
627 memset( satds_c, 0, 16 * sizeof(*satds_c) ); \
628 memset( satds_a, 0, 16 * sizeof(*satds_a) ); \
629 for( int i=0; i<17; i++ ) \
630 bitcosts[i] = 9*(i!=8); \
631 for( int i=0; i<32; i++ ) \
633 pixel *fenc = pbuf1+48+i*12; \
634 pixel *fdec1 = pbuf3+48+i*12; \
635 pixel *fdec2 = pbuf4+48+i*12; \
636 int pred_mode = i%9; \
637 int res_c = INT_MAX; \
638 predict_8x8_filter( fdec1, edge, ALL_NEIGHBORS, ALL_NEIGHBORS ); \
639 for( int j=0; j<9; j++ ) \
641 predict_8x8[j]( fdec1, edge ); \
642 satds_c[j] = pixel_c.cmp[PIXEL_8x8]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
643 if( satds_c[j] < (uint16_t)res_c ) \
644 res_c = satds_c[j] + (j<<16); \
646 predict_8x8[res_c>>16]( fdec1, edge ); \
647 int res_a = call_a( pixel_asm.name, fenc, fdec2, edge, bitcosts+8-pred_mode, satds_a ); \
648 if( res_c != res_a || memcmp(satds_c, satds_a, 16 * sizeof(*satds_c)) ) \
651 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
652 for( int j = 0; j < 9; j++ ) \
653 fprintf( stderr, "%5d ", satds_c[j]); \
654 fprintf( stderr, "\n" ); \
655 for( int j = 0; j < 9; j++ ) \
656 fprintf( stderr, "%5d ", satds_a[j]); \
657 fprintf( stderr, "\n" ); \
660 for( int j=0; j<8; j++ ) \
661 if( memcmp(fdec1+j*FDEC_STRIDE, fdec2+j*FDEC_STRIDE, 8*sizeof(pixel)) ) \
665 fprintf( stderr, #name" [FAILED]\n" ); \
666 for( int j=0; j<8; j++ ) \
668 for( int k=0; k<8; k++ ) \
669 fprintf( stderr, "%02x ", fdec1[k+j*FDEC_STRIDE] ); \
670 fprintf( stderr, "\n" ); \
672 fprintf( stderr, "\n" ); \
673 for( int j=0; j<8; j++ ) \
675 for( int k=0; k<8; k++ ) \
676 fprintf( stderr, "%02x ", fdec2[k+j*FDEC_STRIDE] ); \
677 fprintf( stderr, "\n" ); \
679 fprintf( stderr, "\n" ); \
685 memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) );
686 ok = 1; used_asm = 0;
687 TEST_INTRA_X3( intra_satd_x3_16x16, 0 );
688 TEST_INTRA_X3( intra_satd_x3_8x16c, 0 );
689 TEST_INTRA_X3( intra_satd_x3_8x8c, 0 );
690 TEST_INTRA_X3( intra_sa8d_x3_8x8, 1, edge );
691 TEST_INTRA_X3( intra_satd_x3_4x4, 0 );
692 report( "intra satd_x3 :" );
693 ok = 1; used_asm = 0;
694 TEST_INTRA_X3( intra_sad_x3_16x16, 0 );
695 TEST_INTRA_X3( intra_sad_x3_8x16c, 0 );
696 TEST_INTRA_X3( intra_sad_x3_8x8c, 0 );
697 TEST_INTRA_X3( intra_sad_x3_8x8, 1, edge );
698 TEST_INTRA_X3( intra_sad_x3_4x4, 0 );
699 report( "intra sad_x3 :" );
700 ok = 1; used_asm = 0;
701 TEST_INTRA_X9( intra_satd_x9_4x4, satd );
702 TEST_INTRA8_X9( intra_sa8d_x9_8x8, sa8d );
703 report( "intra satd_x9 :" );
704 ok = 1; used_asm = 0;
705 TEST_INTRA_X9( intra_sad_x9_4x4, sad );
706 TEST_INTRA8_X9( intra_sad_x9_8x8, sad );
707 report( "intra sad_x9 :" );
709 ok = 1; used_asm = 0;
710 if( pixel_asm.ssd_nv12_core != pixel_ref.ssd_nv12_core )
713 set_func_name( "ssd_nv12" );
714 uint64_t res_u_c, res_v_c, res_u_a, res_v_a;
715 pixel_c.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c );
716 pixel_asm.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a );
717 if( res_u_c != res_u_a || res_v_c != res_v_a )
720 fprintf( stderr, "ssd_nv12: %"PRIu64",%"PRIu64" != %"PRIu64",%"PRIu64"\n",
721 res_u_c, res_v_c, res_u_a, res_v_a );
723 call_c( pixel_c.ssd_nv12_core, pbuf1, (intptr_t)368, pbuf2, (intptr_t)368, 360, 8, &res_u_c, &res_v_c );
724 call_a( pixel_asm.ssd_nv12_core, pbuf1, (intptr_t)368, pbuf2, (intptr_t)368, 360, 8, &res_u_a, &res_v_a );
726 report( "ssd_nv12 :" );
728 if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
729 pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
733 ALIGNED_16( int sums[5][4] ) = {{0}};
736 res_c = x264_pixel_ssim_wxh( &pixel_c, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
737 res_a = x264_pixel_ssim_wxh( &pixel_asm, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
738 if( fabs( res_c - res_a ) > 1e-6 )
741 fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
743 set_func_name( "ssim_core" );
744 call_c( pixel_c.ssim_4x4x2_core, pbuf1+2, (intptr_t)32, pbuf2+2, (intptr_t)32, sums );
745 call_a( pixel_asm.ssim_4x4x2_core, pbuf1+2, (intptr_t)32, pbuf2+2, (intptr_t)32, sums );
746 set_func_name( "ssim_end" );
747 call_c2( pixel_c.ssim_end4, sums, sums, 4 );
748 call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
749 /* check incorrect assumptions that 32-bit ints are zero-extended to 64-bit */
750 call_c1( pixel_c.ssim_end4, sums, sums, 3 );
751 call_a1( pixel_asm.ssim_end4, sums, sums, 3 );
755 ok = 1; used_asm = 0;
756 for( int i = 0; i < 32; i++ )
758 for( int i = 0; i < 100 && ok; i++ )
759 if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
761 ALIGNED_16( uint16_t sums[72] );
762 ALIGNED_16( int dc[4] );
763 ALIGNED_16( int16_t mvs_a[48] );
764 ALIGNED_16( int16_t mvs_c[48] );
766 int thresh = rand() & 0x3fff;
767 set_func_name( "esa_ads" );
768 for( int j = 0; j < 72; j++ )
769 sums[j] = rand() & 0x3fff;
770 for( int j = 0; j < 4; j++ )
771 dc[j] = rand() & 0x3fff;
773 mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
774 mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
775 if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
778 printf( "c%d: ", i&3 );
779 for( int j = 0; j < mvn_c; j++ )
780 printf( "%d ", mvs_c[j] );
781 printf( "\na%d: ", i&3 );
782 for( int j = 0; j < mvn_a; j++ )
783 printf( "%d ", mvs_a[j] );
787 report( "esa ads:" );
792 static int check_dct( int cpu_ref, int cpu_new )
794 x264_dct_function_t dct_c;
795 x264_dct_function_t dct_ref;
796 x264_dct_function_t dct_asm;
797 x264_quant_function_t qf;
798 int ret = 0, ok, used_asm, interlace = 0;
799 ALIGNED_ARRAY_N( dctcoef, dct1, [16],[16] );
800 ALIGNED_ARRAY_N( dctcoef, dct2, [16],[16] );
801 ALIGNED_ARRAY_N( dctcoef, dct4, [16],[16] );
802 ALIGNED_ARRAY_N( dctcoef, dct8, [4],[64] );
803 ALIGNED_16( dctcoef dctdc[2][8] );
807 x264_dct_init( 0, &dct_c );
808 x264_dct_init( cpu_ref, &dct_ref);
809 x264_dct_init( cpu_new, &dct_asm );
811 memset( h, 0, sizeof(*h) );
812 x264_param_default( &h->param );
813 h->sps->i_chroma_format_idc = 1;
814 h->chroma_qp_table = i_chroma_qp_table + 12;
815 h->param.analyse.i_luma_deadzone[0] = 0;
816 h->param.analyse.i_luma_deadzone[1] = 0;
817 h->param.analyse.b_transform_8x8 = 1;
818 for( int i = 0; i < 6; i++ )
819 h->pps->scaling_list[i] = x264_cqm_flat16;
821 x264_quant_init( h, 0, &qf );
823 /* overflow test cases */
824 for( int i = 0; i < 5; i++ )
826 pixel *enc = &pbuf3[16*i*FENC_STRIDE];
827 pixel *dec = &pbuf4[16*i*FDEC_STRIDE];
829 for( int j = 0; j < 16; j++ )
831 int cond_a = (i < 2) ? 1 : ((j&3) == 0 || (j&3) == (i-1));
832 int cond_b = (i == 0) ? 1 : !cond_a;
833 enc[0] = enc[1] = enc[4] = enc[5] = enc[8] = enc[9] = enc[12] = enc[13] = cond_a ? PIXEL_MAX : 0;
834 enc[2] = enc[3] = enc[6] = enc[7] = enc[10] = enc[11] = enc[14] = enc[15] = cond_b ? PIXEL_MAX : 0;
836 for( int k = 0; k < 4; k++ )
837 dec[k] = PIXEL_MAX - enc[k];
844 #define TEST_DCT( name, t1, t2, size ) \
845 if( dct_asm.name != dct_ref.name ) \
847 set_func_name( #name ); \
849 pixel *enc = pbuf3; \
850 pixel *dec = pbuf4; \
851 for( int j = 0; j < 5; j++) \
853 call_c( dct_c.name, t1, &pbuf1[j*64], &pbuf2[j*64] ); \
854 call_a( dct_asm.name, t2, &pbuf1[j*64], &pbuf2[j*64] ); \
855 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
858 fprintf( stderr, #name " [FAILED]\n" ); \
859 for( int k = 0; k < size; k++ )\
860 printf( "%d ", ((dctcoef*)t1)[k] );\
862 for( int k = 0; k < size; k++ )\
863 printf( "%d ", ((dctcoef*)t2)[k] );\
867 call_c( dct_c.name, t1, enc, dec ); \
868 call_a( dct_asm.name, t2, enc, dec ); \
869 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
872 fprintf( stderr, #name " [FAILED] (overflow)\n" ); \
875 enc += 16*FENC_STRIDE; \
876 dec += 16*FDEC_STRIDE; \
879 ok = 1; used_asm = 0;
880 TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16 );
881 TEST_DCT( sub8x8_dct, dct1, dct2, 16*4 );
882 TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4 );
883 TEST_DCT( sub8x16_dct_dc, dctdc[0], dctdc[1], 8 );
884 TEST_DCT( sub16x16_dct, dct1, dct2, 16*16 );
885 report( "sub_dct4 :" );
887 ok = 1; used_asm = 0;
888 TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64 );
889 TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*4 );
890 report( "sub_dct8 :" );
893 // fdct and idct are denormalized by different factors, so quant/dequant
894 // is needed to force the coefs into the right range.
895 dct_c.sub16x16_dct( dct4, pbuf1, pbuf2 );
896 dct_c.sub16x16_dct8( dct8, pbuf1, pbuf2 );
897 for( int i = 0; i < 16; i++ )
899 qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
900 qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
902 for( int i = 0; i < 4; i++ )
904 qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
905 qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
907 x264_cqm_delete( h );
909 #define TEST_IDCT( name, src ) \
910 if( dct_asm.name != dct_ref.name ) \
912 set_func_name( #name ); \
914 memcpy( pbuf3, pbuf1, 32*32 * sizeof(pixel) ); \
915 memcpy( pbuf4, pbuf1, 32*32 * sizeof(pixel) ); \
916 memcpy( dct1, src, 256 * sizeof(dctcoef) ); \
917 memcpy( dct2, src, 256 * sizeof(dctcoef) ); \
918 call_c1( dct_c.name, pbuf3, (void*)dct1 ); \
919 call_a1( dct_asm.name, pbuf4, (void*)dct2 ); \
920 if( memcmp( pbuf3, pbuf4, 32*32 * sizeof(pixel) ) ) \
923 fprintf( stderr, #name " [FAILED]\n" ); \
925 call_c2( dct_c.name, pbuf3, (void*)dct1 ); \
926 call_a2( dct_asm.name, pbuf4, (void*)dct2 ); \
928 ok = 1; used_asm = 0;
929 TEST_IDCT( add4x4_idct, dct4 );
930 TEST_IDCT( add8x8_idct, dct4 );
931 TEST_IDCT( add8x8_idct_dc, dct4 );
932 TEST_IDCT( add16x16_idct, dct4 );
933 TEST_IDCT( add16x16_idct_dc, dct4 );
934 report( "add_idct4 :" );
936 ok = 1; used_asm = 0;
937 TEST_IDCT( add8x8_idct8, dct8 );
938 TEST_IDCT( add16x16_idct8, dct8 );
939 report( "add_idct8 :" );
942 #define TEST_DCTDC( name )\
943 ok = 1; used_asm = 0;\
944 if( dct_asm.name != dct_ref.name )\
946 set_func_name( #name );\
948 uint16_t *p = (uint16_t*)buf1;\
949 for( int i = 0; i < 16 && ok; i++ )\
951 for( int j = 0; j < 16; j++ )\
952 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
953 : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
954 : ((*p++)&0x1fff)-0x1000; /* general case */\
955 memcpy( dct2, dct1, 16 * sizeof(dctcoef) );\
956 call_c1( dct_c.name, dct1[0] );\
957 call_a1( dct_asm.name, dct2[0] );\
958 if( memcmp( dct1, dct2, 16 * sizeof(dctcoef) ) )\
961 call_c2( dct_c.name, dct1[0] );\
962 call_a2( dct_asm.name, dct2[0] );\
964 report( #name " :" );
966 TEST_DCTDC( dct4x4dc );
967 TEST_DCTDC( idct4x4dc );
970 #define TEST_DCTDC_CHROMA( name )\
971 ok = 1; used_asm = 0;\
972 if( dct_asm.name != dct_ref.name )\
974 set_func_name( #name );\
976 uint16_t *p = (uint16_t*)buf1;\
977 for( int i = 0; i < 16 && ok; i++ )\
979 for( int j = 0; j < 8; j++ )\
980 dct1[j][0] = !i ? (j^j>>1^j>>2)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
981 : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
982 : ((*p++)&0x1fff)-0x1000; /* general case */\
983 memcpy( dct2, dct1, 8*16 * sizeof(dctcoef) );\
984 call_c1( dct_c.name, dctdc[0], dct1 );\
985 call_a1( dct_asm.name, dctdc[1], dct2 );\
986 if( memcmp( dctdc[0], dctdc[1], 8 * sizeof(dctcoef) ) || memcmp( dct1, dct2, 8*16 * sizeof(dctcoef) ) )\
989 fprintf( stderr, #name " [FAILED]\n" ); \
992 call_c2( dct_c.name, dctdc[0], dct1 );\
993 call_a2( dct_asm.name, dctdc[1], dct2 );\
995 report( #name " :" );
997 TEST_DCTDC_CHROMA( dct2x4dc );
998 #undef TEST_DCTDC_CHROMA
1000 x264_zigzag_function_t zigzag_c[2];
1001 x264_zigzag_function_t zigzag_ref[2];
1002 x264_zigzag_function_t zigzag_asm[2];
1004 ALIGNED_16( dctcoef level1[64] );
1005 ALIGNED_16( dctcoef level2[64] );
1007 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
1008 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1010 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1012 for( int i = 0; i < size*size; i++ ) \
1014 call_c( zigzag_c[interlace].name, t1, dct ); \
1015 call_a( zigzag_asm[interlace].name, t2, dct ); \
1016 if( memcmp( t1, t2, size*size*sizeof(dctcoef) ) ) \
1019 for( int i = 0; i < 2; i++ ) \
1021 dctcoef *d = (dctcoef*)(i ? t2 : t1); \
1022 for( int j = 0; j < size; j++ ) \
1024 for( int k = 0; k < size; k++ ) \
1025 fprintf( stderr, "%2d ", d[k+j*8] ); \
1026 fprintf( stderr, "\n" ); \
1028 fprintf( stderr, "\n" ); \
1030 fprintf( stderr, #name " [FAILED]\n" ); \
1034 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
1035 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1038 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1040 memcpy( pbuf3, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
1041 memcpy( pbuf4, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
1042 nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
1043 nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
1044 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE*sizeof(pixel) ) || nz_c != nz_a ) \
1047 fprintf( stderr, #name " [FAILED]\n" ); \
1049 call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
1050 call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
1053 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
1054 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1057 dctcoef dc_a, dc_c; \
1058 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1060 for( int i = 0; i < 2; i++ ) \
1062 memcpy( pbuf3, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
1063 memcpy( pbuf4, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
1064 for( int j = 0; j < 4; j++ ) \
1066 memcpy( pbuf3 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
1067 memcpy( pbuf4 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
1069 nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
1070 nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
1071 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 ) \
1074 fprintf( stderr, #name " [FAILED]\n" ); \
1078 call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
1079 call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
1082 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
1083 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1085 for( int j = 0; j < 100; j++ ) \
1087 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1089 memcpy(dct, buf1, size*sizeof(dctcoef)); \
1090 for( int i = 0; i < size; i++ ) \
1091 dct[i] = rand()&0x1F ? 0 : dct[i]; \
1092 memcpy(buf3, buf4, 10); \
1093 call_c( zigzag_c[interlace].name, t1, dct, buf3 ); \
1094 call_a( zigzag_asm[interlace].name, t2, dct, buf4 ); \
1095 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( buf3, buf4, 10 ) ) \
1097 ok = 0; printf("%d: %d %d %d %d\n%d %d %d %d\n\n",memcmp( t1, t2, size*sizeof(dctcoef) ),buf3[0], buf3[1], buf3[8], buf3[9], buf4[0], buf4[1], buf4[8], buf4[9]);break;\
1102 x264_zigzag_init( 0, &zigzag_c[0], &zigzag_c[1] );
1103 x264_zigzag_init( cpu_ref, &zigzag_ref[0], &zigzag_ref[1] );
1104 x264_zigzag_init( cpu_new, &zigzag_asm[0], &zigzag_asm[1] );
1106 ok = 1; used_asm = 0;
1107 TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct8[0], 64 );
1108 report( "zigzag_interleave :" );
1110 for( interlace = 0; interlace <= 1; interlace++ )
1112 ok = 1; used_asm = 0;
1113 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, dct8[0], 8 );
1114 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 4 );
1115 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
1116 TEST_ZIGZAG_SUB( sub_8x8, level1, level2, 64 );
1117 TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
1118 report( interlace ? "zigzag_field :" : "zigzag_frame :" );
1120 #undef TEST_ZIGZAG_SCAN
1121 #undef TEST_ZIGZAG_SUB
1126 static int check_mc( int cpu_ref, int cpu_new )
1128 x264_mc_functions_t mc_c;
1129 x264_mc_functions_t mc_ref;
1130 x264_mc_functions_t mc_a;
1131 x264_pixel_function_t pixf;
1133 pixel *src = &(pbuf1)[2*64+2];
1134 pixel *src2[4] = { &(pbuf1)[3*64+2], &(pbuf1)[5*64+2],
1135 &(pbuf1)[7*64+2], &(pbuf1)[9*64+2] };
1136 pixel *dst1 = pbuf3;
1137 pixel *dst2 = pbuf4;
1139 int ret = 0, ok, used_asm;
1141 x264_mc_init( 0, &mc_c, 0 );
1142 x264_mc_init( cpu_ref, &mc_ref, 0 );
1143 x264_mc_init( cpu_new, &mc_a, 0 );
1144 x264_pixel_init( 0, &pixf );
1146 #define MC_TEST_LUMA( w, h ) \
1147 if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
1149 const x264_weight_t *weight = x264_weight_none; \
1150 set_func_name( "mc_luma_%dx%d", w, h ); \
1152 for( int i = 0; i < 1024; i++ ) \
1153 pbuf3[i] = pbuf4[i] = 0xCD; \
1154 call_c( mc_c.mc_luma, dst1, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1155 call_a( mc_a.mc_luma, dst2, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1156 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1158 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
1162 if( mc_a.get_ref != mc_ref.get_ref ) \
1164 pixel *ref = dst2; \
1165 intptr_t ref_stride = 32; \
1166 int w_checked = ( ( sizeof(pixel) == 2 && (w == 12 || w == 20)) ? w-2 : w ); \
1167 const x264_weight_t *weight = x264_weight_none; \
1168 set_func_name( "get_ref_%dx%d", w_checked, h ); \
1170 for( int i = 0; i < 1024; i++ ) \
1171 pbuf3[i] = pbuf4[i] = 0xCD; \
1172 call_c( mc_c.mc_luma, dst1, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1173 ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1174 for( int i = 0; i < h; i++ ) \
1175 if( memcmp( dst1+i*32, ref+i*ref_stride, w_checked * sizeof(pixel) ) ) \
1177 fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w_checked, h ); \
1183 #define MC_TEST_CHROMA( w, h ) \
1184 if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
1186 set_func_name( "mc_chroma_%dx%d", w, h ); \
1188 for( int i = 0; i < 1024; i++ ) \
1189 pbuf3[i] = pbuf4[i] = 0xCD; \
1190 call_c( mc_c.mc_chroma, dst1, dst1+8, (intptr_t)16, src, (intptr_t)64, dx, dy, w, h ); \
1191 call_a( mc_a.mc_chroma, dst2, dst2+8, (intptr_t)16, src, (intptr_t)64, dx, dy, w, h ); \
1192 /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \
1193 for( int j = 0; j < h; j++ ) \
1194 for( int i = w; i < 8; i++ ) \
1196 dst2[i+j*16+8] = dst1[i+j*16+8]; \
1197 dst2[i+j*16 ] = dst1[i+j*16 ]; \
1199 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1201 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
1205 ok = 1; used_asm = 0;
1206 for( int dy = -8; dy < 8; dy++ )
1207 for( int dx = -128; dx < 128; dx++ )
1209 if( rand()&15 ) continue; // running all of them is too slow
1210 MC_TEST_LUMA( 20, 18 );
1211 MC_TEST_LUMA( 16, 16 );
1212 MC_TEST_LUMA( 16, 8 );
1213 MC_TEST_LUMA( 12, 10 );
1214 MC_TEST_LUMA( 8, 16 );
1215 MC_TEST_LUMA( 8, 8 );
1216 MC_TEST_LUMA( 8, 4 );
1217 MC_TEST_LUMA( 4, 8 );
1218 MC_TEST_LUMA( 4, 4 );
1220 report( "mc luma :" );
1222 ok = 1; used_asm = 0;
1223 for( int dy = -1; dy < 9; dy++ )
1224 for( int dx = -128; dx < 128; dx++ )
1226 if( rand()&15 ) continue;
1227 MC_TEST_CHROMA( 8, 8 );
1228 MC_TEST_CHROMA( 8, 4 );
1229 MC_TEST_CHROMA( 4, 8 );
1230 MC_TEST_CHROMA( 4, 4 );
1231 MC_TEST_CHROMA( 4, 2 );
1232 MC_TEST_CHROMA( 2, 4 );
1233 MC_TEST_CHROMA( 2, 2 );
1235 report( "mc chroma :" );
1237 #undef MC_TEST_CHROMA
1239 #define MC_TEST_AVG( name, weight ) \
1241 for( int i = 0; i < 12; i++ ) \
1243 memcpy( pbuf3, pbuf1+320, 320 * sizeof(pixel) ); \
1244 memcpy( pbuf4, pbuf1+320, 320 * sizeof(pixel) ); \
1245 if( mc_a.name[i] != mc_ref.name[i] ) \
1247 set_func_name( "%s_%s", #name, pixel_names[i] ); \
1249 call_c1( mc_c.name[i], pbuf3, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1250 call_a1( mc_a.name[i], pbuf4, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1251 if( memcmp( pbuf3, pbuf4, 320 * sizeof(pixel) ) ) \
1254 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
1256 call_c2( mc_c.name[i], pbuf3, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1257 call_a2( mc_a.name[i], pbuf4, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1262 ok = 1, used_asm = 0;
1263 for( int w = -63; w <= 127 && ok; w++ )
1264 MC_TEST_AVG( avg, w );
1265 report( "mc wpredb :" );
1267 #define MC_TEST_WEIGHT( name, weight, aligned ) \
1268 int align_off = (aligned ? 0 : rand()%16); \
1269 for( int i = 1; i <= 5; i++ ) \
1271 ALIGNED_16( pixel buffC[640] ); \
1272 ALIGNED_16( pixel buffA[640] ); \
1273 int j = X264_MAX( i*4, 2 ); \
1274 memset( buffC, 0, 640 * sizeof(pixel) ); \
1275 memset( buffA, 0, 640 * sizeof(pixel) ); \
1278 /* w12 is the same as w16 in some cases */ \
1279 if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
1281 if( mc_a.name[i] != mc_ref.name[i] ) \
1283 set_func_name( "%s_w%d", #name, j ); \
1285 call_c1( mc_c.weight[i], buffC, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1286 mc_a.weight_cache(&ha, &weight); \
1287 call_a1( weight.weightfn[i], buffA, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1288 for( int k = 0; k < 16; k++ ) \
1289 if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \
1292 fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
1295 /* omit unlikely high scales for benchmarking */ \
1296 if( (s << (8-d)) < 512 ) \
1298 call_c2( mc_c.weight[i], buffC, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1299 call_a2( weight.weightfn[i], buffA, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1304 ok = 1; used_asm = 0;
1307 for( int s = 0; s <= 127 && ok; s++ )
1309 for( int o = -128; o <= 127 && ok; o++ )
1311 if( rand() & 2047 ) continue;
1312 for( int d = 0; d <= 7 && ok; d++ )
1316 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
1317 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
1322 report( "mc weight :" );
1324 ok = 1; used_asm = 0;
1325 for( int o = 0; o <= 127 && ok; o++ )
1328 if( rand() & 15 ) continue;
1329 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1330 MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
1332 report( "mc offsetadd :" );
1333 ok = 1; used_asm = 0;
1334 for( int o = -128; o < 0 && ok; o++ )
1337 if( rand() & 15 ) continue;
1338 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1339 MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
1341 report( "mc offsetsub :" );
1343 ok = 1; used_asm = 0;
1344 for( int height = 8; height <= 16; height += 8 )
1346 if( mc_a.store_interleave_chroma != mc_ref.store_interleave_chroma )
1348 set_func_name( "store_interleave_chroma" );
1350 memset( pbuf3, 0, 64*height );
1351 memset( pbuf4, 0, 64*height );
1352 call_c( mc_c.store_interleave_chroma, pbuf3, (intptr_t)64, pbuf1, pbuf1+16, height );
1353 call_a( mc_a.store_interleave_chroma, pbuf4, (intptr_t)64, pbuf1, pbuf1+16, height );
1354 if( memcmp( pbuf3, pbuf4, 64*height ) )
1357 fprintf( stderr, "store_interleave_chroma FAILED: h=%d\n", height );
1361 if( mc_a.load_deinterleave_chroma_fenc != mc_ref.load_deinterleave_chroma_fenc )
1363 set_func_name( "load_deinterleave_chroma_fenc" );
1365 call_c( mc_c.load_deinterleave_chroma_fenc, pbuf3, pbuf1, (intptr_t)64, height );
1366 call_a( mc_a.load_deinterleave_chroma_fenc, pbuf4, pbuf1, (intptr_t)64, height );
1367 if( memcmp( pbuf3, pbuf4, FENC_STRIDE*height ) )
1370 fprintf( stderr, "load_deinterleave_chroma_fenc FAILED: h=%d\n", height );
1374 if( mc_a.load_deinterleave_chroma_fdec != mc_ref.load_deinterleave_chroma_fdec )
1376 set_func_name( "load_deinterleave_chroma_fdec" );
1378 call_c( mc_c.load_deinterleave_chroma_fdec, pbuf3, pbuf1, (intptr_t)64, height );
1379 call_a( mc_a.load_deinterleave_chroma_fdec, pbuf4, pbuf1, (intptr_t)64, height );
1380 if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*height ) )
1383 fprintf( stderr, "load_deinterleave_chroma_fdec FAILED: h=%d\n", height );
1388 report( "store_interleave :" );
1391 int w, h, src_stride;
1392 } 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} };
1393 ok = 1; used_asm = 0;
1394 if( mc_a.plane_copy != mc_ref.plane_copy )
1396 set_func_name( "plane_copy" );
1398 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1400 int w = plane_specs[i].w;
1401 int h = plane_specs[i].h;
1402 intptr_t src_stride = plane_specs[i].src_stride;
1403 intptr_t dst_stride = (w + 127) & ~63;
1404 assert( dst_stride * h <= 0x1000 );
1405 pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1406 memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1407 memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1408 call_c( mc_c.plane_copy, pbuf3, dst_stride, src1, src_stride, w, h );
1409 call_a( mc_a.plane_copy, pbuf4, dst_stride, src1, src_stride, w, h );
1410 for( int y = 0; y < h; y++ )
1411 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(pixel) ) )
1414 fprintf( stderr, "plane_copy FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1420 if( mc_a.plane_copy_swap != mc_ref.plane_copy_swap )
1422 set_func_name( "plane_copy_swap" );
1424 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1426 int w = (plane_specs[i].w + 1) >> 1;
1427 int h = plane_specs[i].h;
1428 intptr_t src_stride = plane_specs[i].src_stride;
1429 intptr_t dst_stride = (2*w + 127) & ~63;
1430 assert( dst_stride * h <= 0x1000 );
1431 pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1432 memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1433 memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1434 call_c( mc_c.plane_copy_swap, pbuf3, dst_stride, src1, src_stride, w, h );
1435 call_a( mc_a.plane_copy_swap, pbuf4, dst_stride, src1, src_stride, w, h );
1436 for( int y = 0; y < h; y++ )
1437 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) )
1440 fprintf( stderr, "plane_copy_swap FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1446 if( mc_a.plane_copy_interleave != mc_ref.plane_copy_interleave )
1448 set_func_name( "plane_copy_interleave" );
1450 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1452 int w = (plane_specs[i].w + 1) >> 1;
1453 int h = plane_specs[i].h;
1454 intptr_t src_stride = (plane_specs[i].src_stride + 1) >> 1;
1455 intptr_t dst_stride = (2*w + 127) & ~63;
1456 assert( dst_stride * h <= 0x1000 );
1457 pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1458 memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1459 memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1460 call_c( mc_c.plane_copy_interleave, pbuf3, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1461 call_a( mc_a.plane_copy_interleave, pbuf4, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1462 for( int y = 0; y < h; y++ )
1463 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) )
1466 fprintf( stderr, "plane_copy_interleave FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1472 if( mc_a.plane_copy_deinterleave != mc_ref.plane_copy_deinterleave )
1474 set_func_name( "plane_copy_deinterleave" );
1476 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1478 int w = (plane_specs[i].w + 1) >> 1;
1479 int h = plane_specs[i].h;
1480 intptr_t dst_stride = w;
1481 intptr_t src_stride = (2*w + 127) & ~63;
1482 intptr_t offv = (dst_stride*h + 31) & ~15;
1483 memset( pbuf3, 0, 0x1000 );
1484 memset( pbuf4, 0, 0x1000 );
1485 call_c( mc_c.plane_copy_deinterleave, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf1, src_stride, w, h );
1486 call_a( mc_a.plane_copy_deinterleave, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf1, src_stride, w, h );
1487 for( int y = 0; y < h; y++ )
1488 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w ) ||
1489 memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w ) )
1492 fprintf( stderr, "plane_copy_deinterleave FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1498 if( mc_a.plane_copy_deinterleave_rgb != mc_ref.plane_copy_deinterleave_rgb )
1500 set_func_name( "plane_copy_deinterleave_rgb" );
1502 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1504 int w = (plane_specs[i].w + 2) >> 2;
1505 int h = plane_specs[i].h;
1506 intptr_t src_stride = plane_specs[i].src_stride;
1507 intptr_t dst_stride = ALIGN( w, 16 );
1508 intptr_t offv = dst_stride*h + 16;
1510 for( int pw = 3; pw <= 4; pw++ )
1512 memset( pbuf3, 0, 0x1000 );
1513 memset( pbuf4, 0, 0x1000 );
1514 call_c( mc_c.plane_copy_deinterleave_rgb, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf3+2*offv, dst_stride, pbuf1, src_stride, pw, w, h );
1515 call_a( mc_a.plane_copy_deinterleave_rgb, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf4+2*offv, dst_stride, pbuf1, src_stride, pw, w, h );
1516 for( int y = 0; y < h; y++ )
1517 if( memcmp( pbuf3+y*dst_stride+0*offv, pbuf4+y*dst_stride+0*offv, w ) ||
1518 memcmp( pbuf3+y*dst_stride+1*offv, pbuf4+y*dst_stride+1*offv, w ) ||
1519 memcmp( pbuf3+y*dst_stride+2*offv, pbuf4+y*dst_stride+2*offv, w ) )
1522 fprintf( stderr, "plane_copy_deinterleave_rgb FAILED: w=%d h=%d stride=%d pw=%d\n", w, h, (int)src_stride, pw );
1528 report( "plane_copy :" );
1530 if( mc_a.plane_copy_deinterleave_v210 != mc_ref.plane_copy_deinterleave_v210 )
1532 set_func_name( "plane_copy_deinterleave_v210" );
1533 ok = 1; used_asm = 1;
1534 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1536 int w = (plane_specs[i].w + 1) >> 1;
1537 int h = plane_specs[i].h;
1538 intptr_t dst_stride = ALIGN( w, 16 );
1539 intptr_t src_stride = (w + 47) / 48 * 128 / sizeof(uint32_t);
1540 intptr_t offv = dst_stride*h + 32;
1541 memset( pbuf3, 0, 0x1000 );
1542 memset( pbuf4, 0, 0x1000 );
1543 call_c( mc_c.plane_copy_deinterleave_v210, pbuf3, dst_stride, pbuf3+offv, dst_stride, (uint32_t *)buf1, src_stride, w, h );
1544 call_a( mc_a.plane_copy_deinterleave_v210, pbuf4, dst_stride, pbuf4+offv, dst_stride, (uint32_t *)buf1, src_stride, w, h );
1545 for( int y = 0; y < h; y++ )
1546 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(uint16_t) ) ||
1547 memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w*sizeof(uint16_t) ) )
1550 fprintf( stderr, "plane_copy_deinterleave_v210 FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1557 if( mc_a.hpel_filter != mc_ref.hpel_filter )
1559 pixel *srchpel = pbuf1+8+2*64;
1560 pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
1561 pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
1562 void *tmp = pbuf3+49*64;
1563 set_func_name( "hpel_filter" );
1564 ok = 1; used_asm = 1;
1565 memset( pbuf3, 0, 4096 * sizeof(pixel) );
1566 memset( pbuf4, 0, 4096 * sizeof(pixel) );
1567 call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, (intptr_t)64, 48, 10, tmp );
1568 call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, (intptr_t)64, 48, 10, tmp );
1569 for( int i = 0; i < 3; i++ )
1570 for( int j = 0; j < 10; j++ )
1571 //FIXME ideally the first pixels would match too, but they aren't actually used
1572 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
1575 fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
1576 for( int k = 0; k < 48; k++ )
1577 printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
1579 for( int k = 0; k < 48; k++ )
1580 printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
1584 report( "hpel filter :" );
1587 if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
1589 pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
1590 pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1591 set_func_name( "lowres_init" );
1592 ok = 1; used_asm = 1;
1593 for( int w = 96; w <= 96+24; w += 8 )
1595 intptr_t stride = (w*2+31)&~31;
1596 intptr_t stride_lowres = (w+31)&~31;
1597 call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], stride, stride_lowres, w, 8 );
1598 call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], stride, stride_lowres, w, 8 );
1599 for( int i = 0; i < 8; i++ )
1601 for( int j = 0; j < 4; j++ )
1602 if( memcmp( dstc[j]+i*stride_lowres, dsta[j]+i*stride_lowres, w * sizeof(pixel) ) )
1605 fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1606 for( int k = 0; k < w; k++ )
1607 printf( "%d ", dstc[j][k+i*stride_lowres] );
1609 for( int k = 0; k < w; k++ )
1610 printf( "%d ", dsta[j][k+i*stride_lowres] );
1616 report( "lowres init :" );
1619 #define INTEGRAL_INIT( name, size, offset, cmp_len, ... )\
1620 if( mc_a.name != mc_ref.name )\
1622 intptr_t stride = 96;\
1623 set_func_name( #name );\
1625 memcpy( buf3, buf1, size*2*stride );\
1626 memcpy( buf4, buf1, size*2*stride );\
1627 uint16_t *sum = (uint16_t*)buf3;\
1628 call_c1( mc_c.name, sum+offset, __VA_ARGS__ );\
1629 sum = (uint16_t*)buf4;\
1630 call_a1( mc_a.name, sum+offset, __VA_ARGS__ );\
1631 if( memcmp( buf3+2*offset, buf4+2*offset, cmp_len*2 )\
1632 || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
1634 call_c2( mc_c.name, sum+offset, __VA_ARGS__ );\
1635 call_a2( mc_a.name, sum+offset, __VA_ARGS__ );\
1637 ok = 1; used_asm = 0;
1638 INTEGRAL_INIT( integral_init4h, 2, stride, stride-4, pbuf2, stride );
1639 INTEGRAL_INIT( integral_init8h, 2, stride, stride-8, pbuf2, stride );
1640 INTEGRAL_INIT( integral_init4v, 14, 0, stride-8, sum+9*stride, stride );
1641 INTEGRAL_INIT( integral_init8v, 9, 0, stride-8, stride );
1642 report( "integral init :" );
1644 ok = 1; used_asm = 0;
1645 if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1649 for( int i = 0; i < 10; i++ )
1651 float fps_factor = (rand()&65535) / 65535.0f;
1652 set_func_name( "mbtree_propagate_cost" );
1653 int16_t *dsta = (int16_t*)buf3;
1654 int16_t *dstc = dsta+400;
1655 uint16_t *prop = (uint16_t*)buf1;
1656 uint16_t *intra = (uint16_t*)buf4;
1657 uint16_t *inter = intra+128;
1658 uint16_t *qscale = inter+128;
1659 uint16_t *rnd = (uint16_t*)buf2;
1661 for( int j = 0; j < 100; j++ )
1663 intra[j] = *rnd++ & 0x7fff;
1664 intra[j] += !intra[j];
1665 inter[j] = *rnd++ & 0x7fff;
1666 qscale[j] = *rnd++ & 0x7fff;
1668 call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, &fps_factor, 100 );
1669 call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, &fps_factor, 100 );
1670 // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1672 for( int j = 0; j < 100 && ok; j++ )
1674 ok &= abs( dstc[j]-dsta[j] ) <= 1 || fabs( (double)dstc[j]/dsta[j]-1 ) < 1e-4;
1676 fprintf( stderr, "mbtree_propagate_cost FAILED: %f !~= %f\n", (double)dstc[j], (double)dsta[j] );
1681 if( mc_a.mbtree_propagate_list != mc_ref.mbtree_propagate_list )
1684 for( int i = 0; i < 8; i++ )
1686 set_func_name( "mbtree_propagate_list" );
1690 int size = width*height;
1691 h.mb.i_mb_stride = width;
1692 h.mb.i_mb_width = width;
1693 h.mb.i_mb_height = height;
1695 uint16_t *ref_costsc = (uint16_t*)buf3;
1696 uint16_t *ref_costsa = (uint16_t*)buf4;
1697 int16_t (*mvs)[2] = (int16_t(*)[2])(ref_costsc + size);
1698 int16_t *propagate_amount = (int16_t*)(mvs + width);
1699 uint16_t *lowres_costs = (uint16_t*)(propagate_amount + width);
1700 h.scratch_buffer2 = (uint8_t*)(ref_costsa + size);
1701 int bipred_weight = (rand()%63)+1;
1703 for( int j = 0; j < size; j++ )
1704 ref_costsc[j] = ref_costsa[j] = rand()&32767;
1705 for( int j = 0; j < width; j++ )
1707 static const uint8_t list_dist[2][8] = {{0,1,1,1,1,1,1,1},{1,1,3,3,3,3,3,2}};
1708 for( int k = 0; k < 2; k++ )
1709 mvs[j][k] = (rand()&127) - 64;
1710 propagate_amount[j] = rand()&32767;
1711 lowres_costs[j] = list_dist[list][rand()&7] << LOWRES_COST_SHIFT;
1714 call_c1( mc_c.mbtree_propagate_list, &h, ref_costsc, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list );
1715 call_a1( mc_a.mbtree_propagate_list, &h, ref_costsa, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list );
1717 for( int j = 0; j < size && ok; j++ )
1719 ok &= abs(ref_costsa[j] - ref_costsc[j]) <= 1;
1721 fprintf( stderr, "mbtree_propagate_list FAILED at %d: %d !~= %d\n", j, ref_costsc[j], ref_costsa[j] );
1724 call_c2( mc_c.mbtree_propagate_list, &h, ref_costsc, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list );
1725 call_a2( mc_a.mbtree_propagate_list, &h, ref_costsa, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list );
1728 report( "mbtree :" );
1730 if( mc_a.memcpy_aligned != mc_ref.memcpy_aligned )
1732 set_func_name( "memcpy_aligned" );
1733 ok = 1; used_asm = 1;
1734 for( size_t size = 16; size < 256; size += 16 )
1736 memset( buf4, 0xAA, size + 1 );
1737 call_c( mc_c.memcpy_aligned, buf3, buf1, size );
1738 call_a( mc_a.memcpy_aligned, buf4, buf1, size );
1739 if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1742 fprintf( stderr, "memcpy_aligned FAILED: size=%d\n", (int)size );
1746 report( "memcpy aligned :" );
1749 if( mc_a.memzero_aligned != mc_ref.memzero_aligned )
1751 set_func_name( "memzero_aligned" );
1752 ok = 1; used_asm = 1;
1753 for( size_t size = 128; size < 1024; size += 128 )
1755 memset( buf4, 0xAA, size + 1 );
1756 call_c( mc_c.memzero_aligned, buf3, size );
1757 call_a( mc_a.memzero_aligned, buf4, size );
1758 if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1761 fprintf( stderr, "memzero_aligned FAILED: size=%d\n", (int)size );
1765 report( "memzero aligned :" );
1771 static int check_deblock( int cpu_ref, int cpu_new )
1773 x264_deblock_function_t db_c;
1774 x264_deblock_function_t db_ref;
1775 x264_deblock_function_t db_a;
1776 int ret = 0, ok = 1, used_asm = 0;
1777 int alphas[36], betas[36];
1780 x264_deblock_init( 0, &db_c, 0 );
1781 x264_deblock_init( cpu_ref, &db_ref, 0 );
1782 x264_deblock_init( cpu_new, &db_a, 0 );
1784 /* not exactly the real values of a,b,tc but close enough */
1785 for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1787 alphas[i] = a << (BIT_DEPTH-8);
1788 betas[i] = (i+1)/2 << (BIT_DEPTH-8);
1789 tcs[i][0] = tcs[i][3] = (c+6)/10 << (BIT_DEPTH-8);
1790 tcs[i][1] = (c+7)/15 << (BIT_DEPTH-8);
1791 tcs[i][2] = (c+9)/20 << (BIT_DEPTH-8);
1796 #define TEST_DEBLOCK( name, align, ... ) \
1797 for( int i = 0; i < 36; i++ ) \
1799 intptr_t off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1800 for( int j = 0; j < 1024; j++ ) \
1801 /* two distributions of random to excersize different failure modes */ \
1802 pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \
1803 memcpy( pbuf4, pbuf3, 1024 * sizeof(pixel) ); \
1804 if( db_a.name != db_ref.name ) \
1806 set_func_name( #name ); \
1808 call_c1( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1809 call_a1( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1810 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1813 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1816 call_c2( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1817 call_a2( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1821 TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1822 TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1823 TEST_DEBLOCK( deblock_h_chroma_420, 0, tcs[i] );
1824 TEST_DEBLOCK( deblock_h_chroma_422, 0, tcs[i] );
1825 TEST_DEBLOCK( deblock_chroma_420_mbaff, 0, tcs[i] );
1826 TEST_DEBLOCK( deblock_chroma_422_mbaff, 0, tcs[i] );
1827 TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1828 TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1829 TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1830 TEST_DEBLOCK( deblock_h_chroma_420_intra, 0 );
1831 TEST_DEBLOCK( deblock_h_chroma_422_intra, 0 );
1832 TEST_DEBLOCK( deblock_chroma_420_intra_mbaff, 0 );
1833 TEST_DEBLOCK( deblock_chroma_422_intra_mbaff, 0 );
1834 TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1836 if( db_a.deblock_strength != db_ref.deblock_strength )
1838 for( int i = 0; i < 100; i++ )
1840 ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1841 ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1842 ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] );
1843 ALIGNED_ARRAY_N( uint8_t, bs, [2],[2][8][4] );
1844 memset( bs, 99, sizeof(uint8_t)*2*4*8*2 );
1845 for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1846 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1847 for( int j = 0; j < 2; j++ )
1848 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1850 ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1851 for( int l = 0; l < 2; l++ )
1852 mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1854 set_func_name( "deblock_strength" );
1855 call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1856 call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1857 if( memcmp( bs[0], bs[1], sizeof(uint8_t)*2*4*8 ) )
1860 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1861 for( int j = 0; j < 2; j++ )
1863 for( int k = 0; k < 2; k++ )
1864 for( int l = 0; l < 4; l++ )
1866 for( int m = 0; m < 4; m++ )
1867 printf("%d ",bs[j][k][l][m]);
1877 report( "deblock :" );
1882 static int check_quant( int cpu_ref, int cpu_new )
1884 x264_quant_function_t qf_c;
1885 x264_quant_function_t qf_ref;
1886 x264_quant_function_t qf_a;
1887 ALIGNED_ARRAY_N( dctcoef, dct1,[64] );
1888 ALIGNED_ARRAY_N( dctcoef, dct2,[64] );
1889 ALIGNED_ARRAY_N( dctcoef, dct3,[8],[16] );
1890 ALIGNED_ARRAY_N( dctcoef, dct4,[8],[16] );
1891 ALIGNED_ARRAY_N( uint8_t, cqm_buf,[64] );
1892 int ret = 0, ok, used_asm;
1893 int oks[3] = {1,1,1}, used_asms[3] = {0,0,0};
1896 memset( h, 0, sizeof(*h) );
1897 h->sps->i_chroma_format_idc = 1;
1898 x264_param_default( &h->param );
1899 h->chroma_qp_table = i_chroma_qp_table + 12;
1900 h->param.analyse.b_transform_8x8 = 1;
1902 for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1906 for( int i = 0; i < 6; i++ )
1907 h->pps->scaling_list[i] = x264_cqm_flat16;
1908 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1910 else if( i_cqm == 1 )
1912 for( int i = 0; i < 6; i++ )
1913 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1914 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1918 int max_scale = BIT_DEPTH < 10 ? 255 : 228;
1920 for( int i = 0; i < 64; i++ )
1921 cqm_buf[i] = 10 + rand() % (max_scale - 9);
1923 for( int i = 0; i < 64; i++ )
1925 for( int i = 0; i < 6; i++ )
1926 h->pps->scaling_list[i] = cqm_buf;
1927 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1930 h->param.rc.i_qp_min = 0;
1931 h->param.rc.i_qp_max = QP_MAX_SPEC;
1933 x264_quant_init( h, 0, &qf_c );
1934 x264_quant_init( h, cpu_ref, &qf_ref );
1935 x264_quant_init( h, cpu_new, &qf_a );
1937 #define INIT_QUANT8(j,max) \
1939 static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1940 for( int i = 0; i < max; i++ ) \
1942 unsigned int scale = (255*scale1d[(i>>3)&7]*scale1d[i&7])/16; \
1943 dct1[i] = dct2[i] = (j>>(i>>6))&1 ? (rand()%(2*scale+1))-scale : 0; \
1947 #define INIT_QUANT4(j,max) \
1949 static const int scale1d[4] = {4,6,4,6}; \
1950 for( int i = 0; i < max; i++ ) \
1952 unsigned int scale = 255*scale1d[(i>>2)&3]*scale1d[i&3]; \
1953 dct1[i] = dct2[i] = (j>>(i>>4))&1 ? (rand()%(2*scale+1))-scale : 0; \
1957 #define TEST_QUANT_DC( name, cqm ) \
1958 if( qf_a.name != qf_ref.name ) \
1960 set_func_name( #name ); \
1962 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1964 for( int j = 0; j < 2; j++ ) \
1966 int result_c, result_a; \
1967 for( int i = 0; i < 16; i++ ) \
1968 dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1969 result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1970 result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1971 if( memcmp( dct1, dct2, 16*sizeof(dctcoef) ) || result_c != result_a ) \
1974 fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1977 call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1978 call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1983 #define TEST_QUANT( qname, block, type, w, maxj ) \
1984 if( qf_a.qname != qf_ref.qname ) \
1986 set_func_name( #qname ); \
1988 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1990 for( int j = 0; j < maxj; j++ ) \
1992 INIT_QUANT##type(j, w*w) \
1993 int result_c = call_c1( qf_c.qname, (void*)dct1, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1994 int result_a = call_a1( qf_a.qname, (void*)dct2, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1995 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \
1998 fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
2001 call_c2( qf_c.qname, (void*)dct1, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
2002 call_a2( qf_a.qname, (void*)dct2, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
2007 TEST_QUANT( quant_8x8, CQM_8IY, 8, 8, 2 );
2008 TEST_QUANT( quant_8x8, CQM_8PY, 8, 8, 2 );
2009 TEST_QUANT( quant_4x4, CQM_4IY, 4, 4, 2 );
2010 TEST_QUANT( quant_4x4, CQM_4PY, 4, 4, 2 );
2011 TEST_QUANT( quant_4x4x4, CQM_4IY, 4, 8, 16 );
2012 TEST_QUANT( quant_4x4x4, CQM_4PY, 4, 8, 16 );
2013 TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
2014 TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
2016 #define TEST_DEQUANT( qname, dqname, block, w ) \
2017 if( qf_a.dqname != qf_ref.dqname ) \
2019 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
2021 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
2023 INIT_QUANT##w(1, w*w) \
2024 qf_c.qname( dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
2025 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
2026 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
2027 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
2028 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
2031 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
2034 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
2035 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
2039 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
2040 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
2041 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
2042 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
2044 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
2045 if( qf_a.dqname != qf_ref.dqname ) \
2047 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
2049 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
2051 for( int i = 0; i < 16; i++ ) \
2052 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16; \
2053 qf_c.qname( dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
2054 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
2055 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
2056 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
2057 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
2060 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
2062 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
2063 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
2067 TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
2069 if( qf_a.idct_dequant_2x4_dc != qf_ref.idct_dequant_2x4_dc )
2071 set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
2073 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
2075 for( int i = 0; i < 8; i++ )
2076 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
2077 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 );
2078 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 );
2079 call_c( qf_c.idct_dequant_2x4_dc, dct1, dct3, h->dequant4_mf[CQM_4IC], qp+3 );
2080 call_a( qf_a.idct_dequant_2x4_dc, dct1, dct4, h->dequant4_mf[CQM_4IC], qp+3 );
2081 for( int i = 0; i < 8; i++ )
2082 if( dct3[i][0] != dct4[i][0] )
2085 fprintf( stderr, "idct_dequant_2x4_dc (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
2091 if( qf_a.idct_dequant_2x4_dconly != qf_ref.idct_dequant_2x4_dconly )
2093 set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
2095 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
2097 for( int i = 0; i < 8; i++ )
2098 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
2099 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 );
2100 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 );
2101 memcpy( dct2, dct1, 8*sizeof(dctcoef) );
2102 call_c1( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
2103 call_a1( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
2104 if( memcmp( dct1, dct2, 8*sizeof(dctcoef) ) )
2107 fprintf( stderr, "idct_dequant_2x4_dconly (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
2110 call_c2( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
2111 call_a2( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
2115 #define TEST_OPTIMIZE_CHROMA_DC( optname, size ) \
2116 if( qf_a.optname != qf_ref.optname ) \
2118 set_func_name( #optname ); \
2120 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
2122 int qpdc = qp + (size == 8 ? 3 : 0); \
2123 int dmf = h->dequant4_mf[CQM_4IC][qpdc%6][0] << qpdc/6; \
2126 for( int i = 16; ; i <<= 1 ) \
2128 int res_c, res_asm; \
2129 int max = X264_MIN( i, PIXEL_MAX*16 ); \
2130 for( int j = 0; j < size; j++ ) \
2131 dct1[j] = rand()%(max*2+1) - max; \
2132 for( int j = 0; i <= size; j += 4 ) \
2133 qf_c.quant_2x2_dc( &dct1[j], h->quant4_mf[CQM_4IC][qpdc][0]>>1, h->quant4_bias[CQM_4IC][qpdc][0]>>1 ); \
2134 memcpy( dct2, dct1, size*sizeof(dctcoef) ); \
2135 res_c = call_c1( qf_c.optname, dct1, dmf ); \
2136 res_asm = call_a1( qf_a.optname, dct2, dmf ); \
2137 if( res_c != res_asm || memcmp( dct1, dct2, size*sizeof(dctcoef) ) ) \
2140 fprintf( stderr, #optname "(qp=%d, res_c=%d, res_asm=%d): [FAILED]\n", qp, res_c, res_asm ); \
2142 call_c2( qf_c.optname, dct1, dmf ); \
2143 call_a2( qf_a.optname, dct2, dmf ); \
2144 if( i >= PIXEL_MAX*16 ) \
2150 TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x2_dc, 4 );
2151 TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x4_dc, 8 );
2153 x264_cqm_delete( h );
2156 ok = oks[0]; used_asm = used_asms[0];
2157 report( "quant :" );
2159 ok = oks[1]; used_asm = used_asms[1];
2160 report( "dequant :" );
2162 ok = oks[2]; used_asm = used_asms[2];
2163 report( "optimize chroma dc :" );
2165 ok = 1; used_asm = 0;
2166 if( qf_a.denoise_dct != qf_ref.denoise_dct )
2169 for( int size = 16; size <= 64; size += 48 )
2171 set_func_name( "denoise_dct" );
2172 memcpy( dct1, buf1, size*sizeof(dctcoef) );
2173 memcpy( dct2, buf1, size*sizeof(dctcoef) );
2174 memcpy( buf3+256, buf3, 256 );
2175 call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
2176 call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
2177 if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
2179 call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
2180 call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
2183 report( "denoise dct :" );
2185 #define TEST_DECIMATE( decname, w, ac, thresh ) \
2186 if( qf_a.decname != qf_ref.decname ) \
2188 set_func_name( #decname ); \
2190 for( int i = 0; i < 100; i++ ) \
2192 static const int distrib[16] = {1,1,1,1,1,1,1,1,1,1,1,1,2,3,4};\
2193 static const int zerorate_lut[4] = {3,7,15,31};\
2194 int zero_rate = zerorate_lut[i&3];\
2195 for( int idx = 0; idx < w*w; idx++ ) \
2197 int sign = (rand()&1) ? -1 : 1; \
2198 int abs_level = distrib[rand()&15]; \
2199 if( abs_level == 4 ) abs_level = rand()&0x3fff; \
2200 int zero = !(rand()&zero_rate); \
2201 dct1[idx] = zero * abs_level * sign; \
2205 int result_c = call_c( qf_c.decname, dct1 ); \
2206 int result_a = call_a( qf_a.decname, dct1 ); \
2207 if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
2210 fprintf( stderr, #decname ": [FAILED]\n" ); \
2216 ok = 1; used_asm = 0;
2217 TEST_DECIMATE( decimate_score64, 8, 0, 6 );
2218 TEST_DECIMATE( decimate_score16, 4, 0, 6 );
2219 TEST_DECIMATE( decimate_score15, 4, 1, 7 );
2220 report( "decimate_score :" );
2222 #define TEST_LAST( last, lastname, size, ac ) \
2223 if( qf_a.last != qf_ref.last ) \
2225 set_func_name( #lastname ); \
2227 for( int i = 0; i < 100; i++ ) \
2230 int max = rand() & (size-1); \
2231 memset( dct1, 0, size*sizeof(dctcoef) ); \
2232 for( int idx = ac; idx < max; idx++ ) \
2233 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2236 int result_c = call_c( qf_c.last, dct1+ac ); \
2237 int result_a = call_a( qf_a.last, dct1+ac ); \
2238 if( result_c != result_a ) \
2241 fprintf( stderr, #lastname ": [FAILED]\n" ); \
2247 ok = 1; used_asm = 0;
2248 TEST_LAST( coeff_last4 , coeff_last4, 4, 0 );
2249 TEST_LAST( coeff_last8 , coeff_last8, 8, 0 );
2250 TEST_LAST( coeff_last[ DCT_LUMA_AC], coeff_last15, 16, 1 );
2251 TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 16, 0 );
2252 TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 64, 0 );
2253 report( "coeff_last :" );
2255 #define TEST_LEVELRUN( lastname, name, size, ac ) \
2256 if( qf_a.lastname != qf_ref.lastname ) \
2258 set_func_name( #name ); \
2260 for( int i = 0; i < 100; i++ ) \
2262 x264_run_level_t runlevel_c, runlevel_a; \
2264 int max = rand() & (size-1); \
2265 memset( dct1, 0, size*sizeof(dctcoef) ); \
2266 memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
2267 memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
2268 for( int idx = ac; idx < max; idx++ ) \
2269 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2272 int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
2273 int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
2274 if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
2275 runlevel_c.mask != runlevel_a.mask || \
2276 memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c)) \
2279 fprintf( stderr, #name ": [FAILED]\n" ); \
2285 ok = 1; used_asm = 0;
2286 TEST_LEVELRUN( coeff_level_run4 , coeff_level_run4, 4, 0 );
2287 TEST_LEVELRUN( coeff_level_run8 , coeff_level_run8, 8, 0 );
2288 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_AC], coeff_level_run15, 16, 1 );
2289 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 16, 0 );
2290 report( "coeff_level_run :" );
2295 static int check_intra( int cpu_ref, int cpu_new )
2297 int ret = 0, ok = 1, used_asm = 0;
2298 ALIGNED_ARRAY_32( pixel, edge,[36] );
2299 ALIGNED_ARRAY_32( pixel, edge2,[36] );
2300 ALIGNED_ARRAY_32( pixel, fdec,[FDEC_STRIDE*20] );
2303 x264_predict_t predict_16x16[4+3];
2304 x264_predict_t predict_8x8c[4+3];
2305 x264_predict_t predict_8x16c[4+3];
2306 x264_predict8x8_t predict_8x8[9+3];
2307 x264_predict_t predict_4x4[9+3];
2308 x264_predict_8x8_filter_t predict_8x8_filter;
2309 } ip_c, ip_ref, ip_a;
2311 x264_predict_16x16_init( 0, ip_c.predict_16x16 );
2312 x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
2313 x264_predict_8x16c_init( 0, ip_c.predict_8x16c );
2314 x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
2315 x264_predict_4x4_init( 0, ip_c.predict_4x4 );
2317 x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
2318 x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
2319 x264_predict_8x16c_init( cpu_ref, ip_ref.predict_8x16c );
2320 x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
2321 x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
2323 x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
2324 x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
2325 x264_predict_8x16c_init( cpu_new, ip_a.predict_8x16c );
2326 x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
2327 x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
2329 memcpy( fdec, pbuf1, 32*20 * sizeof(pixel) );\
2331 ip_c.predict_8x8_filter( fdec+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
2333 #define INTRA_TEST( name, dir, w, h, align, bench, ... )\
2334 if( ip_a.name[dir] != ip_ref.name[dir] )\
2336 set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
2338 memcpy( pbuf3, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2339 memcpy( pbuf4, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2340 for( int a = 0; a < (do_bench ? 64/sizeof(pixel) : 1); a += align )\
2342 call_c##bench( ip_c.name[dir], pbuf3+48+a, ##__VA_ARGS__ );\
2343 call_a##bench( ip_a.name[dir], pbuf4+48+a, ##__VA_ARGS__ );\
2344 if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*20 * sizeof(pixel) ) )\
2346 fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\
2348 if( ip_c.name == (void *)ip_c.predict_8x8 )\
2350 for( int k = -1; k < 16; k++ )\
2351 printf( "%2x ", edge[16+k] );\
2354 for( int j = 0; j < h; j++ )\
2356 if( ip_c.name == (void *)ip_c.predict_8x8 )\
2357 printf( "%2x ", edge[14-j] );\
2358 for( int k = 0; k < w; k++ )\
2359 printf( "%2x ", pbuf4[48+k+j*FDEC_STRIDE] );\
2363 for( int j = 0; j < h; j++ )\
2365 if( ip_c.name == (void *)ip_c.predict_8x8 )\
2367 for( int k = 0; k < w; k++ )\
2368 printf( "%2x ", pbuf3[48+k+j*FDEC_STRIDE] );\
2376 for( int i = 0; i < 12; i++ )
2377 INTRA_TEST( predict_4x4, i, 4, 4, 4, );
2378 for( int i = 0; i < 7; i++ )
2379 INTRA_TEST( predict_8x8c, i, 8, 8, 16, );
2380 for( int i = 0; i < 7; i++ )
2381 INTRA_TEST( predict_8x16c, i, 8, 16, 16, );
2382 for( int i = 0; i < 7; i++ )
2383 INTRA_TEST( predict_16x16, i, 16, 16, 16, );
2384 for( int i = 0; i < 12; i++ )
2385 INTRA_TEST( predict_8x8, i, 8, 8, 8, , edge );
2387 set_func_name("intra_predict_8x8_filter");
2388 if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
2391 for( int i = 0; i < 32; i++ )
2393 if( !(i&7) || ((i&MB_TOPRIGHT) && !(i&MB_TOP)) )
2395 int neighbor = (i&24)>>1;
2396 memset( edge, 0, 36*sizeof(pixel) );
2397 memset( edge2, 0, 36*sizeof(pixel) );
2398 call_c( ip_c.predict_8x8_filter, pbuf1+48, edge, neighbor, i&7 );
2399 call_a( ip_a.predict_8x8_filter, pbuf1+48, edge2, neighbor, i&7 );
2400 if( !(neighbor&MB_TOPLEFT) )
2401 edge[15] = edge2[15] = 0;
2402 if( memcmp( edge+7, edge2+7, (i&MB_TOPRIGHT ? 26 : i&MB_TOP ? 17 : 8) * sizeof(pixel) ) )
2404 fprintf( stderr, "predict_8x8_filter : [FAILED] %d %d\n", (i&24)>>1, i&7);
2410 #define EXTREMAL_PLANE( w, h ) \
2413 for( int j = 0; j < 7; j++ ) \
2414 max[j] = test ? rand()&PIXEL_MAX : PIXEL_MAX; \
2415 fdec[48-1-FDEC_STRIDE] = (i&1)*max[0]; \
2416 for( int j = 0; j < w/2; j++ ) \
2417 fdec[48+j-FDEC_STRIDE] = (!!(i&2))*max[1]; \
2418 for( int j = w/2; j < w-1; j++ ) \
2419 fdec[48+j-FDEC_STRIDE] = (!!(i&4))*max[2]; \
2420 fdec[48+(w-1)-FDEC_STRIDE] = (!!(i&8))*max[3]; \
2421 for( int j = 0; j < h/2; j++ ) \
2422 fdec[48+j*FDEC_STRIDE-1] = (!!(i&16))*max[4]; \
2423 for( int j = h/2; j < h-1; j++ ) \
2424 fdec[48+j*FDEC_STRIDE-1] = (!!(i&32))*max[5]; \
2425 fdec[48+(h-1)*FDEC_STRIDE-1] = (!!(i&64))*max[6]; \
2427 /* Extremal test case for planar prediction. */
2428 for( int test = 0; test < 100 && ok; test++ )
2429 for( int i = 0; i < 128 && ok; i++ )
2431 EXTREMAL_PLANE( 8, 8 );
2432 INTRA_TEST( predict_8x8c, I_PRED_CHROMA_P, 8, 8, 64, 1 );
2433 EXTREMAL_PLANE( 8, 16 );
2434 INTRA_TEST( predict_8x16c, I_PRED_CHROMA_P, 8, 16, 64, 1 );
2435 EXTREMAL_PLANE( 16, 16 );
2436 INTRA_TEST( predict_16x16, I_PRED_16x16_P, 16, 16, 64, 1 );
2438 report( "intra pred :" );
2442 #define DECL_CABAC(cpu) \
2443 static void run_cabac_decision_##cpu( x264_t *h, uint8_t *dst )\
2446 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2447 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2448 for( int i = 0; i < 0x1000; i++ )\
2449 x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
2451 static void run_cabac_bypass_##cpu( x264_t *h, uint8_t *dst )\
2454 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2455 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2456 for( int i = 0; i < 0x1000; i++ )\
2457 x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
2459 static void run_cabac_terminal_##cpu( x264_t *h, uint8_t *dst )\
2462 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2463 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2464 for( int i = 0; i < 0x1000; i++ )\
2465 x264_cabac_encode_terminal_##cpu( &cb );\
2470 #elif defined(ARCH_AARCH64)
2473 #define run_cabac_decision_asm run_cabac_decision_c
2474 #define run_cabac_bypass_asm run_cabac_bypass_c
2475 #define run_cabac_terminal_asm run_cabac_terminal_c
2478 extern const uint8_t x264_count_cat_m1[14];
2479 void x264_cabac_block_residual_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2480 void x264_cabac_block_residual_8x8_rd_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2481 void x264_cabac_block_residual_rd_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2483 static int check_cabac( int cpu_ref, int cpu_new )
2485 int ret = 0, ok = 1, used_asm = 0;
2487 h.sps->i_chroma_format_idc = 3;
2489 x264_bitstream_function_t bs_ref;
2490 x264_bitstream_function_t bs_a;
2491 x264_bitstream_init( cpu_ref, &bs_ref );
2492 x264_bitstream_init( cpu_new, &bs_a );
2493 x264_quant_init( &h, cpu_new, &h.quantf );
2494 h.quantf.coeff_last[DCT_CHROMA_DC] = h.quantf.coeff_last4;
2496 #define CABAC_RESIDUAL(name, start, end, rd)\
2498 if( bs_a.name##_internal && (bs_a.name##_internal != bs_ref.name##_internal || (cpu_new&X264_CPU_SSE2_IS_SLOW)) )\
2501 set_func_name( #name );\
2502 for( int i = 0; i < 2; i++ )\
2504 for( intptr_t ctx_block_cat = start; ctx_block_cat <= end; ctx_block_cat++ )\
2506 for( int j = 0; j < 256; j++ )\
2508 ALIGNED_ARRAY_N( dctcoef, dct, [2],[64] );\
2509 uint8_t bitstream[2][1<<16];\
2510 static const uint8_t ctx_ac[14] = {0,1,0,0,1,0,0,1,0,0,0,1,0,0};\
2511 int ac = ctx_ac[ctx_block_cat];\
2515 for( int k = 0; k <= x264_count_cat_m1[ctx_block_cat]; k++ )\
2517 /* Very rough distribution that covers possible inputs */\
2519 int coef = !(rnd&3);\
2520 coef += !(rnd& 15) * (rand()&0x0006);\
2521 coef += !(rnd& 63) * (rand()&0x0008);\
2522 coef += !(rnd& 255) * (rand()&0x00F0);\
2523 coef += !(rnd&1023) * (rand()&0x7F00);\
2524 nz |= dct[0][ac+k] = dct[1][ac+k] = coef * ((rand()&1) ? 1 : -1);\
2527 h.mb.b_interlaced = i;\
2528 x264_cabac_t cb[2];\
2529 x264_cabac_context_init( &h, &cb[0], SLICE_TYPE_P, 26, 0 );\
2530 x264_cabac_context_init( &h, &cb[1], SLICE_TYPE_P, 26, 0 );\
2531 x264_cabac_encode_init( &cb[0], bitstream[0], bitstream[0]+0xfff0 );\
2532 x264_cabac_encode_init( &cb[1], bitstream[1], bitstream[1]+0xfff0 );\
2533 cb[0].f8_bits_encoded = 0;\
2534 cb[1].f8_bits_encoded = 0;\
2535 if( !rd ) memcpy( bitstream[1], bitstream[0], 0x400 );\
2536 call_c1( x264_##name##_c, &h, &cb[0], ctx_block_cat, dct[0]+ac );\
2537 call_a1( bs_a.name##_internal, dct[1]+ac, i, ctx_block_cat, &cb[1] );\
2538 ok = cb[0].f8_bits_encoded == cb[1].f8_bits_encoded && !memcmp(cb[0].state, cb[1].state, 1024);\
2539 if( !rd ) ok |= !memcmp( bitstream[1], bitstream[0], 0x400 ) && !memcmp( &cb[1], &cb[0], offsetof(x264_cabac_t, p_start) );\
2542 fprintf( stderr, #name " : [FAILED] ctx_block_cat %d", (int)ctx_block_cat );\
2543 if( rd && cb[0].f8_bits_encoded != cb[1].f8_bits_encoded )\
2544 fprintf( stderr, " (%d != %d)", cb[0].f8_bits_encoded, cb[1].f8_bits_encoded );\
2545 fprintf( stderr, "\n");\
2550 call_c2( x264_##name##_c, &h, &cb[0], ctx_block_cat, dct[0]+ac );\
2551 call_a2( bs_a.name##_internal, dct[1]+ac, i, ctx_block_cat, &cb[1] );\
2560 CABAC_RESIDUAL( cabac_block_residual, 0, DCT_LUMA_8x8, 0 )
2561 report( "cabac residual:" );
2563 ok = 1; used_asm = 0;
2564 CABAC_RESIDUAL( cabac_block_residual_rd, 0, DCT_LUMA_8x8-1, 1 )
2565 CABAC_RESIDUAL( cabac_block_residual_8x8_rd, DCT_LUMA_8x8, DCT_LUMA_8x8, 1 )
2566 report( "cabac residual rd:" );
2568 if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
2570 ok = 1; used_asm = 0;
2571 x264_cabac_init( &h );
2573 set_func_name( "cabac_encode_decision" );
2574 memcpy( buf4, buf3, 0x1000 );
2575 call_c( run_cabac_decision_c, &h, buf3 );
2576 call_a( run_cabac_decision_asm, &h, buf4 );
2577 ok = !memcmp( buf3, buf4, 0x1000 );
2578 report( "cabac decision:" );
2580 set_func_name( "cabac_encode_bypass" );
2581 memcpy( buf4, buf3, 0x1000 );
2582 call_c( run_cabac_bypass_c, &h, buf3 );
2583 call_a( run_cabac_bypass_asm, &h, buf4 );
2584 ok = !memcmp( buf3, buf4, 0x1000 );
2585 report( "cabac bypass:" );
2587 set_func_name( "cabac_encode_terminal" );
2588 memcpy( buf4, buf3, 0x1000 );
2589 call_c( run_cabac_terminal_c, &h, buf3 );
2590 call_a( run_cabac_terminal_asm, &h, buf4 );
2591 ok = !memcmp( buf3, buf4, 0x1000 );
2592 report( "cabac terminal:" );
2597 static int check_bitstream( int cpu_ref, int cpu_new )
2599 x264_bitstream_function_t bs_c;
2600 x264_bitstream_function_t bs_ref;
2601 x264_bitstream_function_t bs_a;
2603 int ret = 0, ok = 1, used_asm = 0;
2605 x264_bitstream_init( 0, &bs_c );
2606 x264_bitstream_init( cpu_ref, &bs_ref );
2607 x264_bitstream_init( cpu_new, &bs_a );
2608 if( bs_a.nal_escape != bs_ref.nal_escape )
2611 uint8_t *input = malloc(size+100);
2612 uint8_t *output1 = malloc(size*2);
2613 uint8_t *output2 = malloc(size*2);
2615 set_func_name( "nal_escape" );
2616 for( int i = 0; i < 100; i++ )
2618 /* Test corner-case sizes */
2619 int test_size = i < 10 ? i+1 : rand() & 0x3fff;
2620 /* Test 8 different probability distributions of zeros */
2621 for( int j = 0; j < test_size+32; j++ )
2622 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
2623 uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
2624 uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
2625 int size_c = end_c-output1;
2626 int size_a = end_a-output2;
2627 if( size_c != size_a || memcmp( output1, output2, size_c ) )
2629 fprintf( stderr, "nal_escape : [FAILED] %d %d\n", size_c, size_a );
2634 for( int j = 0; j < size+32; j++ )
2636 call_c2( bs_c.nal_escape, output1, input, input+size );
2637 call_a2( bs_a.nal_escape, output2, input, input+size );
2642 report( "nal escape:" );
2647 static int check_all_funcs( int cpu_ref, int cpu_new )
2649 return check_pixel( cpu_ref, cpu_new )
2650 + check_dct( cpu_ref, cpu_new )
2651 + check_mc( cpu_ref, cpu_new )
2652 + check_intra( cpu_ref, cpu_new )
2653 + check_deblock( cpu_ref, cpu_new )
2654 + check_quant( cpu_ref, cpu_new )
2655 + check_cabac( cpu_ref, cpu_new )
2656 + check_bitstream( cpu_ref, cpu_new );
2659 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
2661 *cpu_ref = *cpu_new;
2663 #if STACK_ALIGNMENT < 16
2664 *cpu_new |= X264_CPU_STACK_MOD4;
2666 if( *cpu_new & X264_CPU_SSE2_IS_FAST )
2667 *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
2669 fprintf( stderr, "x264: %s\n", name );
2670 return check_all_funcs( *cpu_ref, *cpu_new );
2673 static int check_all_flags( void )
2676 int cpu0 = 0, cpu1 = 0;
2677 uint32_t cpu_detect = x264_cpu_detect();
2679 if( cpu_detect & X264_CPU_MMX2 )
2681 ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMX2, "MMX" );
2682 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
2683 cpu1 &= ~X264_CPU_CACHELINE_64;
2685 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
2686 cpu1 &= ~X264_CPU_CACHELINE_32;
2688 if( cpu_detect & X264_CPU_LZCNT )
2690 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX LZCNT" );
2691 cpu1 &= ~X264_CPU_LZCNT;
2693 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
2694 cpu1 &= ~X264_CPU_SLOW_CTZ;
2696 if( cpu_detect & X264_CPU_SSE )
2697 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE, "SSE" );
2698 if( cpu_detect & X264_CPU_SSE2 )
2700 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
2701 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
2702 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
2703 cpu1 &= ~X264_CPU_CACHELINE_64;
2704 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_SHUFFLE, "SSE2 SlowShuffle" );
2705 cpu1 &= ~X264_CPU_SLOW_SHUFFLE;
2706 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
2707 cpu1 &= ~X264_CPU_SLOW_CTZ;
2708 if( cpu_detect & X264_CPU_LZCNT )
2710 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE2 LZCNT" );
2711 cpu1 &= ~X264_CPU_LZCNT;
2714 if( cpu_detect & X264_CPU_SSE3 )
2716 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
2717 cpu1 &= ~X264_CPU_CACHELINE_64;
2719 if( cpu_detect & X264_CPU_SSSE3 )
2721 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
2722 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
2723 cpu1 &= ~X264_CPU_CACHELINE_64;
2724 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_SHUFFLE, "SSSE3 SlowShuffle" );
2725 cpu1 &= ~X264_CPU_SLOW_SHUFFLE;
2726 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
2727 cpu1 &= ~X264_CPU_SLOW_CTZ;
2728 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
2729 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64 SlowAtom" );
2730 cpu1 &= ~X264_CPU_CACHELINE_64;
2731 cpu1 &= ~X264_CPU_SLOW_ATOM;
2732 if( cpu_detect & X264_CPU_LZCNT )
2734 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSSE3 LZCNT" );
2735 cpu1 &= ~X264_CPU_LZCNT;
2738 if( cpu_detect & X264_CPU_SSE4 )
2739 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
2740 if( cpu_detect & X264_CPU_SSE42 )
2741 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE42, "SSE4.2" );
2742 if( cpu_detect & X264_CPU_AVX )
2743 ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX, "AVX" );
2744 if( cpu_detect & X264_CPU_XOP )
2745 ret |= add_flags( &cpu0, &cpu1, X264_CPU_XOP, "XOP" );
2746 if( cpu_detect & X264_CPU_FMA4 )
2748 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA4, "FMA4" );
2749 cpu1 &= ~X264_CPU_FMA4;
2751 if( cpu_detect & X264_CPU_FMA3 )
2753 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA3, "FMA3" );
2754 cpu1 &= ~X264_CPU_FMA3;
2756 if( cpu_detect & X264_CPU_AVX2 )
2758 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA3 | X264_CPU_AVX2, "AVX2" );
2759 if( cpu_detect & X264_CPU_LZCNT )
2761 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "AVX2 LZCNT" );
2762 cpu1 &= ~X264_CPU_LZCNT;
2765 if( cpu_detect & X264_CPU_BMI1 )
2767 ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI1, "BMI1" );
2768 cpu1 &= ~X264_CPU_BMI1;
2770 if( cpu_detect & X264_CPU_BMI2 )
2772 ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI1|X264_CPU_BMI2, "BMI2" );
2773 cpu1 &= ~(X264_CPU_BMI1|X264_CPU_BMI2);
2776 if( cpu_detect & X264_CPU_ALTIVEC )
2778 fprintf( stderr, "x264: ALTIVEC against C\n" );
2779 ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
2782 if( cpu_detect & X264_CPU_ARMV6 )
2783 ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
2784 if( cpu_detect & X264_CPU_NEON )
2785 ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
2786 if( cpu_detect & X264_CPU_FAST_NEON_MRC )
2787 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
2789 if( cpu_detect & X264_CPU_ARMV8 )
2790 ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV8, "ARMv8" );
2791 if( cpu_detect & X264_CPU_NEON )
2792 ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
2794 if( cpu_detect & X264_CPU_MSA )
2795 ret |= add_flags( &cpu0, &cpu1, X264_CPU_MSA, "MSA" );
2800 int main(int argc, char *argv[])
2804 if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
2806 #if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM && !ARCH_AARCH64 && !ARCH_MIPS
2807 fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
2811 if( argv[1][7] == '=' )
2813 bench_pattern = argv[1]+8;
2814 bench_pattern_len = strlen(bench_pattern);
2820 int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
2821 fprintf( stderr, "x264: using random seed %u\n", seed );
2824 buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 32*BENCH_ALIGNS );
2825 pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 32*BENCH_ALIGNS );
2826 if( !buf1 || !pbuf1 )
2828 fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
2831 #define INIT_POINTER_OFFSETS\
2832 buf2 = buf1 + 0xf00;\
2833 buf3 = buf2 + 0xf00;\
2834 buf4 = buf3 + 0x1000*sizeof(pixel);\
2835 pbuf2 = pbuf1 + 0xf00;\
2836 pbuf3 = (pixel*)buf3;\
2837 pbuf4 = (pixel*)buf4;
2838 INIT_POINTER_OFFSETS;
2839 for( int i = 0; i < 0x1e00; i++ )
2841 buf1[i] = rand() & 0xFF;
2842 pbuf1[i] = rand() & PIXEL_MAX;
2844 memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) );
2846 /* 32-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
2848 for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
2850 INIT_POINTER_OFFSETS;
2851 ret |= x264_stack_pagealign( check_all_flags, i*32 );
2855 fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
2858 ret = x264_stack_pagealign( check_all_flags, 0 );
2862 fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
2865 fprintf( stderr, "x264: All tests passed Yeah :)\n" );