]> git.sesse.net Git - x264/blob - tools/checkasm.c
x86: SSE2 and SSSE3 plane_copy_deinterleave_rgb
[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
1455     if( mc_a.plane_copy_deinterleave_rgb != mc_ref.plane_copy_deinterleave_rgb )
1456     {
1457         set_func_name( "plane_copy_deinterleave_rgb" );
1458         used_asm = 1;
1459         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1460         {
1461             int w = (plane_specs[i].w + 2) >> 2;
1462             int h = plane_specs[i].h;
1463             intptr_t src_stride = plane_specs[i].src_stride;
1464             intptr_t dst_stride = ALIGN( w, 16 );
1465             intptr_t offv = dst_stride*h + 16;
1466
1467             for( int pw = 3; pw <= 4; pw++ )
1468             {
1469                 memset( pbuf3, 0, 0x1000 );
1470                 memset( pbuf4, 0, 0x1000 );
1471                 call_c( mc_c.plane_copy_deinterleave_rgb, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf3+2*offv, dst_stride, pbuf1, src_stride, pw, w, h );
1472                 call_a( mc_a.plane_copy_deinterleave_rgb, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf4+2*offv, dst_stride, pbuf1, src_stride, pw, w, h );
1473                 for( int y = 0; y < h; y++ )
1474                     if( memcmp( pbuf3+y*dst_stride+0*offv, pbuf4+y*dst_stride+0*offv, w ) ||
1475                         memcmp( pbuf3+y*dst_stride+1*offv, pbuf4+y*dst_stride+1*offv, w ) ||
1476                         memcmp( pbuf3+y*dst_stride+2*offv, pbuf4+y*dst_stride+2*offv, w ) )
1477                     {
1478                         ok = 0;
1479                         fprintf( stderr, "plane_copy_deinterleave_rgb FAILED: w=%d h=%d stride=%d pw=%d\n", w, h, (int)src_stride, pw );
1480                         break;
1481                     }
1482             }
1483         }
1484     }
1485     report( "plane_copy :" );
1486
1487     if( mc_a.plane_copy_deinterleave_v210 != mc_ref.plane_copy_deinterleave_v210 )
1488     {
1489         set_func_name( "plane_copy_deinterleave_v210" );
1490         used_asm = 1;
1491         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1492         {
1493             int w = (plane_specs[i].w + 1) >> 1;
1494             int h = plane_specs[i].h;
1495             intptr_t dst_stride = ALIGN( w, 16 );
1496             intptr_t src_stride = (w + 47) / 48 * 128 / sizeof(uint32_t);
1497             intptr_t offv = dst_stride*h + 32;
1498             memset( pbuf3, 0, 0x1000 );
1499             memset( pbuf4, 0, 0x1000 );
1500             call_c( mc_c.plane_copy_deinterleave_v210, pbuf3, dst_stride, pbuf3+offv, dst_stride, (uint32_t *)buf1, src_stride, w, h );
1501             call_a( mc_a.plane_copy_deinterleave_v210, pbuf4, dst_stride, pbuf4+offv, dst_stride, (uint32_t *)buf1, src_stride, w, h );
1502             for( int y = 0; y < h; y++ )
1503                 if( memcmp( pbuf3+y*dst_stride,      pbuf4+y*dst_stride,      w*sizeof(uint16_t) ) ||
1504                     memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w*sizeof(uint16_t) ) )
1505                 {
1506                     ok = 0;
1507                     fprintf( stderr, "plane_copy_deinterleave_v210 FAILED: w=%d h=%d stride=%d\n", w, h, (int)src_stride );
1508                     break;
1509                 }
1510         }
1511     }
1512     report( "v210 :" );
1513
1514     if( mc_a.hpel_filter != mc_ref.hpel_filter )
1515     {
1516         pixel *srchpel = pbuf1+8+2*64;
1517         pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
1518         pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
1519         void *tmp = pbuf3+49*64;
1520         set_func_name( "hpel_filter" );
1521         ok = 1; used_asm = 1;
1522         memset( pbuf3, 0, 4096 * sizeof(pixel) );
1523         memset( pbuf4, 0, 4096 * sizeof(pixel) );
1524         call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, (intptr_t)64, 48, 10, tmp );
1525         call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, (intptr_t)64, 48, 10, tmp );
1526         for( int i = 0; i < 3; i++ )
1527             for( int j = 0; j < 10; j++ )
1528                 //FIXME ideally the first pixels would match too, but they aren't actually used
1529                 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
1530                 {
1531                     ok = 0;
1532                     fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
1533                     for( int k = 0; k < 48; k++ )
1534                         printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
1535                     printf( "\n" );
1536                     for( int k = 0; k < 48; k++ )
1537                         printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
1538                     printf( "\n" );
1539                     break;
1540                 }
1541         report( "hpel filter :" );
1542     }
1543
1544     if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
1545     {
1546         pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
1547         pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1548         set_func_name( "lowres_init" );
1549         ok = 1; used_asm = 1;
1550         for( int w = 96; w <= 96+24; w += 8 )
1551         {
1552             intptr_t stride = (w*2+31)&~31;
1553             intptr_t stride_lowres = (w+31)&~31;
1554             call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], stride, stride_lowres, w, 8 );
1555             call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], stride, stride_lowres, w, 8 );
1556             for( int i = 0; i < 8; i++ )
1557             {
1558                 for( int j = 0; j < 4; j++ )
1559                     if( memcmp( dstc[j]+i*stride_lowres, dsta[j]+i*stride_lowres, w * sizeof(pixel) ) )
1560                     {
1561                         ok = 0;
1562                         fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1563                         for( int k = 0; k < w; k++ )
1564                             printf( "%d ", dstc[j][k+i*stride_lowres] );
1565                         printf( "\n" );
1566                         for( int k = 0; k < w; k++ )
1567                             printf( "%d ", dsta[j][k+i*stride_lowres] );
1568                         printf( "\n" );
1569                         break;
1570                     }
1571             }
1572         }
1573         report( "lowres init :" );
1574     }
1575
1576 #define INTEGRAL_INIT( name, size, ... )\
1577     if( mc_a.name != mc_ref.name )\
1578     {\
1579         intptr_t stride = 96;\
1580         set_func_name( #name );\
1581         used_asm = 1;\
1582         memcpy( buf3, buf1, size*2*stride );\
1583         memcpy( buf4, buf1, size*2*stride );\
1584         uint16_t *sum = (uint16_t*)buf3;\
1585         call_c1( mc_c.name, __VA_ARGS__ );\
1586         sum = (uint16_t*)buf4;\
1587         call_a1( mc_a.name, __VA_ARGS__ );\
1588         if( memcmp( buf3, buf4, (stride-8)*2 ) \
1589             || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
1590             ok = 0;\
1591         call_c2( mc_c.name, __VA_ARGS__ );\
1592         call_a2( mc_a.name, __VA_ARGS__ );\
1593     }
1594     ok = 1; used_asm = 0;
1595     INTEGRAL_INIT( integral_init4h, 2, sum+stride, pbuf2, stride );
1596     INTEGRAL_INIT( integral_init8h, 2, sum+stride, pbuf2, stride );
1597     INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
1598     INTEGRAL_INIT( integral_init8v, 9, sum, stride );
1599     report( "integral init :" );
1600
1601     if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1602     {
1603         ok = 1; used_asm = 1;
1604         x264_emms();
1605         for( int i = 0; i < 10; i++ )
1606         {
1607             float fps_factor = (rand()&65535) / 65535.0f;
1608             set_func_name( "mbtree_propagate" );
1609             int *dsta = (int*)buf3;
1610             int *dstc = dsta+400;
1611             uint16_t *prop = (uint16_t*)buf1;
1612             uint16_t *intra = (uint16_t*)buf4;
1613             uint16_t *inter = intra+128;
1614             uint16_t *qscale = inter+128;
1615             uint16_t *rnd = (uint16_t*)buf2;
1616             x264_emms();
1617             for( int j = 0; j < 100; j++ )
1618             {
1619                 intra[j]  = *rnd++ & 0x7fff;
1620                 intra[j] += !intra[j];
1621                 inter[j]  = *rnd++ & 0x7fff;
1622                 qscale[j] = *rnd++ & 0x7fff;
1623             }
1624             call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, &fps_factor, 100 );
1625             call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, &fps_factor, 100 );
1626             // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1627             x264_emms();
1628             for( int j = 0; j < 100 && ok; j++ )
1629             {
1630                 ok &= abs( dstc[j]-dsta[j] ) <= 1 || fabs( (double)dstc[j]/dsta[j]-1 ) < 1e-4;
1631                 if( !ok )
1632                     fprintf( stderr, "mbtree_propagate FAILED: %f !~= %f\n", (double)dstc[j], (double)dsta[j] );
1633             }
1634         }
1635         report( "mbtree propagate :" );
1636     }
1637
1638     if( mc_a.memcpy_aligned != mc_ref.memcpy_aligned )
1639     {
1640         set_func_name( "memcpy_aligned" );
1641         ok = 1; used_asm = 1;
1642         for( size_t size = 16; size < 256; size += 16 )
1643         {
1644             memset( buf4, 0xAA, size + 1 );
1645             call_c( mc_c.memcpy_aligned, buf3, buf1, size );
1646             call_a( mc_a.memcpy_aligned, buf4, buf1, size );
1647             if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1648             {
1649                 ok = 0;
1650                 fprintf( stderr, "memcpy_aligned FAILED: size=%d\n", (int)size );
1651                 break;
1652             }
1653         }
1654         report( "memcpy aligned :" );
1655     }
1656
1657     if( mc_a.memzero_aligned != mc_ref.memzero_aligned )
1658     {
1659         set_func_name( "memzero_aligned" );
1660         ok = 1; used_asm = 1;
1661         for( size_t size = 128; size < 1024; size += 128 )
1662         {
1663             memset( buf4, 0xAA, size + 1 );
1664             call_c( mc_c.memzero_aligned, buf3, size );
1665             call_a( mc_a.memzero_aligned, buf4, size );
1666             if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1667             {
1668                 ok = 0;
1669                 fprintf( stderr, "memzero_aligned FAILED: size=%d\n", (int)size );
1670                 break;
1671             }
1672         }
1673         report( "memzero aligned :" );
1674     }
1675
1676     return ret;
1677 }
1678
1679 static int check_deblock( int cpu_ref, int cpu_new )
1680 {
1681     x264_deblock_function_t db_c;
1682     x264_deblock_function_t db_ref;
1683     x264_deblock_function_t db_a;
1684     int ret = 0, ok = 1, used_asm = 0;
1685     int alphas[36], betas[36];
1686     int8_t tcs[36][4];
1687
1688     x264_deblock_init( 0, &db_c, 0 );
1689     x264_deblock_init( cpu_ref, &db_ref, 0 );
1690     x264_deblock_init( cpu_new, &db_a, 0 );
1691
1692     /* not exactly the real values of a,b,tc but close enough */
1693     for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1694     {
1695         alphas[i] = a << (BIT_DEPTH-8);
1696         betas[i] = (i+1)/2 << (BIT_DEPTH-8);
1697         tcs[i][0] = tcs[i][3] = (c+6)/10 << (BIT_DEPTH-8);
1698         tcs[i][1] = (c+7)/15 << (BIT_DEPTH-8);
1699         tcs[i][2] = (c+9)/20 << (BIT_DEPTH-8);
1700         a = a*9/10;
1701         c = c*9/10;
1702     }
1703
1704 #define TEST_DEBLOCK( name, align, ... ) \
1705     for( int i = 0; i < 36; i++ ) \
1706     { \
1707         intptr_t off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1708         for( int j = 0; j < 1024; j++ ) \
1709             /* two distributions of random to excersize different failure modes */ \
1710             pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \
1711         memcpy( pbuf4, pbuf3, 1024 * sizeof(pixel) ); \
1712         if( db_a.name != db_ref.name ) \
1713         { \
1714             set_func_name( #name ); \
1715             used_asm = 1; \
1716             call_c1( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1717             call_a1( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1718             if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1719             { \
1720                 ok = 0; \
1721                 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1722                 break; \
1723             } \
1724             call_c2( db_c.name, pbuf3+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1725             call_a2( db_a.name, pbuf4+off, (intptr_t)32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1726         } \
1727     }
1728
1729     TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1730     TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1731     TEST_DEBLOCK( deblock_h_chroma_420, 0, tcs[i] );
1732     TEST_DEBLOCK( deblock_h_chroma_422, 0, tcs[i] );
1733     TEST_DEBLOCK( deblock_chroma_420_mbaff, 0, tcs[i] );
1734     TEST_DEBLOCK( deblock_chroma_422_mbaff, 0, tcs[i] );
1735     TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1736     TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1737     TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1738     TEST_DEBLOCK( deblock_h_chroma_420_intra, 0 );
1739     TEST_DEBLOCK( deblock_h_chroma_422_intra, 0 );
1740     TEST_DEBLOCK( deblock_chroma_420_intra_mbaff, 0 );
1741     TEST_DEBLOCK( deblock_chroma_422_intra_mbaff, 0 );
1742     TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1743
1744     if( db_a.deblock_strength != db_ref.deblock_strength )
1745     {
1746         for( int i = 0; i < 100; i++ )
1747         {
1748             ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1749             ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1750             ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] );
1751             ALIGNED_ARRAY_N( uint8_t, bs, [2],[2][8][4] );
1752             memset( bs, 99, sizeof(uint8_t)*2*4*8*2 );
1753             for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1754                 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1755             for( int j = 0; j < 2; j++ )
1756                 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1757                 {
1758                     ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1759                     for( int l = 0; l < 2; l++ )
1760                         mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1761                 }
1762             set_func_name( "deblock_strength" );
1763             call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1764             call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1765             if( memcmp( bs[0], bs[1], sizeof(uint8_t)*2*4*8 ) )
1766             {
1767                 ok = 0;
1768                 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1769                 for( int j = 0; j < 2; j++ )
1770                 {
1771                     for( int k = 0; k < 2; k++ )
1772                         for( int l = 0; l < 4; l++ )
1773                         {
1774                             for( int m = 0; m < 4; m++ )
1775                                 printf("%d ",bs[j][k][l][m]);
1776                             printf("\n");
1777                         }
1778                     printf("\n");
1779                 }
1780                 break;
1781             }
1782         }
1783     }
1784
1785     report( "deblock :" );
1786
1787     return ret;
1788 }
1789
1790 static int check_quant( int cpu_ref, int cpu_new )
1791 {
1792     x264_quant_function_t qf_c;
1793     x264_quant_function_t qf_ref;
1794     x264_quant_function_t qf_a;
1795     ALIGNED_ARRAY_N( dctcoef, dct1,[64] );
1796     ALIGNED_ARRAY_N( dctcoef, dct2,[64] );
1797     ALIGNED_ARRAY_N( dctcoef, dct3,[8],[16] );
1798     ALIGNED_ARRAY_N( dctcoef, dct4,[8],[16] );
1799     ALIGNED_ARRAY_N( uint8_t, cqm_buf,[64] );
1800     int ret = 0, ok, used_asm;
1801     int oks[3] = {1,1,1}, used_asms[3] = {0,0,0};
1802     x264_t h_buf;
1803     x264_t *h = &h_buf;
1804     memset( h, 0, sizeof(*h) );
1805     h->sps->i_chroma_format_idc = 1;
1806     x264_param_default( &h->param );
1807     h->chroma_qp_table = i_chroma_qp_table + 12;
1808     h->param.analyse.b_transform_8x8 = 1;
1809
1810     for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1811     {
1812         if( i_cqm == 0 )
1813         {
1814             for( int i = 0; i < 6; i++ )
1815                 h->pps->scaling_list[i] = x264_cqm_flat16;
1816             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1817         }
1818         else if( i_cqm == 1 )
1819         {
1820             for( int i = 0; i < 6; i++ )
1821                 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1822             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1823         }
1824         else
1825         {
1826             int max_scale = BIT_DEPTH < 10 ? 255 : 228;
1827             if( i_cqm == 2 )
1828                 for( int i = 0; i < 64; i++ )
1829                     cqm_buf[i] = 10 + rand() % (max_scale - 9);
1830             else
1831                 for( int i = 0; i < 64; i++ )
1832                     cqm_buf[i] = 1;
1833             for( int i = 0; i < 6; i++ )
1834                 h->pps->scaling_list[i] = cqm_buf;
1835             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1836         }
1837
1838         h->param.rc.i_qp_min = 0;
1839         h->param.rc.i_qp_max = QP_MAX_SPEC;
1840         x264_cqm_init( h );
1841         x264_quant_init( h, 0, &qf_c );
1842         x264_quant_init( h, cpu_ref, &qf_ref );
1843         x264_quant_init( h, cpu_new, &qf_a );
1844
1845 #define INIT_QUANT8(j,max) \
1846         { \
1847             static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1848             for( int i = 0; i < max; i++ ) \
1849             { \
1850                 unsigned int scale = (255*scale1d[(i>>3)&7]*scale1d[i&7])/16; \
1851                 dct1[i] = dct2[i] = (j>>(i>>6))&1 ? (rand()%(2*scale+1))-scale : 0; \
1852             } \
1853         }
1854
1855 #define INIT_QUANT4(j,max) \
1856         { \
1857             static const int scale1d[4] = {4,6,4,6}; \
1858             for( int i = 0; i < max; i++ ) \
1859             { \
1860                 unsigned int scale = 255*scale1d[(i>>2)&3]*scale1d[i&3]; \
1861                 dct1[i] = dct2[i] = (j>>(i>>4))&1 ? (rand()%(2*scale+1))-scale : 0; \
1862             } \
1863         }
1864
1865 #define TEST_QUANT_DC( name, cqm ) \
1866         if( qf_a.name != qf_ref.name ) \
1867         { \
1868             set_func_name( #name ); \
1869             used_asms[0] = 1; \
1870             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1871             { \
1872                 for( int j = 0; j < 2; j++ ) \
1873                 { \
1874                     int result_c, result_a; \
1875                     for( int i = 0; i < 16; i++ ) \
1876                         dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1877                     result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1878                     result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1879                     if( memcmp( dct1, dct2, 16*sizeof(dctcoef) ) || result_c != result_a ) \
1880                     { \
1881                         oks[0] = 0; \
1882                         fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1883                         break; \
1884                     } \
1885                     call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1886                     call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1887                 } \
1888             } \
1889         }
1890
1891 #define TEST_QUANT( qname, block, type, w, maxj ) \
1892         if( qf_a.qname != qf_ref.qname ) \
1893         { \
1894             set_func_name( #qname ); \
1895             used_asms[0] = 1; \
1896             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1897             { \
1898                 for( int j = 0; j < maxj; j++ ) \
1899                 { \
1900                     INIT_QUANT##type(j, w*w) \
1901                     int result_c = call_c1( qf_c.qname, (void*)dct1, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1902                     int result_a = call_a1( qf_a.qname, (void*)dct2, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1903                     if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \
1904                     { \
1905                         oks[0] = 0; \
1906                         fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1907                         break; \
1908                     } \
1909                     call_c2( qf_c.qname, (void*)dct1, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1910                     call_a2( qf_a.qname, (void*)dct2, h->quant##type##_mf[block][qp], h->quant##type##_bias[block][qp] ); \
1911                 } \
1912             } \
1913         }
1914
1915         TEST_QUANT( quant_8x8, CQM_8IY, 8, 8, 2 );
1916         TEST_QUANT( quant_8x8, CQM_8PY, 8, 8, 2 );
1917         TEST_QUANT( quant_4x4, CQM_4IY, 4, 4, 2 );
1918         TEST_QUANT( quant_4x4, CQM_4PY, 4, 4, 2 );
1919         TEST_QUANT( quant_4x4x4, CQM_4IY, 4, 8, 16 );
1920         TEST_QUANT( quant_4x4x4, CQM_4PY, 4, 8, 16 );
1921         TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1922         TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1923
1924 #define TEST_DEQUANT( qname, dqname, block, w ) \
1925         if( qf_a.dqname != qf_ref.dqname ) \
1926         { \
1927             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1928             used_asms[1] = 1; \
1929             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1930             { \
1931                 INIT_QUANT##w(1, w*w) \
1932                 qf_c.qname( dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1933                 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1934                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1935                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1936                 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1937                 { \
1938                     oks[1] = 0; \
1939                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1940                     break; \
1941                 } \
1942                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1943                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1944             } \
1945         }
1946
1947         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1948         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1949         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1950         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1951
1952 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1953         if( qf_a.dqname != qf_ref.dqname ) \
1954         { \
1955             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1956             used_asms[1] = 1; \
1957             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1958             { \
1959                 for( int i = 0; i < 16; i++ ) \
1960                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16; \
1961                 qf_c.qname( dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1962                 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1963                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1964                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1965                 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1966                 { \
1967                     oks[1] = 0; \
1968                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1969                 } \
1970                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1971                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1972             } \
1973         }
1974
1975         TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1976
1977         if( qf_a.idct_dequant_2x4_dc != qf_ref.idct_dequant_2x4_dc )
1978         {
1979             set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1980             used_asms[1] = 1;
1981             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1982             {
1983                 for( int i = 0; i < 8; i++ )
1984                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1985                 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 );
1986                 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 );
1987                 call_c( qf_c.idct_dequant_2x4_dc, dct1, dct3, h->dequant4_mf[CQM_4IC], qp+3 );
1988                 call_a( qf_a.idct_dequant_2x4_dc, dct1, dct4, h->dequant4_mf[CQM_4IC], qp+3 );
1989                 for( int i = 0; i < 8; i++ )
1990                     if( dct3[i][0] != dct4[i][0] )
1991                     {
1992                         oks[1] = 0;
1993                         fprintf( stderr, "idct_dequant_2x4_dc (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1994                         break;
1995                     }
1996             }
1997         }
1998
1999         if( qf_a.idct_dequant_2x4_dconly != qf_ref.idct_dequant_2x4_dconly )
2000         {
2001             set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
2002             used_asms[1] = 1;
2003             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
2004             {
2005                 for( int i = 0; i < 8; i++ )
2006                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
2007                 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 );
2008                 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 );
2009                 memcpy( dct2, dct1, 8*sizeof(dctcoef) );
2010                 call_c1( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
2011                 call_a1( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
2012                 if( memcmp( dct1, dct2, 8*sizeof(dctcoef) ) )
2013                 {
2014                     oks[1] = 0;
2015                     fprintf( stderr, "idct_dequant_2x4_dconly (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
2016                     break;
2017                 }
2018                 call_c2( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
2019                 call_a2( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
2020             }
2021         }
2022
2023 #define TEST_OPTIMIZE_CHROMA_DC( optname, size ) \
2024         if( qf_a.optname != qf_ref.optname ) \
2025         { \
2026             set_func_name( #optname ); \
2027             used_asms[2] = 1; \
2028             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
2029             { \
2030                 int qpdc = qp + (size == 8 ? 3 : 0); \
2031                 int dmf = h->dequant4_mf[CQM_4IC][qpdc%6][0] << qpdc/6; \
2032                 if( dmf > 32*64 ) \
2033                     continue; \
2034                 for( int i = 16; ; i <<= 1 ) \
2035                 { \
2036                     int res_c, res_asm; \
2037                     int max = X264_MIN( i, PIXEL_MAX*16 ); \
2038                     for( int j = 0; j < size; j++ ) \
2039                         dct1[j] = rand()%(max*2+1) - max; \
2040                     for( int j = 0; i <= size; j += 4 ) \
2041                         qf_c.quant_2x2_dc( &dct1[j], h->quant4_mf[CQM_4IC][qpdc][0]>>1, h->quant4_bias[CQM_4IC][qpdc][0]>>1 ); \
2042                     memcpy( dct2, dct1, size*sizeof(dctcoef) ); \
2043                     res_c   = call_c1( qf_c.optname, dct1, dmf ); \
2044                     res_asm = call_a1( qf_a.optname, dct2, dmf ); \
2045                     if( res_c != res_asm || memcmp( dct1, dct2, size*sizeof(dctcoef) ) ) \
2046                     { \
2047                         oks[2] = 0; \
2048                         fprintf( stderr, #optname "(qp=%d, res_c=%d, res_asm=%d): [FAILED]\n", qp, res_c, res_asm ); \
2049                     } \
2050                     call_c2( qf_c.optname, dct1, dmf ); \
2051                     call_a2( qf_a.optname, dct2, dmf ); \
2052                     if( i >= PIXEL_MAX*16 ) \
2053                         break; \
2054                 } \
2055             } \
2056         }
2057
2058         TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x2_dc, 4 );
2059         TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x4_dc, 8 );
2060
2061         x264_cqm_delete( h );
2062     }
2063
2064     ok = oks[0]; used_asm = used_asms[0];
2065     report( "quant :" );
2066
2067     ok = oks[1]; used_asm = used_asms[1];
2068     report( "dequant :" );
2069
2070     ok = oks[2]; used_asm = used_asms[2];
2071     report( "optimize chroma dc :" );
2072
2073     ok = 1; used_asm = 0;
2074     if( qf_a.denoise_dct != qf_ref.denoise_dct )
2075     {
2076         used_asm = 1;
2077         for( int size = 16; size <= 64; size += 48 )
2078         {
2079             set_func_name( "denoise_dct" );
2080             memcpy( dct1, buf1, size*sizeof(dctcoef) );
2081             memcpy( dct2, buf1, size*sizeof(dctcoef) );
2082             memcpy( buf3+256, buf3, 256 );
2083             call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3,       (udctcoef*)buf2, size );
2084             call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
2085             if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
2086                 ok = 0;
2087             call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3,       (udctcoef*)buf2, size );
2088             call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
2089         }
2090     }
2091     report( "denoise dct :" );
2092
2093 #define TEST_DECIMATE( decname, w, ac, thresh ) \
2094     if( qf_a.decname != qf_ref.decname ) \
2095     { \
2096         set_func_name( #decname ); \
2097         used_asm = 1; \
2098         for( int i = 0; i < 100; i++ ) \
2099         { \
2100             static const int distrib[16] = {1,1,1,1,1,1,1,1,1,1,1,1,2,3,4};\
2101             static const int zerorate_lut[4] = {3,7,15,31};\
2102             int zero_rate = zerorate_lut[i&3];\
2103             for( int idx = 0; idx < w*w; idx++ ) \
2104             { \
2105                 int sign = (rand()&1) ? -1 : 1; \
2106                 int abs_level = distrib[rand()&15]; \
2107                 if( abs_level == 4 ) abs_level = rand()&0x3fff; \
2108                 int zero = !(rand()&zero_rate); \
2109                 dct1[idx] = zero * abs_level * sign; \
2110             } \
2111             if( ac ) \
2112                 dct1[0] = 0; \
2113             int result_c = call_c( qf_c.decname, dct1 ); \
2114             int result_a = call_a( qf_a.decname, dct1 ); \
2115             if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
2116             { \
2117                 ok = 0; \
2118                 fprintf( stderr, #decname ": [FAILED]\n" ); \
2119                 break; \
2120             } \
2121         } \
2122     }
2123
2124     ok = 1; used_asm = 0;
2125     TEST_DECIMATE( decimate_score64, 8, 0, 6 );
2126     TEST_DECIMATE( decimate_score16, 4, 0, 6 );
2127     TEST_DECIMATE( decimate_score15, 4, 1, 7 );
2128     report( "decimate_score :" );
2129
2130 #define TEST_LAST( last, lastname, size, ac ) \
2131     if( qf_a.last != qf_ref.last ) \
2132     { \
2133         set_func_name( #lastname ); \
2134         used_asm = 1; \
2135         for( int i = 0; i < 100; i++ ) \
2136         { \
2137             int nnz = 0; \
2138             int max = rand() & (size-1); \
2139             memset( dct1, 0, size*sizeof(dctcoef) ); \
2140             for( int idx = ac; idx < max; idx++ ) \
2141                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2142             if( !nnz ) \
2143                 dct1[ac] = 1; \
2144             int result_c = call_c( qf_c.last, dct1+ac ); \
2145             int result_a = call_a( qf_a.last, dct1+ac ); \
2146             if( result_c != result_a ) \
2147             { \
2148                 ok = 0; \
2149                 fprintf( stderr, #lastname ": [FAILED]\n" ); \
2150                 break; \
2151             } \
2152         } \
2153     }
2154
2155     ok = 1; used_asm = 0;
2156     TEST_LAST( coeff_last4              , coeff_last4,   4, 0 );
2157     TEST_LAST( coeff_last8              , coeff_last8,   8, 0 );
2158     TEST_LAST( coeff_last[  DCT_LUMA_AC], coeff_last15, 16, 1 );
2159     TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 16, 0 );
2160     TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 64, 0 );
2161     report( "coeff_last :" );
2162
2163 #define TEST_LEVELRUN( lastname, name, size, ac ) \
2164     if( qf_a.lastname != qf_ref.lastname ) \
2165     { \
2166         set_func_name( #name ); \
2167         used_asm = 1; \
2168         for( int i = 0; i < 100; i++ ) \
2169         { \
2170             x264_run_level_t runlevel_c, runlevel_a; \
2171             int nnz = 0; \
2172             int max = rand() & (size-1); \
2173             memset( dct1, 0, size*sizeof(dctcoef) ); \
2174             memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
2175             memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
2176             for( int idx = ac; idx < max; idx++ ) \
2177                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2178             if( !nnz ) \
2179                 dct1[ac] = 1; \
2180             int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
2181             int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
2182             if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
2183                 runlevel_c.mask != runlevel_a.mask || \
2184                 memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c)) \
2185             { \
2186                 ok = 0; \
2187                 fprintf( stderr, #name ": [FAILED]\n" ); \
2188                 break; \
2189             } \
2190         } \
2191     }
2192
2193     ok = 1; used_asm = 0;
2194     TEST_LEVELRUN( coeff_level_run4              , coeff_level_run4,   4, 0 );
2195     TEST_LEVELRUN( coeff_level_run8              , coeff_level_run8,   8, 0 );
2196     TEST_LEVELRUN( coeff_level_run[  DCT_LUMA_AC], coeff_level_run15, 16, 1 );
2197     TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 16, 0 );
2198     report( "coeff_level_run :" );
2199
2200     return ret;
2201 }
2202
2203 static int check_intra( int cpu_ref, int cpu_new )
2204 {
2205     int ret = 0, ok = 1, used_asm = 0;
2206     ALIGNED_ARRAY_32( pixel, edge,[36] );
2207     ALIGNED_ARRAY_32( pixel, edge2,[36] );
2208     ALIGNED_ARRAY_32( pixel, fdec,[FDEC_STRIDE*20] );
2209     struct
2210     {
2211         x264_predict_t      predict_16x16[4+3];
2212         x264_predict_t      predict_8x8c[4+3];
2213         x264_predict_t      predict_8x16c[4+3];
2214         x264_predict8x8_t   predict_8x8[9+3];
2215         x264_predict_t      predict_4x4[9+3];
2216         x264_predict_8x8_filter_t predict_8x8_filter;
2217     } ip_c, ip_ref, ip_a;
2218
2219     x264_predict_16x16_init( 0, ip_c.predict_16x16 );
2220     x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
2221     x264_predict_8x16c_init( 0, ip_c.predict_8x16c );
2222     x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
2223     x264_predict_4x4_init( 0, ip_c.predict_4x4 );
2224
2225     x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
2226     x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
2227     x264_predict_8x16c_init( cpu_ref, ip_ref.predict_8x16c );
2228     x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
2229     x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
2230
2231     x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
2232     x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
2233     x264_predict_8x16c_init( cpu_new, ip_a.predict_8x16c );
2234     x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
2235     x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
2236
2237     memcpy( fdec, pbuf1, 32*20 * sizeof(pixel) );\
2238
2239     ip_c.predict_8x8_filter( fdec+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
2240
2241 #define INTRA_TEST( name, dir, w, h, align, bench, ... )\
2242     if( ip_a.name[dir] != ip_ref.name[dir] )\
2243     {\
2244         set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
2245         used_asm = 1;\
2246         memcpy( pbuf3, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2247         memcpy( pbuf4, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2248         for( int a = 0; a < (do_bench ? 64/sizeof(pixel) : 1); a += align )\
2249         {\
2250             call_c##bench( ip_c.name[dir], pbuf3+48+a, ##__VA_ARGS__ );\
2251             call_a##bench( ip_a.name[dir], pbuf4+48+a, ##__VA_ARGS__ );\
2252             if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*20 * sizeof(pixel) ) )\
2253             {\
2254                 fprintf( stderr, #name "[%d] :  [FAILED]\n", dir );\
2255                 ok = 0;\
2256                 for( int k = -1; k < 16; k++ )\
2257                     printf( "%2x ", edge[16+k] );\
2258                 printf( "\n" );\
2259                 for( int j = 0; j < h; j++ )\
2260                 {\
2261                     printf( "%2x ", edge[14-j] );\
2262                     for( int k = 0; k < w; k++ )\
2263                         printf( "%2x ", pbuf4[48+k+j*FDEC_STRIDE] );\
2264                     printf( "\n" );\
2265                 }\
2266                 printf( "\n" );\
2267                 for( int j = 0; j < h; j++ )\
2268                 {\
2269                     printf( "   " );\
2270                     for( int k = 0; k < w; k++ )\
2271                         printf( "%2x ", pbuf3[48+k+j*FDEC_STRIDE] );\
2272                     printf( "\n" );\
2273                 }\
2274                 break;\
2275             }\
2276         }\
2277     }
2278
2279     for( int i = 0; i < 12; i++ )
2280         INTRA_TEST(   predict_4x4, i,  4,  4,  4, );
2281     for( int i = 0; i < 7; i++ )
2282         INTRA_TEST(  predict_8x8c, i,  8,  8, 16, );
2283     for( int i = 0; i < 7; i++ )
2284         INTRA_TEST( predict_8x16c, i,  8, 16, 16, );
2285     for( int i = 0; i < 7; i++ )
2286         INTRA_TEST( predict_16x16, i, 16, 16, 16, );
2287     for( int i = 0; i < 12; i++ )
2288         INTRA_TEST(   predict_8x8, i,  8,  8,  8, , edge );
2289
2290     set_func_name("intra_predict_8x8_filter");
2291     if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
2292     {
2293         used_asm = 1;
2294         for( int i = 0; i < 32; i++ )
2295         {
2296             if( !(i&7) || ((i&MB_TOPRIGHT) && !(i&MB_TOP)) )
2297                 continue;
2298             int neighbor = (i&24)>>1;
2299             memset( edge,  0, 36*sizeof(pixel) );
2300             memset( edge2, 0, 36*sizeof(pixel) );
2301             call_c( ip_c.predict_8x8_filter, pbuf1+48, edge,  neighbor, i&7 );
2302             call_a( ip_a.predict_8x8_filter, pbuf1+48, edge2, neighbor, i&7 );
2303             if( !(neighbor&MB_TOPLEFT) )
2304                 edge[15] = edge2[15] = 0;
2305             if( memcmp( edge+7, edge2+7, (i&MB_TOPRIGHT ? 26 : i&MB_TOP ? 17 : 8) * sizeof(pixel) ) )
2306             {
2307                 fprintf( stderr, "predict_8x8_filter :  [FAILED] %d %d\n", (i&24)>>1, i&7);
2308                 ok = 0;
2309             }
2310         }
2311     }
2312
2313 #define EXTREMAL_PLANE( w, h ) \
2314     { \
2315         int max[7]; \
2316         for( int j = 0; j < 7; j++ ) \
2317             max[j] = test ? rand()&PIXEL_MAX : PIXEL_MAX; \
2318         fdec[48-1-FDEC_STRIDE] = (i&1)*max[0]; \
2319         for( int j = 0; j < w/2; j++ ) \
2320             fdec[48+j-FDEC_STRIDE] = (!!(i&2))*max[1]; \
2321         for( int j = w/2; j < w-1; j++ ) \
2322             fdec[48+j-FDEC_STRIDE] = (!!(i&4))*max[2]; \
2323         fdec[48+(w-1)-FDEC_STRIDE] = (!!(i&8))*max[3]; \
2324         for( int j = 0; j < h/2; j++ ) \
2325             fdec[48+j*FDEC_STRIDE-1] = (!!(i&16))*max[4]; \
2326         for( int j = h/2; j < h-1; j++ ) \
2327             fdec[48+j*FDEC_STRIDE-1] = (!!(i&32))*max[5]; \
2328         fdec[48+(h-1)*FDEC_STRIDE-1] = (!!(i&64))*max[6]; \
2329     }
2330     /* Extremal test case for planar prediction. */
2331     for( int test = 0; test < 100 && ok; test++ )
2332         for( int i = 0; i < 128 && ok; i++ )
2333         {
2334             EXTREMAL_PLANE(  8,  8 );
2335             INTRA_TEST(  predict_8x8c, I_PRED_CHROMA_P,  8,  8, 64, 1 );
2336             EXTREMAL_PLANE(  8, 16 );
2337             INTRA_TEST( predict_8x16c, I_PRED_CHROMA_P,  8, 16, 64, 1 );
2338             EXTREMAL_PLANE( 16, 16 );
2339             INTRA_TEST( predict_16x16,  I_PRED_16x16_P, 16, 16, 64, 1 );
2340         }
2341     report( "intra pred :" );
2342     return ret;
2343 }
2344
2345 #define DECL_CABAC(cpu) \
2346 static void run_cabac_decision_##cpu( x264_t *h, uint8_t *dst )\
2347 {\
2348     x264_cabac_t cb;\
2349     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2350     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2351     for( int i = 0; i < 0x1000; i++ )\
2352         x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
2353 }\
2354 static void run_cabac_bypass_##cpu( x264_t *h, uint8_t *dst )\
2355 {\
2356     x264_cabac_t cb;\
2357     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2358     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2359     for( int i = 0; i < 0x1000; i++ )\
2360         x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
2361 }\
2362 static void run_cabac_terminal_##cpu( x264_t *h, uint8_t *dst )\
2363 {\
2364     x264_cabac_t cb;\
2365     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2366     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2367     for( int i = 0; i < 0x1000; i++ )\
2368         x264_cabac_encode_terminal_##cpu( &cb );\
2369 }
2370 DECL_CABAC(c)
2371 #if HAVE_MMX
2372 DECL_CABAC(asm)
2373 #else
2374 #define run_cabac_decision_asm run_cabac_decision_c
2375 #define run_cabac_bypass_asm run_cabac_bypass_c
2376 #define run_cabac_terminal_asm run_cabac_terminal_c
2377 #endif
2378
2379 extern const uint8_t x264_count_cat_m1[14];
2380 void x264_cabac_block_residual_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2381 void x264_cabac_block_residual_8x8_rd_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2382 void x264_cabac_block_residual_rd_c( x264_t *h, x264_cabac_t *cb, int ctx_block_cat, dctcoef *l );
2383
2384 static int check_cabac( int cpu_ref, int cpu_new )
2385 {
2386     int ret = 0, ok = 1, used_asm = 0;
2387     x264_t h;
2388     h.sps->i_chroma_format_idc = 3;
2389
2390     x264_bitstream_function_t bs_ref;
2391     x264_bitstream_function_t bs_a;
2392     x264_bitstream_init( cpu_ref, &bs_ref );
2393     x264_bitstream_init( cpu_new, &bs_a );
2394     x264_quant_init( &h, cpu_new, &h.quantf );
2395     h.quantf.coeff_last[DCT_CHROMA_DC] = h.quantf.coeff_last4;
2396
2397 #define CABAC_RESIDUAL(name, start, end, rd)\
2398 {\
2399     if( bs_a.name##_internal && (bs_a.name##_internal != bs_ref.name##_internal || (cpu_new&X264_CPU_SSE2_IS_SLOW)) )\
2400     {\
2401         used_asm = 1;\
2402         set_func_name( #name );\
2403         for( int i = 0; i < 2; i++ )\
2404         {\
2405             for( intptr_t ctx_block_cat = start; ctx_block_cat <= end; ctx_block_cat++ )\
2406             {\
2407                 for( int j = 0; j < 256; j++ )\
2408                 {\
2409                     ALIGNED_ARRAY_N( dctcoef, dct, [2],[64] );\
2410                     uint8_t bitstream[2][1<<16];\
2411                     static const uint8_t ctx_ac[14] = {0,1,0,0,1,0,0,1,0,0,0,1,0,0};\
2412                     int ac = ctx_ac[ctx_block_cat];\
2413                     int nz = 0;\
2414                     while( !nz )\
2415                     {\
2416                         for( int k = 0; k <= x264_count_cat_m1[ctx_block_cat]; k++ )\
2417                         {\
2418                             /* Very rough distribution that covers possible inputs */\
2419                             int rnd = rand();\
2420                             int coef = !(rnd&3);\
2421                             coef += !(rnd&  15) * (rand()&0x0006);\
2422                             coef += !(rnd&  63) * (rand()&0x0008);\
2423                             coef += !(rnd& 255) * (rand()&0x00F0);\
2424                             coef += !(rnd&1023) * (rand()&0x7F00);\
2425                             nz |= dct[0][ac+k] = dct[1][ac+k] = coef * ((rand()&1) ? 1 : -1);\
2426                         }\
2427                     }\
2428                     h.mb.b_interlaced = i;\
2429                     x264_cabac_t cb[2];\
2430                     x264_cabac_context_init( &h, &cb[0], SLICE_TYPE_P, 26, 0 );\
2431                     x264_cabac_context_init( &h, &cb[1], SLICE_TYPE_P, 26, 0 );\
2432                     x264_cabac_encode_init( &cb[0], bitstream[0], bitstream[0]+0xfff0 );\
2433                     x264_cabac_encode_init( &cb[1], bitstream[1], bitstream[1]+0xfff0 );\
2434                     cb[0].f8_bits_encoded = 0;\
2435                     cb[1].f8_bits_encoded = 0;\
2436                     if( !rd ) memcpy( bitstream[1], bitstream[0], 0x400 );\
2437                     call_c1( x264_##name##_c, &h, &cb[0], ctx_block_cat, dct[0]+ac );\
2438                     call_a1( bs_a.name##_internal, dct[1]+ac, i, ctx_block_cat, &cb[1] );\
2439                     ok = cb[0].f8_bits_encoded == cb[1].f8_bits_encoded && !memcmp(cb[0].state, cb[1].state, 1024);\
2440                     if( !rd ) ok |= !memcmp( bitstream[1], bitstream[0], 0x400 ) && !memcmp( &cb[1], &cb[0], offsetof(x264_cabac_t, p_start) );\
2441                     if( !ok )\
2442                     {\
2443                         fprintf( stderr, #name " :  [FAILED] ctx_block_cat %d", (int)ctx_block_cat );\
2444                         if( rd && cb[0].f8_bits_encoded != cb[1].f8_bits_encoded )\
2445                             fprintf( stderr, " (%d != %d)", cb[0].f8_bits_encoded, cb[1].f8_bits_encoded );\
2446                         fprintf( stderr, "\n");\
2447                         goto name##fail;\
2448                     }\
2449                     if( (j&15) == 0 )\
2450                     {\
2451                         call_c2( x264_##name##_c, &h, &cb[0], ctx_block_cat, dct[0]+ac );\
2452                         call_a2( bs_a.name##_internal, dct[1]+ac, i, ctx_block_cat, &cb[1] );\
2453                     }\
2454                 }\
2455             }\
2456         }\
2457     }\
2458 }\
2459 name##fail:
2460
2461     CABAC_RESIDUAL( cabac_block_residual, 0, DCT_LUMA_8x8, 0 )
2462     report( "cabac residual:" );
2463
2464     ok = 1; used_asm = 0;
2465     CABAC_RESIDUAL( cabac_block_residual_rd, 0, DCT_LUMA_8x8-1, 1 )
2466     CABAC_RESIDUAL( cabac_block_residual_8x8_rd, DCT_LUMA_8x8, DCT_LUMA_8x8, 1 )
2467     report( "cabac residual rd:" );
2468
2469     if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
2470         return ret;
2471     ok = 1; used_asm = 0;
2472     x264_cabac_init( &h );
2473
2474     set_func_name( "cabac_encode_decision" );
2475     memcpy( buf4, buf3, 0x1000 );
2476     call_c( run_cabac_decision_c, &h, buf3 );
2477     call_a( run_cabac_decision_asm, &h, buf4 );
2478     ok = !memcmp( buf3, buf4, 0x1000 );
2479     report( "cabac decision:" );
2480
2481     set_func_name( "cabac_encode_bypass" );
2482     memcpy( buf4, buf3, 0x1000 );
2483     call_c( run_cabac_bypass_c, &h, buf3 );
2484     call_a( run_cabac_bypass_asm, &h, buf4 );
2485     ok = !memcmp( buf3, buf4, 0x1000 );
2486     report( "cabac bypass:" );
2487
2488     set_func_name( "cabac_encode_terminal" );
2489     memcpy( buf4, buf3, 0x1000 );
2490     call_c( run_cabac_terminal_c, &h, buf3 );
2491     call_a( run_cabac_terminal_asm, &h, buf4 );
2492     ok = !memcmp( buf3, buf4, 0x1000 );
2493     report( "cabac terminal:" );
2494
2495     return ret;
2496 }
2497
2498 static int check_bitstream( int cpu_ref, int cpu_new )
2499 {
2500     x264_bitstream_function_t bs_c;
2501     x264_bitstream_function_t bs_ref;
2502     x264_bitstream_function_t bs_a;
2503
2504     int ret = 0, ok = 1, used_asm = 0;
2505
2506     x264_bitstream_init( 0, &bs_c );
2507     x264_bitstream_init( cpu_ref, &bs_ref );
2508     x264_bitstream_init( cpu_new, &bs_a );
2509     if( bs_a.nal_escape != bs_ref.nal_escape )
2510     {
2511         int size = 0x4000;
2512         uint8_t *input = malloc(size+100);
2513         uint8_t *output1 = malloc(size*2);
2514         uint8_t *output2 = malloc(size*2);
2515         used_asm = 1;
2516         set_func_name( "nal_escape" );
2517         for( int i = 0; i < 100; i++ )
2518         {
2519             /* Test corner-case sizes */
2520             int test_size = i < 10 ? i+1 : rand() & 0x3fff;
2521             /* Test 8 different probability distributions of zeros */
2522             for( int j = 0; j < test_size+32; j++ )
2523                 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
2524             uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
2525             uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
2526             int size_c = end_c-output1;
2527             int size_a = end_a-output2;
2528             if( size_c != size_a || memcmp( output1, output2, size_c ) )
2529             {
2530                 fprintf( stderr, "nal_escape :  [FAILED] %d %d\n", size_c, size_a );
2531                 ok = 0;
2532                 break;
2533             }
2534         }
2535         for( int j = 0; j < size+32; j++ )
2536             input[j] = rand();
2537         call_c2( bs_c.nal_escape, output1, input, input+size );
2538         call_a2( bs_a.nal_escape, output2, input, input+size );
2539         free(input);
2540         free(output1);
2541         free(output2);
2542     }
2543     report( "nal escape:" );
2544
2545     return ret;
2546 }
2547
2548 static int check_all_funcs( int cpu_ref, int cpu_new )
2549 {
2550     return check_pixel( cpu_ref, cpu_new )
2551          + check_dct( cpu_ref, cpu_new )
2552          + check_mc( cpu_ref, cpu_new )
2553          + check_intra( cpu_ref, cpu_new )
2554          + check_deblock( cpu_ref, cpu_new )
2555          + check_quant( cpu_ref, cpu_new )
2556          + check_cabac( cpu_ref, cpu_new )
2557          + check_bitstream( cpu_ref, cpu_new );
2558 }
2559
2560 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
2561 {
2562     *cpu_ref = *cpu_new;
2563     *cpu_new |= flags;
2564 #if STACK_ALIGNMENT < 16
2565     *cpu_new |= X264_CPU_STACK_MOD4;
2566 #endif
2567     if( *cpu_new & X264_CPU_SSE2_IS_FAST )
2568         *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
2569     if( !quiet )
2570         fprintf( stderr, "x264: %s\n", name );
2571     return check_all_funcs( *cpu_ref, *cpu_new );
2572 }
2573
2574 static int check_all_flags( void )
2575 {
2576     int ret = 0;
2577     int cpu0 = 0, cpu1 = 0;
2578 #if HAVE_MMX
2579     if( x264_cpu_detect() & X264_CPU_MMX2 )
2580     {
2581         ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMX2, "MMX" );
2582         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
2583         cpu1 &= ~X264_CPU_CACHELINE_64;
2584 #if ARCH_X86
2585         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
2586         cpu1 &= ~X264_CPU_CACHELINE_32;
2587 #endif
2588         if( x264_cpu_detect() & X264_CPU_LZCNT )
2589         {
2590             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
2591             cpu1 &= ~X264_CPU_LZCNT;
2592         }
2593         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
2594         cpu1 &= ~X264_CPU_SLOW_CTZ;
2595     }
2596     if( x264_cpu_detect() & X264_CPU_SSE )
2597         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE, "SSE" );
2598     if( x264_cpu_detect() & X264_CPU_SSE2 )
2599     {
2600         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
2601         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
2602         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
2603         cpu1 &= ~X264_CPU_CACHELINE_64;
2604         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_SHUFFLE, "SSE2 SlowShuffle" );
2605         cpu1 &= ~X264_CPU_SLOW_SHUFFLE;
2606         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
2607         cpu1 &= ~X264_CPU_SLOW_CTZ;
2608     }
2609     if( x264_cpu_detect() & X264_CPU_LZCNT )
2610     {
2611         ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
2612         cpu1 &= ~X264_CPU_LZCNT;
2613     }
2614     if( x264_cpu_detect() & X264_CPU_SSE3 )
2615     {
2616         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
2617         cpu1 &= ~X264_CPU_CACHELINE_64;
2618     }
2619     if( x264_cpu_detect() & X264_CPU_SSSE3 )
2620     {
2621         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
2622         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
2623         cpu1 &= ~X264_CPU_CACHELINE_64;
2624         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_SHUFFLE, "SSSE3 SlowShuffle" );
2625         cpu1 &= ~X264_CPU_SLOW_SHUFFLE;
2626         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
2627         cpu1 &= ~X264_CPU_SLOW_CTZ;
2628         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
2629         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64 SlowAtom" );
2630         cpu1 &= ~X264_CPU_CACHELINE_64;
2631         cpu1 &= ~X264_CPU_SLOW_ATOM;
2632     }
2633     if( x264_cpu_detect() & X264_CPU_SSE4 )
2634         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4, "SSE4" );
2635     if( x264_cpu_detect() & X264_CPU_AVX )
2636         ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX, "AVX" );
2637     if( x264_cpu_detect() & X264_CPU_XOP )
2638         ret |= add_flags( &cpu0, &cpu1, X264_CPU_XOP, "XOP" );
2639     if( x264_cpu_detect() & X264_CPU_FMA4 )
2640     {
2641         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA4, "FMA4" );
2642         cpu1 &= ~X264_CPU_FMA4;
2643     }
2644     if( x264_cpu_detect() & X264_CPU_BMI1 )
2645     {
2646         ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI1, "BMI1" );
2647         cpu1 &= ~X264_CPU_BMI1;
2648     }
2649     if( x264_cpu_detect() & X264_CPU_AVX2 )
2650     {
2651         ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX2, "AVX2" );
2652         if( x264_cpu_detect() & X264_CPU_LZCNT )
2653         {
2654             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "AVX2_LZCNT" );
2655             cpu1 &= ~X264_CPU_LZCNT;
2656         }
2657     }
2658     if( x264_cpu_detect() & X264_CPU_BMI2 )
2659     {
2660         ret |= add_flags( &cpu0, &cpu1, X264_CPU_BMI1|X264_CPU_BMI2, "BMI2" );
2661         cpu1 &= ~(X264_CPU_BMI1|X264_CPU_BMI2);
2662     }
2663     if( x264_cpu_detect() & X264_CPU_FMA3 )
2664     {
2665         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA3, "FMA3" );
2666         cpu1 &= ~X264_CPU_FMA3;
2667     }
2668 #elif ARCH_PPC
2669     if( x264_cpu_detect() & X264_CPU_ALTIVEC )
2670     {
2671         fprintf( stderr, "x264: ALTIVEC against C\n" );
2672         ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
2673     }
2674 #elif ARCH_ARM
2675     if( x264_cpu_detect() & X264_CPU_ARMV6 )
2676         ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
2677     if( x264_cpu_detect() & X264_CPU_NEON )
2678         ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
2679     if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC )
2680         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
2681 #endif
2682     return ret;
2683 }
2684
2685 int main(int argc, char *argv[])
2686 {
2687     int ret = 0;
2688
2689     if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
2690     {
2691 #if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM
2692         fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
2693         return 1;
2694 #endif
2695         do_bench = 1;
2696         if( argv[1][7] == '=' )
2697         {
2698             bench_pattern = argv[1]+8;
2699             bench_pattern_len = strlen(bench_pattern);
2700         }
2701         argc--;
2702         argv++;
2703     }
2704
2705     int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
2706     fprintf( stderr, "x264: using random seed %u\n", seed );
2707     srand( seed );
2708
2709     buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 32*BENCH_ALIGNS );
2710     pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 32*BENCH_ALIGNS );
2711     if( !buf1 || !pbuf1 )
2712     {
2713         fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
2714         return -1;
2715     }
2716 #define INIT_POINTER_OFFSETS\
2717     buf2 = buf1 + 0xf00;\
2718     buf3 = buf2 + 0xf00;\
2719     buf4 = buf3 + 0x1000*sizeof(pixel);\
2720     pbuf2 = pbuf1 + 0xf00;\
2721     pbuf3 = (pixel*)buf3;\
2722     pbuf4 = (pixel*)buf4;
2723     INIT_POINTER_OFFSETS;
2724     for( int i = 0; i < 0x1e00; i++ )
2725     {
2726         buf1[i] = rand() & 0xFF;
2727         pbuf1[i] = rand() & PIXEL_MAX;
2728     }
2729     memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) );
2730
2731     /* 32-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
2732     if( do_bench )
2733         for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
2734         {
2735             INIT_POINTER_OFFSETS;
2736             ret |= x264_stack_pagealign( check_all_flags, i*32 );
2737             buf1 += 32;
2738             pbuf1 += 32;
2739             quiet = 1;
2740             fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
2741         }
2742     else
2743         ret = x264_stack_pagealign( check_all_flags, 0 );
2744
2745     if( ret )
2746     {
2747         fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
2748         return -1;
2749     }
2750     fprintf( stderr, "x264: All tests passed Yeah :)\n" );
2751     if( do_bench )
2752         print_bench();
2753     return 0;
2754 }
2755