1 /*****************************************************************************
2 * checkasm.c: assembly check tool
3 *****************************************************************************
4 * Copyright (C) 2003-2011 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Laurent Aimar <fenrir@via.ecp.fr>
8 * Fiona Glaser <fiona@x264.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
29 #include "common/common.h"
30 #include "common/cpu.h"
32 // GCC doesn't align stack variables on ARM, so use .bss
35 #define ALIGNED_16( var ) DECLARE_ALIGNED( static var, 16 )
38 /* buf1, buf2: initialised to random data and shouldn't write into them */
40 /* buf3, buf4: used to store output */
42 /* pbuf1, pbuf2: initialised to random pixel data and shouldn't write into them. */
44 /* pbuf3, pbuf4: point to buf3, buf4, just for type convenience */
49 #define report( name ) { \
50 if( used_asm && !quiet ) \
51 fprintf( stderr, " - %-21s [%s]\n", name, ok ? "OK" : "FAILED" ); \
55 #define BENCH_RUNS 100 // tradeoff between accuracy and speed
56 #define BENCH_ALIGNS 16 // number of stack+heap data alignments (another accuracy vs speed tradeoff)
57 #define MAX_FUNCS 1000 // just has to be big enough to hold all the existing functions
58 #define MAX_CPUS 30 // number of different combinations of cpu flags
62 void *pointer; // just for detecting duplicates
71 bench_t vers[MAX_CPUS];
75 int bench_pattern_len = 0;
76 const char *bench_pattern = "";
78 static bench_func_t benchs[MAX_FUNCS];
80 static const char *pixel_names[12] = { "16x16", "16x8", "8x16", "8x8", "8x4", "4x8", "4x4", "4x16", "4x2", "2x8", "2x4", "2x2" };
81 static const char *intra_predict_16x16_names[7] = { "v", "h", "dc", "p", "dcl", "dct", "dc8" };
82 static const char *intra_predict_8x8c_names[7] = { "dc", "h", "v", "p", "dcl", "dct", "dc8" };
83 static const char *intra_predict_4x4_names[12] = { "v", "h", "dc", "ddl", "ddr", "vr", "hd", "vl", "hu", "dcl", "dct", "dc8" };
84 static const char **intra_predict_8x8_names = intra_predict_4x4_names;
85 static const char **intra_predict_8x16c_names = intra_predict_8x8c_names;
87 #define set_func_name(...) snprintf( func_name, sizeof(func_name), __VA_ARGS__ )
89 static inline uint32_t read_time(void)
92 #if HAVE_X86_INLINE_ASM
93 asm volatile( "rdtsc" :"=a"(a) ::"edx" );
95 asm volatile( "mftb %0" : "=r" (a) );
96 #elif ARCH_ARM // ARMv7 only
97 asm volatile( "mrc p15, 0, %0, c9, c13, 0" : "=r"(a) );
102 static bench_t* get_bench( const char *name, int cpu )
105 for( i = 0; benchs[i].name && strcmp(name, benchs[i].name); i++ )
106 assert( i < MAX_FUNCS );
107 if( !benchs[i].name )
108 benchs[i].name = strdup( name );
110 return &benchs[i].vers[0];
111 for( j = 1; benchs[i].vers[j].cpu && benchs[i].vers[j].cpu != cpu; j++ )
112 assert( j < MAX_CPUS );
113 benchs[i].vers[j].cpu = cpu;
114 return &benchs[i].vers[j];
117 static int cmp_nop( const void *a, const void *b )
119 return *(uint16_t*)a - *(uint16_t*)b;
122 static int cmp_bench( const void *a, const void *b )
124 // asciibetical sort except preserving numbers
125 const char *sa = ((bench_func_t*)a)->name;
126 const char *sb = ((bench_func_t*)b)->name;
131 if( isdigit( *sa ) && isdigit( *sb ) && isdigit( sa[1] ) != isdigit( sb[1] ) )
132 return isdigit( sa[1] ) - isdigit( sb[1] );
138 static void print_bench(void)
140 uint16_t nops[10000] = {0};
141 int nfuncs, nop_time=0;
143 for( int i = 0; i < 10000; i++ )
146 nops[i] = read_time() - t;
148 qsort( nops, 10000, sizeof(uint16_t), cmp_nop );
149 for( int i = 500; i < 9500; i++ )
152 printf( "nop: %d\n", nop_time );
154 for( nfuncs = 0; nfuncs < MAX_FUNCS && benchs[nfuncs].name; nfuncs++ );
155 qsort( benchs, nfuncs, sizeof(bench_func_t), cmp_bench );
156 for( int i = 0; i < nfuncs; i++ )
157 for( int j = 0; j < MAX_CPUS && (!j || benchs[i].vers[j].cpu); j++ )
160 bench_t *b = &benchs[i].vers[j];
163 for( k = 0; k < j && benchs[i].vers[k].pointer != b->pointer; k++ );
166 printf( "%s_%s%s: %"PRId64"\n", benchs[i].name,
167 b->cpu&X264_CPU_FMA4 ? "fma4" :
168 b->cpu&X264_CPU_XOP ? "xop" :
169 b->cpu&X264_CPU_AVX ? "avx" :
170 b->cpu&X264_CPU_SSE4 ? "sse4" :
171 b->cpu&X264_CPU_SSSE3 ? "ssse3" :
172 b->cpu&X264_CPU_SSE3 ? "sse3" :
173 /* print sse2slow only if there's also a sse2fast version of the same func */
174 b->cpu&X264_CPU_SSE2_IS_SLOW && j<MAX_CPUS-1 && b[1].cpu&X264_CPU_SSE2_IS_FAST && !(b[1].cpu&X264_CPU_SSE3) ? "sse2slow" :
175 b->cpu&X264_CPU_SSE2 ? "sse2" :
176 b->cpu&X264_CPU_MMX ? "mmx" :
177 b->cpu&X264_CPU_ALTIVEC ? "altivec" :
178 b->cpu&X264_CPU_NEON ? "neon" :
179 b->cpu&X264_CPU_ARMV6 ? "armv6" : "c",
180 b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
181 b->cpu&X264_CPU_CACHELINE_64 ? "_c64" :
182 b->cpu&X264_CPU_SHUFFLE_IS_FAST && !(b->cpu&X264_CPU_SSE4) ? "_fastshuffle" :
183 b->cpu&X264_CPU_SSE_MISALIGN ? "_misalign" :
184 b->cpu&X264_CPU_LZCNT ? "_lzcnt" :
185 b->cpu&X264_CPU_FAST_NEON_MRC ? "_fast_mrc" :
186 b->cpu&X264_CPU_SLOW_CTZ ? "_slow_ctz" :
187 b->cpu&X264_CPU_SLOW_ATOM ? "_slow_atom" : "",
188 ((int64_t)10*b->cycles/b->den - nop_time)/4 );
192 #if ARCH_X86 || ARCH_X86_64
193 int x264_stack_pagealign( int (*func)(), int align );
195 #define x264_stack_pagealign( func, align ) func()
198 #define call_c1(func,...) func(__VA_ARGS__)
200 #if ARCH_X86 || defined(_WIN64)
201 /* detect when callee-saved regs aren't saved.
202 * needs an explicit asm check because it only sometimes crashes in normal use. */
203 intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... );
204 #define call_a1(func,...) x264_checkasm_call((intptr_t(*)())func, &ok, __VA_ARGS__)
206 #define call_a1 call_c1
209 #define call_bench(func,cpu,...)\
210 if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
214 call_a1(func, __VA_ARGS__);\
215 for( int ti = 0; ti < (cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
217 uint32_t t = read_time();\
222 t = read_time() - t;\
223 if( t*tcount <= tsum*4 && ti > 0 )\
229 bench_t *b = get_bench( func_name, cpu );\
235 /* for most functions, run benchmark and correctness test at the same time.
236 * for those that modify their inputs, run the above macros separately */
237 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
238 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
239 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
240 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
243 static int check_pixel( int cpu_ref, int cpu_new )
245 x264_pixel_function_t pixel_c;
246 x264_pixel_function_t pixel_ref;
247 x264_pixel_function_t pixel_asm;
248 x264_predict_t predict_4x4[12];
249 x264_predict8x8_t predict_8x8[12];
250 x264_predict_8x8_filter_t predict_8x8_filter;
251 ALIGNED_16( pixel edge[36] );
252 uint16_t cost_mv[32];
253 int ret = 0, ok, used_asm;
255 x264_pixel_init( 0, &pixel_c );
256 x264_pixel_init( cpu_ref, &pixel_ref );
257 x264_pixel_init( cpu_new, &pixel_asm );
258 x264_predict_4x4_init( 0, predict_4x4 );
259 x264_predict_8x8_init( 0, predict_8x8, &predict_8x8_filter );
260 predict_8x8_filter( pbuf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
263 for( int i = 0; i < 256; i++ )
268 pbuf4[i] = -(z&1) & PIXEL_MAX;
269 pbuf3[i] = ~pbuf4[i] & PIXEL_MAX;
271 // random pattern made of maxed pixel differences, in case an intermediate value overflows
272 for( int i = 256; i < 0x1000; i++ )
274 pbuf4[i] = -(pbuf1[i&~0x88]&1) & PIXEL_MAX;
275 pbuf3[i] = ~(pbuf4[i]) & PIXEL_MAX;
278 #define TEST_PIXEL( name, align ) \
279 ok = 1, used_asm = 0; \
280 for( int i = 0; i < 8; i++ ) \
282 int res_c, res_asm; \
283 if( pixel_asm.name[i] != pixel_ref.name[i] ) \
285 set_func_name( "%s_%s", #name, pixel_names[i] ); \
287 for( int j = 0; j < 64; j++ ) \
289 res_c = call_c( pixel_c.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \
290 res_asm = call_a( pixel_asm.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \
291 if( res_c != res_asm ) \
294 fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
298 for( int j = 0; j < 0x1000 && ok; j += 256 ) \
300 res_c = pixel_c .name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
301 res_asm = pixel_asm.name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
302 if( res_c != res_asm ) \
305 fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
310 report( "pixel " #name " :" );
312 TEST_PIXEL( sad, 0 );
313 TEST_PIXEL( sad_aligned, 1 );
314 TEST_PIXEL( ssd, 1 );
315 TEST_PIXEL( satd, 0 );
316 TEST_PIXEL( sa8d, 1 );
318 #define TEST_PIXEL_X( N ) \
319 ok = 1; used_asm = 0; \
320 for( int i = 0; i < 7; i++ ) \
322 int res_c[4]={0}, res_asm[4]={0}; \
323 if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
325 set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
327 for( int j = 0; j < 64; j++ ) \
329 pixel *pix2 = pbuf2+j; \
330 res_c[0] = pixel_c.sad[i]( pbuf1, 16, pix2, 64 ); \
331 res_c[1] = pixel_c.sad[i]( pbuf1, 16, pix2+6, 64 ); \
332 res_c[2] = pixel_c.sad[i]( pbuf1, 16, pix2+1, 64 ); \
335 res_c[3] = pixel_c.sad[i]( pbuf1, 16, pix2+10, 64 ); \
336 call_a( pixel_asm.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
339 call_a( pixel_asm.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
340 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
343 fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
344 i, res_c[0], res_c[1], res_c[2], res_c[3], \
345 res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
348 call_c2( pixel_c.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
350 call_c2( pixel_c.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
354 report( "pixel sad_x"#N" :" );
359 #define TEST_PIXEL_VAR( i ) \
360 if( pixel_asm.var[i] != pixel_ref.var[i] ) \
362 set_func_name( "%s_%s", "var", pixel_names[i] ); \
364 /* abi-check wrapper can't return uint64_t, so separate it from return value check */ \
365 call_c1( pixel_c.var[i], pbuf1, 16 ); \
366 call_a1( pixel_asm.var[i], pbuf1, 16 ); \
367 uint64_t res_c = pixel_c.var[i]( pbuf1, 16 ); \
368 uint64_t res_asm = pixel_asm.var[i]( pbuf1, 16 ); \
369 if( res_c != res_asm ) \
372 fprintf( stderr, "var[%d]: %d %d != %d %d [FAILED]\n", i, (int)res_c, (int)(res_c>>32), (int)res_asm, (int)(res_asm>>32) ); \
374 call_c2( pixel_c.var[i], pbuf1, 16 ); \
375 call_a2( pixel_asm.var[i], pbuf1, 16 ); \
378 ok = 1; used_asm = 0;
379 TEST_PIXEL_VAR( PIXEL_16x16 );
380 TEST_PIXEL_VAR( PIXEL_8x16 );
381 TEST_PIXEL_VAR( PIXEL_8x8 );
382 report( "pixel var :" );
384 #define TEST_PIXEL_VAR2( i ) \
385 if( pixel_asm.var2[i] != pixel_ref.var2[i] ) \
387 int res_c, res_asm, ssd_c, ssd_asm; \
388 set_func_name( "%s_%s", "var2", pixel_names[i] ); \
390 res_c = call_c( pixel_c.var2[i], pbuf1, 16, pbuf2, 16, &ssd_c ); \
391 res_asm = call_a( pixel_asm.var2[i], pbuf1, 16, pbuf2, 16, &ssd_asm ); \
392 if( res_c != res_asm || ssd_c != ssd_asm ) \
395 fprintf( stderr, "var2[%d]: %d != %d or %d != %d [FAILED]\n", i, res_c, res_asm, ssd_c, ssd_asm ); \
399 ok = 1; used_asm = 0;
400 TEST_PIXEL_VAR2( PIXEL_8x16 );
401 TEST_PIXEL_VAR2( PIXEL_8x8 );
402 report( "pixel var2 :" );
404 ok = 1; used_asm = 0;
405 for( int i = 0; i < 4; i++ )
406 if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
408 set_func_name( "hadamard_ac_%s", pixel_names[i] );
410 for( int j = 0; j < 32; j++ )
412 pixel *pix = (j&16 ? pbuf1 : pbuf3) + (j&15)*256;
413 call_c1( pixel_c.hadamard_ac[i], pbuf1, 16 );
414 call_a1( pixel_asm.hadamard_ac[i], pbuf1, 16 );
415 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
416 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
420 fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
424 call_c2( pixel_c.hadamard_ac[i], pbuf1, 16 );
425 call_a2( pixel_asm.hadamard_ac[i], pbuf1, 16 );
427 report( "pixel hadamard_ac :" );
430 for( int i = 0; i < 32; i++ )
431 for( int j = 0; j < 16; j++ )
432 pbuf4[16*i+j] = -((i+j)&1) & PIXEL_MAX;
433 ok = 1; used_asm = 0;
434 if( pixel_asm.vsad != pixel_ref.vsad )
436 for( int h = 2; h <= 32; h += 2 )
439 set_func_name( "vsad" );
441 for( int j = 0; j < 2 && ok; j++ )
443 pixel *p = j ? pbuf4 : pbuf1;
444 res_c = call_c( pixel_c.vsad, p, 16, h );
445 res_asm = call_a( pixel_asm.vsad, p, 16, h );
446 if( res_c != res_asm )
449 fprintf( stderr, "vsad: height=%d, %d != %d\n", h, res_c, res_asm );
455 report( "pixel vsad :" );
457 #define TEST_INTRA_X3( name, i8x8, ... ) \
458 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
460 int res_c[3], res_asm[3]; \
461 set_func_name( #name ); \
463 call_c( pixel_c.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_c ); \
464 call_a( pixel_asm.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_asm ); \
465 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
468 fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
469 res_c[0], res_c[1], res_c[2], \
470 res_asm[0], res_asm[1], res_asm[2] ); \
474 #define TEST_INTRA_X9( name, cmp ) \
475 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
477 set_func_name( #name ); \
479 ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
480 for( int i=0; i<17; i++ ) \
481 bitcosts[i] = 9*(i!=8); \
482 memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
483 memcpy( pbuf4, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
484 for( int i=0; i<32; i++ ) \
486 pixel *fenc = pbuf1+48+i*12; \
487 pixel *fdec1 = pbuf3+48+i*12; \
488 pixel *fdec2 = pbuf4+48+i*12; \
489 int pred_mode = i%9; \
490 int res_c = INT_MAX; \
491 for( int j=0; j<9; j++ ) \
493 predict_4x4[j]( fdec1 ); \
494 int cost = pixel_c.cmp[PIXEL_4x4]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
495 if( cost < (uint16_t)res_c ) \
496 res_c = cost + (j<<16); \
498 predict_4x4[res_c>>16]( fdec1 ); \
499 int res_a = call_a( pixel_asm.name, fenc, fdec2, bitcosts+8-pred_mode ); \
500 if( res_c != res_a ) \
503 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
506 if( memcmp(fdec1, fdec2, 4*FDEC_STRIDE*sizeof(pixel)) ) \
509 fprintf( stderr, #name" [FAILED]\n" ); \
510 for( int j=0; j<16; j++ ) \
511 fprintf( stderr, "%02x ", fdec1[(j&3)+(j>>2)*FDEC_STRIDE] ); \
512 fprintf( stderr, "\n" ); \
513 for( int j=0; j<16; j++ ) \
514 fprintf( stderr, "%02x ", fdec2[(j&3)+(j>>2)*FDEC_STRIDE] ); \
515 fprintf( stderr, "\n" ); \
521 #define TEST_INTRA8_X9( name, cmp ) \
522 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
524 set_func_name( #name ); \
526 ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
527 ALIGNED_ARRAY_16( uint16_t, satds_c,[16] ); \
528 ALIGNED_ARRAY_16( uint16_t, satds_a,[16] ); \
529 memset( satds_c, 0, 16 * sizeof(*satds_c) ); \
530 memset( satds_a, 0, 16 * sizeof(*satds_a) ); \
531 for( int i=0; i<17; i++ ) \
532 bitcosts[i] = 9*(i!=8); \
533 for( int i=0; i<32; i++ ) \
535 pixel *fenc = pbuf1+48+i*12; \
536 pixel *fdec1 = pbuf3+48+i*12; \
537 pixel *fdec2 = pbuf4+48+i*12; \
538 int pred_mode = i%9; \
539 int res_c = INT_MAX; \
540 predict_8x8_filter( fdec1, edge, ALL_NEIGHBORS, ALL_NEIGHBORS ); \
541 for( int j=0; j<9; j++ ) \
543 predict_8x8[j]( fdec1, edge ); \
544 satds_c[j] = pixel_c.cmp[PIXEL_8x8]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
545 if( satds_c[j] < (uint16_t)res_c ) \
546 res_c = satds_c[j] + (j<<16); \
548 predict_8x8[res_c>>16]( fdec1, edge ); \
549 int res_a = call_a( pixel_asm.name, fenc, fdec2, edge, bitcosts+8-pred_mode, satds_a ); \
550 if( res_c != res_a || memcmp(satds_c, satds_a, sizeof(satds_c)) ) \
553 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
554 for( int j = 0; j < 9; j++ ) \
555 fprintf( stderr, "%5d ", satds_c[j]); \
556 fprintf( stderr, "\n" ); \
557 for( int j = 0; j < 9; j++ ) \
558 fprintf( stderr, "%5d ", satds_a[j]); \
559 fprintf( stderr, "\n" ); \
562 for( int j=0; j<8; j++ ) \
563 if( memcmp(fdec1+j*FDEC_STRIDE, fdec2+j*FDEC_STRIDE, 8*sizeof(pixel)) ) \
567 fprintf( stderr, #name" [FAILED]\n" ); \
568 for( int j=0; j<8; j++ ) \
570 for( int k=0; k<8; k++ ) \
571 fprintf( stderr, "%02x ", fdec1[k+j*FDEC_STRIDE] ); \
572 fprintf( stderr, "\n" ); \
574 fprintf( stderr, "\n" ); \
575 for( int j=0; j<8; j++ ) \
577 for( int k=0; k<8; k++ ) \
578 fprintf( stderr, "%02x ", fdec2[k+j*FDEC_STRIDE] ); \
579 fprintf( stderr, "\n" ); \
581 fprintf( stderr, "\n" ); \
587 memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) );
588 ok = 1; used_asm = 0;
589 TEST_INTRA_X3( intra_satd_x3_16x16, 0 );
590 TEST_INTRA_X3( intra_satd_x3_8x16c, 0 );
591 TEST_INTRA_X3( intra_satd_x3_8x8c, 0 );
592 TEST_INTRA_X3( intra_sa8d_x3_8x8, 1, edge );
593 TEST_INTRA_X3( intra_satd_x3_4x4, 0 );
594 report( "intra satd_x3 :" );
595 ok = 1; used_asm = 0;
596 TEST_INTRA_X3( intra_sad_x3_16x16, 0 );
597 TEST_INTRA_X3( intra_sad_x3_8x16c, 0 );
598 TEST_INTRA_X3( intra_sad_x3_8x8c, 0 );
599 TEST_INTRA_X3( intra_sad_x3_8x8, 1, edge );
600 TEST_INTRA_X3( intra_sad_x3_4x4, 0 );
601 report( "intra sad_x3 :" );
602 ok = 1; used_asm = 0;
603 TEST_INTRA_X9( intra_satd_x9_4x4, satd );
604 TEST_INTRA8_X9( intra_sa8d_x9_8x8, sa8d );
605 report( "intra satd_x9 :" );
606 ok = 1; used_asm = 0;
607 TEST_INTRA_X9( intra_sad_x9_4x4, sad );
608 TEST_INTRA8_X9( intra_sad_x9_8x8, sad );
609 report( "intra sad_x9 :" );
611 ok = 1; used_asm = 0;
612 if( pixel_asm.ssd_nv12_core != pixel_ref.ssd_nv12_core )
615 set_func_name( "ssd_nv12" );
616 uint64_t res_u_c, res_v_c, res_u_a, res_v_a;
617 pixel_c.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c );
618 pixel_asm.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a );
619 if( res_u_c != res_u_a || res_v_c != res_v_a )
622 fprintf( stderr, "ssd_nv12: %"PRIu64",%"PRIu64" != %"PRIu64",%"PRIu64"\n",
623 res_u_c, res_v_c, res_u_a, res_v_a );
625 call_c( pixel_c.ssd_nv12_core, pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c );
626 call_a( pixel_asm.ssd_nv12_core, pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a );
628 report( "ssd_nv12 :" );
630 if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
631 pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
635 ALIGNED_16( int sums[5][4] ) = {{0}};
638 res_c = x264_pixel_ssim_wxh( &pixel_c, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
639 res_a = x264_pixel_ssim_wxh( &pixel_asm, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
640 if( fabs( res_c - res_a ) > 1e-6 )
643 fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
645 set_func_name( "ssim_core" );
646 call_c2( pixel_c.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums );
647 call_a2( pixel_asm.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums );
648 set_func_name( "ssim_end" );
649 call_c2( pixel_c.ssim_end4, sums, sums, 4 );
650 call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
654 ok = 1; used_asm = 0;
655 for( int i = 0; i < 32; i++ )
657 for( int i = 0; i < 100 && ok; i++ )
658 if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
660 ALIGNED_16( uint16_t sums[72] );
661 ALIGNED_16( int dc[4] );
662 ALIGNED_16( int16_t mvs_a[32] );
663 ALIGNED_16( int16_t mvs_c[32] );
665 int thresh = rand() & 0x3fff;
666 set_func_name( "esa_ads" );
667 for( int j = 0; j < 72; j++ )
668 sums[j] = rand() & 0x3fff;
669 for( int j = 0; j < 4; j++ )
670 dc[j] = rand() & 0x3fff;
672 mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
673 mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
674 if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
677 printf( "c%d: ", i&3 );
678 for( int j = 0; j < mvn_c; j++ )
679 printf( "%d ", mvs_c[j] );
680 printf( "\na%d: ", i&3 );
681 for( int j = 0; j < mvn_a; j++ )
682 printf( "%d ", mvs_a[j] );
686 report( "esa ads:" );
691 static int check_dct( int cpu_ref, int cpu_new )
693 x264_dct_function_t dct_c;
694 x264_dct_function_t dct_ref;
695 x264_dct_function_t dct_asm;
696 x264_quant_function_t qf;
697 int ret = 0, ok, used_asm, interlace = 0;
698 ALIGNED_16( dctcoef dct1[16][16] );
699 ALIGNED_16( dctcoef dct2[16][16] );
700 ALIGNED_16( dctcoef dct4[16][16] );
701 ALIGNED_16( dctcoef dct8[4][64] );
702 ALIGNED_16( dctcoef dctdc[2][8] );
706 x264_dct_init( 0, &dct_c );
707 x264_dct_init( cpu_ref, &dct_ref);
708 x264_dct_init( cpu_new, &dct_asm );
710 memset( h, 0, sizeof(*h) );
711 x264_param_default( &h->param );
712 h->sps->i_chroma_format_idc = 1;
713 h->chroma_qp_table = i_chroma_qp_table + 12;
714 h->param.analyse.i_luma_deadzone[0] = 0;
715 h->param.analyse.i_luma_deadzone[1] = 0;
716 h->param.analyse.b_transform_8x8 = 1;
717 for( int i = 0; i < 6; i++ )
718 h->pps->scaling_list[i] = x264_cqm_flat16;
720 x264_quant_init( h, 0, &qf );
722 /* overflow test cases */
723 for( int i = 0; i < 5; i++ )
725 pixel *enc = &pbuf3[16*i*FENC_STRIDE];
726 pixel *dec = &pbuf4[16*i*FDEC_STRIDE];
728 for( int j = 0; j < 16; j++ )
730 int cond_a = (i < 2) ? 1 : ((j&3) == 0 || (j&3) == (i-1));
731 int cond_b = (i == 0) ? 1 : !cond_a;
732 enc[0] = enc[1] = enc[4] = enc[5] = enc[8] = enc[9] = enc[12] = enc[13] = cond_a ? PIXEL_MAX : 0;
733 enc[2] = enc[3] = enc[6] = enc[7] = enc[10] = enc[11] = enc[14] = enc[15] = cond_b ? PIXEL_MAX : 0;
735 for( int k = 0; k < 4; k++ )
736 dec[k] = PIXEL_MAX - enc[k];
743 #define TEST_DCT( name, t1, t2, size ) \
744 if( dct_asm.name != dct_ref.name ) \
746 set_func_name( #name ); \
748 pixel *enc = pbuf3; \
749 pixel *dec = pbuf4; \
750 for( int j = 0; j < 5; j++) \
752 call_c( dct_c.name, t1, &pbuf1[j*64], &pbuf2[j*64] ); \
753 call_a( dct_asm.name, t2, &pbuf1[j*64], &pbuf2[j*64] ); \
754 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
757 fprintf( stderr, #name " [FAILED]\n" ); \
760 call_c( dct_c.name, t1, enc, dec ); \
761 call_a( dct_asm.name, t2, enc, dec ); \
762 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
765 fprintf( stderr, #name " [FAILED] (overflow)\n" ); \
768 enc += 16*FENC_STRIDE; \
769 dec += 16*FDEC_STRIDE; \
772 ok = 1; used_asm = 0;
773 TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16 );
774 TEST_DCT( sub8x8_dct, dct1, dct2, 16*4 );
775 TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4 );
776 TEST_DCT( sub8x16_dct_dc, dctdc[0], dctdc[1], 8 );
777 TEST_DCT( sub16x16_dct, dct1, dct2, 16*16 );
778 report( "sub_dct4 :" );
780 ok = 1; used_asm = 0;
781 TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64 );
782 TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*4 );
783 report( "sub_dct8 :" );
786 // fdct and idct are denormalized by different factors, so quant/dequant
787 // is needed to force the coefs into the right range.
788 dct_c.sub16x16_dct( dct4, pbuf1, pbuf2 );
789 dct_c.sub16x16_dct8( dct8, pbuf1, pbuf2 );
790 for( int i = 0; i < 16; i++ )
792 qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
793 qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
795 for( int i = 0; i < 4; i++ )
797 qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
798 qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
800 x264_cqm_delete( h );
802 #define TEST_IDCT( name, src ) \
803 if( dct_asm.name != dct_ref.name ) \
805 set_func_name( #name ); \
807 memcpy( pbuf3, pbuf1, 32*32 * sizeof(pixel) ); \
808 memcpy( pbuf4, pbuf1, 32*32 * sizeof(pixel) ); \
809 memcpy( dct1, src, 256 * sizeof(dctcoef) ); \
810 memcpy( dct2, src, 256 * sizeof(dctcoef) ); \
811 call_c1( dct_c.name, pbuf3, (void*)dct1 ); \
812 call_a1( dct_asm.name, pbuf4, (void*)dct2 ); \
813 if( memcmp( pbuf3, pbuf4, 32*32 * sizeof(pixel) ) ) \
816 fprintf( stderr, #name " [FAILED]\n" ); \
818 call_c2( dct_c.name, pbuf3, (void*)dct1 ); \
819 call_a2( dct_asm.name, pbuf4, (void*)dct2 ); \
821 ok = 1; used_asm = 0;
822 TEST_IDCT( add4x4_idct, dct4 );
823 TEST_IDCT( add8x8_idct, dct4 );
824 TEST_IDCT( add8x8_idct_dc, dct4 );
825 TEST_IDCT( add16x16_idct, dct4 );
826 TEST_IDCT( add16x16_idct_dc, dct4 );
827 report( "add_idct4 :" );
829 ok = 1; used_asm = 0;
830 TEST_IDCT( add8x8_idct8, dct8 );
831 TEST_IDCT( add16x16_idct8, dct8 );
832 report( "add_idct8 :" );
835 #define TEST_DCTDC( name )\
836 ok = 1; used_asm = 0;\
837 if( dct_asm.name != dct_ref.name )\
839 set_func_name( #name );\
841 uint16_t *p = (uint16_t*)buf1;\
842 for( int i = 0; i < 16 && ok; i++ )\
844 for( int j = 0; j < 16; j++ )\
845 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
846 : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
847 : ((*p++)&0x1fff)-0x1000; /* general case */\
848 memcpy( dct2, dct1, 16 * sizeof(dctcoef) );\
849 call_c1( dct_c.name, dct1[0] );\
850 call_a1( dct_asm.name, dct2[0] );\
851 if( memcmp( dct1, dct2, 16 * sizeof(dctcoef) ) )\
854 call_c2( dct_c.name, dct1[0] );\
855 call_a2( dct_asm.name, dct2[0] );\
857 report( #name " :" );
859 TEST_DCTDC( dct4x4dc );
860 TEST_DCTDC( idct4x4dc );
863 #define TEST_DCTDC_CHROMA( name )\
864 ok = 1; used_asm = 0;\
865 if( dct_asm.name != dct_ref.name )\
867 set_func_name( #name );\
869 uint16_t *p = (uint16_t*)buf1;\
870 for( int i = 0; i < 16 && ok; i++ )\
872 for( int j = 0; j < 8; j++ )\
873 dct1[j][0] = !i ? (j^j>>1^j>>2)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
874 : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
875 : ((*p++)&0x1fff)-0x1000; /* general case */\
876 memcpy( dct2, dct1, 8*16 * sizeof(dctcoef) );\
877 call_c1( dct_c.name, dctdc[0], dct1 );\
878 call_a1( dct_asm.name, dctdc[1], dct2 );\
879 if( memcmp( dctdc[0], dctdc[1], 8 * sizeof(dctcoef) ) || memcmp( dct1, dct2, 8*16 * sizeof(dctcoef) ) )\
882 fprintf( stderr, #name " [FAILED]\n" ); \
885 call_c2( dct_c.name, dctdc[0], dct1 );\
886 call_a2( dct_asm.name, dctdc[1], dct2 );\
888 report( #name " :" );
890 TEST_DCTDC_CHROMA( dct2x4dc );
891 #undef TEST_DCTDC_CHROMA
893 x264_zigzag_function_t zigzag_c[2];
894 x264_zigzag_function_t zigzag_ref[2];
895 x264_zigzag_function_t zigzag_asm[2];
897 ALIGNED_16( dctcoef level1[64] );
898 ALIGNED_16( dctcoef level2[64] );
900 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
901 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
903 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
905 memcpy(dct, buf1, size*sizeof(dctcoef)); \
906 call_c( zigzag_c[interlace].name, t1, dct ); \
907 call_a( zigzag_asm[interlace].name, t2, dct ); \
908 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
911 fprintf( stderr, #name " [FAILED]\n" ); \
915 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
916 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
919 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
921 memcpy( pbuf3, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
922 memcpy( pbuf4, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
923 nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
924 nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
925 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE*sizeof(pixel) ) || nz_c != nz_a ) \
928 fprintf( stderr, #name " [FAILED]\n" ); \
930 call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
931 call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
934 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
935 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
938 dctcoef dc_a, dc_c; \
939 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
941 for( int i = 0; i < 2; i++ ) \
943 memcpy( pbuf3, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
944 memcpy( pbuf4, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
945 for( int j = 0; j < 4; j++ ) \
947 memcpy( pbuf3 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
948 memcpy( pbuf4 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
950 nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
951 nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
952 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 ) \
955 fprintf( stderr, #name " [FAILED]\n" ); \
959 call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
960 call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
963 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
964 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
966 for( int j = 0; j < 100; j++ ) \
968 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
970 memcpy(dct, buf1, size*sizeof(dctcoef)); \
971 for( int i = 0; i < size; i++ ) \
972 dct[i] = rand()&0x1F ? 0 : dct[i]; \
973 memcpy(buf3, buf4, 10); \
974 call_c( zigzag_c[interlace].name, t1, dct, buf3 ); \
975 call_a( zigzag_asm[interlace].name, t2, dct, buf4 ); \
976 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( buf3, buf4, 10 ) ) \
983 x264_zigzag_init( 0, &zigzag_c[0], &zigzag_c[1] );
984 x264_zigzag_init( cpu_ref, &zigzag_ref[0], &zigzag_ref[1] );
985 x264_zigzag_init( cpu_new, &zigzag_asm[0], &zigzag_asm[1] );
987 ok = 1; used_asm = 0;
988 TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct1[0], 64 );
989 report( "zigzag_interleave :" );
991 for( interlace = 0; interlace <= 1; interlace++ )
993 ok = 1; used_asm = 0;
994 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
995 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
996 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
997 TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
998 report( interlace ? "zigzag_field :" : "zigzag_frame :" );
1000 #undef TEST_ZIGZAG_SCAN
1001 #undef TEST_ZIGZAG_SUB
1006 static int check_mc( int cpu_ref, int cpu_new )
1008 x264_mc_functions_t mc_c;
1009 x264_mc_functions_t mc_ref;
1010 x264_mc_functions_t mc_a;
1011 x264_pixel_function_t pixf;
1013 pixel *src = &(pbuf1)[2*64+2];
1014 pixel *src2[4] = { &(pbuf1)[3*64+2], &(pbuf1)[5*64+2],
1015 &(pbuf1)[7*64+2], &(pbuf1)[9*64+2] };
1016 pixel *dst1 = pbuf3;
1017 pixel *dst2 = pbuf4;
1019 int ret = 0, ok, used_asm;
1021 x264_mc_init( 0, &mc_c );
1022 x264_mc_init( cpu_ref, &mc_ref );
1023 x264_mc_init( cpu_new, &mc_a );
1024 x264_pixel_init( 0, &pixf );
1026 #define MC_TEST_LUMA( w, h ) \
1027 if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
1029 const x264_weight_t *weight = x264_weight_none; \
1030 set_func_name( "mc_luma_%dx%d", w, h ); \
1032 for( int i = 0; i < 1024; i++ ) \
1033 pbuf3[i] = pbuf4[i] = 0xCD; \
1034 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
1035 call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h, weight ); \
1036 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1038 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
1042 if( mc_a.get_ref != mc_ref.get_ref ) \
1044 pixel *ref = dst2; \
1045 int ref_stride = 32; \
1046 int w_checked = ( ( sizeof(pixel) == 2 && (w == 12 || w == 20)) ? w-2 : w ); \
1047 const x264_weight_t *weight = x264_weight_none; \
1048 set_func_name( "get_ref_%dx%d", w_checked, h ); \
1050 for( int i = 0; i < 1024; i++ ) \
1051 pbuf3[i] = pbuf4[i] = 0xCD; \
1052 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
1053 ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h, weight ); \
1054 for( int i = 0; i < h; i++ ) \
1055 if( memcmp( dst1+i*32, ref+i*ref_stride, w_checked * sizeof(pixel) ) ) \
1057 fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w_checked, h ); \
1063 #define MC_TEST_CHROMA( w, h ) \
1064 if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
1066 set_func_name( "mc_chroma_%dx%d", w, h ); \
1068 for( int i = 0; i < 1024; i++ ) \
1069 pbuf3[i] = pbuf4[i] = 0xCD; \
1070 call_c( mc_c.mc_chroma, dst1, dst1+8, 16, src, 64, dx, dy, w, h ); \
1071 call_a( mc_a.mc_chroma, dst2, dst2+8, 16, src, 64, dx, dy, w, h ); \
1072 /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \
1073 for( int j = 0; j < h; j++ ) \
1074 for( int i = w; i < 8; i++ ) \
1076 dst2[i+j*16+8] = dst1[i+j*16+8]; \
1077 dst2[i+j*16] = dst1[i+j*16]; \
1079 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1081 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
1085 ok = 1; used_asm = 0;
1086 for( int dy = -8; dy < 8; dy++ )
1087 for( int dx = -128; dx < 128; dx++ )
1089 if( rand()&15 ) continue; // running all of them is too slow
1090 MC_TEST_LUMA( 20, 18 );
1091 MC_TEST_LUMA( 16, 16 );
1092 MC_TEST_LUMA( 16, 8 );
1093 MC_TEST_LUMA( 12, 10 );
1094 MC_TEST_LUMA( 8, 16 );
1095 MC_TEST_LUMA( 8, 8 );
1096 MC_TEST_LUMA( 8, 4 );
1097 MC_TEST_LUMA( 4, 8 );
1098 MC_TEST_LUMA( 4, 4 );
1100 report( "mc luma :" );
1102 ok = 1; used_asm = 0;
1103 for( int dy = -1; dy < 9; dy++ )
1104 for( int dx = -128; dx < 128; dx++ )
1106 if( rand()&15 ) continue;
1107 MC_TEST_CHROMA( 8, 8 );
1108 MC_TEST_CHROMA( 8, 4 );
1109 MC_TEST_CHROMA( 4, 8 );
1110 MC_TEST_CHROMA( 4, 4 );
1111 MC_TEST_CHROMA( 4, 2 );
1112 MC_TEST_CHROMA( 2, 4 );
1113 MC_TEST_CHROMA( 2, 2 );
1115 report( "mc chroma :" );
1117 #undef MC_TEST_CHROMA
1119 #define MC_TEST_AVG( name, weight ) \
1121 ok = 1, used_asm = 0; \
1122 for( int i = 0; i < 12; i++ ) \
1124 memcpy( pbuf3, pbuf1+320, 320 * sizeof(pixel) ); \
1125 memcpy( pbuf4, pbuf1+320, 320 * sizeof(pixel) ); \
1126 if( mc_a.name[i] != mc_ref.name[i] ) \
1128 set_func_name( "%s_%s", #name, pixel_names[i] ); \
1130 call_c1( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1131 call_a1( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1132 if( memcmp( pbuf3, pbuf4, 320 * sizeof(pixel) ) ) \
1135 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
1137 call_c2( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1138 call_a2( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1143 for( int w = -63; w <= 127 && ok; w++ )
1144 MC_TEST_AVG( avg, w );
1145 report( "mc wpredb :" );
1147 #define MC_TEST_WEIGHT( name, weight, aligned ) \
1148 int align_off = (aligned ? 0 : rand()%16); \
1149 ok = 1, used_asm = 0; \
1150 for( int i = 1; i <= 5; i++ ) \
1152 ALIGNED_16( pixel buffC[640] ); \
1153 ALIGNED_16( pixel buffA[640] ); \
1154 int j = X264_MAX( i*4, 2 ); \
1155 memset( buffC, 0, 640 * sizeof(pixel) ); \
1156 memset( buffA, 0, 640 * sizeof(pixel) ); \
1159 /* w12 is the same as w16 in some cases */ \
1160 if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
1162 if( mc_a.name[i] != mc_ref.name[i] ) \
1164 set_func_name( "%s_w%d", #name, j ); \
1166 call_c1( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
1167 mc_a.weight_cache(&ha, &weight); \
1168 call_a1( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
1169 for( int k = 0; k < 16; k++ ) \
1170 if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \
1173 fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
1176 call_c2( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
1177 call_a2( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
1181 ok = 1; used_asm = 0;
1184 for( int s = 0; s <= 127 && ok; s++ )
1186 for( int o = -128; o <= 127 && ok; o++ )
1188 if( rand() & 2047 ) continue;
1189 for( int d = 0; d <= 7 && ok; d++ )
1193 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
1194 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
1199 report( "mc weight :" );
1201 ok = 1; used_asm = 0;
1202 for( int o = 0; o <= 127 && ok; o++ )
1205 if( rand() & 15 ) continue;
1206 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1207 MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
1209 report( "mc offsetadd :" );
1210 ok = 1; used_asm = 0;
1211 for( int o = -128; o < 0 && ok; o++ )
1214 if( rand() & 15 ) continue;
1215 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1216 MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
1218 report( "mc offsetsub :" );
1220 ok = 1; used_asm = 0;
1221 for( int height = 8; height <= 16; height += 8 )
1223 if( mc_a.store_interleave_chroma != mc_ref.store_interleave_chroma )
1225 set_func_name( "store_interleave_chroma" );
1227 memset( pbuf3, 0, 64*height );
1228 memset( pbuf4, 0, 64*height );
1229 call_c( mc_c.store_interleave_chroma, pbuf3, 64, pbuf1, pbuf1+16, height );
1230 call_a( mc_a.store_interleave_chroma, pbuf4, 64, pbuf1, pbuf1+16, height );
1231 if( memcmp( pbuf3, pbuf4, 64*height ) )
1234 fprintf( stderr, "store_interleave_chroma FAILED: h=%d\n", height );
1238 if( mc_a.load_deinterleave_chroma_fenc != mc_ref.load_deinterleave_chroma_fenc )
1240 set_func_name( "load_deinterleave_chroma_fenc" );
1242 call_c( mc_c.load_deinterleave_chroma_fenc, pbuf3, pbuf1, 64, height );
1243 call_a( mc_a.load_deinterleave_chroma_fenc, pbuf4, pbuf1, 64, height );
1244 if( memcmp( pbuf3, pbuf4, FENC_STRIDE*height ) )
1247 fprintf( stderr, "load_deinterleave_chroma_fenc FAILED: h=%d\n", height );
1251 if( mc_a.load_deinterleave_chroma_fdec != mc_ref.load_deinterleave_chroma_fdec )
1253 set_func_name( "load_deinterleave_chroma_fdec" );
1255 call_c( mc_c.load_deinterleave_chroma_fdec, pbuf3, pbuf1, 64, height );
1256 call_a( mc_a.load_deinterleave_chroma_fdec, pbuf4, pbuf1, 64, height );
1257 if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*height ) )
1260 fprintf( stderr, "load_deinterleave_chroma_fdec FAILED: h=%d\n", height );
1265 report( "store_interleave :" );
1268 int w, h, src_stride;
1269 } 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} };
1270 ok = 1; used_asm = 0;
1271 if( mc_a.plane_copy != mc_ref.plane_copy )
1273 set_func_name( "plane_copy" );
1275 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1277 int w = plane_specs[i].w;
1278 int h = plane_specs[i].h;
1279 int src_stride = plane_specs[i].src_stride;
1280 int dst_stride = (w + 127) & ~63;
1281 assert( dst_stride * h <= 0x1000 );
1282 pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1283 memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1284 memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1285 call_c( mc_c.plane_copy, pbuf3, dst_stride, src1, src_stride, w, h );
1286 call_a( mc_a.plane_copy, pbuf4, dst_stride, src1, src_stride, w, h );
1287 for( int y = 0; y < h; y++ )
1288 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(pixel) ) )
1291 fprintf( stderr, "plane_copy FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1297 if( mc_a.plane_copy_interleave != mc_ref.plane_copy_interleave )
1299 set_func_name( "plane_copy_interleave" );
1301 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1303 int w = (plane_specs[i].w + 1) >> 1;
1304 int h = plane_specs[i].h;
1305 int src_stride = (plane_specs[i].src_stride + 1) >> 1;
1306 int dst_stride = (2*w + 127) & ~63;
1307 assert( dst_stride * h <= 0x1000 );
1308 pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1309 memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1310 memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1311 call_c( mc_c.plane_copy_interleave, pbuf3, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1312 call_a( mc_a.plane_copy_interleave, pbuf4, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1313 for( int y = 0; y < h; y++ )
1314 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) )
1317 fprintf( stderr, "plane_copy_interleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1323 if( mc_a.plane_copy_deinterleave != mc_ref.plane_copy_deinterleave )
1325 set_func_name( "plane_copy_deinterleave" );
1327 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1329 int w = (plane_specs[i].w + 1) >> 1;
1330 int h = plane_specs[i].h;
1332 int src_stride = (2*w + 127) & ~63;
1333 int offv = (dst_stride*h + 31) & ~15;
1334 memset( pbuf3, 0, 0x1000 );
1335 memset( pbuf4, 0, 0x1000 );
1336 call_c( mc_c.plane_copy_deinterleave, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf1, src_stride, w, h );
1337 call_a( mc_a.plane_copy_deinterleave, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf1, src_stride, w, h );
1338 for( int y = 0; y < h; y++ )
1339 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w ) ||
1340 memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w ) )
1343 fprintf( stderr, "plane_copy_deinterleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1348 report( "plane_copy :" );
1350 if( mc_a.hpel_filter != mc_ref.hpel_filter )
1352 pixel *srchpel = pbuf1+8+2*64;
1353 pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
1354 pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
1355 void *tmp = pbuf3+49*64;
1356 set_func_name( "hpel_filter" );
1357 ok = 1; used_asm = 1;
1358 memset( pbuf3, 0, 4096 * sizeof(pixel) );
1359 memset( pbuf4, 0, 4096 * sizeof(pixel) );
1360 call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, 64, 48, 10, tmp );
1361 call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, 64, 48, 10, tmp );
1362 for( int i = 0; i < 3; i++ )
1363 for( int j = 0; j < 10; j++ )
1364 //FIXME ideally the first pixels would match too, but they aren't actually used
1365 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
1368 fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
1369 for( int k = 0; k < 48; k++ )
1370 printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
1372 for( int k = 0; k < 48; k++ )
1373 printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
1377 report( "hpel filter :" );
1380 if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
1382 pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
1383 pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1384 set_func_name( "lowres_init" );
1385 ok = 1; used_asm = 1;
1386 for( int w = 40; w <= 48; w += 8 )
1388 int stride = (w+8)&~15;
1389 call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
1390 call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
1391 for( int i = 0; i < 16; i++ )
1393 for( int j = 0; j < 4; j++ )
1394 if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w * sizeof(pixel) ) )
1397 fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1398 for( int k = 0; k < w; k++ )
1399 printf( "%d ", dstc[j][k+i*stride] );
1401 for( int k = 0; k < w; k++ )
1402 printf( "%d ", dsta[j][k+i*stride] );
1408 report( "lowres init :" );
1411 #define INTEGRAL_INIT( name, size, ... )\
1412 if( mc_a.name != mc_ref.name )\
1415 set_func_name( #name );\
1417 memcpy( buf3, buf1, size*2*stride );\
1418 memcpy( buf4, buf1, size*2*stride );\
1419 uint16_t *sum = (uint16_t*)buf3;\
1420 call_c1( mc_c.name, __VA_ARGS__ );\
1421 sum = (uint16_t*)buf4;\
1422 call_a1( mc_a.name, __VA_ARGS__ );\
1423 if( memcmp( buf3, buf4, (stride-8)*2 ) \
1424 || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
1426 call_c2( mc_c.name, __VA_ARGS__ );\
1427 call_a2( mc_a.name, __VA_ARGS__ );\
1429 ok = 1; used_asm = 0;
1430 INTEGRAL_INIT( integral_init4h, 2, sum+stride, pbuf2, stride );
1431 INTEGRAL_INIT( integral_init8h, 2, sum+stride, pbuf2, stride );
1432 INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
1433 INTEGRAL_INIT( integral_init8v, 9, sum, stride );
1434 report( "integral init :" );
1436 if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1439 for( int i = 0; i < 10; i++ )
1441 float fps_factor = (rand()&65535) / 256.;
1442 ok = 1; used_asm = 1;
1443 set_func_name( "mbtree_propagate" );
1444 int *dsta = (int*)buf3;
1445 int *dstc = dsta+400;
1446 uint16_t *prop = (uint16_t*)buf1;
1447 uint16_t *intra = (uint16_t*)buf4;
1448 uint16_t *inter = intra+128;
1449 uint16_t *qscale = inter+128;
1450 uint16_t *rnd = (uint16_t*)buf2;
1452 for( int j = 0; j < 100; j++ )
1454 intra[j] = *rnd++ & 0x7fff;
1455 intra[j] += !intra[j];
1456 inter[j] = *rnd++ & 0x7fff;
1457 qscale[j] = *rnd++ & 0x7fff;
1459 call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, &fps_factor, 100 );
1460 call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, &fps_factor, 100 );
1461 // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1463 for( int j = 0; j < 100 && ok; j++ )
1465 ok &= abs( dstc[j]-dsta[j] ) <= 1 || fabs( (double)dstc[j]/dsta[j]-1 ) < 1e-4;
1467 fprintf( stderr, "mbtree_propagate FAILED: %f !~= %f\n", (double)dstc[j], (double)dsta[j] );
1470 report( "mbtree propagate :" );
1473 if( mc_a.memcpy_aligned != mc_ref.memcpy_aligned )
1475 set_func_name( "memcpy_aligned" );
1476 ok = 1; used_asm = 1;
1477 for( int size = 16; size < 256; size += 16 )
1479 memset( buf4, 0xAA, size + 1 );
1480 call_c( mc_c.memcpy_aligned, buf3, buf1, size );
1481 call_a( mc_a.memcpy_aligned, buf4, buf1, size );
1482 if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1485 fprintf( stderr, "memcpy_aligned FAILED: size=%d\n", size );
1489 report( "memcpy aligned :" );
1492 if( mc_a.memzero_aligned != mc_ref.memzero_aligned )
1494 set_func_name( "memzero_aligned" );
1495 ok = 1; used_asm = 1;
1496 for( int size = 128; size < 1024; size += 128 )
1498 memset( buf4, 0xAA, size + 1 );
1499 call_c( mc_c.memzero_aligned, buf3, size );
1500 call_a( mc_a.memzero_aligned, buf4, size );
1501 if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1504 fprintf( stderr, "memzero_aligned FAILED: size=%d\n", size );
1508 report( "memzero aligned :" );
1514 static int check_deblock( int cpu_ref, int cpu_new )
1516 x264_deblock_function_t db_c;
1517 x264_deblock_function_t db_ref;
1518 x264_deblock_function_t db_a;
1519 int ret = 0, ok = 1, used_asm = 0;
1520 int alphas[36], betas[36];
1523 x264_deblock_init( 0, &db_c, 0 );
1524 x264_deblock_init( cpu_ref, &db_ref, 0 );
1525 x264_deblock_init( cpu_new, &db_a, 0 );
1527 /* not exactly the real values of a,b,tc but close enough */
1528 for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1530 alphas[i] = a << (BIT_DEPTH-8);
1531 betas[i] = (i+1)/2 << (BIT_DEPTH-8);
1532 tcs[i][0] = tcs[i][3] = (c+6)/10 << (BIT_DEPTH-8);
1533 tcs[i][1] = (c+7)/15 << (BIT_DEPTH-8);
1534 tcs[i][2] = (c+9)/20 << (BIT_DEPTH-8);
1539 #define TEST_DEBLOCK( name, align, ... ) \
1540 for( int i = 0; i < 36; i++ ) \
1542 int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1543 for( int j = 0; j < 1024; j++ ) \
1544 /* two distributions of random to excersize different failure modes */ \
1545 pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \
1546 memcpy( pbuf4, pbuf3, 1024 * sizeof(pixel) ); \
1547 if( db_a.name != db_ref.name ) \
1549 set_func_name( #name ); \
1551 call_c1( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1552 call_a1( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1553 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1556 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1559 call_c2( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1560 call_a2( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1564 TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1565 TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1566 TEST_DEBLOCK( deblock_h_chroma_420, 0, tcs[i] );
1567 TEST_DEBLOCK( deblock_h_chroma_422, 0, tcs[i] );
1568 TEST_DEBLOCK( deblock_chroma_420_mbaff, 0, tcs[i] );
1569 TEST_DEBLOCK( deblock_chroma_422_mbaff, 0, tcs[i] );
1570 TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1571 TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1572 TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1573 TEST_DEBLOCK( deblock_h_chroma_420_intra, 0 );
1574 TEST_DEBLOCK( deblock_h_chroma_422_intra, 0 );
1575 TEST_DEBLOCK( deblock_chroma_420_intra_mbaff, 0 );
1576 TEST_DEBLOCK( deblock_chroma_422_intra_mbaff, 0 );
1577 TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1579 if( db_a.deblock_strength != db_ref.deblock_strength )
1581 for( int i = 0; i < 100; i++ )
1583 ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1584 ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1585 ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] );
1586 ALIGNED_ARRAY_16( uint8_t, bs, [2],[2][8][4] );
1587 memset( bs, 99, sizeof(bs) );
1588 for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1589 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1590 for( int j = 0; j < 2; j++ )
1591 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1593 ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1594 for( int l = 0; l < 2; l++ )
1595 mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1597 set_func_name( "deblock_strength" );
1598 call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1599 call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1600 if( memcmp( bs[0], bs[1], sizeof(bs[0]) ) )
1603 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1604 for( int j = 0; j < 2; j++ )
1606 for( int k = 0; k < 2; k++ )
1607 for( int l = 0; l < 4; l++ )
1609 for( int m = 0; m < 4; m++ )
1610 printf("%d ",bs[j][k][l][m]);
1620 report( "deblock :" );
1625 static int check_quant( int cpu_ref, int cpu_new )
1627 x264_quant_function_t qf_c;
1628 x264_quant_function_t qf_ref;
1629 x264_quant_function_t qf_a;
1630 ALIGNED_16( dctcoef dct1[64] );
1631 ALIGNED_16( dctcoef dct2[64] );
1632 ALIGNED_16( dctcoef dct3[8][16] );
1633 ALIGNED_16( dctcoef dct4[8][16] );
1634 ALIGNED_16( uint8_t cqm_buf[64] );
1635 int ret = 0, ok, used_asm;
1636 int oks[3] = {1,1,1}, used_asms[3] = {0,0,0};
1639 memset( h, 0, sizeof(*h) );
1640 h->sps->i_chroma_format_idc = 1;
1641 x264_param_default( &h->param );
1642 h->chroma_qp_table = i_chroma_qp_table + 12;
1643 h->param.analyse.b_transform_8x8 = 1;
1645 for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1649 for( int i = 0; i < 6; i++ )
1650 h->pps->scaling_list[i] = x264_cqm_flat16;
1651 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1653 else if( i_cqm == 1 )
1655 for( int i = 0; i < 6; i++ )
1656 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1657 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1661 int max_scale = BIT_DEPTH < 10 ? 255 : 228;
1663 for( int i = 0; i < 64; i++ )
1664 cqm_buf[i] = 10 + rand() % (max_scale - 9);
1666 for( int i = 0; i < 64; i++ )
1668 for( int i = 0; i < 6; i++ )
1669 h->pps->scaling_list[i] = cqm_buf;
1670 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1673 h->param.rc.i_qp_min = 0;
1674 h->param.rc.i_qp_max = QP_MAX;
1676 x264_quant_init( h, 0, &qf_c );
1677 x264_quant_init( h, cpu_ref, &qf_ref );
1678 x264_quant_init( h, cpu_new, &qf_a );
1680 #define INIT_QUANT8(j) \
1682 static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1683 for( int i = 0; i < 64; i++ ) \
1685 unsigned int scale = (255*scale1d[i>>3]*scale1d[i&7])/16; \
1686 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1690 #define INIT_QUANT4(j) \
1692 static const int scale1d[4] = {4,6,4,6}; \
1693 for( int i = 0; i < 16; i++ ) \
1695 unsigned int scale = 255*scale1d[i>>2]*scale1d[i&3]; \
1696 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1700 #define TEST_QUANT_DC( name, cqm ) \
1701 if( qf_a.name != qf_ref.name ) \
1703 set_func_name( #name ); \
1705 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1707 for( int j = 0; j < 2; j++ ) \
1709 int result_c, result_a; \
1710 for( int i = 0; i < 16; i++ ) \
1711 dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1712 result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1713 result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1714 if( memcmp( dct1, dct2, 16*sizeof(dctcoef) ) || result_c != result_a ) \
1717 fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1720 call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1721 call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1726 #define TEST_QUANT( qname, block, w ) \
1727 if( qf_a.qname != qf_ref.qname ) \
1729 set_func_name( #qname ); \
1731 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1733 for( int j = 0; j < 2; j++ ) \
1736 int result_c = call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1737 int result_a = call_a1( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1738 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \
1741 fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1744 call_c2( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1745 call_a2( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1750 TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1751 TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1752 TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1753 TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1754 TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1755 TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1757 #define TEST_DEQUANT( qname, dqname, block, w ) \
1758 if( qf_a.dqname != qf_ref.dqname ) \
1760 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1762 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1765 qf_c.qname( dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1766 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1767 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1768 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1769 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1772 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1775 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1776 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1780 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1781 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1782 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1783 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1785 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1786 if( qf_a.dqname != qf_ref.dqname ) \
1788 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1790 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1792 for( int i = 0; i < 16; i++ ) \
1793 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16; \
1794 qf_c.qname( dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1795 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1796 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1797 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1798 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1801 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1803 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1804 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1808 TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1810 if( qf_a.idct_dequant_2x4_dc != qf_ref.idct_dequant_2x4_dc )
1812 set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1814 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1816 for( int i = 0; i < 8; i++ )
1817 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1818 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 );
1819 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 );
1820 call_c( qf_c.idct_dequant_2x4_dc, dct1, dct3, h->dequant4_mf[CQM_4IC], qp+3 );
1821 call_a( qf_a.idct_dequant_2x4_dc, dct1, dct4, h->dequant4_mf[CQM_4IC], qp+3 );
1822 for( int i = 0; i < 8; i++ )
1823 if( dct3[i][0] != dct4[i][0] )
1826 fprintf( stderr, "idct_dequant_2x4_dc (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1832 if( qf_a.idct_dequant_2x4_dconly != qf_ref.idct_dequant_2x4_dconly )
1834 set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1836 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1838 for( int i = 0; i < 8; i++ )
1839 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1840 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 );
1841 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 );
1842 memcpy( dct2, dct1, 8*sizeof(dctcoef) );
1843 call_c1( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1844 call_a1( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1845 if( memcmp( dct1, dct2, 8*sizeof(dctcoef) ) )
1848 fprintf( stderr, "idct_dequant_2x4_dconly (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1851 call_c2( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1852 call_a2( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1856 #define TEST_OPTIMIZE_CHROMA_DC( optname, size ) \
1857 if( qf_a.optname != qf_ref.optname ) \
1859 set_func_name( #optname ); \
1861 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1863 int qpdc = qp + (size == 8 ? 3 : 0); \
1864 int dmf = h->dequant4_mf[CQM_4IC][qpdc%6][0] << qpdc/6; \
1867 for( int i = 16; ; i <<= 1 ) \
1869 int res_c, res_asm; \
1870 int max = X264_MIN( i, PIXEL_MAX*16 ); \
1871 for( int j = 0; j < size; j++ ) \
1872 dct1[j] = rand()%(max*2+1) - max; \
1873 for( int j = 0; i <= size; j += 4 ) \
1874 qf_c.quant_2x2_dc( &dct1[j], h->quant4_mf[CQM_4IC][qpdc][0]>>1, h->quant4_bias[CQM_4IC][qpdc][0]>>1 ); \
1875 memcpy( dct2, dct1, size*sizeof(dctcoef) ); \
1876 res_c = call_c1( qf_c.optname, dct1, dmf ); \
1877 res_asm = call_a1( qf_a.optname, dct2, dmf ); \
1878 if( res_c != res_asm || memcmp( dct1, dct2, size*sizeof(dctcoef) ) ) \
1881 fprintf( stderr, #optname "(qp=%d, res_c=%d, res_asm=%d): [FAILED]\n", qp, res_c, res_asm ); \
1883 call_c2( qf_c.optname, dct1, dmf ); \
1884 call_a2( qf_a.optname, dct2, dmf ); \
1885 if( i >= PIXEL_MAX*16 ) \
1891 TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x2_dc, 4 );
1892 TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x4_dc, 8 );
1894 x264_cqm_delete( h );
1897 ok = oks[0]; used_asm = used_asms[0];
1898 report( "quant :" );
1900 ok = oks[1]; used_asm = used_asms[1];
1901 report( "dequant :" );
1903 ok = oks[2]; used_asm = used_asms[2];
1904 report( "optimize chroma dc :" );
1906 ok = 1; used_asm = 0;
1907 if( qf_a.denoise_dct != qf_ref.denoise_dct )
1910 for( int size = 16; size <= 64; size += 48 )
1912 set_func_name( "denoise_dct" );
1913 memcpy( dct1, buf1, size*sizeof(dctcoef) );
1914 memcpy( dct2, buf1, size*sizeof(dctcoef) );
1915 memcpy( buf3+256, buf3, 256 );
1916 call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
1917 call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
1918 if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1920 call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
1921 call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
1924 report( "denoise dct :" );
1926 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1927 if( qf_a.decname != qf_ref.decname ) \
1929 set_func_name( #decname ); \
1931 for( int i = 0; i < 100; i++ ) \
1933 static const int distrib[16] = {1,1,1,1,1,1,1,1,1,1,1,1,2,3,4};\
1934 static const int zerorate_lut[4] = {3,7,15,31};\
1935 int zero_rate = zerorate_lut[i&3];\
1936 for( int idx = 0; idx < w*w; idx++ ) \
1938 int sign = (rand()&1) ? -1 : 1; \
1939 int abs_level = distrib[rand()&15]; \
1940 if( abs_level == 4 ) abs_level = rand()&0x3fff; \
1941 int zero = !(rand()&zero_rate); \
1942 dct1[idx] = zero * abs_level * sign; \
1946 int result_c = call_c( qf_c.decname, dct1 ); \
1947 int result_a = call_a( qf_a.decname, dct1 ); \
1948 if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1951 fprintf( stderr, #decname ": [FAILED]\n" ); \
1957 ok = 1; used_asm = 0;
1958 TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1959 TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1960 TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1961 report( "decimate_score :" );
1963 #define TEST_LAST( last, lastname, size, ac ) \
1964 if( qf_a.last != qf_ref.last ) \
1966 set_func_name( #lastname ); \
1968 for( int i = 0; i < 100; i++ ) \
1971 int max = rand() & (size-1); \
1972 memset( dct1, 0, size*sizeof(dctcoef) ); \
1973 for( int idx = ac; idx < max; idx++ ) \
1974 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1977 int result_c = call_c( qf_c.last, dct1+ac ); \
1978 int result_a = call_a( qf_a.last, dct1+ac ); \
1979 if( result_c != result_a ) \
1982 fprintf( stderr, #lastname ": [FAILED]\n" ); \
1988 ok = 1; used_asm = 0;
1989 TEST_LAST( coeff_last4 , coeff_last4, 4, 0 );
1990 TEST_LAST( coeff_last8 , coeff_last8, 8, 0 );
1991 TEST_LAST( coeff_last[ DCT_LUMA_AC], coeff_last15, 16, 1 );
1992 TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 16, 0 );
1993 TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 64, 0 );
1994 report( "coeff_last :" );
1996 #define TEST_LEVELRUN( lastname, name, size, ac ) \
1997 if( qf_a.lastname != qf_ref.lastname ) \
1999 set_func_name( #name ); \
2001 for( int i = 0; i < 100; i++ ) \
2003 x264_run_level_t runlevel_c, runlevel_a; \
2005 int max = rand() & (size-1); \
2006 memset( dct1, 0, size*sizeof(dctcoef) ); \
2007 memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
2008 memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
2009 for( int idx = ac; idx < max; idx++ ) \
2010 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2013 int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
2014 int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
2015 if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
2016 runlevel_c.mask != runlevel_a.mask || \
2017 memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c) || \
2018 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
2021 fprintf( stderr, #name ": [FAILED]\n" ); \
2027 ok = 1; used_asm = 0;
2028 TEST_LEVELRUN( coeff_level_run4 , coeff_level_run4, 4, 0 );
2029 TEST_LEVELRUN( coeff_level_run8 , coeff_level_run8, 8, 0 );
2030 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_AC], coeff_level_run15, 16, 1 );
2031 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 16, 0 );
2032 report( "coeff_level_run :" );
2037 static int check_intra( int cpu_ref, int cpu_new )
2039 int ret = 0, ok = 1, used_asm = 0;
2040 ALIGNED_ARRAY_32( pixel, edge,[36] );
2041 ALIGNED_ARRAY_32( pixel, edge2,[36] );
2042 ALIGNED_16( pixel fdec[FDEC_STRIDE*20] );
2045 x264_predict_t predict_16x16[4+3];
2046 x264_predict_t predict_8x8c[4+3];
2047 x264_predict_t predict_8x16c[4+3];
2048 x264_predict8x8_t predict_8x8[9+3];
2049 x264_predict_t predict_4x4[9+3];
2050 x264_predict_8x8_filter_t predict_8x8_filter;
2051 } ip_c, ip_ref, ip_a;
2053 x264_predict_16x16_init( 0, ip_c.predict_16x16 );
2054 x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
2055 x264_predict_8x16c_init( 0, ip_c.predict_8x16c );
2056 x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
2057 x264_predict_4x4_init( 0, ip_c.predict_4x4 );
2059 x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
2060 x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
2061 x264_predict_8x16c_init( cpu_ref, ip_ref.predict_8x16c );
2062 x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
2063 x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
2065 x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
2066 x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
2067 x264_predict_8x16c_init( cpu_new, ip_a.predict_8x16c );
2068 x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
2069 x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
2071 memcpy( fdec, pbuf1, 32*20 * sizeof(pixel) );\
2073 ip_c.predict_8x8_filter( fdec+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
2075 #define INTRA_TEST( name, dir, w, h, align, bench, ... )\
2076 if( ip_a.name[dir] != ip_ref.name[dir] )\
2078 set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
2080 memcpy( pbuf3, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2081 memcpy( pbuf4, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2082 for( int a = 0; a < (do_bench ? 64/sizeof(pixel) : 1); a += align )\
2084 call_c##bench( ip_c.name[dir], pbuf3+48+a, ##__VA_ARGS__ );\
2085 call_a##bench( ip_a.name[dir], pbuf4+48+a, ##__VA_ARGS__ );\
2086 if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*20 * sizeof(pixel) ) )\
2088 fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\
2090 for( int k = -1; k < 16; k++ )\
2091 printf( "%2x ", edge[16+k] );\
2093 for( int j = 0; j < h; j++ )\
2095 printf( "%2x ", edge[14-j] );\
2096 for( int k = 0; k < w; k++ )\
2097 printf( "%2x ", pbuf4[48+k+j*FDEC_STRIDE] );\
2101 for( int j = 0; j < h; j++ )\
2104 for( int k = 0; k < w; k++ )\
2105 printf( "%2x ", pbuf3[48+k+j*FDEC_STRIDE] );\
2113 for( int i = 0; i < 12; i++ )
2114 INTRA_TEST( predict_4x4, i, 4, 4, 4, );
2115 for( int i = 0; i < 7; i++ )
2116 INTRA_TEST( predict_8x8c, i, 8, 8, 16, );
2117 for( int i = 0; i < 7; i++ )
2118 INTRA_TEST( predict_8x16c, i, 8, 16, 16, );
2119 for( int i = 0; i < 7; i++ )
2120 INTRA_TEST( predict_16x16, i, 16, 16, 16, );
2121 for( int i = 0; i < 12; i++ )
2122 INTRA_TEST( predict_8x8, i, 8, 8, 8, , edge );
2124 set_func_name("intra_predict_8x8_filter");
2125 if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
2128 for( int i = 0; i < 32; i++ )
2130 if( !(i&7) || ((i&MB_TOPRIGHT) && !(i&MB_TOP)) )
2132 int neighbor = (i&24)>>1;
2133 memset( edge, 0, 36*sizeof(pixel) );
2134 memset( edge2, 0, 36*sizeof(pixel) );
2135 call_c( ip_c.predict_8x8_filter, pbuf1+48, edge, neighbor, i&7 );
2136 call_a( ip_a.predict_8x8_filter, pbuf1+48, edge2, neighbor, i&7 );
2137 if( !(neighbor&MB_TOPLEFT) )
2138 edge[15] = edge2[15] = 0;
2139 if( memcmp( edge+7, edge2+7, (i&MB_TOPRIGHT ? 26 : i&MB_TOP ? 17 : 8) * sizeof(pixel) ) )
2141 fprintf( stderr, "predict_8x8_filter : [FAILED] %d %d\n", (i&24)>>1, i&7);
2147 #define EXTREMAL_PLANE( w, h ) \
2150 for( int j = 0; j < 7; j++ ) \
2151 max[j] = test ? rand()&PIXEL_MAX : PIXEL_MAX; \
2152 fdec[48-1-FDEC_STRIDE] = (i&1)*max[0]; \
2153 for( int j = 0; j < w/2; j++ ) \
2154 fdec[48+j-FDEC_STRIDE] = (!!(i&2))*max[1]; \
2155 for( int j = w/2; j < w-1; j++ ) \
2156 fdec[48+j-FDEC_STRIDE] = (!!(i&4))*max[2]; \
2157 fdec[48+(w-1)-FDEC_STRIDE] = (!!(i&8))*max[3]; \
2158 for( int j = 0; j < h/2; j++ ) \
2159 fdec[48+j*FDEC_STRIDE-1] = (!!(i&16))*max[4]; \
2160 for( int j = h/2; j < h-1; j++ ) \
2161 fdec[48+j*FDEC_STRIDE-1] = (!!(i&32))*max[5]; \
2162 fdec[48+(h-1)*FDEC_STRIDE-1] = (!!(i&64))*max[6]; \
2164 /* Extremal test case for planar prediction. */
2165 for( int test = 0; test < 100 && ok; test++ )
2166 for( int i = 0; i < 128 && ok; i++ )
2168 EXTREMAL_PLANE( 8, 8 );
2169 INTRA_TEST( predict_8x8c, I_PRED_CHROMA_P, 8, 8, 64, 1 );
2170 EXTREMAL_PLANE( 8, 16 );
2171 INTRA_TEST( predict_8x16c, I_PRED_CHROMA_P, 8, 16, 64, 1 );
2172 EXTREMAL_PLANE( 16, 16 );
2173 INTRA_TEST( predict_16x16, I_PRED_16x16_P, 16, 16, 64, 1 );
2175 report( "intra pred :" );
2179 #define DECL_CABAC(cpu) \
2180 static void run_cabac_decision_##cpu( x264_t *h, uint8_t *dst )\
2183 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2184 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2185 for( int i = 0; i < 0x1000; i++ )\
2186 x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
2188 static void run_cabac_bypass_##cpu( x264_t *h, uint8_t *dst )\
2191 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2192 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2193 for( int i = 0; i < 0x1000; i++ )\
2194 x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
2196 static void run_cabac_terminal_##cpu( x264_t *h, uint8_t *dst )\
2199 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2200 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2201 for( int i = 0; i < 0x1000; i++ )\
2202 x264_cabac_encode_terminal_##cpu( &cb );\
2208 #define run_cabac_decision_asm run_cabac_decision_c
2209 #define run_cabac_bypass_asm run_cabac_bypass_c
2210 #define run_cabac_terminal_asm run_cabac_terminal_c
2213 static int check_cabac( int cpu_ref, int cpu_new )
2215 int ret = 0, ok, used_asm = 1;
2217 h.sps->i_chroma_format_idc = 3;
2218 if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
2220 x264_cabac_init( &h );
2222 set_func_name( "cabac_encode_decision" );
2223 memcpy( buf4, buf3, 0x1000 );
2224 call_c( run_cabac_decision_c, &h, buf3 );
2225 call_a( run_cabac_decision_asm, &h, buf4 );
2226 ok = !memcmp( buf3, buf4, 0x1000 );
2227 report( "cabac decision:" );
2229 set_func_name( "cabac_encode_bypass" );
2230 memcpy( buf4, buf3, 0x1000 );
2231 call_c( run_cabac_bypass_c, &h, buf3 );
2232 call_a( run_cabac_bypass_asm, &h, buf4 );
2233 ok = !memcmp( buf3, buf4, 0x1000 );
2234 report( "cabac bypass:" );
2236 set_func_name( "cabac_encode_terminal" );
2237 memcpy( buf4, buf3, 0x1000 );
2238 call_c( run_cabac_terminal_c, &h, buf3 );
2239 call_a( run_cabac_terminal_asm, &h, buf4 );
2240 ok = !memcmp( buf3, buf4, 0x1000 );
2241 report( "cabac terminal:" );
2246 static int check_bitstream( int cpu_ref, int cpu_new )
2248 x264_bitstream_function_t bs_c;
2249 x264_bitstream_function_t bs_ref;
2250 x264_bitstream_function_t bs_a;
2252 int ret = 0, ok = 1, used_asm = 0;
2254 x264_bitstream_init( 0, &bs_c );
2255 x264_bitstream_init( cpu_ref, &bs_ref );
2256 x264_bitstream_init( cpu_new, &bs_a );
2257 if( bs_a.nal_escape != bs_ref.nal_escape )
2260 uint8_t *input = malloc(size+100);
2261 uint8_t *output1 = malloc(size*2);
2262 uint8_t *output2 = malloc(size*2);
2264 set_func_name( "nal_escape" );
2265 for( int i = 0; i < 100; i++ )
2267 /* Test corner-case sizes */
2268 int test_size = i < 10 ? i+1 : rand() & 0x3fff;
2269 /* Test 8 different probability distributions of zeros */
2270 for( int j = 0; j < test_size+32; j++ )
2271 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
2272 uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
2273 uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
2274 int size_c = end_c-output1;
2275 int size_a = end_a-output2;
2276 if( size_c != size_a || memcmp( output1, output2, size_c ) )
2278 fprintf( stderr, "nal_escape : [FAILED] %d %d\n", size_c, size_a );
2283 for( int j = 0; j < size+32; j++ )
2285 call_c2( bs_c.nal_escape, output1, input, input+size );
2286 call_a2( bs_a.nal_escape, output2, input, input+size );
2291 report( "nal escape:" );
2296 static int check_all_funcs( int cpu_ref, int cpu_new )
2298 return check_pixel( cpu_ref, cpu_new )
2299 + check_dct( cpu_ref, cpu_new )
2300 + check_mc( cpu_ref, cpu_new )
2301 + check_intra( cpu_ref, cpu_new )
2302 + check_deblock( cpu_ref, cpu_new )
2303 + check_quant( cpu_ref, cpu_new )
2304 + check_cabac( cpu_ref, cpu_new )
2305 + check_bitstream( cpu_ref, cpu_new );
2308 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
2310 *cpu_ref = *cpu_new;
2312 #if BROKEN_STACK_ALIGNMENT
2313 *cpu_new |= X264_CPU_STACK_MOD4;
2315 if( *cpu_new & X264_CPU_SSE2_IS_FAST )
2316 *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
2318 fprintf( stderr, "x264: %s\n", name );
2319 return check_all_funcs( *cpu_ref, *cpu_new );
2322 static int check_all_flags( void )
2325 int cpu0 = 0, cpu1 = 0;
2327 if( x264_cpu_detect() & X264_CPU_MMX2 )
2329 ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMX2, "MMX" );
2330 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
2331 cpu1 &= ~X264_CPU_CACHELINE_64;
2333 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
2334 cpu1 &= ~X264_CPU_CACHELINE_32;
2336 if( x264_cpu_detect() & X264_CPU_LZCNT )
2338 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
2339 cpu1 &= ~X264_CPU_LZCNT;
2341 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
2342 cpu1 &= ~X264_CPU_SLOW_CTZ;
2344 if( x264_cpu_detect() & X264_CPU_SSE2 )
2346 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
2347 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
2348 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
2349 cpu1 &= ~X264_CPU_CACHELINE_64;
2350 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSE2 FastShuffle" );
2351 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
2352 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
2353 cpu1 &= ~X264_CPU_SLOW_CTZ;
2354 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSE2 SlowAtom" );
2355 cpu1 &= ~X264_CPU_SLOW_ATOM;
2357 if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
2359 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
2360 cpu1 &= ~X264_CPU_SSE_MISALIGN;
2362 if( x264_cpu_detect() & X264_CPU_LZCNT )
2364 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
2365 cpu1 &= ~X264_CPU_LZCNT;
2367 if( x264_cpu_detect() & X264_CPU_SSE3 )
2369 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
2370 cpu1 &= ~X264_CPU_CACHELINE_64;
2372 if( x264_cpu_detect() & X264_CPU_SSSE3 )
2374 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
2375 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
2376 cpu1 &= ~X264_CPU_CACHELINE_64;
2377 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSSE3 FastShuffle" );
2378 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
2379 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
2380 cpu1 &= ~X264_CPU_SLOW_CTZ;
2381 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
2382 cpu1 &= ~X264_CPU_SLOW_ATOM;
2384 if( x264_cpu_detect() & X264_CPU_SSE4 )
2385 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4 | X264_CPU_SHUFFLE_IS_FAST, "SSE4" );
2386 if( x264_cpu_detect() & X264_CPU_AVX )
2387 ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX, "AVX" );
2388 if( x264_cpu_detect() & X264_CPU_XOP )
2389 ret |= add_flags( &cpu0, &cpu1, X264_CPU_XOP, "XOP" );
2390 if( x264_cpu_detect() & X264_CPU_FMA4 )
2391 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA4, "FMA4" );
2393 if( x264_cpu_detect() & X264_CPU_ALTIVEC )
2395 fprintf( stderr, "x264: ALTIVEC against C\n" );
2396 ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
2399 if( x264_cpu_detect() & X264_CPU_ARMV6 )
2400 ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
2401 if( x264_cpu_detect() & X264_CPU_NEON )
2402 ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
2403 if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC )
2404 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
2409 int main(int argc, char *argv[])
2413 if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
2415 #if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM
2416 fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
2420 if( argv[1][7] == '=' )
2422 bench_pattern = argv[1]+8;
2423 bench_pattern_len = strlen(bench_pattern);
2429 int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
2430 fprintf( stderr, "x264: using random seed %u\n", seed );
2433 buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 16*BENCH_ALIGNS );
2434 pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 16*BENCH_ALIGNS );
2435 if( !buf1 || !pbuf1 )
2437 fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
2440 #define INIT_POINTER_OFFSETS\
2441 buf2 = buf1 + 0xf00;\
2442 buf3 = buf2 + 0xf00;\
2443 buf4 = buf3 + 0x1000*sizeof(pixel);\
2444 pbuf2 = pbuf1 + 0xf00;\
2445 pbuf3 = (pixel*)buf3;\
2446 pbuf4 = (pixel*)buf4;
2447 INIT_POINTER_OFFSETS;
2448 for( int i = 0; i < 0x1e00; i++ )
2450 buf1[i] = rand() & 0xFF;
2451 pbuf1[i] = rand() & PIXEL_MAX;
2453 memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) );
2455 /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
2457 for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
2459 INIT_POINTER_OFFSETS;
2460 ret |= x264_stack_pagealign( check_all_flags, i*16 );
2464 fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
2467 ret = check_all_flags();
2471 fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
2474 fprintf( stderr, "x264: All tests passed Yeah :)\n" );