1 /*****************************************************************************
2 * checkasm.c: assembly check tool
3 *****************************************************************************
4 * Copyright (C) 2003-2014 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];
141 int nfuncs, nop_time=0;
143 for( int i = 0; i < 10000; i++ )
145 uint32_t t = read_time();
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,
168 b->cpu&X264_CPU_AVX2 && b->cpu&X264_CPU_FMA3 ? "avx2_fma3" :
169 b->cpu&X264_CPU_AVX2 ? "avx2" :
170 b->cpu&X264_CPU_FMA3 ? "fma3" :
171 b->cpu&X264_CPU_FMA4 ? "fma4" :
172 b->cpu&X264_CPU_XOP ? "xop" :
173 b->cpu&X264_CPU_AVX ? "avx" :
174 b->cpu&X264_CPU_SSE4 ? "sse4" :
175 b->cpu&X264_CPU_SSSE3 ? "ssse3" :
176 b->cpu&X264_CPU_SSE3 ? "sse3" :
177 /* print sse2slow only if there's also a sse2fast version of the same func */
178 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" :
179 b->cpu&X264_CPU_SSE2 ? "sse2" :
180 b->cpu&X264_CPU_SSE ? "sse" :
181 b->cpu&X264_CPU_MMX ? "mmx" :
183 b->cpu&X264_CPU_ALTIVEC ? "altivec" :
185 b->cpu&X264_CPU_NEON ? "neon" :
186 b->cpu&X264_CPU_ARMV6 ? "armv6" :
190 b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
191 b->cpu&X264_CPU_SLOW_ATOM && b->cpu&X264_CPU_CACHELINE_64 ? "_c64_atom" :
192 b->cpu&X264_CPU_CACHELINE_64 ? "_c64" :
193 b->cpu&X264_CPU_SLOW_SHUFFLE ? "_slowshuffle" :
194 b->cpu&X264_CPU_LZCNT ? "_lzcnt" :
195 b->cpu&X264_CPU_BMI2 ? "_bmi2" :
196 b->cpu&X264_CPU_BMI1 ? "_bmi1" :
197 b->cpu&X264_CPU_SLOW_CTZ ? "_slow_ctz" :
198 b->cpu&X264_CPU_SLOW_ATOM ? "_atom" :
200 b->cpu&X264_CPU_FAST_NEON_MRC ? "_fast_mrc" :
203 ((int64_t)10*b->cycles/b->den - nop_time)/4 );
207 #if ARCH_X86 || ARCH_X86_64
208 int x264_stack_pagealign( int (*func)(), int align );
210 /* detect when callee-saved regs aren't saved
211 * needs an explicit asm check because it only sometimes crashes in normal use. */
212 intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... );
214 #define x264_stack_pagealign( func, align ) func()
217 #define call_c1(func,...) func(__VA_ARGS__)
220 /* Evil hack: detect incorrect assumptions that 32-bit ints are zero-extended to 64-bit.
221 * This is done by clobbering the stack with junk around the stack pointer and calling the
222 * assembly function through x264_checkasm_call with added dummy arguments which forces all
223 * real arguments to be passed on the stack and not in registers. For 32-bit argument the
224 * upper half of the 64-bit register location on the stack will now contain junk. Note that
225 * this is dependant on compiler behaviour and that interrupts etc. at the wrong time may
226 * overwrite the junk written to the stack so there's no guarantee that it will always
227 * detect all functions that assumes zero-extension.
229 void x264_checkasm_stack_clobber( uint64_t clobber, ... );
230 #define call_a1(func,...) ({ \
231 uint64_t r = (rand() & 0xffff) * 0x0001000100010001ULL; \
232 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 */ \
233 x264_checkasm_call(( intptr_t(*)())func, &ok, 0, 0, 0, 0, __VA_ARGS__ ); })
235 #define call_a1(func,...) x264_checkasm_call( (intptr_t(*)())func, &ok, __VA_ARGS__ )
237 #define call_a1 call_c1
240 #define call_bench(func,cpu,...)\
241 if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
245 call_a1(func, __VA_ARGS__);\
246 for( int ti = 0; ti < (cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
248 uint32_t t = read_time();\
253 t = read_time() - t;\
254 if( (uint64_t)t*tcount <= tsum*4 && ti > 0 )\
260 bench_t *b = get_bench( func_name, cpu );\
266 /* for most functions, run benchmark and correctness test at the same time.
267 * for those that modify their inputs, run the above macros separately */
268 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
269 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
270 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
271 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
274 static int check_pixel( int cpu_ref, int cpu_new )
276 x264_pixel_function_t pixel_c;
277 x264_pixel_function_t pixel_ref;
278 x264_pixel_function_t pixel_asm;
279 x264_predict_t predict_4x4[12];
280 x264_predict8x8_t predict_8x8[12];
281 x264_predict_8x8_filter_t predict_8x8_filter;
282 ALIGNED_16( pixel edge[36] );
283 uint16_t cost_mv[32];
284 int ret = 0, ok, used_asm;
286 x264_pixel_init( 0, &pixel_c );
287 x264_pixel_init( cpu_ref, &pixel_ref );
288 x264_pixel_init( cpu_new, &pixel_asm );
289 x264_predict_4x4_init( 0, predict_4x4 );
290 x264_predict_8x8_init( 0, predict_8x8, &predict_8x8_filter );
291 predict_8x8_filter( pbuf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
294 for( int i = 0; i < 256; i++ )
299 pbuf4[i] = -(z&1) & PIXEL_MAX;
300 pbuf3[i] = ~pbuf4[i] & PIXEL_MAX;
302 // random pattern made of maxed pixel differences, in case an intermediate value overflows
303 for( int i = 256; i < 0x1000; i++ )
305 pbuf4[i] = -(pbuf1[i&~0x88]&1) & PIXEL_MAX;
306 pbuf3[i] = ~(pbuf4[i]) & PIXEL_MAX;
309 #define TEST_PIXEL( name, align ) \
310 ok = 1, used_asm = 0; \
311 for( int i = 0; i < ARRAY_ELEMS(pixel_c.name); i++ ) \
313 int res_c, res_asm; \
314 if( pixel_asm.name[i] != pixel_ref.name[i] ) \
316 set_func_name( "%s_%s", #name, pixel_names[i] ); \
318 for( int j = 0; j < 64; j++ ) \
320 res_c = call_c( pixel_c.name[i], pbuf1, (intptr_t)16, pbuf2+j*!align, (intptr_t)64 ); \
321 res_asm = call_a( pixel_asm.name[i], pbuf1, (intptr_t)16, pbuf2+j*!align, (intptr_t)64 ); \
322 if( res_c != res_asm ) \
325 fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
329 for( int j = 0; j < 0x1000 && ok; j += 256 ) \
331 res_c = pixel_c .name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
332 res_asm = pixel_asm.name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
333 if( res_c != res_asm ) \
336 fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
341 report( "pixel " #name " :" );
343 TEST_PIXEL( sad, 0 );
344 TEST_PIXEL( sad_aligned, 1 );
345 TEST_PIXEL( ssd, 1 );
346 TEST_PIXEL( satd, 0 );
347 TEST_PIXEL( sa8d, 1 );
349 ok = 1, used_asm = 0;
350 if( pixel_asm.sa8d_satd[PIXEL_16x16] != pixel_ref.sa8d_satd[PIXEL_16x16] )
352 set_func_name( "sa8d_satd_%s", pixel_names[PIXEL_16x16] );
354 for( int j = 0; j < 64; j++ )
356 uint32_t cost8_c = pixel_c.sa8d[PIXEL_16x16]( pbuf1, 16, pbuf2, 64 );
357 uint32_t cost4_c = pixel_c.satd[PIXEL_16x16]( pbuf1, 16, pbuf2, 64 );
358 uint64_t res_a = call_a( pixel_asm.sa8d_satd[PIXEL_16x16], pbuf1, (intptr_t)16, pbuf2, (intptr_t)64 );
359 uint32_t cost8_a = res_a;
360 uint32_t cost4_a = res_a >> 32;
361 if( cost8_a != cost8_c || cost4_a != cost4_c )
364 fprintf( stderr, "sa8d_satd [%d]: (%d,%d) != (%d,%d) [FAILED]\n", PIXEL_16x16,
365 cost8_c, cost4_c, cost8_a, cost4_a );
369 for( int j = 0; j < 0x1000 && ok; j += 256 ) \
371 uint32_t cost8_c = pixel_c.sa8d[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 );
372 uint32_t cost4_c = pixel_c.satd[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 );
373 uint64_t res_a = pixel_asm.sa8d_satd[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 );
374 uint32_t cost8_a = res_a;
375 uint32_t cost4_a = res_a >> 32;
376 if( cost8_a != cost8_c || cost4_a != cost4_c )
379 fprintf( stderr, "sa8d_satd [%d]: overflow (%d,%d) != (%d,%d) [FAILED]\n", PIXEL_16x16,
380 cost8_c, cost4_c, cost8_a, cost4_a );
384 report( "pixel sa8d_satd :" );
386 #define TEST_PIXEL_X( N ) \
387 ok = 1; used_asm = 0; \
388 for( int i = 0; i < 7; i++ ) \
390 ALIGNED_16( int res_c[4] ) = {0}; \
391 ALIGNED_16( int res_asm[4] ) = {0}; \
392 if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
394 set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
396 for( int j = 0; j < 64; j++ ) \
398 pixel *pix2 = pbuf2+j; \
399 res_c[0] = pixel_c.sad[i]( pbuf1, 16, pix2, 64 ); \
400 res_c[1] = pixel_c.sad[i]( pbuf1, 16, pix2+6, 64 ); \
401 res_c[2] = pixel_c.sad[i]( pbuf1, 16, pix2+1, 64 ); \
404 res_c[3] = pixel_c.sad[i]( pbuf1, 16, pix2+10, 64 ); \
405 call_a( pixel_asm.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, (intptr_t)64, res_asm ); \
408 call_a( pixel_asm.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, (intptr_t)64, res_asm ); \
409 if( memcmp(res_c, res_asm, N*sizeof(int)) ) \
412 fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
413 i, res_c[0], res_c[1], res_c[2], res_c[3], \
414 res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
417 call_c2( pixel_c.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, (intptr_t)64, res_asm ); \
419 call_c2( pixel_c.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, (intptr_t)64, res_asm ); \
423 report( "pixel sad_x"#N" :" );
428 #define TEST_PIXEL_VAR( i ) \
429 if( pixel_asm.var[i] != pixel_ref.var[i] ) \
431 set_func_name( "%s_%s", "var", pixel_names[i] ); \
433 /* abi-check wrapper can't return uint64_t, so separate it from return value check */ \
434 call_c1( pixel_c.var[i], pbuf1, 16 ); \
435 call_a1( pixel_asm.var[i], pbuf1, (intptr_t)16 ); \
436 uint64_t res_c = pixel_c.var[i]( pbuf1, 16 ); \
437 uint64_t res_asm = pixel_asm.var[i]( pbuf1, 16 ); \
438 if( res_c != res_asm ) \
441 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) ); \
443 call_c2( pixel_c.var[i], pbuf1, (intptr_t)16 ); \
444 call_a2( pixel_asm.var[i], pbuf1, (intptr_t)16 ); \
447 ok = 1; used_asm = 0;
448 TEST_PIXEL_VAR( PIXEL_16x16 );
449 TEST_PIXEL_VAR( PIXEL_8x16 );
450 TEST_PIXEL_VAR( PIXEL_8x8 );
451 report( "pixel var :" );
453 #define TEST_PIXEL_VAR2( i ) \
454 if( pixel_asm.var2[i] != pixel_ref.var2[i] ) \
456 int res_c, res_asm, ssd_c, ssd_asm; \
457 set_func_name( "%s_%s", "var2", pixel_names[i] ); \
459 res_c = call_c( pixel_c.var2[i], pbuf1, (intptr_t)16, pbuf2, (intptr_t)16, &ssd_c ); \
460 res_asm = call_a( pixel_asm.var2[i], pbuf1, (intptr_t)16, pbuf2, (intptr_t)16, &ssd_asm ); \
461 if( res_c != res_asm || ssd_c != ssd_asm ) \
464 fprintf( stderr, "var2[%d]: %d != %d or %d != %d [FAILED]\n", i, res_c, res_asm, ssd_c, ssd_asm ); \
468 ok = 1; used_asm = 0;
469 TEST_PIXEL_VAR2( PIXEL_8x16 );
470 TEST_PIXEL_VAR2( PIXEL_8x8 );
471 report( "pixel var2 :" );
473 ok = 1; used_asm = 0;
474 for( int i = 0; i < 4; i++ )
475 if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
477 set_func_name( "hadamard_ac_%s", pixel_names[i] );
479 for( int j = 0; j < 32; j++ )
481 pixel *pix = (j&16 ? pbuf1 : pbuf3) + (j&15)*256;
482 call_c1( pixel_c.hadamard_ac[i], pbuf1, (intptr_t)16 );
483 call_a1( pixel_asm.hadamard_ac[i], pbuf1, (intptr_t)16 );
484 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
485 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
489 fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
493 call_c2( pixel_c.hadamard_ac[i], pbuf1, (intptr_t)16 );
494 call_a2( pixel_asm.hadamard_ac[i], pbuf1, (intptr_t)16 );
496 report( "pixel hadamard_ac :" );
499 for( int i = 0; i < 32; i++ )
500 for( int j = 0; j < 16; j++ )
501 pbuf4[16*i+j] = -((i+j)&1) & PIXEL_MAX;
502 ok = 1; used_asm = 0;
503 if( pixel_asm.vsad != pixel_ref.vsad )
505 for( int h = 2; h <= 32; h += 2 )
508 set_func_name( "vsad" );
510 for( int j = 0; j < 2 && ok; j++ )
512 pixel *p = j ? pbuf4 : pbuf1;
513 res_c = call_c( pixel_c.vsad, p, (intptr_t)16, h );
514 res_asm = call_a( pixel_asm.vsad, p, (intptr_t)16, h );
515 if( res_c != res_asm )
518 fprintf( stderr, "vsad: height=%d, %d != %d\n", h, res_c, res_asm );
524 report( "pixel vsad :" );
526 ok = 1; used_asm = 0;
527 if( pixel_asm.asd8 != pixel_ref.asd8 )
529 set_func_name( "asd8" );
531 int res_c = call_c( pixel_c.asd8, pbuf1, (intptr_t)8, pbuf2, (intptr_t)8, 16 );
532 int res_a = call_a( pixel_asm.asd8, pbuf1, (intptr_t)8, pbuf2, (intptr_t)8, 16 );
536 fprintf( stderr, "asd: %d != %d\n", res_c, res_a );
539 report( "pixel asd :" );
541 #define TEST_INTRA_X3( name, i8x8, ... ) \
542 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
544 ALIGNED_16( int res_c[3] ); \
545 ALIGNED_16( int res_asm[3] ); \
546 set_func_name( #name ); \
548 call_c( pixel_c.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_c ); \
549 call_a( pixel_asm.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_asm ); \
550 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
553 fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
554 res_c[0], res_c[1], res_c[2], \
555 res_asm[0], res_asm[1], res_asm[2] ); \
559 #define TEST_INTRA_X9( name, cmp ) \
560 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
562 set_func_name( #name ); \
564 ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
565 for( int i=0; i<17; i++ ) \
566 bitcosts[i] = 9*(i!=8); \
567 memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
568 memcpy( pbuf4, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
569 for( int i=0; i<32; i++ ) \
571 pixel *fenc = pbuf1+48+i*12; \
572 pixel *fdec1 = pbuf3+48+i*12; \
573 pixel *fdec2 = pbuf4+48+i*12; \
574 int pred_mode = i%9; \
575 int res_c = INT_MAX; \
576 for( int j=0; j<9; j++ ) \
578 predict_4x4[j]( fdec1 ); \
579 int cost = pixel_c.cmp[PIXEL_4x4]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
580 if( cost < (uint16_t)res_c ) \
581 res_c = cost + (j<<16); \
583 predict_4x4[res_c>>16]( fdec1 ); \
584 int res_a = call_a( pixel_asm.name, fenc, fdec2, bitcosts+8-pred_mode ); \
585 if( res_c != res_a ) \
588 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
591 if( memcmp(fdec1, fdec2, 4*FDEC_STRIDE*sizeof(pixel)) ) \
594 fprintf( stderr, #name" [FAILED]\n" ); \
595 for( int j=0; j<16; j++ ) \
596 fprintf( stderr, "%02x ", fdec1[(j&3)+(j>>2)*FDEC_STRIDE] ); \
597 fprintf( stderr, "\n" ); \
598 for( int j=0; j<16; j++ ) \
599 fprintf( stderr, "%02x ", fdec2[(j&3)+(j>>2)*FDEC_STRIDE] ); \
600 fprintf( stderr, "\n" ); \
606 #define TEST_INTRA8_X9( name, cmp ) \
607 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
609 set_func_name( #name ); \
611 ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
612 ALIGNED_ARRAY_16( uint16_t, satds_c,[16] ); \
613 ALIGNED_ARRAY_16( uint16_t, satds_a,[16] ); \
614 memset( satds_c, 0, 16 * sizeof(*satds_c) ); \
615 memset( satds_a, 0, 16 * sizeof(*satds_a) ); \
616 for( int i=0; i<17; i++ ) \
617 bitcosts[i] = 9*(i!=8); \
618 for( int i=0; i<32; i++ ) \
620 pixel *fenc = pbuf1+48+i*12; \
621 pixel *fdec1 = pbuf3+48+i*12; \
622 pixel *fdec2 = pbuf4+48+i*12; \
623 int pred_mode = i%9; \
624 int res_c = INT_MAX; \
625 predict_8x8_filter( fdec1, edge, ALL_NEIGHBORS, ALL_NEIGHBORS ); \
626 for( int j=0; j<9; j++ ) \
628 predict_8x8[j]( fdec1, edge ); \
629 satds_c[j] = pixel_c.cmp[PIXEL_8x8]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
630 if( satds_c[j] < (uint16_t)res_c ) \
631 res_c = satds_c[j] + (j<<16); \
633 predict_8x8[res_c>>16]( fdec1, edge ); \
634 int res_a = call_a( pixel_asm.name, fenc, fdec2, edge, bitcosts+8-pred_mode, satds_a ); \
635 if( res_c != res_a || memcmp(satds_c, satds_a, sizeof(satds_c)) ) \
638 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
639 for( int j = 0; j < 9; j++ ) \
640 fprintf( stderr, "%5d ", satds_c[j]); \
641 fprintf( stderr, "\n" ); \
642 for( int j = 0; j < 9; j++ ) \
643 fprintf( stderr, "%5d ", satds_a[j]); \
644 fprintf( stderr, "\n" ); \
647 for( int j=0; j<8; j++ ) \
648 if( memcmp(fdec1+j*FDEC_STRIDE, fdec2+j*FDEC_STRIDE, 8*sizeof(pixel)) ) \
652 fprintf( stderr, #name" [FAILED]\n" ); \
653 for( int j=0; j<8; j++ ) \
655 for( int k=0; k<8; k++ ) \
656 fprintf( stderr, "%02x ", fdec1[k+j*FDEC_STRIDE] ); \
657 fprintf( stderr, "\n" ); \
659 fprintf( stderr, "\n" ); \
660 for( int j=0; j<8; j++ ) \
662 for( int k=0; k<8; k++ ) \
663 fprintf( stderr, "%02x ", fdec2[k+j*FDEC_STRIDE] ); \
664 fprintf( stderr, "\n" ); \
666 fprintf( stderr, "\n" ); \
672 memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) );
673 ok = 1; used_asm = 0;
674 TEST_INTRA_X3( intra_satd_x3_16x16, 0 );
675 TEST_INTRA_X3( intra_satd_x3_8x16c, 0 );
676 TEST_INTRA_X3( intra_satd_x3_8x8c, 0 );
677 TEST_INTRA_X3( intra_sa8d_x3_8x8, 1, edge );
678 TEST_INTRA_X3( intra_satd_x3_4x4, 0 );
679 report( "intra satd_x3 :" );
680 ok = 1; used_asm = 0;
681 TEST_INTRA_X3( intra_sad_x3_16x16, 0 );
682 TEST_INTRA_X3( intra_sad_x3_8x16c, 0 );
683 TEST_INTRA_X3( intra_sad_x3_8x8c, 0 );
684 TEST_INTRA_X3( intra_sad_x3_8x8, 1, edge );
685 TEST_INTRA_X3( intra_sad_x3_4x4, 0 );
686 report( "intra sad_x3 :" );
687 ok = 1; used_asm = 0;
688 TEST_INTRA_X9( intra_satd_x9_4x4, satd );
689 TEST_INTRA8_X9( intra_sa8d_x9_8x8, sa8d );
690 report( "intra satd_x9 :" );
691 ok = 1; used_asm = 0;
692 TEST_INTRA_X9( intra_sad_x9_4x4, sad );
693 TEST_INTRA8_X9( intra_sad_x9_8x8, sad );
694 report( "intra sad_x9 :" );
696 ok = 1; used_asm = 0;
697 if( pixel_asm.ssd_nv12_core != pixel_ref.ssd_nv12_core )
700 set_func_name( "ssd_nv12" );
701 uint64_t res_u_c, res_v_c, res_u_a, res_v_a;
702 pixel_c.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c );
703 pixel_asm.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a );
704 if( res_u_c != res_u_a || res_v_c != res_v_a )
707 fprintf( stderr, "ssd_nv12: %"PRIu64",%"PRIu64" != %"PRIu64",%"PRIu64"\n",
708 res_u_c, res_v_c, res_u_a, res_v_a );
710 call_c( pixel_c.ssd_nv12_core, pbuf1, (intptr_t)368, pbuf2, (intptr_t)368, 360, 8, &res_u_c, &res_v_c );
711 call_a( pixel_asm.ssd_nv12_core, pbuf1, (intptr_t)368, pbuf2, (intptr_t)368, 360, 8, &res_u_a, &res_v_a );
713 report( "ssd_nv12 :" );
715 if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
716 pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
720 ALIGNED_16( int sums[5][4] ) = {{0}};
723 res_c = x264_pixel_ssim_wxh( &pixel_c, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
724 res_a = x264_pixel_ssim_wxh( &pixel_asm, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
725 if( fabs( res_c - res_a ) > 1e-6 )
728 fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
730 set_func_name( "ssim_core" );
731 call_c2( pixel_c.ssim_4x4x2_core, pbuf1+2, (intptr_t)32, pbuf2+2, (intptr_t)32, sums );
732 call_a2( pixel_asm.ssim_4x4x2_core, pbuf1+2, (intptr_t)32, pbuf2+2, (intptr_t)32, sums );
733 set_func_name( "ssim_end" );
734 call_c2( pixel_c.ssim_end4, sums, sums, 4 );
735 call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
739 ok = 1; used_asm = 0;
740 for( int i = 0; i < 32; i++ )
742 for( int i = 0; i < 100 && ok; i++ )
743 if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
745 ALIGNED_16( uint16_t sums[72] );
746 ALIGNED_16( int dc[4] );
747 ALIGNED_16( int16_t mvs_a[48] );
748 ALIGNED_16( int16_t mvs_c[48] );
750 int thresh = rand() & 0x3fff;
751 set_func_name( "esa_ads" );
752 for( int j = 0; j < 72; j++ )
753 sums[j] = rand() & 0x3fff;
754 for( int j = 0; j < 4; j++ )
755 dc[j] = rand() & 0x3fff;
757 mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
758 mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
759 if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
762 printf( "c%d: ", i&3 );
763 for( int j = 0; j < mvn_c; j++ )
764 printf( "%d ", mvs_c[j] );
765 printf( "\na%d: ", i&3 );
766 for( int j = 0; j < mvn_a; j++ )
767 printf( "%d ", mvs_a[j] );
771 report( "esa ads:" );
776 static int check_dct( int cpu_ref, int cpu_new )
778 x264_dct_function_t dct_c;
779 x264_dct_function_t dct_ref;
780 x264_dct_function_t dct_asm;
781 x264_quant_function_t qf;
782 int ret = 0, ok, used_asm, interlace = 0;
783 ALIGNED_ARRAY_N( dctcoef, dct1, [16],[16] );
784 ALIGNED_ARRAY_N( dctcoef, dct2, [16],[16] );
785 ALIGNED_ARRAY_N( dctcoef, dct4, [16],[16] );
786 ALIGNED_ARRAY_N( dctcoef, dct8, [4],[64] );
787 ALIGNED_16( dctcoef dctdc[2][8] );
791 x264_dct_init( 0, &dct_c );
792 x264_dct_init( cpu_ref, &dct_ref);
793 x264_dct_init( cpu_new, &dct_asm );
795 memset( h, 0, sizeof(*h) );
796 x264_param_default( &h->param );
797 h->sps->i_chroma_format_idc = 1;
798 h->chroma_qp_table = i_chroma_qp_table + 12;
799 h->param.analyse.i_luma_deadzone[0] = 0;
800 h->param.analyse.i_luma_deadzone[1] = 0;
801 h->param.analyse.b_transform_8x8 = 1;
802 for( int i = 0; i < 6; i++ )
803 h->pps->scaling_list[i] = x264_cqm_flat16;
805 x264_quant_init( h, 0, &qf );
807 /* overflow test cases */
808 for( int i = 0; i < 5; i++ )
810 pixel *enc = &pbuf3[16*i*FENC_STRIDE];
811 pixel *dec = &pbuf4[16*i*FDEC_STRIDE];
813 for( int j = 0; j < 16; j++ )
815 int cond_a = (i < 2) ? 1 : ((j&3) == 0 || (j&3) == (i-1));
816 int cond_b = (i == 0) ? 1 : !cond_a;
817 enc[0] = enc[1] = enc[4] = enc[5] = enc[8] = enc[9] = enc[12] = enc[13] = cond_a ? PIXEL_MAX : 0;
818 enc[2] = enc[3] = enc[6] = enc[7] = enc[10] = enc[11] = enc[14] = enc[15] = cond_b ? PIXEL_MAX : 0;
820 for( int k = 0; k < 4; k++ )
821 dec[k] = PIXEL_MAX - enc[k];
828 #define TEST_DCT( name, t1, t2, size ) \
829 if( dct_asm.name != dct_ref.name ) \
831 set_func_name( #name ); \
833 pixel *enc = pbuf3; \
834 pixel *dec = pbuf4; \
835 for( int j = 0; j < 5; j++) \
837 call_c( dct_c.name, t1, &pbuf1[j*64], &pbuf2[j*64] ); \
838 call_a( dct_asm.name, t2, &pbuf1[j*64], &pbuf2[j*64] ); \
839 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
842 fprintf( stderr, #name " [FAILED]\n" ); \
843 for( int k = 0; k < size; k++ )\
844 printf( "%d ", ((dctcoef*)t1)[k] );\
846 for( int k = 0; k < size; k++ )\
847 printf( "%d ", ((dctcoef*)t2)[k] );\
851 call_c( dct_c.name, t1, enc, dec ); \
852 call_a( dct_asm.name, t2, enc, dec ); \
853 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
856 fprintf( stderr, #name " [FAILED] (overflow)\n" ); \
859 enc += 16*FENC_STRIDE; \
860 dec += 16*FDEC_STRIDE; \
863 ok = 1; used_asm = 0;
864 TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16 );
865 TEST_DCT( sub8x8_dct, dct1, dct2, 16*4 );
866 TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4 );
867 TEST_DCT( sub8x16_dct_dc, dctdc[0], dctdc[1], 8 );
868 TEST_DCT( sub16x16_dct, dct1, dct2, 16*16 );
869 report( "sub_dct4 :" );
871 ok = 1; used_asm = 0;
872 TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64 );
873 TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*4 );
874 report( "sub_dct8 :" );
877 // fdct and idct are denormalized by different factors, so quant/dequant
878 // is needed to force the coefs into the right range.
879 dct_c.sub16x16_dct( dct4, pbuf1, pbuf2 );
880 dct_c.sub16x16_dct8( dct8, pbuf1, pbuf2 );
881 for( int i = 0; i < 16; i++ )
883 qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
884 qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
886 for( int i = 0; i < 4; i++ )
888 qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
889 qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
891 x264_cqm_delete( h );
893 #define TEST_IDCT( name, src ) \
894 if( dct_asm.name != dct_ref.name ) \
896 set_func_name( #name ); \
898 memcpy( pbuf3, pbuf1, 32*32 * sizeof(pixel) ); \
899 memcpy( pbuf4, pbuf1, 32*32 * sizeof(pixel) ); \
900 memcpy( dct1, src, 256 * sizeof(dctcoef) ); \
901 memcpy( dct2, src, 256 * sizeof(dctcoef) ); \
902 call_c1( dct_c.name, pbuf3, (void*)dct1 ); \
903 call_a1( dct_asm.name, pbuf4, (void*)dct2 ); \
904 if( memcmp( pbuf3, pbuf4, 32*32 * sizeof(pixel) ) ) \
907 fprintf( stderr, #name " [FAILED]\n" ); \
909 call_c2( dct_c.name, pbuf3, (void*)dct1 ); \
910 call_a2( dct_asm.name, pbuf4, (void*)dct2 ); \
912 ok = 1; used_asm = 0;
913 TEST_IDCT( add4x4_idct, dct4 );
914 TEST_IDCT( add8x8_idct, dct4 );
915 TEST_IDCT( add8x8_idct_dc, dct4 );
916 TEST_IDCT( add16x16_idct, dct4 );
917 TEST_IDCT( add16x16_idct_dc, dct4 );
918 report( "add_idct4 :" );
920 ok = 1; used_asm = 0;
921 TEST_IDCT( add8x8_idct8, dct8 );
922 TEST_IDCT( add16x16_idct8, dct8 );
923 report( "add_idct8 :" );
926 #define TEST_DCTDC( name )\
927 ok = 1; used_asm = 0;\
928 if( dct_asm.name != dct_ref.name )\
930 set_func_name( #name );\
932 uint16_t *p = (uint16_t*)buf1;\
933 for( int i = 0; i < 16 && ok; i++ )\
935 for( int j = 0; j < 16; j++ )\
936 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
937 : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
938 : ((*p++)&0x1fff)-0x1000; /* general case */\
939 memcpy( dct2, dct1, 16 * sizeof(dctcoef) );\
940 call_c1( dct_c.name, dct1[0] );\
941 call_a1( dct_asm.name, dct2[0] );\
942 if( memcmp( dct1, dct2, 16 * sizeof(dctcoef) ) )\
945 call_c2( dct_c.name, dct1[0] );\
946 call_a2( dct_asm.name, dct2[0] );\
948 report( #name " :" );
950 TEST_DCTDC( dct4x4dc );
951 TEST_DCTDC( idct4x4dc );
954 #define TEST_DCTDC_CHROMA( name )\
955 ok = 1; used_asm = 0;\
956 if( dct_asm.name != dct_ref.name )\
958 set_func_name( #name );\
960 uint16_t *p = (uint16_t*)buf1;\
961 for( int i = 0; i < 16 && ok; i++ )\
963 for( int j = 0; j < 8; j++ )\
964 dct1[j][0] = !i ? (j^j>>1^j>>2)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
965 : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
966 : ((*p++)&0x1fff)-0x1000; /* general case */\
967 memcpy( dct2, dct1, 8*16 * sizeof(dctcoef) );\
968 call_c1( dct_c.name, dctdc[0], dct1 );\
969 call_a1( dct_asm.name, dctdc[1], dct2 );\
970 if( memcmp( dctdc[0], dctdc[1], 8 * sizeof(dctcoef) ) || memcmp( dct1, dct2, 8*16 * sizeof(dctcoef) ) )\
973 fprintf( stderr, #name " [FAILED]\n" ); \
976 call_c2( dct_c.name, dctdc[0], dct1 );\
977 call_a2( dct_asm.name, dctdc[1], dct2 );\
979 report( #name " :" );
981 TEST_DCTDC_CHROMA( dct2x4dc );
982 #undef TEST_DCTDC_CHROMA
984 x264_zigzag_function_t zigzag_c[2];
985 x264_zigzag_function_t zigzag_ref[2];
986 x264_zigzag_function_t zigzag_asm[2];
988 ALIGNED_16( dctcoef level1[64] );
989 ALIGNED_16( dctcoef level2[64] );
991 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
992 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
994 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
996 for( int i = 0; i < size*size; i++ ) \
998 call_c( zigzag_c[interlace].name, t1, dct ); \
999 call_a( zigzag_asm[interlace].name, t2, dct ); \
1000 if( memcmp( t1, t2, size*size*sizeof(dctcoef) ) ) \
1003 for( int i = 0; i < 2; i++ ) \
1005 dctcoef *d = (dctcoef*)(i ? t2 : t1); \
1006 for( int j = 0; j < size; j++ ) \
1008 for( int k = 0; k < size; k++ ) \
1009 fprintf( stderr, "%2d ", d[k+j*8] ); \
1010 fprintf( stderr, "\n" ); \
1012 fprintf( stderr, "\n" ); \
1014 fprintf( stderr, #name " [FAILED]\n" ); \
1018 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
1019 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1022 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1024 memcpy( pbuf3, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
1025 memcpy( pbuf4, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
1026 nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
1027 nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
1028 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE*sizeof(pixel) ) || nz_c != nz_a ) \
1031 fprintf( stderr, #name " [FAILED]\n" ); \
1033 call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
1034 call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
1037 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
1038 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1041 dctcoef dc_a, dc_c; \
1042 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1044 for( int i = 0; i < 2; i++ ) \
1046 memcpy( pbuf3, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
1047 memcpy( pbuf4, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
1048 for( int j = 0; j < 4; j++ ) \
1050 memcpy( pbuf3 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
1051 memcpy( pbuf4 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
1053 nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
1054 nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
1055 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 ) \
1058 fprintf( stderr, #name " [FAILED]\n" ); \
1062 call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
1063 call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
1066 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
1067 if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1069 for( int j = 0; j < 100; j++ ) \
1071 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1073 memcpy(dct, buf1, size*sizeof(dctcoef)); \
1074 for( int i = 0; i < size; i++ ) \
1075 dct[i] = rand()&0x1F ? 0 : dct[i]; \
1076 memcpy(buf3, buf4, 10); \
1077 call_c( zigzag_c[interlace].name, t1, dct, buf3 ); \
1078 call_a( zigzag_asm[interlace].name, t2, dct, buf4 ); \
1079 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( buf3, buf4, 10 ) ) \
1081 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;\
1086 x264_zigzag_init( 0, &zigzag_c[0], &zigzag_c[1] );
1087 x264_zigzag_init( cpu_ref, &zigzag_ref[0], &zigzag_ref[1] );
1088 x264_zigzag_init( cpu_new, &zigzag_asm[0], &zigzag_asm[1] );
1090 ok = 1; used_asm = 0;
1091 TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct8[0], 64 );
1092 report( "zigzag_interleave :" );
1094 for( interlace = 0; interlace <= 1; interlace++ )
1096 ok = 1; used_asm = 0;
1097 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, dct8[0], 8 );
1098 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 4 );
1099 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
1100 TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
1101 report( interlace ? "zigzag_field :" : "zigzag_frame :" );
1103 #undef TEST_ZIGZAG_SCAN
1104 #undef TEST_ZIGZAG_SUB
1109 static int check_mc( int cpu_ref, int cpu_new )
1111 x264_mc_functions_t mc_c;
1112 x264_mc_functions_t mc_ref;
1113 x264_mc_functions_t mc_a;
1114 x264_pixel_function_t pixf;
1116 pixel *src = &(pbuf1)[2*64+2];
1117 pixel *src2[4] = { &(pbuf1)[3*64+2], &(pbuf1)[5*64+2],
1118 &(pbuf1)[7*64+2], &(pbuf1)[9*64+2] };
1119 pixel *dst1 = pbuf3;
1120 pixel *dst2 = pbuf4;
1122 int ret = 0, ok, used_asm;
1124 x264_mc_init( 0, &mc_c, 0 );
1125 x264_mc_init( cpu_ref, &mc_ref, 0 );
1126 x264_mc_init( cpu_new, &mc_a, 0 );
1127 x264_pixel_init( 0, &pixf );
1129 #define MC_TEST_LUMA( w, h ) \
1130 if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
1132 const x264_weight_t *weight = x264_weight_none; \
1133 set_func_name( "mc_luma_%dx%d", w, h ); \
1135 for( int i = 0; i < 1024; i++ ) \
1136 pbuf3[i] = pbuf4[i] = 0xCD; \
1137 call_c( mc_c.mc_luma, dst1, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1138 call_a( mc_a.mc_luma, dst2, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1139 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1141 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
1145 if( mc_a.get_ref != mc_ref.get_ref ) \
1147 pixel *ref = dst2; \
1148 intptr_t ref_stride = 32; \
1149 int w_checked = ( ( sizeof(pixel) == 2 && (w == 12 || w == 20)) ? w-2 : w ); \
1150 const x264_weight_t *weight = x264_weight_none; \
1151 set_func_name( "get_ref_%dx%d", w_checked, h ); \
1153 for( int i = 0; i < 1024; i++ ) \
1154 pbuf3[i] = pbuf4[i] = 0xCD; \
1155 call_c( mc_c.mc_luma, dst1, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1156 ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1157 for( int i = 0; i < h; i++ ) \
1158 if( memcmp( dst1+i*32, ref+i*ref_stride, w_checked * sizeof(pixel) ) ) \
1160 fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w_checked, h ); \
1166 #define MC_TEST_CHROMA( w, h ) \
1167 if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
1169 set_func_name( "mc_chroma_%dx%d", w, h ); \
1171 for( int i = 0; i < 1024; i++ ) \
1172 pbuf3[i] = pbuf4[i] = 0xCD; \
1173 call_c( mc_c.mc_chroma, dst1, dst1+8, (intptr_t)16, src, (intptr_t)64, dx, dy, w, h ); \
1174 call_a( mc_a.mc_chroma, dst2, dst2+8, (intptr_t)16, src, (intptr_t)64, dx, dy, w, h ); \
1175 /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \
1176 for( int j = 0; j < h; j++ ) \
1177 for( int i = w; i < 8; i++ ) \
1179 dst2[i+j*16+8] = dst1[i+j*16+8]; \
1180 dst2[i+j*16 ] = dst1[i+j*16 ]; \
1182 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1184 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
1188 ok = 1; used_asm = 0;
1189 for( int dy = -8; dy < 8; dy++ )
1190 for( int dx = -128; dx < 128; dx++ )
1192 if( rand()&15 ) continue; // running all of them is too slow
1193 MC_TEST_LUMA( 20, 18 );
1194 MC_TEST_LUMA( 16, 16 );
1195 MC_TEST_LUMA( 16, 8 );
1196 MC_TEST_LUMA( 12, 10 );
1197 MC_TEST_LUMA( 8, 16 );
1198 MC_TEST_LUMA( 8, 8 );
1199 MC_TEST_LUMA( 8, 4 );
1200 MC_TEST_LUMA( 4, 8 );
1201 MC_TEST_LUMA( 4, 4 );
1203 report( "mc luma :" );
1205 ok = 1; used_asm = 0;
1206 for( int dy = -1; dy < 9; dy++ )
1207 for( int dx = -128; dx < 128; dx++ )
1209 if( rand()&15 ) continue;
1210 MC_TEST_CHROMA( 8, 8 );
1211 MC_TEST_CHROMA( 8, 4 );
1212 MC_TEST_CHROMA( 4, 8 );
1213 MC_TEST_CHROMA( 4, 4 );
1214 MC_TEST_CHROMA( 4, 2 );
1215 MC_TEST_CHROMA( 2, 4 );
1216 MC_TEST_CHROMA( 2, 2 );
1218 report( "mc chroma :" );
1220 #undef MC_TEST_CHROMA
1222 #define MC_TEST_AVG( name, weight ) \
1224 for( int i = 0; i < 12; i++ ) \
1226 memcpy( pbuf3, pbuf1+320, 320 * sizeof(pixel) ); \
1227 memcpy( pbuf4, pbuf1+320, 320 * sizeof(pixel) ); \
1228 if( mc_a.name[i] != mc_ref.name[i] ) \
1230 set_func_name( "%s_%s", #name, pixel_names[i] ); \
1232 call_c1( mc_c.name[i], pbuf3, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1233 call_a1( mc_a.name[i], pbuf4, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1234 if( memcmp( pbuf3, pbuf4, 320 * sizeof(pixel) ) ) \
1237 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
1239 call_c2( mc_c.name[i], pbuf3, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1240 call_a2( mc_a.name[i], pbuf4, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1245 ok = 1, used_asm = 0;
1246 for( int w = -63; w <= 127 && ok; w++ )
1247 MC_TEST_AVG( avg, w );
1248 report( "mc wpredb :" );
1250 #define MC_TEST_WEIGHT( name, weight, aligned ) \
1251 int align_off = (aligned ? 0 : rand()%16); \
1252 for( int i = 1; i <= 5; i++ ) \
1254 ALIGNED_16( pixel buffC[640] ); \
1255 ALIGNED_16( pixel buffA[640] ); \
1256 int j = X264_MAX( i*4, 2 ); \
1257 memset( buffC, 0, 640 * sizeof(pixel) ); \
1258 memset( buffA, 0, 640 * sizeof(pixel) ); \
1261 /* w12 is the same as w16 in some cases */ \
1262 if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
1264 if( mc_a.name[i] != mc_ref.name[i] ) \
1266 set_func_name( "%s_w%d", #name, j ); \
1268 call_c1( mc_c.weight[i], buffC, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1269 mc_a.weight_cache(&ha, &weight); \
1270 call_a1( weight.weightfn[i], buffA, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1271 for( int k = 0; k < 16; k++ ) \
1272 if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \
1275 fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
1278 /* omit unlikely high scales for benchmarking */ \
1279 if( (s << (8-d)) < 512 ) \
1281 call_c2( mc_c.weight[i], buffC, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1282 call_a2( weight.weightfn[i], buffA, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1287 ok = 1; used_asm = 0;
1290 for( int s = 0; s <= 127 && ok; s++ )
1292 for( int o = -128; o <= 127 && ok; o++ )
1294 if( rand() & 2047 ) continue;
1295 for( int d = 0; d <= 7 && ok; d++ )
1299 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
1300 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
1305 report( "mc weight :" );
1307 ok = 1; used_asm = 0;
1308 for( int o = 0; o <= 127 && ok; o++ )
1311 if( rand() & 15 ) continue;
1312 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1313 MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
1315 report( "mc offsetadd :" );
1316 ok = 1; used_asm = 0;
1317 for( int o = -128; o < 0 && ok; o++ )
1320 if( rand() & 15 ) continue;
1321 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1322 MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
1324 report( "mc offsetsub :" );
1326 ok = 1; used_asm = 0;
1327 for( int height = 8; height <= 16; height += 8 )
1329 if( mc_a.store_interleave_chroma != mc_ref.store_interleave_chroma )
1331 set_func_name( "store_interleave_chroma" );
1333 memset( pbuf3, 0, 64*height );
1334 memset( pbuf4, 0, 64*height );
1335 call_c( mc_c.store_interleave_chroma, pbuf3, (intptr_t)64, pbuf1, pbuf1+16, height );
1336 call_a( mc_a.store_interleave_chroma, pbuf4, (intptr_t)64, pbuf1, pbuf1+16, height );
1337 if( memcmp( pbuf3, pbuf4, 64*height ) )
1340 fprintf( stderr, "store_interleave_chroma FAILED: h=%d\n", height );
1344 if( mc_a.load_deinterleave_chroma_fenc != mc_ref.load_deinterleave_chroma_fenc )
1346 set_func_name( "load_deinterleave_chroma_fenc" );
1348 call_c( mc_c.load_deinterleave_chroma_fenc, pbuf3, pbuf1, (intptr_t)64, height );
1349 call_a( mc_a.load_deinterleave_chroma_fenc, pbuf4, pbuf1, (intptr_t)64, height );
1350 if( memcmp( pbuf3, pbuf4, FENC_STRIDE*height ) )
1353 fprintf( stderr, "load_deinterleave_chroma_fenc FAILED: h=%d\n", height );
1357 if( mc_a.load_deinterleave_chroma_fdec != mc_ref.load_deinterleave_chroma_fdec )
1359 set_func_name( "load_deinterleave_chroma_fdec" );
1361 call_c( mc_c.load_deinterleave_chroma_fdec, pbuf3, pbuf1, (intptr_t)64, height );
1362 call_a( mc_a.load_deinterleave_chroma_fdec, pbuf4, pbuf1, (intptr_t)64, height );
1363 if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*height ) )
1366 fprintf( stderr, "load_deinterleave_chroma_fdec FAILED: h=%d\n", height );
1371 report( "store_interleave :" );
1374 int w, h, src_stride;
1375 } 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} };
1376 ok = 1; used_asm = 0;
1377 if( mc_a.plane_copy != mc_ref.plane_copy )
1379 set_func_name( "plane_copy" );
1381 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1383 int w = plane_specs[i].w;
1384 int h = plane_specs[i].h;
1385 intptr_t src_stride = plane_specs[i].src_stride;
1386 intptr_t dst_stride = (w + 127) & ~63;
1387 assert( dst_stride * h <= 0x1000 );
1388 pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1389 memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1390 memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1391 call_c( mc_c.plane_copy, pbuf3, dst_stride, src1, src_stride, w, h );
1392 call_a( mc_a.plane_copy, pbuf4, dst_stride, src1, src_stride, w, h );
1393 for( int y = 0; y < h; y++ )
1394 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(pixel) ) )
1397 fprintf( stderr, "plane_copy FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1403 if( mc_a.plane_copy_interleave != mc_ref.plane_copy_interleave )
1405 set_func_name( "plane_copy_interleave" );
1407 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1409 int w = (plane_specs[i].w + 1) >> 1;
1410 int h = plane_specs[i].h;
1411 intptr_t src_stride = (plane_specs[i].src_stride + 1) >> 1;
1412 intptr_t dst_stride = (2*w + 127) & ~63;
1413 assert( dst_stride * h <= 0x1000 );
1414 pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1415 memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1416 memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1417 call_c( mc_c.plane_copy_interleave, pbuf3, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1418 call_a( mc_a.plane_copy_interleave, pbuf4, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1419 for( int y = 0; y < h; y++ )
1420 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) )
1423 fprintf( stderr, "plane_copy_interleave FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1429 if( mc_a.plane_copy_deinterleave != mc_ref.plane_copy_deinterleave )
1431 set_func_name( "plane_copy_deinterleave" );
1433 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1435 int w = (plane_specs[i].w + 1) >> 1;
1436 int h = plane_specs[i].h;
1437 intptr_t dst_stride = w;
1438 intptr_t src_stride = (2*w + 127) & ~63;
1439 intptr_t offv = (dst_stride*h + 31) & ~15;
1440 memset( pbuf3, 0, 0x1000 );
1441 memset( pbuf4, 0, 0x1000 );
1442 call_c( mc_c.plane_copy_deinterleave, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf1, src_stride, w, h );
1443 call_a( mc_a.plane_copy_deinterleave, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf1, src_stride, w, h );
1444 for( int y = 0; y < h; y++ )
1445 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w ) ||
1446 memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w ) )
1449 fprintf( stderr, "plane_copy_deinterleave FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1454 report( "plane_copy :" );
1456 if( mc_a.hpel_filter != mc_ref.hpel_filter )
1458 pixel *srchpel = pbuf1+8+2*64;
1459 pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
1460 pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
1461 void *tmp = pbuf3+49*64;
1462 set_func_name( "hpel_filter" );
1463 ok = 1; used_asm = 1;
1464 memset( pbuf3, 0, 4096 * sizeof(pixel) );
1465 memset( pbuf4, 0, 4096 * sizeof(pixel) );
1466 call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, (intptr_t)64, 48, 10, tmp );
1467 call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, (intptr_t)64, 48, 10, tmp );
1468 for( int i = 0; i < 3; i++ )
1469 for( int j = 0; j < 10; j++ )
1470 //FIXME ideally the first pixels would match too, but they aren't actually used
1471 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
1474 fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
1475 for( int k = 0; k < 48; k++ )
1476 printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
1478 for( int k = 0; k < 48; k++ )
1479 printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
1483 report( "hpel filter :" );
1486 if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
1488 pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
1489 pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1490 set_func_name( "lowres_init" );
1491 ok = 1; used_asm = 1;
1492 for( int w = 96; w <= 96+24; w += 8 )
1494 intptr_t stride = (w*2+31)&~31;
1495 intptr_t stride_lowres = (w+31)&~31;
1496 call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], stride, stride_lowres, w, 8 );
1497 call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], stride, stride_lowres, w, 8 );
1498 for( int i = 0; i < 8; i++ )
1500 for( int j = 0; j < 4; j++ )
1501 if( memcmp( dstc[j]+i*stride_lowres, dsta[j]+i*stride_lowres, w * sizeof(pixel) ) )
1504 fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1505 for( int k = 0; k < w; k++ )
1506 printf( "%d ", dstc[j][k+i*stride_lowres] );
1508 for( int k = 0; k < w; k++ )
1509 printf( "%d ", dsta[j][k+i*stride_lowres] );
1515 report( "lowres init :" );
1518 #define INTEGRAL_INIT( name, size, ... )\
1519 if( mc_a.name != mc_ref.name )\
1521 intptr_t stride = 96;\
1522 set_func_name( #name );\
1524 memcpy( buf3, buf1, size*2*stride );\
1525 memcpy( buf4, buf1, size*2*stride );\
1526 uint16_t *sum = (uint16_t*)buf3;\
1527 call_c1( mc_c.name, __VA_ARGS__ );\
1528 sum = (uint16_t*)buf4;\
1529 call_a1( mc_a.name, __VA_ARGS__ );\
1530 if( memcmp( buf3, buf4, (stride-8)*2 ) \
1531 || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
1533 call_c2( mc_c.name, __VA_ARGS__ );\
1534 call_a2( mc_a.name, __VA_ARGS__ );\
1536 ok = 1; used_asm = 0;
1537 INTEGRAL_INIT( integral_init4h, 2, sum+stride, pbuf2, stride );
1538 INTEGRAL_INIT( integral_init8h, 2, sum+stride, pbuf2, stride );
1539 INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
1540 INTEGRAL_INIT( integral_init8v, 9, sum, stride );
1541 report( "integral init :" );
1543 if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1545 ok = 1; used_asm = 1;
1547 for( int i = 0; i < 10; i++ )
1549 float fps_factor = (rand()&65535) / 256.;
1550 set_func_name( "mbtree_propagate" );
1551 int *dsta = (int*)buf3;
1552 int *dstc = dsta+400;
1553 uint16_t *prop = (uint16_t*)buf1;
1554 uint16_t *intra = (uint16_t*)buf4;
1555 uint16_t *inter = intra+128;
1556 uint16_t *qscale = inter+128;
1557 uint16_t *rnd = (uint16_t*)buf2;
1559 for( int j = 0; j < 100; j++ )
1561 intra[j] = *rnd++ & 0x7fff;
1562 intra[j] += !intra[j];
1563 inter[j] = *rnd++ & 0x7fff;
1564 qscale[j] = *rnd++ & 0x7fff;
1566 call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, &fps_factor, 100 );
1567 call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, &fps_factor, 100 );
1568 // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1570 for( int j = 0; j < 100 && ok; j++ )
1572 ok &= abs( dstc[j]-dsta[j] ) <= 1 || fabs( (double)dstc[j]/dsta[j]-1 ) < 1e-4;
1574 fprintf( stderr, "mbtree_propagate FAILED: %f !~= %f\n", (double)dstc[j], (double)dsta[j] );
1577 report( "mbtree propagate :" );
1580 if( mc_a.memcpy_aligned != mc_ref.memcpy_aligned )
1582 set_func_name( "memcpy_aligned" );
1583 ok = 1; used_asm = 1;
1584 for( size_t size = 16; size < 256; size += 16 )
1586 memset( buf4, 0xAA, size + 1 );
1587 call_c( mc_c.memcpy_aligned, buf3, buf1, size );
1588 call_a( mc_a.memcpy_aligned, buf4, buf1, size );
1589 if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1592 fprintf( stderr, "memcpy_aligned FAILED: size=%d\n", (int)size );
1596 report( "memcpy aligned :" );
1599 if( mc_a.memzero_aligned != mc_ref.memzero_aligned )
1601 set_func_name( "memzero_aligned" );
1602 ok = 1; used_asm = 1;
1603 for( size_t size = 128; size < 1024; size += 128 )
1605 memset( buf4, 0xAA, size + 1 );
1606 call_c( mc_c.memzero_aligned, buf3, size );
1607 call_a( mc_a.memzero_aligned, buf4, size );
1608 if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1611 fprintf( stderr, "memzero_aligned FAILED: size=%d\n", (int)size );
1615 report( "memzero aligned :" );
1621 static int check_deblock( int cpu_ref, int cpu_new )
1623 x264_deblock_function_t db_c;
1624 x264_deblock_function_t db_ref;
1625 x264_deblock_function_t db_a;
1626 int ret = 0, ok = 1, used_asm = 0;
1627 int alphas[36], betas[36];
1630 x264_deblock_init( 0, &db_c, 0 );
1631 x264_deblock_init( cpu_ref, &db_ref, 0 );
1632 x264_deblock_init( cpu_new, &db_a, 0 );
1634 /* not exactly the real values of a,b,tc but close enough */
1635 for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1637 alphas[i] = a << (BIT_DEPTH-8);
1638 betas[i] = (i+1)/2 << (BIT_DEPTH-8);
1639 tcs[i][0] = tcs[i][3] = (c+6)/10 << (BIT_DEPTH-8);
1640 tcs[i][1] = (c+7)/15 << (BIT_DEPTH-8);
1641 tcs[i][2] = (c+9)/20 << (BIT_DEPTH-8);
1646 #define TEST_DEBLOCK( name, align, ... ) \
1647 for( int i = 0; i < 36; i++ ) \
1649 intptr_t off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1650 for( int j = 0; j < 1024; j++ ) \
1651 /* two distributions of random to excersize different failure modes */ \
1652 pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \
1653 memcpy( pbuf4, pbuf3, 1024 * sizeof(pixel) ); \
1654 if( db_a.name != db_ref.name ) \
1656 set_func_name( #name ); \
1658 call_c1( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1659 call_a1( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1660 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1663 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1666 call_c2( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1667 call_a2( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1671 TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1672 TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1673 TEST_DEBLOCK( deblock_h_chroma_420, 0, tcs[i] );
1674 TEST_DEBLOCK( deblock_h_chroma_422, 0, tcs[i] );
1675 TEST_DEBLOCK( deblock_chroma_420_mbaff, 0, tcs[i] );
1676 TEST_DEBLOCK( deblock_chroma_422_mbaff, 0, tcs[i] );
1677 TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1678 TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1679 TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1680 TEST_DEBLOCK( deblock_h_chroma_420_intra, 0 );
1681 TEST_DEBLOCK( deblock_h_chroma_422_intra, 0 );
1682 TEST_DEBLOCK( deblock_chroma_420_intra_mbaff, 0 );
1683 TEST_DEBLOCK( deblock_chroma_422_intra_mbaff, 0 );
1684 TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1686 if( db_a.deblock_strength != db_ref.deblock_strength )
1688 for( int i = 0; i < 100; i++ )
1690 ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1691 ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1692 ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] );
1693 ALIGNED_ARRAY_N( uint8_t, bs, [2],[2][8][4] );
1694 memset( bs, 99, sizeof(uint8_t)*2*4*8*2 );
1695 for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1696 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1697 for( int j = 0; j < 2; j++ )
1698 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1700 ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1701 for( int l = 0; l < 2; l++ )
1702 mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1704 set_func_name( "deblock_strength" );
1705 call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1706 call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1707 if( memcmp( bs[0], bs[1], sizeof(uint8_t)*2*4*8 ) )
1710 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1711 for( int j = 0; j < 2; j++ )
1713 for( int k = 0; k < 2; k++ )
1714 for( int l = 0; l < 4; l++ )
1716 for( int m = 0; m < 4; m++ )
1717 printf("%d ",bs[j][k][l][m]);
1727 report( "deblock :" );
1732 static int check_quant( int cpu_ref, int cpu_new )
1734 x264_quant_function_t qf_c;
1735 x264_quant_function_t qf_ref;
1736 x264_quant_function_t qf_a;
1737 ALIGNED_ARRAY_N( dctcoef, dct1,[64] );
1738 ALIGNED_ARRAY_N( dctcoef, dct2,[64] );
1739 ALIGNED_ARRAY_N( dctcoef, dct3,[8],[16] );
1740 ALIGNED_ARRAY_N( dctcoef, dct4,[8],[16] );
1741 ALIGNED_ARRAY_N( uint8_t, cqm_buf,[64] );
1742 int ret = 0, ok, used_asm;
1743 int oks[3] = {1,1,1}, used_asms[3] = {0,0,0};
1746 memset( h, 0, sizeof(*h) );
1747 h->sps->i_chroma_format_idc = 1;
1748 x264_param_default( &h->param );
1749 h->chroma_qp_table = i_chroma_qp_table + 12;
1750 h->param.analyse.b_transform_8x8 = 1;
1752 for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1756 for( int i = 0; i < 6; i++ )
1757 h->pps->scaling_list[i] = x264_cqm_flat16;
1758 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1760 else if( i_cqm == 1 )
1762 for( int i = 0; i < 6; i++ )
1763 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1764 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1768 int max_scale = BIT_DEPTH < 10 ? 255 : 228;
1770 for( int i = 0; i < 64; i++ )
1771 cqm_buf[i] = 10 + rand() % (max_scale - 9);
1773 for( int i = 0; i < 64; i++ )
1775 for( int i = 0; i < 6; i++ )
1776 h->pps->scaling_list[i] = cqm_buf;
1777 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1780 h->param.rc.i_qp_min = 0;
1781 h->param.rc.i_qp_max = QP_MAX;
1783 x264_quant_init( h, 0, &qf_c );
1784 x264_quant_init( h, cpu_ref, &qf_ref );
1785 x264_quant_init( h, cpu_new, &qf_a );
1787 #define INIT_QUANT8(j,max) \
1789 static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1790 for( int i = 0; i < max; i++ ) \
1792 unsigned int scale = (255*scale1d[(i>>3)&7]*scale1d[i&7])/16; \
1793 dct1[i] = dct2[i] = (j>>(i>>6))&1 ? (rand()%(2*scale+1))-scale : 0; \
1797 #define INIT_QUANT4(j,max) \
1799 static const int scale1d[4] = {4,6,4,6}; \
1800 for( int i = 0; i < max; i++ ) \
1802 unsigned int scale = 255*scale1d[(i>>2)&3]*scale1d[i&3]; \
1803 dct1[i] = dct2[i] = (j>>(i>>4))&1 ? (rand()%(2*scale+1))-scale : 0; \
1807 #define TEST_QUANT_DC( name, cqm ) \
1808 if( qf_a.name != qf_ref.name ) \
1810 set_func_name( #name ); \
1812 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1814 for( int j = 0; j < 2; j++ ) \
1816 int result_c, result_a; \
1817 for( int i = 0; i < 16; i++ ) \
1818 dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1819 result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1820 result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1821 if( memcmp( dct1, dct2, 16*sizeof(dctcoef) ) || result_c != result_a ) \
1824 fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1827 call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1828 call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1833 #define TEST_QUANT( qname, block, type, w, maxj ) \
1834 if( qf_a.qname != qf_ref.qname ) \
1836 set_func_name( #qname ); \
1838 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1840 for( int j = 0; j < maxj; j++ ) \
1842 INIT_QUANT##type(j, w*w) \
1843 int result_c = call_c1( qf_c.qname, (void*)dct1, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1844 int result_a = call_a1( qf_a.qname, (void*)dct2, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1845 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \
1848 fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1851 call_c2( qf_c.qname, (void*)dct1, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1852 call_a2( qf_a.qname, (void*)dct2, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1857 TEST_QUANT( quant_8x8, CQM_8IY, 8, 8, 2 );
1858 TEST_QUANT( quant_8x8, CQM_8PY, 8, 8, 2 );
1859 TEST_QUANT( quant_4x4, CQM_4IY, 4, 4, 2 );
1860 TEST_QUANT( quant_4x4, CQM_4PY, 4, 4, 2 );
1861 TEST_QUANT( quant_4x4x4, CQM_4IY, 4, 8, 16 );
1862 TEST_QUANT( quant_4x4x4, CQM_4PY, 4, 8, 16 );
1863 TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1864 TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1866 #define TEST_DEQUANT( qname, dqname, block, w ) \
1867 if( qf_a.dqname != qf_ref.dqname ) \
1869 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1871 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1873 INIT_QUANT##w(1, w*w) \
1874 qf_c.qname( dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1875 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1876 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1877 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1878 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1881 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1884 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1885 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1889 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1890 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1891 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1892 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1894 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1895 if( qf_a.dqname != qf_ref.dqname ) \
1897 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1899 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1901 for( int i = 0; i < 16; i++ ) \
1902 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16; \
1903 qf_c.qname( dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1904 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1905 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1906 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1907 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1910 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1912 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1913 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1917 TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1919 if( qf_a.idct_dequant_2x4_dc != qf_ref.idct_dequant_2x4_dc )
1921 set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1923 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1925 for( int i = 0; i < 8; i++ )
1926 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1927 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 );
1928 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 );
1929 call_c( qf_c.idct_dequant_2x4_dc, dct1, dct3, h->dequant4_mf[CQM_4IC], qp+3 );
1930 call_a( qf_a.idct_dequant_2x4_dc, dct1, dct4, h->dequant4_mf[CQM_4IC], qp+3 );
1931 for( int i = 0; i < 8; i++ )
1932 if( dct3[i][0] != dct4[i][0] )
1935 fprintf( stderr, "idct_dequant_2x4_dc (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1941 if( qf_a.idct_dequant_2x4_dconly != qf_ref.idct_dequant_2x4_dconly )
1943 set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1945 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1947 for( int i = 0; i < 8; i++ )
1948 dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1949 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 );
1950 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 );
1951 memcpy( dct2, dct1, 8*sizeof(dctcoef) );
1952 call_c1( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1953 call_a1( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1954 if( memcmp( dct1, dct2, 8*sizeof(dctcoef) ) )
1957 fprintf( stderr, "idct_dequant_2x4_dconly (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1960 call_c2( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1961 call_a2( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1965 #define TEST_OPTIMIZE_CHROMA_DC( optname, size ) \
1966 if( qf_a.optname != qf_ref.optname ) \
1968 set_func_name( #optname ); \
1970 for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1972 int qpdc = qp + (size == 8 ? 3 : 0); \
1973 int dmf = h->dequant4_mf[CQM_4IC][qpdc%6][0] << qpdc/6; \
1976 for( int i = 16; ; i <<= 1 ) \
1978 int res_c, res_asm; \
1979 int max = X264_MIN( i, PIXEL_MAX*16 ); \
1980 for( int j = 0; j < size; j++ ) \
1981 dct1[j] = rand()%(max*2+1) - max; \
1982 for( int j = 0; i <= size; j += 4 ) \
1983 qf_c.quant_2x2_dc( &dct1[j], h->quant4_mf[CQM_4IC][qpdc][0]>>1, h->quant4_bias[CQM_4IC][qpdc][0]>>1 ); \
1984 memcpy( dct2, dct1, size*sizeof(dctcoef) ); \
1985 res_c = call_c1( qf_c.optname, dct1, dmf ); \
1986 res_asm = call_a1( qf_a.optname, dct2, dmf ); \
1987 if( res_c != res_asm || memcmp( dct1, dct2, size*sizeof(dctcoef) ) ) \
1990 fprintf( stderr, #optname "(qp=%d, res_c=%d, res_asm=%d): [FAILED]\n", qp, res_c, res_asm ); \
1992 call_c2( qf_c.optname, dct1, dmf ); \
1993 call_a2( qf_a.optname, dct2, dmf ); \
1994 if( i >= PIXEL_MAX*16 ) \
2000 TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x2_dc, 4 );
2001 TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x4_dc, 8 );
2003 x264_cqm_delete( h );
2006 ok = oks[0]; used_asm = used_asms[0];
2007 report( "quant :" );
2009 ok = oks[1]; used_asm = used_asms[1];
2010 report( "dequant :" );
2012 ok = oks[2]; used_asm = used_asms[2];
2013 report( "optimize chroma dc :" );
2015 ok = 1; used_asm = 0;
2016 if( qf_a.denoise_dct != qf_ref.denoise_dct )
2019 for( int size = 16; size <= 64; size += 48 )
2021 set_func_name( "denoise_dct" );
2022 memcpy( dct1, buf1, size*sizeof(dctcoef) );
2023 memcpy( dct2, buf1, size*sizeof(dctcoef) );
2024 memcpy( buf3+256, buf3, 256 );
2025 call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
2026 call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
2027 if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
2029 call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
2030 call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
2033 report( "denoise dct :" );
2035 #define TEST_DECIMATE( decname, w, ac, thresh ) \
2036 if( qf_a.decname != qf_ref.decname ) \
2038 set_func_name( #decname ); \
2040 for( int i = 0; i < 100; i++ ) \
2042 static const int distrib[16] = {1,1,1,1,1,1,1,1,1,1,1,1,2,3,4};\
2043 static const int zerorate_lut[4] = {3,7,15,31};\
2044 int zero_rate = zerorate_lut[i&3];\
2045 for( int idx = 0; idx < w*w; idx++ ) \
2047 int sign = (rand()&1) ? -1 : 1; \
2048 int abs_level = distrib[rand()&15]; \
2049 if( abs_level == 4 ) abs_level = rand()&0x3fff; \
2050 int zero = !(rand()&zero_rate); \
2051 dct1[idx] = zero * abs_level * sign; \
2055 int result_c = call_c( qf_c.decname, dct1 ); \
2056 int result_a = call_a( qf_a.decname, dct1 ); \
2057 if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
2060 fprintf( stderr, #decname ": [FAILED]\n" ); \
2066 ok = 1; used_asm = 0;
2067 TEST_DECIMATE( decimate_score64, 8, 0, 6 );
2068 TEST_DECIMATE( decimate_score16, 4, 0, 6 );
2069 TEST_DECIMATE( decimate_score15, 4, 1, 7 );
2070 report( "decimate_score :" );
2072 #define TEST_LAST( last, lastname, size, ac ) \
2073 if( qf_a.last != qf_ref.last ) \
2075 set_func_name( #lastname ); \
2077 for( int i = 0; i < 100; i++ ) \
2080 int max = rand() & (size-1); \
2081 memset( dct1, 0, size*sizeof(dctcoef) ); \
2082 for( int idx = ac; idx < max; idx++ ) \
2083 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2086 int result_c = call_c( qf_c.last, dct1+ac ); \
2087 int result_a = call_a( qf_a.last, dct1+ac ); \
2088 if( result_c != result_a ) \
2091 fprintf( stderr, #lastname ": [FAILED]\n" ); \
2097 ok = 1; used_asm = 0;
2098 TEST_LAST( coeff_last4 , coeff_last4, 4, 0 );
2099 TEST_LAST( coeff_last8 , coeff_last8, 8, 0 );
2100 TEST_LAST( coeff_last[ DCT_LUMA_AC], coeff_last15, 16, 1 );
2101 TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 16, 0 );
2102 TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 64, 0 );
2103 report( "coeff_last :" );
2105 #define TEST_LEVELRUN( lastname, name, size, ac ) \
2106 if( qf_a.lastname != qf_ref.lastname ) \
2108 set_func_name( #name ); \
2110 for( int i = 0; i < 100; i++ ) \
2112 x264_run_level_t runlevel_c, runlevel_a; \
2114 int max = rand() & (size-1); \
2115 memset( dct1, 0, size*sizeof(dctcoef) ); \
2116 memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
2117 memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
2118 for( int idx = ac; idx < max; idx++ ) \
2119 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2122 int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
2123 int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
2124 if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
2125 runlevel_c.mask != runlevel_a.mask || \
2126 memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c)) \
2129 fprintf( stderr, #name ": [FAILED]\n" ); \
2135 ok = 1; used_asm = 0;
2136 TEST_LEVELRUN( coeff_level_run4 , coeff_level_run4, 4, 0 );
2137 TEST_LEVELRUN( coeff_level_run8 , coeff_level_run8, 8, 0 );
2138 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_AC], coeff_level_run15, 16, 1 );
2139 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 16, 0 );
2140 report( "coeff_level_run :" );
2145 static int check_intra( int cpu_ref, int cpu_new )
2147 int ret = 0, ok = 1, used_asm = 0;
2148 ALIGNED_ARRAY_32( pixel, edge,[36] );
2149 ALIGNED_ARRAY_32( pixel, edge2,[36] );
2150 ALIGNED_ARRAY_32( pixel, fdec,[FDEC_STRIDE*20] );
2153 x264_predict_t predict_16x16[4+3];
2154 x264_predict_t predict_8x8c[4+3];
2155 x264_predict_t predict_8x16c[4+3];
2156 x264_predict8x8_t predict_8x8[9+3];
2157 x264_predict_t predict_4x4[9+3];
2158 x264_predict_8x8_filter_t predict_8x8_filter;
2159 } ip_c, ip_ref, ip_a;
2161 x264_predict_16x16_init( 0, ip_c.predict_16x16 );
2162 x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
2163 x264_predict_8x16c_init( 0, ip_c.predict_8x16c );
2164 x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
2165 x264_predict_4x4_init( 0, ip_c.predict_4x4 );
2167 x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
2168 x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
2169 x264_predict_8x16c_init( cpu_ref, ip_ref.predict_8x16c );
2170 x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
2171 x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
2173 x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
2174 x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
2175 x264_predict_8x16c_init( cpu_new, ip_a.predict_8x16c );
2176 x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
2177 x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
2179 memcpy( fdec, pbuf1, 32*20 * sizeof(pixel) );\
2181 ip_c.predict_8x8_filter( fdec+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
2183 #define INTRA_TEST( name, dir, w, h, align, bench, ... )\
2184 if( ip_a.name[dir] != ip_ref.name[dir] )\
2186 set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
2188 memcpy( pbuf3, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2189 memcpy( pbuf4, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2190 for( int a = 0; a < (do_bench ? 64/sizeof(pixel) : 1); a += align )\
2192 call_c##bench( ip_c.name[dir], pbuf3+48+a, ##__VA_ARGS__ );\
2193 call_a##bench( ip_a.name[dir], pbuf4+48+a, ##__VA_ARGS__ );\
2194 if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*20 * sizeof(pixel) ) )\
2196 fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\
2198 for( int k = -1; k < 16; k++ )\
2199 printf( "%2x ", edge[16+k] );\
2201 for( int j = 0; j < h; j++ )\
2203 printf( "%2x ", edge[14-j] );\
2204 for( int k = 0; k < w; k++ )\
2205 printf( "%2x ", pbuf4[48+k+j*FDEC_STRIDE] );\
2209 for( int j = 0; j < h; j++ )\
2212 for( int k = 0; k < w; k++ )\
2213 printf( "%2x ", pbuf3[48+k+j*FDEC_STRIDE] );\
2221 for( int i = 0; i < 12; i++ )
2222 INTRA_TEST( predict_4x4, i, 4, 4, 4, );
2223 for( int i = 0; i < 7; i++ )
2224 INTRA_TEST( predict_8x8c, i, 8, 8, 16, );
2225 for( int i = 0; i < 7; i++ )
2226 INTRA_TEST( predict_8x16c, i, 8, 16, 16, );
2227 for( int i = 0; i < 7; i++ )
2228 INTRA_TEST( predict_16x16, i, 16, 16, 16, );
2229 for( int i = 0; i < 12; i++ )
2230 INTRA_TEST( predict_8x8, i, 8, 8, 8, , edge );
2232 set_func_name("intra_predict_8x8_filter");
2233 if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
2236 for( int i = 0; i < 32; i++ )
2238 if( !(i&7) || ((i&MB_TOPRIGHT) && !(i&MB_TOP)) )
2240 int neighbor = (i&24)>>1;
2241 memset( edge, 0, 36*sizeof(pixel) );
2242 memset( edge2, 0, 36*sizeof(pixel) );
2243 call_c( ip_c.predict_8x8_filter, pbuf1+48, edge, neighbor, i&7 );
2244 call_a( ip_a.predict_8x8_filter, pbuf1+48, edge2, neighbor, i&7 );
2245 if( !(neighbor&MB_TOPLEFT) )
2246 edge[15] = edge2[15] = 0;
2247 if( memcmp( edge+7, edge2+7, (i&MB_TOPRIGHT ? 26 : i&MB_TOP ? 17 : 8) * sizeof(pixel) ) )
2249 fprintf( stderr, "predict_8x8_filter : [FAILED] %d %d\n", (i&24)>>1, i&7);
2255 #define EXTREMAL_PLANE( w, h ) \
2258 for( int j = 0; j < 7; j++ ) \
2259 max[j] = test ? rand()&PIXEL_MAX : PIXEL_MAX; \
2260 fdec[48-1-FDEC_STRIDE] = (i&1)*max[0]; \
2261 for( int j = 0; j < w/2; j++ ) \
2262 fdec[48+j-FDEC_STRIDE] = (!!(i&2))*max[1]; \
2263 for( int j = w/2; j < w-1; j++ ) \
2264 fdec[48+j-FDEC_STRIDE] = (!!(i&4))*max[2]; \
2265 fdec[48+(w-1)-FDEC_STRIDE] = (!!(i&8))*max[3]; \
2266 for( int j = 0; j < h/2; j++ ) \
2267 fdec[48+j*FDEC_STRIDE-1] = (!!(i&16))*max[4]; \
2268 for( int j = h/2; j < h-1; j++ ) \
2269 fdec[48+j*FDEC_STRIDE-1] = (!!(i&32))*max[5]; \
2270 fdec[48+(h-1)*FDEC_STRIDE-1] = (!!(i&64))*max[6]; \
2272 /* Extremal test case for planar prediction. */
2273 for( int test = 0; test < 100 && ok; test++ )
2274 for( int i = 0; i < 128 && ok; i++ )
2276 EXTREMAL_PLANE( 8, 8 );
2277 INTRA_TEST( predict_8x8c, I_PRED_CHROMA_P, 8, 8, 64, 1 );
2278 EXTREMAL_PLANE( 8, 16 );
2279 INTRA_TEST( predict_8x16c, I_PRED_CHROMA_P, 8, 16, 64, 1 );
2280 EXTREMAL_PLANE( 16, 16 );
2281 INTRA_TEST( predict_16x16, I_PRED_16x16_P, 16, 16, 64, 1 );
2283 report( "intra pred :" );
2287 #define DECL_CABAC(cpu) \
2288 static void run_cabac_decision_##cpu( x264_t *h, uint8_t *dst )\
2291 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2292 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2293 for( int i = 0; i < 0x1000; i++ )\
2294 x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
2296 static void run_cabac_bypass_##cpu( x264_t *h, uint8_t *dst )\
2299 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2300 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2301 for( int i = 0; i < 0x1000; i++ )\
2302 x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
2304 static void run_cabac_terminal_##cpu( x264_t *h, uint8_t *dst )\
2307 x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2308 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2309 for( int i = 0; i < 0x1000; i++ )\
2310 x264_cabac_encode_terminal_##cpu( &cb );\
2316 #define run_cabac_decision_asm run_cabac_decision_c
2317 #define run_cabac_bypass_asm run_cabac_bypass_c
2318 #define run_cabac_terminal_asm run_cabac_terminal_c
2321 extern const uint8_t x264_count_cat_m1[14];
2322 void x264_cabac_block_residual_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2323 void x264_cabac_block_residual_8x8_rd_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2324 void x264_cabac_block_residual_rd_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2326 static int check_cabac( int cpu_ref, int cpu_new )
2328 int ret = 0, ok = 1, used_asm = 0;
2330 h.sps->i_chroma_format_idc = 3;
2332 x264_bitstream_function_t bs_ref;
2333 x264_bitstream_function_t bs_a;
2334 x264_bitstream_init( cpu_ref, &bs_ref );
2335 x264_bitstream_init( cpu_new, &bs_a );
2336 x264_quant_init( &h, cpu_new, &h.quantf );
2337 h.quantf.coeff_last[DCT_CHROMA_DC] = h.quantf.coeff_last4;
2339 #define CABAC_RESIDUAL(name, start, end, rd)\
2341 if( bs_a.name##_internal && (bs_a.name##_internal != bs_ref.name##_internal || (cpu_new&X264_CPU_SSE2_IS_SLOW)) )\
2344 set_func_name( #name );\
2345 for( int i = 0; i < 2; i++ )\
2347 for( intptr_t ctx_block_cat = start; ctx_block_cat <= end; ctx_block_cat++ )\
2349 for( int j = 0; j < 256; j++ )\
2351 ALIGNED_ARRAY_N( dctcoef, dct, [2],[64] );\
2352 uint8_t bitstream[2][1<<16];\
2353 static const uint8_t ctx_ac[14] = {0,1,0,0,1,0,0,1,0,0,0,1,0,0};\
2354 int ac = ctx_ac[ctx_block_cat];\
2358 for( int k = 0; k <= x264_count_cat_m1[ctx_block_cat]; k++ )\
2360 /* Very rough distribution that covers possible inputs */\
2362 int coef = !(rnd&3);\
2363 coef += !(rnd& 15) * (rand()&0x0006);\
2364 coef += !(rnd& 63) * (rand()&0x0008);\
2365 coef += !(rnd& 255) * (rand()&0x00F0);\
2366 coef += !(rnd&1023) * (rand()&0x7F00);\
2367 nz |= dct[0][ac+k] = dct[1][ac+k] = coef * ((rand()&1) ? 1 : -1);\
2370 h.mb.b_interlaced = i;\
2371 x264_cabac_t cb[2];\
2372 x264_cabac_context_init( &h, &cb[0], SLICE_TYPE_P, 26, 0 );\
2373 x264_cabac_context_init( &h, &cb[1], SLICE_TYPE_P, 26, 0 );\
2374 x264_cabac_encode_init( &cb[0], bitstream[0], bitstream[0]+0xfff0 );\
2375 x264_cabac_encode_init( &cb[1], bitstream[1], bitstream[1]+0xfff0 );\
2376 cb[0].f8_bits_encoded = 0;\
2377 cb[1].f8_bits_encoded = 0;\
2378 if( !rd ) memcpy( bitstream[1], bitstream[0], 0x400 );\
2379 call_c1( x264_##name##_c, &h, &cb[0], ctx_block_cat, dct[0]+ac );\
2380 call_a1( bs_a.name##_internal, dct[1]+ac, i, ctx_block_cat, &cb[1] );\
2381 ok = cb[0].f8_bits_encoded == cb[1].f8_bits_encoded && !memcmp(cb[0].state, cb[1].state, 1024);\
2382 if( !rd ) ok |= !memcmp( bitstream[1], bitstream[0], 0x400 ) && !memcmp( &cb[1], &cb[0], offsetof(x264_cabac_t, p_start) );\
2385 fprintf( stderr, #name " : [FAILED] ctx_block_cat %d", (int)ctx_block_cat );\
2386 if( rd && cb[0].f8_bits_encoded != cb[1].f8_bits_encoded )\
2387 fprintf( stderr, " (%d != %d)", cb[0].f8_bits_encoded, cb[1].f8_bits_encoded );\
2388 fprintf( stderr, "\n");\
2393 call_c2( x264_##name##_c, &h, &cb[0], ctx_block_cat, dct[0]+ac );\
2394 call_a2( bs_a.name##_internal, dct[1]+ac, i, ctx_block_cat, &cb[1] );\
2403 CABAC_RESIDUAL( cabac_block_residual, 0, DCT_LUMA_8x8, 0 )
2404 report( "cabac residual:" );
2406 ok = 1; used_asm = 0;
2407 CABAC_RESIDUAL( cabac_block_residual_rd, 0, DCT_LUMA_8x8-1, 1 )
2408 CABAC_RESIDUAL( cabac_block_residual_8x8_rd, DCT_LUMA_8x8, DCT_LUMA_8x8, 1 )
2409 report( "cabac residual rd:" );
2411 if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
2413 ok = 1; used_asm = 0;
2414 x264_cabac_init( &h );
2416 set_func_name( "cabac_encode_decision" );
2417 memcpy( buf4, buf3, 0x1000 );
2418 call_c( run_cabac_decision_c, &h, buf3 );
2419 call_a( run_cabac_decision_asm, &h, buf4 );
2420 ok = !memcmp( buf3, buf4, 0x1000 );
2421 report( "cabac decision:" );
2423 set_func_name( "cabac_encode_bypass" );
2424 memcpy( buf4, buf3, 0x1000 );
2425 call_c( run_cabac_bypass_c, &h, buf3 );
2426 call_a( run_cabac_bypass_asm, &h, buf4 );
2427 ok = !memcmp( buf3, buf4, 0x1000 );
2428 report( "cabac bypass:" );
2430 set_func_name( "cabac_encode_terminal" );
2431 memcpy( buf4, buf3, 0x1000 );
2432 call_c( run_cabac_terminal_c, &h, buf3 );
2433 call_a( run_cabac_terminal_asm, &h, buf4 );
2434 ok = !memcmp( buf3, buf4, 0x1000 );
2435 report( "cabac terminal:" );
2440 static int check_bitstream( int cpu_ref, int cpu_new )
2442 x264_bitstream_function_t bs_c;
2443 x264_bitstream_function_t bs_ref;
2444 x264_bitstream_function_t bs_a;
2446 int ret = 0, ok = 1, used_asm = 0;
2448 x264_bitstream_init( 0, &bs_c );
2449 x264_bitstream_init( cpu_ref, &bs_ref );
2450 x264_bitstream_init( cpu_new, &bs_a );
2451 if( bs_a.nal_escape != bs_ref.nal_escape )
2454 uint8_t *input = malloc(size+100);
2455 uint8_t *output1 = malloc(size*2);
2456 uint8_t *output2 = malloc(size*2);
2458 set_func_name( "nal_escape" );
2459 for( int i = 0; i < 100; i++ )
2461 /* Test corner-case sizes */
2462 int test_size = i < 10 ? i+1 : rand() & 0x3fff;
2463 /* Test 8 different probability distributions of zeros */
2464 for( int j = 0; j < test_size+32; j++ )
2465 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
2466 uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
2467 uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
2468 int size_c = end_c-output1;
2469 int size_a = end_a-output2;
2470 if( size_c != size_a || memcmp( output1, output2, size_c ) )
2472 fprintf( stderr, "nal_escape : [FAILED] %d %d\n", size_c, size_a );
2477 for( int j = 0; j < size+32; j++ )
2479 call_c2( bs_c.nal_escape, output1, input, input+size );
2480 call_a2( bs_a.nal_escape, output2, input, input+size );
2485 report( "nal escape:" );
2490 static int check_all_funcs( int cpu_ref, int cpu_new )
2492 return check_pixel( cpu_ref, cpu_new )
2493 + check_dct( cpu_ref, cpu_new )
2494 + check_mc( cpu_ref, cpu_new )
2495 + check_intra( cpu_ref, cpu_new )
2496 + check_deblock( cpu_ref, cpu_new )
2497 + check_quant( cpu_ref, cpu_new )
2498 + check_cabac( cpu_ref, cpu_new )
2499 + check_bitstream( cpu_ref, cpu_new );
2502 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
2504 *cpu_ref = *cpu_new;
2506 #if BROKEN_STACK_ALIGNMENT
2507 *cpu_new |= X264_CPU_STACK_MOD4;
2509 if( *cpu_new & X264_CPU_SSE2_IS_FAST )
2510 *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
2512 fprintf( stderr, "x264: %s\n", name );
2513 return check_all_funcs( *cpu_ref, *cpu_new );
2516 static int check_all_flags( void )
2519 int cpu0 = 0, cpu1 = 0;
2521 if( x264_cpu_detect() & X264_CPU_MMX2 )
2523 ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMX2, "MMX" );
2524 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
2525 cpu1 &= ~X264_CPU_CACHELINE_64;
2527 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
2528 cpu1 &= ~X264_CPU_CACHELINE_32;
2530 if( x264_cpu_detect() & X264_CPU_LZCNT )
2532 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
2533 cpu1 &= ~X264_CPU_LZCNT;
2535 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
2536 cpu1 &= ~X264_CPU_SLOW_CTZ;
2538 if( x264_cpu_detect() & X264_CPU_SSE )
2539 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE, "SSE" );
2540 if( x264_cpu_detect() & X264_CPU_SSE2 )
2542 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
2543 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
2544 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
2545 cpu1 &= ~X264_CPU_CACHELINE_64;
2546 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_SHUFFLE, "SSE2 SlowShuffle" );
2547 cpu1 &= ~X264_CPU_SLOW_SHUFFLE;
2548 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
2549 cpu1 &= ~X264_CPU_SLOW_CTZ;
2551 if( x264_cpu_detect() & X264_CPU_LZCNT )
2553 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
2554 cpu1 &= ~X264_CPU_LZCNT;
2556 if( x264_cpu_detect() & X264_CPU_SSE3 )
2558 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
2559 cpu1 &= ~X264_CPU_CACHELINE_64;
2561 if( x264_cpu_detect() & X264_CPU_SSSE3 )
2563 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
2564 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
2565 cpu1 &= ~X264_CPU_CACHELINE_64;
2566 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_SHUFFLE, "SSSE3 SlowShuffle" );
2567 cpu1 &= ~X264_CPU_SLOW_SHUFFLE;
2568 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
2569 cpu1 &= ~X264_CPU_SLOW_CTZ;
2570 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
2571 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64 SlowAtom" );
2572 cpu1 &= ~X264_CPU_CACHELINE_64;
2573 cpu1 &= ~X264_CPU_SLOW_ATOM;
2575 if( x264_cpu_detect() & X264_CPU_SSE4 )
2576 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
2577 if( x264_cpu_detect() & X264_CPU_AVX )
2578 ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX, "AVX" );
2579 if( x264_cpu_detect() & X264_CPU_XOP )
2580 ret |= add_flags( &cpu0, &cpu1, X264_CPU_XOP, "XOP" );
2581 if( x264_cpu_detect() & X264_CPU_FMA4 )
2583 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA4, "FMA4" );
2584 cpu1 &= ~X264_CPU_FMA4;
2586 if( x264_cpu_detect() & X264_CPU_BMI1 )
2588 ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI1, "BMI1" );
2589 cpu1 &= ~X264_CPU_BMI1;
2591 if( x264_cpu_detect() & X264_CPU_AVX2 )
2593 ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX2, "AVX2" );
2594 if( x264_cpu_detect() & X264_CPU_LZCNT )
2596 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "AVX2_LZCNT" );
2597 cpu1 &= ~X264_CPU_LZCNT;
2600 if( x264_cpu_detect() & X264_CPU_BMI2 )
2602 ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI1|X264_CPU_BMI2, "BMI2" );
2603 cpu1 &= ~(X264_CPU_BMI1|X264_CPU_BMI2);
2605 if( x264_cpu_detect() & X264_CPU_FMA3 )
2607 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA3, "FMA3" );
2608 cpu1 &= ~X264_CPU_FMA3;
2611 if( x264_cpu_detect() & X264_CPU_ALTIVEC )
2613 fprintf( stderr, "x264: ALTIVEC against C\n" );
2614 ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
2617 if( x264_cpu_detect() & X264_CPU_ARMV6 )
2618 ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
2619 if( x264_cpu_detect() & X264_CPU_NEON )
2620 ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
2621 if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC )
2622 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
2627 int main(int argc, char *argv[])
2631 if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
2633 #if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM
2634 fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
2638 if( argv[1][7] == '=' )
2640 bench_pattern = argv[1]+8;
2641 bench_pattern_len = strlen(bench_pattern);
2647 int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
2648 fprintf( stderr, "x264: using random seed %u\n", seed );
2651 buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 32*BENCH_ALIGNS );
2652 pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 32*BENCH_ALIGNS );
2653 if( !buf1 || !pbuf1 )
2655 fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
2658 #define INIT_POINTER_OFFSETS\
2659 buf2 = buf1 + 0xf00;\
2660 buf3 = buf2 + 0xf00;\
2661 buf4 = buf3 + 0x1000*sizeof(pixel);\
2662 pbuf2 = pbuf1 + 0xf00;\
2663 pbuf3 = (pixel*)buf3;\
2664 pbuf4 = (pixel*)buf4;
2665 INIT_POINTER_OFFSETS;
2666 for( int i = 0; i < 0x1e00; i++ )
2668 buf1[i] = rand() & 0xFF;
2669 pbuf1[i] = rand() & PIXEL_MAX;
2671 memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) );
2673 /* 32-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
2675 for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
2677 INIT_POINTER_OFFSETS;
2678 ret |= x264_stack_pagealign( check_all_flags, i*32 );
2682 fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
2685 ret = x264_stack_pagealign( check_all_flags, 0 );
2689 fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
2692 fprintf( stderr, "x264: All tests passed Yeah :)\n" );