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