]> git.sesse.net Git - x264/blob - tools/checkasm.c
Use a large LUT for CAVLC zero-run bit codes
[x264] / tools / checkasm.c
1 /*****************************************************************************
2  * checkasm.c: assembly check tool
3  *****************************************************************************
4  * Copyright (C) 2003-2011 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                 break; \
759             } \
760             call_c( dct_c.name, t1, enc, dec ); \
761             call_a( dct_asm.name, t2, enc, dec ); \
762             if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
763             { \
764                 ok = 0; \
765                 fprintf( stderr, #name " [FAILED] (overflow)\n" ); \
766                 break; \
767             } \
768             enc += 16*FENC_STRIDE; \
769             dec += 16*FDEC_STRIDE; \
770         } \
771     }
772     ok = 1; used_asm = 0;
773     TEST_DCT( sub4x4_dct, dct1[0], dct2[0], 16 );
774     TEST_DCT( sub8x8_dct, dct1, dct2, 16*4 );
775     TEST_DCT( sub8x8_dct_dc, dctdc[0], dctdc[1], 4 );
776     TEST_DCT( sub8x16_dct_dc, dctdc[0], dctdc[1], 8 );
777     TEST_DCT( sub16x16_dct, dct1, dct2, 16*16 );
778     report( "sub_dct4 :" );
779
780     ok = 1; used_asm = 0;
781     TEST_DCT( sub8x8_dct8, (void*)dct1[0], (void*)dct2[0], 64 );
782     TEST_DCT( sub16x16_dct8, (void*)dct1, (void*)dct2, 64*4 );
783     report( "sub_dct8 :" );
784 #undef TEST_DCT
785
786     // fdct and idct are denormalized by different factors, so quant/dequant
787     // is needed to force the coefs into the right range.
788     dct_c.sub16x16_dct( dct4, pbuf1, pbuf2 );
789     dct_c.sub16x16_dct8( dct8, pbuf1, pbuf2 );
790     for( int i = 0; i < 16; i++ )
791     {
792         qf.quant_4x4( dct4[i], h->quant4_mf[CQM_4IY][20], h->quant4_bias[CQM_4IY][20] );
793         qf.dequant_4x4( dct4[i], h->dequant4_mf[CQM_4IY], 20 );
794     }
795     for( int i = 0; i < 4; i++ )
796     {
797         qf.quant_8x8( dct8[i], h->quant8_mf[CQM_8IY][20], h->quant8_bias[CQM_8IY][20] );
798         qf.dequant_8x8( dct8[i], h->dequant8_mf[CQM_8IY], 20 );
799     }
800     x264_cqm_delete( h );
801
802 #define TEST_IDCT( name, src ) \
803     if( dct_asm.name != dct_ref.name ) \
804     { \
805         set_func_name( #name ); \
806         used_asm = 1; \
807         memcpy( pbuf3, pbuf1, 32*32 * sizeof(pixel) ); \
808         memcpy( pbuf4, pbuf1, 32*32 * sizeof(pixel) ); \
809         memcpy( dct1, src, 256 * sizeof(dctcoef) ); \
810         memcpy( dct2, src, 256 * sizeof(dctcoef) ); \
811         call_c1( dct_c.name, pbuf3, (void*)dct1 ); \
812         call_a1( dct_asm.name, pbuf4, (void*)dct2 ); \
813         if( memcmp( pbuf3, pbuf4, 32*32 * sizeof(pixel) ) ) \
814         { \
815             ok = 0; \
816             fprintf( stderr, #name " [FAILED]\n" ); \
817         } \
818         call_c2( dct_c.name, pbuf3, (void*)dct1 ); \
819         call_a2( dct_asm.name, pbuf4, (void*)dct2 ); \
820     }
821     ok = 1; used_asm = 0;
822     TEST_IDCT( add4x4_idct, dct4 );
823     TEST_IDCT( add8x8_idct, dct4 );
824     TEST_IDCT( add8x8_idct_dc, dct4 );
825     TEST_IDCT( add16x16_idct, dct4 );
826     TEST_IDCT( add16x16_idct_dc, dct4 );
827     report( "add_idct4 :" );
828
829     ok = 1; used_asm = 0;
830     TEST_IDCT( add8x8_idct8, dct8 );
831     TEST_IDCT( add16x16_idct8, dct8 );
832     report( "add_idct8 :" );
833 #undef TEST_IDCT
834
835 #define TEST_DCTDC( name )\
836     ok = 1; used_asm = 0;\
837     if( dct_asm.name != dct_ref.name )\
838     {\
839         set_func_name( #name );\
840         used_asm = 1;\
841         uint16_t *p = (uint16_t*)buf1;\
842         for( int i = 0; i < 16 && ok; i++ )\
843         {\
844             for( int j = 0; j < 16; j++ )\
845                 dct1[0][j] = !i ? (j^j>>1^j>>2^j>>3)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
846                            : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
847                            : ((*p++)&0x1fff)-0x1000; /* general case */\
848             memcpy( dct2, dct1, 16 * sizeof(dctcoef) );\
849             call_c1( dct_c.name, dct1[0] );\
850             call_a1( dct_asm.name, dct2[0] );\
851             if( memcmp( dct1, dct2, 16 * sizeof(dctcoef) ) )\
852                 ok = 0;\
853         }\
854         call_c2( dct_c.name, dct1[0] );\
855         call_a2( dct_asm.name, dct2[0] );\
856     }\
857     report( #name " :" );
858
859     TEST_DCTDC(  dct4x4dc );
860     TEST_DCTDC( idct4x4dc );
861 #undef TEST_DCTDC
862
863 #define TEST_DCTDC_CHROMA( name )\
864     ok = 1; used_asm = 0;\
865     if( dct_asm.name != dct_ref.name )\
866     {\
867         set_func_name( #name );\
868         used_asm = 1;\
869         uint16_t *p = (uint16_t*)buf1;\
870         for( int i = 0; i < 16 && ok; i++ )\
871         {\
872             for( int j = 0; j < 8; j++ )\
873                 dct1[j][0] = !i ? (j^j>>1^j>>2)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max dc */\
874                            : i<8 ? (*p++)&1 ? PIXEL_MAX*16 : -PIXEL_MAX*16 /* max elements */\
875                            : ((*p++)&0x1fff)-0x1000; /* general case */\
876             memcpy( dct2, dct1, 8*16 * sizeof(dctcoef) );\
877             call_c1( dct_c.name, dctdc[0], dct1 );\
878             call_a1( dct_asm.name, dctdc[1], dct2 );\
879             if( memcmp( dctdc[0], dctdc[1], 8 * sizeof(dctcoef) ) || memcmp( dct1, dct2, 8*16 * sizeof(dctcoef) ) )\
880             {\
881                 ok = 0;\
882                 fprintf( stderr, #name " [FAILED]\n" ); \
883             }\
884         }\
885         call_c2( dct_c.name, dctdc[0], dct1 );\
886         call_a2( dct_asm.name, dctdc[1], dct2 );\
887     }\
888     report( #name " :" );
889
890     TEST_DCTDC_CHROMA( dct2x4dc );
891 #undef TEST_DCTDC_CHROMA
892
893     x264_zigzag_function_t zigzag_c[2];
894     x264_zigzag_function_t zigzag_ref[2];
895     x264_zigzag_function_t zigzag_asm[2];
896
897     ALIGNED_16( dctcoef level1[64] );
898     ALIGNED_16( dctcoef level2[64] );
899
900 #define TEST_ZIGZAG_SCAN( name, t1, t2, dct, size ) \
901     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
902     { \
903         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
904         used_asm = 1; \
905         memcpy(dct, buf1, size*sizeof(dctcoef)); \
906         call_c( zigzag_c[interlace].name, t1, dct ); \
907         call_a( zigzag_asm[interlace].name, t2, dct ); \
908         if( memcmp( t1, t2, size*sizeof(dctcoef) ) ) \
909         { \
910             ok = 0; \
911             fprintf( stderr, #name " [FAILED]\n" ); \
912         } \
913     }
914
915 #define TEST_ZIGZAG_SUB( name, t1, t2, size ) \
916     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
917     { \
918         int nz_a, nz_c; \
919         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
920         used_asm = 1; \
921         memcpy( pbuf3, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
922         memcpy( pbuf4, pbuf1, 16*FDEC_STRIDE * sizeof(pixel) ); \
923         nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
924         nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
925         if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( pbuf3, pbuf4, 16*FDEC_STRIDE*sizeof(pixel) ) || nz_c != nz_a ) \
926         { \
927             ok = 0; \
928             fprintf( stderr, #name " [FAILED]\n" ); \
929         } \
930         call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3 ); \
931         call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4 ); \
932     }
933
934 #define TEST_ZIGZAG_SUBAC( name, t1, t2 ) \
935     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
936     { \
937         int nz_a, nz_c; \
938         dctcoef dc_a, dc_c; \
939         set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
940         used_asm = 1; \
941         for( int i = 0; i < 2; i++ ) \
942         { \
943             memcpy( pbuf3, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
944             memcpy( pbuf4, pbuf2, 16*FDEC_STRIDE * sizeof(pixel) ); \
945             for( int j = 0; j < 4; j++ ) \
946             { \
947                 memcpy( pbuf3 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
948                 memcpy( pbuf4 + j*FDEC_STRIDE, (i?pbuf1:pbuf2) + j*FENC_STRIDE, 4 * sizeof(pixel) ); \
949             } \
950             nz_c = call_c1( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
951             nz_a = call_a1( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
952             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 ) \
953             { \
954                 ok = 0; \
955                 fprintf( stderr, #name " [FAILED]\n" ); \
956                 break; \
957             } \
958         } \
959         call_c2( zigzag_c[interlace].name, t1, pbuf2, pbuf3, &dc_c ); \
960         call_a2( zigzag_asm[interlace].name, t2, pbuf2, pbuf4, &dc_a ); \
961     }
962
963 #define TEST_INTERLEAVE( name, t1, t2, dct, size ) \
964     if( zigzag_asm[interlace].name != zigzag_ref[interlace].name ) \
965     { \
966         for( int j = 0; j < 100; j++ ) \
967         { \
968             set_func_name( "zigzag_"#name"_%s", interlace?"field":"frame" ); \
969             used_asm = 1; \
970             memcpy(dct, buf1, size*sizeof(dctcoef)); \
971             for( int i = 0; i < size; i++ ) \
972                 dct[i] = rand()&0x1F ? 0 : dct[i]; \
973             memcpy(buf3, buf4, 10); \
974             call_c( zigzag_c[interlace].name, t1, dct, buf3 ); \
975             call_a( zigzag_asm[interlace].name, t2, dct, buf4 ); \
976             if( memcmp( t1, t2, size*sizeof(dctcoef) ) || memcmp( buf3, buf4, 10 ) ) \
977             { \
978                 ok = 0; \
979             } \
980         } \
981     }
982
983     x264_zigzag_init( 0, &zigzag_c[0], &zigzag_c[1] );
984     x264_zigzag_init( cpu_ref, &zigzag_ref[0], &zigzag_ref[1] );
985     x264_zigzag_init( cpu_new, &zigzag_asm[0], &zigzag_asm[1] );
986
987     ok = 1; used_asm = 0;
988     TEST_INTERLEAVE( interleave_8x8_cavlc, level1, level2, dct1[0], 64 );
989     report( "zigzag_interleave :" );
990
991     for( interlace = 0; interlace <= 1; interlace++ )
992     {
993         ok = 1; used_asm = 0;
994         TEST_ZIGZAG_SCAN( scan_8x8, level1, level2, (void*)dct1, 64 );
995         TEST_ZIGZAG_SCAN( scan_4x4, level1, level2, dct1[0], 16  );
996         TEST_ZIGZAG_SUB( sub_4x4, level1, level2, 16 );
997         TEST_ZIGZAG_SUBAC( sub_4x4ac, level1, level2 );
998         report( interlace ? "zigzag_field :" : "zigzag_frame :" );
999     }
1000 #undef TEST_ZIGZAG_SCAN
1001 #undef TEST_ZIGZAG_SUB
1002
1003     return ret;
1004 }
1005
1006 static int check_mc( int cpu_ref, int cpu_new )
1007 {
1008     x264_mc_functions_t mc_c;
1009     x264_mc_functions_t mc_ref;
1010     x264_mc_functions_t mc_a;
1011     x264_pixel_function_t pixf;
1012
1013     pixel *src     = &(pbuf1)[2*64+2];
1014     pixel *src2[4] = { &(pbuf1)[3*64+2], &(pbuf1)[5*64+2],
1015                        &(pbuf1)[7*64+2], &(pbuf1)[9*64+2] };
1016     pixel *dst1    = pbuf3;
1017     pixel *dst2    = pbuf4;
1018
1019     int ret = 0, ok, used_asm;
1020
1021     x264_mc_init( 0, &mc_c );
1022     x264_mc_init( cpu_ref, &mc_ref );
1023     x264_mc_init( cpu_new, &mc_a );
1024     x264_pixel_init( 0, &pixf );
1025
1026 #define MC_TEST_LUMA( w, h ) \
1027         if( mc_a.mc_luma != mc_ref.mc_luma && !(w&(w-1)) && h<=16 ) \
1028         { \
1029             const x264_weight_t *weight = x264_weight_none; \
1030             set_func_name( "mc_luma_%dx%d", w, h ); \
1031             used_asm = 1; \
1032             for( int i = 0; i < 1024; i++ ) \
1033                 pbuf3[i] = pbuf4[i] = 0xCD; \
1034             call_c( mc_c.mc_luma, dst1, 32, src2, 64, dx, dy, w, h, weight ); \
1035             call_a( mc_a.mc_luma, dst2, 32, src2, 64, dx, dy, w, h, weight ); \
1036             if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1037             { \
1038                 fprintf( stderr, "mc_luma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
1039                 ok = 0; \
1040             } \
1041         } \
1042         if( mc_a.get_ref != mc_ref.get_ref ) \
1043         { \
1044             pixel *ref = dst2; \
1045             int ref_stride = 32; \
1046             int w_checked = ( ( sizeof(pixel) == 2 && (w == 12 || w == 20)) ? w-2 : w ); \
1047             const x264_weight_t *weight = x264_weight_none; \
1048             set_func_name( "get_ref_%dx%d", w_checked, 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             ref = (pixel*)call_a( mc_a.get_ref, ref, &ref_stride, src2, 64, dx, dy, w, h, weight ); \
1054             for( int i = 0; i < h; i++ ) \
1055                 if( memcmp( dst1+i*32, ref+i*ref_stride, w_checked * sizeof(pixel) ) ) \
1056                 { \
1057                     fprintf( stderr, "get_ref[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w_checked, h ); \
1058                     ok = 0; \
1059                     break; \
1060                 } \
1061         }
1062
1063 #define MC_TEST_CHROMA( w, h ) \
1064         if( mc_a.mc_chroma != mc_ref.mc_chroma ) \
1065         { \
1066             set_func_name( "mc_chroma_%dx%d", w, 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_chroma, dst1, dst1+8, 16, src, 64, dx, dy, w, h ); \
1071             call_a( mc_a.mc_chroma, dst2, dst2+8, 16, src, 64, dx, dy, w, h ); \
1072             /* mc_chroma width=2 may write garbage to the right of dst. ignore that. */ \
1073             for( int j = 0; j < h; j++ ) \
1074                 for( int i = w; i < 8; i++ ) \
1075                 { \
1076                     dst2[i+j*16+8] = dst1[i+j*16+8]; \
1077                     dst2[i+j*16] = dst1[i+j*16]; \
1078                 } \
1079             if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1080             { \
1081                 fprintf( stderr, "mc_chroma[mv(%d,%d) %2dx%-2d]     [FAILED]\n", dx, dy, w, h ); \
1082                 ok = 0; \
1083             } \
1084         }
1085     ok = 1; used_asm = 0;
1086     for( int dy = -8; dy < 8; dy++ )
1087         for( int dx = -128; dx < 128; dx++ )
1088         {
1089             if( rand()&15 ) continue; // running all of them is too slow
1090             MC_TEST_LUMA( 20, 18 );
1091             MC_TEST_LUMA( 16, 16 );
1092             MC_TEST_LUMA( 16, 8 );
1093             MC_TEST_LUMA( 12, 10 );
1094             MC_TEST_LUMA( 8, 16 );
1095             MC_TEST_LUMA( 8, 8 );
1096             MC_TEST_LUMA( 8, 4 );
1097             MC_TEST_LUMA( 4, 8 );
1098             MC_TEST_LUMA( 4, 4 );
1099         }
1100     report( "mc luma :" );
1101
1102     ok = 1; used_asm = 0;
1103     for( int dy = -1; dy < 9; dy++ )
1104         for( int dx = -128; dx < 128; dx++ )
1105         {
1106             if( rand()&15 ) continue;
1107             MC_TEST_CHROMA( 8, 8 );
1108             MC_TEST_CHROMA( 8, 4 );
1109             MC_TEST_CHROMA( 4, 8 );
1110             MC_TEST_CHROMA( 4, 4 );
1111             MC_TEST_CHROMA( 4, 2 );
1112             MC_TEST_CHROMA( 2, 4 );
1113             MC_TEST_CHROMA( 2, 2 );
1114         }
1115     report( "mc chroma :" );
1116 #undef MC_TEST_LUMA
1117 #undef MC_TEST_CHROMA
1118
1119 #define MC_TEST_AVG( name, weight ) \
1120 { \
1121     ok = 1, used_asm = 0; \
1122     for( int i = 0; i < 12; i++ ) \
1123     { \
1124         memcpy( pbuf3, pbuf1+320, 320 * sizeof(pixel) ); \
1125         memcpy( pbuf4, pbuf1+320, 320 * sizeof(pixel) ); \
1126         if( mc_a.name[i] != mc_ref.name[i] ) \
1127         { \
1128             set_func_name( "%s_%s", #name, pixel_names[i] ); \
1129             used_asm = 1; \
1130             call_c1( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1131             call_a1( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1132             if( memcmp( pbuf3, pbuf4, 320 * sizeof(pixel) ) ) \
1133             { \
1134                 ok = 0; \
1135                 fprintf( stderr, #name "[%d]: [FAILED]\n", i ); \
1136             } \
1137             call_c2( mc_c.name[i], pbuf3, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1138             call_a2( mc_a.name[i], pbuf4, 16, pbuf2+1, 16, pbuf1+18, 16, weight ); \
1139         } \
1140     } \
1141 }
1142
1143     for( int w = -63; w <= 127 && ok; w++ )
1144         MC_TEST_AVG( avg, w );
1145     report( "mc wpredb :" );
1146
1147 #define MC_TEST_WEIGHT( name, weight, aligned ) \
1148     int align_off = (aligned ? 0 : rand()%16); \
1149     ok = 1, used_asm = 0; \
1150     for( int i = 1; i <= 5; i++ ) \
1151     { \
1152         ALIGNED_16( pixel buffC[640] ); \
1153         ALIGNED_16( pixel buffA[640] ); \
1154         int j = X264_MAX( i*4, 2 ); \
1155         memset( buffC, 0, 640 * sizeof(pixel) ); \
1156         memset( buffA, 0, 640 * sizeof(pixel) ); \
1157         x264_t ha; \
1158         ha.mc = mc_a; \
1159         /* w12 is the same as w16 in some cases */ \
1160         if( i == 3 && mc_a.name[i] == mc_a.name[i+1] ) \
1161             continue; \
1162         if( mc_a.name[i] != mc_ref.name[i] ) \
1163         { \
1164             set_func_name( "%s_w%d", #name, j ); \
1165             used_asm = 1; \
1166             call_c1( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
1167             mc_a.weight_cache(&ha, &weight); \
1168             call_a1( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
1169             for( int k = 0; k < 16; k++ ) \
1170                 if( memcmp( &buffC[k*32], &buffA[k*32], j * sizeof(pixel) ) ) \
1171                 { \
1172                     ok = 0; \
1173                     fprintf( stderr, #name "[%d]: [FAILED] s:%d o:%d d%d\n", i, s, o, d ); \
1174                     break; \
1175                 } \
1176             call_c2( mc_c.weight[i], buffC, 32, pbuf2+align_off, 32, &weight, 16 ); \
1177             call_a2( weight.weightfn[i], buffA, 32, pbuf2+align_off, 32, &weight, 16 ); \
1178         } \
1179     }
1180
1181     ok = 1; used_asm = 0;
1182
1183     int align_cnt = 0;
1184     for( int s = 0; s <= 127 && ok; s++ )
1185     {
1186         for( int o = -128; o <= 127 && ok; o++ )
1187         {
1188             if( rand() & 2047 ) continue;
1189             for( int d = 0; d <= 7 && ok; d++ )
1190             {
1191                 if( s == 1<<d )
1192                     continue;
1193                 x264_weight_t weight = { .i_scale = s, .i_denom = d, .i_offset = o };
1194                 MC_TEST_WEIGHT( weight, weight, (align_cnt++ % 4) );
1195             }
1196         }
1197
1198     }
1199     report( "mc weight :" );
1200
1201     ok = 1; used_asm = 0;
1202     for( int o = 0; o <= 127 && ok; o++ )
1203     {
1204         int s = 1, d = 0;
1205         if( rand() & 15 ) continue;
1206         x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1207         MC_TEST_WEIGHT( offsetadd, weight, (align_cnt++ % 4) );
1208     }
1209     report( "mc offsetadd :" );
1210     ok = 1; used_asm = 0;
1211     for( int o = -128; o < 0 && ok; o++ )
1212     {
1213         int s = 1, d = 0;
1214         if( rand() & 15 ) continue;
1215         x264_weight_t weight = { .i_scale = 1, .i_denom = 0, .i_offset = o };
1216         MC_TEST_WEIGHT( offsetsub, weight, (align_cnt++ % 4) );
1217     }
1218     report( "mc offsetsub :" );
1219
1220     ok = 1; used_asm = 0;
1221     for( int height = 8; height <= 16; height += 8 )
1222     {
1223         if( mc_a.store_interleave_chroma != mc_ref.store_interleave_chroma )
1224         {
1225             set_func_name( "store_interleave_chroma" );
1226             used_asm = 1;
1227             memset( pbuf3, 0, 64*height );
1228             memset( pbuf4, 0, 64*height );
1229             call_c( mc_c.store_interleave_chroma, pbuf3, 64, pbuf1, pbuf1+16, height );
1230             call_a( mc_a.store_interleave_chroma, pbuf4, 64, pbuf1, pbuf1+16, height );
1231             if( memcmp( pbuf3, pbuf4, 64*height ) )
1232             {
1233                 ok = 0;
1234                 fprintf( stderr, "store_interleave_chroma FAILED: h=%d\n", height );
1235                 break;
1236             }
1237         }
1238         if( mc_a.load_deinterleave_chroma_fenc != mc_ref.load_deinterleave_chroma_fenc )
1239         {
1240             set_func_name( "load_deinterleave_chroma_fenc" );
1241             used_asm = 1;
1242             call_c( mc_c.load_deinterleave_chroma_fenc, pbuf3, pbuf1, 64, height );
1243             call_a( mc_a.load_deinterleave_chroma_fenc, pbuf4, pbuf1, 64, height );
1244             if( memcmp( pbuf3, pbuf4, FENC_STRIDE*height ) )
1245             {
1246                 ok = 0;
1247                 fprintf( stderr, "load_deinterleave_chroma_fenc FAILED: h=%d\n", height );
1248                 break;
1249             }
1250         }
1251         if( mc_a.load_deinterleave_chroma_fdec != mc_ref.load_deinterleave_chroma_fdec )
1252         {
1253             set_func_name( "load_deinterleave_chroma_fdec" );
1254             used_asm = 1;
1255             call_c( mc_c.load_deinterleave_chroma_fdec, pbuf3, pbuf1, 64, height );
1256             call_a( mc_a.load_deinterleave_chroma_fdec, pbuf4, pbuf1, 64, height );
1257             if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*height ) )
1258             {
1259                 ok = 0;
1260                 fprintf( stderr, "load_deinterleave_chroma_fdec FAILED: h=%d\n", height );
1261                 break;
1262             }
1263         }
1264     }
1265     report( "store_interleave :" );
1266
1267     struct plane_spec {
1268         int w, h, src_stride;
1269     } 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} };
1270     ok = 1; used_asm = 0;
1271     if( mc_a.plane_copy != mc_ref.plane_copy )
1272     {
1273         set_func_name( "plane_copy" );
1274         used_asm = 1;
1275         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1276         {
1277             int w = plane_specs[i].w;
1278             int h = plane_specs[i].h;
1279             int src_stride = plane_specs[i].src_stride;
1280             int dst_stride = (w + 127) & ~63;
1281             assert( dst_stride * h <= 0x1000 );
1282             pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1283             memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1284             memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1285             call_c( mc_c.plane_copy, pbuf3, dst_stride, src1, src_stride, w, h );
1286             call_a( mc_a.plane_copy, pbuf4, dst_stride, src1, src_stride, w, h );
1287             for( int y = 0; y < h; y++ )
1288                 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, w*sizeof(pixel) ) )
1289                 {
1290                     ok = 0;
1291                     fprintf( stderr, "plane_copy FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1292                     break;
1293                 }
1294         }
1295     }
1296
1297     if( mc_a.plane_copy_interleave != mc_ref.plane_copy_interleave )
1298     {
1299         set_func_name( "plane_copy_interleave" );
1300         used_asm = 1;
1301         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1302         {
1303             int w = (plane_specs[i].w + 1) >> 1;
1304             int h = plane_specs[i].h;
1305             int src_stride = (plane_specs[i].src_stride + 1) >> 1;
1306             int dst_stride = (2*w + 127) & ~63;
1307             assert( dst_stride * h <= 0x1000 );
1308             pixel *src1 = pbuf1 + X264_MAX(0, -src_stride) * (h-1);
1309             memset( pbuf3, 0, 0x1000*sizeof(pixel) );
1310             memset( pbuf4, 0, 0x1000*sizeof(pixel) );
1311             call_c( mc_c.plane_copy_interleave, pbuf3, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1312             call_a( mc_a.plane_copy_interleave, pbuf4, dst_stride, src1, src_stride, src1+1024, src_stride+16, w, h );
1313             for( int y = 0; y < h; y++ )
1314                 if( memcmp( pbuf3+y*dst_stride, pbuf4+y*dst_stride, 2*w*sizeof(pixel) ) )
1315                 {
1316                     ok = 0;
1317                     fprintf( stderr, "plane_copy_interleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1318                     break;
1319                 }
1320         }
1321     }
1322
1323     if( mc_a.plane_copy_deinterleave != mc_ref.plane_copy_deinterleave )
1324     {
1325         set_func_name( "plane_copy_deinterleave" );
1326         used_asm = 1;
1327         for( int i = 0; i < sizeof(plane_specs)/sizeof(*plane_specs); i++ )
1328         {
1329             int w = (plane_specs[i].w + 1) >> 1;
1330             int h = plane_specs[i].h;
1331             int dst_stride = w;
1332             int src_stride = (2*w + 127) & ~63;
1333             int offv = (dst_stride*h + 31) & ~15;
1334             memset( pbuf3, 0, 0x1000 );
1335             memset( pbuf4, 0, 0x1000 );
1336             call_c( mc_c.plane_copy_deinterleave, pbuf3, dst_stride, pbuf3+offv, dst_stride, pbuf1, src_stride, w, h );
1337             call_a( mc_a.plane_copy_deinterleave, pbuf4, dst_stride, pbuf4+offv, dst_stride, pbuf1, src_stride, w, h );
1338             for( int y = 0; y < h; y++ )
1339                 if( memcmp( pbuf3+y*dst_stride,      pbuf4+y*dst_stride, w ) ||
1340                     memcmp( pbuf3+y*dst_stride+offv, pbuf4+y*dst_stride+offv, w ) )
1341                 {
1342                     ok = 0;
1343                     fprintf( stderr, "plane_copy_deinterleave FAILED: w=%d h=%d stride=%d\n", w, h, src_stride );
1344                     break;
1345                 }
1346         }
1347     }
1348     report( "plane_copy :" );
1349
1350     if( mc_a.hpel_filter != mc_ref.hpel_filter )
1351     {
1352         pixel *srchpel = pbuf1+8+2*64;
1353         pixel *dstc[3] = { pbuf3+8, pbuf3+8+16*64, pbuf3+8+32*64 };
1354         pixel *dsta[3] = { pbuf4+8, pbuf4+8+16*64, pbuf4+8+32*64 };
1355         void *tmp = pbuf3+49*64;
1356         set_func_name( "hpel_filter" );
1357         ok = 1; used_asm = 1;
1358         memset( pbuf3, 0, 4096 * sizeof(pixel) );
1359         memset( pbuf4, 0, 4096 * sizeof(pixel) );
1360         call_c( mc_c.hpel_filter, dstc[0], dstc[1], dstc[2], srchpel, 64, 48, 10, tmp );
1361         call_a( mc_a.hpel_filter, dsta[0], dsta[1], dsta[2], srchpel, 64, 48, 10, tmp );
1362         for( int i = 0; i < 3; i++ )
1363             for( int j = 0; j < 10; j++ )
1364                 //FIXME ideally the first pixels would match too, but they aren't actually used
1365                 if( memcmp( dstc[i]+j*64+2, dsta[i]+j*64+2, 43 * sizeof(pixel) ) )
1366                 {
1367                     ok = 0;
1368                     fprintf( stderr, "hpel filter differs at plane %c line %d\n", "hvc"[i], j );
1369                     for( int k = 0; k < 48; k++ )
1370                         printf( "%02x%s", dstc[i][j*64+k], (k+1)&3 ? "" : " " );
1371                     printf( "\n" );
1372                     for( int k = 0; k < 48; k++ )
1373                         printf( "%02x%s", dsta[i][j*64+k], (k+1)&3 ? "" : " " );
1374                     printf( "\n" );
1375                     break;
1376                 }
1377         report( "hpel filter :" );
1378     }
1379
1380     if( mc_a.frame_init_lowres_core != mc_ref.frame_init_lowres_core )
1381     {
1382         pixel *dstc[4] = { pbuf3, pbuf3+1024, pbuf3+2048, pbuf3+3072 };
1383         pixel *dsta[4] = { pbuf4, pbuf4+1024, pbuf4+2048, pbuf4+3072 };
1384         set_func_name( "lowres_init" );
1385         ok = 1; used_asm = 1;
1386         for( int w = 40; w <= 48; w += 8 )
1387         {
1388             int stride = (w+8)&~15;
1389             call_c( mc_c.frame_init_lowres_core, pbuf1, dstc[0], dstc[1], dstc[2], dstc[3], w*2, stride, w, 16 );
1390             call_a( mc_a.frame_init_lowres_core, pbuf1, dsta[0], dsta[1], dsta[2], dsta[3], w*2, stride, w, 16 );
1391             for( int i = 0; i < 16; i++ )
1392             {
1393                 for( int j = 0; j < 4; j++ )
1394                     if( memcmp( dstc[j]+i*stride, dsta[j]+i*stride, w * sizeof(pixel) ) )
1395                     {
1396                         ok = 0;
1397                         fprintf( stderr, "frame_init_lowres differs at plane %d line %d\n", j, i );
1398                         for( int k = 0; k < w; k++ )
1399                             printf( "%d ", dstc[j][k+i*stride] );
1400                         printf( "\n" );
1401                         for( int k = 0; k < w; k++ )
1402                             printf( "%d ", dsta[j][k+i*stride] );
1403                         printf( "\n" );
1404                         break;
1405                     }
1406             }
1407         }
1408         report( "lowres init :" );
1409     }
1410
1411 #define INTEGRAL_INIT( name, size, ... )\
1412     if( mc_a.name != mc_ref.name )\
1413     {\
1414         int stride = 80;\
1415         set_func_name( #name );\
1416         used_asm = 1;\
1417         memcpy( buf3, buf1, size*2*stride );\
1418         memcpy( buf4, buf1, size*2*stride );\
1419         uint16_t *sum = (uint16_t*)buf3;\
1420         call_c1( mc_c.name, __VA_ARGS__ );\
1421         sum = (uint16_t*)buf4;\
1422         call_a1( mc_a.name, __VA_ARGS__ );\
1423         if( memcmp( buf3, buf4, (stride-8)*2 ) \
1424             || (size>9 && memcmp( buf3+18*stride, buf4+18*stride, (stride-8)*2 )))\
1425             ok = 0;\
1426         call_c2( mc_c.name, __VA_ARGS__ );\
1427         call_a2( mc_a.name, __VA_ARGS__ );\
1428     }
1429     ok = 1; used_asm = 0;
1430     INTEGRAL_INIT( integral_init4h, 2, sum+stride, pbuf2, stride );
1431     INTEGRAL_INIT( integral_init8h, 2, sum+stride, pbuf2, stride );
1432     INTEGRAL_INIT( integral_init4v, 14, sum, sum+9*stride, stride );
1433     INTEGRAL_INIT( integral_init8v, 9, sum, stride );
1434     report( "integral init :" );
1435
1436     if( mc_a.mbtree_propagate_cost != mc_ref.mbtree_propagate_cost )
1437     {
1438         x264_emms();
1439         for( int i = 0; i < 10; i++ )
1440         {
1441             float fps_factor = (rand()&65535) / 256.;
1442             ok = 1; used_asm = 1;
1443             set_func_name( "mbtree_propagate" );
1444             int *dsta = (int*)buf3;
1445             int *dstc = dsta+400;
1446             uint16_t *prop = (uint16_t*)buf1;
1447             uint16_t *intra = (uint16_t*)buf4;
1448             uint16_t *inter = intra+128;
1449             uint16_t *qscale = inter+128;
1450             uint16_t *rnd = (uint16_t*)buf2;
1451             x264_emms();
1452             for( int j = 0; j < 100; j++ )
1453             {
1454                 intra[j]  = *rnd++ & 0x7fff;
1455                 intra[j] += !intra[j];
1456                 inter[j]  = *rnd++ & 0x7fff;
1457                 qscale[j] = *rnd++ & 0x7fff;
1458             }
1459             call_c( mc_c.mbtree_propagate_cost, dstc, prop, intra, inter, qscale, &fps_factor, 100 );
1460             call_a( mc_a.mbtree_propagate_cost, dsta, prop, intra, inter, qscale, &fps_factor, 100 );
1461             // I don't care about exact rounding, this is just how close the floating-point implementation happens to be
1462             x264_emms();
1463             for( int j = 0; j < 100 && ok; j++ )
1464             {
1465                 ok &= abs( dstc[j]-dsta[j] ) <= 1 || fabs( (double)dstc[j]/dsta[j]-1 ) < 1e-4;
1466                 if( !ok )
1467                     fprintf( stderr, "mbtree_propagate FAILED: %f !~= %f\n", (double)dstc[j], (double)dsta[j] );
1468             }
1469         }
1470         report( "mbtree propagate :" );
1471     }
1472
1473     if( mc_a.memcpy_aligned != mc_ref.memcpy_aligned )
1474     {
1475         set_func_name( "memcpy_aligned" );
1476         ok = 1; used_asm = 1;
1477         for( int size = 16; size < 256; size += 16 )
1478         {
1479             memset( buf4, 0xAA, size + 1 );
1480             call_c( mc_c.memcpy_aligned, buf3, buf1, size );
1481             call_a( mc_a.memcpy_aligned, buf4, buf1, size );
1482             if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1483             {
1484                 ok = 0;
1485                 fprintf( stderr, "memcpy_aligned FAILED: size=%d\n", size );
1486                 break;
1487             }
1488         }
1489         report( "memcpy aligned :" );
1490     }
1491
1492     if( mc_a.memzero_aligned != mc_ref.memzero_aligned )
1493     {
1494         set_func_name( "memzero_aligned" );
1495         ok = 1; used_asm = 1;
1496         for( int size = 128; size < 1024; size += 128 )
1497         {
1498             memset( buf4, 0xAA, size + 1 );
1499             call_c( mc_c.memzero_aligned, buf3, size );
1500             call_a( mc_a.memzero_aligned, buf4, size );
1501             if( memcmp( buf3, buf4, size ) || buf4[size] != 0xAA )
1502             {
1503                 ok = 0;
1504                 fprintf( stderr, "memzero_aligned FAILED: size=%d\n", size );
1505                 break;
1506             }
1507         }
1508         report( "memzero aligned :" );
1509     }
1510
1511     return ret;
1512 }
1513
1514 static int check_deblock( int cpu_ref, int cpu_new )
1515 {
1516     x264_deblock_function_t db_c;
1517     x264_deblock_function_t db_ref;
1518     x264_deblock_function_t db_a;
1519     int ret = 0, ok = 1, used_asm = 0;
1520     int alphas[36], betas[36];
1521     int8_t tcs[36][4];
1522
1523     x264_deblock_init( 0, &db_c, 0 );
1524     x264_deblock_init( cpu_ref, &db_ref, 0 );
1525     x264_deblock_init( cpu_new, &db_a, 0 );
1526
1527     /* not exactly the real values of a,b,tc but close enough */
1528     for( int i = 35, a = 255, c = 250; i >= 0; i-- )
1529     {
1530         alphas[i] = a << (BIT_DEPTH-8);
1531         betas[i] = (i+1)/2 << (BIT_DEPTH-8);
1532         tcs[i][0] = tcs[i][3] = (c+6)/10 << (BIT_DEPTH-8);
1533         tcs[i][1] = (c+7)/15 << (BIT_DEPTH-8);
1534         tcs[i][2] = (c+9)/20 << (BIT_DEPTH-8);
1535         a = a*9/10;
1536         c = c*9/10;
1537     }
1538
1539 #define TEST_DEBLOCK( name, align, ... ) \
1540     for( int i = 0; i < 36; i++ ) \
1541     { \
1542         int off = 8*32 + (i&15)*4*!align; /* benchmark various alignments of h filter */ \
1543         for( int j = 0; j < 1024; j++ ) \
1544             /* two distributions of random to excersize different failure modes */ \
1545             pbuf3[j] = rand() & (i&1 ? 0xf : PIXEL_MAX ); \
1546         memcpy( pbuf4, pbuf3, 1024 * sizeof(pixel) ); \
1547         if( db_a.name != db_ref.name ) \
1548         { \
1549             set_func_name( #name ); \
1550             used_asm = 1; \
1551             call_c1( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1552             call_a1( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1553             if( memcmp( pbuf3, pbuf4, 1024 * sizeof(pixel) ) ) \
1554             { \
1555                 ok = 0; \
1556                 fprintf( stderr, #name "(a=%d, b=%d): [FAILED]\n", alphas[i], betas[i] ); \
1557                 break; \
1558             } \
1559             call_c2( db_c.name, pbuf3+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1560             call_a2( db_a.name, pbuf4+off, 32, alphas[i], betas[i], ##__VA_ARGS__ ); \
1561         } \
1562     }
1563
1564     TEST_DEBLOCK( deblock_luma[0], 0, tcs[i] );
1565     TEST_DEBLOCK( deblock_luma[1], 1, tcs[i] );
1566     TEST_DEBLOCK( deblock_h_chroma_420, 0, tcs[i] );
1567     TEST_DEBLOCK( deblock_h_chroma_422, 0, tcs[i] );
1568     TEST_DEBLOCK( deblock_chroma_420_mbaff, 0, tcs[i] );
1569     TEST_DEBLOCK( deblock_chroma_422_mbaff, 0, tcs[i] );
1570     TEST_DEBLOCK( deblock_chroma[1], 1, tcs[i] );
1571     TEST_DEBLOCK( deblock_luma_intra[0], 0 );
1572     TEST_DEBLOCK( deblock_luma_intra[1], 1 );
1573     TEST_DEBLOCK( deblock_h_chroma_420_intra, 0 );
1574     TEST_DEBLOCK( deblock_h_chroma_422_intra, 0 );
1575     TEST_DEBLOCK( deblock_chroma_420_intra_mbaff, 0 );
1576     TEST_DEBLOCK( deblock_chroma_422_intra_mbaff, 0 );
1577     TEST_DEBLOCK( deblock_chroma_intra[1], 1 );
1578
1579     if( db_a.deblock_strength != db_ref.deblock_strength )
1580     {
1581         for( int i = 0; i < 100; i++ )
1582         {
1583             ALIGNED_ARRAY_16( uint8_t, nnz, [X264_SCAN8_SIZE] );
1584             ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
1585             ALIGNED_ARRAY_16( int16_t, mv, [2],[X264_SCAN8_LUMA_SIZE][2] );
1586             ALIGNED_ARRAY_16( uint8_t, bs, [2],[2][8][4] );
1587             memset( bs, 99, sizeof(bs) );
1588             for( int j = 0; j < X264_SCAN8_SIZE; j++ )
1589                 nnz[j] = ((rand()&7) == 7) * rand() & 0xf;
1590             for( int j = 0; j < 2; j++ )
1591                 for( int k = 0; k < X264_SCAN8_LUMA_SIZE; k++ )
1592                 {
1593                     ref[j][k] = ((rand()&3) != 3) ? 0 : (rand() & 31) - 2;
1594                     for( int l = 0; l < 2; l++ )
1595                         mv[j][k][l] = ((rand()&7) != 7) ? (rand()&7) - 3 : (rand()&1023) - 512;
1596                 }
1597             set_func_name( "deblock_strength" );
1598             call_c( db_c.deblock_strength, nnz, ref, mv, bs[0], 2<<(i&1), ((i>>1)&1) );
1599             call_a( db_a.deblock_strength, nnz, ref, mv, bs[1], 2<<(i&1), ((i>>1)&1) );
1600             if( memcmp( bs[0], bs[1], sizeof(bs[0]) ) )
1601             {
1602                 ok = 0;
1603                 fprintf( stderr, "deblock_strength: [FAILED]\n" );
1604                 for( int j = 0; j < 2; j++ )
1605                 {
1606                     for( int k = 0; k < 2; k++ )
1607                         for( int l = 0; l < 4; l++ )
1608                         {
1609                             for( int m = 0; m < 4; m++ )
1610                                 printf("%d ",bs[j][k][l][m]);
1611                             printf("\n");
1612                         }
1613                     printf("\n");
1614                 }
1615                 break;
1616             }
1617         }
1618     }
1619
1620     report( "deblock :" );
1621
1622     return ret;
1623 }
1624
1625 static int check_quant( int cpu_ref, int cpu_new )
1626 {
1627     x264_quant_function_t qf_c;
1628     x264_quant_function_t qf_ref;
1629     x264_quant_function_t qf_a;
1630     ALIGNED_16( dctcoef dct1[64] );
1631     ALIGNED_16( dctcoef dct2[64] );
1632     ALIGNED_16( dctcoef dct3[8][16] );
1633     ALIGNED_16( dctcoef dct4[8][16] );
1634     ALIGNED_16( uint8_t cqm_buf[64] );
1635     int ret = 0, ok, used_asm;
1636     int oks[3] = {1,1,1}, used_asms[3] = {0,0,0};
1637     x264_t h_buf;
1638     x264_t *h = &h_buf;
1639     memset( h, 0, sizeof(*h) );
1640     h->sps->i_chroma_format_idc = 1;
1641     x264_param_default( &h->param );
1642     h->chroma_qp_table = i_chroma_qp_table + 12;
1643     h->param.analyse.b_transform_8x8 = 1;
1644
1645     for( int i_cqm = 0; i_cqm < 4; i_cqm++ )
1646     {
1647         if( i_cqm == 0 )
1648         {
1649             for( int i = 0; i < 6; i++ )
1650                 h->pps->scaling_list[i] = x264_cqm_flat16;
1651             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_FLAT;
1652         }
1653         else if( i_cqm == 1 )
1654         {
1655             for( int i = 0; i < 6; i++ )
1656                 h->pps->scaling_list[i] = x264_cqm_jvt[i];
1657             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_JVT;
1658         }
1659         else
1660         {
1661             int max_scale = BIT_DEPTH < 10 ? 255 : 228;
1662             if( i_cqm == 2 )
1663                 for( int i = 0; i < 64; i++ )
1664                     cqm_buf[i] = 10 + rand() % (max_scale - 9);
1665             else
1666                 for( int i = 0; i < 64; i++ )
1667                     cqm_buf[i] = 1;
1668             for( int i = 0; i < 6; i++ )
1669                 h->pps->scaling_list[i] = cqm_buf;
1670             h->param.i_cqm_preset = h->pps->i_cqm_preset = X264_CQM_CUSTOM;
1671         }
1672
1673         h->param.rc.i_qp_min = 0;
1674         h->param.rc.i_qp_max = QP_MAX;
1675         x264_cqm_init( h );
1676         x264_quant_init( h, 0, &qf_c );
1677         x264_quant_init( h, cpu_ref, &qf_ref );
1678         x264_quant_init( h, cpu_new, &qf_a );
1679
1680 #define INIT_QUANT8(j) \
1681         { \
1682             static const int scale1d[8] = {32,31,24,31,32,31,24,31}; \
1683             for( int i = 0; i < 64; i++ ) \
1684             { \
1685                 unsigned int scale = (255*scale1d[i>>3]*scale1d[i&7])/16; \
1686                 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1687             } \
1688         }
1689
1690 #define INIT_QUANT4(j) \
1691         { \
1692             static const int scale1d[4] = {4,6,4,6}; \
1693             for( int i = 0; i < 16; i++ ) \
1694             { \
1695                 unsigned int scale = 255*scale1d[i>>2]*scale1d[i&3]; \
1696                 dct1[i] = dct2[i] = j ? (rand()%(2*scale+1))-scale : 0; \
1697             } \
1698         }
1699
1700 #define TEST_QUANT_DC( name, cqm ) \
1701         if( qf_a.name != qf_ref.name ) \
1702         { \
1703             set_func_name( #name ); \
1704             used_asms[0] = 1; \
1705             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1706             { \
1707                 for( int j = 0; j < 2; j++ ) \
1708                 { \
1709                     int result_c, result_a; \
1710                     for( int i = 0; i < 16; i++ ) \
1711                         dct1[i] = dct2[i] = j ? (rand() & 0x1fff) - 0xfff : 0; \
1712                     result_c = call_c1( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1713                     result_a = call_a1( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1714                     if( memcmp( dct1, dct2, 16*sizeof(dctcoef) ) || result_c != result_a ) \
1715                     { \
1716                         oks[0] = 0; \
1717                         fprintf( stderr, #name "(cqm=%d): [FAILED]\n", i_cqm ); \
1718                         break; \
1719                     } \
1720                     call_c2( qf_c.name, dct1, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1721                     call_a2( qf_a.name, dct2, h->quant4_mf[CQM_4IY][qp][0], h->quant4_bias[CQM_4IY][qp][0] ); \
1722                 } \
1723             } \
1724         }
1725
1726 #define TEST_QUANT( qname, block, w ) \
1727         if( qf_a.qname != qf_ref.qname ) \
1728         { \
1729             set_func_name( #qname ); \
1730             used_asms[0] = 1; \
1731             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1732             { \
1733                 for( int j = 0; j < 2; j++ ) \
1734                 { \
1735                     INIT_QUANT##w(j) \
1736                     int result_c = call_c1( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1737                     int result_a = call_a1( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1738                     if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) || result_c != result_a ) \
1739                     { \
1740                         oks[0] = 0; \
1741                         fprintf( stderr, #qname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1742                         break; \
1743                     } \
1744                     call_c2( qf_c.qname, dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1745                     call_a2( qf_a.qname, dct2, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1746                 } \
1747             } \
1748         }
1749
1750         TEST_QUANT( quant_8x8, CQM_8IY, 8 );
1751         TEST_QUANT( quant_8x8, CQM_8PY, 8 );
1752         TEST_QUANT( quant_4x4, CQM_4IY, 4 );
1753         TEST_QUANT( quant_4x4, CQM_4PY, 4 );
1754         TEST_QUANT_DC( quant_4x4_dc, **h->quant4_mf[CQM_4IY] );
1755         TEST_QUANT_DC( quant_2x2_dc, **h->quant4_mf[CQM_4IC] );
1756
1757 #define TEST_DEQUANT( qname, dqname, block, w ) \
1758         if( qf_a.dqname != qf_ref.dqname ) \
1759         { \
1760             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1761             used_asms[1] = 1; \
1762             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1763             { \
1764                 INIT_QUANT##w(1) \
1765                 qf_c.qname( dct1, h->quant##w##_mf[block][qp], h->quant##w##_bias[block][qp] ); \
1766                 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1767                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1768                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1769                 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1770                 { \
1771                     oks[1] = 0; \
1772                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1773                     break; \
1774                 } \
1775                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1776                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1777             } \
1778         }
1779
1780         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8IY, 8 );
1781         TEST_DEQUANT( quant_8x8, dequant_8x8, CQM_8PY, 8 );
1782         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4IY, 4 );
1783         TEST_DEQUANT( quant_4x4, dequant_4x4, CQM_4PY, 4 );
1784
1785 #define TEST_DEQUANT_DC( qname, dqname, block, w ) \
1786         if( qf_a.dqname != qf_ref.dqname ) \
1787         { \
1788             set_func_name( "%s_%s", #dqname, i_cqm?"cqm":"flat" ); \
1789             used_asms[1] = 1; \
1790             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1791             { \
1792                 for( int i = 0; i < 16; i++ ) \
1793                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16; \
1794                 qf_c.qname( dct1, h->quant##w##_mf[block][qp][0]>>1, h->quant##w##_bias[block][qp][0]>>1 ); \
1795                 memcpy( dct2, dct1, w*w*sizeof(dctcoef) ); \
1796                 call_c1( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1797                 call_a1( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1798                 if( memcmp( dct1, dct2, w*w*sizeof(dctcoef) ) ) \
1799                 { \
1800                     oks[1] = 0; \
1801                     fprintf( stderr, #dqname "(qp=%d, cqm=%d, block="#block"): [FAILED]\n", qp, i_cqm ); \
1802                 } \
1803                 call_c2( qf_c.dqname, dct1, h->dequant##w##_mf[block], qp ); \
1804                 call_a2( qf_a.dqname, dct2, h->dequant##w##_mf[block], qp ); \
1805             } \
1806         }
1807
1808         TEST_DEQUANT_DC( quant_4x4_dc, dequant_4x4_dc, CQM_4IY, 4 );
1809
1810         if( qf_a.idct_dequant_2x4_dc != qf_ref.idct_dequant_2x4_dc )
1811         {
1812             set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1813             used_asms[1] = 1;
1814             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1815             {
1816                 for( int i = 0; i < 8; i++ )
1817                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1818                 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 );
1819                 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 );
1820                 call_c( qf_c.idct_dequant_2x4_dc, dct1, dct3, h->dequant4_mf[CQM_4IC], qp+3 );
1821                 call_a( qf_a.idct_dequant_2x4_dc, dct1, dct4, h->dequant4_mf[CQM_4IC], qp+3 );
1822                 for( int i = 0; i < 8; i++ )
1823                     if( dct3[i][0] != dct4[i][0] )
1824                     {
1825                         oks[1] = 0;
1826                         fprintf( stderr, "idct_dequant_2x4_dc (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1827                         break;
1828                     }
1829             }
1830         }
1831
1832         if( qf_a.idct_dequant_2x4_dconly != qf_ref.idct_dequant_2x4_dconly )
1833         {
1834             set_func_name( "idct_dequant_2x4_dc_%s", i_cqm?"cqm":"flat" );
1835             used_asms[1] = 1;
1836             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- )
1837             {
1838                 for( int i = 0; i < 8; i++ )
1839                     dct1[i] = rand()%(PIXEL_MAX*16*2+1) - PIXEL_MAX*16;
1840                 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 );
1841                 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 );
1842                 memcpy( dct2, dct1, 8*sizeof(dctcoef) );
1843                 call_c1( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1844                 call_a1( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1845                 if( memcmp( dct1, dct2, 8*sizeof(dctcoef) ) )
1846                 {
1847                     oks[1] = 0;
1848                     fprintf( stderr, "idct_dequant_2x4_dconly (qp=%d, cqm=%d): [FAILED]\n", qp, i_cqm );
1849                     break;
1850                 }
1851                 call_c2( qf_c.idct_dequant_2x4_dconly, dct1, h->dequant4_mf[CQM_4IC], qp+3 );
1852                 call_a2( qf_a.idct_dequant_2x4_dconly, dct2, h->dequant4_mf[CQM_4IC], qp+3 );
1853             }
1854         }
1855
1856 #define TEST_OPTIMIZE_CHROMA_DC( optname, size ) \
1857         if( qf_a.optname != qf_ref.optname ) \
1858         { \
1859             set_func_name( #optname ); \
1860             used_asms[2] = 1; \
1861             for( int qp = h->param.rc.i_qp_max; qp >= h->param.rc.i_qp_min; qp-- ) \
1862             { \
1863                 int qpdc = qp + (size == 8 ? 3 : 0); \
1864                 int dmf = h->dequant4_mf[CQM_4IC][qpdc%6][0] << qpdc/6; \
1865                 if( dmf > 32*64 ) \
1866                     continue; \
1867                 for( int i = 16; ; i <<= 1 ) \
1868                 { \
1869                     int res_c, res_asm; \
1870                     int max = X264_MIN( i, PIXEL_MAX*16 ); \
1871                     for( int j = 0; j < size; j++ ) \
1872                         dct1[j] = rand()%(max*2+1) - max; \
1873                     for( int j = 0; i <= size; j += 4 ) \
1874                         qf_c.quant_2x2_dc( &dct1[j], h->quant4_mf[CQM_4IC][qpdc][0]>>1, h->quant4_bias[CQM_4IC][qpdc][0]>>1 ); \
1875                     memcpy( dct2, dct1, size*sizeof(dctcoef) ); \
1876                     res_c   = call_c1( qf_c.optname, dct1, dmf ); \
1877                     res_asm = call_a1( qf_a.optname, dct2, dmf ); \
1878                     if( res_c != res_asm || memcmp( dct1, dct2, size*sizeof(dctcoef) ) ) \
1879                     { \
1880                         oks[2] = 0; \
1881                         fprintf( stderr, #optname "(qp=%d, res_c=%d, res_asm=%d): [FAILED]\n", qp, res_c, res_asm ); \
1882                     } \
1883                     call_c2( qf_c.optname, dct1, dmf ); \
1884                     call_a2( qf_a.optname, dct2, dmf ); \
1885                     if( i >= PIXEL_MAX*16 ) \
1886                         break; \
1887                 } \
1888             } \
1889         }
1890
1891         TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x2_dc, 4 );
1892         TEST_OPTIMIZE_CHROMA_DC( optimize_chroma_2x4_dc, 8 );
1893
1894         x264_cqm_delete( h );
1895     }
1896
1897     ok = oks[0]; used_asm = used_asms[0];
1898     report( "quant :" );
1899
1900     ok = oks[1]; used_asm = used_asms[1];
1901     report( "dequant :" );
1902
1903     ok = oks[2]; used_asm = used_asms[2];
1904     report( "optimize chroma dc :" );
1905
1906     ok = 1; used_asm = 0;
1907     if( qf_a.denoise_dct != qf_ref.denoise_dct )
1908     {
1909         used_asm = 1;
1910         for( int size = 16; size <= 64; size += 48 )
1911         {
1912             set_func_name( "denoise_dct" );
1913             memcpy( dct1, buf1, size*sizeof(dctcoef) );
1914             memcpy( dct2, buf1, size*sizeof(dctcoef) );
1915             memcpy( buf3+256, buf3, 256 );
1916             call_c1( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
1917             call_a1( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
1918             if( memcmp( dct1, dct2, size*sizeof(dctcoef) ) || memcmp( buf3+4, buf3+256+4, (size-1)*sizeof(uint32_t) ) )
1919                 ok = 0;
1920             call_c2( qf_c.denoise_dct, dct1, (uint32_t*)buf3, (udctcoef*)buf2, size );
1921             call_a2( qf_a.denoise_dct, dct2, (uint32_t*)(buf3+256), (udctcoef*)buf2, size );
1922         }
1923     }
1924     report( "denoise dct :" );
1925
1926 #define TEST_DECIMATE( decname, w, ac, thresh ) \
1927     if( qf_a.decname != qf_ref.decname ) \
1928     { \
1929         set_func_name( #decname ); \
1930         used_asm = 1; \
1931         for( int i = 0; i < 100; i++ ) \
1932         { \
1933             static const int distrib[16] = {1,1,1,1,1,1,1,1,1,1,1,1,2,3,4};\
1934             static const int zerorate_lut[4] = {3,7,15,31};\
1935             int zero_rate = zerorate_lut[i&3];\
1936             for( int idx = 0; idx < w*w; idx++ ) \
1937             { \
1938                 int sign = (rand()&1) ? -1 : 1; \
1939                 int abs_level = distrib[rand()&15]; \
1940                 if( abs_level == 4 ) abs_level = rand()&0x3fff; \
1941                 int zero = !(rand()&zero_rate); \
1942                 dct1[idx] = zero * abs_level * sign; \
1943             } \
1944             if( ac ) \
1945                 dct1[0] = 0; \
1946             int result_c = call_c( qf_c.decname, dct1 ); \
1947             int result_a = call_a( qf_a.decname, dct1 ); \
1948             if( X264_MIN(result_c,thresh) != X264_MIN(result_a,thresh) ) \
1949             { \
1950                 ok = 0; \
1951                 fprintf( stderr, #decname ": [FAILED]\n" ); \
1952                 break; \
1953             } \
1954         } \
1955     }
1956
1957     ok = 1; used_asm = 0;
1958     TEST_DECIMATE( decimate_score64, 8, 0, 6 );
1959     TEST_DECIMATE( decimate_score16, 4, 0, 6 );
1960     TEST_DECIMATE( decimate_score15, 4, 1, 7 );
1961     report( "decimate_score :" );
1962
1963 #define TEST_LAST( last, lastname, size, ac ) \
1964     if( qf_a.last != qf_ref.last ) \
1965     { \
1966         set_func_name( #lastname ); \
1967         used_asm = 1; \
1968         for( int i = 0; i < 100; i++ ) \
1969         { \
1970             int nnz = 0; \
1971             int max = rand() & (size-1); \
1972             memset( dct1, 0, size*sizeof(dctcoef) ); \
1973             for( int idx = ac; idx < max; idx++ ) \
1974                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
1975             if( !nnz ) \
1976                 dct1[ac] = 1; \
1977             int result_c = call_c( qf_c.last, dct1+ac ); \
1978             int result_a = call_a( qf_a.last, dct1+ac ); \
1979             if( result_c != result_a ) \
1980             { \
1981                 ok = 0; \
1982                 fprintf( stderr, #lastname ": [FAILED]\n" ); \
1983                 break; \
1984             } \
1985         } \
1986     }
1987
1988     ok = 1; used_asm = 0;
1989     TEST_LAST( coeff_last4              , coeff_last4,   4, 0 );
1990     TEST_LAST( coeff_last8              , coeff_last8,   8, 0 );
1991     TEST_LAST( coeff_last[  DCT_LUMA_AC], coeff_last15, 16, 1 );
1992     TEST_LAST( coeff_last[ DCT_LUMA_4x4], coeff_last16, 16, 0 );
1993     TEST_LAST( coeff_last[ DCT_LUMA_8x8], coeff_last64, 64, 0 );
1994     report( "coeff_last :" );
1995
1996 #define TEST_LEVELRUN( lastname, name, size, ac ) \
1997     if( qf_a.lastname != qf_ref.lastname ) \
1998     { \
1999         set_func_name( #name ); \
2000         used_asm = 1; \
2001         for( int i = 0; i < 100; i++ ) \
2002         { \
2003             x264_run_level_t runlevel_c, runlevel_a; \
2004             int nnz = 0; \
2005             int max = rand() & (size-1); \
2006             memset( dct1, 0, size*sizeof(dctcoef) ); \
2007             memcpy( &runlevel_a, buf1+i, sizeof(x264_run_level_t) ); \
2008             memcpy( &runlevel_c, buf1+i, sizeof(x264_run_level_t) ); \
2009             for( int idx = ac; idx < max; idx++ ) \
2010                 nnz |= dct1[idx] = !(rand()&3) + (!(rand()&15))*rand(); \
2011             if( !nnz ) \
2012                 dct1[ac] = 1; \
2013             int result_c = call_c( qf_c.lastname, dct1+ac, &runlevel_c ); \
2014             int result_a = call_a( qf_a.lastname, dct1+ac, &runlevel_a ); \
2015             if( result_c != result_a || runlevel_c.last != runlevel_a.last || \
2016                 runlevel_c.mask != runlevel_a.mask || \
2017                 memcmp(runlevel_c.level, runlevel_a.level, sizeof(dctcoef)*result_c) || \
2018                 memcmp(runlevel_c.run, runlevel_a.run, sizeof(uint8_t)*(result_c-1)) ) \
2019             { \
2020                 ok = 0; \
2021                 fprintf( stderr, #name ": [FAILED]\n" ); \
2022                 break; \
2023             } \
2024         } \
2025     }
2026
2027     ok = 1; used_asm = 0;
2028     TEST_LEVELRUN( coeff_level_run4              , coeff_level_run4,   4, 0 );
2029     TEST_LEVELRUN( coeff_level_run8              , coeff_level_run8,   8, 0 );
2030     TEST_LEVELRUN( coeff_level_run[  DCT_LUMA_AC], coeff_level_run15, 16, 1 );
2031     TEST_LEVELRUN( coeff_level_run[ DCT_LUMA_4x4], coeff_level_run16, 16, 0 );
2032     report( "coeff_level_run :" );
2033
2034     return ret;
2035 }
2036
2037 static int check_intra( int cpu_ref, int cpu_new )
2038 {
2039     int ret = 0, ok = 1, used_asm = 0;
2040     ALIGNED_ARRAY_32( pixel, edge,[36] );
2041     ALIGNED_ARRAY_32( pixel, edge2,[36] );
2042     ALIGNED_16( pixel fdec[FDEC_STRIDE*20] );
2043     struct
2044     {
2045         x264_predict_t      predict_16x16[4+3];
2046         x264_predict_t      predict_8x8c[4+3];
2047         x264_predict_t      predict_8x16c[4+3];
2048         x264_predict8x8_t   predict_8x8[9+3];
2049         x264_predict_t      predict_4x4[9+3];
2050         x264_predict_8x8_filter_t predict_8x8_filter;
2051     } ip_c, ip_ref, ip_a;
2052
2053     x264_predict_16x16_init( 0, ip_c.predict_16x16 );
2054     x264_predict_8x8c_init( 0, ip_c.predict_8x8c );
2055     x264_predict_8x16c_init( 0, ip_c.predict_8x16c );
2056     x264_predict_8x8_init( 0, ip_c.predict_8x8, &ip_c.predict_8x8_filter );
2057     x264_predict_4x4_init( 0, ip_c.predict_4x4 );
2058
2059     x264_predict_16x16_init( cpu_ref, ip_ref.predict_16x16 );
2060     x264_predict_8x8c_init( cpu_ref, ip_ref.predict_8x8c );
2061     x264_predict_8x16c_init( cpu_ref, ip_ref.predict_8x16c );
2062     x264_predict_8x8_init( cpu_ref, ip_ref.predict_8x8, &ip_ref.predict_8x8_filter );
2063     x264_predict_4x4_init( cpu_ref, ip_ref.predict_4x4 );
2064
2065     x264_predict_16x16_init( cpu_new, ip_a.predict_16x16 );
2066     x264_predict_8x8c_init( cpu_new, ip_a.predict_8x8c );
2067     x264_predict_8x16c_init( cpu_new, ip_a.predict_8x16c );
2068     x264_predict_8x8_init( cpu_new, ip_a.predict_8x8, &ip_a.predict_8x8_filter );
2069     x264_predict_4x4_init( cpu_new, ip_a.predict_4x4 );
2070
2071     memcpy( fdec, pbuf1, 32*20 * sizeof(pixel) );\
2072
2073     ip_c.predict_8x8_filter( fdec+48, edge, ALL_NEIGHBORS, ALL_NEIGHBORS );
2074
2075 #define INTRA_TEST( name, dir, w, h, align, bench, ... )\
2076     if( ip_a.name[dir] != ip_ref.name[dir] )\
2077     {\
2078         set_func_name( "intra_%s_%s", #name, intra_##name##_names[dir] );\
2079         used_asm = 1;\
2080         memcpy( pbuf3, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2081         memcpy( pbuf4, fdec, FDEC_STRIDE*20 * sizeof(pixel) );\
2082         for( int a = 0; a < (do_bench ? 64/sizeof(pixel) : 1); a += align )\
2083         {\
2084             call_c##bench( ip_c.name[dir], pbuf3+48+a, ##__VA_ARGS__ );\
2085             call_a##bench( ip_a.name[dir], pbuf4+48+a, ##__VA_ARGS__ );\
2086             if( memcmp( pbuf3, pbuf4, FDEC_STRIDE*20 * sizeof(pixel) ) )\
2087             {\
2088                 fprintf( stderr, #name "[%d] :  [FAILED]\n", dir );\
2089                 ok = 0;\
2090                 for( int k = -1; k < 16; k++ )\
2091                     printf( "%2x ", edge[16+k] );\
2092                 printf( "\n" );\
2093                 for( int j = 0; j < h; j++ )\
2094                 {\
2095                     printf( "%2x ", edge[14-j] );\
2096                     for( int k = 0; k < w; k++ )\
2097                         printf( "%2x ", pbuf4[48+k+j*FDEC_STRIDE] );\
2098                     printf( "\n" );\
2099                 }\
2100                 printf( "\n" );\
2101                 for( int j = 0; j < h; j++ )\
2102                 {\
2103                     printf( "   " );\
2104                     for( int k = 0; k < w; k++ )\
2105                         printf( "%2x ", pbuf3[48+k+j*FDEC_STRIDE] );\
2106                     printf( "\n" );\
2107                 }\
2108                 break;\
2109             }\
2110         }\
2111     }
2112
2113     for( int i = 0; i < 12; i++ )
2114         INTRA_TEST(   predict_4x4, i,  4,  4,  4, );
2115     for( int i = 0; i < 7; i++ )
2116         INTRA_TEST(  predict_8x8c, i,  8,  8, 16, );
2117     for( int i = 0; i < 7; i++ )
2118         INTRA_TEST( predict_8x16c, i,  8, 16, 16, );
2119     for( int i = 0; i < 7; i++ )
2120         INTRA_TEST( predict_16x16, i, 16, 16, 16, );
2121     for( int i = 0; i < 12; i++ )
2122         INTRA_TEST(   predict_8x8, i,  8,  8,  8, , edge );
2123
2124     set_func_name("intra_predict_8x8_filter");
2125     if( ip_a.predict_8x8_filter != ip_ref.predict_8x8_filter )
2126     {
2127         used_asm = 1;
2128         for( int i = 0; i < 32; i++ )
2129         {
2130             if( !(i&7) || ((i&MB_TOPRIGHT) && !(i&MB_TOP)) )
2131                 continue;
2132             int neighbor = (i&24)>>1;
2133             memset( edge,  0, 36*sizeof(pixel) );
2134             memset( edge2, 0, 36*sizeof(pixel) );
2135             call_c( ip_c.predict_8x8_filter, pbuf1+48, edge,  neighbor, i&7 );
2136             call_a( ip_a.predict_8x8_filter, pbuf1+48, edge2, neighbor, i&7 );
2137             if( !(neighbor&MB_TOPLEFT) )
2138                 edge[15] = edge2[15] = 0;
2139             if( memcmp( edge+7, edge2+7, (i&MB_TOPRIGHT ? 26 : i&MB_TOP ? 17 : 8) * sizeof(pixel) ) )
2140             {
2141                 fprintf( stderr, "predict_8x8_filter :  [FAILED] %d %d\n", (i&24)>>1, i&7);
2142                 ok = 0;
2143             }
2144         }
2145     }
2146
2147 #define EXTREMAL_PLANE( w, h ) \
2148     { \
2149         int max[7]; \
2150         for( int j = 0; j < 7; j++ ) \
2151             max[j] = test ? rand()&PIXEL_MAX : PIXEL_MAX; \
2152         fdec[48-1-FDEC_STRIDE] = (i&1)*max[0]; \
2153         for( int j = 0; j < w/2; j++ ) \
2154             fdec[48+j-FDEC_STRIDE] = (!!(i&2))*max[1]; \
2155         for( int j = w/2; j < w-1; j++ ) \
2156             fdec[48+j-FDEC_STRIDE] = (!!(i&4))*max[2]; \
2157         fdec[48+(w-1)-FDEC_STRIDE] = (!!(i&8))*max[3]; \
2158         for( int j = 0; j < h/2; j++ ) \
2159             fdec[48+j*FDEC_STRIDE-1] = (!!(i&16))*max[4]; \
2160         for( int j = h/2; j < h-1; j++ ) \
2161             fdec[48+j*FDEC_STRIDE-1] = (!!(i&32))*max[5]; \
2162         fdec[48+(h-1)*FDEC_STRIDE-1] = (!!(i&64))*max[6]; \
2163     }
2164     /* Extremal test case for planar prediction. */
2165     for( int test = 0; test < 100 && ok; test++ )
2166         for( int i = 0; i < 128 && ok; i++ )
2167         {
2168             EXTREMAL_PLANE(  8,  8 );
2169             INTRA_TEST(  predict_8x8c, I_PRED_CHROMA_P,  8,  8, 64, 1 );
2170             EXTREMAL_PLANE(  8, 16 );
2171             INTRA_TEST( predict_8x16c, I_PRED_CHROMA_P,  8, 16, 64, 1 );
2172             EXTREMAL_PLANE( 16, 16 );
2173             INTRA_TEST( predict_16x16,  I_PRED_16x16_P, 16, 16, 64, 1 );
2174         }
2175     report( "intra pred :" );
2176     return ret;
2177 }
2178
2179 #define DECL_CABAC(cpu) \
2180 static void run_cabac_decision_##cpu( x264_t *h, uint8_t *dst )\
2181 {\
2182     x264_cabac_t cb;\
2183     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2184     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2185     for( int i = 0; i < 0x1000; i++ )\
2186         x264_cabac_encode_decision_##cpu( &cb, buf1[i]>>1, buf1[i]&1 );\
2187 }\
2188 static void run_cabac_bypass_##cpu( x264_t *h, uint8_t *dst )\
2189 {\
2190     x264_cabac_t cb;\
2191     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2192     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2193     for( int i = 0; i < 0x1000; i++ )\
2194         x264_cabac_encode_bypass_##cpu( &cb, buf1[i]&1 );\
2195 }\
2196 static void run_cabac_terminal_##cpu( x264_t *h, uint8_t *dst )\
2197 {\
2198     x264_cabac_t cb;\
2199     x264_cabac_context_init( h, &cb, SLICE_TYPE_P, 26, 0 );\
2200     x264_cabac_encode_init( &cb, dst, dst+0xff0 );\
2201     for( int i = 0; i < 0x1000; i++ )\
2202         x264_cabac_encode_terminal_##cpu( &cb );\
2203 }
2204 DECL_CABAC(c)
2205 #if HAVE_MMX
2206 DECL_CABAC(asm)
2207 #else
2208 #define run_cabac_decision_asm run_cabac_decision_c
2209 #define run_cabac_bypass_asm run_cabac_bypass_c
2210 #define run_cabac_terminal_asm run_cabac_terminal_c
2211 #endif
2212
2213 static int check_cabac( int cpu_ref, int cpu_new )
2214 {
2215     int ret = 0, ok, used_asm = 1;
2216     x264_t h;
2217     h.sps->i_chroma_format_idc = 3;
2218     if( cpu_ref || run_cabac_decision_c == run_cabac_decision_asm )
2219         return 0;
2220     x264_cabac_init( &h );
2221
2222     set_func_name( "cabac_encode_decision" );
2223     memcpy( buf4, buf3, 0x1000 );
2224     call_c( run_cabac_decision_c, &h, buf3 );
2225     call_a( run_cabac_decision_asm, &h, buf4 );
2226     ok = !memcmp( buf3, buf4, 0x1000 );
2227     report( "cabac decision:" );
2228
2229     set_func_name( "cabac_encode_bypass" );
2230     memcpy( buf4, buf3, 0x1000 );
2231     call_c( run_cabac_bypass_c, &h, buf3 );
2232     call_a( run_cabac_bypass_asm, &h, buf4 );
2233     ok = !memcmp( buf3, buf4, 0x1000 );
2234     report( "cabac bypass:" );
2235
2236     set_func_name( "cabac_encode_terminal" );
2237     memcpy( buf4, buf3, 0x1000 );
2238     call_c( run_cabac_terminal_c, &h, buf3 );
2239     call_a( run_cabac_terminal_asm, &h, buf4 );
2240     ok = !memcmp( buf3, buf4, 0x1000 );
2241     report( "cabac terminal:" );
2242
2243     return ret;
2244 }
2245
2246 static int check_bitstream( int cpu_ref, int cpu_new )
2247 {
2248     x264_bitstream_function_t bs_c;
2249     x264_bitstream_function_t bs_ref;
2250     x264_bitstream_function_t bs_a;
2251
2252     int ret = 0, ok = 1, used_asm = 0;
2253
2254     x264_bitstream_init( 0, &bs_c );
2255     x264_bitstream_init( cpu_ref, &bs_ref );
2256     x264_bitstream_init( cpu_new, &bs_a );
2257     if( bs_a.nal_escape != bs_ref.nal_escape )
2258     {
2259         int size = 0x4000;
2260         uint8_t *input = malloc(size+100);
2261         uint8_t *output1 = malloc(size*2);
2262         uint8_t *output2 = malloc(size*2);
2263         used_asm = 1;
2264         set_func_name( "nal_escape" );
2265         for( int i = 0; i < 100; i++ )
2266         {
2267             /* Test corner-case sizes */
2268             int test_size = i < 10 ? i+1 : rand() & 0x3fff;
2269             /* Test 8 different probability distributions of zeros */
2270             for( int j = 0; j < test_size+32; j++ )
2271                 input[j] = (rand()&((1 << ((i&7)+1)) - 1)) * rand();
2272             uint8_t *end_c = (uint8_t*)call_c1( bs_c.nal_escape, output1, input, input+test_size );
2273             uint8_t *end_a = (uint8_t*)call_a1( bs_a.nal_escape, output2, input, input+test_size );
2274             int size_c = end_c-output1;
2275             int size_a = end_a-output2;
2276             if( size_c != size_a || memcmp( output1, output2, size_c ) )
2277             {
2278                 fprintf( stderr, "nal_escape :  [FAILED] %d %d\n", size_c, size_a );
2279                 ok = 0;
2280                 break;
2281             }
2282         }
2283         for( int j = 0; j < size+32; j++ )
2284             input[j] = rand();
2285         call_c2( bs_c.nal_escape, output1, input, input+size );
2286         call_a2( bs_a.nal_escape, output2, input, input+size );
2287         free(input);
2288         free(output1);
2289         free(output2);
2290     }
2291     report( "nal escape:" );
2292
2293     return ret;
2294 }
2295
2296 static int check_all_funcs( int cpu_ref, int cpu_new )
2297 {
2298     return check_pixel( cpu_ref, cpu_new )
2299          + check_dct( cpu_ref, cpu_new )
2300          + check_mc( cpu_ref, cpu_new )
2301          + check_intra( cpu_ref, cpu_new )
2302          + check_deblock( cpu_ref, cpu_new )
2303          + check_quant( cpu_ref, cpu_new )
2304          + check_cabac( cpu_ref, cpu_new )
2305          + check_bitstream( cpu_ref, cpu_new );
2306 }
2307
2308 static int add_flags( int *cpu_ref, int *cpu_new, int flags, const char *name )
2309 {
2310     *cpu_ref = *cpu_new;
2311     *cpu_new |= flags;
2312 #if BROKEN_STACK_ALIGNMENT
2313     *cpu_new |= X264_CPU_STACK_MOD4;
2314 #endif
2315     if( *cpu_new & X264_CPU_SSE2_IS_FAST )
2316         *cpu_new &= ~X264_CPU_SSE2_IS_SLOW;
2317     if( !quiet )
2318         fprintf( stderr, "x264: %s\n", name );
2319     return check_all_funcs( *cpu_ref, *cpu_new );
2320 }
2321
2322 static int check_all_flags( void )
2323 {
2324     int ret = 0;
2325     int cpu0 = 0, cpu1 = 0;
2326 #if HAVE_MMX
2327     if( x264_cpu_detect() & X264_CPU_MMX2 )
2328     {
2329         ret |= add_flags( &cpu0, &cpu1, X264_CPU_MMX | X264_CPU_MMX2, "MMX" );
2330         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "MMX Cache64" );
2331         cpu1 &= ~X264_CPU_CACHELINE_64;
2332 #if ARCH_X86
2333         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_32, "MMX Cache32" );
2334         cpu1 &= ~X264_CPU_CACHELINE_32;
2335 #endif
2336         if( x264_cpu_detect() & X264_CPU_LZCNT )
2337         {
2338             ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "MMX_LZCNT" );
2339             cpu1 &= ~X264_CPU_LZCNT;
2340         }
2341         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "MMX SlowCTZ" );
2342         cpu1 &= ~X264_CPU_SLOW_CTZ;
2343     }
2344     if( x264_cpu_detect() & X264_CPU_SSE2 )
2345     {
2346         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE | X264_CPU_SSE2 | X264_CPU_SSE2_IS_SLOW, "SSE2Slow" );
2347         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE2_IS_FAST, "SSE2Fast" );
2348         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSE2Fast Cache64" );
2349         cpu1 &= ~X264_CPU_CACHELINE_64;
2350         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSE2 FastShuffle" );
2351         cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
2352         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSE2 SlowCTZ" );
2353         cpu1 &= ~X264_CPU_SLOW_CTZ;
2354         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSE2 SlowAtom" );
2355         cpu1 &= ~X264_CPU_SLOW_ATOM;
2356     }
2357     if( x264_cpu_detect() & X264_CPU_SSE_MISALIGN )
2358     {
2359         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE_MISALIGN, "SSE_Misalign" );
2360         cpu1 &= ~X264_CPU_SSE_MISALIGN;
2361     }
2362     if( x264_cpu_detect() & X264_CPU_LZCNT )
2363     {
2364         ret |= add_flags( &cpu0, &cpu1, X264_CPU_LZCNT, "SSE_LZCNT" );
2365         cpu1 &= ~X264_CPU_LZCNT;
2366     }
2367     if( x264_cpu_detect() & X264_CPU_SSE3 )
2368     {
2369         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE3 | X264_CPU_CACHELINE_64, "SSE3" );
2370         cpu1 &= ~X264_CPU_CACHELINE_64;
2371     }
2372     if( x264_cpu_detect() & X264_CPU_SSSE3 )
2373     {
2374         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSSE3, "SSSE3" );
2375         ret |= add_flags( &cpu0, &cpu1, X264_CPU_CACHELINE_64, "SSSE3 Cache64" );
2376         cpu1 &= ~X264_CPU_CACHELINE_64;
2377         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SHUFFLE_IS_FAST, "SSSE3 FastShuffle" );
2378         cpu1 &= ~X264_CPU_SHUFFLE_IS_FAST;
2379         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_CTZ, "SSSE3 SlowCTZ" );
2380         cpu1 &= ~X264_CPU_SLOW_CTZ;
2381         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SLOW_ATOM, "SSSE3 SlowAtom" );
2382         cpu1 &= ~X264_CPU_SLOW_ATOM;
2383     }
2384     if( x264_cpu_detect() & X264_CPU_SSE4 )
2385         ret |= add_flags( &cpu0, &cpu1, X264_CPU_SSE4 | X264_CPU_SHUFFLE_IS_FAST, "SSE4" );
2386     if( x264_cpu_detect() & X264_CPU_AVX )
2387         ret |= add_flags( &cpu0, &cpu1, X264_CPU_AVX, "AVX" );
2388     if( x264_cpu_detect() & X264_CPU_XOP )
2389         ret |= add_flags( &cpu0, &cpu1, X264_CPU_XOP, "XOP" );
2390     if( x264_cpu_detect() & X264_CPU_FMA4 )
2391         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FMA4, "FMA4" );
2392 #elif ARCH_PPC
2393     if( x264_cpu_detect() & X264_CPU_ALTIVEC )
2394     {
2395         fprintf( stderr, "x264: ALTIVEC against C\n" );
2396         ret = check_all_funcs( 0, X264_CPU_ALTIVEC );
2397     }
2398 #elif ARCH_ARM
2399     if( x264_cpu_detect() & X264_CPU_ARMV6 )
2400         ret |= add_flags( &cpu0, &cpu1, X264_CPU_ARMV6, "ARMv6" );
2401     if( x264_cpu_detect() & X264_CPU_NEON )
2402         ret |= add_flags( &cpu0, &cpu1, X264_CPU_NEON, "NEON" );
2403     if( x264_cpu_detect() & X264_CPU_FAST_NEON_MRC )
2404         ret |= add_flags( &cpu0, &cpu1, X264_CPU_FAST_NEON_MRC, "Fast NEON MRC" );
2405 #endif
2406     return ret;
2407 }
2408
2409 int main(int argc, char *argv[])
2410 {
2411     int ret = 0;
2412
2413     if( argc > 1 && !strncmp( argv[1], "--bench", 7 ) )
2414     {
2415 #if !ARCH_X86 && !ARCH_X86_64 && !ARCH_PPC && !ARCH_ARM
2416         fprintf( stderr, "no --bench for your cpu until you port rdtsc\n" );
2417         return 1;
2418 #endif
2419         do_bench = 1;
2420         if( argv[1][7] == '=' )
2421         {
2422             bench_pattern = argv[1]+8;
2423             bench_pattern_len = strlen(bench_pattern);
2424         }
2425         argc--;
2426         argv++;
2427     }
2428
2429     int seed = ( argc > 1 ) ? atoi(argv[1]) : x264_mdate();
2430     fprintf( stderr, "x264: using random seed %u\n", seed );
2431     srand( seed );
2432
2433     buf1 = x264_malloc( 0x1e00 + 0x2000*sizeof(pixel) + 16*BENCH_ALIGNS );
2434     pbuf1 = x264_malloc( 0x1e00*sizeof(pixel) + 16*BENCH_ALIGNS );
2435     if( !buf1 || !pbuf1 )
2436     {
2437         fprintf( stderr, "malloc failed, unable to initiate tests!\n" );
2438         return -1;
2439     }
2440 #define INIT_POINTER_OFFSETS\
2441     buf2 = buf1 + 0xf00;\
2442     buf3 = buf2 + 0xf00;\
2443     buf4 = buf3 + 0x1000*sizeof(pixel);\
2444     pbuf2 = pbuf1 + 0xf00;\
2445     pbuf3 = (pixel*)buf3;\
2446     pbuf4 = (pixel*)buf4;
2447     INIT_POINTER_OFFSETS;
2448     for( int i = 0; i < 0x1e00; i++ )
2449     {
2450         buf1[i] = rand() & 0xFF;
2451         pbuf1[i] = rand() & PIXEL_MAX;
2452     }
2453     memset( buf1+0x1e00, 0, 0x2000*sizeof(pixel) );
2454
2455     /* 16-byte alignment is guaranteed whenever it's useful, but some functions also vary in speed depending on %64 */
2456     if( do_bench )
2457         for( int i = 0; i < BENCH_ALIGNS && !ret; i++ )
2458         {
2459             INIT_POINTER_OFFSETS;
2460             ret |= x264_stack_pagealign( check_all_flags, i*16 );
2461             buf1 += 16;
2462             pbuf1 += 16;
2463             quiet = 1;
2464             fprintf( stderr, "%d/%d\r", i+1, BENCH_ALIGNS );
2465         }
2466     else
2467         ret = check_all_flags();
2468
2469     if( ret )
2470     {
2471         fprintf( stderr, "x264: at least one test has failed. Go and fix that Right Now!\n" );
2472         return -1;
2473     }
2474     fprintf( stderr, "x264: All tests passed Yeah :)\n" );
2475     if( do_bench )
2476         print_bench();
2477     return 0;
2478 }
2479