+
+ 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:" );
+