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