X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=tools%2Fcheckasm.c;h=721c3d7f33f3b576c7f42ac316eef66dcd0930e4;hb=366fa85885053c7b836a4272a4fbec1852103979;hp=be581f51d4ed5afc6b3a993faa3fddc937de35f9;hpb=a35fd4194dd7004abe6f66679496beded405515a;p=x264 diff --git a/tools/checkasm.c b/tools/checkasm.c index be581f51..721c3d7f 100644 --- a/tools/checkasm.c +++ b/tools/checkasm.c @@ -1,7 +1,7 @@ /***************************************************************************** * checkasm.c: assembly check tool ***************************************************************************** - * Copyright (C) 2003-2011 x264 project + * Copyright (C) 2003-2015 x264 project * * Authors: Loren Merritt * Laurent Aimar @@ -61,7 +61,7 @@ typedef struct { void *pointer; // just for detecting duplicates uint32_t cpu; - uint32_t cycles; + uint64_t cycles; uint32_t den; } bench_t; @@ -90,11 +90,19 @@ static inline uint32_t read_time(void) { uint32_t a = 0; #if HAVE_X86_INLINE_ASM - asm volatile( "rdtsc" :"=a"(a) ::"edx" ); + asm volatile( "lfence \n" + "rdtsc \n" + : "=a"(a) :: "edx", "memory" ); #elif ARCH_PPC - asm volatile( "mftb %0" : "=r" (a) ); + asm volatile( "mftb %0" : "=r"(a) :: "memory" ); #elif ARCH_ARM // ARMv7 only - asm volatile( "mrc p15, 0, %0, c9, c13, 0" : "=r"(a) ); + asm volatile( "mrc p15, 0, %0, c9, c13, 0" : "=r"(a) :: "memory" ); +#elif ARCH_AARCH64 + uint64_t b = 0; + asm volatile( "mrs %0, pmccntr_el0" : "=r"(b) :: "memory" ); + a = b; +#elif ARCH_MIPS + asm volatile( "rdhwr %0, $2" : "=r"(a) :: "memory" ); #endif return a; } @@ -137,12 +145,12 @@ static int cmp_bench( const void *a, const void *b ) static void print_bench(void) { - uint16_t nops[10000] = {0}; + uint16_t nops[10000]; int nfuncs, nop_time=0; for( int i = 0; i < 10000; i++ ) { - int t = read_time(); + uint32_t t = read_time(); nops[i] = read_time() - t; } qsort( nops, 10000, sizeof(uint16_t), cmp_nop ); @@ -164,52 +172,104 @@ static void print_bench(void) if( k < j ) continue; printf( "%s_%s%s: %"PRId64"\n", benchs[i].name, +#if HAVE_MMX + b->cpu&X264_CPU_AVX2 ? "avx2" : + b->cpu&X264_CPU_FMA3 ? "fma3" : b->cpu&X264_CPU_FMA4 ? "fma4" : b->cpu&X264_CPU_XOP ? "xop" : b->cpu&X264_CPU_AVX ? "avx" : + b->cpu&X264_CPU_SSE42 ? "sse42" : b->cpu&X264_CPU_SSE4 ? "sse4" : b->cpu&X264_CPU_SSSE3 ? "ssse3" : b->cpu&X264_CPU_SSE3 ? "sse3" : /* print sse2slow only if there's also a sse2fast version of the same func */ b->cpu&X264_CPU_SSE2_IS_SLOW && jcpu&X264_CPU_SSE2 ? "sse2" : + b->cpu&X264_CPU_SSE ? "sse" : b->cpu&X264_CPU_MMX ? "mmx" : +#elif ARCH_PPC b->cpu&X264_CPU_ALTIVEC ? "altivec" : +#elif ARCH_ARM b->cpu&X264_CPU_NEON ? "neon" : - b->cpu&X264_CPU_ARMV6 ? "armv6" : "c", + b->cpu&X264_CPU_ARMV6 ? "armv6" : +#elif ARCH_AARCH64 + b->cpu&X264_CPU_NEON ? "neon" : + b->cpu&X264_CPU_ARMV8 ? "armv8" : +#elif ARCH_MIPS + b->cpu&X264_CPU_MSA ? "msa" : +#endif + "c", +#if HAVE_MMX b->cpu&X264_CPU_CACHELINE_32 ? "_c32" : + b->cpu&X264_CPU_SLOW_ATOM && b->cpu&X264_CPU_CACHELINE_64 ? "_c64_atom" : b->cpu&X264_CPU_CACHELINE_64 ? "_c64" : - b->cpu&X264_CPU_SHUFFLE_IS_FAST && !(b->cpu&X264_CPU_SSE4) ? "_fastshuffle" : - b->cpu&X264_CPU_SSE_MISALIGN ? "_misalign" : + b->cpu&X264_CPU_SLOW_SHUFFLE ? "_slowshuffle" : b->cpu&X264_CPU_LZCNT ? "_lzcnt" : - b->cpu&X264_CPU_FAST_NEON_MRC ? "_fast_mrc" : + b->cpu&X264_CPU_BMI2 ? "_bmi2" : + b->cpu&X264_CPU_BMI1 ? "_bmi1" : b->cpu&X264_CPU_SLOW_CTZ ? "_slow_ctz" : - b->cpu&X264_CPU_SLOW_ATOM ? "_slow_atom" : "", - ((int64_t)10*b->cycles/b->den - nop_time)/4 ); + b->cpu&X264_CPU_SLOW_ATOM ? "_atom" : +#elif ARCH_ARM + b->cpu&X264_CPU_FAST_NEON_MRC ? "_fast_mrc" : +#endif + "", + (int64_t)(10*b->cycles/b->den - nop_time)/4 ); } } #if ARCH_X86 || ARCH_X86_64 int x264_stack_pagealign( int (*func)(), int align ); + +/* detect when callee-saved regs aren't saved + * needs an explicit asm check because it only sometimes crashes in normal use. */ +intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... ); #else #define x264_stack_pagealign( func, align ) func() #endif +#if ARCH_AARCH64 +intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... ); +#endif + +#if ARCH_ARM +intptr_t x264_checkasm_call_neon( intptr_t (*func)(), int *ok, ... ); +intptr_t x264_checkasm_call_noneon( intptr_t (*func)(), int *ok, ... ); +intptr_t (*x264_checkasm_call)( intptr_t (*func)(), int *ok, ... ) = x264_checkasm_call_noneon; +#endif + #define call_c1(func,...) func(__VA_ARGS__) -#if ARCH_X86 || defined(_WIN64) -/* detect when callee-saved regs aren't saved. - * needs an explicit asm check because it only sometimes crashes in normal use. */ -intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... ); -#define call_a1(func,...) x264_checkasm_call((intptr_t(*)())func, &ok, __VA_ARGS__) +#if ARCH_X86_64 +/* Evil hack: detect incorrect assumptions that 32-bit ints are zero-extended to 64-bit. + * This is done by clobbering the stack with junk around the stack pointer and calling the + * assembly function through x264_checkasm_call with added dummy arguments which forces all + * real arguments to be passed on the stack and not in registers. For 32-bit argument the + * upper half of the 64-bit register location on the stack will now contain junk. Note that + * this is dependant on compiler behaviour and that interrupts etc. at the wrong time may + * overwrite the junk written to the stack so there's no guarantee that it will always + * detect all functions that assumes zero-extension. + */ +void x264_checkasm_stack_clobber( uint64_t clobber, ... ); +#define call_a1(func,...) ({ \ + uint64_t r = (rand() & 0xffff) * 0x0001000100010001ULL; \ + 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 */ \ + x264_checkasm_call(( intptr_t(*)())func, &ok, 0, 0, 0, 0, __VA_ARGS__ ); }) +#elif ARCH_X86 || (ARCH_AARCH64 && !defined(__APPLE__)) || ARCH_ARM +#define call_a1(func,...) x264_checkasm_call( (intptr_t(*)())func, &ok, __VA_ARGS__ ) #else #define call_a1 call_c1 #endif +#if ARCH_ARM +#define call_a1_64(func,...) ((uint64_t (*)(intptr_t(*)(), int*, ...))x264_checkasm_call)( (intptr_t(*)())func, &ok, __VA_ARGS__ ) +#else +#define call_a1_64 call_a1 +#endif + #define call_bench(func,cpu,...)\ if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\ {\ - uint32_t tsum = 0;\ + uint64_t tsum = 0;\ int tcount = 0;\ call_a1(func, __VA_ARGS__);\ for( int ti = 0; ti < (cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\ @@ -220,7 +280,7 @@ intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... ); func(__VA_ARGS__);\ func(__VA_ARGS__);\ t = read_time() - t;\ - if( t*tcount <= tsum*4 && ti > 0 )\ + if( (uint64_t)t*tcount <= tsum*4 && ti > 0 )\ {\ tsum += t;\ tcount++;\ @@ -238,6 +298,7 @@ intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... ); #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); }) #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); }) #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); }) +#define call_a64(func,...) ({ call_a2(func,__VA_ARGS__); call_a1_64(func,__VA_ARGS__); }) static int check_pixel( int cpu_ref, int cpu_new ) @@ -277,7 +338,7 @@ static int check_pixel( int cpu_ref, int cpu_new ) #define TEST_PIXEL( name, align ) \ ok = 1, used_asm = 0; \ - for( int i = 0; i < 8; i++ ) \ + for( int i = 0; i < ARRAY_ELEMS(pixel_c.name); i++ ) \ { \ int res_c, res_asm; \ if( pixel_asm.name[i] != pixel_ref.name[i] ) \ @@ -286,8 +347,8 @@ static int check_pixel( int cpu_ref, int cpu_new ) used_asm = 1; \ for( int j = 0; j < 64; j++ ) \ { \ - res_c = call_c( pixel_c.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \ - res_asm = call_a( pixel_asm.name[i], pbuf1, 16, pbuf2+j*!align, 64 ); \ + res_c = call_c( pixel_c.name[i], pbuf1, (intptr_t)16, pbuf2+j*!align, (intptr_t)64 ); \ + res_asm = call_a( pixel_asm.name[i], pbuf1, (intptr_t)16, pbuf2+j*!align, (intptr_t)64 ); \ if( res_c != res_asm ) \ { \ ok = 0; \ @@ -315,11 +376,49 @@ static int check_pixel( int cpu_ref, int cpu_new ) TEST_PIXEL( satd, 0 ); TEST_PIXEL( sa8d, 1 ); + ok = 1, used_asm = 0; + if( pixel_asm.sa8d_satd[PIXEL_16x16] != pixel_ref.sa8d_satd[PIXEL_16x16] ) + { + set_func_name( "sa8d_satd_%s", pixel_names[PIXEL_16x16] ); + used_asm = 1; + for( int j = 0; j < 64; j++ ) + { + uint32_t cost8_c = pixel_c.sa8d[PIXEL_16x16]( pbuf1, 16, pbuf2, 64 ); + uint32_t cost4_c = pixel_c.satd[PIXEL_16x16]( pbuf1, 16, pbuf2, 64 ); + uint64_t res_a = call_a64( pixel_asm.sa8d_satd[PIXEL_16x16], pbuf1, (intptr_t)16, pbuf2, (intptr_t)64 ); + uint32_t cost8_a = res_a; + uint32_t cost4_a = res_a >> 32; + if( cost8_a != cost8_c || cost4_a != cost4_c ) + { + ok = 0; + fprintf( stderr, "sa8d_satd [%d]: (%d,%d) != (%d,%d) [FAILED]\n", PIXEL_16x16, + cost8_c, cost4_c, cost8_a, cost4_a ); + break; + } + } + for( int j = 0; j < 0x1000 && ok; j += 256 ) \ + { + uint32_t cost8_c = pixel_c.sa8d[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 ); + uint32_t cost4_c = pixel_c.satd[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 ); + uint64_t res_a = pixel_asm.sa8d_satd[PIXEL_16x16]( pbuf3+j, 16, pbuf4+j, 16 ); + uint32_t cost8_a = res_a; + uint32_t cost4_a = res_a >> 32; + if( cost8_a != cost8_c || cost4_a != cost4_c ) + { + ok = 0; + fprintf( stderr, "sa8d_satd [%d]: overflow (%d,%d) != (%d,%d) [FAILED]\n", PIXEL_16x16, + cost8_c, cost4_c, cost8_a, cost4_a ); + } + } + } + report( "pixel sa8d_satd :" ); + #define TEST_PIXEL_X( N ) \ ok = 1; used_asm = 0; \ for( int i = 0; i < 7; i++ ) \ { \ - int res_c[4]={0}, res_asm[4]={0}; \ + ALIGNED_16( int res_c[4] ) = {0}; \ + ALIGNED_16( int res_asm[4] ) = {0}; \ if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \ { \ set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \ @@ -327,17 +426,17 @@ static int check_pixel( int cpu_ref, int cpu_new ) for( int j = 0; j < 64; j++ ) \ { \ pixel *pix2 = pbuf2+j; \ - res_c[0] = pixel_c.sad[i]( pbuf1, 16, pix2, 64 ); \ + res_c[0] = pixel_c.sad[i]( pbuf1, 16, pix2, 64 ); \ res_c[1] = pixel_c.sad[i]( pbuf1, 16, pix2+6, 64 ); \ res_c[2] = pixel_c.sad[i]( pbuf1, 16, pix2+1, 64 ); \ if( N == 4 ) \ { \ res_c[3] = pixel_c.sad[i]( pbuf1, 16, pix2+10, 64 ); \ - call_a( pixel_asm.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \ + call_a( pixel_asm.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, (intptr_t)64, res_asm ); \ } \ else \ - call_a( pixel_asm.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \ - if( memcmp(res_c, res_asm, sizeof(res_c)) ) \ + call_a( pixel_asm.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, (intptr_t)64, res_asm ); \ + if( memcmp(res_c, res_asm, N*sizeof(int)) ) \ { \ ok = 0; \ fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \ @@ -345,9 +444,9 @@ static int check_pixel( int cpu_ref, int cpu_new ) res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \ } \ if( N == 4 ) \ - call_c2( pixel_c.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, 64, res_asm ); \ + call_c2( pixel_c.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, (intptr_t)64, res_asm ); \ else \ - call_c2( pixel_c.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, 64, res_asm ); \ + call_c2( pixel_c.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, (intptr_t)64, res_asm ); \ } \ } \ } \ @@ -362,8 +461,8 @@ static int check_pixel( int cpu_ref, int cpu_new ) set_func_name( "%s_%s", "var", pixel_names[i] ); \ used_asm = 1; \ /* abi-check wrapper can't return uint64_t, so separate it from return value check */ \ - call_c1( pixel_c.var[i], pbuf1, 16 ); \ - call_a1( pixel_asm.var[i], pbuf1, 16 ); \ + call_c1( pixel_c.var[i], pbuf1, 16 ); \ + call_a1( pixel_asm.var[i], pbuf1, (intptr_t)16 ); \ uint64_t res_c = pixel_c.var[i]( pbuf1, 16 ); \ uint64_t res_asm = pixel_asm.var[i]( pbuf1, 16 ); \ if( res_c != res_asm ) \ @@ -371,8 +470,8 @@ static int check_pixel( int cpu_ref, int cpu_new ) ok = 0; \ 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) ); \ } \ - call_c2( pixel_c.var[i], pbuf1, 16 ); \ - call_a2( pixel_asm.var[i], pbuf1, 16 ); \ + call_c2( pixel_c.var[i], pbuf1, (intptr_t)16 ); \ + call_a2( pixel_asm.var[i], pbuf1, (intptr_t)16 ); \ } ok = 1; used_asm = 0; @@ -387,8 +486,8 @@ static int check_pixel( int cpu_ref, int cpu_new ) int res_c, res_asm, ssd_c, ssd_asm; \ set_func_name( "%s_%s", "var2", pixel_names[i] ); \ used_asm = 1; \ - res_c = call_c( pixel_c.var2[i], pbuf1, 16, pbuf2, 16, &ssd_c ); \ - res_asm = call_a( pixel_asm.var2[i], pbuf1, 16, pbuf2, 16, &ssd_asm ); \ + res_c = call_c( pixel_c.var2[i], pbuf1, (intptr_t)16, pbuf2, (intptr_t)16, &ssd_c ); \ + res_asm = call_a( pixel_asm.var2[i], pbuf1, (intptr_t)16, pbuf2, (intptr_t)16, &ssd_asm ); \ if( res_c != res_asm || ssd_c != ssd_asm ) \ { \ ok = 0; \ @@ -410,8 +509,8 @@ static int check_pixel( int cpu_ref, int cpu_new ) for( int j = 0; j < 32; j++ ) { pixel *pix = (j&16 ? pbuf1 : pbuf3) + (j&15)*256; - call_c1( pixel_c.hadamard_ac[i], pbuf1, 16 ); - call_a1( pixel_asm.hadamard_ac[i], pbuf1, 16 ); + call_c1( pixel_c.hadamard_ac[i], pbuf1, (intptr_t)16 ); + call_a1( pixel_asm.hadamard_ac[i], pbuf1, (intptr_t)16 ); uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 ); uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 ); if( rc != ra ) @@ -421,8 +520,8 @@ static int check_pixel( int cpu_ref, int cpu_new ) break; } } - call_c2( pixel_c.hadamard_ac[i], pbuf1, 16 ); - call_a2( pixel_asm.hadamard_ac[i], pbuf1, 16 ); + call_c2( pixel_c.hadamard_ac[i], pbuf1, (intptr_t)16 ); + call_a2( pixel_asm.hadamard_ac[i], pbuf1, (intptr_t)16 ); } report( "pixel hadamard_ac :" ); @@ -441,8 +540,8 @@ static int check_pixel( int cpu_ref, int cpu_new ) for( int j = 0; j < 2 && ok; j++ ) { pixel *p = j ? pbuf4 : pbuf1; - res_c = call_c( pixel_c.vsad, p, 16, h ); - res_asm = call_a( pixel_asm.vsad, p, 16, h ); + res_c = call_c( pixel_c.vsad, p, (intptr_t)16, h ); + res_asm = call_a( pixel_asm.vsad, p, (intptr_t)16, h ); if( res_c != res_asm ) { ok = 0; @@ -454,10 +553,26 @@ static int check_pixel( int cpu_ref, int cpu_new ) } report( "pixel vsad :" ); + ok = 1; used_asm = 0; + if( pixel_asm.asd8 != pixel_ref.asd8 ) + { + set_func_name( "asd8" ); + used_asm = 1; + int res_c = call_c( pixel_c.asd8, pbuf1, (intptr_t)8, pbuf2, (intptr_t)8, 16 ); + int res_a = call_a( pixel_asm.asd8, pbuf1, (intptr_t)8, pbuf2, (intptr_t)8, 16 ); + if( res_c != res_a ) + { + ok = 0; + fprintf( stderr, "asd: %d != %d\n", res_c, res_a ); + } + } + report( "pixel asd :" ); + #define TEST_INTRA_X3( name, i8x8, ... ) \ if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \ { \ - int res_c[3], res_asm[3]; \ + ALIGNED_16( int res_c[3] ); \ + ALIGNED_16( int res_asm[3] ); \ set_func_name( #name ); \ used_asm = 1; \ call_c( pixel_c.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_c ); \ @@ -547,7 +662,7 @@ static int check_pixel( int cpu_ref, int cpu_new ) } \ predict_8x8[res_c>>16]( fdec1, edge ); \ int res_a = call_a( pixel_asm.name, fenc, fdec2, edge, bitcosts+8-pred_mode, satds_a ); \ - if( res_c != res_a || memcmp(satds_c, satds_a, sizeof(satds_c)) ) \ + if( res_c != res_a || memcmp(satds_c, satds_a, 16 * sizeof(*satds_c)) ) \ { \ ok = 0; \ fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \ @@ -614,16 +729,19 @@ static int check_pixel( int cpu_ref, int cpu_new ) used_asm = 1; set_func_name( "ssd_nv12" ); uint64_t res_u_c, res_v_c, res_u_a, res_v_a; - pixel_c.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c ); - pixel_asm.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a ); - if( res_u_c != res_u_a || res_v_c != res_v_a ) + for( int w = 8; w <= 360; w += 8 ) { - ok = 0; - fprintf( stderr, "ssd_nv12: %"PRIu64",%"PRIu64" != %"PRIu64",%"PRIu64"\n", - res_u_c, res_v_c, res_u_a, res_v_a ); + pixel_c.ssd_nv12_core( pbuf1, 368, pbuf2, 368, w, 8, &res_u_c, &res_v_c ); + pixel_asm.ssd_nv12_core( pbuf1, 368, pbuf2, 368, w, 8, &res_u_a, &res_v_a ); + if( res_u_c != res_u_a || res_v_c != res_v_a ) + { + ok = 0; + fprintf( stderr, "ssd_nv12: %"PRIu64",%"PRIu64" != %"PRIu64",%"PRIu64"\n", + res_u_c, res_v_c, res_u_a, res_v_a ); + } } - call_c( pixel_c.ssd_nv12_core, pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c ); - call_a( pixel_asm.ssd_nv12_core, pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a ); + call_c( pixel_c.ssd_nv12_core, pbuf1, (intptr_t)368, pbuf2, (intptr_t)368, 360, 8, &res_u_c, &res_v_c ); + call_a( pixel_asm.ssd_nv12_core, pbuf1, (intptr_t)368, pbuf2, (intptr_t)368, 360, 8, &res_u_a, &res_v_a ); } report( "ssd_nv12 :" ); @@ -643,11 +761,14 @@ static int check_pixel( int cpu_ref, int cpu_new ) fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a ); } set_func_name( "ssim_core" ); - call_c2( pixel_c.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums ); - call_a2( pixel_asm.ssim_4x4x2_core, pbuf1+2, 32, pbuf2+2, 32, sums ); + call_c( pixel_c.ssim_4x4x2_core, pbuf1+2, (intptr_t)32, pbuf2+2, (intptr_t)32, sums ); + call_a( pixel_asm.ssim_4x4x2_core, pbuf1+2, (intptr_t)32, pbuf2+2, (intptr_t)32, sums ); set_func_name( "ssim_end" ); call_c2( pixel_c.ssim_end4, sums, sums, 4 ); call_a2( pixel_asm.ssim_end4, sums, sums, 4 ); + /* check incorrect assumptions that 32-bit ints are zero-extended to 64-bit */ + call_c1( pixel_c.ssim_end4, sums, sums, 3 ); + call_a1( pixel_asm.ssim_end4, sums, sums, 3 ); report( "ssim :" ); } @@ -659,8 +780,8 @@ static int check_pixel( int cpu_ref, int cpu_new ) { ALIGNED_16( uint16_t sums[72] ); ALIGNED_16( int dc[4] ); - ALIGNED_16( int16_t mvs_a[32] ); - ALIGNED_16( int16_t mvs_c[32] ); + ALIGNED_16( int16_t mvs_a[48] ); + ALIGNED_16( int16_t mvs_c[48] ); int mvn_a, mvn_c; int thresh = rand() & 0x3fff; set_func_name( "esa_ads" ); @@ -695,10 +816,10 @@ static int check_dct( int cpu_ref, int cpu_new ) x264_dct_function_t dct_asm; x264_quant_function_t qf; int ret = 0, ok, used_asm, interlace = 0; - ALIGNED_16( dctcoef dct1[16][16] ); - ALIGNED_16( dctcoef dct2[16][16] ); - ALIGNED_16( dctcoef dct4[16][16] ); - ALIGNED_16( dctcoef dct8[4][64] ); + ALIGNED_ARRAY_N( dctcoef, dct1, [16],[16] ); + ALIGNED_ARRAY_N( dctcoef, dct2, [16],[16] ); + ALIGNED_ARRAY_N( dctcoef, dct4, [16],[16] ); + ALIGNED_ARRAY_N( dctcoef, dct8, [4],[64] ); ALIGNED_16( dctcoef dctdc[2][8] ); x264_t h_buf; x264_t *h = &h_buf; @@ -908,12 +1029,24 @@ static int check_dct( int cpu_ref, int cpu_new ) { \ set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \ used_asm = 1; \ - memcpy(dct, buf1, size*sizeof(dctcoef)); \ + for( int i = 0; i < size*size; i++ ) \ + dct[i] = i; \ call_c( zigzag_c[interlace].name, t1, dct ); \ call_a( zigzag_asm[interlace].name, t2, dct ); \ - if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \ + if( memcmp( t1, t2, size*size*sizeof(dctcoef) ) ) \ { \ ok = 0; \ + for( int i = 0; i < 2; i++ ) \ + { \ + dctcoef *d = (dctcoef*)(i ? t2 : t1); \ + for( int j = 0; j < size; j++ ) \ + { \ + for( int k = 0; k < size; k++ ) \ + fprintf( stderr, "%2d ", d[k+j*8] ); \ + fprintf( stderr, "\n" ); \ + } \ + fprintf( stderr, "\n" ); \ + } \ fprintf( stderr, #name " [FAILED]\n" ); \ } \ } @@ -981,7 +1114,7 @@ static int check_dct( int cpu_ref, int cpu_new ) call_a( zigzag_asm[interlace].name, t2, dct, buf4 ); \ if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( buf3, buf4, 10 ) ) \ { \ - ok = 0; \ + 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;\ } \ } \ } @@ -991,15 +1124,16 @@ static int check_dct( int cpu_ref, int cpu_new ) x264_zigzag_init( cpu_new, &zigzag_asm[0], &zigzag_asm[1] ); ok = 1; used_asm = 0; - TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct1[0], 64 ); + TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct8[0], 64 ); report( "zigzag_interleave :" ); for( interlace = 0; interlace <= 1; interlace++ ) { ok = 1; used_asm = 0; - TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 ); - TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16 ); + TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, dct8[0], 8 ); + TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 4 ); TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 ); + TEST_ZIGZAG_SUB( sub_8x8, level1, level2, 64 ); TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 ); report( interlace ? "zigzag_field :" : "zigzag_frame :" ); } @@ -1024,9 +1158,9 @@ static int check_mc( int cpu_ref, int cpu_new ) int ret = 0, ok, used_asm; - x264_mc_init( 0, &mc_c ); - x264_mc_init( cpu_ref, &mc_ref ); - x264_mc_init( cpu_new, &mc_a ); + x264_mc_init( 0, &mc_c, 0 ); + x264_mc_init( cpu_ref, &mc_ref, 0 ); + x264_mc_init( cpu_new, &mc_a, 0 ); x264_pixel_init( 0, &pixf ); #define MC_TEST_LUMA( w, h ) \ @@ -1037,8 +1171,8 @@ static int check_mc( int cpu_ref, int cpu_new ) used_asm = 1; \ for( int i = 0; i < 1024; i++ ) \ pbuf3[i] = pbuf4[i] = 0xCD; \ - call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \ - call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h, weight ); \ + call_c( mc_c.mc_luma, dst1, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \ + call_a( mc_a.mc_luma, dst2, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \ if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \ { \ fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d] [FAILED]\n", dx, dy, w, h ); \ @@ -1048,15 +1182,15 @@ static int check_mc( int cpu_ref, int cpu_new ) if( mc_a.get_ref != mc_ref.get_ref ) \ { \ pixel *ref = dst2; \ - int ref_stride = 32; \ + intptr_t ref_stride = 32; \ int w_checked = ( ( sizeof(pixel) == 2 && (w == 12 || w == 20)) ? w-2 : w ); \ const x264_weight_t *weight = x264_weight_none; \ set_func_name( "get_ref_%dx%d", w_checked, h ); \ used_asm = 1; \ for( int i = 0; i < 1024; i++ ) \ pbuf3[i] = pbuf4[i] = 0xCD; \ - call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \ - ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h, weight ); \ + call_c( mc_c.mc_luma, dst1, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \ + ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, (intptr_t)64, dx, dy, w, h, weight ); \ for( int i = 0; i < h; i++ ) \ if( memcmp( dst1+i*32, ref+i*ref_stride, w_checked * sizeof(pixel) ) ) \ { \ @@ -1073,14 +1207,14 @@ static int check_mc( int cpu_ref, int cpu_new ) used_asm = 1; \ for( int i = 0; i < 1024; i++ ) \ pbuf3[i] = pbuf4[i] = 0xCD; \ - call_c( mc_c.mc_chroma, dst1, dst1+8, 16, src, 64, dx, dy, w, h ); \ - call_a( mc_a.mc_chroma, dst2, dst2+8, 16, src, 64, dx, dy, w, h ); \ + call_c( mc_c.mc_chroma, dst1, dst1+8, (intptr_t)16, src, (intptr_t)64, dx, dy, w, h ); \ + call_a( mc_a.mc_chroma, dst2, dst2+8, (intptr_t)16, src, (intptr_t)64, dx, dy, w, h ); \ /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \ for( int j = 0; j < h; j++ ) \ for( int i = w; i < 8; i++ ) \ { \ dst2[i+j*16+8] = dst1[i+j*16+8]; \ - dst2[i+j*16] = dst1[i+j*16]; \ + dst2[i+j*16 ] = dst1[i+j*16 ]; \ } \ if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \ { \ @@ -1124,7 +1258,6 @@ static int check_mc( int cpu_ref, int cpu_new ) #define MC_TEST_AVG( name, weight ) \ { \ - ok = 1, used_asm = 0; \ for( int i = 0; i < 12; i++ ) \ { \ memcpy( pbuf3, pbuf1+320, 320 * sizeof(pixel) ); \ @@ -1133,26 +1266,26 @@ static int check_mc( int cpu_ref, int cpu_new ) { \ set_func_name( "%s_%s", #name, pixel_names[i] ); \ used_asm = 1; \ - call_c1( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \ - call_a1( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \ + call_c1( mc_c.name[i], pbuf3, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \ + call_a1( mc_a.name[i], pbuf4, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \ if( memcmp( pbuf3, pbuf4, 320 * sizeof(pixel) ) ) \ { \ ok = 0; \ fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \ } \ - call_c2( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \ - call_a2( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \ + call_c2( mc_c.name[i], pbuf3, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \ + call_a2( mc_a.name[i], pbuf4, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \ } \ } \ } + ok = 1, used_asm = 0; for( int w = -63; w <= 127 && ok; w++ ) MC_TEST_AVG( avg, w ); report( "mc wpredb :" ); #define MC_TEST_WEIGHT( name, weight, aligned ) \ int align_off = (aligned ? 0 : rand()%16); \ - ok = 1, used_asm = 0; \ for( int i = 1; i <= 5; i++ ) \ { \ ALIGNED_16( pixel buffC[640] ); \ @@ -1169,9 +1302,9 @@ static int check_mc( int cpu_ref, int cpu_new ) { \ set_func_name( "%s_w%d", #name, j ); \ used_asm = 1; \ - call_c1( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \ + call_c1( mc_c.weight[i], buffC, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \ mc_a.weight_cache(&ha, &weight); \ - call_a1( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \ + call_a1( weight.weightfn[i], buffA, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \ for( int k = 0; k < 16; k++ ) \ if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \ { \ @@ -1179,8 +1312,12 @@ static int check_mc( int cpu_ref, int cpu_new ) fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \ break; \ } \ - call_c2( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \ - call_a2( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \ + /* omit unlikely high scales for benchmarking */ \ + if( (s << (8-d)) < 512 ) \ + { \ + call_c2( mc_c.weight[i], buffC, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \ + call_a2( weight.weightfn[i], buffA, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \ + } \ } \ } @@ -1232,8 +1369,8 @@ static int check_mc( int cpu_ref, int cpu_new ) used_asm = 1; memset( pbuf3, 0, 64*height ); memset( pbuf4, 0, 64*height ); - call_c( mc_c.store_interleave_chroma, pbuf3, 64, pbuf1, pbuf1+16, height ); - call_a( mc_a.store_interleave_chroma, pbuf4, 64, pbuf1, pbuf1+16, height ); + call_c( mc_c.store_interleave_chroma, pbuf3, (intptr_t)64, pbuf1, pbuf1+16, height ); + call_a( mc_a.store_interleave_chroma, pbuf4, (intptr_t)64, pbuf1, pbuf1+16, height ); if( memcmp( pbuf3, pbuf4, 64*height ) ) { ok = 0; @@ -1245,8 +1382,8 @@ static int check_mc( int cpu_ref, int cpu_new ) { set_func_name( "load_deinterleave_chroma_fenc" ); used_asm = 1; - call_c( mc_c.load_deinterleave_chroma_fenc, pbuf3, pbuf1, 64, height ); - call_a( mc_a.load_deinterleave_chroma_fenc, pbuf4, pbuf1, 64, height ); + call_c( mc_c.load_deinterleave_chroma_fenc, pbuf3, pbuf1, (intptr_t)64, height ); + call_a( mc_a.load_deinterleave_chroma_fenc, pbuf4, pbuf1, (intptr_t)64, height ); if( memcmp( pbuf3, pbuf4, FENC_STRIDE*height ) ) { ok = 0; @@ -1258,8 +1395,8 @@ static int check_mc( int cpu_ref, int cpu_new ) { set_func_name( "load_deinterleave_chroma_fdec" ); used_asm = 1; - call_c( mc_c.load_deinterleave_chroma_fdec, pbuf3, pbuf1, 64, height ); - call_a( mc_a.load_deinterleave_chroma_fdec, pbuf4, pbuf1, 64, height ); + call_c( mc_c.load_deinterleave_chroma_fdec, pbuf3, pbuf1, (intptr_t)64, height ); + call_a( mc_a.load_deinterleave_chroma_fdec, pbuf4, pbuf1, (intptr_t)64, height ); if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*height ) ) { ok = 0; @@ -1282,8 +1419,8 @@ static int check_mc( int cpu_ref, int cpu_new ) { int w = plane_specs[i].w; int h = plane_specs[i].h; - int src_stride = plane_specs[i].src_stride; - int dst_stride = (w + 127) & ~63; + intptr_t src_stride = plane_specs[i].src_stride; + intptr_t dst_stride = (w + 127) & ~63; assert( dst_stride * h <= 0x1000 ); pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1); memset( pbuf3, 0, 0x1000*sizeof(pixel) ); @@ -1294,7 +1431,33 @@ static int check_mc( int cpu_ref, int cpu_new ) if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(pixel) ) ) { ok = 0; - fprintf( stderr, "plane_copy FAILED: w=%d h=%d stride=%d\n", w, h, src_stride ); + fprintf( stderr, "plane_copy FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride ); + break; + } + } + } + + if( mc_a.plane_copy_swap != mc_ref.plane_copy_swap ) + { + set_func_name( "plane_copy_swap" ); + used_asm = 1; + for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ ) + { + int w = (plane_specs[i].w + 1) >> 1; + int h = plane_specs[i].h; + intptr_t src_stride = plane_specs[i].src_stride; + intptr_t dst_stride = (2*w + 127) & ~63; + assert( dst_stride * h <= 0x1000 ); + pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1); + memset( pbuf3, 0, 0x1000*sizeof(pixel) ); + memset( pbuf4, 0, 0x1000*sizeof(pixel) ); + call_c( mc_c.plane_copy_swap, pbuf3, dst_stride, src1, src_stride, w, h ); + call_a( mc_a.plane_copy_swap, pbuf4, dst_stride, src1, src_stride, w, h ); + for( int y = 0; y < h; y++ ) + if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) ) + { + ok = 0; + fprintf( stderr, "plane_copy_swap FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride ); break; } } @@ -1308,8 +1471,8 @@ static int check_mc( int cpu_ref, int cpu_new ) { int w = (plane_specs[i].w + 1) >> 1; int h = plane_specs[i].h; - int src_stride = (plane_specs[i].src_stride + 1) >> 1; - int dst_stride = (2*w + 127) & ~63; + intptr_t src_stride = (plane_specs[i].src_stride + 1) >> 1; + intptr_t dst_stride = (2*w + 127) & ~63; assert( dst_stride * h <= 0x1000 ); pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1); memset( pbuf3, 0, 0x1000*sizeof(pixel) ); @@ -1320,7 +1483,7 @@ static int check_mc( int cpu_ref, int cpu_new ) if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) ) { ok = 0; - fprintf( stderr, "plane_copy_interleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride ); + fprintf( stderr, "plane_copy_interleave FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride ); break; } } @@ -1334,9 +1497,9 @@ static int check_mc( int cpu_ref, int cpu_new ) { int w = (plane_specs[i].w + 1) >> 1; int h = plane_specs[i].h; - int dst_stride = w; - int src_stride = (2*w + 127) & ~63; - int offv = (dst_stride*h + 31) & ~15; + intptr_t dst_stride = w; + intptr_t src_stride = (2*w + 127) & ~63; + intptr_t offv = (dst_stride*h + 31) & ~15; memset( pbuf3, 0, 0x1000 ); memset( pbuf4, 0, 0x1000 ); call_c( mc_c.plane_copy_deinterleave, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf1, src_stride, w, h ); @@ -1346,13 +1509,71 @@ static int check_mc( int cpu_ref, int cpu_new ) memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w ) ) { ok = 0; - fprintf( stderr, "plane_copy_deinterleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride ); + fprintf( stderr, "plane_copy_deinterleave FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride ); break; } } } + + if( mc_a.plane_copy_deinterleave_rgb != mc_ref.plane_copy_deinterleave_rgb ) + { + set_func_name( "plane_copy_deinterleave_rgb" ); + used_asm = 1; + for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ ) + { + int w = (plane_specs[i].w + 2) >> 2; + int h = plane_specs[i].h; + intptr_t src_stride = plane_specs[i].src_stride; + intptr_t dst_stride = ALIGN( w, 16 ); + intptr_t offv = dst_stride*h + 16; + + for( int pw = 3; pw <= 4; pw++ ) + { + memset( pbuf3, 0, 0x1000 ); + memset( pbuf4, 0, 0x1000 ); + call_c( mc_c.plane_copy_deinterleave_rgb, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf3+2*offv, dst_stride, pbuf1, src_stride, pw, w, h ); + call_a( mc_a.plane_copy_deinterleave_rgb, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf4+2*offv, dst_stride, pbuf1, src_stride, pw, w, h ); + for( int y = 0; y < h; y++ ) + if( memcmp( pbuf3+y*dst_stride+0*offv, pbuf4+y*dst_stride+0*offv, w ) || + memcmp( pbuf3+y*dst_stride+1*offv, pbuf4+y*dst_stride+1*offv, w ) || + memcmp( pbuf3+y*dst_stride+2*offv, pbuf4+y*dst_stride+2*offv, w ) ) + { + ok = 0; + fprintf( stderr, "plane_copy_deinterleave_rgb FAILED: w=%d h=%d stride=%d pw=%d\n", w, h, (int)src_stride, pw ); + break; + } + } + } + } report( "plane_copy :" ); + if( mc_a.plane_copy_deinterleave_v210 != mc_ref.plane_copy_deinterleave_v210 ) + { + set_func_name( "plane_copy_deinterleave_v210" ); + ok = 1; used_asm = 1; + for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ ) + { + int w = (plane_specs[i].w + 1) >> 1; + int h = plane_specs[i].h; + intptr_t dst_stride = ALIGN( w, 16 ); + intptr_t src_stride = (w + 47) / 48 * 128 / sizeof(uint32_t); + intptr_t offv = dst_stride*h + 32; + memset( pbuf3, 0, 0x1000 ); + memset( pbuf4, 0, 0x1000 ); + call_c( mc_c.plane_copy_deinterleave_v210, pbuf3, dst_stride, pbuf3+offv, dst_stride, (uint32_t *)buf1, src_stride, w, h ); + call_a( mc_a.plane_copy_deinterleave_v210, pbuf4, dst_stride, pbuf4+offv, dst_stride, (uint32_t *)buf1, src_stride, w, h ); + for( int y = 0; y < h; y++ ) + if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(uint16_t) ) || + memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w*sizeof(uint16_t) ) ) + { + ok = 0; + fprintf( stderr, "plane_copy_deinterleave_v210 FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride ); + break; + } + } + report( "v210 :" ); + } + if( mc_a.hpel_filter != mc_ref.hpel_filter ) { pixel *srchpel = pbuf1+8+2*64; @@ -1363,8 +1584,8 @@ static int check_mc( int cpu_ref, int cpu_new ) ok = 1; used_asm = 1; memset( pbuf3, 0, 4096 * sizeof(pixel) ); memset( pbuf4, 0, 4096 * sizeof(pixel) ); - call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, 64, 48, 10, tmp ); - call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, 64, 48, 10, tmp ); + call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, (intptr_t)64, 48, 10, tmp ); + call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, (intptr_t)64, 48, 10, tmp ); for( int i = 0; i < 3; i++ ) for( int j = 0; j < 10; j++ ) //FIXME ideally the first pixels would match too, but they aren't actually used @@ -1389,23 +1610,24 @@ static int check_mc( int cpu_ref, int cpu_new ) pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 }; set_func_name( "lowres_init" ); ok = 1; used_asm = 1; - for( int w = 40; w <= 48; w += 8 ) + for( int w = 96; w <= 96+24; w += 8 ) { - int stride = (w+8)&~15; - call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 ); - call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 ); - for( int i = 0; i < 16; i++ ) + intptr_t stride = (w*2+31)&~31; + intptr_t stride_lowres = (w+31)&~31; + call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], stride, stride_lowres, w, 8 ); + call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], stride, stride_lowres, w, 8 ); + for( int i = 0; i < 8; i++ ) { for( int j = 0; j < 4; j++ ) - if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w * sizeof(pixel) ) ) + if( memcmp( dstc[j]+i*stride_lowres, dsta[j]+i*stride_lowres, w * sizeof(pixel) ) ) { ok = 0; fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i ); for( int k = 0; k < w; k++ ) - printf( "%d ", dstc[j][k+i*stride] ); + printf( "%d ", dstc[j][k+i*stride_lowres] ); printf( "\n" ); for( int k = 0; k < w; k++ ) - printf( "%d ", dsta[j][k+i*stride] ); + printf( "%d ", dsta[j][k+i*stride_lowres] ); printf( "\n" ); break; } @@ -1414,41 +1636,42 @@ static int check_mc( int cpu_ref, int cpu_new ) report( "lowres init :" ); } -#define INTEGRAL_INIT( name, size, ... )\ +#define INTEGRAL_INIT( name, size, offset, cmp_len, ... )\ if( mc_a.name != mc_ref.name )\ {\ - int stride = 80;\ + intptr_t stride = 96;\ set_func_name( #name );\ used_asm = 1;\ memcpy( buf3, buf1, size*2*stride );\ memcpy( buf4, buf1, size*2*stride );\ uint16_t *sum = (uint16_t*)buf3;\ - call_c1( mc_c.name, __VA_ARGS__ );\ + call_c1( mc_c.name, sum+offset, __VA_ARGS__ );\ sum = (uint16_t*)buf4;\ - call_a1( mc_a.name, __VA_ARGS__ );\ - if( memcmp( buf3, buf4, (stride-8)*2 ) \ + call_a1( mc_a.name, sum+offset, __VA_ARGS__ );\ + if( memcmp( buf3+2*offset, buf4+2*offset, cmp_len*2 )\ || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\ ok = 0;\ - call_c2( mc_c.name, __VA_ARGS__ );\ - call_a2( mc_a.name, __VA_ARGS__ );\ + call_c2( mc_c.name, sum+offset, __VA_ARGS__ );\ + call_a2( mc_a.name, sum+offset, __VA_ARGS__ );\ } ok = 1; used_asm = 0; - INTEGRAL_INIT( integral_init4h, 2, sum+stride, pbuf2, stride ); - INTEGRAL_INIT( integral_init8h, 2, sum+stride, pbuf2, stride ); - INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride ); - INTEGRAL_INIT( integral_init8v, 9, sum, stride ); + INTEGRAL_INIT( integral_init4h, 2, stride, stride-4, pbuf2, stride ); + INTEGRAL_INIT( integral_init8h, 2, stride, stride-8, pbuf2, stride ); + INTEGRAL_INIT( integral_init4v, 14, 0, stride-8, sum+9*stride, stride ); + INTEGRAL_INIT( integral_init8v, 9, 0, stride-8, stride ); report( "integral init :" ); + ok = 1; used_asm = 0; if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost ) { + used_asm = 1; x264_emms(); for( int i = 0; i < 10; i++ ) { - float fps_factor = (rand()&65535) / 256.; - ok = 1; used_asm = 1; - set_func_name( "mbtree_propagate" ); - int *dsta = (int*)buf3; - int *dstc = dsta+400; + float fps_factor = (rand()&65535) / 65535.0f; + set_func_name( "mbtree_propagate_cost" ); + int16_t *dsta = (int16_t*)buf3; + int16_t *dstc = dsta+400; uint16_t *prop = (uint16_t*)buf1; uint16_t *intra = (uint16_t*)buf4; uint16_t *inter = intra+128; @@ -1470,17 +1693,65 @@ static int check_mc( int cpu_ref, int cpu_new ) { ok &= abs( dstc[j]-dsta[j] ) <= 1 || fabs( (double)dstc[j]/dsta[j]-1 ) < 1e-4; if( !ok ) - fprintf( stderr, "mbtree_propagate FAILED: %f !~= %f\n", (double)dstc[j], (double)dsta[j] ); + fprintf( stderr, "mbtree_propagate_cost FAILED: %f !~= %f\n", (double)dstc[j], (double)dsta[j] ); } } - report( "mbtree propagate :" ); } + if( mc_a.mbtree_propagate_list != mc_ref.mbtree_propagate_list ) + { + used_asm = 1; + for( int i = 0; i < 8; i++ ) + { + set_func_name( "mbtree_propagate_list" ); + x264_t h; + int height = 4; + int width = 128; + int size = width*height; + h.mb.i_mb_stride = width; + h.mb.i_mb_width = width; + h.mb.i_mb_height = height; + + uint16_t *ref_costsc = (uint16_t*)buf3; + uint16_t *ref_costsa = (uint16_t*)buf4; + int16_t (*mvs)[2] = (int16_t(*)[2])(ref_costsc + size); + int16_t *propagate_amount = (int16_t*)(mvs + width); + uint16_t *lowres_costs = (uint16_t*)(propagate_amount + width); + h.scratch_buffer2 = (uint8_t*)(ref_costsa + size); + int bipred_weight = (rand()%63)+1; + int list = i&1; + for( int j = 0; j < size; j++ ) + ref_costsc[j] = ref_costsa[j] = rand()&32767; + for( int j = 0; j < width; j++ ) + { + static const uint8_t list_dist[2][8] = {{0,1,1,1,1,1,1,1},{1,1,3,3,3,3,3,2}}; + for( int k = 0; k < 2; k++ ) + mvs[j][k] = (rand()&127) - 64; + propagate_amount[j] = rand()&32767; + lowres_costs[j] = list_dist[list][rand()&7] << LOWRES_COST_SHIFT; + } + + call_c1( mc_c.mbtree_propagate_list, &h, ref_costsc, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list ); + call_a1( mc_a.mbtree_propagate_list, &h, ref_costsa, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list ); + + for( int j = 0; j < size && ok; j++ ) + { + ok &= abs(ref_costsa[j] - ref_costsc[j]) <= 1; + if( !ok ) + fprintf( stderr, "mbtree_propagate_list FAILED at %d: %d !~= %d\n", j, ref_costsc[j], ref_costsa[j] ); + } + + call_c2( mc_c.mbtree_propagate_list, &h, ref_costsc, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list ); + call_a2( mc_a.mbtree_propagate_list, &h, ref_costsa, mvs, propagate_amount, lowres_costs, bipred_weight, 0, width, list ); + } + } + report( "mbtree :" ); + if( mc_a.memcpy_aligned != mc_ref.memcpy_aligned ) { set_func_name( "memcpy_aligned" ); ok = 1; used_asm = 1; - for( int size = 16; size < 256; size += 16 ) + for( size_t size = 16; size < 256; size += 16 ) { memset( buf4, 0xAA, size + 1 ); call_c( mc_c.memcpy_aligned, buf3, buf1, size ); @@ -1488,7 +1759,7 @@ static int check_mc( int cpu_ref, int cpu_new ) if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA ) { ok = 0; - fprintf( stderr, "memcpy_aligned FAILED: size=%d\n", size ); + fprintf( stderr, "memcpy_aligned FAILED: size=%d\n", (int)size ); break; } } @@ -1499,7 +1770,7 @@ static int check_mc( int cpu_ref, int cpu_new ) { set_func_name( "memzero_aligned" ); ok = 1; used_asm = 1; - for( int size = 128; size < 1024; size += 128 ) + for( size_t size = 128; size < 1024; size += 128 ) { memset( buf4, 0xAA, size + 1 ); call_c( mc_c.memzero_aligned, buf3, size ); @@ -1507,7 +1778,7 @@ static int check_mc( int cpu_ref, int cpu_new ) if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA ) { ok = 0; - fprintf( stderr, "memzero_aligned FAILED: size=%d\n", size ); + fprintf( stderr, "memzero_aligned FAILED: size=%d\n", (int)size ); break; } } @@ -1545,7 +1816,7 @@ static int check_deblock( int cpu_ref, int cpu_new ) #define TEST_DEBLOCK( name, align, ... ) \ for( int i = 0; i < 36; i++ ) \ { \ - int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \ + intptr_t off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \ for( int j = 0; j < 1024; j++ ) \ /* two distributions of random to excersize different failure modes */ \ pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \ @@ -1554,16 +1825,16 @@ static int check_deblock( int cpu_ref, int cpu_new ) { \ set_func_name( #name ); \ used_asm = 1; \ - call_c1( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \ - call_a1( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \ + call_c1( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \ + call_a1( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \ if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \ { \ ok = 0; \ fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \ break; \ } \ - call_c2( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \ - call_a2( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \ + call_c2( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \ + call_a2( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \ } \ } @@ -1589,8 +1860,8 @@ static int check_deblock( int cpu_ref, int cpu_new ) ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] ); ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] ); ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] ); - ALIGNED_ARRAY_16( uint8_t, bs, [2],[2][8][4] ); - memset( bs, 99, sizeof(bs) ); + ALIGNED_ARRAY_N( uint8_t, bs, [2],[2][8][4] ); + memset( bs, 99, sizeof(uint8_t)*2*4*8*2 ); for( int j = 0; j < X264_SCAN8_SIZE; j++ ) nnz[j] = ((rand()&7) == 7) * rand() & 0xf; for( int j = 0; j < 2; j++ ) @@ -1603,7 +1874,7 @@ static int check_deblock( int cpu_ref, int cpu_new ) set_func_name( "deblock_strength" ); call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) ); call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) ); - if( memcmp( bs[0], bs[1], sizeof(bs[0]) ) ) + if( memcmp( bs[0], bs[1], sizeof(uint8_t)*2*4*8 ) ) { ok = 0; fprintf( stderr, "deblock_strength: [FAILED]\n" ); @@ -1633,11 +1904,11 @@ static int check_quant( int cpu_ref, int cpu_new ) x264_quant_function_t qf_c; x264_quant_function_t qf_ref; x264_quant_function_t qf_a; - ALIGNED_16( dctcoef dct1[64] ); - ALIGNED_16( dctcoef dct2[64] ); - ALIGNED_16( dctcoef dct3[8][16] ); - ALIGNED_16( dctcoef dct4[8][16] ); - ALIGNED_16( uint8_t cqm_buf[64] ); + ALIGNED_ARRAY_N( dctcoef, dct1,[64] ); + ALIGNED_ARRAY_N( dctcoef, dct2,[64] ); + ALIGNED_ARRAY_N( dctcoef, dct3,[8],[16] ); + ALIGNED_ARRAY_N( dctcoef, dct4,[8],[16] ); + ALIGNED_ARRAY_N( uint8_t, cqm_buf,[64] ); int ret = 0, ok, used_asm; int oks[3] = {1,1,1}, used_asms[3] = {0,0,0}; x264_t h_buf; @@ -1677,29 +1948,29 @@ static int check_quant( int cpu_ref, int cpu_new ) } h->param.rc.i_qp_min = 0; - h->param.rc.i_qp_max = QP_MAX; + h->param.rc.i_qp_max = QP_MAX_SPEC; x264_cqm_init( h ); x264_quant_init( h, 0, &qf_c ); x264_quant_init( h, cpu_ref, &qf_ref ); x264_quant_init( h, cpu_new, &qf_a ); -#define INIT_QUANT8(j) \ +#define INIT_QUANT8(j,max) \ { \ static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \ - for( int i = 0; i < 64; i++ ) \ + for( int i = 0; i < max; i++ ) \ { \ - unsigned int scale = (255*scale1d[i>>3]*scale1d[i&7])/16; \ - dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \ + unsigned int scale = (255*scale1d[(i>>3)&7]*scale1d[i&7])/16; \ + dct1[i] = dct2[i] = (j>>(i>>6))&1 ? (rand()%(2*scale+1))-scale : 0; \ } \ } -#define INIT_QUANT4(j) \ +#define INIT_QUANT4(j,max) \ { \ static const int scale1d[4] = {4,6,4,6}; \ - for( int i = 0; i < 16; i++ ) \ + for( int i = 0; i < max; i++ ) \ { \ - unsigned int scale = 255*scale1d[i>>2]*scale1d[i&3]; \ - dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \ + unsigned int scale = 255*scale1d[(i>>2)&3]*scale1d[i&3]; \ + dct1[i] = dct2[i] = (j>>(i>>4))&1 ? (rand()%(2*scale+1))-scale : 0; \ } \ } @@ -1729,34 +2000,36 @@ static int check_quant( int cpu_ref, int cpu_new ) } \ } -#define TEST_QUANT( qname, block, w ) \ +#define TEST_QUANT( qname, block, type, w, maxj ) \ if( qf_a.qname != qf_ref.qname ) \ { \ set_func_name( #qname ); \ used_asms[0] = 1; \ for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \ { \ - for( int j = 0; j < 2; j++ ) \ + for( int j = 0; j < maxj; j++ ) \ { \ - INIT_QUANT##w(j) \ - int result_c = call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \ - int result_a = call_a1( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \ + INIT_QUANT##type(j, w*w) \ + int result_c = call_c1( qf_c.qname, (void*)dct1, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \ + int result_a = call_a1( qf_a.qname, (void*)dct2, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \ if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \ { \ oks[0] = 0; \ fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \ break; \ } \ - call_c2( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \ - call_a2( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \ + call_c2( qf_c.qname, (void*)dct1, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \ + call_a2( qf_a.qname, (void*)dct2, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \ } \ } \ } - TEST_QUANT( quant_8x8, CQM_8IY, 8 ); - TEST_QUANT( quant_8x8, CQM_8PY, 8 ); - TEST_QUANT( quant_4x4, CQM_4IY, 4 ); - TEST_QUANT( quant_4x4, CQM_4PY, 4 ); + TEST_QUANT( quant_8x8, CQM_8IY, 8, 8, 2 ); + TEST_QUANT( quant_8x8, CQM_8PY, 8, 8, 2 ); + TEST_QUANT( quant_4x4, CQM_4IY, 4, 4, 2 ); + TEST_QUANT( quant_4x4, CQM_4PY, 4, 4, 2 ); + TEST_QUANT( quant_4x4x4, CQM_4IY, 4, 8, 16 ); + TEST_QUANT( quant_4x4x4, CQM_4PY, 4, 8, 16 ); TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] ); TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] ); @@ -1767,7 +2040,7 @@ static int check_quant( int cpu_ref, int cpu_new ) used_asms[1] = 1; \ for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \ { \ - INIT_QUANT##w(1) \ + INIT_QUANT##w(1, w*w) \ qf_c.qname( dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \ memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \ call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \ @@ -1919,11 +2192,11 @@ static int check_quant( int cpu_ref, int cpu_new ) memcpy( dct1, buf1, size*sizeof(dctcoef) ); memcpy( dct2, buf1, size*sizeof(dctcoef) ); memcpy( buf3+256, buf3, 256 ); - call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size ); + call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size ); call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size ); if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) ) ok = 0; - call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size ); + call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size ); call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size ); } } @@ -2020,8 +2293,7 @@ static int check_quant( int cpu_ref, int cpu_new ) int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \ if( result_c != result_a || runlevel_c.last != runlevel_a.last || \ runlevel_c.mask != runlevel_a.mask || \ - memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c) || \ - memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \ + memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c)) \ { \ ok = 0; \ fprintf( stderr, #name ": [FAILED]\n" ); \ @@ -2045,7 +2317,7 @@ static int check_intra( int cpu_ref, int cpu_new ) int ret = 0, ok = 1, used_asm = 0; ALIGNED_ARRAY_32( pixel, edge,[36] ); ALIGNED_ARRAY_32( pixel, edge2,[36] ); - ALIGNED_16( pixel fdec[FDEC_STRIDE*20] ); + ALIGNED_ARRAY_32( pixel, fdec,[FDEC_STRIDE*20] ); struct { x264_predict_t predict_16x16[4+3]; @@ -2093,12 +2365,16 @@ static int check_intra( int cpu_ref, int cpu_new ) {\ fprintf( stderr, #name "[%d] : [FAILED]\n", dir );\ ok = 0;\ - for( int k = -1; k < 16; k++ )\ - printf( "%2x ", edge[16+k] );\ - printf( "\n" );\ + if( ip_c.name == (void *)ip_c.predict_8x8 )\ + {\ + for( int k = -1; k < 16; k++ )\ + printf( "%2x ", edge[16+k] );\ + printf( "\n" );\ + }\ for( int j = 0; j < h; j++ )\ {\ - printf( "%2x ", edge[14-j] );\ + if( ip_c.name == (void *)ip_c.predict_8x8 )\ + printf( "%2x ", edge[14-j] );\ for( int k = 0; k < w; k++ )\ printf( "%2x ", pbuf4[48+k+j*FDEC_STRIDE] );\ printf( "\n" );\ @@ -2106,7 +2382,8 @@ static int check_intra( int cpu_ref, int cpu_new ) printf( "\n" );\ for( int j = 0; j < h; j++ )\ {\ - printf( " " );\ + if( ip_c.name == (void *)ip_c.predict_8x8 )\ + printf( " " );\ for( int k = 0; k < w; k++ )\ printf( "%2x ", pbuf3[48+k+j*FDEC_STRIDE] );\ printf( "\n" );\ @@ -2210,19 +2487,107 @@ static void run_cabac_terminal_##cpu( x264_t *h, uint8_t *dst )\ DECL_CABAC(c) #if HAVE_MMX DECL_CABAC(asm) +#elif defined(ARCH_AARCH64) +DECL_CABAC(asm) #else #define run_cabac_decision_asm run_cabac_decision_c #define run_cabac_bypass_asm run_cabac_bypass_c #define run_cabac_terminal_asm run_cabac_terminal_c #endif +extern const uint8_t x264_count_cat_m1[14]; +void x264_cabac_block_residual_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l ); +void x264_cabac_block_residual_8x8_rd_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l ); +void x264_cabac_block_residual_rd_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l ); + static int check_cabac( int cpu_ref, int cpu_new ) { - int ret = 0, ok, used_asm = 1; + int ret = 0, ok = 1, used_asm = 0; x264_t h; h.sps->i_chroma_format_idc = 3; + + x264_bitstream_function_t bs_ref; + x264_bitstream_function_t bs_a; + x264_bitstream_init( cpu_ref, &bs_ref ); + x264_bitstream_init( cpu_new, &bs_a ); + x264_quant_init( &h, cpu_new, &h.quantf ); + h.quantf.coeff_last[DCT_CHROMA_DC] = h.quantf.coeff_last4; + +#define CABAC_RESIDUAL(name, start, end, rd)\ +{\ + if( bs_a.name##_internal && (bs_a.name##_internal != bs_ref.name##_internal || (cpu_new&X264_CPU_SSE2_IS_SLOW)) )\ + {\ + used_asm = 1;\ + set_func_name( #name );\ + for( int i = 0; i < 2; i++ )\ + {\ + for( intptr_t ctx_block_cat = start; ctx_block_cat <= end; ctx_block_cat++ )\ + {\ + for( int j = 0; j < 256; j++ )\ + {\ + ALIGNED_ARRAY_N( dctcoef, dct, [2],[64] );\ + uint8_t bitstream[2][1<<16];\ + static const uint8_t ctx_ac[14] = {0,1,0,0,1,0,0,1,0,0,0,1,0,0};\ + int ac = ctx_ac[ctx_block_cat];\ + int nz = 0;\ + while( !nz )\ + {\ + for( int k = 0; k <= x264_count_cat_m1[ctx_block_cat]; k++ )\ + {\ + /* Very rough distribution that covers possible inputs */\ + int rnd = rand();\ + int coef = !(rnd&3);\ + coef += !(rnd& 15) * (rand()&0x0006);\ + coef += !(rnd& 63) * (rand()&0x0008);\ + coef += !(rnd& 255) * (rand()&0x00F0);\ + coef += !(rnd&1023) * (rand()&0x7F00);\ + nz |= dct[0][ac+k] = dct[1][ac+k] = coef * ((rand()&1) ? 1 : -1);\ + }\ + }\ + h.mb.b_interlaced = i;\ + x264_cabac_t cb[2];\ + x264_cabac_context_init( &h, &cb[0], SLICE_TYPE_P, 26, 0 );\ + x264_cabac_context_init( &h, &cb[1], SLICE_TYPE_P, 26, 0 );\ + x264_cabac_encode_init( &cb[0], bitstream[0], bitstream[0]+0xfff0 );\ + x264_cabac_encode_init( &cb[1], bitstream[1], bitstream[1]+0xfff0 );\ + cb[0].f8_bits_encoded = 0;\ + cb[1].f8_bits_encoded = 0;\ + if( !rd ) memcpy( bitstream[1], bitstream[0], 0x400 );\ + call_c1( x264_##name##_c, &h, &cb[0], ctx_block_cat, dct[0]+ac );\ + call_a1( bs_a.name##_internal, dct[1]+ac, i, ctx_block_cat, &cb[1] );\ + ok = cb[0].f8_bits_encoded == cb[1].f8_bits_encoded && !memcmp(cb[0].state, cb[1].state, 1024);\ + if( !rd ) ok |= !memcmp( bitstream[1], bitstream[0], 0x400 ) && !memcmp( &cb[1], &cb[0], offsetof(x264_cabac_t, p_start) );\ + if( !ok )\ + {\ + fprintf( stderr, #name " : [FAILED] ctx_block_cat %d", (int)ctx_block_cat );\ + if( rd && cb[0].f8_bits_encoded != cb[1].f8_bits_encoded )\ + fprintf( stderr, " (%d != %d)", cb[0].f8_bits_encoded, cb[1].f8_bits_encoded );\ + fprintf( stderr, "\n");\ + goto name##fail;\ + }\ + if( (j&15) == 0 )\ + {\ + call_c2( x264_##name##_c, &h, &cb[0], ctx_block_cat, dct[0]+ac );\ + call_a2( bs_a.name##_internal, dct[1]+ac, i, ctx_block_cat, &cb[1] );\ + }\ + }\ + }\ + }\ + }\ +}\ +name##fail: + + CABAC_RESIDUAL( cabac_block_residual, 0, DCT_LUMA_8x8, 0 ) + report( "cabac residual:" ); + + ok = 1; used_asm = 0; + CABAC_RESIDUAL( cabac_block_residual_rd, 0, DCT_LUMA_8x8-1, 1 ) + CABAC_RESIDUAL( cabac_block_residual_8x8_rd, DCT_LUMA_8x8, DCT_LUMA_8x8, 1 ) + report( "cabac residual rd:" ); + if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm ) - return 0; + return ret; + ok = 1; used_asm = 0; x264_cabac_init( &h ); set_func_name( "cabac_encode_decision" ); @@ -2315,7 +2680,7 @@ static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name ) { *cpu_ref = *cpu_new; *cpu_new |= flags; -#if BROKEN_STACK_ALIGNMENT +#if STACK_ALIGNMENT < 16 *cpu_new |= X264_CPU_STACK_MOD4; #endif if( *cpu_new & X264_CPU_SSE2_IS_FAST ) @@ -2329,8 +2694,9 @@ static int check_all_flags( void ) { int ret = 0; int cpu0 = 0, cpu1 = 0; + uint32_t cpu_detect = x264_cpu_detect(); #if HAVE_MMX - if( x264_cpu_detect() & X264_CPU_MMX2 ) + if( cpu_detect & X264_CPU_MMX2 ) { ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMX2, "MMX" ); ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" ); @@ -2339,75 +2705,116 @@ static int check_all_flags( void ) ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" ); cpu1 &= ~X264_CPU_CACHELINE_32; #endif - if( x264_cpu_detect() & X264_CPU_LZCNT ) + if( cpu_detect & X264_CPU_LZCNT ) { - ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" ); + ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX LZCNT" ); cpu1 &= ~X264_CPU_LZCNT; } ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" ); cpu1 &= ~X264_CPU_SLOW_CTZ; } - if( x264_cpu_detect() & X264_CPU_SSE2 ) + if( cpu_detect & X264_CPU_SSE ) + ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE, "SSE" ); + if( cpu_detect & X264_CPU_SSE2 ) { - ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" ); + ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" ); ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" ); ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" ); cpu1 &= ~X264_CPU_CACHELINE_64; - ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSE2 FastShuffle" ); - cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST; + ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_SHUFFLE, "SSE2 SlowShuffle" ); + cpu1 &= ~X264_CPU_SLOW_SHUFFLE; ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" ); cpu1 &= ~X264_CPU_SLOW_CTZ; - ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSE2 SlowAtom" ); - cpu1 &= ~X264_CPU_SLOW_ATOM; - } - if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN ) - { - ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" ); - cpu1 &= ~X264_CPU_SSE_MISALIGN; - } - if( x264_cpu_detect() & X264_CPU_LZCNT ) - { - ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" ); - cpu1 &= ~X264_CPU_LZCNT; + if( cpu_detect & X264_CPU_LZCNT ) + { + ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE2 LZCNT" ); + cpu1 &= ~X264_CPU_LZCNT; + } } - if( x264_cpu_detect() & X264_CPU_SSE3 ) + if( cpu_detect & X264_CPU_SSE3 ) { ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" ); cpu1 &= ~X264_CPU_CACHELINE_64; } - if( x264_cpu_detect() & X264_CPU_SSSE3 ) + if( cpu_detect & X264_CPU_SSSE3 ) { ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" ); ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" ); cpu1 &= ~X264_CPU_CACHELINE_64; - ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSSE3 FastShuffle" ); - cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST; + ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_SHUFFLE, "SSSE3 SlowShuffle" ); + cpu1 &= ~X264_CPU_SLOW_SHUFFLE; ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" ); cpu1 &= ~X264_CPU_SLOW_CTZ; ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" ); + ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64 SlowAtom" ); + cpu1 &= ~X264_CPU_CACHELINE_64; cpu1 &= ~X264_CPU_SLOW_ATOM; + if( cpu_detect & X264_CPU_LZCNT ) + { + ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSSE3 LZCNT" ); + cpu1 &= ~X264_CPU_LZCNT; + } } - if( x264_cpu_detect() & X264_CPU_SSE4 ) - ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4 | X264_CPU_SHUFFLE_IS_FAST, "SSE4" ); - if( x264_cpu_detect() & X264_CPU_AVX ) + if( cpu_detect & X264_CPU_SSE4 ) + ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" ); + if( cpu_detect & X264_CPU_SSE42 ) + ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE42, "SSE4.2" ); + if( cpu_detect & X264_CPU_AVX ) ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX, "AVX" ); - if( x264_cpu_detect() & X264_CPU_XOP ) + if( cpu_detect & X264_CPU_XOP ) ret |= add_flags( &cpu0, &cpu1, X264_CPU_XOP, "XOP" ); - if( x264_cpu_detect() & X264_CPU_FMA4 ) + if( cpu_detect & X264_CPU_FMA4 ) + { ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA4, "FMA4" ); + cpu1 &= ~X264_CPU_FMA4; + } + if( cpu_detect & X264_CPU_FMA3 ) + { + ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA3, "FMA3" ); + cpu1 &= ~X264_CPU_FMA3; + } + if( cpu_detect & X264_CPU_AVX2 ) + { + ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA3 | X264_CPU_AVX2, "AVX2" ); + if( cpu_detect & X264_CPU_LZCNT ) + { + ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "AVX2 LZCNT" ); + cpu1 &= ~X264_CPU_LZCNT; + } + } + if( cpu_detect & X264_CPU_BMI1 ) + { + ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI1, "BMI1" ); + cpu1 &= ~X264_CPU_BMI1; + } + if( cpu_detect & X264_CPU_BMI2 ) + { + ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI1|X264_CPU_BMI2, "BMI2" ); + cpu1 &= ~(X264_CPU_BMI1|X264_CPU_BMI2); + } #elif ARCH_PPC - if( x264_cpu_detect() & X264_CPU_ALTIVEC ) + if( cpu_detect & X264_CPU_ALTIVEC ) { fprintf( stderr, "x264: ALTIVEC against C\n" ); ret = check_all_funcs( 0, X264_CPU_ALTIVEC ); } #elif ARCH_ARM - if( x264_cpu_detect() & X264_CPU_ARMV6 ) + if( cpu_detect & X264_CPU_NEON ) + x264_checkasm_call = x264_checkasm_call_neon; + if( cpu_detect & X264_CPU_ARMV6 ) ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" ); - if( x264_cpu_detect() & X264_CPU_NEON ) + if( cpu_detect & X264_CPU_NEON ) ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" ); - if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC ) + if( cpu_detect & X264_CPU_FAST_NEON_MRC ) ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" ); +#elif ARCH_AARCH64 + if( cpu_detect & X264_CPU_ARMV8 ) + ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV8, "ARMv8" ); + if( cpu_detect & X264_CPU_NEON ) + ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" ); +#elif ARCH_MIPS + if( cpu_detect & X264_CPU_MSA ) + ret |= add_flags( &cpu0, &cpu1, X264_CPU_MSA, "MSA" ); #endif return ret; } @@ -2418,7 +2825,7 @@ int main(int argc, char *argv[]) if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) ) { -#if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM +#if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM && !ARCH_AARCH64 && !ARCH_MIPS fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" ); return 1; #endif @@ -2436,8 +2843,8 @@ int main(int argc, char *argv[]) fprintf( stderr, "x264: using random seed %u\n", seed ); srand( seed ); - buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 16*BENCH_ALIGNS ); - pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 16*BENCH_ALIGNS ); + buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 32*BENCH_ALIGNS ); + pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 32*BENCH_ALIGNS ); if( !buf1 || !pbuf1 ) { fprintf( stderr, "malloc failed, unable to initiate tests!\n" ); @@ -2458,19 +2865,19 @@ int main(int argc, char *argv[]) } memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) ); - /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */ + /* 32-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */ if( do_bench ) for( int i = 0; i < BENCH_ALIGNS && !ret; i++ ) { INIT_POINTER_OFFSETS; - ret |= x264_stack_pagealign( check_all_flags, i*16 ); - buf1 += 16; - pbuf1 += 16; + ret |= x264_stack_pagealign( check_all_flags, i*32 ); + buf1 += 32; + pbuf1 += 32; quiet = 1; fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS ); } else - ret = check_all_flags(); + ret = x264_stack_pagealign( check_all_flags, 0 ); if( ret ) {