]> git.sesse.net Git - x264/blob - tools/checkasm.c
Faster chroma weight cost calculation
[x264] / tools / checkasm.c
1 /*****************************************************************************
2  * checkasm.c: assembly check tool
3  *****************************************************************************
4  * Copyright (C) 2003-2012 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *          Fiona Glaser <fiona@x264.com>
9  *
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.
14  *
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.
19  *
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.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include <ctype.h>
29 #include "common/common.h"
30 #include "common/cpu.h"
31
32 // GCC doesn't align stack variables on ARM, so use .bss
33 #if ARCH_ARM
34 #undef ALIGNED_16
35 #define ALIGNED_16( var ) DECLARE_ALIGNED( static var, 16 )
36 #endif
37
38 /* buf1, buf2: initialised to random data and shouldn't write into them */
39 uint8_t *buf1, *buf2;
40 /* buf3, buf4: used to store output */
41 uint8_t *buf3, *buf4;
42 /* pbuf1, pbuf2: initialised to random pixel data and shouldn't write into them. */
43 pixel *pbuf1, *pbuf2;
44 /* pbuf3, pbuf4: point to buf3, buf4, just for type convenience */
45 pixel *pbuf3, *pbuf4;
46
47 int quiet = 0;
48
49 #define report( name ) { \
50     if( used_asm && !quiet ) \
51         fprintf( stderr, " - %-21s [%s]\n", name, ok ? "OK" : "FAILED" ); \
52     if( !ok ) ret = -1; \
53 }
54
55 #define BENCH_RUNS 100  // tradeoff between accuracy and speed
56 #define BENCH_ALIGNS 16 // number of stack+heap data alignments (another accuracy vs speed tradeoff)
57 #define MAX_FUNCS 1000  // just has to be big enough to hold all the existing functions
58 #define MAX_CPUS 30     // number of different combinations of cpu flags
59
60 typedef struct
61 {
62     void *pointer; // just for detecting duplicates
63     uint32_t cpu;
64     uint32_t cycles;
65     uint32_t den;
66 } bench_t;
67
68 typedef struct
69 {
70     char *name;
71     bench_t vers[MAX_CPUS];
72 } bench_func_t;
73
74 int do_bench = 0;
75 int bench_pattern_len = 0;
76 const char *bench_pattern = "";
77 char func_name[100];
78 static bench_func_t benchs[MAX_FUNCS];
79
80 static const char *pixel_names[12] = { "16x16", "16x8", "8x16", "8x8", "8x4", "4x8", "4x4", "4x16", "4x2", "2x8", "2x4", "2x2" };
81 static const char *intra_predict_16x16_names[7] = { "v", "h", "dc", "p", "dcl", "dct", "dc8" };
82 static const char *intra_predict_8x8c_names[7] = { "dc", "h", "v", "p", "dcl", "dct", "dc8" };
83 static const char *intra_predict_4x4_names[12] = { "v", "h", "dc", "ddl", "ddr", "vr", "hd", "vl", "hu", "dcl", "dct", "dc8" };
84 static const char **intra_predict_8x8_names = intra_predict_4x4_names;
85 static const char **intra_predict_8x16c_names = intra_predict_8x8c_names;
86
87 #define set_func_name(...) snprintf( func_name, sizeof(func_name), __VA_ARGS__ )
88
89 static inline uint32_t read_time(void)
90 {
91     uint32_t a = 0;
92 #if HAVE_X86_INLINE_ASM
93     asm volatile( "rdtsc" :"=a"(a) ::"edx" );
94 #elif ARCH_PPC
95     asm volatile( "mftb %0" : "=r" (a) );
96 #elif ARCH_ARM     // ARMv7 only
97     asm volatile( "mrc p15, 0, %0, c9, c13, 0" : "=r"(a) );
98 #endif
99     return a;
100 }
101
102 static bench_t* get_bench( const char *name, int cpu )
103 {
104     int i, j;
105     for( i = 0; benchs[i].name && strcmp(name, benchs[i].name); i++ )
106         assert( i < MAX_FUNCS );
107     if( !benchs[i].name )
108         benchs[i].name = strdup( name );
109     if( !cpu )
110         return &benchs[i].vers[0];
111     for( j = 1; benchs[i].vers[j].cpu && benchs[i].vers[j].cpu != cpu; j++ )
112         assert( j < MAX_CPUS );
113     benchs[i].vers[j].cpu = cpu;
114     return &benchs[i].vers[j];
115 }
116
117 static int cmp_nop( const void *a, const void *b )
118 {
119     return *(uint16_t*)a - *(uint16_t*)b;
120 }
121
122 static int cmp_bench( const void *a, const void *b )
123 {
124     // asciibetical sort except preserving numbers
125     const char *sa = ((bench_func_t*)a)->name;
126     const char *sb = ((bench_func_t*)b)->name;
127     for( ;; sa++, sb++ )
128     {
129         if( !*sa && !*sb )
130             return 0;
131         if( isdigit( *sa ) && isdigit( *sb ) && isdigit( sa[1] ) != isdigit( sb[1] ) )
132             return isdigit( sa[1] ) - isdigit( sb[1] );
133         if( *sa != *sb )
134             return *sa - *sb;
135     }
136 }
137
138 static void print_bench(void)
139 {
140     uint16_t nops[10000] = {0};
141     int nfuncs, nop_time=0;
142
143     for( int i = 0; i < 10000; i++ )
144     {
145         int t = read_time();
146         nops[i] = read_time() - t;
147     }
148     qsort( nops, 10000, sizeof(uint16_t), cmp_nop );
149     for( int i = 500; i < 9500; i++ )
150         nop_time += nops[i];
151     nop_time /= 900;
152     printf( "nop: %d\n", nop_time );
153
154     for( nfuncs = 0; nfuncs < MAX_FUNCS && benchs[nfuncs].name; nfuncs++ );
155     qsort( benchs, nfuncs, sizeof(bench_func_t), cmp_bench );
156     for( int i = 0; i < nfuncs; i++ )
157         for( int j = 0; j < MAX_CPUS && (!j || benchs[i].vers[j].cpu); j++ )
158         {
159             int k;
160             bench_t *b = &benchs[i].vers[j];
161             if( !b->den )
162                 continue;
163             for( k = 0; k < j && benchs[i].vers[k].pointer != b->pointer; k++ );
164             if( k < j )
165                 continue;
166             printf( "%s_%s%s: %"PRId64"\n", benchs[i].name,
167                     b->cpu&X264_CPU_AVX2 ? "avx2" :
168                     b->cpu&X264_CPU_FMA3 ? "fma3" :
169                     b->cpu&X264_CPU_FMA4 ? "fma4" :
170                     b->cpu&X264_CPU_XOP ? "xop" :
171                     b->cpu&X264_CPU_AVX ? "avx" :
172                     b->cpu&X264_CPU_SSE4 ? "sse4" :
173                     b->cpu&X264_CPU_SSSE3 ? "ssse3" :
174                     b->cpu&X264_CPU_SSE3 ? "sse3" :
175                     /* print sse2slow only if there's also a sse2fast version of the same func */
176                     b->cpu&X264_CPU_SSE2_IS_SLOW && j<MAX_CPUS-1 && b[1].cpu&X264_CPU_SSE2_IS_FAST && !(b[1].cpu&X264_CPU_SSE3) ? "sse2slow" :
177                     b->cpu&X264_CPU_SSE2 ? "sse2" :
178                     b->cpu&X264_CPU_MMX ? "mmx" :
179                     b->cpu&X264_CPU_ALTIVEC ? "altivec" :
180                     b->cpu&X264_CPU_NEON ? "neon" :
181                     b->cpu&X264_CPU_ARMV6 ? "armv6" : "c",
182                     b->cpu&X264_CPU_CACHELINE_32 ? "_c32" :
183                     b->cpu&X264_CPU_CACHELINE_64 ? "_c64" :
184                     b->cpu&X264_CPU_SHUFFLE_IS_FAST && !(b->cpu&X264_CPU_SSE4) ? "_fastshuffle" :
185                     b->cpu&X264_CPU_SSE_MISALIGN ? "_misalign" :
186                     b->cpu&X264_CPU_LZCNT ? "_lzcnt" :
187                     b->cpu&X264_CPU_BMI2 ? "_bmi2" :
188                     b->cpu&X264_CPU_TBM ? "_tbm" :
189                     b->cpu&X264_CPU_BMI1 ? "_bmi1" :
190                     b->cpu&X264_CPU_FAST_NEON_MRC ? "_fast_mrc" :
191                     b->cpu&X264_CPU_SLOW_CTZ ? "_slow_ctz" :
192                     b->cpu&X264_CPU_SLOW_ATOM ? "_slow_atom" : "",
193                     ((int64_t)10*b->cycles/b->den - nop_time)/4 );
194         }
195 }
196
197 #if ARCH_X86 || ARCH_X86_64
198 int x264_stack_pagealign( int (*func)(), int align );
199
200 /* detect when callee-saved regs aren't saved
201  * needs an explicit asm check because it only sometimes crashes in normal use. */
202 intptr_t x264_checkasm_call( intptr_t (*func)(), int *ok, ... );
203 #else
204 #define x264_stack_pagealign( func, align ) func()
205 #endif
206
207 #define call_c1(func,...) func(__VA_ARGS__)
208
209 #if ARCH_X86_64
210 /* Evil hack: detect incorrect assumptions that 32-bit ints are zero-extended to 64-bit.
211  * This is done by clobbering the stack with junk around the stack pointer and calling the
212  * assembly function through x264_checkasm_call with added dummy arguments which forces all
213  * real arguments to be passed on the stack and not in registers. For 32-bit argument the
214  * upper half of the 64-bit register location on the stack will now contain junk. Note that
215  * this is dependant on compiler behaviour and that interrupts etc. at the wrong time may
216  * overwrite the junk written to the stack so there's no guarantee that it will always
217  * detect all functions that assumes zero-extension.
218  */
219 void x264_checkasm_stack_clobber( uint64_t clobber, ... );
220 #define call_a1(func,...) ({ \
221     uint64_t r = (rand() & 0xffff) * 0x0001000100010001ULL; \
222     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 */ \
223     x264_checkasm_call(( intptr_t(*)())func, &ok, 0, 0, 0, 0, __VA_ARGS__ ); })
224 #elif ARCH_X86
225 #define call_a1(func,...) x264_checkasm_call( (intptr_t(*)())func, &ok, __VA_ARGS__ )
226 #else
227 #define call_a1 call_c1
228 #endif
229
230 #define call_bench(func,cpu,...)\
231     if( do_bench && !strncmp(func_name, bench_pattern, bench_pattern_len) )\
232     {\
233         uint32_t tsum = 0;\
234         int tcount = 0;\
235         call_a1(func, __VA_ARGS__);\
236         for( int ti = 0; ti < (cpu?BENCH_RUNS:BENCH_RUNS/4); ti++ )\
237         {\
238             uint32_t t = read_time();\
239             func(__VA_ARGS__);\
240             func(__VA_ARGS__);\
241             func(__VA_ARGS__);\
242             func(__VA_ARGS__);\
243             t = read_time() - t;\
244             if( t*tcount <= tsum*4 && ti > 0 )\
245             {\
246                 tsum += t;\
247                 tcount++;\
248             }\
249         }\
250         bench_t *b = get_bench( func_name, cpu );\
251         b->cycles += tsum;\
252         b->den += tcount;\
253         b->pointer = func;\
254     }
255
256 /* for most functions, run benchmark and correctness test at the same time.
257  * for those that modify their inputs, run the above macros separately */
258 #define call_a(func,...) ({ call_a2(func,__VA_ARGS__); call_a1(func,__VA_ARGS__); })
259 #define call_c(func,...) ({ call_c2(func,__VA_ARGS__); call_c1(func,__VA_ARGS__); })
260 #define call_a2(func,...) ({ call_bench(func,cpu_new,__VA_ARGS__); })
261 #define call_c2(func,...) ({ call_bench(func,0,__VA_ARGS__); })
262
263
264 static int check_pixel( int cpu_ref, int cpu_new )
265 {
266     x264_pixel_function_t pixel_c;
267     x264_pixel_function_t pixel_ref;
268     x264_pixel_function_t pixel_asm;
269     x264_predict_t predict_4x4[12];
270     x264_predict8x8_t predict_8x8[12];
271     x264_predict_8x8_filter_t predict_8x8_filter;
272     ALIGNED_16( pixel edge[36] );
273     uint16_t cost_mv[32];
274     int ret = 0, ok, used_asm;
275
276     x264_pixel_init( 0, &pixel_c );
277     x264_pixel_init( cpu_ref, &pixel_ref );
278     x264_pixel_init( cpu_new, &pixel_asm );
279     x264_predict_4x4_init( 0, predict_4x4 );
280     x264_predict_8x8_init( 0, predict_8x8, &predict_8x8_filter );
281     predict_8x8_filter( pbuf2+40, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
282
283     // maximize sum
284     for( int i = 0; i < 256; i++ )
285     {
286         int z = i|(i>>4);
287         z ^= z>>2;
288         z ^= z>>1;
289         pbuf4[i] = -(z&1) & PIXEL_MAX;
290         pbuf3[i] = ~pbuf4[i] & PIXEL_MAX;
291     }
292     // random pattern made of maxed pixel differences, in case an intermediate value overflows
293     for( int i = 256; i < 0x1000; i++ )
294     {
295         pbuf4[i] = -(pbuf1[i&~0x88]&1) & PIXEL_MAX;
296         pbuf3[i] = ~(pbuf4[i]) & PIXEL_MAX;
297     }
298
299 #define TEST_PIXEL( name, align ) \
300     ok = 1, used_asm = 0; \
301     for( int i = 0; i < 8; i++ ) \
302     { \
303         int res_c, res_asm; \
304         if( pixel_asm.name[i] != pixel_ref.name[i] ) \
305         { \
306             set_func_name( "%s_%s", #name, pixel_names[i] ); \
307             used_asm = 1; \
308             for( int j = 0; j < 64; j++ ) \
309             { \
310                 res_c   = call_c( pixel_c.name[i],   pbuf1, (intptr_t)16, pbuf2+j*!align, (intptr_t)64 ); \
311                 res_asm = call_a( pixel_asm.name[i], pbuf1, (intptr_t)16, pbuf2+j*!align, (intptr_t)64 ); \
312                 if( res_c != res_asm ) \
313                 { \
314                     ok = 0; \
315                     fprintf( stderr, #name "[%d]: %d != %d [FAILED]\n", i, res_c, res_asm ); \
316                     break; \
317                 } \
318             } \
319             for( int j = 0; j < 0x1000 && ok; j += 256 ) \
320             { \
321                 res_c   = pixel_c  .name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
322                 res_asm = pixel_asm.name[i]( pbuf3+j, 16, pbuf4+j, 16 ); \
323                 if( res_c != res_asm ) \
324                 { \
325                     ok = 0; \
326                     fprintf( stderr, #name "[%d]: overflow %d != %d\n", i, res_c, res_asm ); \
327                 } \
328             } \
329         } \
330     } \
331     report( "pixel " #name " :" );
332
333     TEST_PIXEL( sad, 0 );
334     TEST_PIXEL( sad_aligned, 1 );
335     TEST_PIXEL( ssd, 1 );
336     TEST_PIXEL( satd, 0 );
337     TEST_PIXEL( sa8d, 1 );
338
339 #define TEST_PIXEL_X( N ) \
340     ok = 1; used_asm = 0; \
341     for( int i = 0; i < 7; i++ ) \
342     { \
343         int res_c[4]={0}, res_asm[4]={0}; \
344         if( pixel_asm.sad_x##N[i] && pixel_asm.sad_x##N[i] != pixel_ref.sad_x##N[i] ) \
345         { \
346             set_func_name( "sad_x%d_%s", N, pixel_names[i] ); \
347             used_asm = 1; \
348             for( int j = 0; j < 64; j++ ) \
349             { \
350                 pixel *pix2 = pbuf2+j; \
351                 res_c[0] = pixel_c.sad[i]( pbuf1, 16, pix2,   64 ); \
352                 res_c[1] = pixel_c.sad[i]( pbuf1, 16, pix2+6, 64 ); \
353                 res_c[2] = pixel_c.sad[i]( pbuf1, 16, pix2+1, 64 ); \
354                 if( N == 4 ) \
355                 { \
356                     res_c[3] = pixel_c.sad[i]( pbuf1, 16, pix2+10, 64 ); \
357                     call_a( pixel_asm.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, (intptr_t)64, res_asm ); \
358                 } \
359                 else \
360                     call_a( pixel_asm.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, (intptr_t)64, res_asm ); \
361                 if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
362                 { \
363                     ok = 0; \
364                     fprintf( stderr, "sad_x"#N"[%d]: %d,%d,%d,%d != %d,%d,%d,%d [FAILED]\n", \
365                              i, res_c[0], res_c[1], res_c[2], res_c[3], \
366                              res_asm[0], res_asm[1], res_asm[2], res_asm[3] ); \
367                 } \
368                 if( N == 4 ) \
369                     call_c2( pixel_c.sad_x4[i], pbuf1, pix2, pix2+6, pix2+1, pix2+10, (intptr_t)64, res_asm ); \
370                 else \
371                     call_c2( pixel_c.sad_x3[i], pbuf1, pix2, pix2+6, pix2+1, (intptr_t)64, res_asm ); \
372             } \
373         } \
374     } \
375     report( "pixel sad_x"#N" :" );
376
377     TEST_PIXEL_X(3);
378     TEST_PIXEL_X(4);
379
380 #define TEST_PIXEL_VAR( i ) \
381     if( pixel_asm.var[i] != pixel_ref.var[i] ) \
382     { \
383         set_func_name( "%s_%s", "var", pixel_names[i] ); \
384         used_asm = 1; \
385         /* abi-check wrapper can't return uint64_t, so separate it from return value check */ \
386         call_c1( pixel_c.var[i],   pbuf1,           16 ); \
387         call_a1( pixel_asm.var[i], pbuf1, (intptr_t)16 ); \
388         uint64_t res_c   = pixel_c.var[i]( pbuf1, 16 ); \
389         uint64_t res_asm = pixel_asm.var[i]( pbuf1, 16 ); \
390         if( res_c != res_asm ) \
391         { \
392             ok = 0; \
393             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) ); \
394         } \
395         call_c2( pixel_c.var[i],   pbuf1, (intptr_t)16 ); \
396         call_a2( pixel_asm.var[i], pbuf1, (intptr_t)16 ); \
397     }
398
399     ok = 1; used_asm = 0;
400     TEST_PIXEL_VAR( PIXEL_16x16 );
401     TEST_PIXEL_VAR( PIXEL_8x16 );
402     TEST_PIXEL_VAR( PIXEL_8x8 );
403     report( "pixel var :" );
404
405 #define TEST_PIXEL_VAR2( i ) \
406     if( pixel_asm.var2[i] != pixel_ref.var2[i] ) \
407     { \
408         int res_c, res_asm, ssd_c, ssd_asm; \
409         set_func_name( "%s_%s", "var2", pixel_names[i] ); \
410         used_asm = 1; \
411         res_c   = call_c( pixel_c.var2[i],   pbuf1, (intptr_t)16, pbuf2, (intptr_t)16, &ssd_c   ); \
412         res_asm = call_a( pixel_asm.var2[i], pbuf1, (intptr_t)16, pbuf2, (intptr_t)16, &ssd_asm ); \
413         if( res_c != res_asm || ssd_c != ssd_asm ) \
414         { \
415             ok = 0; \
416             fprintf( stderr, "var2[%d]: %d != %d or %d != %d [FAILED]\n", i, res_c, res_asm, ssd_c, ssd_asm ); \
417         } \
418     }
419
420     ok = 1; used_asm = 0;
421     TEST_PIXEL_VAR2( PIXEL_8x16 );
422     TEST_PIXEL_VAR2( PIXEL_8x8 );
423     report( "pixel var2 :" );
424
425     ok = 1; used_asm = 0;
426     for( int i = 0; i < 4; i++ )
427         if( pixel_asm.hadamard_ac[i] != pixel_ref.hadamard_ac[i] )
428         {
429             set_func_name( "hadamard_ac_%s", pixel_names[i] );
430             used_asm = 1;
431             for( int j = 0; j < 32; j++ )
432             {
433                 pixel *pix = (j&16 ? pbuf1 : pbuf3) + (j&15)*256;
434                 call_c1( pixel_c.hadamard_ac[i],   pbuf1, (intptr_t)16 );
435                 call_a1( pixel_asm.hadamard_ac[i], pbuf1, (intptr_t)16 );
436                 uint64_t rc = pixel_c.hadamard_ac[i]( pix, 16 );
437                 uint64_t ra = pixel_asm.hadamard_ac[i]( pix, 16 );
438                 if( rc != ra )
439                 {
440                     ok = 0;
441                     fprintf( stderr, "hadamard_ac[%d]: %d,%d != %d,%d\n", i, (int)rc, (int)(rc>>32), (int)ra, (int)(ra>>32) );
442                     break;
443                 }
444             }
445             call_c2( pixel_c.hadamard_ac[i],   pbuf1, (intptr_t)16 );
446             call_a2( pixel_asm.hadamard_ac[i], pbuf1, (intptr_t)16 );
447         }
448     report( "pixel hadamard_ac :" );
449
450     // maximize sum
451     for( int i = 0; i < 32; i++ )
452         for( int j = 0; j < 16; j++ )
453             pbuf4[16*i+j] = -((i+j)&1) & PIXEL_MAX;
454     ok = 1; used_asm = 0;
455     if( pixel_asm.vsad != pixel_ref.vsad )
456     {
457         for( int h = 2; h <= 32; h += 2 )
458         {
459             int res_c, res_asm;
460             set_func_name( "vsad" );
461             used_asm = 1;
462             for( int j = 0; j < 2 && ok; j++ )
463             {
464                 pixel *p = j ? pbuf4 : pbuf1;
465                 res_c   = call_c( pixel_c.vsad,   p, (intptr_t)16, h );
466                 res_asm = call_a( pixel_asm.vsad, p, (intptr_t)16, h );
467                 if( res_c != res_asm )
468                 {
469                     ok = 0;
470                     fprintf( stderr, "vsad: height=%d, %d != %d\n", h, res_c, res_asm );
471                     break;
472                 }
473             }
474         }
475     }
476     report( "pixel vsad :" );
477
478     ok = 1; used_asm = 0;
479     if( pixel_asm.asd8 != pixel_ref.asd8 )
480     {
481         set_func_name( "asd8" );
482         used_asm = 1;
483         int res_c = call_c( pixel_c.asd8,   pbuf1, (intptr_t)8, pbuf2, (intptr_t)8, 16 );
484         int res_a = call_a( pixel_asm.asd8, pbuf1, (intptr_t)8, pbuf2, (intptr_t)8, 16 );
485         if( res_c != res_a )
486         {
487             ok = 0;
488             fprintf( stderr, "asd: %d != %d\n", res_c, res_a );
489         }
490     }
491     report( "pixel asd :" );
492
493 #define TEST_INTRA_X3( name, i8x8, ... ) \
494     if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
495     { \
496         int res_c[3], res_asm[3]; \
497         set_func_name( #name ); \
498         used_asm = 1; \
499         call_c( pixel_c.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_c ); \
500         call_a( pixel_asm.name, pbuf1+48, i8x8 ? edge : pbuf3+48, res_asm ); \
501         if( memcmp(res_c, res_asm, sizeof(res_c)) ) \
502         { \
503             ok = 0; \
504             fprintf( stderr, #name": %d,%d,%d != %d,%d,%d [FAILED]\n", \
505                      res_c[0], res_c[1], res_c[2], \
506                      res_asm[0], res_asm[1], res_asm[2] ); \
507         } \
508     }
509
510 #define TEST_INTRA_X9( name, cmp ) \
511     if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
512     { \
513         set_func_name( #name ); \
514         used_asm = 1; \
515         ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
516         for( int i=0; i<17; i++ ) \
517             bitcosts[i] = 9*(i!=8); \
518         memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
519         memcpy( pbuf4, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) ); \
520         for( int i=0; i<32; i++ ) \
521         { \
522             pixel *fenc = pbuf1+48+i*12; \
523             pixel *fdec1 = pbuf3+48+i*12; \
524             pixel *fdec2 = pbuf4+48+i*12; \
525             int pred_mode = i%9; \
526             int res_c = INT_MAX; \
527             for( int j=0; j<9; j++ ) \
528             { \
529                 predict_4x4[j]( fdec1 ); \
530                 int cost = pixel_c.cmp[PIXEL_4x4]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
531                 if( cost < (uint16_t)res_c ) \
532                     res_c = cost + (j<<16); \
533             } \
534             predict_4x4[res_c>>16]( fdec1 ); \
535             int res_a = call_a( pixel_asm.name, fenc, fdec2, bitcosts+8-pred_mode ); \
536             if( res_c != res_a ) \
537             { \
538                 ok = 0; \
539                 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
540                 break; \
541             } \
542             if( memcmp(fdec1, fdec2, 4*FDEC_STRIDE*sizeof(pixel)) ) \
543             { \
544                 ok = 0; \
545                 fprintf( stderr, #name" [FAILED]\n" ); \
546                 for( int j=0; j<16; j++ ) \
547                     fprintf( stderr, "%02x ", fdec1[(j&3)+(j>>2)*FDEC_STRIDE] ); \
548                 fprintf( stderr, "\n" ); \
549                 for( int j=0; j<16; j++ ) \
550                     fprintf( stderr, "%02x ", fdec2[(j&3)+(j>>2)*FDEC_STRIDE] ); \
551                 fprintf( stderr, "\n" ); \
552                 break; \
553             } \
554         } \
555     }
556
557 #define TEST_INTRA8_X9( name, cmp ) \
558     if( pixel_asm.name && pixel_asm.name != pixel_ref.name ) \
559     { \
560         set_func_name( #name ); \
561         used_asm = 1; \
562         ALIGNED_ARRAY_64( uint16_t, bitcosts,[17] ); \
563         ALIGNED_ARRAY_16( uint16_t, satds_c,[16] ); \
564         ALIGNED_ARRAY_16( uint16_t, satds_a,[16] ); \
565         memset( satds_c, 0, 16 * sizeof(*satds_c) ); \
566         memset( satds_a, 0, 16 * sizeof(*satds_a) ); \
567         for( int i=0; i<17; i++ ) \
568             bitcosts[i] = 9*(i!=8); \
569         for( int i=0; i<32; i++ ) \
570         { \
571             pixel *fenc = pbuf1+48+i*12; \
572             pixel *fdec1 = pbuf3+48+i*12; \
573             pixel *fdec2 = pbuf4+48+i*12; \
574             int pred_mode = i%9; \
575             int res_c = INT_MAX; \
576             predict_8x8_filter( fdec1, edge, ALL_NEIGHBORS, ALL_NEIGHBORS ); \
577             for( int j=0; j<9; j++ ) \
578             { \
579                 predict_8x8[j]( fdec1, edge ); \
580                 satds_c[j] = pixel_c.cmp[PIXEL_8x8]( fenc, FENC_STRIDE, fdec1, FDEC_STRIDE ) + 9*(j!=pred_mode); \
581                 if( satds_c[j] < (uint16_t)res_c ) \
582                     res_c = satds_c[j] + (j<<16); \
583             } \
584             predict_8x8[res_c>>16]( fdec1, edge ); \
585             int res_a = call_a( pixel_asm.name, fenc, fdec2, edge, bitcosts+8-pred_mode, satds_a ); \
586             if( res_c != res_a || memcmp(satds_c, satds_a, sizeof(satds_c)) ) \
587             { \
588                 ok = 0; \
589                 fprintf( stderr, #name": %d,%d != %d,%d [FAILED]\n", res_c>>16, res_c&0xffff, res_a>>16, res_a&0xffff ); \
590                 for( int j = 0; j < 9; j++ ) \
591                     fprintf( stderr, "%5d ", satds_c[j]); \
592                 fprintf( stderr, "\n" ); \
593                 for( int j = 0; j < 9; j++ ) \
594                     fprintf( stderr, "%5d ", satds_a[j]); \
595                 fprintf( stderr, "\n" ); \
596                 break; \
597             } \
598             for( int j=0; j<8; j++ ) \
599                 if( memcmp(fdec1+j*FDEC_STRIDE, fdec2+j*FDEC_STRIDE, 8*sizeof(pixel)) ) \
600                     ok = 0; \
601             if( !ok ) \
602             { \
603                 fprintf( stderr, #name" [FAILED]\n" ); \
604                 for( int j=0; j<8; j++ ) \
605                 { \
606                     for( int k=0; k<8; k++ ) \
607                         fprintf( stderr, "%02x ", fdec1[k+j*FDEC_STRIDE] ); \
608                     fprintf( stderr, "\n" ); \
609                 } \
610                 fprintf( stderr, "\n" ); \
611                 for( int j=0; j<8; j++ ) \
612                 { \
613                     for( int k=0; k<8; k++ ) \
614                         fprintf( stderr, "%02x ", fdec2[k+j*FDEC_STRIDE] ); \
615                     fprintf( stderr, "\n" ); \
616                 } \
617                 fprintf( stderr, "\n" ); \
618                 break; \
619             } \
620         } \
621     }
622
623     memcpy( pbuf3, pbuf2, 20*FDEC_STRIDE*sizeof(pixel) );
624     ok = 1; used_asm = 0;
625     TEST_INTRA_X3( intra_satd_x3_16x16, 0 );
626     TEST_INTRA_X3( intra_satd_x3_8x16c, 0 );
627     TEST_INTRA_X3( intra_satd_x3_8x8c, 0 );
628     TEST_INTRA_X3( intra_sa8d_x3_8x8, 1, edge );
629     TEST_INTRA_X3( intra_satd_x3_4x4, 0 );
630     report( "intra satd_x3 :" );
631     ok = 1; used_asm = 0;
632     TEST_INTRA_X3( intra_sad_x3_16x16, 0 );
633     TEST_INTRA_X3( intra_sad_x3_8x16c, 0 );
634     TEST_INTRA_X3( intra_sad_x3_8x8c, 0 );
635     TEST_INTRA_X3( intra_sad_x3_8x8, 1, edge );
636     TEST_INTRA_X3( intra_sad_x3_4x4, 0 );
637     report( "intra sad_x3 :" );
638     ok = 1; used_asm = 0;
639     TEST_INTRA_X9( intra_satd_x9_4x4, satd );
640     TEST_INTRA8_X9( intra_sa8d_x9_8x8, sa8d );
641     report( "intra satd_x9 :" );
642     ok = 1; used_asm = 0;
643     TEST_INTRA_X9( intra_sad_x9_4x4, sad );
644     TEST_INTRA8_X9( intra_sad_x9_8x8, sad );
645     report( "intra sad_x9 :" );
646
647     ok = 1; used_asm = 0;
648     if( pixel_asm.ssd_nv12_core != pixel_ref.ssd_nv12_core )
649     {
650         used_asm = 1;
651         set_func_name( "ssd_nv12" );
652         uint64_t res_u_c, res_v_c, res_u_a, res_v_a;
653         pixel_c.ssd_nv12_core(   pbuf1, 368, pbuf2, 368, 360, 8, &res_u_c, &res_v_c );
654         pixel_asm.ssd_nv12_core( pbuf1, 368, pbuf2, 368, 360, 8, &res_u_a, &res_v_a );
655         if( res_u_c != res_u_a || res_v_c != res_v_a )
656         {
657             ok = 0;
658             fprintf( stderr, "ssd_nv12: %"PRIu64",%"PRIu64" != %"PRIu64",%"PRIu64"\n",
659                      res_u_c, res_v_c, res_u_a, res_v_a );
660         }
661         call_c( pixel_c.ssd_nv12_core,   pbuf1, (intptr_t)368, pbuf2, (intptr_t)368, 360, 8, &res_u_c, &res_v_c );
662         call_a( pixel_asm.ssd_nv12_core, pbuf1, (intptr_t)368, pbuf2, (intptr_t)368, 360, 8, &res_u_a, &res_v_a );
663     }
664     report( "ssd_nv12 :" );
665
666     if( pixel_asm.ssim_4x4x2_core != pixel_ref.ssim_4x4x2_core ||
667         pixel_asm.ssim_end4 != pixel_ref.ssim_end4 )
668     {
669         int cnt;
670         float res_c, res_a;
671         ALIGNED_16( int sums[5][4] ) = {{0}};
672         used_asm = ok = 1;
673         x264_emms();
674         res_c = x264_pixel_ssim_wxh( &pixel_c,   pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
675         res_a = x264_pixel_ssim_wxh( &pixel_asm, pbuf1+2, 32, pbuf2+2, 32, 32, 28, pbuf3, &cnt );
676         if( fabs( res_c - res_a ) > 1e-6 )
677         {
678             ok = 0;
679             fprintf( stderr, "ssim: %.7f != %.7f [FAILED]\n", res_c, res_a );
680         }
681         set_func_name( "ssim_core" );
682         call_c2( pixel_c.ssim_4x4x2_core,   pbuf1+2, (intptr_t)32, pbuf2+2, (intptr_t)32, sums );
683         call_a2( pixel_asm.ssim_4x4x2_core, pbuf1+2, (intptr_t)32, pbuf2+2, (intptr_t)32, sums );
684         set_func_name( "ssim_end" );
685         call_c2( pixel_c.ssim_end4,   sums, sums, 4 );
686         call_a2( pixel_asm.ssim_end4, sums, sums, 4 );
687         report( "ssim :" );
688     }
689
690     ok = 1; used_asm = 0;
691     for( int i = 0; i < 32; i++ )
692         cost_mv[i] = i*10;
693     for( int i = 0; i < 100 && ok; i++ )
694         if( pixel_asm.ads[i&3] != pixel_ref.ads[i&3] )
695         {
696             ALIGNED_16( uint16_t sums[72] );
697             ALIGNED_16( int dc[4] );
698             ALIGNED_16( int16_t mvs_a[32] );
699             ALIGNED_16( int16_t mvs_c[32] );
700             int mvn_a, mvn_c;
701             int thresh = rand() & 0x3fff;
702             set_func_name( "esa_ads" );
703             for( int j = 0; j < 72; j++ )
704                 sums[j] = rand() & 0x3fff;
705             for( int j = 0; j < 4; j++ )
706                 dc[j] = rand() & 0x3fff;
707             used_asm = 1;
708             mvn_c = call_c( pixel_c.ads[i&3], dc, sums, 32, cost_mv, mvs_c, 28, thresh );
709             mvn_a = call_a( pixel_asm.ads[i&3], dc, sums, 32, cost_mv, mvs_a, 28, thresh );
710             if( mvn_c != mvn_a || memcmp( mvs_c, mvs_a, mvn_c*sizeof(*mvs_c) ) )
711             {
712                 ok = 0;
713                 printf( "c%d: ", i&3 );
714                 for( int j = 0; j < mvn_c; j++ )
715                     printf( "%d ", mvs_c[j] );
716                 printf( "\na%d: ", i&3 );
717                 for( int j = 0; j < mvn_a; j++ )
718                     printf( "%d ", mvs_a[j] );
719                 printf( "\n\n" );
720             }
721         }
722     report( "esa ads:" );
723
724     return ret;
725 }
726
727 static int check_dct( int cpu_ref, int cpu_new )
728 {
729     x264_dct_function_t dct_c;
730     x264_dct_function_t dct_ref;
731     x264_dct_function_t dct_asm;
732     x264_quant_function_t qf;
733     int ret = 0, ok, used_asm, interlace = 0;
734     ALIGNED_16( dctcoef dct1[16][16] );
735     ALIGNED_16( dctcoef dct2[16][16] );
736     ALIGNED_16( dctcoef dct4[16][16] );
737     ALIGNED_16( dctcoef dct8[4][64] );
738     ALIGNED_16( dctcoef dctdc[2][8] );
739     x264_t h_buf;
740     x264_t *h = &h_buf;
741
742     x264_dct_init( 0, &dct_c );
743     x264_dct_init( cpu_ref, &dct_ref);
744     x264_dct_init( cpu_new, &dct_asm );
745
746     memset( h, 0, sizeof(*h) );
747     x264_param_default( &h->param );
748     h->sps->i_chroma_format_idc = 1;
749     h->chroma_qp_table = i_chroma_qp_table + 12;
750     h->param.analyse.i_luma_deadzone[0] = 0;
751     h->param.analyse.i_luma_deadzone[1] = 0;
752     h->param.analyse.b_transform_8x8 = 1;
753     for( int i = 0; i < 6; i++ )
754         h->pps->scaling_list[i] = x264_cqm_flat16;
755     x264_cqm_init( h );
756     x264_quant_init( h, 0, &qf );
757
758     /* overflow test cases */
759     for( int i = 0; i < 5; i++ )
760     {
761         pixel *enc = &pbuf3[16*i*FENC_STRIDE];
762         pixel *dec = &pbuf4[16*i*FDEC_STRIDE];
763
764         for( int j = 0; j < 16; j++ )
765         {
766             int cond_a = (i < 2) ? 1 : ((j&3) == 0 || (j&3) == (i-1));
767             int cond_b = (i == 0) ? 1 : !cond_a;
768             enc[0] = enc[1] = enc[4] = enc[5] = enc[8] = enc[9] = enc[12] = enc[13] = cond_a ? PIXEL_MAX : 0;
769             enc[2] = enc[3] = enc[6] = enc[7] = enc[10] = enc[11] = enc[14] = enc[15] = cond_b ? PIXEL_MAX : 0;
770
771             for( int k = 0; k < 4; k++ )
772                 dec[k] = PIXEL_MAX - enc[k];
773
774             enc += FENC_STRIDE;
775             dec += FDEC_STRIDE;
776         }
777     }
778
779 #define TEST_DCT( name, t1, t2, size ) \
780     if( dct_asm.name != dct_ref.name ) \
781     { \
782         set_func_name( #name ); \
783         used_asm = 1; \
784         pixel *enc = pbuf3; \
785         pixel *dec = pbuf4; \
786         for( int j = 0; j < 5; j++) \
787         { \
788             call_c( dct_c.name, t1, &pbuf1[j*64], &pbuf2[j*64] ); \
789             call_a( dct_asm.name, t2, &pbuf1[j*64], &pbuf2[j*64] ); \
790             if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
791             { \
792                 ok = 0; \
793                 fprintf( stderr, #name " [FAILED]\n" ); \
794                 for( int k = 0; k < size; k++ )\
795                     printf( "%d ", ((dctcoef*)t1)[k] );\
796                 printf("\n");\
797                 for( int k = 0; k < size; k++ )\
798                     printf( "%d ", ((dctcoef*)t2)[k] );\
799                 printf("\n");\
800                 break; \
801             } \
802             call_c( dct_c.name, t1, enc, dec ); \
803             call_a( dct_asm.name, t2, enc, dec ); \
804             if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
805             { \
806                 ok = 0; \
807                 fprintf( stderr, #name " [FAILED] (overflow)\n" ); \
808                 break; \
809             } \
810             enc += 16*FENC_STRIDE; \
811             dec += 16*FDEC_STRIDE; \
812         } \
813     }
814     ok = 1; used_asm = 0;
815     TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16 );
816     TEST_DCT( sub8x8_dct, dct1, dct2, 16*4 );
817     TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4 );
818     TEST_DCT( sub8x16_dct_dc, dctdc[0], dctdc[1], 8 );
819     TEST_DCT( sub16x16_dct, dct1, dct2, 16*16 );
820     report( "sub_dct4 :" );
821
822     ok = 1; used_asm = 0;
823     TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64 );
824     TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*4 );
825     report( "sub_dct8 :" );
826 #undef TEST_DCT
827
828     // fdct and idct are denormalized by different factors, so quant/dequant
829     // is needed to force the coefs into the right range.
830     dct_c.sub16x16_dct( dct4, pbuf1, pbuf2 );
831     dct_c.sub16x16_dct8( dct8, pbuf1, pbuf2 );
832     for( int i = 0; i < 16; i++ )
833     {
834         qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
835         qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
836     }
837     for( int i = 0; i < 4; i++ )
838     {
839         qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
840         qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
841     }
842     x264_cqm_delete( h );
843
844 #define TEST_IDCT( name, src ) \
845     if( dct_asm.name != dct_ref.name ) \
846     { \
847         set_func_name( #name ); \
848         used_asm = 1; \
849         memcpy( pbuf3, pbuf1, 32*32 * sizeof(pixel) ); \
850         memcpy( pbuf4, pbuf1, 32*32 * sizeof(pixel) ); \
851         memcpy( dct1, src, 256 * sizeof(dctcoef) ); \
852         memcpy( dct2, src, 256 * sizeof(dctcoef) ); \
853         call_c1( dct_c.name, pbuf3, (void*)dct1 ); \
854         call_a1( dct_asm.name, pbuf4, (void*)dct2 ); \
855         if( memcmp( pbuf3, pbuf4, 32*32 * sizeof(pixel) ) ) \
856         { \
857             ok = 0; \
858             fprintf( stderr, #name " [FAILED]\n" ); \
859         } \
860         call_c2( dct_c.name, pbuf3, (void*)dct1 ); \
861         call_a2( dct_asm.name, pbuf4, (void*)dct2 ); \
862     }
863     ok = 1; used_asm = 0;
864     TEST_IDCT( add4x4_idct, dct4 );
865     TEST_IDCT( add8x8_idct, dct4 );
866     TEST_IDCT( add8x8_idct_dc, dct4 );
867     TEST_IDCT( add16x16_idct, dct4 );
868     TEST_IDCT( add16x16_idct_dc, dct4 );
869     report( "add_idct4 :" );
870
871     ok = 1; used_asm = 0;
872     TEST_IDCT( add8x8_idct8, dct8 );
873     TEST_IDCT( add16x16_idct8, dct8 );
874     report( "add_idct8 :" );
875 #undef TEST_IDCT
876
877 #define TEST_DCTDC( name )\
878     ok = 1; used_asm = 0;\
879     if( dct_asm.name != dct_ref.name )\
880     {\
881         set_func_name( #name );\
882         used_asm = 1;\
883         uint16_t *p = (uint16_t*)buf1;\
884         for( int i = 0; i < 16 && ok; i++ )\
885         {\
886             for( int j = 0; j < 16; j++ )\
887                 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
888                            : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
889                            : ((*p++)&0x1fff)-0x1000; /* general case */\
890             memcpy( dct2, dct1, 16 * sizeof(dctcoef) );\
891             call_c1( dct_c.name, dct1[0] );\
892             call_a1( dct_asm.name, dct2[0] );\
893             if( memcmp( dct1, dct2, 16 * sizeof(dctcoef) ) )\
894                 ok = 0;\
895         }\
896         call_c2( dct_c.name, dct1[0] );\
897         call_a2( dct_asm.name, dct2[0] );\
898     }\
899     report( #name " :" );
900
901     TEST_DCTDC(  dct4x4dc );
902     TEST_DCTDC( idct4x4dc );
903 #undef TEST_DCTDC
904
905 #define TEST_DCTDC_CHROMA( name )\
906     ok = 1; used_asm = 0;\
907     if( dct_asm.name != dct_ref.name )\
908     {\
909         set_func_name( #name );\
910         used_asm = 1;\
911         uint16_t *p = (uint16_t*)buf1;\
912         for( int i = 0; i < 16 && ok; i++ )\
913         {\
914             for( int j = 0; j < 8; j++ )\
915                 dct1[j][0] = !i ? (j^j>>1^j>>2)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
916                            : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
917                            : ((*p++)&0x1fff)-0x1000; /* general case */\
918             memcpy( dct2, dct1, 8*16 * sizeof(dctcoef) );\
919             call_c1( dct_c.name, dctdc[0], dct1 );\
920             call_a1( dct_asm.name, dctdc[1], dct2 );\
921             if( memcmp( dctdc[0], dctdc[1], 8 * sizeof(dctcoef) ) || memcmp( dct1, dct2, 8*16 * sizeof(dctcoef) ) )\
922             {\
923                 ok = 0;\
924                 fprintf( stderr, #name " [FAILED]\n" ); \
925             }\
926         }\
927         call_c2( dct_c.name, dctdc[0], dct1 );\
928         call_a2( dct_asm.name, dctdc[1], dct2 );\
929     }\
930     report( #name " :" );
931
932     TEST_DCTDC_CHROMA( dct2x4dc );
933 #undef TEST_DCTDC_CHROMA
934
935     x264_zigzag_function_t zigzag_c[2];
936     x264_zigzag_function_t zigzag_ref[2];
937     x264_zigzag_function_t zigzag_asm[2];
938
939     ALIGNED_16( dctcoef level1[64] );
940     ALIGNED_16( dctcoef level2[64] );
941
942 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
943     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
944     { \
945         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
946         used_asm = 1; \
947         for( int i = 0; i < size*size; i++ ) \
948             dct[i] = i; \
949         call_c( zigzag_c[interlace].name, t1, dct ); \
950         call_a( zigzag_asm[interlace].name, t2, dct ); \
951         if( memcmp( t1, t2, size*size*sizeof(dctcoef) ) ) \
952         { \
953             ok = 0; \
954             for( int i = 0; i < 2; i++ ) \
955             { \
956                 dctcoef *d = (dctcoef*)(i ? t2 : t1); \
957                 for( int j = 0; j < size; j++ ) \
958                 { \
959                     for( int k = 0; k < size; k++ ) \
960                         fprintf( stderr, "%2d ", d[k+j*8] ); \
961                     fprintf( stderr, "\n" ); \
962                 } \
963                 fprintf( stderr, "\n" ); \
964             } \
965             fprintf( stderr, #name " [FAILED]\n" ); \
966         } \
967     }
968
969 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
970     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
971     { \
972         int nz_a, nz_c; \
973         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
974         used_asm = 1; \
975         memcpy( pbuf3, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
976         memcpy( pbuf4, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
977         nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
978         nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
979         if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE*sizeof(pixel) ) || nz_c != nz_a ) \
980         { \
981             ok = 0; \
982             fprintf( stderr, #name " [FAILED]\n" ); \
983         } \
984         call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
985         call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
986     }
987
988 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
989     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
990     { \
991         int nz_a, nz_c; \
992         dctcoef dc_a, dc_c; \
993         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
994         used_asm = 1; \
995         for( int i = 0; i < 2; i++ ) \
996         { \
997             memcpy( pbuf3, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
998             memcpy( pbuf4, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
999             for( int j = 0; j < 4; j++ ) \
1000             { \
1001                 memcpy( pbuf3 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
1002                 memcpy( pbuf4 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
1003             } \
1004             nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
1005             nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
1006             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 ) \
1007             { \
1008                 ok = 0; \
1009                 fprintf( stderr, #name " [FAILED]\n" ); \
1010                 break; \
1011             } \
1012         } \
1013         call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
1014         call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
1015     }
1016
1017 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
1018     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
1019     { \
1020         for( int j = 0; j < 100; j++ ) \
1021         { \
1022             set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
1023             used_asm = 1; \
1024             memcpy(dct, buf1, size*sizeof(dctcoef)); \
1025             for( int i = 0; i < size; i++ ) \
1026                 dct[i] = rand()&0x1F ? 0 : dct[i]; \
1027             memcpy(buf3, buf4, 10); \
1028             call_c( zigzag_c[interlace].name, t1, dct, buf3 ); \
1029             call_a( zigzag_asm[interlace].name, t2, dct, buf4 ); \
1030             if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( buf3, buf4, 10 ) ) \
1031             { \
1032                 ok = 0; \
1033             } \
1034         } \
1035     }
1036
1037     x264_zigzag_init( 0, &zigzag_c[0], &zigzag_c[1] );
1038     x264_zigzag_init( cpu_ref, &zigzag_ref[0], &zigzag_ref[1] );
1039     x264_zigzag_init( cpu_new, &zigzag_asm[0], &zigzag_asm[1] );
1040
1041     ok = 1; used_asm = 0;
1042     TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct1[0], 64 );
1043     report( "zigzag_interleave :" );
1044
1045     for( interlace = 0; interlace <= 1; interlace++ )
1046     {
1047         ok = 1; used_asm = 0;
1048         TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, dct1[0], 8 );
1049         TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 4 );
1050         TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
1051         TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
1052         report( interlace ? "zigzag_field :" : "zigzag_frame :" );
1053     }
1054 #undef TEST_ZIGZAG_SCAN
1055 #undef TEST_ZIGZAG_SUB
1056
1057     return ret;
1058 }
1059
1060 static int check_mc( int cpu_ref, int cpu_new )
1061 {
1062     x264_mc_functions_t mc_c;
1063     x264_mc_functions_t mc_ref;
1064     x264_mc_functions_t mc_a;
1065     x264_pixel_function_t pixf;
1066
1067     pixel *src     = &(pbuf1)[2*64+2];
1068     pixel *src2[4] = { &(pbuf1)[3*64+2], &(pbuf1)[5*64+2],
1069                        &(pbuf1)[7*64+2], &(pbuf1)[9*64+2] };
1070     pixel *dst1    = pbuf3;
1071     pixel *dst2    = pbuf4;
1072
1073     int ret = 0, ok, used_asm;
1074
1075     x264_mc_init( 0, &mc_c );
1076     x264_mc_init( cpu_ref, &mc_ref );
1077     x264_mc_init( cpu_new, &mc_a );
1078     x264_pixel_init( 0, &pixf );
1079
1080 #define MC_TEST_LUMA( w, h ) \
1081         if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
1082         { \
1083             const x264_weight_t *weight = x264_weight_none; \
1084             set_func_name( "mc_luma_%dx%d", w, h ); \
1085             used_asm = 1; \
1086             for( int i = 0; i < 1024; i++ ) \
1087                 pbuf3[i] = pbuf4[i] = 0xCD; \
1088             call_c( mc_c.mc_luma, dst1, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1089             call_a( mc_a.mc_luma, dst2, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1090             if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1091             { \
1092                 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
1093                 ok = 0; \
1094             } \
1095         } \
1096         if( mc_a.get_ref != mc_ref.get_ref ) \
1097         { \
1098             pixel *ref = dst2; \
1099             intptr_t ref_stride = 32; \
1100             int w_checked = ( ( sizeof(pixel) == 2 && (w == 12 || w == 20)) ? w-2 : w ); \
1101             const x264_weight_t *weight = x264_weight_none; \
1102             set_func_name( "get_ref_%dx%d", w_checked, h ); \
1103             used_asm = 1; \
1104             for( int i = 0; i < 1024; i++ ) \
1105                 pbuf3[i] = pbuf4[i] = 0xCD; \
1106             call_c( mc_c.mc_luma, dst1, (intptr_t)32, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1107             ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, (intptr_t)64, dx, dy, w, h, weight ); \
1108             for( int i = 0; i < h; i++ ) \
1109                 if( memcmp( dst1+i*32, ref+i*ref_stride, w_checked * sizeof(pixel) ) ) \
1110                 { \
1111                     fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w_checked, h ); \
1112                     ok = 0; \
1113                     break; \
1114                 } \
1115         }
1116
1117 #define MC_TEST_CHROMA( w, h ) \
1118         if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
1119         { \
1120             set_func_name( "mc_chroma_%dx%d", w, h ); \
1121             used_asm = 1; \
1122             for( int i = 0; i < 1024; i++ ) \
1123                 pbuf3[i] = pbuf4[i] = 0xCD; \
1124             call_c( mc_c.mc_chroma, dst1, dst1+8, (intptr_t)16, src, (intptr_t)64, dx, dy, w, h ); \
1125             call_a( mc_a.mc_chroma, dst2, dst2+8, (intptr_t)16, src, (intptr_t)64, dx, dy, w, h ); \
1126             /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \
1127             for( int j = 0; j < h; j++ ) \
1128                 for( int i = w; i < 8; i++ ) \
1129                 { \
1130                     dst2[i+j*16+8] = dst1[i+j*16+8]; \
1131                     dst2[i+j*16  ] = dst1[i+j*16  ]; \
1132                 } \
1133             if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1134             { \
1135                 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
1136                 ok = 0; \
1137             } \
1138         }
1139     ok = 1; used_asm = 0;
1140     for( int dy = -8; dy < 8; dy++ )
1141         for( int dx = -128; dx < 128; dx++ )
1142         {
1143             if( rand()&15 ) continue; // running all of them is too slow
1144             MC_TEST_LUMA( 20, 18 );
1145             MC_TEST_LUMA( 16, 16 );
1146             MC_TEST_LUMA( 16, 8 );
1147             MC_TEST_LUMA( 12, 10 );
1148             MC_TEST_LUMA( 8, 16 );
1149             MC_TEST_LUMA( 8, 8 );
1150             MC_TEST_LUMA( 8, 4 );
1151             MC_TEST_LUMA( 4, 8 );
1152             MC_TEST_LUMA( 4, 4 );
1153         }
1154     report( "mc luma :" );
1155
1156     ok = 1; used_asm = 0;
1157     for( int dy = -1; dy < 9; dy++ )
1158         for( int dx = -128; dx < 128; dx++ )
1159         {
1160             if( rand()&15 ) continue;
1161             MC_TEST_CHROMA( 8, 8 );
1162             MC_TEST_CHROMA( 8, 4 );
1163             MC_TEST_CHROMA( 4, 8 );
1164             MC_TEST_CHROMA( 4, 4 );
1165             MC_TEST_CHROMA( 4, 2 );
1166             MC_TEST_CHROMA( 2, 4 );
1167             MC_TEST_CHROMA( 2, 2 );
1168         }
1169     report( "mc chroma :" );
1170 #undef MC_TEST_LUMA
1171 #undef MC_TEST_CHROMA
1172
1173 #define MC_TEST_AVG( name, weight ) \
1174 { \
1175     for( int i = 0; i < 12; i++ ) \
1176     { \
1177         memcpy( pbuf3, pbuf1+320, 320 * sizeof(pixel) ); \
1178         memcpy( pbuf4, pbuf1+320, 320 * sizeof(pixel) ); \
1179         if( mc_a.name[i] != mc_ref.name[i] ) \
1180         { \
1181             set_func_name( "%s_%s", #name, pixel_names[i] ); \
1182             used_asm = 1; \
1183             call_c1( mc_c.name[i], pbuf3, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1184             call_a1( mc_a.name[i], pbuf4, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1185             if( memcmp( pbuf3, pbuf4, 320 * sizeof(pixel) ) ) \
1186             { \
1187                 ok = 0; \
1188                 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
1189             } \
1190             call_c2( mc_c.name[i], pbuf3, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1191             call_a2( mc_a.name[i], pbuf4, (intptr_t)16, pbuf2+1, (intptr_t)16, pbuf1+18, (intptr_t)16, weight ); \
1192         } \
1193     } \
1194 }
1195
1196     ok = 1, used_asm = 0;
1197     for( int w = -63; w <= 127 && ok; w++ )
1198         MC_TEST_AVG( avg, w );
1199     report( "mc wpredb :" );
1200
1201 #define MC_TEST_WEIGHT( name, weight, aligned ) \
1202     int align_off = (aligned ? 0 : rand()%16); \
1203     for( int i = 1; i <= 5; i++ ) \
1204     { \
1205         ALIGNED_16( pixel buffC[640] ); \
1206         ALIGNED_16( pixel buffA[640] ); \
1207         int j = X264_MAX( i*4, 2 ); \
1208         memset( buffC, 0, 640 * sizeof(pixel) ); \
1209         memset( buffA, 0, 640 * sizeof(pixel) ); \
1210         x264_t ha; \
1211         ha.mc = mc_a; \
1212         /* w12 is the same as w16 in some cases */ \
1213         if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
1214             continue; \
1215         if( mc_a.name[i] != mc_ref.name[i] ) \
1216         { \
1217             set_func_name( "%s_w%d", #name, j ); \
1218             used_asm = 1; \
1219             call_c1( mc_c.weight[i],     buffC, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1220             mc_a.weight_cache(&ha, &weight); \
1221             call_a1( weight.weightfn[i], buffA, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1222             for( int k = 0; k < 16; k++ ) \
1223                 if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \
1224                 { \
1225                     ok = 0; \
1226                     fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
1227                     break; \
1228                 } \
1229             call_c2( mc_c.weight[i],     buffC, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1230             call_a2( weight.weightfn[i], buffA, (intptr_t)32, pbuf2+align_off, (intptr_t)32, &weight, 16 ); \
1231         } \
1232     }
1233
1234     ok = 1; used_asm = 0;
1235
1236     int align_cnt = 0;
1237     for( int s = 0; s <= 127 && ok; s++ )
1238     {
1239         for( int o = -128; o <= 127 && ok; o++ )
1240         {
1241             if( rand() & 2047 ) continue;
1242             for( int d = 0; d <= 7 && ok; d++ )
1243             {
1244                 if( s == 1<<d )
1245                     continue;
1246                 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
1247                 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
1248             }
1249         }
1250
1251     }
1252     report( "mc weight :" );
1253
1254     ok = 1; used_asm = 0;
1255     for( int o = 0; o <= 127 && ok; o++ )
1256     {
1257         int s = 1, d = 0;
1258         if( rand() & 15 ) continue;
1259         x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1260         MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
1261     }
1262     report( "mc offsetadd :" );
1263     ok = 1; used_asm = 0;
1264     for( int o = -128; o < 0 && ok; o++ )
1265     {
1266         int s = 1, d = 0;
1267         if( rand() & 15 ) continue;
1268         x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1269         MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
1270     }
1271     report( "mc offsetsub :" );
1272
1273     ok = 1; used_asm = 0;
1274     for( int height = 8; height <= 16; height += 8 )
1275     {
1276         if( mc_a.store_interleave_chroma != mc_ref.store_interleave_chroma )
1277         {
1278             set_func_name( "store_interleave_chroma" );
1279             used_asm = 1;
1280             memset( pbuf3, 0, 64*height );
1281             memset( pbuf4, 0, 64*height );
1282             call_c( mc_c.store_interleave_chroma, pbuf3, (intptr_t)64, pbuf1, pbuf1+16, height );
1283             call_a( mc_a.store_interleave_chroma, pbuf4, (intptr_t)64, pbuf1, pbuf1+16, height );
1284             if( memcmp( pbuf3, pbuf4, 64*height ) )
1285             {
1286                 ok = 0;
1287                 fprintf( stderr, "store_interleave_chroma FAILED: h=%d\n", height );
1288                 break;
1289             }
1290         }
1291         if( mc_a.load_deinterleave_chroma_fenc != mc_ref.load_deinterleave_chroma_fenc )
1292         {
1293             set_func_name( "load_deinterleave_chroma_fenc" );
1294             used_asm = 1;
1295             call_c( mc_c.load_deinterleave_chroma_fenc, pbuf3, pbuf1, (intptr_t)64, height );
1296             call_a( mc_a.load_deinterleave_chroma_fenc, pbuf4, pbuf1, (intptr_t)64, height );
1297             if( memcmp( pbuf3, pbuf4, FENC_STRIDE*height ) )
1298             {
1299                 ok = 0;
1300                 fprintf( stderr, "load_deinterleave_chroma_fenc FAILED: h=%d\n", height );
1301                 break;
1302             }
1303         }
1304         if( mc_a.load_deinterleave_chroma_fdec != mc_ref.load_deinterleave_chroma_fdec )
1305         {
1306             set_func_name( "load_deinterleave_chroma_fdec" );
1307             used_asm = 1;
1308             call_c( mc_c.load_deinterleave_chroma_fdec, pbuf3, pbuf1, (intptr_t)64, height );
1309             call_a( mc_a.load_deinterleave_chroma_fdec, pbuf4, pbuf1, (intptr_t)64, height );
1310             if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*height ) )
1311             {
1312                 ok = 0;
1313                 fprintf( stderr, "load_deinterleave_chroma_fdec FAILED: h=%d\n", height );
1314                 break;
1315             }
1316         }
1317     }
1318     report( "store_interleave :" );
1319
1320     struct plane_spec {
1321         int w, h, src_stride;
1322     } 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} };
1323     ok = 1; used_asm = 0;
1324     if( mc_a.plane_copy != mc_ref.plane_copy )
1325     {
1326         set_func_name( "plane_copy" );
1327         used_asm = 1;
1328         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1329         {
1330             int w = plane_specs[i].w;
1331             int h = plane_specs[i].h;
1332             intptr_t src_stride = plane_specs[i].src_stride;
1333             intptr_t dst_stride = (w + 127) & ~63;
1334             assert( dst_stride * h <= 0x1000 );
1335             pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1336             memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1337             memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1338             call_c( mc_c.plane_copy, pbuf3, dst_stride, src1, src_stride, w, h );
1339             call_a( mc_a.plane_copy, pbuf4, dst_stride, src1, src_stride, w, h );
1340             for( int y = 0; y < h; y++ )
1341                 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(pixel) ) )
1342                 {
1343                     ok = 0;
1344                     fprintf( stderr, "plane_copy FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1345                     break;
1346                 }
1347         }
1348     }
1349
1350     if( mc_a.plane_copy_interleave != mc_ref.plane_copy_interleave )
1351     {
1352         set_func_name( "plane_copy_interleave" );
1353         used_asm = 1;
1354         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1355         {
1356             int w = (plane_specs[i].w + 1) >> 1;
1357             int h = plane_specs[i].h;
1358             intptr_t src_stride = (plane_specs[i].src_stride + 1) >> 1;
1359             intptr_t dst_stride = (2*w + 127) & ~63;
1360             assert( dst_stride * h <= 0x1000 );
1361             pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1362             memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1363             memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1364             call_c( mc_c.plane_copy_interleave, pbuf3, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1365             call_a( mc_a.plane_copy_interleave, pbuf4, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1366             for( int y = 0; y < h; y++ )
1367                 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) )
1368                 {
1369                     ok = 0;
1370                     fprintf( stderr, "plane_copy_interleave FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1371                     break;
1372                 }
1373         }
1374     }
1375
1376     if( mc_a.plane_copy_deinterleave != mc_ref.plane_copy_deinterleave )
1377     {
1378         set_func_name( "plane_copy_deinterleave" );
1379         used_asm = 1;
1380         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1381         {
1382             int w = (plane_specs[i].w + 1) >> 1;
1383             int h = plane_specs[i].h;
1384             intptr_t dst_stride = w;
1385             intptr_t src_stride = (2*w + 127) & ~63;
1386             intptr_t offv = (dst_stride*h + 31) & ~15;
1387             memset( pbuf3, 0, 0x1000 );
1388             memset( pbuf4, 0, 0x1000 );
1389             call_c( mc_c.plane_copy_deinterleave, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf1, src_stride, w, h );
1390             call_a( mc_a.plane_copy_deinterleave, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf1, src_stride, w, h );
1391             for( int y = 0; y < h; y++ )
1392                 if( memcmp( pbuf3+y*dst_stride,      pbuf4+y*dst_stride, w ) ||
1393                     memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w ) )
1394                 {
1395                     ok = 0;
1396                     fprintf( stderr, "plane_copy_deinterleave FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1397                     break;
1398                 }
1399         }
1400     }
1401     report( "plane_copy :" );
1402
1403     if( mc_a.hpel_filter != mc_ref.hpel_filter )
1404     {
1405         pixel *srchpel = pbuf1+8+2*64;
1406         pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
1407         pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
1408         void *tmp = pbuf3+49*64;
1409         set_func_name( "hpel_filter" );
1410         ok = 1; used_asm = 1;
1411         memset( pbuf3, 0, 4096 * sizeof(pixel) );
1412         memset( pbuf4, 0, 4096 * sizeof(pixel) );
1413         call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, (intptr_t)64, 48, 10, tmp );
1414         call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, (intptr_t)64, 48, 10, tmp );
1415         for( int i = 0; i < 3; i++ )
1416             for( int j = 0; j < 10; j++ )
1417                 //FIXME ideally the first pixels would match too, but they aren't actually used
1418                 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
1419                 {
1420                     ok = 0;
1421                     fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
1422                     for( int k = 0; k < 48; k++ )
1423                         printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
1424                     printf( "\n" );
1425                     for( int k = 0; k < 48; k++ )
1426                         printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
1427                     printf( "\n" );
1428                     break;
1429                 }
1430         report( "hpel filter :" );
1431     }
1432
1433     if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
1434     {
1435         pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
1436         pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1437         set_func_name( "lowres_init" );
1438         ok = 1; used_asm = 1;
1439         for( int w = 40; w <= 48; w += 8 )
1440         {
1441             intptr_t stride = (w+8)&~15;
1442             call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], (intptr_t)w*2, stride, w, 16 );
1443             call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], (intptr_t)w*2, stride, w, 16 );
1444             for( int i = 0; i < 16; i++ )
1445             {
1446                 for( int j = 0; j < 4; j++ )
1447                     if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w * sizeof(pixel) ) )
1448                     {
1449                         ok = 0;
1450                         fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1451                         for( int k = 0; k < w; k++ )
1452                             printf( "%d ", dstc[j][k+i*stride] );
1453                         printf( "\n" );
1454                         for( int k = 0; k < w; k++ )
1455                             printf( "%d ", dsta[j][k+i*stride] );
1456                         printf( "\n" );
1457                         break;
1458                     }
1459             }
1460         }
1461         report( "lowres init :" );
1462     }
1463
1464 #define INTEGRAL_INIT( name, size, ... )\
1465     if( mc_a.name != mc_ref.name )\
1466     {\
1467         intptr_t stride = 80;\
1468         set_func_name( #name );\
1469         used_asm = 1;\
1470         memcpy( buf3, buf1, size*2*stride );\
1471         memcpy( buf4, buf1, size*2*stride );\
1472         uint16_t *sum = (uint16_t*)buf3;\
1473         call_c1( mc_c.name, __VA_ARGS__ );\
1474         sum = (uint16_t*)buf4;\
1475         call_a1( mc_a.name, __VA_ARGS__ );\
1476         if( memcmp( buf3, buf4, (stride-8)*2 ) \
1477             || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
1478             ok = 0;\
1479         call_c2( mc_c.name, __VA_ARGS__ );\
1480         call_a2( mc_a.name, __VA_ARGS__ );\
1481     }
1482     ok = 1; used_asm = 0;
1483     INTEGRAL_INIT( integral_init4h, 2, sum+stride, pbuf2, stride );
1484     INTEGRAL_INIT( integral_init8h, 2, sum+stride, pbuf2, stride );
1485     INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
1486     INTEGRAL_INIT( integral_init8v, 9, sum, stride );
1487     report( "integral init :" );
1488
1489     if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1490     {
1491         ok = 1; used_asm = 1;
1492         x264_emms();
1493         for( int i = 0; i < 10; i++ )
1494         {
1495             float fps_factor = (rand()&65535) / 256.;
1496             set_func_name( "mbtree_propagate" );
1497             int *dsta = (int*)buf3;
1498             int *dstc = dsta+400;
1499             uint16_t *prop = (uint16_t*)buf1;
1500             uint16_t *intra = (uint16_t*)buf4;
1501             uint16_t *inter = intra+128;
1502             uint16_t *qscale = inter+128;
1503             uint16_t *rnd = (uint16_t*)buf2;
1504             x264_emms();
1505             for( int j = 0; j < 100; j++ )
1506             {
1507                 intra[j]  = *rnd++ & 0x7fff;
1508                 intra[j] += !intra[j];
1509                 inter[j]  = *rnd++ & 0x7fff;
1510                 qscale[j] = *rnd++ & 0x7fff;
1511             }
1512             call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, &fps_factor, 100 );
1513             call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, &fps_factor, 100 );
1514             // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1515             x264_emms();
1516             for( int j = 0; j < 100 && ok; j++ )
1517             {
1518                 ok &= abs( dstc[j]-dsta[j] ) <= 1 || fabs( (double)dstc[j]/dsta[j]-1 ) < 1e-4;
1519                 if( !ok )
1520                     fprintf( stderr, "mbtree_propagate FAILED: %f !~= %f\n", (double)dstc[j], (double)dsta[j] );
1521             }
1522         }
1523         report( "mbtree propagate :" );
1524     }
1525
1526     if( mc_a.memcpy_aligned != mc_ref.memcpy_aligned )
1527     {
1528         set_func_name( "memcpy_aligned" );
1529         ok = 1; used_asm = 1;
1530         for( size_t size = 16; size < 256; size += 16 )
1531         {
1532             memset( buf4, 0xAA, size + 1 );
1533             call_c( mc_c.memcpy_aligned, buf3, buf1, size );
1534             call_a( mc_a.memcpy_aligned, buf4, buf1, size );
1535             if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1536             {
1537                 ok = 0;
1538                 fprintf( stderr, "memcpy_aligned FAILED: size=%d\n", (int)size );
1539                 break;
1540             }
1541         }
1542         report( "memcpy aligned :" );
1543     }
1544
1545     if( mc_a.memzero_aligned != mc_ref.memzero_aligned )
1546     {
1547         set_func_name( "memzero_aligned" );
1548         ok = 1; used_asm = 1;
1549         for( size_t size = 128; size < 1024; size += 128 )
1550         {
1551             memset( buf4, 0xAA, size + 1 );
1552             call_c( mc_c.memzero_aligned, buf3, size );
1553             call_a( mc_a.memzero_aligned, buf4, size );
1554             if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1555             {
1556                 ok = 0;
1557                 fprintf( stderr, "memzero_aligned FAILED: size=%d\n", (int)size );
1558                 break;
1559             }
1560         }
1561         report( "memzero aligned :" );
1562     }
1563
1564     return ret;
1565 }
1566
1567 static int check_deblock( int cpu_ref, int cpu_new )
1568 {
1569     x264_deblock_function_t db_c;
1570     x264_deblock_function_t db_ref;
1571     x264_deblock_function_t db_a;
1572     int ret = 0, ok = 1, used_asm = 0;
1573     int alphas[36], betas[36];
1574     int8_t tcs[36][4];
1575
1576     x264_deblock_init( 0, &db_c, 0 );
1577     x264_deblock_init( cpu_ref, &db_ref, 0 );
1578     x264_deblock_init( cpu_new, &db_a, 0 );
1579
1580     /* not exactly the real values of a,b,tc but close enough */
1581     for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1582     {
1583         alphas[i] = a << (BIT_DEPTH-8);
1584         betas[i] = (i+1)/2 << (BIT_DEPTH-8);
1585         tcs[i][0] = tcs[i][3] = (c+6)/10 << (BIT_DEPTH-8);
1586         tcs[i][1] = (c+7)/15 << (BIT_DEPTH-8);
1587         tcs[i][2] = (c+9)/20 << (BIT_DEPTH-8);
1588         a = a*9/10;
1589         c = c*9/10;
1590     }
1591
1592 #define TEST_DEBLOCK( name, align, ... ) \
1593     for( int i = 0; i < 36; i++ ) \
1594     { \
1595         intptr_t off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1596         for( int j = 0; j < 1024; j++ ) \
1597             /* two distributions of random to excersize different failure modes */ \
1598             pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \
1599         memcpy( pbuf4, pbuf3, 1024 * sizeof(pixel) ); \
1600         if( db_a.name != db_ref.name ) \
1601         { \
1602             set_func_name( #name ); \
1603             used_asm = 1; \
1604             call_c1( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1605             call_a1( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1606             if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1607             { \
1608                 ok = 0; \
1609                 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1610                 break; \
1611             } \
1612             call_c2( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1613             call_a2( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1614         } \
1615     }
1616
1617     TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1618     TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1619     TEST_DEBLOCK( deblock_h_chroma_420, 0, tcs[i] );
1620     TEST_DEBLOCK( deblock_h_chroma_422, 0, tcs[i] );
1621     TEST_DEBLOCK( deblock_chroma_420_mbaff, 0, tcs[i] );
1622     TEST_DEBLOCK( deblock_chroma_422_mbaff, 0, tcs[i] );
1623     TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1624     TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1625     TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1626     TEST_DEBLOCK( deblock_h_chroma_420_intra, 0 );
1627     TEST_DEBLOCK( deblock_h_chroma_422_intra, 0 );
1628     TEST_DEBLOCK( deblock_chroma_420_intra_mbaff, 0 );
1629     TEST_DEBLOCK( deblock_chroma_422_intra_mbaff, 0 );
1630     TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1631
1632     if( db_a.deblock_strength != db_ref.deblock_strength )
1633     {
1634         for( int i = 0; i < 100; i++ )
1635         {
1636             ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1637             ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1638             ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] );
1639             ALIGNED_ARRAY_16( uint8_t, bs, [2],[2][8][4] );
1640             memset( bs, 99, sizeof(bs) );
1641             for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1642                 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1643             for( int j = 0; j < 2; j++ )
1644                 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1645                 {
1646                     ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1647                     for( int l = 0; l < 2; l++ )
1648                         mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1649                 }
1650             set_func_name( "deblock_strength" );
1651             call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1652             call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1653             if( memcmp( bs[0], bs[1], sizeof(bs[0]) ) )
1654             {
1655                 ok = 0;
1656                 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1657                 for( int j = 0; j < 2; j++ )
1658                 {
1659                     for( int k = 0; k < 2; k++ )
1660                         for( int l = 0; l < 4; l++ )
1661                         {
1662                             for( int m = 0; m < 4; m++ )
1663                                 printf("%d ",bs[j][k][l][m]);
1664                             printf("\n");
1665                         }
1666                     printf("\n");
1667                 }
1668                 break;
1669             }
1670         }
1671     }
1672
1673     report( "deblock :" );
1674
1675     return ret;
1676 }
1677
1678 static int check_quant( int cpu_ref, int cpu_new )
1679 {
1680     x264_quant_function_t qf_c;
1681     x264_quant_function_t qf_ref;
1682     x264_quant_function_t qf_a;
1683     ALIGNED_16( dctcoef dct1[64] );
1684     ALIGNED_16( dctcoef dct2[64] );
1685     ALIGNED_16( dctcoef dct3[8][16] );
1686     ALIGNED_16( dctcoef dct4[8][16] );
1687     ALIGNED_16( uint8_t cqm_buf[64] );
1688     int ret = 0, ok, used_asm;
1689     int oks[3] = {1,1,1}, used_asms[3] = {0,0,0};
1690     x264_t h_buf;
1691     x264_t *h = &h_buf;
1692     memset( h, 0, sizeof(*h) );
1693     h->sps->i_chroma_format_idc = 1;
1694     x264_param_default( &h->param );
1695     h->chroma_qp_table = i_chroma_qp_table + 12;
1696     h->param.analyse.b_transform_8x8 = 1;
1697
1698     for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1699     {
1700         if( i_cqm == 0 )
1701         {
1702             for( int i = 0; i < 6; i++ )
1703                 h->pps->scaling_list[i] = x264_cqm_flat16;
1704             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1705         }
1706         else if( i_cqm == 1 )
1707         {
1708             for( int i = 0; i < 6; i++ )
1709                 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1710             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1711         }
1712         else
1713         {
1714             int max_scale = BIT_DEPTH < 10 ? 255 : 228;
1715             if( i_cqm == 2 )
1716                 for( int i = 0; i < 64; i++ )
1717                     cqm_buf[i] = 10 + rand() % (max_scale - 9);
1718             else
1719                 for( int i = 0; i < 64; i++ )
1720                     cqm_buf[i] = 1;
1721             for( int i = 0; i < 6; i++ )
1722                 h->pps->scaling_list[i] = cqm_buf;
1723             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1724         }
1725
1726         h->param.rc.i_qp_min = 0;
1727         h->param.rc.i_qp_max = QP_MAX;
1728         x264_cqm_init( h );
1729         x264_quant_init( h, 0, &qf_c );
1730         x264_quant_init( h, cpu_ref, &qf_ref );
1731         x264_quant_init( h, cpu_new, &qf_a );
1732
1733 #define INIT_QUANT8(j) \
1734         { \
1735             static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1736             for( int i = 0; i < 64; i++ ) \
1737             { \
1738                 unsigned int scale = (255*scale1d[i>>3]*scale1d[i&7])/16; \
1739                 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1740             } \
1741         }
1742
1743 #define INIT_QUANT4(j) \
1744         { \
1745             static const int scale1d[4] = {4,6,4,6}; \
1746             for( int i = 0; i < 16; i++ ) \
1747             { \
1748                 unsigned int scale = 255*scale1d[i>>2]*scale1d[i&3]; \
1749                 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1750             } \
1751         }
1752
1753 #define TEST_QUANT_DC( name, cqm ) \
1754         if( qf_a.name != qf_ref.name ) \
1755         { \
1756             set_func_name( #name ); \
1757             used_asms[0] = 1; \
1758             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1759             { \
1760                 for( int j = 0; j < 2; j++ ) \
1761                 { \
1762                     int result_c, result_a; \
1763                     for( int i = 0; i < 16; i++ ) \
1764                         dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1765                     result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1766                     result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1767                     if( memcmp( dct1, dct2, 16*sizeof(dctcoef) ) || result_c != result_a ) \
1768                     { \
1769                         oks[0] = 0; \
1770                         fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1771                         break; \
1772                     } \
1773                     call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1774                     call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1775                 } \
1776             } \
1777         }
1778
1779 #define TEST_QUANT( qname, block, w ) \
1780         if( qf_a.qname != qf_ref.qname ) \
1781         { \
1782             set_func_name( #qname ); \
1783             used_asms[0] = 1; \
1784             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1785             { \
1786                 for( int j = 0; j < 2; j++ ) \
1787                 { \
1788                     INIT_QUANT##w(j) \
1789                     int result_c = call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1790                     int result_a = call_a1( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1791                     if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \
1792                     { \
1793                         oks[0] = 0; \
1794                         fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1795                         break; \
1796                     } \
1797                     call_c2( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1798                     call_a2( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1799                 } \
1800             } \
1801         }
1802
1803         TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1804         TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1805         TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1806         TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1807         TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1808         TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1809
1810 #define TEST_DEQUANT( qname, dqname, block, w ) \
1811         if( qf_a.dqname != qf_ref.dqname ) \
1812         { \
1813             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1814             used_asms[1] = 1; \
1815             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1816             { \
1817                 INIT_QUANT##w(1) \
1818                 qf_c.qname( dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1819                 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1820                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1821                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1822                 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1823                 { \
1824                     oks[1] = 0; \
1825                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1826                     break; \
1827                 } \
1828                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1829                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1830             } \
1831         }
1832
1833         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1834         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1835         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1836         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1837
1838 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1839         if( qf_a.dqname != qf_ref.dqname ) \
1840         { \
1841             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1842             used_asms[1] = 1; \
1843             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1844             { \
1845                 for( int i = 0; i < 16; i++ ) \
1846                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16; \
1847                 qf_c.qname( dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1848                 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1849                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1850                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1851                 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1852                 { \
1853                     oks[1] = 0; \
1854                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1855                 } \
1856                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1857                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1858             } \
1859         }
1860
1861         TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1862
1863         if( qf_a.idct_dequant_2x4_dc != qf_ref.idct_dequant_2x4_dc )
1864         {
1865             set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1866             used_asms[1] = 1;
1867             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1868             {
1869                 for( int i = 0; i < 8; i++ )
1870                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1871                 qf_c.quant_2x2_dc( &dct1[0], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
1872                 qf_c.quant_2x2_dc( &dct1[4], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
1873                 call_c( qf_c.idct_dequant_2x4_dc, dct1, dct3, h->dequant4_mf[CQM_4IC], qp+3 );
1874                 call_a( qf_a.idct_dequant_2x4_dc, dct1, dct4, h->dequant4_mf[CQM_4IC], qp+3 );
1875                 for( int i = 0; i < 8; i++ )
1876                     if( dct3[i][0] != dct4[i][0] )
1877                     {
1878                         oks[1] = 0;
1879                         fprintf( stderr, "idct_dequant_2x4_dc (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1880                         break;
1881                     }
1882             }
1883         }
1884
1885         if( qf_a.idct_dequant_2x4_dconly != qf_ref.idct_dequant_2x4_dconly )
1886         {
1887             set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1888             used_asms[1] = 1;
1889             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1890             {
1891                 for( int i = 0; i < 8; i++ )
1892                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1893                 qf_c.quant_2x2_dc( &dct1[0], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
1894                 qf_c.quant_2x2_dc( &dct1[4], h->quant4_mf[CQM_4IC][qp+3][0]>>1, h->quant4_bias[CQM_4IC][qp+3][0]>>1 );
1895                 memcpy( dct2, dct1, 8*sizeof(dctcoef) );
1896                 call_c1( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1897                 call_a1( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1898                 if( memcmp( dct1, dct2, 8*sizeof(dctcoef) ) )
1899                 {
1900                     oks[1] = 0;
1901                     fprintf( stderr, "idct_dequant_2x4_dconly (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1902                     break;
1903                 }
1904                 call_c2( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1905                 call_a2( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1906             }
1907         }
1908
1909 #define TEST_OPTIMIZE_CHROMA_DC( optname, size ) \
1910         if( qf_a.optname != qf_ref.optname ) \
1911         { \
1912             set_func_name( #optname ); \
1913             used_asms[2] = 1; \
1914             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1915             { \
1916                 int qpdc = qp + (size == 8 ? 3 : 0); \
1917                 int dmf = h->dequant4_mf[CQM_4IC][qpdc%6][0] << qpdc/6; \
1918                 if( dmf > 32*64 ) \
1919                     continue; \
1920                 for( int i = 16; ; i <<= 1 ) \
1921                 { \
1922                     int res_c, res_asm; \
1923                     int max = X264_MIN( i, PIXEL_MAX*16 ); \
1924                     for( int j = 0; j < size; j++ ) \
1925                         dct1[j] = rand()%(max*2+1) - max; \
1926                     for( int j = 0; i <= size; j += 4 ) \
1927                         qf_c.quant_2x2_dc( &dct1[j], h->quant4_mf[CQM_4IC][qpdc][0]>>1, h->quant4_bias[CQM_4IC][qpdc][0]>>1 ); \
1928                     memcpy( dct2, dct1, size*sizeof(dctcoef) ); \
1929                     res_c   = call_c1( qf_c.optname, dct1, dmf ); \
1930                     res_asm = call_a1( qf_a.optname, dct2, dmf ); \
1931                     if( res_c != res_asm || memcmp( dct1, dct2, size*sizeof(dctcoef) ) ) \
1932                     { \
1933                         oks[2] = 0; \
1934                         fprintf( stderr, #optname "(qp=%d, res_c=%d, res_asm=%d): [FAILED]\n", qp, res_c, res_asm ); \
1935                     } \
1936                     call_c2( qf_c.optname, dct1, dmf ); \
1937                     call_a2( qf_a.optname, dct2, dmf ); \
1938                     if( i >= PIXEL_MAX*16 ) \
1939                         break; \
1940                 } \
1941             } \
1942         }
1943
1944         TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x2_dc, 4 );
1945         TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x4_dc, 8 );
1946
1947         x264_cqm_delete( h );
1948     }
1949
1950     ok = oks[0]; used_asm = used_asms[0];
1951     report( "quant :" );
1952
1953     ok = oks[1]; used_asm = used_asms[1];
1954     report( "dequant :" );
1955
1956     ok = oks[2]; used_asm = used_asms[2];
1957     report( "optimize chroma dc :" );
1958
1959     ok = 1; used_asm = 0;
1960     if( qf_a.denoise_dct != qf_ref.denoise_dct )
1961     {
1962         used_asm = 1;
1963         for( int size = 16; size <= 64; size += 48 )
1964         {
1965             set_func_name( "denoise_dct" );
1966             memcpy( dct1, buf1, size*sizeof(dctcoef) );
1967             memcpy( dct2, buf1, size*sizeof(dctcoef) );
1968             memcpy( buf3+256, buf3, 256 );
1969             call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3,       (udctcoef*)buf2, size );
1970             call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
1971             if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1972                 ok = 0;
1973             call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3,       (udctcoef*)buf2, size );
1974             call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
1975         }
1976     }
1977     report( "denoise dct :" );
1978
1979 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1980     if( qf_a.decname != qf_ref.decname ) \
1981     { \
1982         set_func_name( #decname ); \
1983         used_asm = 1; \
1984         for( int i = 0; i < 100; i++ ) \
1985         { \
1986             static const int distrib[16] = {1,1,1,1,1,1,1,1,1,1,1,1,2,3,4};\
1987             static const int zerorate_lut[4] = {3,7,15,31};\
1988             int zero_rate = zerorate_lut[i&3];\
1989             for( int idx = 0; idx < w*w; idx++ ) \
1990             { \
1991                 int sign = (rand()&1) ? -1 : 1; \
1992                 int abs_level = distrib[rand()&15]; \
1993                 if( abs_level == 4 ) abs_level = rand()&0x3fff; \
1994                 int zero = !(rand()&zero_rate); \
1995                 dct1[idx] = zero * abs_level * sign; \
1996             } \
1997             if( ac ) \
1998                 dct1[0] = 0; \
1999             int result_c = call_c( qf_c.decname, dct1 ); \
2000             int result_a = call_a( qf_a.decname, dct1 ); \
2001             if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
2002             { \
2003                 ok = 0; \
2004                 fprintf( stderr, #decname ": [FAILED]\n" ); \
2005                 break; \
2006             } \
2007         } \
2008     }
2009
2010     ok = 1; used_asm = 0;
2011     TEST_DECIMATE( decimate_score64, 8, 0, 6 );
2012     TEST_DECIMATE( decimate_score16, 4, 0, 6 );
2013     TEST_DECIMATE( decimate_score15, 4, 1, 7 );
2014     report( "decimate_score :" );
2015
2016 #define TEST_LAST( last, lastname, size, ac ) \
2017     if( qf_a.last != qf_ref.last ) \
2018     { \
2019         set_func_name( #lastname ); \
2020         used_asm = 1; \
2021         for( int i = 0; i < 100; i++ ) \
2022         { \
2023             int nnz = 0; \
2024             int max = rand() & (size-1); \
2025             memset( dct1, 0, size*sizeof(dctcoef) ); \
2026             for( int idx = ac; idx < max; idx++ ) \
2027                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2028             if( !nnz ) \
2029                 dct1[ac] = 1; \
2030             int result_c = call_c( qf_c.last, dct1+ac ); \
2031             int result_a = call_a( qf_a.last, dct1+ac ); \
2032             if( result_c != result_a ) \
2033             { \
2034                 ok = 0; \
2035                 fprintf( stderr, #lastname ": [FAILED]\n" ); \
2036                 break; \
2037             } \
2038         } \
2039     }
2040
2041     ok = 1; used_asm = 0;
2042     TEST_LAST( coeff_last4              , coeff_last4,   4, 0 );
2043     TEST_LAST( coeff_last8              , coeff_last8,   8, 0 );
2044     TEST_LAST( coeff_last[  DCT_LUMA_AC], coeff_last15, 16, 1 );
2045     TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 16, 0 );
2046     TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 64, 0 );
2047     report( "coeff_last :" );
2048
2049 #define TEST_LEVELRUN( lastname, name, size, ac ) \
2050     if( qf_a.lastname != qf_ref.lastname ) \
2051     { \
2052         set_func_name( #name ); \
2053         used_asm = 1; \
2054         for( int i = 0; i < 100; i++ ) \
2055         { \
2056             x264_run_level_t runlevel_c, runlevel_a; \
2057             int nnz = 0; \
2058             int max = rand() & (size-1); \
2059             memset( dct1, 0, size*sizeof(dctcoef) ); \
2060             memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
2061             memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
2062             for( int idx = ac; idx < max; idx++ ) \
2063                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2064             if( !nnz ) \
2065                 dct1[ac] = 1; \
2066             int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
2067             int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
2068             if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
2069                 runlevel_c.mask != runlevel_a.mask || \
2070                 memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c)) \
2071             { \
2072                 ok = 0; \
2073                 fprintf( stderr, #name ": [FAILED]\n" ); \
2074                 break; \
2075             } \
2076         } \
2077     }
2078
2079     ok = 1; used_asm = 0;
2080     TEST_LEVELRUN( coeff_level_run4              , coeff_level_run4,   4, 0 );
2081     TEST_LEVELRUN( coeff_level_run8              , coeff_level_run8,   8, 0 );
2082     TEST_LEVELRUN( coeff_level_run[  DCT_LUMA_AC], coeff_level_run15, 16, 1 );
2083     TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 16, 0 );
2084     report( "coeff_level_run :" );
2085
2086     return ret;
2087 }
2088
2089 static int check_intra( int cpu_ref, int cpu_new )
2090 {
2091     int ret = 0, ok = 1, used_asm = 0;
2092     ALIGNED_ARRAY_32( pixel, edge,[36] );
2093     ALIGNED_ARRAY_32( pixel, edge2,[36] );
2094     ALIGNED_16( pixel fdec[FDEC_STRIDE*20] );
2095     struct
2096     {
2097         x264_predict_t      predict_16x16[4+3];
2098         x264_predict_t      predict_8x8c[4+3];
2099         x264_predict_t      predict_8x16c[4+3];
2100         x264_predict8x8_t   predict_8x8[9+3];
2101         x264_predict_t      predict_4x4[9+3];
2102         x264_predict_8x8_filter_t predict_8x8_filter;
2103     } ip_c, ip_ref, ip_a;
2104
2105     x264_predict_16x16_init( 0, ip_c.predict_16x16 );
2106     x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
2107     x264_predict_8x16c_init( 0, ip_c.predict_8x16c );
2108     x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
2109     x264_predict_4x4_init( 0, ip_c.predict_4x4 );
2110
2111     x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
2112     x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
2113     x264_predict_8x16c_init( cpu_ref, ip_ref.predict_8x16c );
2114     x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
2115     x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
2116
2117     x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
2118     x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
2119     x264_predict_8x16c_init( cpu_new, ip_a.predict_8x16c );
2120     x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
2121     x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
2122
2123     memcpy( fdec, pbuf1, 32*20 * sizeof(pixel) );\
2124
2125     ip_c.predict_8x8_filter( fdec+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
2126
2127 #define INTRA_TEST( name, dir, w, h, align, bench, ... )\
2128     if( ip_a.name[dir] != ip_ref.name[dir] )\
2129     {\
2130         set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
2131         used_asm = 1;\
2132         memcpy( pbuf3, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2133         memcpy( pbuf4, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2134         for( int a = 0; a < (do_bench ? 64/sizeof(pixel) : 1); a += align )\
2135         {\
2136             call_c##bench( ip_c.name[dir], pbuf3+48+a, ##__VA_ARGS__ );\
2137             call_a##bench( ip_a.name[dir], pbuf4+48+a, ##__VA_ARGS__ );\
2138             if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*20 * sizeof(pixel) ) )\
2139             {\
2140                 fprintf( stderr, #name "[%d] :  [FAILED]\n", dir );\
2141                 ok = 0;\
2142                 for( int k = -1; k < 16; k++ )\
2143                     printf( "%2x ", edge[16+k] );\
2144                 printf( "\n" );\
2145                 for( int j = 0; j < h; j++ )\
2146                 {\
2147                     printf( "%2x ", edge[14-j] );\
2148                     for( int k = 0; k < w; k++ )\
2149                         printf( "%2x ", pbuf4[48+k+j*FDEC_STRIDE] );\
2150                     printf( "\n" );\
2151                 }\
2152                 printf( "\n" );\
2153                 for( int j = 0; j < h; j++ )\
2154                 {\
2155                     printf( "   " );\
2156                     for( int k = 0; k < w; k++ )\
2157                         printf( "%2x ", pbuf3[48+k+j*FDEC_STRIDE] );\
2158                     printf( "\n" );\
2159                 }\
2160                 break;\
2161             }\
2162         }\
2163     }
2164
2165     for( int i = 0; i < 12; i++ )
2166         INTRA_TEST(   predict_4x4, i,  4,  4,  4, );
2167     for( int i = 0; i < 7; i++ )
2168         INTRA_TEST(  predict_8x8c, i,  8,  8, 16, );
2169     for( int i = 0; i < 7; i++ )
2170         INTRA_TEST( predict_8x16c, i,  8, 16, 16, );
2171     for( int i = 0; i < 7; i++ )
2172         INTRA_TEST( predict_16x16, i, 16, 16, 16, );
2173     for( int i = 0; i < 12; i++ )
2174         INTRA_TEST(   predict_8x8, i,  8,  8,  8, , edge );
2175
2176     set_func_name("intra_predict_8x8_filter");
2177     if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
2178     {
2179         used_asm = 1;
2180         for( int i = 0; i < 32; i++ )
2181         {
2182             if( !(i&7) || ((i&MB_TOPRIGHT) && !(i&MB_TOP)) )
2183                 continue;
2184             int neighbor = (i&24)>>1;
2185             memset( edge,  0, 36*sizeof(pixel) );
2186             memset( edge2, 0, 36*sizeof(pixel) );
2187             call_c( ip_c.predict_8x8_filter, pbuf1+48, edge,  neighbor, i&7 );
2188             call_a( ip_a.predict_8x8_filter, pbuf1+48, edge2, neighbor, i&7 );
2189             if( !(neighbor&MB_TOPLEFT) )
2190                 edge[15] = edge2[15] = 0;
2191             if( memcmp( edge+7, edge2+7, (i&MB_TOPRIGHT ? 26 : i&MB_TOP ? 17 : 8) * sizeof(pixel) ) )
2192             {
2193                 fprintf( stderr, "predict_8x8_filter :  [FAILED] %d %d\n", (i&24)>>1, i&7);
2194                 ok = 0;
2195             }
2196         }
2197     }
2198
2199 #define EXTREMAL_PLANE( w, h ) \
2200     { \
2201         int max[7]; \
2202         for( int j = 0; j < 7; j++ ) \
2203             max[j] = test ? rand()&PIXEL_MAX : PIXEL_MAX; \
2204         fdec[48-1-FDEC_STRIDE] = (i&1)*max[0]; \
2205         for( int j = 0; j < w/2; j++ ) \
2206             fdec[48+j-FDEC_STRIDE] = (!!(i&2))*max[1]; \
2207         for( int j = w/2; j < w-1; j++ ) \
2208             fdec[48+j-FDEC_STRIDE] = (!!(i&4))*max[2]; \
2209         fdec[48+(w-1)-FDEC_STRIDE] = (!!(i&8))*max[3]; \
2210         for( int j = 0; j < h/2; j++ ) \
2211             fdec[48+j*FDEC_STRIDE-1] = (!!(i&16))*max[4]; \
2212         for( int j = h/2; j < h-1; j++ ) \
2213             fdec[48+j*FDEC_STRIDE-1] = (!!(i&32))*max[5]; \
2214         fdec[48+(h-1)*FDEC_STRIDE-1] = (!!(i&64))*max[6]; \
2215     }
2216     /* Extremal test case for planar prediction. */
2217     for( int test = 0; test < 100 && ok; test++ )
2218         for( int i = 0; i < 128 && ok; i++ )
2219         {
2220             EXTREMAL_PLANE(  8,  8 );
2221             INTRA_TEST(  predict_8x8c, I_PRED_CHROMA_P,  8,  8, 64, 1 );
2222             EXTREMAL_PLANE(  8, 16 );
2223             INTRA_TEST( predict_8x16c, I_PRED_CHROMA_P,  8, 16, 64, 1 );
2224             EXTREMAL_PLANE( 16, 16 );
2225             INTRA_TEST( predict_16x16,  I_PRED_16x16_P, 16, 16, 64, 1 );
2226         }
2227     report( "intra pred :" );
2228     return ret;
2229 }
2230
2231 #define DECL_CABAC(cpu) \
2232 static void run_cabac_decision_##cpu( x264_t *h, uint8_t *dst )\
2233 {\
2234     x264_cabac_t cb;\
2235     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2236     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2237     for( int i = 0; i < 0x1000; i++ )\
2238         x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
2239 }\
2240 static void run_cabac_bypass_##cpu( x264_t *h, uint8_t *dst )\
2241 {\
2242     x264_cabac_t cb;\
2243     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2244     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2245     for( int i = 0; i < 0x1000; i++ )\
2246         x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
2247 }\
2248 static void run_cabac_terminal_##cpu( x264_t *h, uint8_t *dst )\
2249 {\
2250     x264_cabac_t cb;\
2251     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2252     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2253     for( int i = 0; i < 0x1000; i++ )\
2254         x264_cabac_encode_terminal_##cpu( &cb );\
2255 }
2256 DECL_CABAC(c)
2257 #if HAVE_MMX
2258 DECL_CABAC(asm)
2259 #else
2260 #define run_cabac_decision_asm run_cabac_decision_c
2261 #define run_cabac_bypass_asm run_cabac_bypass_c
2262 #define run_cabac_terminal_asm run_cabac_terminal_c
2263 #endif
2264
2265 static int check_cabac( int cpu_ref, int cpu_new )
2266 {
2267     int ret = 0, ok, used_asm = 1;
2268     x264_t h;
2269     h.sps->i_chroma_format_idc = 3;
2270     if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
2271         return 0;
2272     x264_cabac_init( &h );
2273
2274     set_func_name( "cabac_encode_decision" );
2275     memcpy( buf4, buf3, 0x1000 );
2276     call_c( run_cabac_decision_c, &h, buf3 );
2277     call_a( run_cabac_decision_asm, &h, buf4 );
2278     ok = !memcmp( buf3, buf4, 0x1000 );
2279     report( "cabac decision:" );
2280
2281     set_func_name( "cabac_encode_bypass" );
2282     memcpy( buf4, buf3, 0x1000 );
2283     call_c( run_cabac_bypass_c, &h, buf3 );
2284     call_a( run_cabac_bypass_asm, &h, buf4 );
2285     ok = !memcmp( buf3, buf4, 0x1000 );
2286     report( "cabac bypass:" );
2287
2288     set_func_name( "cabac_encode_terminal" );
2289     memcpy( buf4, buf3, 0x1000 );
2290     call_c( run_cabac_terminal_c, &h, buf3 );
2291     call_a( run_cabac_terminal_asm, &h, buf4 );
2292     ok = !memcmp( buf3, buf4, 0x1000 );
2293     report( "cabac terminal:" );
2294
2295     return ret;
2296 }
2297
2298 static int check_bitstream( int cpu_ref, int cpu_new )
2299 {
2300     x264_bitstream_function_t bs_c;
2301     x264_bitstream_function_t bs_ref;
2302     x264_bitstream_function_t bs_a;
2303
2304     int ret = 0, ok = 1, used_asm = 0;
2305
2306     x264_bitstream_init( 0, &bs_c );
2307     x264_bitstream_init( cpu_ref, &bs_ref );
2308     x264_bitstream_init( cpu_new, &bs_a );
2309     if( bs_a.nal_escape != bs_ref.nal_escape )
2310     {
2311         int size = 0x4000;
2312         uint8_t *input = malloc(size+100);
2313         uint8_t *output1 = malloc(size*2);
2314         uint8_t *output2 = malloc(size*2);
2315         used_asm = 1;
2316         set_func_name( "nal_escape" );
2317         for( int i = 0; i < 100; i++ )
2318         {
2319             /* Test corner-case sizes */
2320             int test_size = i < 10 ? i+1 : rand() & 0x3fff;
2321             /* Test 8 different probability distributions of zeros */
2322             for( int j = 0; j < test_size+32; j++ )
2323                 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
2324             uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
2325             uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
2326             int size_c = end_c-output1;
2327             int size_a = end_a-output2;
2328             if( size_c != size_a || memcmp( output1, output2, size_c ) )
2329             {
2330                 fprintf( stderr, "nal_escape :  [FAILED] %d %d\n", size_c, size_a );
2331                 ok = 0;
2332                 break;
2333             }
2334         }
2335         for( int j = 0; j < size+32; j++ )
2336             input[j] = rand();
2337         call_c2( bs_c.nal_escape, output1, input, input+size );
2338         call_a2( bs_a.nal_escape, output2, input, input+size );
2339         free(input);
2340         free(output1);
2341         free(output2);
2342     }
2343     report( "nal escape:" );
2344
2345     return ret;
2346 }
2347
2348 static int check_all_funcs( int cpu_ref, int cpu_new )
2349 {
2350     return check_pixel( cpu_ref, cpu_new )
2351          + check_dct( cpu_ref, cpu_new )
2352          + check_mc( cpu_ref, cpu_new )
2353          + check_intra( cpu_ref, cpu_new )
2354          + check_deblock( cpu_ref, cpu_new )
2355          + check_quant( cpu_ref, cpu_new )
2356          + check_cabac( cpu_ref, cpu_new )
2357          + check_bitstream( cpu_ref, cpu_new );
2358 }
2359
2360 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
2361 {
2362     *cpu_ref = *cpu_new;
2363     *cpu_new |= flags;
2364 #if BROKEN_STACK_ALIGNMENT
2365     *cpu_new |= X264_CPU_STACK_MOD4;
2366 #endif
2367     if( *cpu_new & X264_CPU_SSE2_IS_FAST )
2368         *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
2369     if( !quiet )
2370         fprintf( stderr, "x264: %s\n", name );
2371     return check_all_funcs( *cpu_ref, *cpu_new );
2372 }
2373
2374 static int check_all_flags( void )
2375 {
2376     int ret = 0;
2377     int cpu0 = 0, cpu1 = 0;
2378 #if HAVE_MMX
2379     if( x264_cpu_detect() & X264_CPU_MMX2 )
2380     {
2381         ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMX2, "MMX" );
2382         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
2383         cpu1 &= ~X264_CPU_CACHELINE_64;
2384 #if ARCH_X86
2385         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
2386         cpu1 &= ~X264_CPU_CACHELINE_32;
2387 #endif
2388         if( x264_cpu_detect() & X264_CPU_LZCNT )
2389         {
2390             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
2391             cpu1 &= ~X264_CPU_LZCNT;
2392         }
2393         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
2394         cpu1 &= ~X264_CPU_SLOW_CTZ;
2395     }
2396     if( x264_cpu_detect() & X264_CPU_SSE2 )
2397     {
2398         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
2399         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
2400         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
2401         cpu1 &= ~X264_CPU_CACHELINE_64;
2402         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSE2 FastShuffle" );
2403         cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
2404         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
2405         cpu1 &= ~X264_CPU_SLOW_CTZ;
2406         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSE2 SlowAtom" );
2407         cpu1 &= ~X264_CPU_SLOW_ATOM;
2408     }
2409     if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
2410     {
2411         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
2412         cpu1 &= ~X264_CPU_SSE_MISALIGN;
2413     }
2414     if( x264_cpu_detect() & X264_CPU_LZCNT )
2415     {
2416         ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
2417         cpu1 &= ~X264_CPU_LZCNT;
2418     }
2419     if( x264_cpu_detect() & X264_CPU_SSE3 )
2420     {
2421         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
2422         cpu1 &= ~X264_CPU_CACHELINE_64;
2423     }
2424     if( x264_cpu_detect() & X264_CPU_SSSE3 )
2425     {
2426         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
2427         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
2428         cpu1 &= ~X264_CPU_CACHELINE_64;
2429         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSSE3 FastShuffle" );
2430         cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
2431         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
2432         cpu1 &= ~X264_CPU_SLOW_CTZ;
2433         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
2434         cpu1 &= ~X264_CPU_SLOW_ATOM;
2435     }
2436     if( x264_cpu_detect() & X264_CPU_SSE4 )
2437         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4 | X264_CPU_SHUFFLE_IS_FAST, "SSE4" );
2438     if( x264_cpu_detect() & X264_CPU_AVX )
2439         ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX, "AVX" );
2440     if( x264_cpu_detect() & X264_CPU_XOP )
2441         ret |= add_flags( &cpu0, &cpu1, X264_CPU_XOP, "XOP" );
2442     if( x264_cpu_detect() & X264_CPU_FMA4 )
2443     {
2444         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA4, "FMA4" );
2445         cpu1 &= ~X264_CPU_FMA4;
2446     }
2447     if( x264_cpu_detect() & X264_CPU_FMA3 )
2448     {
2449         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA3, "FMA3" );
2450         cpu1 &= ~X264_CPU_FMA3;
2451     }
2452     if( x264_cpu_detect() & X264_CPU_BMI1 )
2453     {
2454         ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI1, "BMI1" );
2455         if( x264_cpu_detect() & X264_CPU_TBM )
2456         {
2457             ret |= add_flags( &cpu0, &cpu1, X264_CPU_TBM, "TBM" );
2458             cpu1 &= ~X264_CPU_TBM;
2459         }
2460         if( x264_cpu_detect() & X264_CPU_BMI2 )
2461         {
2462             ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI2, "BMI2" );
2463             cpu1 &= ~X264_CPU_BMI2;
2464         }
2465         cpu1 &= ~X264_CPU_BMI1;
2466     }
2467     if( x264_cpu_detect() & X264_CPU_AVX2 )
2468         ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX2, "AVX2" );
2469 #elif ARCH_PPC
2470     if( x264_cpu_detect() & X264_CPU_ALTIVEC )
2471     {
2472         fprintf( stderr, "x264: ALTIVEC against C\n" );
2473         ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
2474     }
2475 #elif ARCH_ARM
2476     if( x264_cpu_detect() & X264_CPU_ARMV6 )
2477         ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
2478     if( x264_cpu_detect() & X264_CPU_NEON )
2479         ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
2480     if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC )
2481         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
2482 #endif
2483     return ret;
2484 }
2485
2486 int main(int argc, char *argv[])
2487 {
2488     int ret = 0;
2489
2490     if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
2491     {
2492 #if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM
2493         fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
2494         return 1;
2495 #endif
2496         do_bench = 1;
2497         if( argv[1][7] == '=' )
2498         {
2499             bench_pattern = argv[1]+8;
2500             bench_pattern_len = strlen(bench_pattern);
2501         }
2502         argc--;
2503         argv++;
2504     }
2505
2506     int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
2507     fprintf( stderr, "x264: using random seed %u\n", seed );
2508     srand( seed );
2509
2510     buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 16*BENCH_ALIGNS );
2511     pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 16*BENCH_ALIGNS );
2512     if( !buf1 || !pbuf1 )
2513     {
2514         fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
2515         return -1;
2516     }
2517 #define INIT_POINTER_OFFSETS\
2518     buf2 = buf1 + 0xf00;\
2519     buf3 = buf2 + 0xf00;\
2520     buf4 = buf3 + 0x1000*sizeof(pixel);\
2521     pbuf2 = pbuf1 + 0xf00;\
2522     pbuf3 = (pixel*)buf3;\
2523     pbuf4 = (pixel*)buf4;
2524     INIT_POINTER_OFFSETS;
2525     for( int i = 0; i < 0x1e00; i++ )
2526     {
2527         buf1[i] = rand() & 0xFF;
2528         pbuf1[i] = rand() & PIXEL_MAX;
2529     }
2530     memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) );
2531
2532     /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
2533     if( do_bench )
2534         for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
2535         {
2536             INIT_POINTER_OFFSETS;
2537             ret |= x264_stack_pagealign( check_all_flags, i*16 );
2538             buf1 += 16;
2539             pbuf1 += 16;
2540             quiet = 1;
2541             fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
2542         }
2543     else
2544         ret = check_all_flags();
2545
2546     if( ret )
2547     {
2548         fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
2549         return -1;
2550     }
2551     fprintf( stderr, "x264: All tests passed Yeah :)\n" );
2552     if( do_bench )
2553         print_bench();
2554     return 0;
2555 }
2556