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