]> git.sesse.net Git - x264/blob - tools/checkasm.c
XOP frame_init_lowres
[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     ok = 1, used_asm = 0; \
1140     for( int i = 0; i < 12; i++ ) \
1141     { \
1142         memcpy( pbuf3, pbuf1+320, 320 * sizeof(pixel) ); \
1143         memcpy( pbuf4, pbuf1+320, 320 * sizeof(pixel) ); \
1144         if( mc_a.name[i] != mc_ref.name[i] ) \
1145         { \
1146             set_func_name( "%s_%s", #name, pixel_names[i] ); \
1147             used_asm = 1; \
1148             call_c1( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1149             call_a1( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1150             if( memcmp( pbuf3, pbuf4, 320 * sizeof(pixel) ) ) \
1151             { \
1152                 ok = 0; \
1153                 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
1154             } \
1155             call_c2( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1156             call_a2( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1157         } \
1158     } \
1159 }
1160
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     ok = 1, used_asm = 0; \
1168     for( int i = 1; i <= 5; i++ ) \
1169     { \
1170         ALIGNED_16( pixel buffC[640] ); \
1171         ALIGNED_16( pixel buffA[640] ); \
1172         int j = X264_MAX( i*4, 2 ); \
1173         memset( buffC, 0, 640 * sizeof(pixel) ); \
1174         memset( buffA, 0, 640 * sizeof(pixel) ); \
1175         x264_t ha; \
1176         ha.mc = mc_a; \
1177         /* w12 is the same as w16 in some cases */ \
1178         if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
1179             continue; \
1180         if( mc_a.name[i] != mc_ref.name[i] ) \
1181         { \
1182             set_func_name( "%s_w%d", #name, j ); \
1183             used_asm = 1; \
1184             call_c1( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
1185             mc_a.weight_cache(&ha, &weight); \
1186             call_a1( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
1187             for( int k = 0; k < 16; k++ ) \
1188                 if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \
1189                 { \
1190                     ok = 0; \
1191                     fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
1192                     break; \
1193                 } \
1194             call_c2( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
1195             call_a2( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
1196         } \
1197     }
1198
1199     ok = 1; used_asm = 0;
1200
1201     int align_cnt = 0;
1202     for( int s = 0; s <= 127 && ok; s++ )
1203     {
1204         for( int o = -128; o <= 127 && ok; o++ )
1205         {
1206             if( rand() & 2047 ) continue;
1207             for( int d = 0; d <= 7 && ok; d++ )
1208             {
1209                 if( s == 1<<d )
1210                     continue;
1211                 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
1212                 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
1213             }
1214         }
1215
1216     }
1217     report( "mc weight :" );
1218
1219     ok = 1; used_asm = 0;
1220     for( int o = 0; o <= 127 && ok; o++ )
1221     {
1222         int s = 1, d = 0;
1223         if( rand() & 15 ) continue;
1224         x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1225         MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
1226     }
1227     report( "mc offsetadd :" );
1228     ok = 1; used_asm = 0;
1229     for( int o = -128; o < 0 && ok; o++ )
1230     {
1231         int s = 1, d = 0;
1232         if( rand() & 15 ) continue;
1233         x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1234         MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
1235     }
1236     report( "mc offsetsub :" );
1237
1238     ok = 1; used_asm = 0;
1239     for( int height = 8; height <= 16; height += 8 )
1240     {
1241         if( mc_a.store_interleave_chroma != mc_ref.store_interleave_chroma )
1242         {
1243             set_func_name( "store_interleave_chroma" );
1244             used_asm = 1;
1245             memset( pbuf3, 0, 64*height );
1246             memset( pbuf4, 0, 64*height );
1247             call_c( mc_c.store_interleave_chroma, pbuf3, 64, pbuf1, pbuf1+16, height );
1248             call_a( mc_a.store_interleave_chroma, pbuf4, 64, pbuf1, pbuf1+16, height );
1249             if( memcmp( pbuf3, pbuf4, 64*height ) )
1250             {
1251                 ok = 0;
1252                 fprintf( stderr, "store_interleave_chroma FAILED: h=%d\n", height );
1253                 break;
1254             }
1255         }
1256         if( mc_a.load_deinterleave_chroma_fenc != mc_ref.load_deinterleave_chroma_fenc )
1257         {
1258             set_func_name( "load_deinterleave_chroma_fenc" );
1259             used_asm = 1;
1260             call_c( mc_c.load_deinterleave_chroma_fenc, pbuf3, pbuf1, 64, height );
1261             call_a( mc_a.load_deinterleave_chroma_fenc, pbuf4, pbuf1, 64, height );
1262             if( memcmp( pbuf3, pbuf4, FENC_STRIDE*height ) )
1263             {
1264                 ok = 0;
1265                 fprintf( stderr, "load_deinterleave_chroma_fenc FAILED: h=%d\n", height );
1266                 break;
1267             }
1268         }
1269         if( mc_a.load_deinterleave_chroma_fdec != mc_ref.load_deinterleave_chroma_fdec )
1270         {
1271             set_func_name( "load_deinterleave_chroma_fdec" );
1272             used_asm = 1;
1273             call_c( mc_c.load_deinterleave_chroma_fdec, pbuf3, pbuf1, 64, height );
1274             call_a( mc_a.load_deinterleave_chroma_fdec, pbuf4, pbuf1, 64, height );
1275             if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*height ) )
1276             {
1277                 ok = 0;
1278                 fprintf( stderr, "load_deinterleave_chroma_fdec FAILED: h=%d\n", height );
1279                 break;
1280             }
1281         }
1282     }
1283     report( "store_interleave :" );
1284
1285     struct plane_spec {
1286         int w, h, src_stride;
1287     } 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} };
1288     ok = 1; used_asm = 0;
1289     if( mc_a.plane_copy != mc_ref.plane_copy )
1290     {
1291         set_func_name( "plane_copy" );
1292         used_asm = 1;
1293         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1294         {
1295             int w = plane_specs[i].w;
1296             int h = plane_specs[i].h;
1297             int src_stride = plane_specs[i].src_stride;
1298             int dst_stride = (w + 127) & ~63;
1299             assert( dst_stride * h <= 0x1000 );
1300             pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1301             memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1302             memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1303             call_c( mc_c.plane_copy, pbuf3, dst_stride, src1, src_stride, w, h );
1304             call_a( mc_a.plane_copy, pbuf4, dst_stride, src1, src_stride, w, h );
1305             for( int y = 0; y < h; y++ )
1306                 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(pixel) ) )
1307                 {
1308                     ok = 0;
1309                     fprintf( stderr, "plane_copy FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1310                     break;
1311                 }
1312         }
1313     }
1314
1315     if( mc_a.plane_copy_interleave != mc_ref.plane_copy_interleave )
1316     {
1317         set_func_name( "plane_copy_interleave" );
1318         used_asm = 1;
1319         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1320         {
1321             int w = (plane_specs[i].w + 1) >> 1;
1322             int h = plane_specs[i].h;
1323             int src_stride = (plane_specs[i].src_stride + 1) >> 1;
1324             int dst_stride = (2*w + 127) & ~63;
1325             assert( dst_stride * h <= 0x1000 );
1326             pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1327             memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1328             memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1329             call_c( mc_c.plane_copy_interleave, pbuf3, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1330             call_a( mc_a.plane_copy_interleave, pbuf4, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1331             for( int y = 0; y < h; y++ )
1332                 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) )
1333                 {
1334                     ok = 0;
1335                     fprintf( stderr, "plane_copy_interleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1336                     break;
1337                 }
1338         }
1339     }
1340
1341     if( mc_a.plane_copy_deinterleave != mc_ref.plane_copy_deinterleave )
1342     {
1343         set_func_name( "plane_copy_deinterleave" );
1344         used_asm = 1;
1345         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1346         {
1347             int w = (plane_specs[i].w + 1) >> 1;
1348             int h = plane_specs[i].h;
1349             int dst_stride = w;
1350             int src_stride = (2*w + 127) & ~63;
1351             int offv = (dst_stride*h + 31) & ~15;
1352             memset( pbuf3, 0, 0x1000 );
1353             memset( pbuf4, 0, 0x1000 );
1354             call_c( mc_c.plane_copy_deinterleave, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf1, src_stride, w, h );
1355             call_a( mc_a.plane_copy_deinterleave, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf1, src_stride, w, h );
1356             for( int y = 0; y < h; y++ )
1357                 if( memcmp( pbuf3+y*dst_stride,      pbuf4+y*dst_stride, w ) ||
1358                     memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w ) )
1359                 {
1360                     ok = 0;
1361                     fprintf( stderr, "plane_copy_deinterleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1362                     break;
1363                 }
1364         }
1365     }
1366     report( "plane_copy :" );
1367
1368     if( mc_a.hpel_filter != mc_ref.hpel_filter )
1369     {
1370         pixel *srchpel = pbuf1+8+2*64;
1371         pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
1372         pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
1373         void *tmp = pbuf3+49*64;
1374         set_func_name( "hpel_filter" );
1375         ok = 1; used_asm = 1;
1376         memset( pbuf3, 0, 4096 * sizeof(pixel) );
1377         memset( pbuf4, 0, 4096 * sizeof(pixel) );
1378         call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, 64, 48, 10, tmp );
1379         call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, 64, 48, 10, tmp );
1380         for( int i = 0; i < 3; i++ )
1381             for( int j = 0; j < 10; j++ )
1382                 //FIXME ideally the first pixels would match too, but they aren't actually used
1383                 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
1384                 {
1385                     ok = 0;
1386                     fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
1387                     for( int k = 0; k < 48; k++ )
1388                         printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
1389                     printf( "\n" );
1390                     for( int k = 0; k < 48; k++ )
1391                         printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
1392                     printf( "\n" );
1393                     break;
1394                 }
1395         report( "hpel filter :" );
1396     }
1397
1398     if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
1399     {
1400         pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
1401         pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1402         set_func_name( "lowres_init" );
1403         ok = 1; used_asm = 1;
1404         for( int w = 40; w <= 48; w += 8 )
1405         {
1406             int stride = (w+8)&~15;
1407             call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
1408             call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
1409             for( int i = 0; i < 16; i++ )
1410             {
1411                 for( int j = 0; j < 4; j++ )
1412                     if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w * sizeof(pixel) ) )
1413                     {
1414                         ok = 0;
1415                         fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1416                         for( int k = 0; k < w; k++ )
1417                             printf( "%d ", dstc[j][k+i*stride] );
1418                         printf( "\n" );
1419                         for( int k = 0; k < w; k++ )
1420                             printf( "%d ", dsta[j][k+i*stride] );
1421                         printf( "\n" );
1422                         break;
1423                     }
1424             }
1425         }
1426         report( "lowres init :" );
1427     }
1428
1429 #define INTEGRAL_INIT( name, size, ... )\
1430     if( mc_a.name != mc_ref.name )\
1431     {\
1432         int stride = 80;\
1433         set_func_name( #name );\
1434         used_asm = 1;\
1435         memcpy( buf3, buf1, size*2*stride );\
1436         memcpy( buf4, buf1, size*2*stride );\
1437         uint16_t *sum = (uint16_t*)buf3;\
1438         call_c1( mc_c.name, __VA_ARGS__ );\
1439         sum = (uint16_t*)buf4;\
1440         call_a1( mc_a.name, __VA_ARGS__ );\
1441         if( memcmp( buf3, buf4, (stride-8)*2 ) \
1442             || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
1443             ok = 0;\
1444         call_c2( mc_c.name, __VA_ARGS__ );\
1445         call_a2( mc_a.name, __VA_ARGS__ );\
1446     }
1447     ok = 1; used_asm = 0;
1448     INTEGRAL_INIT( integral_init4h, 2, sum+stride, pbuf2, stride );
1449     INTEGRAL_INIT( integral_init8h, 2, sum+stride, pbuf2, stride );
1450     INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
1451     INTEGRAL_INIT( integral_init8v, 9, sum, stride );
1452     report( "integral init :" );
1453
1454     if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1455     {
1456         x264_emms();
1457         for( int i = 0; i < 10; i++ )
1458         {
1459             float fps_factor = (rand()&65535) / 256.;
1460             ok = 1; used_asm = 1;
1461             set_func_name( "mbtree_propagate" );
1462             int *dsta = (int*)buf3;
1463             int *dstc = dsta+400;
1464             uint16_t *prop = (uint16_t*)buf1;
1465             uint16_t *intra = (uint16_t*)buf4;
1466             uint16_t *inter = intra+128;
1467             uint16_t *qscale = inter+128;
1468             uint16_t *rnd = (uint16_t*)buf2;
1469             x264_emms();
1470             for( int j = 0; j < 100; j++ )
1471             {
1472                 intra[j]  = *rnd++ & 0x7fff;
1473                 intra[j] += !intra[j];
1474                 inter[j]  = *rnd++ & 0x7fff;
1475                 qscale[j] = *rnd++ & 0x7fff;
1476             }
1477             call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, &fps_factor, 100 );
1478             call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, &fps_factor, 100 );
1479             // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1480             x264_emms();
1481             for( int j = 0; j < 100 && ok; j++ )
1482             {
1483                 ok &= abs( dstc[j]-dsta[j] ) <= 1 || fabs( (double)dstc[j]/dsta[j]-1 ) < 1e-4;
1484                 if( !ok )
1485                     fprintf( stderr, "mbtree_propagate FAILED: %f !~= %f\n", (double)dstc[j], (double)dsta[j] );
1486             }
1487         }
1488         report( "mbtree propagate :" );
1489     }
1490
1491     if( mc_a.memcpy_aligned != mc_ref.memcpy_aligned )
1492     {
1493         set_func_name( "memcpy_aligned" );
1494         ok = 1; used_asm = 1;
1495         for( int size = 16; size < 256; size += 16 )
1496         {
1497             memset( buf4, 0xAA, size + 1 );
1498             call_c( mc_c.memcpy_aligned, buf3, buf1, size );
1499             call_a( mc_a.memcpy_aligned, buf4, buf1, size );
1500             if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1501             {
1502                 ok = 0;
1503                 fprintf( stderr, "memcpy_aligned FAILED: size=%d\n", size );
1504                 break;
1505             }
1506         }
1507         report( "memcpy aligned :" );
1508     }
1509
1510     if( mc_a.memzero_aligned != mc_ref.memzero_aligned )
1511     {
1512         set_func_name( "memzero_aligned" );
1513         ok = 1; used_asm = 1;
1514         for( int size = 128; size < 1024; size += 128 )
1515         {
1516             memset( buf4, 0xAA, size + 1 );
1517             call_c( mc_c.memzero_aligned, buf3, size );
1518             call_a( mc_a.memzero_aligned, buf4, size );
1519             if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1520             {
1521                 ok = 0;
1522                 fprintf( stderr, "memzero_aligned FAILED: size=%d\n", size );
1523                 break;
1524             }
1525         }
1526         report( "memzero aligned :" );
1527     }
1528
1529     return ret;
1530 }
1531
1532 static int check_deblock( int cpu_ref, int cpu_new )
1533 {
1534     x264_deblock_function_t db_c;
1535     x264_deblock_function_t db_ref;
1536     x264_deblock_function_t db_a;
1537     int ret = 0, ok = 1, used_asm = 0;
1538     int alphas[36], betas[36];
1539     int8_t tcs[36][4];
1540
1541     x264_deblock_init( 0, &db_c, 0 );
1542     x264_deblock_init( cpu_ref, &db_ref, 0 );
1543     x264_deblock_init( cpu_new, &db_a, 0 );
1544
1545     /* not exactly the real values of a,b,tc but close enough */
1546     for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1547     {
1548         alphas[i] = a << (BIT_DEPTH-8);
1549         betas[i] = (i+1)/2 << (BIT_DEPTH-8);
1550         tcs[i][0] = tcs[i][3] = (c+6)/10 << (BIT_DEPTH-8);
1551         tcs[i][1] = (c+7)/15 << (BIT_DEPTH-8);
1552         tcs[i][2] = (c+9)/20 << (BIT_DEPTH-8);
1553         a = a*9/10;
1554         c = c*9/10;
1555     }
1556
1557 #define TEST_DEBLOCK( name, align, ... ) \
1558     for( int i = 0; i < 36; i++ ) \
1559     { \
1560         int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1561         for( int j = 0; j < 1024; j++ ) \
1562             /* two distributions of random to excersize different failure modes */ \
1563             pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \
1564         memcpy( pbuf4, pbuf3, 1024 * sizeof(pixel) ); \
1565         if( db_a.name != db_ref.name ) \
1566         { \
1567             set_func_name( #name ); \
1568             used_asm = 1; \
1569             call_c1( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1570             call_a1( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1571             if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1572             { \
1573                 ok = 0; \
1574                 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1575                 break; \
1576             } \
1577             call_c2( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1578             call_a2( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1579         } \
1580     }
1581
1582     TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1583     TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1584     TEST_DEBLOCK( deblock_h_chroma_420, 0, tcs[i] );
1585     TEST_DEBLOCK( deblock_h_chroma_422, 0, tcs[i] );
1586     TEST_DEBLOCK( deblock_chroma_420_mbaff, 0, tcs[i] );
1587     TEST_DEBLOCK( deblock_chroma_422_mbaff, 0, tcs[i] );
1588     TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1589     TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1590     TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1591     TEST_DEBLOCK( deblock_h_chroma_420_intra, 0 );
1592     TEST_DEBLOCK( deblock_h_chroma_422_intra, 0 );
1593     TEST_DEBLOCK( deblock_chroma_420_intra_mbaff, 0 );
1594     TEST_DEBLOCK( deblock_chroma_422_intra_mbaff, 0 );
1595     TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1596
1597     if( db_a.deblock_strength != db_ref.deblock_strength )
1598     {
1599         for( int i = 0; i < 100; i++ )
1600         {
1601             ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1602             ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1603             ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] );
1604             ALIGNED_ARRAY_16( uint8_t, bs, [2],[2][8][4] );
1605             memset( bs, 99, sizeof(bs) );
1606             for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1607                 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1608             for( int j = 0; j < 2; j++ )
1609                 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1610                 {
1611                     ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1612                     for( int l = 0; l < 2; l++ )
1613                         mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1614                 }
1615             set_func_name( "deblock_strength" );
1616             call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1617             call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1618             if( memcmp( bs[0], bs[1], sizeof(bs[0]) ) )
1619             {
1620                 ok = 0;
1621                 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1622                 for( int j = 0; j < 2; j++ )
1623                 {
1624                     for( int k = 0; k < 2; k++ )
1625                         for( int l = 0; l < 4; l++ )
1626                         {
1627                             for( int m = 0; m < 4; m++ )
1628                                 printf("%d ",bs[j][k][l][m]);
1629                             printf("\n");
1630                         }
1631                     printf("\n");
1632                 }
1633                 break;
1634             }
1635         }
1636     }
1637
1638     report( "deblock :" );
1639
1640     return ret;
1641 }
1642
1643 static int check_quant( int cpu_ref, int cpu_new )
1644 {
1645     x264_quant_function_t qf_c;
1646     x264_quant_function_t qf_ref;
1647     x264_quant_function_t qf_a;
1648     ALIGNED_16( dctcoef dct1[64] );
1649     ALIGNED_16( dctcoef dct2[64] );
1650     ALIGNED_16( dctcoef dct3[8][16] );
1651     ALIGNED_16( dctcoef dct4[8][16] );
1652     ALIGNED_16( uint8_t cqm_buf[64] );
1653     int ret = 0, ok, used_asm;
1654     int oks[3] = {1,1,1}, used_asms[3] = {0,0,0};
1655     x264_t h_buf;
1656     x264_t *h = &h_buf;
1657     memset( h, 0, sizeof(*h) );
1658     h->sps->i_chroma_format_idc = 1;
1659     x264_param_default( &h->param );
1660     h->chroma_qp_table = i_chroma_qp_table + 12;
1661     h->param.analyse.b_transform_8x8 = 1;
1662
1663     for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1664     {
1665         if( i_cqm == 0 )
1666         {
1667             for( int i = 0; i < 6; i++ )
1668                 h->pps->scaling_list[i] = x264_cqm_flat16;
1669             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1670         }
1671         else if( i_cqm == 1 )
1672         {
1673             for( int i = 0; i < 6; i++ )
1674                 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1675             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1676         }
1677         else
1678         {
1679             int max_scale = BIT_DEPTH < 10 ? 255 : 228;
1680             if( i_cqm == 2 )
1681                 for( int i = 0; i < 64; i++ )
1682                     cqm_buf[i] = 10 + rand() % (max_scale - 9);
1683             else
1684                 for( int i = 0; i < 64; i++ )
1685                     cqm_buf[i] = 1;
1686             for( int i = 0; i < 6; i++ )
1687                 h->pps->scaling_list[i] = cqm_buf;
1688             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1689         }
1690
1691         h->param.rc.i_qp_min = 0;
1692         h->param.rc.i_qp_max = QP_MAX;
1693         x264_cqm_init( h );
1694         x264_quant_init( h, 0, &qf_c );
1695         x264_quant_init( h, cpu_ref, &qf_ref );
1696         x264_quant_init( h, cpu_new, &qf_a );
1697
1698 #define INIT_QUANT8(j) \
1699         { \
1700             static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1701             for( int i = 0; i < 64; i++ ) \
1702             { \
1703                 unsigned int scale = (255*scale1d[i>>3]*scale1d[i&7])/16; \
1704                 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1705             } \
1706         }
1707
1708 #define INIT_QUANT4(j) \
1709         { \
1710             static const int scale1d[4] = {4,6,4,6}; \
1711             for( int i = 0; i < 16; i++ ) \
1712             { \
1713                 unsigned int scale = 255*scale1d[i>>2]*scale1d[i&3]; \
1714                 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1715             } \
1716         }
1717
1718 #define TEST_QUANT_DC( name, cqm ) \
1719         if( qf_a.name != qf_ref.name ) \
1720         { \
1721             set_func_name( #name ); \
1722             used_asms[0] = 1; \
1723             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1724             { \
1725                 for( int j = 0; j < 2; j++ ) \
1726                 { \
1727                     int result_c, result_a; \
1728                     for( int i = 0; i < 16; i++ ) \
1729                         dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1730                     result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1731                     result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1732                     if( memcmp( dct1, dct2, 16*sizeof(dctcoef) ) || result_c != result_a ) \
1733                     { \
1734                         oks[0] = 0; \
1735                         fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1736                         break; \
1737                     } \
1738                     call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1739                     call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1740                 } \
1741             } \
1742         }
1743
1744 #define TEST_QUANT( qname, block, w ) \
1745         if( qf_a.qname != qf_ref.qname ) \
1746         { \
1747             set_func_name( #qname ); \
1748             used_asms[0] = 1; \
1749             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1750             { \
1751                 for( int j = 0; j < 2; j++ ) \
1752                 { \
1753                     INIT_QUANT##w(j) \
1754                     int result_c = call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1755                     int result_a = call_a1( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1756                     if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \
1757                     { \
1758                         oks[0] = 0; \
1759                         fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1760                         break; \
1761                     } \
1762                     call_c2( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1763                     call_a2( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1764                 } \
1765             } \
1766         }
1767
1768         TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1769         TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1770         TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1771         TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1772         TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1773         TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1774
1775 #define TEST_DEQUANT( qname, dqname, block, w ) \
1776         if( qf_a.dqname != qf_ref.dqname ) \
1777         { \
1778             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1779             used_asms[1] = 1; \
1780             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1781             { \
1782                 INIT_QUANT##w(1) \
1783                 qf_c.qname( dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1784                 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1785                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1786                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1787                 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1788                 { \
1789                     oks[1] = 0; \
1790                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1791                     break; \
1792                 } \
1793                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1794                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1795             } \
1796         }
1797
1798         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1799         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1800         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1801         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1802
1803 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1804         if( qf_a.dqname != qf_ref.dqname ) \
1805         { \
1806             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1807             used_asms[1] = 1; \
1808             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1809             { \
1810                 for( int i = 0; i < 16; i++ ) \
1811                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16; \
1812                 qf_c.qname( dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1813                 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1814                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1815                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1816                 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1817                 { \
1818                     oks[1] = 0; \
1819                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1820                 } \
1821                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1822                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1823             } \
1824         }
1825
1826         TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1827
1828         if( qf_a.idct_dequant_2x4_dc != qf_ref.idct_dequant_2x4_dc )
1829         {
1830             set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1831             used_asms[1] = 1;
1832             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1833             {
1834                 for( int i = 0; i < 8; i++ )
1835                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1836                 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 );
1837                 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 );
1838                 call_c( qf_c.idct_dequant_2x4_dc, dct1, dct3, h->dequant4_mf[CQM_4IC], qp+3 );
1839                 call_a( qf_a.idct_dequant_2x4_dc, dct1, dct4, h->dequant4_mf[CQM_4IC], qp+3 );
1840                 for( int i = 0; i < 8; i++ )
1841                     if( dct3[i][0] != dct4[i][0] )
1842                     {
1843                         oks[1] = 0;
1844                         fprintf( stderr, "idct_dequant_2x4_dc (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1845                         break;
1846                     }
1847             }
1848         }
1849
1850         if( qf_a.idct_dequant_2x4_dconly != qf_ref.idct_dequant_2x4_dconly )
1851         {
1852             set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1853             used_asms[1] = 1;
1854             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1855             {
1856                 for( int i = 0; i < 8; i++ )
1857                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1858                 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 );
1859                 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 );
1860                 memcpy( dct2, dct1, 8*sizeof(dctcoef) );
1861                 call_c1( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1862                 call_a1( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1863                 if( memcmp( dct1, dct2, 8*sizeof(dctcoef) ) )
1864                 {
1865                     oks[1] = 0;
1866                     fprintf( stderr, "idct_dequant_2x4_dconly (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1867                     break;
1868                 }
1869                 call_c2( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1870                 call_a2( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1871             }
1872         }
1873
1874 #define TEST_OPTIMIZE_CHROMA_DC( optname, size ) \
1875         if( qf_a.optname != qf_ref.optname ) \
1876         { \
1877             set_func_name( #optname ); \
1878             used_asms[2] = 1; \
1879             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1880             { \
1881                 int qpdc = qp + (size == 8 ? 3 : 0); \
1882                 int dmf = h->dequant4_mf[CQM_4IC][qpdc%6][0] << qpdc/6; \
1883                 if( dmf > 32*64 ) \
1884                     continue; \
1885                 for( int i = 16; ; i <<= 1 ) \
1886                 { \
1887                     int res_c, res_asm; \
1888                     int max = X264_MIN( i, PIXEL_MAX*16 ); \
1889                     for( int j = 0; j < size; j++ ) \
1890                         dct1[j] = rand()%(max*2+1) - max; \
1891                     for( int j = 0; i <= size; j += 4 ) \
1892                         qf_c.quant_2x2_dc( &dct1[j], h->quant4_mf[CQM_4IC][qpdc][0]>>1, h->quant4_bias[CQM_4IC][qpdc][0]>>1 ); \
1893                     memcpy( dct2, dct1, size*sizeof(dctcoef) ); \
1894                     res_c   = call_c1( qf_c.optname, dct1, dmf ); \
1895                     res_asm = call_a1( qf_a.optname, dct2, dmf ); \
1896                     if( res_c != res_asm || memcmp( dct1, dct2, size*sizeof(dctcoef) ) ) \
1897                     { \
1898                         oks[2] = 0; \
1899                         fprintf( stderr, #optname "(qp=%d, res_c=%d, res_asm=%d): [FAILED]\n", qp, res_c, res_asm ); \
1900                     } \
1901                     call_c2( qf_c.optname, dct1, dmf ); \
1902                     call_a2( qf_a.optname, dct2, dmf ); \
1903                     if( i >= PIXEL_MAX*16 ) \
1904                         break; \
1905                 } \
1906             } \
1907         }
1908
1909         TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x2_dc, 4 );
1910         TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x4_dc, 8 );
1911
1912         x264_cqm_delete( h );
1913     }
1914
1915     ok = oks[0]; used_asm = used_asms[0];
1916     report( "quant :" );
1917
1918     ok = oks[1]; used_asm = used_asms[1];
1919     report( "dequant :" );
1920
1921     ok = oks[2]; used_asm = used_asms[2];
1922     report( "optimize chroma dc :" );
1923
1924     ok = 1; used_asm = 0;
1925     if( qf_a.denoise_dct != qf_ref.denoise_dct )
1926     {
1927         used_asm = 1;
1928         for( int size = 16; size <= 64; size += 48 )
1929         {
1930             set_func_name( "denoise_dct" );
1931             memcpy( dct1, buf1, size*sizeof(dctcoef) );
1932             memcpy( dct2, buf1, size*sizeof(dctcoef) );
1933             memcpy( buf3+256, buf3, 256 );
1934             call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
1935             call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
1936             if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1937                 ok = 0;
1938             call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
1939             call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
1940         }
1941     }
1942     report( "denoise dct :" );
1943
1944 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1945     if( qf_a.decname != qf_ref.decname ) \
1946     { \
1947         set_func_name( #decname ); \
1948         used_asm = 1; \
1949         for( int i = 0; i < 100; i++ ) \
1950         { \
1951             static const int distrib[16] = {1,1,1,1,1,1,1,1,1,1,1,1,2,3,4};\
1952             static const int zerorate_lut[4] = {3,7,15,31};\
1953             int zero_rate = zerorate_lut[i&3];\
1954             for( int idx = 0; idx < w*w; idx++ ) \
1955             { \
1956                 int sign = (rand()&1) ? -1 : 1; \
1957                 int abs_level = distrib[rand()&15]; \
1958                 if( abs_level == 4 ) abs_level = rand()&0x3fff; \
1959                 int zero = !(rand()&zero_rate); \
1960                 dct1[idx] = zero * abs_level * sign; \
1961             } \
1962             if( ac ) \
1963                 dct1[0] = 0; \
1964             int result_c = call_c( qf_c.decname, dct1 ); \
1965             int result_a = call_a( qf_a.decname, dct1 ); \
1966             if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1967             { \
1968                 ok = 0; \
1969                 fprintf( stderr, #decname ": [FAILED]\n" ); \
1970                 break; \
1971             } \
1972         } \
1973     }
1974
1975     ok = 1; used_asm = 0;
1976     TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1977     TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1978     TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1979     report( "decimate_score :" );
1980
1981 #define TEST_LAST( last, lastname, size, ac ) \
1982     if( qf_a.last != qf_ref.last ) \
1983     { \
1984         set_func_name( #lastname ); \
1985         used_asm = 1; \
1986         for( int i = 0; i < 100; i++ ) \
1987         { \
1988             int nnz = 0; \
1989             int max = rand() & (size-1); \
1990             memset( dct1, 0, size*sizeof(dctcoef) ); \
1991             for( int idx = ac; idx < max; idx++ ) \
1992                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1993             if( !nnz ) \
1994                 dct1[ac] = 1; \
1995             int result_c = call_c( qf_c.last, dct1+ac ); \
1996             int result_a = call_a( qf_a.last, dct1+ac ); \
1997             if( result_c != result_a ) \
1998             { \
1999                 ok = 0; \
2000                 fprintf( stderr, #lastname ": [FAILED]\n" ); \
2001                 break; \
2002             } \
2003         } \
2004     }
2005
2006     ok = 1; used_asm = 0;
2007     TEST_LAST( coeff_last4              , coeff_last4,   4, 0 );
2008     TEST_LAST( coeff_last8              , coeff_last8,   8, 0 );
2009     TEST_LAST( coeff_last[  DCT_LUMA_AC], coeff_last15, 16, 1 );
2010     TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 16, 0 );
2011     TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 64, 0 );
2012     report( "coeff_last :" );
2013
2014 #define TEST_LEVELRUN( lastname, name, size, ac ) \
2015     if( qf_a.lastname != qf_ref.lastname ) \
2016     { \
2017         set_func_name( #name ); \
2018         used_asm = 1; \
2019         for( int i = 0; i < 100; i++ ) \
2020         { \
2021             x264_run_level_t runlevel_c, runlevel_a; \
2022             int nnz = 0; \
2023             int max = rand() & (size-1); \
2024             memset( dct1, 0, size*sizeof(dctcoef) ); \
2025             memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
2026             memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
2027             for( int idx = ac; idx < max; idx++ ) \
2028                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2029             if( !nnz ) \
2030                 dct1[ac] = 1; \
2031             int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
2032             int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
2033             if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
2034                 runlevel_c.mask != runlevel_a.mask || \
2035                 memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c) || \
2036                 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
2037             { \
2038                 ok = 0; \
2039                 fprintf( stderr, #name ": [FAILED]\n" ); \
2040                 break; \
2041             } \
2042         } \
2043     }
2044
2045     ok = 1; used_asm = 0;
2046     TEST_LEVELRUN( coeff_level_run4              , coeff_level_run4,   4, 0 );
2047     TEST_LEVELRUN( coeff_level_run8              , coeff_level_run8,   8, 0 );
2048     TEST_LEVELRUN( coeff_level_run[  DCT_LUMA_AC], coeff_level_run15, 16, 1 );
2049     TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 16, 0 );
2050     report( "coeff_level_run :" );
2051
2052     return ret;
2053 }
2054
2055 static int check_intra( int cpu_ref, int cpu_new )
2056 {
2057     int ret = 0, ok = 1, used_asm = 0;
2058     ALIGNED_ARRAY_32( pixel, edge,[36] );
2059     ALIGNED_ARRAY_32( pixel, edge2,[36] );
2060     ALIGNED_16( pixel fdec[FDEC_STRIDE*20] );
2061     struct
2062     {
2063         x264_predict_t      predict_16x16[4+3];
2064         x264_predict_t      predict_8x8c[4+3];
2065         x264_predict_t      predict_8x16c[4+3];
2066         x264_predict8x8_t   predict_8x8[9+3];
2067         x264_predict_t      predict_4x4[9+3];
2068         x264_predict_8x8_filter_t predict_8x8_filter;
2069     } ip_c, ip_ref, ip_a;
2070
2071     x264_predict_16x16_init( 0, ip_c.predict_16x16 );
2072     x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
2073     x264_predict_8x16c_init( 0, ip_c.predict_8x16c );
2074     x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
2075     x264_predict_4x4_init( 0, ip_c.predict_4x4 );
2076
2077     x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
2078     x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
2079     x264_predict_8x16c_init( cpu_ref, ip_ref.predict_8x16c );
2080     x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
2081     x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
2082
2083     x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
2084     x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
2085     x264_predict_8x16c_init( cpu_new, ip_a.predict_8x16c );
2086     x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
2087     x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
2088
2089     memcpy( fdec, pbuf1, 32*20 * sizeof(pixel) );\
2090
2091     ip_c.predict_8x8_filter( fdec+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
2092
2093 #define INTRA_TEST( name, dir, w, h, align, bench, ... )\
2094     if( ip_a.name[dir] != ip_ref.name[dir] )\
2095     {\
2096         set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
2097         used_asm = 1;\
2098         memcpy( pbuf3, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2099         memcpy( pbuf4, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2100         for( int a = 0; a < (do_bench ? 64/sizeof(pixel) : 1); a += align )\
2101         {\
2102             call_c##bench( ip_c.name[dir], pbuf3+48+a, ##__VA_ARGS__ );\
2103             call_a##bench( ip_a.name[dir], pbuf4+48+a, ##__VA_ARGS__ );\
2104             if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*20 * sizeof(pixel) ) )\
2105             {\
2106                 fprintf( stderr, #name "[%d] :  [FAILED]\n", dir );\
2107                 ok = 0;\
2108                 for( int k = -1; k < 16; k++ )\
2109                     printf( "%2x ", edge[16+k] );\
2110                 printf( "\n" );\
2111                 for( int j = 0; j < h; j++ )\
2112                 {\
2113                     printf( "%2x ", edge[14-j] );\
2114                     for( int k = 0; k < w; k++ )\
2115                         printf( "%2x ", pbuf4[48+k+j*FDEC_STRIDE] );\
2116                     printf( "\n" );\
2117                 }\
2118                 printf( "\n" );\
2119                 for( int j = 0; j < h; j++ )\
2120                 {\
2121                     printf( "   " );\
2122                     for( int k = 0; k < w; k++ )\
2123                         printf( "%2x ", pbuf3[48+k+j*FDEC_STRIDE] );\
2124                     printf( "\n" );\
2125                 }\
2126                 break;\
2127             }\
2128         }\
2129     }
2130
2131     for( int i = 0; i < 12; i++ )
2132         INTRA_TEST(   predict_4x4, i,  4,  4,  4, );
2133     for( int i = 0; i < 7; i++ )
2134         INTRA_TEST(  predict_8x8c, i,  8,  8, 16, );
2135     for( int i = 0; i < 7; i++ )
2136         INTRA_TEST( predict_8x16c, i,  8, 16, 16, );
2137     for( int i = 0; i < 7; i++ )
2138         INTRA_TEST( predict_16x16, i, 16, 16, 16, );
2139     for( int i = 0; i < 12; i++ )
2140         INTRA_TEST(   predict_8x8, i,  8,  8,  8, , edge );
2141
2142     set_func_name("intra_predict_8x8_filter");
2143     if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
2144     {
2145         used_asm = 1;
2146         for( int i = 0; i < 32; i++ )
2147         {
2148             if( !(i&7) || ((i&MB_TOPRIGHT) && !(i&MB_TOP)) )
2149                 continue;
2150             int neighbor = (i&24)>>1;
2151             memset( edge,  0, 36*sizeof(pixel) );
2152             memset( edge2, 0, 36*sizeof(pixel) );
2153             call_c( ip_c.predict_8x8_filter, pbuf1+48, edge,  neighbor, i&7 );
2154             call_a( ip_a.predict_8x8_filter, pbuf1+48, edge2, neighbor, i&7 );
2155             if( !(neighbor&MB_TOPLEFT) )
2156                 edge[15] = edge2[15] = 0;
2157             if( memcmp( edge+7, edge2+7, (i&MB_TOPRIGHT ? 26 : i&MB_TOP ? 17 : 8) * sizeof(pixel) ) )
2158             {
2159                 fprintf( stderr, "predict_8x8_filter :  [FAILED] %d %d\n", (i&24)>>1, i&7);
2160                 ok = 0;
2161             }
2162         }
2163     }
2164
2165 #define EXTREMAL_PLANE( w, h ) \
2166     { \
2167         int max[7]; \
2168         for( int j = 0; j < 7; j++ ) \
2169             max[j] = test ? rand()&PIXEL_MAX : PIXEL_MAX; \
2170         fdec[48-1-FDEC_STRIDE] = (i&1)*max[0]; \
2171         for( int j = 0; j < w/2; j++ ) \
2172             fdec[48+j-FDEC_STRIDE] = (!!(i&2))*max[1]; \
2173         for( int j = w/2; j < w-1; j++ ) \
2174             fdec[48+j-FDEC_STRIDE] = (!!(i&4))*max[2]; \
2175         fdec[48+(w-1)-FDEC_STRIDE] = (!!(i&8))*max[3]; \
2176         for( int j = 0; j < h/2; j++ ) \
2177             fdec[48+j*FDEC_STRIDE-1] = (!!(i&16))*max[4]; \
2178         for( int j = h/2; j < h-1; j++ ) \
2179             fdec[48+j*FDEC_STRIDE-1] = (!!(i&32))*max[5]; \
2180         fdec[48+(h-1)*FDEC_STRIDE-1] = (!!(i&64))*max[6]; \
2181     }
2182     /* Extremal test case for planar prediction. */
2183     for( int test = 0; test < 100 && ok; test++ )
2184         for( int i = 0; i < 128 && ok; i++ )
2185         {
2186             EXTREMAL_PLANE(  8,  8 );
2187             INTRA_TEST(  predict_8x8c, I_PRED_CHROMA_P,  8,  8, 64, 1 );
2188             EXTREMAL_PLANE(  8, 16 );
2189             INTRA_TEST( predict_8x16c, I_PRED_CHROMA_P,  8, 16, 64, 1 );
2190             EXTREMAL_PLANE( 16, 16 );
2191             INTRA_TEST( predict_16x16,  I_PRED_16x16_P, 16, 16, 64, 1 );
2192         }
2193     report( "intra pred :" );
2194     return ret;
2195 }
2196
2197 #define DECL_CABAC(cpu) \
2198 static void run_cabac_decision_##cpu( x264_t *h, uint8_t *dst )\
2199 {\
2200     x264_cabac_t cb;\
2201     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2202     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2203     for( int i = 0; i < 0x1000; i++ )\
2204         x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
2205 }\
2206 static void run_cabac_bypass_##cpu( x264_t *h, uint8_t *dst )\
2207 {\
2208     x264_cabac_t cb;\
2209     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2210     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2211     for( int i = 0; i < 0x1000; i++ )\
2212         x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
2213 }\
2214 static void run_cabac_terminal_##cpu( x264_t *h, uint8_t *dst )\
2215 {\
2216     x264_cabac_t cb;\
2217     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2218     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2219     for( int i = 0; i < 0x1000; i++ )\
2220         x264_cabac_encode_terminal_##cpu( &cb );\
2221 }
2222 DECL_CABAC(c)
2223 #if HAVE_MMX
2224 DECL_CABAC(asm)
2225 #else
2226 #define run_cabac_decision_asm run_cabac_decision_c
2227 #define run_cabac_bypass_asm run_cabac_bypass_c
2228 #define run_cabac_terminal_asm run_cabac_terminal_c
2229 #endif
2230
2231 static int check_cabac( int cpu_ref, int cpu_new )
2232 {
2233     int ret = 0, ok, used_asm = 1;
2234     x264_t h;
2235     h.sps->i_chroma_format_idc = 3;
2236     if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
2237         return 0;
2238     x264_cabac_init( &h );
2239
2240     set_func_name( "cabac_encode_decision" );
2241     memcpy( buf4, buf3, 0x1000 );
2242     call_c( run_cabac_decision_c, &h, buf3 );
2243     call_a( run_cabac_decision_asm, &h, buf4 );
2244     ok = !memcmp( buf3, buf4, 0x1000 );
2245     report( "cabac decision:" );
2246
2247     set_func_name( "cabac_encode_bypass" );
2248     memcpy( buf4, buf3, 0x1000 );
2249     call_c( run_cabac_bypass_c, &h, buf3 );
2250     call_a( run_cabac_bypass_asm, &h, buf4 );
2251     ok = !memcmp( buf3, buf4, 0x1000 );
2252     report( "cabac bypass:" );
2253
2254     set_func_name( "cabac_encode_terminal" );
2255     memcpy( buf4, buf3, 0x1000 );
2256     call_c( run_cabac_terminal_c, &h, buf3 );
2257     call_a( run_cabac_terminal_asm, &h, buf4 );
2258     ok = !memcmp( buf3, buf4, 0x1000 );
2259     report( "cabac terminal:" );
2260
2261     return ret;
2262 }
2263
2264 static int check_bitstream( int cpu_ref, int cpu_new )
2265 {
2266     x264_bitstream_function_t bs_c;
2267     x264_bitstream_function_t bs_ref;
2268     x264_bitstream_function_t bs_a;
2269
2270     int ret = 0, ok = 1, used_asm = 0;
2271
2272     x264_bitstream_init( 0, &bs_c );
2273     x264_bitstream_init( cpu_ref, &bs_ref );
2274     x264_bitstream_init( cpu_new, &bs_a );
2275     if( bs_a.nal_escape != bs_ref.nal_escape )
2276     {
2277         int size = 0x4000;
2278         uint8_t *input = malloc(size+100);
2279         uint8_t *output1 = malloc(size*2);
2280         uint8_t *output2 = malloc(size*2);
2281         used_asm = 1;
2282         set_func_name( "nal_escape" );
2283         for( int i = 0; i < 100; i++ )
2284         {
2285             /* Test corner-case sizes */
2286             int test_size = i < 10 ? i+1 : rand() & 0x3fff;
2287             /* Test 8 different probability distributions of zeros */
2288             for( int j = 0; j < test_size+32; j++ )
2289                 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
2290             uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
2291             uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
2292             int size_c = end_c-output1;
2293             int size_a = end_a-output2;
2294             if( size_c != size_a || memcmp( output1, output2, size_c ) )
2295             {
2296                 fprintf( stderr, "nal_escape :  [FAILED] %d %d\n", size_c, size_a );
2297                 ok = 0;
2298                 break;
2299             }
2300         }
2301         for( int j = 0; j < size+32; j++ )
2302             input[j] = rand();
2303         call_c2( bs_c.nal_escape, output1, input, input+size );
2304         call_a2( bs_a.nal_escape, output2, input, input+size );
2305         free(input);
2306         free(output1);
2307         free(output2);
2308     }
2309     report( "nal escape:" );
2310
2311     return ret;
2312 }
2313
2314 static int check_all_funcs( int cpu_ref, int cpu_new )
2315 {
2316     return check_pixel( cpu_ref, cpu_new )
2317          + check_dct( cpu_ref, cpu_new )
2318          + check_mc( cpu_ref, cpu_new )
2319          + check_intra( cpu_ref, cpu_new )
2320          + check_deblock( cpu_ref, cpu_new )
2321          + check_quant( cpu_ref, cpu_new )
2322          + check_cabac( cpu_ref, cpu_new )
2323          + check_bitstream( cpu_ref, cpu_new );
2324 }
2325
2326 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
2327 {
2328     *cpu_ref = *cpu_new;
2329     *cpu_new |= flags;
2330 #if BROKEN_STACK_ALIGNMENT
2331     *cpu_new |= X264_CPU_STACK_MOD4;
2332 #endif
2333     if( *cpu_new & X264_CPU_SSE2_IS_FAST )
2334         *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
2335     if( !quiet )
2336         fprintf( stderr, "x264: %s\n", name );
2337     return check_all_funcs( *cpu_ref, *cpu_new );
2338 }
2339
2340 static int check_all_flags( void )
2341 {
2342     int ret = 0;
2343     int cpu0 = 0, cpu1 = 0;
2344 #if HAVE_MMX
2345     if( x264_cpu_detect() & X264_CPU_MMX2 )
2346     {
2347         ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMX2, "MMX" );
2348         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
2349         cpu1 &= ~X264_CPU_CACHELINE_64;
2350 #if ARCH_X86
2351         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
2352         cpu1 &= ~X264_CPU_CACHELINE_32;
2353 #endif
2354         if( x264_cpu_detect() & X264_CPU_LZCNT )
2355         {
2356             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
2357             cpu1 &= ~X264_CPU_LZCNT;
2358         }
2359         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
2360         cpu1 &= ~X264_CPU_SLOW_CTZ;
2361     }
2362     if( x264_cpu_detect() & X264_CPU_SSE2 )
2363     {
2364         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
2365         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
2366         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
2367         cpu1 &= ~X264_CPU_CACHELINE_64;
2368         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSE2 FastShuffle" );
2369         cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
2370         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
2371         cpu1 &= ~X264_CPU_SLOW_CTZ;
2372         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSE2 SlowAtom" );
2373         cpu1 &= ~X264_CPU_SLOW_ATOM;
2374     }
2375     if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
2376     {
2377         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
2378         cpu1 &= ~X264_CPU_SSE_MISALIGN;
2379     }
2380     if( x264_cpu_detect() & X264_CPU_LZCNT )
2381     {
2382         ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
2383         cpu1 &= ~X264_CPU_LZCNT;
2384     }
2385     if( x264_cpu_detect() & X264_CPU_SSE3 )
2386     {
2387         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
2388         cpu1 &= ~X264_CPU_CACHELINE_64;
2389     }
2390     if( x264_cpu_detect() & X264_CPU_SSSE3 )
2391     {
2392         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
2393         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
2394         cpu1 &= ~X264_CPU_CACHELINE_64;
2395         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSSE3 FastShuffle" );
2396         cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
2397         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
2398         cpu1 &= ~X264_CPU_SLOW_CTZ;
2399         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
2400         cpu1 &= ~X264_CPU_SLOW_ATOM;
2401     }
2402     if( x264_cpu_detect() & X264_CPU_SSE4 )
2403         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4 | X264_CPU_SHUFFLE_IS_FAST, "SSE4" );
2404     if( x264_cpu_detect() & X264_CPU_AVX )
2405         ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX, "AVX" );
2406     if( x264_cpu_detect() & X264_CPU_XOP )
2407         ret |= add_flags( &cpu0, &cpu1, X264_CPU_XOP, "XOP" );
2408     if( x264_cpu_detect() & X264_CPU_FMA4 )
2409         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA4, "FMA4" );
2410 #elif ARCH_PPC
2411     if( x264_cpu_detect() & X264_CPU_ALTIVEC )
2412     {
2413         fprintf( stderr, "x264: ALTIVEC against C\n" );
2414         ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
2415     }
2416 #elif ARCH_ARM
2417     if( x264_cpu_detect() & X264_CPU_ARMV6 )
2418         ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
2419     if( x264_cpu_detect() & X264_CPU_NEON )
2420         ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
2421     if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC )
2422         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
2423 #endif
2424     return ret;
2425 }
2426
2427 int main(int argc, char *argv[])
2428 {
2429     int ret = 0;
2430
2431     if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
2432     {
2433 #if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM
2434         fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
2435         return 1;
2436 #endif
2437         do_bench = 1;
2438         if( argv[1][7] == '=' )
2439         {
2440             bench_pattern = argv[1]+8;
2441             bench_pattern_len = strlen(bench_pattern);
2442         }
2443         argc--;
2444         argv++;
2445     }
2446
2447     int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
2448     fprintf( stderr, "x264: using random seed %u\n", seed );
2449     srand( seed );
2450
2451     buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 16*BENCH_ALIGNS );
2452     pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 16*BENCH_ALIGNS );
2453     if( !buf1 || !pbuf1 )
2454     {
2455         fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
2456         return -1;
2457     }
2458 #define INIT_POINTER_OFFSETS\
2459     buf2 = buf1 + 0xf00;\
2460     buf3 = buf2 + 0xf00;\
2461     buf4 = buf3 + 0x1000*sizeof(pixel);\
2462     pbuf2 = pbuf1 + 0xf00;\
2463     pbuf3 = (pixel*)buf3;\
2464     pbuf4 = (pixel*)buf4;
2465     INIT_POINTER_OFFSETS;
2466     for( int i = 0; i < 0x1e00; i++ )
2467     {
2468         buf1[i] = rand() & 0xFF;
2469         pbuf1[i] = rand() & PIXEL_MAX;
2470     }
2471     memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) );
2472
2473     /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
2474     if( do_bench )
2475         for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
2476         {
2477             INIT_POINTER_OFFSETS;
2478             ret |= x264_stack_pagealign( check_all_flags, i*16 );
2479             buf1 += 16;
2480             pbuf1 += 16;
2481             quiet = 1;
2482             fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
2483         }
2484     else
2485         ret = check_all_flags();
2486
2487     if( ret )
2488     {
2489         fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
2490         return -1;
2491     }
2492     fprintf( stderr, "x264: All tests passed Yeah :)\n" );
2493     if( do_bench )
2494         print_bench();
2495     return 0;
2496 }
2497