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