1 /*****************************************************************************
2 * checkasm.c: assembly check tool
3 *****************************************************************************
4 * Copyright (C) 2003-2010 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 *****************************************************************************/
33 #include "common/common.h"
34 #include "common/cpu.h"
36 // GCC doesn't align stack variables on ARM, so use .bss
39 #define ALIGNED_16( var ) DECLARE_ALIGNED( static var, 16 )
42 /* buf1, buf2: initialised to random data and shouldn't write into them */
44 /* buf3, buf4: used to store output */
46 /* pbuf1, pbuf2: initialised to random pixel data and shouldn't write into them. */
48 /* pbuf3, pbuf4: point to buf3, buf4, just for type convenience */
53 #define report( name ) { \
54 if( used_asm && !quiet ) \
55 fprintf( stderr, " - %-21s [%s]\n", name, ok ? "OK" : "FAILED" ); \
59 #define BENCH_RUNS 100 // tradeoff between accuracy and speed
60 #define BENCH_ALIGNS 16 // number of stack+heap data alignments (another accuracy vs speed tradeoff)
61 #define MAX_FUNCS 1000 // just has to be big enough to hold all the existing functions
62 #define MAX_CPUS 10 // number of different combinations of cpu flags
65 void *pointer; // just for detecting duplicates
73 bench_t vers[MAX_CPUS];
77 int bench_pattern_len = 0;
78 const char *bench_pattern = "";
80 static bench_func_t benchs[MAX_FUNCS];
82 static const char *pixel_names[10] = { "16x16", "16x8", "8x16", "8x8", "8x4", "4x8", "4x4", "4x2", "2x4", "2x2" };
83 static const char *intra_predict_16x16_names[7] = { "v", "h", "dc", "p", "dcl", "dct", "dc8" };
84 static const char *intra_predict_8x8c_names[7] = { "dc", "h", "v", "p", "dcl", "dct", "dc8" };
85 static const char *intra_predict_4x4_names[12] = { "v", "h", "dc", "ddl", "ddr", "vr", "hd", "vl", "hu", "dcl", "dct", "dc8" };
86 static const char **intra_predict_8x8_names = intra_predict_4x4_names;
88 #define set_func_name(...) snprintf( func_name, sizeof(func_name), __VA_ARGS__ )
90 static inline uint32_t read_time(void)
93 #if defined(__GNUC__) && (ARCH_X86 || ARCH_X86_64)
94 asm volatile( "rdtsc" :"=a"(a) ::"edx" );
96 asm volatile( "mftb %0" : "=r" (a) );
97 #elif ARCH_ARM // ARMv7 only
98 asm volatile( "mrc p15, 0, %0, c9, c13, 0" : "=r"(a) );
103 static bench_t* get_bench( const char *name, int cpu )
106 for( i = 0; benchs[i].name && strcmp(name, benchs[i].name); i++ )
107 assert( i < MAX_FUNCS );
108 if( !benchs[i].name )
109 benchs[i].name = strdup( name );
111 return &benchs[i].vers[0];
112 for( j = 1; benchs[i].vers[j].cpu && benchs[i].vers[j].cpu != cpu; j++ )
113 assert( j < MAX_CPUS );
114 benchs[i].vers[j].cpu = cpu;
115 return &benchs[i].vers[j];
118 static int cmp_nop( const void *a, const void *b )
120 return *(uint16_t*)a - *(uint16_t*)b;
123 static int cmp_bench( const void *a, const void *b )
125 // asciibetical sort except preserving numbers
126 const char *sa = ((bench_func_t*)a)->name;
127 const char *sb = ((bench_func_t*)b)->name;
132 if( isdigit( *sa ) && isdigit( *sb ) && isdigit( sa[1] ) != isdigit( sb[1] ) )
133 return isdigit( sa[1] ) - isdigit( sb[1] );
139 static void print_bench(void)
141 uint16_t nops[10000] = {0};
142 int nfuncs, nop_time=0;
144 for( int i = 0; i < 10000; i++ )
147 nops[i] = read_time() - t;
149 qsort( nops, 10000, sizeof(uint16_t), cmp_nop );
150 for( int i = 500; i < 9500; i++ )
153 printf( "nop: %d\n", nop_time );
155 for( nfuncs = 0; nfuncs < MAX_FUNCS && benchs[nfuncs].name; nfuncs++ );
156 qsort( benchs, nfuncs, sizeof(bench_func_t), cmp_bench );
157 for( int i = 0; i < nfuncs; i++ )
158 for( int j = 0; j < MAX_CPUS && (!j || benchs[i].vers[j].cpu); j++ )
161 bench_t *b = &benchs[i].vers[j];
164 for( k = 0; k < j && benchs[i].vers[k].pointer != b->pointer; k++ );
167 printf( "%s_%s%s: %"PRId64"\n", benchs[i].name,
168 b->cpu&X264_CPU_SSE4 ? "sse4" :
169 b->cpu&X264_CPU_SHUFFLE_IS_FAST ? "fastshuffle" :
170 b->cpu&X264_CPU_SSSE3 ? "ssse3" :
171 b->cpu&X264_CPU_SSE3 ? "sse3" :
172 /* print sse2slow only if there's also a sse2fast version of the same func */
173 b->cpu&X264_CPU_SSE2_IS_SLOW && j<MAX_CPUS && b[1].cpu&X264_CPU_SSE2_IS_FAST && !(b[1].cpu&X264_CPU_SSE3) ? "sse2slow" :
174 b->cpu&X264_CPU_SSE2 ? "sse2" :
175 b->cpu&X264_CPU_MMX ? "mmx" :
176 b->cpu&X264_CPU_ALTIVEC ? "altivec" :
177 b->cpu&X264_CPU_NEON ? "neon" :
178 b->cpu&X264_CPU_ARMV6 ? "armv6" : "c",
179 b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
180 b->cpu&X264_CPU_CACHELINE_64 ? "_c64" :
181 b->cpu&X264_CPU_SSE_MISALIGN ? "_misalign" :
182 b->cpu&X264_CPU_LZCNT ? "_lzcnt" :
183 b->cpu&X264_CPU_FAST_NEON_MRC ? "_fast_mrc" :
184 b->cpu&X264_CPU_SLOW_CTZ ? "_slow_ctz" :
185 b->cpu&X264_CPU_SLOW_ATOM ? "_slow_atom" : "",
186 ((int64_t)10*b->cycles/b->den - nop_time)/4 );
190 #if ARCH_X86 || ARCH_X86_64
191 int x264_stack_pagealign( int (*func)(), int align );
193 #define x264_stack_pagealign( func, align ) func()
196 #define call_c1(func,...) func(__VA_ARGS__)
198 #if ARCH_X86 || defined(_WIN64)
199 /* detect when callee-saved regs aren't saved.
200 * needs an explicit asm check because it only sometimes crashes in normal use. */
201 intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... );
202 #define call_a1(func,...) x264_checkasm_call((intptr_t(*)())func, &ok, __VA_ARGS__)
204 #define call_a1 call_c1
207 #define call_bench(func,cpu,...)\
208 if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
212 call_a1(func, __VA_ARGS__);\
213 for( int ti = 0; ti < (cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
215 uint32_t t = read_time();\
220 t = read_time() - t;\
221 if( t*tcount <= tsum*4 && ti > 0 )\
227 bench_t *b = get_bench( func_name, cpu );\
233 /* for most functions, run benchmark and correctness test at the same time.
234 * for those that modify their inputs, run the above macros separately */
235 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
236 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
237 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
238 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
241 static int check_pixel( int cpu_ref, int cpu_new )
243 x264_pixel_function_t pixel_c;
244 x264_pixel_function_t pixel_ref;
245 x264_pixel_function_t pixel_asm;
246 x264_predict8x8_t predict_8x8[9+3];
247 x264_predict_8x8_filter_t predict_8x8_filter;
248 ALIGNED_16( pixel edge[33] );
249 uint16_t cost_mv[32];
250 int ret = 0, ok, used_asm;
252 x264_pixel_init( 0, &pixel_c );
253 x264_pixel_init( cpu_ref, &pixel_ref );
254 x264_pixel_init( cpu_new, &pixel_asm );
255 x264_predict_8x8_init( 0, predict_8x8, &predict_8x8_filter );
256 predict_8x8_filter( pbuf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
259 for( int i = 0; i < 256; i++ )
264 pbuf4[i] = -(z&1) & PIXEL_MAX;
265 pbuf3[i] = ~pbuf4[i] & PIXEL_MAX;
267 // random pattern made of maxed pixel differences, in case an intermediate value overflows
268 for( int i = 256; i < 0x1000; i++ )
270 pbuf4[i] = -(pbuf1[i&~0x88]&1) & PIXEL_MAX;
271 pbuf3[i] = ~(pbuf4[i]) & PIXEL_MAX;
274 #define TEST_PIXEL( name, align ) \
275 ok = 1, used_asm = 0; \
276 for( int i = 0; i < 7; i++ ) \
278 int res_c, res_asm; \
279 if( pixel_asm.name[i] != pixel_ref.name[i] ) \
281 set_func_name( "%s_%s", #name, pixel_names[i] ); \
283 for( int j = 0; j < 64; j++ ) \
285 res_c = call_c( pixel_c.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \
286 res_asm = call_a( pixel_asm.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \
287 if( res_c != res_asm ) \
290 fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
294 for( int j = 0; j < 0x1000 && ok; j += 256 ) \
296 res_c = pixel_c .name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
297 res_asm = pixel_asm.name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
298 if( res_c != res_asm ) \
301 fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
306 report( "pixel " #name " :" );
308 TEST_PIXEL( sad, 0 );
309 TEST_PIXEL( sad_aligned, 1 );
310 TEST_PIXEL( ssd, 1 );
311 TEST_PIXEL( satd, 0 );
312 TEST_PIXEL( sa8d, 1 );
314 #define TEST_PIXEL_X( N ) \
315 ok = 1; used_asm = 0; \
316 for( int i = 0; i < 7; i++ ) \
318 int res_c[4]={0}, res_asm[4]={0}; \
319 if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
321 set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
323 for( int j = 0; j < 64; j++ ) \
325 pixel *pix2 = pbuf2+j; \
326 res_c[0] = pixel_c.sad[i]( pbuf1, 16, pix2, 64 ); \
327 res_c[1] = pixel_c.sad[i]( pbuf1, 16, pix2+6, 64 ); \
328 res_c[2] = pixel_c.sad[i]( pbuf1, 16, pix2+1, 64 ); \
331 res_c[3] = pixel_c.sad[i]( pbuf1, 16, pix2+10, 64 ); \
332 call_a( pixel_asm.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
335 call_a( pixel_asm.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
336 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
339 fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
340 i, res_c[0], res_c[1], res_c[2], res_c[3], \
341 res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
344 call_c2( pixel_c.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \
346 call_c2( pixel_c.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \
350 report( "pixel sad_x"#N" :" );
355 #define TEST_PIXEL_VAR( i ) \
356 if( pixel_asm.var[i] != pixel_ref.var[i] ) \
358 set_func_name( "%s_%s", "var", pixel_names[i] ); \
360 /* abi-check wrapper can't return uint64_t, so separate it from return value check */ \
361 call_c1( pixel_c.var[i], pbuf1, 16 ); \
362 call_a1( pixel_asm.var[i], pbuf1, 16 ); \
363 uint64_t res_c = pixel_c.var[i]( pbuf1, 16 ); \
364 uint64_t res_asm = pixel_asm.var[i]( pbuf1, 16 ); \
365 if( res_c != res_asm ) \
368 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) ); \
370 call_c2( pixel_c.var[i], pbuf1, 16 ); \
371 call_a2( pixel_asm.var[i], pbuf1, 16 ); \
374 ok = 1; used_asm = 0;
375 TEST_PIXEL_VAR( PIXEL_16x16 );
376 TEST_PIXEL_VAR( PIXEL_8x8 );
377 report( "pixel var :" );
379 ok = 1; used_asm = 0;
380 if( pixel_asm.var2_8x8 != pixel_ref.var2_8x8 )
382 int res_c, res_asm, ssd_c, ssd_asm;
383 set_func_name( "var2_8x8" );
385 res_c = call_c( pixel_c.var2_8x8, pbuf1, 16, pbuf2, 16, &ssd_c );
386 res_asm = call_a( pixel_asm.var2_8x8, pbuf1, 16, pbuf2, 16, &ssd_asm );
387 if( res_c != res_asm || ssd_c != ssd_asm )
390 fprintf( stderr, "var2_8x8: %d != %d or %d != %d [FAILED]\n", res_c, res_asm, ssd_c, ssd_asm );
394 report( "pixel var2 :" );
396 ok = 1; used_asm = 0;
397 for( int i = 0; i < 4; i++ )
398 if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
400 set_func_name( "hadamard_ac_%s", pixel_names[i] );
402 for( int j = 0; j < 32; j++ )
404 pixel *pix = (j&16 ? pbuf1 : pbuf3) + (j&15)*256;
405 call_c1( pixel_c.hadamard_ac[i], pbuf1, 16 );
406 call_a1( pixel_asm.hadamard_ac[i], pbuf1, 16 );
407 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
408 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
412 fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
416 call_c2( pixel_c.hadamard_ac[i], pbuf1, 16 );
417 call_a2( pixel_asm.hadamard_ac[i], pbuf1, 16 );
419 report( "pixel hadamard_ac :" );
421 #define TEST_INTRA_MBCMP( name, pred, satd, i8x8, ... ) \
422 if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
424 int res_c[3], res_asm[3]; \
425 set_func_name( #name ); \
427 call_c( pixel_c.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_c ); \
428 call_a( pixel_asm.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_asm ); \
429 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
432 fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
433 res_c[0], res_c[1], res_c[2], \
434 res_asm[0], res_asm[1], res_asm[2] ); \
438 ok = 1; used_asm = 0;
439 TEST_INTRA_MBCMP( intra_satd_x3_16x16, predict_16x16, satd[PIXEL_16x16], 0 );
440 TEST_INTRA_MBCMP( intra_satd_x3_8x8c , predict_8x8c , satd[PIXEL_8x8] , 0 );
441 TEST_INTRA_MBCMP( intra_satd_x3_4x4 , predict_4x4 , satd[PIXEL_4x4] , 0 );
442 TEST_INTRA_MBCMP( intra_sa8d_x3_8x8 , predict_8x8 , sa8d[PIXEL_8x8] , 1, edge );
443 report( "intra satd_x3 :" );
444 TEST_INTRA_MBCMP( intra_sad_x3_16x16 , predict_16x16, sad [PIXEL_16x16], 0 );
445 TEST_INTRA_MBCMP( intra_sad_x3_8x8c , predict_8x8c , sad [PIXEL_8x8] , 0 );
446 TEST_INTRA_MBCMP( intra_sad_x3_8x8 , predict_8x8 , sad [PIXEL_8x8] , 1, edge );
447 TEST_INTRA_MBCMP( intra_sad_x3_4x4 , predict_4x4 , sad [PIXEL_4x4] , 0 );
448 report( "intra sad_x3 :" );
450 ok = 1; used_asm = 0;
451 if( pixel_asm.ssd_nv12_core != pixel_ref.ssd_nv12_core )
454 set_func_name( "ssd_nv12" );
455 uint64_t res_u_c, res_v_c, res_u_a, res_v_a;
456 pixel_c.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c );
457 pixel_asm.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a );
458 if( res_u_c != res_u_a || res_v_c != res_v_a )
461 fprintf( stderr, "ssd_nv12: %"PRIu64",%"PRIu64" != %"PRIu64",%"PRIu64"\n",
462 res_u_c, res_v_c, res_u_a, res_v_a );
464 call_c( pixel_c.ssd_nv12_core, pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c );
465 call_a( pixel_asm.ssd_nv12_core, pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a );
467 report( "ssd_nv12 :" );
469 if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
470 pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
473 ALIGNED_16( int sums[5][4] ) = {{0}};
476 res_c = x264_pixel_ssim_wxh( &pixel_c, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3 );
477 res_a = x264_pixel_ssim_wxh( &pixel_asm, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3 );
478 if( fabs( res_c - res_a ) > 1e-6 )
481 fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
483 set_func_name( "ssim_core" );
484 call_c2( pixel_c.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums );
485 call_a2( pixel_asm.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums );
486 set_func_name( "ssim_end" );
487 call_c2( pixel_c.ssim_end4, sums, sums, 4 );
488 call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
492 ok = 1; used_asm = 0;
493 for( int i = 0; i < 32; i++ )
495 for( int i = 0; i < 100 && ok; i++ )
496 if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
498 ALIGNED_16( uint16_t sums[72] );
499 ALIGNED_16( int dc[4] );
500 int16_t mvs_a[32], mvs_c[32];
502 int thresh = rand() & 0x3fff;
503 set_func_name( "esa_ads" );
504 for( int j = 0; j < 72; j++ )
505 sums[j] = rand() & 0x3fff;
506 for( int j = 0; j < 4; j++ )
507 dc[j] = rand() & 0x3fff;
509 mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
510 mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
511 if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
514 printf( "c%d: ", i&3 );
515 for( int j = 0; j < mvn_c; j++ )
516 printf( "%d ", mvs_c[j] );
517 printf( "\na%d: ", i&3 );
518 for( int j = 0; j < mvn_a; j++ )
519 printf( "%d ", mvs_a[j] );
523 report( "esa ads:" );
528 static int check_dct( int cpu_ref, int cpu_new )
530 x264_dct_function_t dct_c;
531 x264_dct_function_t dct_ref;
532 x264_dct_function_t dct_asm;
533 x264_quant_function_t qf;
534 int ret = 0, ok, used_asm, interlace;
535 ALIGNED_16( dctcoef dct1[16][16] );
536 ALIGNED_16( dctcoef dct2[16][16] );
537 ALIGNED_16( dctcoef dct4[16][16] );
538 ALIGNED_16( dctcoef dct8[4][64] );
539 ALIGNED_8( dctcoef dctdc[2][4] );
543 x264_dct_init( 0, &dct_c );
544 x264_dct_init( cpu_ref, &dct_ref);
545 x264_dct_init( cpu_new, &dct_asm );
547 memset( h, 0, sizeof(*h) );
548 h->pps = h->pps_array;
549 x264_param_default( &h->param );
550 h->chroma_qp_table = i_chroma_qp_table + 12;
551 h->param.analyse.i_luma_deadzone[0] = 0;
552 h->param.analyse.i_luma_deadzone[1] = 0;
553 h->param.analyse.b_transform_8x8 = 1;
554 for( int i = 0; i < 6; i++ )
555 h->pps->scaling_list[i] = x264_cqm_flat16;
557 x264_quant_init( h, 0, &qf );
559 #define TEST_DCT( name, t1, t2, size ) \
560 if( dct_asm.name != dct_ref.name ) \
562 set_func_name( #name ); \
564 call_c( dct_c.name, t1, pbuf1, pbuf2 ); \
565 call_a( dct_asm.name, t2, pbuf1, pbuf2 ); \
566 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
569 fprintf( stderr, #name " [FAILED]\n" ); \
572 ok = 1; used_asm = 0;
573 TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16 );
574 TEST_DCT( sub8x8_dct, dct1, dct2, 16*4 );
575 TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4 );
576 TEST_DCT( sub16x16_dct, dct1, dct2, 16*16 );
577 report( "sub_dct4 :" );
579 ok = 1; used_asm = 0;
580 TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64 );
581 TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*4 );
582 report( "sub_dct8 :" );
585 // fdct and idct are denormalized by different factors, so quant/dequant
586 // is needed to force the coefs into the right range.
587 dct_c.sub16x16_dct( dct4, pbuf1, pbuf2 );
588 dct_c.sub16x16_dct8( dct8, pbuf1, pbuf2 );
589 for( int i = 0; i < 16; i++ )
591 qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
592 qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
594 for( int i = 0; i < 4; i++ )
596 qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
597 qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
600 #define TEST_IDCT( name, src ) \
601 if( dct_asm.name != dct_ref.name ) \
603 set_func_name( #name ); \
605 memcpy( pbuf3, pbuf1, 32*32 * sizeof(pixel) ); \
606 memcpy( pbuf4, pbuf1, 32*32 * sizeof(pixel) ); \
607 memcpy( dct1, src, 256 * sizeof(dctcoef) ); \
608 memcpy( dct2, src, 256 * sizeof(dctcoef) ); \
609 call_c1( dct_c.name, pbuf3, (void*)dct1 ); \
610 call_a1( dct_asm.name, pbuf4, (void*)dct2 ); \
611 if( memcmp( pbuf3, pbuf4, 32*32 * sizeof(pixel) ) ) \
614 fprintf( stderr, #name " [FAILED]\n" ); \
616 call_c2( dct_c.name, pbuf3, (void*)dct1 ); \
617 call_a2( dct_asm.name, pbuf4, (void*)dct2 ); \
619 ok = 1; used_asm = 0;
620 TEST_IDCT( add4x4_idct, dct4 );
621 TEST_IDCT( add8x8_idct, dct4 );
622 TEST_IDCT( add8x8_idct_dc, dct4 );
623 TEST_IDCT( add16x16_idct, dct4 );
624 TEST_IDCT( add16x16_idct_dc, dct4 );
625 report( "add_idct4 :" );
627 ok = 1; used_asm = 0;
628 TEST_IDCT( add8x8_idct8, dct8 );
629 TEST_IDCT( add16x16_idct8, dct8 );
630 report( "add_idct8 :" );
633 #define TEST_DCTDC( name )\
634 ok = 1; used_asm = 0;\
635 if( dct_asm.name != dct_ref.name )\
637 set_func_name( #name );\
639 uint16_t *p = (uint16_t*)buf1;\
640 for( int i = 0; i < 16 && ok; i++ )\
642 for( int j = 0; j < 16; j++ )\
643 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? 4080 : -4080 /* max dc */\
644 : i<8 ? (*p++)&1 ? 4080 : -4080 /* max elements */\
645 : ((*p++)&0x1fff)-0x1000; /* general case */\
646 memcpy( dct2, dct1, 16 * sizeof(dctcoef) );\
647 call_c1( dct_c.name, dct1[0] );\
648 call_a1( dct_asm.name, dct2[0] );\
649 if( memcmp( dct1, dct2, 16 * sizeof(dctcoef) ) )\
652 call_c2( dct_c.name, dct1[0] );\
653 call_a2( dct_asm.name, dct2[0] );\
655 report( #name " :" );
657 TEST_DCTDC( dct4x4dc );
658 TEST_DCTDC( idct4x4dc );
661 x264_zigzag_function_t zigzag_c;
662 x264_zigzag_function_t zigzag_ref;
663 x264_zigzag_function_t zigzag_asm;
665 ALIGNED_16( dctcoef level1[64] );
666 ALIGNED_16( dctcoef level2[64] );
668 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
669 if( zigzag_asm.name != zigzag_ref.name ) \
671 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
673 memcpy(dct, buf1, size*sizeof(dctcoef)); \
674 call_c( zigzag_c.name, t1, dct ); \
675 call_a( zigzag_asm.name, t2, dct ); \
676 if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
679 fprintf( stderr, #name " [FAILED]\n" ); \
683 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
684 if( zigzag_asm.name != zigzag_ref.name ) \
687 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
689 memcpy( pbuf3, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
690 memcpy( pbuf4, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
691 nz_c = call_c1( zigzag_c.name, t1, pbuf2, pbuf3 ); \
692 nz_a = call_a1( zigzag_asm.name, t2, pbuf2, pbuf4 ); \
693 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE*sizeof(pixel) ) || nz_c != nz_a ) \
696 fprintf( stderr, #name " [FAILED]\n" ); \
698 call_c2( zigzag_c.name, t1, pbuf2, pbuf3 ); \
699 call_a2( zigzag_asm.name, t2, pbuf2, pbuf4 ); \
702 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
703 if( zigzag_asm.name != zigzag_ref.name ) \
706 dctcoef dc_a, dc_c; \
707 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
709 for( int i = 0; i < 2; i++ ) \
711 memcpy( pbuf3, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
712 memcpy( pbuf4, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
713 for( int j = 0; j < 4; j++ ) \
715 memcpy( pbuf3 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
716 memcpy( pbuf4 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
718 nz_c = call_c1( zigzag_c.name, t1, pbuf2, pbuf3, &dc_c ); \
719 nz_a = call_a1( zigzag_asm.name, t2, pbuf2, pbuf4, &dc_a ); \
720 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 ) \
723 fprintf( stderr, #name " [FAILED]\n" ); \
727 call_c2( zigzag_c.name, t1, pbuf2, pbuf3, &dc_c ); \
728 call_a2( zigzag_asm.name, t2, pbuf2, pbuf4, &dc_a ); \
731 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
732 if( zigzag_asm.name != zigzag_ref.name ) \
734 for( int j = 0; j < 100; j++ ) \
736 set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
738 memcpy(dct, buf1, size*sizeof(dctcoef)); \
739 for( int i = 0; i < size; i++ ) \
740 dct[i] = rand()&0x1F ? 0 : dct[i]; \
741 memcpy(buf3, buf4, 10); \
742 call_c( zigzag_c.name, t1, dct, buf3 ); \
743 call_a( zigzag_asm.name, t2, dct, buf4 ); \
744 if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( buf3, buf4, 10 ) ) \
752 x264_zigzag_init( 0, &zigzag_c, 0 );
753 x264_zigzag_init( cpu_ref, &zigzag_ref, 0 );
754 x264_zigzag_init( cpu_new, &zigzag_asm, 0 );
756 ok = 1; used_asm = 0;
757 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
758 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
759 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
760 TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
761 report( "zigzag_frame :" );
764 x264_zigzag_init( 0, &zigzag_c, 1 );
765 x264_zigzag_init( cpu_ref, &zigzag_ref, 1 );
766 x264_zigzag_init( cpu_new, &zigzag_asm, 1 );
768 ok = 1; used_asm = 0;
769 TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
770 TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 );
771 TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
772 TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
773 report( "zigzag_field :" );
775 ok = 1; used_asm = 0;
776 TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct1[0], 64 );
777 report( "zigzag_interleave :" );
778 #undef TEST_ZIGZAG_SCAN
779 #undef TEST_ZIGZAG_SUB
784 static int check_mc( int cpu_ref, int cpu_new )
786 x264_mc_functions_t mc_c;
787 x264_mc_functions_t mc_ref;
788 x264_mc_functions_t mc_a;
789 x264_pixel_function_t pixf;
791 pixel *src = &(pbuf1)[2*64+2];
792 pixel *src2[4] = { &(pbuf1)[3*64+2], &(pbuf1)[5*64+2],
793 &(pbuf1)[7*64+2], &(pbuf1)[9*64+2] };
797 int ret = 0, ok, used_asm;
799 x264_mc_init( 0, &mc_c );
800 x264_mc_init( cpu_ref, &mc_ref );
801 x264_mc_init( cpu_new, &mc_a );
802 x264_pixel_init( 0, &pixf );
804 #define MC_TEST_LUMA( w, h ) \
805 if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
807 const x264_weight_t *weight = weight_none; \
808 set_func_name( "mc_luma_%dx%d", w, h ); \
810 for( int i = 0; i < 1024; i++ ) \
811 pbuf3[i] = pbuf4[i] = 0xCD; \
812 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
813 call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h, weight ); \
814 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
816 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
820 if( mc_a.get_ref != mc_ref.get_ref ) \
823 int ref_stride = 32; \
824 const x264_weight_t *weight = weight_none; \
825 set_func_name( "get_ref_%dx%d", w, h ); \
827 for( int i = 0; i < 1024; i++ ) \
828 pbuf3[i] = pbuf4[i] = 0xCD; \
829 call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
830 ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h, weight ); \
831 for( int i = 0; i < h; i++ ) \
832 if( memcmp( dst1+i*32, ref+i*ref_stride, w * sizeof(pixel) ) ) \
834 fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
840 #define MC_TEST_CHROMA( w, h ) \
841 if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
843 set_func_name( "mc_chroma_%dx%d", w, h ); \
845 for( int i = 0; i < 1024; i++ ) \
846 pbuf3[i] = pbuf4[i] = 0xCD; \
847 call_c( mc_c.mc_chroma, dst1, dst1+8, 16, src, 64, dx, dy, w, h ); \
848 call_a( mc_a.mc_chroma, dst2, dst2+8, 16, src, 64, dx, dy, w, h ); \
849 /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \
850 for( int j = 0; j < h; j++ ) \
851 for( int i = w; i < 8; i++ ) \
853 dst2[i+j*16+8] = dst1[i+j*16+8]; \
854 dst2[i+j*16] = dst1[i+j*16]; \
856 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
858 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \
862 ok = 1; used_asm = 0;
863 for( int dy = -8; dy < 8; dy++ )
864 for( int dx = -128; dx < 128; dx++ )
866 if( rand()&15 ) continue; // running all of them is too slow
867 MC_TEST_LUMA( 20, 18 );
868 MC_TEST_LUMA( 16, 16 );
869 MC_TEST_LUMA( 16, 8 );
870 MC_TEST_LUMA( 12, 10 );
871 MC_TEST_LUMA( 8, 16 );
872 MC_TEST_LUMA( 8, 8 );
873 MC_TEST_LUMA( 8, 4 );
874 MC_TEST_LUMA( 4, 8 );
875 MC_TEST_LUMA( 4, 4 );
877 report( "mc luma :" );
879 ok = 1; used_asm = 0;
880 for( int dy = -1; dy < 9; dy++ )
881 for( int dx = -128; dx < 128; dx++ )
883 if( rand()&15 ) continue;
884 MC_TEST_CHROMA( 8, 8 );
885 MC_TEST_CHROMA( 8, 4 );
886 MC_TEST_CHROMA( 4, 8 );
887 MC_TEST_CHROMA( 4, 4 );
888 MC_TEST_CHROMA( 4, 2 );
889 MC_TEST_CHROMA( 2, 4 );
890 MC_TEST_CHROMA( 2, 2 );
892 report( "mc chroma :" );
894 #undef MC_TEST_CHROMA
896 #define MC_TEST_AVG( name, weight ) \
898 ok = 1, used_asm = 0; \
899 for( int i = 0; i < 10; i++ ) \
901 memcpy( pbuf3, pbuf1+320, 320 * sizeof(pixel) ); \
902 memcpy( pbuf4, pbuf1+320, 320 * sizeof(pixel) ); \
903 if( mc_a.name[i] != mc_ref.name[i] ) \
905 set_func_name( "%s_%s", #name, pixel_names[i] ); \
907 call_c1( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
908 call_a1( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
909 if( memcmp( pbuf3, pbuf4, 320 * sizeof(pixel) ) ) \
912 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
914 call_c2( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
915 call_a2( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
920 for( int w = -63; w <= 127 && ok; w++ )
921 MC_TEST_AVG( avg, w );
922 report( "mc wpredb :" );
924 #define MC_TEST_WEIGHT( name, weight, aligned ) \
925 int align_off = (aligned ? 0 : rand()%16); \
926 ok = 1, used_asm = 0; \
927 for( int i = 1; i <= 5; i++ ) \
929 ALIGNED_16( pixel buffC[640] ); \
930 ALIGNED_16( pixel buffA[640] ); \
931 int j = X264_MAX( i*4, 2 ); \
932 memset( buffC, 0, 640 * sizeof(pixel) ); \
933 memset( buffA, 0, 640 * sizeof(pixel) ); \
936 /* w12 is the same as w16 in some cases */ \
937 if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
939 if( mc_a.name[i] != mc_ref.name[i] ) \
941 set_func_name( "%s_w%d", #name, j ); \
943 call_c1( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
944 mc_a.weight_cache(&ha, &weight); \
945 call_a1( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
946 for( int k = 0; k < 16; k++ ) \
947 if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \
950 fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
953 call_c2( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
954 call_a2( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
958 ok = 1; used_asm = 0;
961 for( int s = 0; s <= 127 && ok; s++ )
963 for( int o = -128; o <= 127 && ok; o++ )
965 if( rand() & 2047 ) continue;
966 for( int d = 0; d <= 7 && ok; d++ )
970 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
971 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
976 report( "mc weight :" );
978 ok = 1; used_asm = 0;
979 for( int o = 0; o <= 127 && ok; o++ )
982 if( rand() & 15 ) continue;
983 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
984 MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
986 report( "mc offsetadd :" );
987 ok = 1; used_asm = 0;
988 for( int o = -128; o < 0 && ok; o++ )
991 if( rand() & 15 ) continue;
992 x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
993 MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
995 report( "mc offsetsub :" );
997 ok = 1; used_asm = 0;
998 if( mc_a.store_interleave_8x8x2 != mc_ref.store_interleave_8x8x2 )
1000 set_func_name( "store_interleave_8x8x2" );
1002 memset( pbuf3, 0, 64*8 );
1003 memset( pbuf4, 0, 64*8 );
1004 call_c( mc_c.store_interleave_8x8x2, pbuf3, 64, pbuf1, pbuf1+16 );
1005 call_a( mc_a.store_interleave_8x8x2, pbuf4, 64, pbuf1, pbuf1+16 );
1006 if( memcmp( pbuf3, pbuf4, 64*8 ) )
1009 if( mc_a.load_deinterleave_8x8x2_fenc != mc_ref.load_deinterleave_8x8x2_fenc )
1011 set_func_name( "load_deinterleave_8x8x2_fenc" );
1013 call_c( mc_c.load_deinterleave_8x8x2_fenc, pbuf3, pbuf1, 64 );
1014 call_a( mc_a.load_deinterleave_8x8x2_fenc, pbuf4, pbuf1, 64 );
1015 if( memcmp( pbuf3, pbuf4, FENC_STRIDE*8 ) )
1018 if( mc_a.load_deinterleave_8x8x2_fdec != mc_ref.load_deinterleave_8x8x2_fdec )
1020 set_func_name( "load_deinterleave_8x8x2_fdec" );
1022 call_c( mc_c.load_deinterleave_8x8x2_fdec, pbuf3, pbuf1, 64 );
1023 call_a( mc_a.load_deinterleave_8x8x2_fdec, pbuf4, pbuf1, 64 );
1024 if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*8 ) )
1027 report( "store_interleave :" );
1030 int w, h, src_stride;
1031 } 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} };
1032 ok = 1; used_asm = 0;
1033 if( mc_a.plane_copy != mc_ref.plane_copy )
1035 set_func_name( "plane_copy" );
1037 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1039 int w = plane_specs[i].w;
1040 int h = plane_specs[i].h;
1041 int src_stride = plane_specs[i].src_stride;
1042 int dst_stride = (w + 127) & ~63;
1043 assert( dst_stride * h <= 0x1000 );
1044 uint8_t *src1 = buf1 + X264_MAX(0, -src_stride) * (h-1);
1045 memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1046 memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1047 call_c( mc_c.plane_copy, pbuf3, dst_stride, src1, src_stride, w, h );
1048 call_a( mc_a.plane_copy, pbuf4, dst_stride, src1, src_stride, w, h );
1049 for( int y = 0; y < h; y++ )
1050 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(pixel) ) )
1053 fprintf( stderr, "plane_copy FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1059 if( mc_a.plane_copy_interleave != mc_ref.plane_copy_interleave )
1061 set_func_name( "plane_copy_interleave" );
1063 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1065 int w = (plane_specs[i].w + 1) >> 1;
1066 int h = plane_specs[i].h;
1067 int src_stride = (plane_specs[i].src_stride + 1) >> 1;
1068 int dst_stride = (2*w + 127) & ~63;
1069 assert( dst_stride * h <= 0x1000 );
1070 uint8_t *src1 = buf1 + X264_MAX(0, -src_stride) * (h-1);
1071 memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1072 memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1073 call_c( mc_c.plane_copy_interleave, pbuf3, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1074 call_a( mc_a.plane_copy_interleave, pbuf4, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1075 for( int y = 0; y < h; y++ )
1076 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) )
1079 fprintf( stderr, "plane_copy_interleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1085 if( mc_a.plane_copy_deinterleave != mc_ref.plane_copy_deinterleave )
1087 set_func_name( "plane_copy_deinterleave" );
1089 for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1091 int w = (plane_specs[i].w + 1) >> 1;
1092 int h = plane_specs[i].h;
1094 int src_stride = (2*w + 127) & ~63;
1095 int offv = (dst_stride*h + 31) & ~15;
1096 memset( pbuf3, 0, 0x1000 );
1097 memset( pbuf4, 0, 0x1000 );
1098 call_c( mc_c.plane_copy_deinterleave, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf1, src_stride, w, h );
1099 call_a( mc_a.plane_copy_deinterleave, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf1, src_stride, w, h );
1100 for( int y = 0; y < h; y++ )
1101 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w ) ||
1102 memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w ) )
1105 fprintf( stderr, "plane_copy_deinterleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1110 report( "plane_copy :" );
1112 if( mc_a.hpel_filter != mc_ref.hpel_filter )
1114 pixel *srchpel = pbuf1+8+2*64;
1115 pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
1116 pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
1117 void *tmp = pbuf3+49*64;
1118 set_func_name( "hpel_filter" );
1119 ok = 1; used_asm = 1;
1120 memset( pbuf3, 0, 4096 * sizeof(pixel) );
1121 memset( pbuf4, 0, 4096 * sizeof(pixel) );
1122 call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, 64, 48, 10, tmp );
1123 call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, 64, 48, 10, tmp );
1124 for( int i = 0; i < 3; i++ )
1125 for( int j = 0; j < 10; j++ )
1126 //FIXME ideally the first pixels would match too, but they aren't actually used
1127 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
1130 fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
1131 for( int k = 0; k < 48; k++ )
1132 printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
1134 for( int k = 0; k < 48; k++ )
1135 printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
1139 report( "hpel filter :" );
1142 if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
1144 pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
1145 pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1146 set_func_name( "lowres_init" );
1147 ok = 1; used_asm = 1;
1148 for( int w = 40; w <= 48; w += 8 )
1150 int stride = (w+8)&~15;
1151 call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
1152 call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
1153 for( int i = 0; i < 16; i++ )
1155 for( int j = 0; j < 4; j++ )
1156 if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w * sizeof(pixel) ) )
1159 fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1160 for( int k = 0; k < w; k++ )
1161 printf( "%d ", dstc[j][k+i*stride] );
1163 for( int k = 0; k < w; k++ )
1164 printf( "%d ", dsta[j][k+i*stride] );
1170 report( "lowres init :" );
1173 #define INTEGRAL_INIT( name, size, ... )\
1174 if( mc_a.name != mc_ref.name )\
1177 set_func_name( #name );\
1179 memcpy( buf3, buf1, size*2*stride );\
1180 memcpy( buf4, buf1, size*2*stride );\
1181 uint16_t *sum = (uint16_t*)buf3;\
1182 call_c1( mc_c.name, __VA_ARGS__ );\
1183 sum = (uint16_t*)buf4;\
1184 call_a1( mc_a.name, __VA_ARGS__ );\
1185 if( memcmp( buf3, buf4, (stride-8)*2 ) \
1186 || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
1188 call_c2( mc_c.name, __VA_ARGS__ );\
1189 call_a2( mc_a.name, __VA_ARGS__ );\
1191 ok = 1; used_asm = 0;
1192 INTEGRAL_INIT( integral_init4h, 2, sum+stride, pbuf2, stride );
1193 INTEGRAL_INIT( integral_init8h, 2, sum+stride, pbuf2, stride );
1194 INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
1195 INTEGRAL_INIT( integral_init8v, 9, sum, stride );
1196 report( "integral init :" );
1198 if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1200 ok = 1; used_asm = 1;
1201 set_func_name( "mbtree_propagate" );
1202 int *dsta = (int*)buf3;
1203 int *dstc = dsta+400;
1204 uint16_t *prop = (uint16_t*)buf1;
1205 uint16_t *intra = (uint16_t*)buf4;
1206 uint16_t *inter = intra+400;
1207 uint16_t *qscale = inter+400;
1208 uint16_t *rnd = (uint16_t*)buf2;
1210 for( int i = 0; i < 400; i++ )
1212 intra[i] = *rnd++ & 0x7fff;
1213 intra[i] += !intra[i];
1214 inter[i] = *rnd++ & 0x7fff;
1215 qscale[i] = *rnd++ & 0x7fff;
1217 call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, 400 );
1218 call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, 400 );
1219 // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1221 for( int i = 0; i < 400; i++ )
1222 ok &= abs( dstc[i]-dsta[i] ) <= 1 || fabs( (double)dstc[i]/dsta[i]-1 ) < 1e-6;
1223 report( "mbtree propagate :" );
1229 static int check_deblock( int cpu_ref, int cpu_new )
1231 x264_deblock_function_t db_c;
1232 x264_deblock_function_t db_ref;
1233 x264_deblock_function_t db_a;
1234 int ret = 0, ok = 1, used_asm = 0;
1235 int alphas[36], betas[36];
1238 x264_deblock_init( 0, &db_c );
1239 x264_deblock_init( cpu_ref, &db_ref );
1240 x264_deblock_init( cpu_new, &db_a );
1242 /* not exactly the real values of a,b,tc but close enough */
1243 for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1245 alphas[i] = a << (BIT_DEPTH-8);
1246 betas[i] = (i+1)/2 << (BIT_DEPTH-8);
1247 tcs[i][0] = tcs[i][3] = (c+6)/10 << (BIT_DEPTH-8);
1248 tcs[i][1] = (c+7)/15 << (BIT_DEPTH-8);
1249 tcs[i][2] = (c+9)/20 << (BIT_DEPTH-8);
1254 #define TEST_DEBLOCK( name, align, ... ) \
1255 for( int i = 0; i < 36; i++ ) \
1257 int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1258 for( int j = 0; j < 1024; j++ ) \
1259 /* two distributions of random to excersize different failure modes */ \
1260 pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \
1261 memcpy( pbuf4, pbuf3, 1024 * sizeof(pixel) ); \
1262 if( db_a.name != db_ref.name ) \
1264 set_func_name( #name ); \
1266 call_c1( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1267 call_a1( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1268 if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1271 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1274 call_c2( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1275 call_a2( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1279 TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1280 TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1281 TEST_DEBLOCK( deblock_chroma[0], 0, tcs[i] );
1282 TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1283 TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1284 TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1285 TEST_DEBLOCK( deblock_chroma_intra[0], 0 );
1286 TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1288 if( db_a.deblock_strength != db_ref.deblock_strength )
1290 for( int i = 0; i < 100; i++ )
1292 ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1293 ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1294 ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] );
1295 ALIGNED_ARRAY_16( uint8_t, bs, [2],[2][4][4] );
1296 for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1297 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1298 for( int j = 0; j < 2; j++ )
1299 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1301 ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1302 for( int l = 0; l < 2; l++ )
1303 mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1305 set_func_name( "deblock_strength" );
1306 call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1307 call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1308 if( memcmp( bs[0], bs[1], sizeof(bs[0]) ) )
1311 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1312 for( int j = 0; j < 2; j++ )
1314 for( int k = 0; k < 2; k++ )
1315 for( int l = 0; l < 4; l++ )
1317 for( int m = 0; m < 4; m++ )
1318 printf("%d ",bs[j][k][l][m]);
1328 report( "deblock :" );
1333 static int check_quant( int cpu_ref, int cpu_new )
1335 x264_quant_function_t qf_c;
1336 x264_quant_function_t qf_ref;
1337 x264_quant_function_t qf_a;
1338 ALIGNED_16( dctcoef dct1[64] );
1339 ALIGNED_16( dctcoef dct2[64] );
1340 ALIGNED_16( uint8_t cqm_buf[64] );
1341 int ret = 0, ok, used_asm;
1342 int oks[2] = {1,1}, used_asms[2] = {0,0};
1345 memset( h, 0, sizeof(*h) );
1346 h->pps = h->pps_array;
1347 x264_param_default( &h->param );
1348 h->chroma_qp_table = i_chroma_qp_table + 12;
1349 h->param.rc.i_qp_min = 26 + QP_BD_OFFSET;
1350 h->param.analyse.b_transform_8x8 = 1;
1352 for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1356 for( int i = 0; i < 6; i++ )
1357 h->pps->scaling_list[i] = x264_cqm_flat16;
1358 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1360 else if( i_cqm == 1 )
1362 for( int i = 0; i < 6; i++ )
1363 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1364 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1368 int max_scale = BIT_DEPTH < 10 ? 255 : 228;
1370 for( int i = 0; i < 64; i++ )
1371 cqm_buf[i] = 10 + rand() % (max_scale - 9);
1373 for( int i = 0; i < 64; i++ )
1375 for( int i = 0; i < 6; i++ )
1376 h->pps->scaling_list[i] = cqm_buf;
1377 h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1381 x264_quant_init( h, 0, &qf_c );
1382 x264_quant_init( h, cpu_ref, &qf_ref );
1383 x264_quant_init( h, cpu_new, &qf_a );
1385 #define INIT_QUANT8(j) \
1387 static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1388 for( int i = 0; i < 64; i++ ) \
1390 unsigned int scale = (255*scale1d[i>>3]*scale1d[i&7])/16; \
1391 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1395 #define INIT_QUANT4(j) \
1397 static const int scale1d[4] = {4,6,4,6}; \
1398 for( int i = 0; i < 16; i++ ) \
1400 unsigned int scale = 255*scale1d[i>>2]*scale1d[i&3]; \
1401 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1405 #define TEST_QUANT_DC( name, cqm ) \
1406 if( qf_a.name != qf_ref.name ) \
1408 set_func_name( #name ); \
1410 for( int qp = QP_MAX; qp > 0; qp-- ) \
1412 for( int j = 0; j < 2; j++ ) \
1414 int result_c, result_a; \
1415 for( int i = 0; i < 16; i++ ) \
1416 dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1417 result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1418 result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1419 if( memcmp( dct1, dct2, 16*sizeof(dctcoef) ) || result_c != result_a ) \
1422 fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1425 call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1426 call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1431 #define TEST_QUANT( qname, block, w ) \
1432 if( qf_a.qname != qf_ref.qname ) \
1434 set_func_name( #qname ); \
1436 for( int qp = QP_MAX; qp > 0; qp-- ) \
1438 for( int j = 0; j < 2; j++ ) \
1441 int result_c = call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1442 int result_a = call_a1( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1443 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \
1446 fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1449 call_c2( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1450 call_a2( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1455 TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1456 TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1457 TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1458 TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1459 TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1460 TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1462 #define TEST_DEQUANT( qname, dqname, block, w ) \
1463 if( qf_a.dqname != qf_ref.dqname ) \
1465 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1467 for( int qp = QP_MAX; qp > 0; qp-- ) \
1470 call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1471 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1472 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1473 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1474 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1477 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1480 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1481 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1485 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1486 TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1487 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1488 TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1490 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1491 if( qf_a.dqname != qf_ref.dqname ) \
1493 set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1495 for( int qp = QP_MAX; qp > 0; qp-- ) \
1497 for( int i = 0; i < 16; i++ ) \
1499 call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1500 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1501 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1502 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1503 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1506 fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1508 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1509 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1513 TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1515 x264_cqm_delete( h );
1518 ok = oks[0]; used_asm = used_asms[0];
1519 report( "quant :" );
1521 ok = oks[1]; used_asm = used_asms[1];
1522 report( "dequant :" );
1524 ok = 1; used_asm = 0;
1525 if( qf_a.denoise_dct != qf_ref.denoise_dct )
1528 for( int size = 16; size <= 64; size += 48 )
1530 set_func_name( "denoise_dct" );
1531 memcpy( dct1, buf1, size*sizeof(dctcoef) );
1532 memcpy( dct2, buf1, size*sizeof(dctcoef) );
1533 memcpy( buf3+256, buf3, 256 );
1534 call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1535 call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1536 if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1538 call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (uint16_t*)buf2, size );
1539 call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (uint16_t*)buf2, size );
1542 report( "denoise dct :" );
1544 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1545 if( qf_a.decname != qf_ref.decname ) \
1547 set_func_name( #decname ); \
1549 for( int i = 0; i < 100; i++ ) \
1551 for( int idx = 0; idx < w*w; idx++ ) \
1552 dct1[idx] = !(rand()&3) + (!(rand()&15))*(rand()&3); \
1555 int result_c = call_c( qf_c.decname, dct1 ); \
1556 int result_a = call_a( qf_a.decname, dct1 ); \
1557 if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1560 fprintf( stderr, #decname ": [FAILED]\n" ); \
1566 ok = 1; used_asm = 0;
1567 TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1568 TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1569 TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1570 report( "decimate_score :" );
1572 #define TEST_LAST( last, lastname, w, ac ) \
1573 if( qf_a.last != qf_ref.last ) \
1575 set_func_name( #lastname ); \
1577 for( int i = 0; i < 100; i++ ) \
1580 int max = rand() & (w*w-1); \
1581 memset( dct1, 0, w*w*sizeof(dctcoef) ); \
1582 for( int idx = ac; idx < max; idx++ ) \
1583 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1586 int result_c = call_c( qf_c.last, dct1+ac ); \
1587 int result_a = call_a( qf_a.last, dct1+ac ); \
1588 if( result_c != result_a ) \
1591 fprintf( stderr, #lastname ": [FAILED]\n" ); \
1597 ok = 1; used_asm = 0;
1598 TEST_LAST( coeff_last[DCT_CHROMA_DC], coeff_last4, 2, 0 );
1599 TEST_LAST( coeff_last[ DCT_LUMA_AC], coeff_last15, 4, 1 );
1600 TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 4, 0 );
1601 TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 8, 0 );
1602 report( "coeff_last :" );
1604 #define TEST_LEVELRUN( lastname, name, w, ac ) \
1605 if( qf_a.lastname != qf_ref.lastname ) \
1607 set_func_name( #name ); \
1609 for( int i = 0; i < 100; i++ ) \
1611 x264_run_level_t runlevel_c, runlevel_a; \
1613 int max = rand() & (w*w-1); \
1614 memset( dct1, 0, w*w*sizeof(dctcoef) ); \
1615 memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
1616 memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
1617 for( int idx = ac; idx < max; idx++ ) \
1618 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1621 int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
1622 int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
1623 if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
1624 memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c) || \
1625 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
1628 fprintf( stderr, #name ": [FAILED]\n" ); \
1634 ok = 1; used_asm = 0;
1635 TEST_LEVELRUN( coeff_level_run[DCT_CHROMA_DC], coeff_level_run4, 2, 0 );
1636 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_AC], coeff_level_run15, 4, 1 );
1637 TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 4, 0 );
1638 report( "coeff_level_run :" );
1643 static int check_intra( int cpu_ref, int cpu_new )
1645 int ret = 0, ok = 1, used_asm = 0;
1646 ALIGNED_16( pixel edge[33] );
1647 ALIGNED_16( pixel edge2[33] );
1650 x264_predict_t predict_16x16[4+3];
1651 x264_predict_t predict_8x8c[4+3];
1652 x264_predict8x8_t predict_8x8[9+3];
1653 x264_predict_t predict_4x4[9+3];
1654 x264_predict_8x8_filter_t predict_8x8_filter;
1655 } ip_c, ip_ref, ip_a;
1657 x264_predict_16x16_init( 0, ip_c.predict_16x16 );
1658 x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
1659 x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
1660 x264_predict_4x4_init( 0, ip_c.predict_4x4 );
1662 x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
1663 x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
1664 x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
1665 x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
1667 x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
1668 x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
1669 x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
1670 x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
1672 ip_c.predict_8x8_filter( pbuf1+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
1674 #define INTRA_TEST( name, dir, w, ... )\
1675 if( ip_a.name[dir] != ip_ref.name[dir] )\
1677 set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
1679 memcpy( pbuf3, pbuf1, 32*20 * sizeof(pixel) );\
1680 memcpy( pbuf4, pbuf1, 32*20 * sizeof(pixel) );\
1681 call_c( ip_c.name[dir], pbuf3+48, ##__VA_ARGS__ );\
1682 call_a( ip_a.name[dir], pbuf4+48, ##__VA_ARGS__ );\
1683 if( memcmp( pbuf3, pbuf4, 32*20 * sizeof(pixel) ) )\
1685 fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\
1687 for( int k = -1; k < 16; k++ )\
1688 printf( "%2x ", edge[16+k] );\
1690 for( int j = 0; j < w; j++ )\
1692 printf( "%2x ", edge[14-j] );\
1693 for( int k = 0; k < w; k++ )\
1694 printf( "%2x ", pbuf4[48+k+j*32] );\
1698 for( int j = 0; j < w; j++ )\
1701 for( int k = 0; k < w; k++ )\
1702 printf( "%2x ", pbuf3[48+k+j*32] );\
1708 for( int i = 0; i < 12; i++ )
1709 INTRA_TEST( predict_4x4, i, 4 );
1710 for( int i = 0; i < 7; i++ )
1711 INTRA_TEST( predict_8x8c, i, 8 );
1712 for( int i = 0; i < 7; i++ )
1713 INTRA_TEST( predict_16x16, i, 16 );
1714 for( int i = 0; i < 12; i++ )
1715 INTRA_TEST( predict_8x8, i, 8, edge );
1717 set_func_name("intra_predict_8x8_filter");
1718 if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
1721 for( int i = 0; i < 32; i++ )
1723 memcpy( edge2, edge, 33 * sizeof(pixel) );
1724 call_c(ip_c.predict_8x8_filter, pbuf1+48, edge, (i&24)>>1, i&7);
1725 call_a(ip_a.predict_8x8_filter, pbuf1+48, edge2, (i&24)>>1, i&7);
1726 if( memcmp( edge, edge2, 33 * sizeof(pixel) ) )
1728 fprintf( stderr, "predict_8x8_filter : [FAILED] %d %d\n", (i&24)>>1, i&7);
1734 report( "intra pred :" );
1738 #define DECL_CABAC(cpu) \
1739 static void run_cabac_decision_##cpu( uint8_t *dst )\
1742 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1743 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1744 for( int i = 0; i < 0x1000; i++ )\
1745 x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
1747 static void run_cabac_bypass_##cpu( uint8_t *dst )\
1750 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1751 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1752 for( int i = 0; i < 0x1000; i++ )\
1753 x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
1755 static void run_cabac_terminal_##cpu( uint8_t *dst )\
1758 x264_cabac_context_init( &cb, SLICE_TYPE_P, 26, 0 );\
1759 x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
1760 for( int i = 0; i < 0x1000; i++ )\
1761 x264_cabac_encode_terminal_##cpu( &cb );\
1767 #define run_cabac_decision_asm run_cabac_decision_c
1768 #define run_cabac_bypass_asm run_cabac_bypass_c
1769 #define run_cabac_terminal_asm run_cabac_terminal_c
1772 static int check_cabac( int cpu_ref, int cpu_new )
1774 int ret = 0, ok, used_asm = 1;
1775 if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
1778 set_func_name( "cabac_encode_decision" );
1779 memcpy( buf4, buf3, 0x1000 );
1780 call_c( run_cabac_decision_c, buf3 );
1781 call_a( run_cabac_decision_asm, buf4 );
1782 ok = !memcmp( buf3, buf4, 0x1000 );
1783 report( "cabac decision:" );
1785 set_func_name( "cabac_encode_bypass" );
1786 memcpy( buf4, buf3, 0x1000 );
1787 call_c( run_cabac_bypass_c, buf3 );
1788 call_a( run_cabac_bypass_asm, buf4 );
1789 ok = !memcmp( buf3, buf4, 0x1000 );
1790 report( "cabac bypass:" );
1792 set_func_name( "cabac_encode_terminal" );
1793 memcpy( buf4, buf3, 0x1000 );
1794 call_c( run_cabac_terminal_c, buf3 );
1795 call_a( run_cabac_terminal_asm, buf4 );
1796 ok = !memcmp( buf3, buf4, 0x1000 );
1797 report( "cabac terminal:" );
1802 static int check_bitstream( int cpu_ref, int cpu_new )
1804 x264_bitstream_function_t bs_c;
1805 x264_bitstream_function_t bs_ref;
1806 x264_bitstream_function_t bs_a;
1808 int ret = 0, ok = 1, used_asm = 0;
1810 x264_bitstream_init( 0, &bs_c );
1811 x264_bitstream_init( cpu_ref, &bs_ref );
1812 x264_bitstream_init( cpu_new, &bs_a );
1813 if( bs_a.nal_escape != bs_ref.nal_escape )
1816 uint8_t *input = malloc(size+100);
1817 uint8_t *output1 = malloc(size*2);
1818 uint8_t *output2 = malloc(size*2);
1820 set_func_name( "nal_escape" );
1821 for( int i = 0; i < 100; i++ )
1823 /* Test corner-case sizes */
1824 int test_size = i < 10 ? i+1 : rand() & 0x3fff;
1825 /* Test 8 different probability distributions of zeros */
1826 for( int j = 0; j < test_size; j++ )
1827 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
1828 uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
1829 uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
1830 int size_c = end_c-output1;
1831 int size_a = end_a-output2;
1832 if( size_c != size_a || memcmp( output1, output2, size_c ) )
1834 fprintf( stderr, "nal_escape : [FAILED] %d %d\n", size_c, size_a );
1839 for( int j = 0; j < size; j++ )
1841 call_c2( bs_c.nal_escape, output1, input, input+size );
1842 call_a2( bs_a.nal_escape, output2, input, input+size );
1847 report( "nal escape:" );
1852 static int check_all_funcs( int cpu_ref, int cpu_new )
1854 return check_pixel( cpu_ref, cpu_new )
1855 + check_dct( cpu_ref, cpu_new )
1856 + check_mc( cpu_ref, cpu_new )
1857 + check_intra( cpu_ref, cpu_new )
1858 + check_deblock( cpu_ref, cpu_new )
1859 + check_quant( cpu_ref, cpu_new )
1860 + check_cabac( cpu_ref, cpu_new )
1861 + check_bitstream( cpu_ref, cpu_new );
1864 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
1866 *cpu_ref = *cpu_new;
1868 if( *cpu_new & X264_CPU_SSE2_IS_FAST )
1869 *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
1871 fprintf( stderr, "x264: %s\n", name );
1872 return check_all_funcs( *cpu_ref, *cpu_new );
1875 static int check_all_flags( void )
1878 int cpu0 = 0, cpu1 = 0;
1880 if( x264_cpu_detect() & X264_CPU_MMXEXT )
1882 ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMXEXT, "MMX" );
1883 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
1884 cpu1 &= ~X264_CPU_CACHELINE_64;
1886 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
1887 cpu1 &= ~X264_CPU_CACHELINE_32;
1889 if( x264_cpu_detect() & X264_CPU_LZCNT )
1891 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
1892 cpu1 &= ~X264_CPU_LZCNT;
1894 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
1895 cpu1 &= ~X264_CPU_SLOW_CTZ;
1897 if( x264_cpu_detect() & X264_CPU_SSE2 )
1899 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
1900 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
1901 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
1902 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSE2 FastShuffle" );
1903 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
1904 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
1905 cpu1 &= ~X264_CPU_SLOW_CTZ;
1906 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSE2 SlowAtom" );
1907 cpu1 &= ~X264_CPU_SLOW_ATOM;
1909 if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
1911 cpu1 &= ~X264_CPU_CACHELINE_64;
1912 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
1913 cpu1 &= ~X264_CPU_SSE_MISALIGN;
1915 if( x264_cpu_detect() & X264_CPU_LZCNT )
1917 cpu1 &= ~X264_CPU_CACHELINE_64;
1918 ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
1919 cpu1 &= ~X264_CPU_LZCNT;
1921 if( x264_cpu_detect() & X264_CPU_SSE3 )
1922 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
1923 if( x264_cpu_detect() & X264_CPU_SSSE3 )
1925 cpu1 &= ~X264_CPU_CACHELINE_64;
1926 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
1927 ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
1928 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSSE3 FastShuffle" );
1929 cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
1930 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
1931 cpu1 &= ~X264_CPU_SLOW_CTZ;
1932 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
1933 cpu1 &= ~X264_CPU_SLOW_ATOM;
1935 if( x264_cpu_detect() & X264_CPU_SSE4 )
1937 cpu1 &= ~X264_CPU_CACHELINE_64;
1938 ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
1941 if( x264_cpu_detect() & X264_CPU_ALTIVEC )
1943 fprintf( stderr, "x264: ALTIVEC against C\n" );
1944 ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
1947 if( x264_cpu_detect() & X264_CPU_ARMV6 )
1948 ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
1949 if( x264_cpu_detect() & X264_CPU_NEON )
1950 ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
1951 if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC )
1952 ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
1957 int main(int argc, char *argv[])
1961 if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
1963 #if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM
1964 fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
1968 if( argv[1][7] == '=' )
1970 bench_pattern = argv[1]+8;
1971 bench_pattern_len = strlen(bench_pattern);
1977 int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
1978 fprintf( stderr, "x264: using random seed %u\n", seed );
1981 buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 16*BENCH_ALIGNS );
1982 pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 16*BENCH_ALIGNS );
1983 if( !buf1 || !pbuf1 )
1985 fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
1988 #define INIT_POINTER_OFFSETS\
1989 buf2 = buf1 + 0xf00;\
1990 buf3 = buf2 + 0xf00;\
1991 buf4 = buf3 + 0x1000*sizeof(pixel);\
1992 pbuf2 = pbuf1 + 0xf00;\
1993 pbuf3 = (pixel*)buf3;\
1994 pbuf4 = (pixel*)buf4;
1995 INIT_POINTER_OFFSETS;
1996 for( int i = 0; i < 0x1e00; i++ )
1998 buf1[i] = rand() & 0xFF;
1999 pbuf1[i] = rand() & PIXEL_MAX;
2001 memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) );
2003 /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
2005 for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
2007 INIT_POINTER_OFFSETS;
2008 ret |= x264_stack_pagealign( check_all_flags, i*16 );
2012 fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
2015 ret = check_all_flags();
2019 fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
2022 fprintf( stderr, "x264: All tests passed Yeah :)\n" );